pointvec/desktop/sources/scripts/dotgrid.js

416 lines
16 KiB
JavaScript
Raw Normal View History

2018-03-07 03:08:34 +00:00
function Dotgrid(width,height,grid_x,grid_y,block_x,block_y)
2016-12-31 15:00:57 +00:00
{
2018-01-12 08:09:26 +00:00
this.controller = new Controller();
2017-11-07 02:10:09 +00:00
this.theme = new Theme();
2017-11-14 21:11:09 +00:00
this.interface = new Interface();
2018-01-31 09:10:48 +00:00
this.history = new History();
this.guide = new Guide();
this.render = new Render();
this.tool = new Tool();
2018-03-07 00:50:41 +00:00
this.picker = new Picker();
2017-11-07 02:10:09 +00:00
2016-12-31 17:18:01 +00:00
this.grid_x = grid_x;
this.grid_y = grid_y;
2016-12-31 20:37:13 +00:00
this.block_x = block_x;
this.block_y = block_y;
2016-12-31 21:35:14 +00:00
2018-05-08 21:33:17 +00:00
this.cursor = { pos:{x:0,y:0},translation:null,multi:false,updated:0 }
2018-05-07 23:32:00 +00:00
2016-12-31 15:00:57 +00:00
this.install = function()
2018-01-12 03:22:50 +00:00
{
2018-05-08 09:05:19 +00:00
document.getElementById("app").appendChild(this.guide.el);
2017-11-05 00:52:05 +00:00
2017-11-07 02:10:09 +00:00
this.theme.start();
2018-03-06 22:57:52 +00:00
this.tool.start();
2017-11-09 19:47:06 +00:00
this.guide.start();
2017-11-14 21:11:09 +00:00
this.interface.start();
2018-01-12 08:09:26 +00:00
2018-01-13 03:24:18 +00:00
this.controller.add("default","*","About",() => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Dotgrid'); },"CmdOrCtrl+,");
this.controller.add("default","*","Fullscreen",() => { app.toggle_fullscreen(); },"CmdOrCtrl+Enter");
this.controller.add("default","*","Hide",() => { app.toggle_visible(); },"CmdOrCtrl+H");
this.controller.add("default","*","Inspect",() => { app.inspect(); },"CmdOrCtrl+.");
this.controller.add("default","*","Documentation",() => { dotgrid.controller.docs(); },"CmdOrCtrl+Esc");
2018-01-12 20:00:53 +00:00
this.controller.add("default","*","Reset",() => { dotgrid.reset(); dotgrid.theme.reset(); },"CmdOrCtrl+Backspace");
this.controller.add("default","*","Quit",() => { app.exit(); },"CmdOrCtrl+Q");
2018-01-12 08:46:09 +00:00
this.controller.add("default","File","New",() => { dotgrid.new(); },"CmdOrCtrl+N");
this.controller.add("default","File","Open",() => { dotgrid.open(); },"CmdOrCtrl+O");
this.controller.add("default","File","Save",() => { dotgrid.save(); },"CmdOrCtrl+S");
2018-01-13 20:11:57 +00:00
this.controller.add("default","Edit","Copy",() => { document.execCommand('copy'); },"CmdOrCtrl+C");
2018-02-07 06:44:18 +00:00
this.controller.add("default","Edit","Cut",() => { document.execCommand('cut'); },"CmdOrCtrl+X");
2018-01-13 20:11:57 +00:00
this.controller.add("default","Edit","Paste",() => { document.execCommand('paste'); },"CmdOrCtrl+V");
2018-02-06 03:27:48 +00:00
this.controller.add("default","Edit","Undo",() => { dotgrid.tool.undo(); },"CmdOrCtrl+Z");
this.controller.add("default","Edit","Redo",() => { dotgrid.tool.redo(); },"CmdOrCtrl+Shift+Z");
this.controller.add("default","Edit","Delete",() => { dotgrid.tool.remove_segment(); },"Backspace");
2018-02-06 01:23:25 +00:00
this.controller.add("default","Edit","Deselect",() => { dotgrid.tool.clear(); },"Esc");
2018-01-12 08:46:09 +00:00
this.controller.add("default","Stroke","Line",() => { dotgrid.tool.cast("line"); },"A");
2018-02-06 03:27:48 +00:00
this.controller.add("default","Stroke","Arc",() => { dotgrid.tool.cast("arc_c"); },"S"); // 0,1
this.controller.add("default","Stroke","Arc Rev",() => { dotgrid.tool.cast("arc_r")},"D"); // 0,0
this.controller.add("default","Stroke","Bezier",() => { dotgrid.tool.cast("bezier") },"F");
this.controller.add("default","Stroke","Connect",() => { dotgrid.tool.cast("close") },"Z");
2018-01-12 08:46:09 +00:00
2018-02-06 18:42:34 +00:00
this.controller.add("default","Effect","Linecap",() => { dotgrid.mod_linecap(); },"Q");
this.controller.add("default","Effect","Linejoin",() => { dotgrid.mod_linejoin(); },"W");
this.controller.add("default","Effect","Mirror",() => { dotgrid.mod_mirror(); },"E");
2018-03-07 07:43:46 +00:00
this.controller.add("default","Effect","Fill",() => { dotgrid.mod_fill(); },"R");
2018-03-21 07:10:09 +00:00
this.controller.add("default","Effect","Color",() => { dotgrid.picker.start(); },"G");
2018-01-29 20:48:58 +00:00
this.controller.add("default","Effect","Thicker",() => { dotgrid.mod_thickness(1) },"}");
this.controller.add("default","Effect","Thinner",() => { dotgrid.mod_thickness(-1) },"{");
this.controller.add("default","Effect","Thicker +5",() => { dotgrid.mod_thickness(5,true) },"]");
this.controller.add("default","Effect","Thinner -5",() => { dotgrid.mod_thickness(-5,true) },"[");
2018-02-06 18:42:34 +00:00
2018-05-10 20:46:50 +00:00
this.controller.add("default","Manual","Add Point",() => { dotgrid.tool.add_vertex(dotgrid.cursor.pos); dotgrid.guide.refresh() },"Enter");
this.controller.add("default","Manual","Move Up",() => { dotgrid.cursor.pos.y -= 15; dotgrid.guide.refresh() },"Up");
this.controller.add("default","Manual","Move Right",() => { dotgrid.cursor.pos.x -= 15; dotgrid.guide.refresh() },"Right");
this.controller.add("default","Manual","Move Down",() => { dotgrid.cursor.pos.y += 15; dotgrid.guide.refresh() },"Down");
this.controller.add("default","Manual","Move Left",() => { dotgrid.cursor.pos.x += 15; dotgrid.guide.refresh() },"Left");
2018-05-07 23:32:00 +00:00
this.controller.add("default","Manual","Remove Point",() => { dotgrid.tool.remove_segments_at(dotgrid.cursor.pos); },"CmdOrCtrl+Backspace");
2018-03-06 20:32:31 +00:00
this.controller.add("default","Layers","Foreground",() => { dotgrid.tool.select_layer(0) },"CmdOrCtrl+1");
this.controller.add("default","Layers","Middleground",() => { dotgrid.tool.select_layer(1) },"CmdOrCtrl+2");
this.controller.add("default","Layers","Background",() => { dotgrid.tool.select_layer(2) },"CmdOrCtrl+3");
2018-02-06 18:42:34 +00:00
2018-01-29 20:48:58 +00:00
this.controller.add("default","View","Tools",() => { dotgrid.interface.toggle(); },"U");
2018-01-13 03:24:18 +00:00
this.controller.add("default","View","Grid",() => { dotgrid.guide.toggle(); },"H");
2018-01-12 08:46:09 +00:00
2018-03-07 00:50:41 +00:00
this.controller.add("default","Mode","Picker",() => { dotgrid.picker.start(); },"CmdOrCtrl+P");
2018-02-19 02:29:07 +00:00
2018-03-07 00:50:41 +00:00
this.controller.add("picker","*","About",() => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Dotgrid'); },"CmdOrCtrl+,");
this.controller.add("picker","*","Fullscreen",() => { app.toggle_fullscreen(); },"CmdOrCtrl+Enter");
this.controller.add("picker","*","Hide",() => { app.toggle_visible(); },"CmdOrCtrl+H");
this.controller.add("picker","*","Inspect",() => { app.inspect(); },"CmdOrCtrl+.");
this.controller.add("picker","*","Documentation",() => { dotgrid.controller.docs(); },"CmdOrCtrl+Esc");
this.controller.add("picker","*","Reset",() => { dotgrid.reset(); dotgrid.theme.reset(); },"CmdOrCtrl+Backspace");
this.controller.add("picker","*","Quit",() => { app.exit(); },"CmdOrCtrl+Q");
this.controller.add_role("picker","Edit","undo");
this.controller.add_role("picker","Edit","redo");
this.controller.add_role("picker","Edit","cut");
this.controller.add_role("picker","Edit","copy");
this.controller.add_role("picker","Edit","paste");
this.controller.add_role("picker","Edit","delete");
this.controller.add_role("picker","Edit","selectall");
this.controller.add("picker","Mode","Stop Picker Mode",() => { dotgrid.picker.stop(); },"Escape");
2018-02-19 02:29:07 +00:00
2018-01-12 08:09:26 +00:00
this.controller.commit();
2017-11-12 20:26:23 +00:00
2018-01-13 20:11:57 +00:00
document.addEventListener('mousedown', function(e){ dotgrid.mouse_down(e); }, false);
document.addEventListener('mousemove', function(e){ dotgrid.mouse_move(e); }, false);
2018-02-06 18:18:43 +00:00
document.addEventListener('contextmenu', function(e){ dotgrid.mouse_alt(e); }, false);
2018-01-13 20:11:57 +00:00
document.addEventListener('mouseup', function(e){ dotgrid.mouse_up(e);}, false);
document.addEventListener('copy', function(e){ dotgrid.copy(e); e.preventDefault(); }, false);
2018-02-07 06:44:18 +00:00
document.addEventListener('cut', function(e){ dotgrid.cut(e); e.preventDefault(); }, false);
2018-01-13 20:11:57 +00:00
document.addEventListener('paste', function(e){ dotgrid.paste(e); e.preventDefault(); }, false);
2018-01-12 03:22:50 +00:00
window.addEventListener('drop', dotgrid.drag);
dotgrid.set_size({width:300,height:300});
2018-01-31 09:10:48 +00:00
this.new();
2016-12-31 17:18:01 +00:00
}
2018-05-07 05:03:35 +00:00
// File
2018-01-12 08:46:09 +00:00
2018-01-12 08:09:26 +00:00
this.new = function()
{
2018-05-07 04:51:58 +00:00
this.set_size({width:300,height:300})
2018-02-06 03:27:48 +00:00
this.history.push(this.tool.layers);
2018-05-07 05:03:35 +00:00
this.clear();
2018-01-12 08:09:26 +00:00
}
this.save = function()
{
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-01-12 08:09:26 +00:00
dialog.showSaveDialog((fileName) => {
if (fileName === undefined){ return; }
2018-05-10 20:46:50 +00:00
fs.writeFile(fileName+".svg", dotgrid.render.to_svg());
2018-05-10 21:05:46 +00:00
fs.writeFile(fileName+'.png', dotgrid.render.to_png());
2018-04-09 04:50:52 +00:00
fs.writeFile(fileName+'.grid', dotgrid.tool.export());
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh()
2018-01-12 08:09:26 +00:00
});
}
this.open = function()
{
2018-04-09 04:50:52 +00:00
var paths = dialog.showOpenDialog({properties: ['openFile'],filters:[{name:"Dotgrid Image",extensions:["dot","grid"]}]});
2018-01-12 08:09:26 +00:00
if(!paths){ console.log("Nothing to load"); return; }
fs.readFile(paths[0], 'utf-8', (err, data) => {
if(err){ alert("An error ocurred reading the file :" + err.message); return; }
2018-02-07 20:34:17 +00:00
dotgrid.tool.replace(JSON.parse(data.toString().trim()));
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-01-12 08:09:26 +00:00
});
}
2016-12-31 17:18:01 +00:00
// Cursor
this.mouse_down = function(e)
{
2018-05-08 21:33:17 +00:00
var o = e.target.getAttribute("ar");
if(o){
if(o == "line"){ this.tool.cast("line"); return; }
if(o == "arc_c"){ this.tool.cast("arc_c"); return;}
if(o == "arc_r"){ this.tool.cast("arc_r"); return; }
if(o == "bezier"){ this.tool.cast("bezier"); return; }
if(o == "close"){ this.tool.cast("close"); return; }
if(o == "thickness"){ this.mod_thickness(); return; }
if(o == "linecap"){ this.mod_linecap(); return; }
if(o == "linejoin"){ this.mod_linejoin(); return; }
if(o == "mirror"){ this.mod_mirror(); return; }
if(o == "fill"){ this.mod_fill(); return; }
if(o == "color"){ setTimeout(()=>{ this.picker.start(); }, 100); return; }
if(o == "depth"){ this.tool.select_next_layer(); return; }
e.preventDefault();
}
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5});
pos = this.position_on_grid(pos);
2017-11-13 18:52:53 +00:00
2018-02-06 04:19:34 +00:00
if(e.altKey){ dotgrid.tool.remove_segments_at(pos); return; }
2018-03-21 07:35:28 +00:00
if(dotgrid.tool.vertex_at(pos)){
2018-05-07 23:32:00 +00:00
console.log("Begin translation"); dotgrid.cursor.translation = {from:pos,to:pos};
if(e.shiftKey){ console.log("Begin translation(multi)"); dotgrid.cursor.multi = true; }
2018-03-21 07:35:28 +00:00
}
2016-12-31 17:18:01 +00:00
}
this.mouse_move = function(e)
{
2018-03-07 03:08:34 +00:00
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5}); pos = this.position_on_grid(pos);
2016-12-31 17:18:01 +00:00
2018-05-07 23:32:00 +00:00
this.cursor.pos = pos;
2018-05-08 21:33:17 +00:00
this.cursor.updated = new Date().getTime();
2018-05-10 07:49:41 +00:00
this.cursor.operation = e.target.getAttribute("ar");
2018-05-07 23:32:00 +00:00
if(dotgrid.cursor.translation && (Math.abs(dotgrid.cursor.translation.from.x) != Math.abs(pos.x) || Math.abs(dotgrid.cursor.translation.from.y) != Math.abs(pos.y))){ dotgrid.cursor.translation.to = pos; }
2017-11-13 03:54:56 +00:00
2018-05-10 07:49:41 +00:00
dotgrid.guide.refresh();
2018-03-07 02:45:41 +00:00
e.preventDefault();
2016-12-31 17:18:01 +00:00
}
this.mouse_up = function(e)
{
2018-05-08 21:33:17 +00:00
if(e.target.getAttribute("ar")){ return } // If clicking on interface
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5});
pos = this.position_on_grid(pos);
2017-11-07 11:04:47 +00:00
2018-05-10 07:49:41 +00:00
if(e.altKey || e.target.id != "guide"){ return; }
2017-11-13 18:52:53 +00:00
2018-05-07 23:32:00 +00:00
if(pos.x > 0) { dotgrid.cursor.translation = null; return; }
2017-11-14 01:28:06 +00:00
2018-05-07 23:32:00 +00:00
if(dotgrid.cursor.translation && (Math.abs(dotgrid.cursor.translation.from.x) != Math.abs(dotgrid.cursor.translation.to.x) || Math.abs(dotgrid.cursor.translation.from.y) != Math.abs(dotgrid.cursor.translation.to.y))){
if(dotgrid.cursor.multi){
dotgrid.tool.translate_multi(dotgrid.cursor.translation.from,dotgrid.cursor.translation.to);
2018-03-21 07:35:28 +00:00
}
else{
2018-05-07 23:32:00 +00:00
dotgrid.tool.translate(dotgrid.cursor.translation.from,dotgrid.cursor.translation.to);
2018-03-21 07:35:28 +00:00
}
2018-05-07 23:32:00 +00:00
dotgrid.cursor.translation = null;
dotgrid.cursor.multi = null;
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-03-07 02:45:41 +00:00
e.preventDefault();
2017-11-13 03:54:56 +00:00
return;
}
this.tool.add_vertex({x:pos.x * -1,y:pos.y});
2018-05-07 23:32:00 +00:00
dotgrid.cursor.translation = null;
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-03-07 02:45:41 +00:00
e.preventDefault();
2017-11-17 06:45:15 +00:00
}
2018-02-06 18:18:43 +00:00
this.mouse_alt = function(e)
{
2018-03-07 03:08:34 +00:00
var pos = this.position_in_grid({x:e.clientX+5,y:e.clientY-5}); pos = this.position_on_grid(pos);
2018-02-06 18:18:43 +00:00
dotgrid.tool.remove_segments_at(pos);
2018-02-17 01:42:52 +00:00
e.preventDefault();
2018-02-06 18:18:43 +00:00
2018-05-07 05:03:35 +00:00
setTimeout(() => { dotgrid.tool.clear(); },150);
2018-04-02 21:48:36 +00:00
}
2018-02-06 04:25:57 +00:00
// Toggles
2018-01-29 20:48:58 +00:00
this.mod_thickness = function(mod,step = false)
2017-11-04 23:59:11 +00:00
{
2018-03-06 22:26:18 +00:00
if(!mod){ mod = 1; this.tool.style().thickness = this.tool.style().thickness > 30 ? 1 : this.tool.style().thickness }
2017-11-14 21:11:09 +00:00
2018-01-29 20:48:58 +00:00
if(step){
2018-03-06 22:26:18 +00:00
this.tool.style().thickness = parseInt(this.tool.style().thickness/5) * 5;
2018-01-29 20:48:58 +00:00
}
2018-03-06 22:26:18 +00:00
this.tool.style().thickness = Math.max(this.tool.style().thickness+mod,0);
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-04 23:59:11 +00:00
}
this.mod_linecap_index = 1;
this.mod_linecap = function(mod)
{
var a = ["butt","square","round"];
this.mod_linecap_index += 1;
2018-03-06 22:37:01 +00:00
this.tool.style().strokeLinecap = a[this.mod_linecap_index % a.length];
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-04 23:59:11 +00:00
}
2018-02-04 22:45:58 +00:00
this.mod_linejoin_index = 1;
this.mod_linejoin = function(mod)
{
var a = ["miter","round","bevel"];
this.mod_linejoin_index += 1;
2018-03-06 22:37:01 +00:00
this.tool.style().strokeLinejoin = a[this.mod_linejoin_index % a.length];
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-02-04 22:45:58 +00:00
}
2017-11-10 19:46:11 +00:00
this.mod_mirror = function()
{
2018-04-16 04:24:09 +00:00
this.tool.style().mirror_style += 1;
this.tool.style().mirror_style = this.tool.style().mirror_style > 7 ? 0 : this.tool.style().mirror_style;
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-10 19:46:11 +00:00
}
2018-03-07 07:43:46 +00:00
this.mod_fill = function()
{
this.tool.style().fill = this.tool.style().fill == "none" ? this.tool.style().color : "none";
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-03-07 07:43:46 +00:00
}
2018-05-07 05:03:35 +00:00
// Basics
2018-05-07 04:51:58 +00:00
2018-01-08 07:53:54 +00:00
this.set_size = function(size = {width:300,height:300},interface = true)
2017-11-21 22:58:07 +00:00
{
2018-05-07 04:51:58 +00:00
size = { width:clamp(parseInt(size.width/15)*15,100,1000),height:clamp(parseInt(size.height/15)*15,100,1000)}
2018-05-06 23:15:23 +00:00
2017-11-21 22:58:07 +00:00
var win = require('electron').remote.getCurrentWindow();
2018-01-08 07:53:54 +00:00
win.setSize(size.width+100,size.height+100+(interface ? 10 : 0),true);
2017-11-21 22:58:07 +00:00
2018-05-07 22:47:09 +00:00
this.tool.settings.size.width = size.width
this.tool.settings.size.height = size.height
2018-05-10 08:01:31 +00:00
2017-11-21 22:58:07 +00:00
this.grid_x = size.width/15
this.grid_y = size.height/15
2018-05-07 04:24:01 +00:00
2018-05-07 22:47:09 +00:00
this.grid_width = this.tool.settings.size.width/this.grid_x;
this.grid_height = this.tool.settings.size.height/this.grid_y;
2018-05-07 04:51:58 +00:00
2017-11-21 22:58:07 +00:00
dotgrid.guide.resize(size);
this.interface.update();
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-04 23:59:11 +00:00
}
2016-12-31 17:18:01 +00:00
// Draw
2018-01-12 08:46:09 +00:00
2016-12-31 17:37:10 +00:00
this.reset = function()
2016-12-31 17:18:01 +00:00
{
2018-02-06 19:16:01 +00:00
this.tool.clear();
2016-12-31 17:18:01 +00:00
}
2017-11-07 21:40:13 +00:00
this.clear = function()
{
2018-02-04 21:09:46 +00:00
this.history.clear();
2018-02-06 19:16:01 +00:00
this.tool.reset();
2017-11-07 21:40:13 +00:00
this.reset();
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-07 21:40:13 +00:00
}
2018-01-12 03:22:50 +00:00
this.drag = function(e)
{
e.preventDefault();
e.stopPropagation();
var file = e.dataTransfer.files[0];
2018-04-09 04:50:52 +00:00
if(!file.path || file.path.indexOf(".dot") < 0 && file.path.indexOf(".grid") < 0){ console.log("Dotgrid","Not a dot file"); return; }
2018-01-12 03:22:50 +00:00
var reader = new FileReader();
reader.onload = function(e){
2018-02-07 20:34:17 +00:00
dotgrid.tool.replace(JSON.parse(e.target.result.toString().trim()));
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-01-12 03:22:50 +00:00
};
reader.readAsText(file);
}
2017-11-21 21:24:25 +00:00
this.copy = function(e)
{
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-01-13 20:11:57 +00:00
2018-03-07 02:57:54 +00:00
e.clipboardData.setData('text/source', dotgrid.tool.export(dotgrid.tool.layer()));
e.clipboardData.setData('text/plain', dotgrid.tool.path());
2018-05-10 20:46:50 +00:00
e.clipboardData.setData('text/html', dotgrid.render.to_svg());
e.clipboardData.setData('text/svg+xml', dotgrid.render.to_svg());
2018-02-07 06:44:18 +00:00
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-02-07 06:44:18 +00:00
}
this.cut = function(e)
{
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2018-02-07 06:44:18 +00:00
e.clipboardData.setData('text/plain', dotgrid.tool.export(dotgrid.tool.layer()));
2018-05-10 20:46:50 +00:00
e.clipboardData.setData('text/html', dotgrid.render.to_svg());
e.clipboardData.setData('text/svg+xml', dotgrid.render.to_svg());
2018-02-07 06:44:18 +00:00
dotgrid.tool.layers[dotgrid.tool.index] = [];
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-21 21:24:25 +00:00
}
this.paste = function(e)
{
2018-03-07 02:57:54 +00:00
var data = e.clipboardData.getData("text/source");
2018-05-08 01:32:06 +00:00
if(is_json(data)){
data = JSON.parse(data.trim());
dotgrid.tool.import(data);
}
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh();
2017-11-21 21:24:25 +00:00
}
2016-12-31 17:18:01 +00:00
// Normalizers
2017-11-07 11:04:47 +00:00
this.position_in_grid = function(pos)
2016-12-31 17:18:01 +00:00
{
2018-05-07 22:47:09 +00:00
return {x:(window.innerWidth/2) - (this.tool.settings.size.width/2) - pos.x,y:pos.y - (30+10)}
2016-12-31 17:18:01 +00:00
}
2017-11-13 03:54:56 +00:00
this.position_on_grid = function(pos)
2016-12-31 17:18:01 +00:00
{
2017-11-14 21:57:17 +00:00
pos.y = pos.y - 7.5
2017-11-21 22:58:07 +00:00
pos.x = pos.x + 7.5
2017-11-07 11:04:47 +00:00
x = Math.round(pos.x/this.grid_width)*this.grid_width
2017-11-12 21:47:34 +00:00
y = Math.round(pos.y/this.grid_height)*this.grid_height
2018-05-07 04:51:58 +00:00
2018-05-08 09:15:40 +00:00
x = clamp(x * -1,0,this.tool.settings.size.width)
y = clamp(y,0,this.tool.settings.size.height)
return {x:x*-1,y:y};
}
2018-05-08 01:32:06 +00:00
2018-05-08 21:33:17 +00:00
function is_json(text){ try{ JSON.parse(text);return true; } catch(error){ return false; }}
2018-05-07 05:03:35 +00:00
function pos_is_equal(a,b){ return a && b && a.x == b.x && a.y == b.y }
2018-05-07 04:51:58 +00:00
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
2017-11-06 04:35:29 +00:00
}
2017-11-07 02:10:09 +00:00
2017-11-12 21:47:34 +00:00
window.addEventListener('resize', function(e)
{
2018-05-10 20:46:50 +00:00
dotgrid.guide.refresh()
2017-11-12 21:47:34 +00:00
}, false);
2018-01-12 03:22:50 +00:00
window.addEventListener('dragover',function(e)
2017-11-07 02:10:09 +00:00
{
e.stopPropagation();
2018-01-12 03:22:50 +00:00
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
2018-01-13 20:11:57 +00:00
});
2018-03-21 07:10:09 +00:00
String.prototype.capitalize = function()
{
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
2018-05-09 07:22:00 +00:00
}