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