pointvec/desktop/sources/scripts/tool.js

330 lines
8.8 KiB
JavaScript
Raw Normal View History

2018-08-28 00:34:17 -04:00
'use strict';
function Tool()
{
2018-02-05 20:14:23 -05:00
this.index = 0;
2018-05-07 18:47:09 -04:00
this.settings = { size:{width:300,height:300} }
2018-02-06 14:16:01 -05:00
this.layers = [[],[],[]];
2018-03-06 17:37:01 -05:00
this.styles = [
2018-05-11 18:19:24 -04:00
{ thickness:10,strokeLinecap:"round",strokeLinejoin:"round",color:"#f00",fill:"none",mirror_style:0 },
{ thickness:10,strokeLinecap:"round",strokeLinejoin:"round",color:"#0f0",fill:"none",mirror_style:0 },
{ thickness:10,strokeLinecap:"round",strokeLinejoin:"round",color:"#00f",fill:"none",mirror_style:0 }
2018-03-06 17:37:01 -05:00
];
2018-05-09 03:19:24 -04:00
this.vertices = [];
2018-05-07 00:24:01 -04:00
this.reqs = { line:2,arc_c:2,arc_r:2,bezier:3,close:0 };
2018-03-06 17:57:52 -05:00
this.start = function()
{
this.styles[0].color = dotgrid.theme.active.f_high
this.styles[1].color = dotgrid.theme.active.f_med
this.styles[2].color = dotgrid.theme.active.f_low
}
2018-02-06 14:16:01 -05:00
this.reset = function()
{
this.layers = [[],[],[]];
2018-05-09 03:19:24 -04:00
this.vertices = [];
2018-02-06 14:16:01 -05:00
this.index = 0;
}
2018-03-06 22:08:34 -05:00
this.clear = function()
2018-03-06 17:26:18 -05:00
{
2018-05-09 03:19:24 -04:00
this.vertices = [];
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-03-06 17:26:18 -05:00
}
2018-03-06 22:08:34 -05:00
this.undo = function()
2018-02-05 20:14:23 -05:00
{
2018-03-06 22:08:34 -05:00
this.layers = dotgrid.history.prev();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-03-06 22:08:34 -05:00
}
this.redo = function()
{
this.layers = dotgrid.history.next();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-03-06 22:08:34 -05:00
}
2018-08-03 23:55:08 -04:00
this.length = function()
{
return this.layers[0].length + this.layers[1].length + this.layers[2].length
}
2018-03-06 22:08:34 -05:00
// I/O
2018-05-07 00:24:01 -04:00
this.export = function(target = {settings:this.settings,layers:this.layers,styles:this.styles})
2018-03-06 22:08:34 -05:00
{
return JSON.stringify(copy(target), null, 2);
}
this.import = function(layer)
{
this.layers[this.index] = this.layers[this.index].concat(layer)
dotgrid.history.push(this.layers);
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-03-06 22:08:34 -05:00
}
this.replace = function(dot)
{
2018-05-10 03:53:09 -04:00
if(!dot.layers || dot.layers.length != 3){ console.warn("Incompatible version"); return; }
2018-05-18 00:55:19 -04:00
if(dot.settings.width && dot.settings.height){
dot.settings.size = {width:dot.settings.width,height:dot.settings.height}
}
2018-05-07 18:47:09 -04:00
if(this.settings && (this.settings.size.width != dot.settings.size.width || this.settings.size.height != dot.settings.size.height)){
dotgrid.set_size({width:dot.settings.size.width,height:dot.settings.size.height})
2018-05-07 00:51:58 -04:00
}
2018-03-06 22:08:34 -05:00
this.layers = dot.layers;
this.styles = dot.styles;
2018-05-07 00:51:58 -04:00
this.settings = dot.settings;
2018-03-06 22:08:34 -05:00
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-03-06 22:08:34 -05:00
dotgrid.history.push(this.layers);
2018-02-05 20:14:23 -05:00
}
2018-03-06 22:08:34 -05:00
// EDIT
this.remove_segment = function()
{
2018-05-09 03:19:24 -04:00
if(this.vertices.length > 0){ this.clear(); return; }
2018-02-12 15:29:16 -05:00
2018-02-05 20:14:23 -05:00
this.layer().pop();
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
}
2018-02-05 23:19:34 -05:00
this.remove_segments_at = function(pos)
{
2018-08-26 15:39:15 -04:00
for(let segment_id in this.layer()){
let segment = this.layer()[segment_id];
for(let vertex_id in segment.vertices){
let vertex = segment.vertices[vertex_id];
2018-02-05 23:35:25 -05:00
if(Math.abs(pos.x) == Math.abs(vertex.x) && Math.abs(pos.y) == Math.abs(vertex.y)){
2018-05-09 03:19:24 -04:00
segment.vertices.splice(vertex_id,1)
2018-02-05 23:35:25 -05:00
}
}
2018-05-09 03:19:24 -04:00
if(segment.vertices.length < 2){
2018-02-16 20:42:52 -05:00
this.layers[this.index].splice(segment_id,1)
}
2018-02-05 23:35:25 -05:00
}
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-02-05 23:19:34 -05:00
}
this.add_vertex = function(pos)
{
2018-05-07 19:32:00 -04:00
pos = {x:Math.abs(pos.x),y:Math.abs(pos.y)}
2018-05-09 03:19:24 -04:00
this.vertices.push(pos);
2018-09-11 21:20:31 -04:00
dotgrid.interface.update(true);
}
2018-02-06 13:42:34 -05:00
this.vertex_at = function(pos)
{
2018-08-26 15:39:15 -04:00
for(let segment_id in this.layer()){
let segment = this.layer()[segment_id];
for(let vertex_id in segment.vertices){
let vertex = segment.vertices[vertex_id];
2018-02-06 13:42:34 -05:00
if(vertex.x == Math.abs(pos.x) && vertex.y == Math.abs(pos.y)){
return vertex;
}
}
}
return null;
}
this.cast = function(type)
{
2018-02-05 20:14:23 -05:00
if(!this.layer()){ this.layers[this.index] = []; }
2018-02-05 22:47:25 -05:00
if(!this.can_cast(type)){ console.warn("Cannot cast"); return; }
2018-08-26 15:39:15 -04:00
let append_target = this.can_append({type:type,vertices:this.vertices.slice()})
2018-02-10 23:00:22 -05:00
if(append_target){
2018-05-09 03:19:24 -04:00
this.layers[this.index][append_target].vertices = this.layers[this.index][append_target].vertices.concat(this.vertices.slice())
2018-02-10 23:00:22 -05:00
}
else{
2018-05-09 03:19:24 -04:00
this.layer().push({type:type,vertices:this.vertices.slice()})
2018-02-10 23:00:22 -05:00
}
2018-05-10 17:13:01 -04:00
2018-05-10 17:47:09 -04:00
dotgrid.history.push(this.layers);
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-02-05 22:42:14 -05:00
console.log(`Casted ${type} -> ${this.layer().length} elements`);
}
2018-08-02 20:53:26 -04:00
this.i = { linecap:0,linejoin:0,thickness:5 }
this.toggle = function(type,mod = 1)
{
if(type == "linecap"){
2018-08-26 15:39:15 -04:00
let a = ["butt","square","round"];
2018-08-02 20:53:26 -04:00
this.i.linecap += mod;
this.style().strokeLinecap = a[this.i.linecap % a.length];
}
2018-08-02 20:57:36 -04:00
else if(type == "linejoin"){
2018-08-26 15:39:15 -04:00
let a = ["miter","round","bevel"];
2018-08-02 20:53:26 -04:00
this.i.linejoin += mod;
this.style().strokeLinejoin = a[this.i.linejoin % a.length];
}
2018-08-02 20:57:36 -04:00
else if(type == "fill"){
2018-08-02 20:53:26 -04:00
this.style().fill = this.style().fill == "none" ? this.style().color : "none";
}
2018-08-02 20:57:36 -04:00
else if(type == "thickness"){
2018-08-03 22:36:45 -04:00
this.style().thickness = clamp(this.style().thickness+mod,1,100);
2018-08-02 20:53:26 -04:00
}
2018-08-02 20:57:36 -04:00
else if(type == "mirror"){
this.style().mirror_style = this.style().mirror_style > 3 ? 0 : this.style().mirror_style+1;
}
else{
console.warn("Unknown",type)
}
2018-09-11 21:20:31 -04:00
dotgrid.interface.update(true);
dotgrid.guide.update();
2018-08-02 20:53:26 -04:00
}
2018-08-02 20:57:36 -04:00
this.misc = function(type)
{
dotgrid.picker.start();
}
2018-08-03 23:55:08 -04:00
this.source = function(type)
{
if(type == "grid"){ dotgrid.guide.toggle(); }
if(type == "screen"){ app.toggle_fullscreen(); }
if(type == "open"){ dotgrid.open(); }
if(type == "save"){ dotgrid.save(); }
if(type == "render"){ dotgrid.render(); }
if(type == "export"){ dotgrid.export(); }
}
2018-02-10 23:00:22 -05:00
this.can_append = function(content)
{
2018-08-26 15:39:15 -04:00
for(let id in this.layer()){
let stroke = this.layer()[id];
2018-02-10 23:00:22 -05:00
if(stroke.type != content.type){ continue; }
2018-05-18 00:55:19 -04:00
if(!stroke.vertices){ continue; }
2018-05-09 03:19:24 -04:00
if(!stroke.vertices[stroke.vertices.length-1]){ continue; }
if(stroke.vertices[stroke.vertices.length-1].x != content.vertices[0].x){ continue; }
if(stroke.vertices[stroke.vertices.length-1].y != content.vertices[0].y){ continue; }
2018-02-10 23:00:22 -05:00
return id;
}
return false;
}
2018-02-05 22:27:48 -05:00
this.can_cast = function(type)
2018-02-05 22:47:25 -05:00
{
2018-02-06 01:51:09 -05:00
if(!type){ return false; }
2018-02-05 22:47:25 -05:00
// Cannot cast close twice
if(type == "close"){
2018-08-26 15:39:15 -04:00
let prev = this.layer()[this.layer().length-1];
2018-02-06 01:51:09 -05:00
if(!prev || prev.type == "close"){
2018-02-05 22:47:25 -05:00
return false;
}
}
2018-05-08 17:33:17 -04:00
if(type == "bezier"){
2018-06-03 17:52:19 -04:00
if(this.vertices.length != 3 && this.vertices.length != 5 && this.vertices.length != 7 && this.vertices.length != 9){
2018-05-08 17:33:17 -04:00
return false;
}
}
2018-05-09 03:19:24 -04:00
return this.vertices.length >= this.reqs[type];
2018-02-05 22:27:48 -05:00
}
2018-02-06 13:42:34 -05:00
this.paths = function()
{
2018-08-26 15:39:15 -04:00
let l1 = new Generator(dotgrid.tool.layers[0],dotgrid.tool.styles[0]).toString({x:-10,y:-10},1)
let l2 = new Generator(dotgrid.tool.layers[1],dotgrid.tool.styles[1]).toString({x:-10,y:-10},1)
let l3 = new Generator(dotgrid.tool.layers[2],dotgrid.tool.styles[2]).toString({x:-10,y:-10},1)
2018-02-06 13:42:34 -05:00
2018-05-10 16:46:50 -04:00
return [l1,l2,l3]
}
2018-05-10 16:46:50 -04:00
this.path = function()
2018-04-15 22:32:54 -04:00
{
2018-08-02 21:10:57 -04:00
return new Generator(dotgrid.tool.layer(),dotgrid.tool.style()).toString({x:-10,y:-10},1)
2018-04-15 22:32:54 -04:00
}
2018-02-05 20:02:13 -05:00
this.translate = function(a,b)
{
2018-08-26 15:39:15 -04:00
for(let segment_id in this.layer()){
let segment = this.layer()[segment_id];
for(let vertex_id in segment.vertices){
let vertex = segment.vertices[vertex_id];
2018-02-05 20:02:13 -05:00
if(vertex.x == Math.abs(a.x) && vertex.y == Math.abs(a.y)){
2018-05-09 03:19:24 -04:00
segment.vertices[vertex_id] = {x:Math.abs(b.x),y:Math.abs(b.y)};
2018-02-05 20:02:13 -05:00
}
}
}
2018-02-05 22:27:48 -05:00
dotgrid.history.push(this.layers);
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
2018-02-05 20:02:13 -05:00
}
2018-03-21 03:35:28 -04:00
this.translate_multi = function(a,b)
{
2018-08-26 15:39:15 -04:00
let offset = {x:a.x - b.x,y:a.y - b.y}
2018-03-21 03:35:28 -04:00
2018-08-26 15:39:15 -04:00
for(let segment_id in this.layer()){
let segment = this.layer()[segment_id];
for(let vertex_id in segment.vertices){
let vertex = segment.vertices[vertex_id];
2018-08-03 23:55:08 -04:00
segment.vertices[vertex_id] = {x:vertex.x-offset.x,y:vertex.y-offset.y};
2018-03-21 03:35:28 -04:00
}
}
dotgrid.history.push(this.layers);
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
2018-03-21 03:35:28 -04:00
}
2018-03-06 22:08:34 -05:00
// Style
2018-02-06 01:34:45 -05:00
2018-03-06 22:08:34 -05:00
this.style = function()
2018-02-06 01:34:45 -05:00
{
2018-03-06 22:08:34 -05:00
if(!this.styles[this.index]){
this.styles[this.index] = [];
}
return this.styles[this.index];
2018-02-06 01:34:45 -05:00
}
2018-03-06 22:08:34 -05:00
// Layers
2018-02-07 15:34:17 -05:00
2018-03-06 22:08:34 -05:00
this.layer = function()
2018-02-06 01:34:45 -05:00
{
2018-03-06 22:08:34 -05:00
if(!this.layers[this.index]){
this.layers[this.index] = [];
}
return this.layers[this.index];
2018-02-06 01:34:45 -05:00
}
2018-02-06 13:42:34 -05:00
2018-02-12 15:29:16 -05:00
this.select_layer = function(id)
2018-02-06 13:42:34 -05:00
{
2018-02-12 15:29:16 -05:00
this.index = clamp(id,0,2);
2018-02-06 13:42:34 -05:00
this.clear();
2018-09-11 21:20:31 -04:00
dotgrid.guide.update();
dotgrid.interface.update(true);
2018-02-06 13:42:34 -05:00
console.log(`layer:${this.index}`)
}
2018-03-06 17:02:43 -05:00
this.select_next_layer = function()
2018-02-12 15:29:16 -05:00
{
2018-03-06 17:02:43 -05:00
this.index = this.index >= 2 ? 0 : this.index+1
this.select_layer(this.index);
2018-02-06 13:42:34 -05:00
}
2018-02-07 01:44:18 -05:00
function copy(data){ return data ? JSON.parse(JSON.stringify(data)) : []; }
2018-02-12 15:29:16 -05:00
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
2018-05-09 03:19:24 -04:00
}