pointvec/desktop/sources/scripts/tool.js

241 lines
5.8 KiB
JavaScript
Raw Normal View History

function Tool()
{
2018-02-06 01:14:23 +00:00
this.index = 0;
2018-02-06 19:16:01 +00:00
this.layers = [[],[],[]];
this.verteces = [];
2018-02-06 03:42:14 +00:00
this.reqs = {line:2,arc_c:2,arc_r:2,bezier:3,close:0};
2018-02-06 19:16:01 +00:00
this.reset = function()
{
this.layers = [[],[],[]];
this.verteces = [];
this.index = 0;
}
2018-02-06 01:14:23 +00:00
this.layer = function()
{
if(!this.layers[this.index]){
this.layers[this.index] = [];
}
return this.layers[this.index];
}
this.remove_segment = function()
{
2018-02-12 20:29:16 +00:00
if(this.verteces.length > 0){ this.clear(); return; }
2018-02-06 01:14:23 +00:00
this.layer().pop();
this.clear();
dotgrid.draw();
}
2018-02-06 04:19:34 +00:00
this.remove_segments_at = function(pos)
{
2018-02-06 04:35:25 +00:00
for(segment_id in this.layer()){
var segment = this.layer()[segment_id];
for(vertex_id in segment.verteces){
var vertex = segment.verteces[vertex_id];
if(Math.abs(pos.x) == Math.abs(vertex.x) && Math.abs(pos.y) == Math.abs(vertex.y)){
segment.verteces.splice(vertex_id,1)
}
}
2018-02-17 01:42:52 +00:00
if(segment.verteces.length < 2){
this.layers[this.index].splice(segment_id,1)
}
2018-02-06 04:35:25 +00:00
}
this.clear();
dotgrid.draw();
2018-02-06 04:19:34 +00:00
}
this.add_vertex = function(pos)
{
this.verteces.push(pos);
}
2018-02-06 18:42:34 +00:00
this.vertex_at = function(pos)
{
for(segment_id in this.layer()){
var segment = this.layer()[segment_id];
for(vertex_id in segment.verteces){
var vertex = segment.verteces[vertex_id];
if(vertex.x == Math.abs(pos.x) && vertex.y == Math.abs(pos.y)){
return vertex;
}
}
}
return null;
}
this.cast = function(type)
{
2018-02-06 01:14:23 +00:00
if(!this.layer()){ this.layers[this.index] = []; }
2018-02-06 03:47:25 +00:00
if(!this.can_cast(type)){ console.warn("Cannot cast"); return; }
2018-02-11 04:00:22 +00:00
var append_target = this.can_append({type:type,verteces:this.verteces.slice()})
if(append_target){
this.layers[this.index][append_target].verteces = this.layers[this.index][append_target].verteces.concat(this.verteces.slice())
}
else{
this.layer().push({type:type,verteces:this.verteces.slice()})
}
this.clear();
dotgrid.draw();
2018-02-06 03:27:48 +00:00
dotgrid.history.push(this.layers);
2018-02-06 03:42:14 +00:00
console.log(`Casted ${type} -> ${this.layer().length} elements`);
}
2018-02-11 04:00:22 +00:00
this.can_append = function(content)
{
for(id in this.layer()){
var stroke = this.layer()[id];
if(stroke.type != content.type){ continue; }
if(stroke.verteces[stroke.verteces.length-1].x != content.verteces[0].x){ continue; }
if(stroke.verteces[stroke.verteces.length-1].y != content.verteces[0].y){ continue; }
return id;
}
return false;
}
2018-02-06 03:27:48 +00:00
this.can_cast = function(type)
2018-02-06 03:47:25 +00:00
{
2018-02-06 06:51:09 +00:00
if(!type){ return false; }
2018-02-06 03:47:25 +00:00
// Cannot cast close twice
if(type == "close"){
var prev = this.layer()[this.layer().length-1];
2018-02-06 06:51:09 +00:00
if(!prev || prev.type == "close"){
2018-02-06 03:47:25 +00:00
return false;
}
}
2018-02-06 03:42:14 +00:00
return this.verteces.length >= this.reqs[type];
2018-02-06 03:27:48 +00:00
}
2018-02-06 04:19:34 +00:00
this.path = function(layer = this.layer())
{
2018-02-06 04:19:34 +00:00
if(layer.length > 0 && layer[0].type == "close"){ return ""; }
var html = "";
2018-02-06 04:19:34 +00:00
for(id in layer){
var segment = layer[id];
2018-02-06 03:47:25 +00:00
html += segment.type == "close" ? "Z " : this.render(segment);
}
return html
}
2018-02-06 18:42:34 +00:00
this.paths = function()
{
return [this.path(this.layers[0]),this.path(this.layers[1]),this.path(this.layers[2])]
}
this.render = function(segment)
{
var type = segment.type;
var verteces = segment.verteces;
2018-02-06 03:27:48 +00:00
var html = ``;
var skip = 0;
for(id in verteces){
2018-02-06 03:27:48 +00:00
if(skip > 0){ skip -= 1; continue; }
if(id == 0){ html += `M${verteces[id].x},${verteces[id].y} `; }
var vertex = verteces[id];
2018-02-06 03:27:48 +00:00
var next = verteces[parseInt(id)+1]
2018-02-06 03:42:14 +00:00
var after_next = verteces[parseInt(id)+2]
2018-02-06 03:27:48 +00:00
if(type == "line"){
html += `L${vertex.x},${vertex.y} `;
}
else if(type == "arc_c" && next){
html += `A${next.x - vertex.x},${next.y - vertex.y} 0 0,1 ${next.x},${next.y} `;
}
else if(type == "arc_r" && next){
html += `A${next.x - vertex.x},${next.y - vertex.y} 0 0,0 ${next.x},${next.y} `;
2018-02-06 03:27:48 +00:00
}
2018-02-06 03:42:14 +00:00
else if(type == "bezier" && next && after_next){
html += `Q${next.x},${next.y} ${after_next.x},${after_next.y} `;
skip = 1
2018-02-06 03:42:14 +00:00
}
}
2018-02-06 03:47:25 +00:00
return html
}
2018-02-06 01:02:13 +00:00
this.translate = function(a,b)
{
2018-02-06 01:14:23 +00:00
for(segment_id in this.layer()){
var segment = this.layer()[segment_id];
2018-02-06 01:02:13 +00:00
for(vertex_id in segment.verteces){
var vertex = segment.verteces[vertex_id];
if(vertex.x == Math.abs(a.x) && vertex.y == Math.abs(a.y)){
segment.verteces[vertex_id] = {x:Math.abs(b.x),y:Math.abs(b.y)};
}
}
}
2018-02-06 03:27:48 +00:00
dotgrid.history.push(this.layers);
this.clear();
2018-02-06 01:02:13 +00:00
dotgrid.draw();
}
this.clear = function()
{
this.verteces = [];
2018-02-06 01:23:25 +00:00
dotgrid.draw();
}
2018-02-06 03:27:48 +00:00
this.undo = function()
{
this.layers = dotgrid.history.prev();
dotgrid.draw();
}
this.redo = function()
{
this.layers = dotgrid.history.next();
dotgrid.draw();
}
2018-02-06 06:34:45 +00:00
2018-02-07 06:44:18 +00:00
this.export = function(target = this.layers)
2018-02-06 06:34:45 +00:00
{
2018-02-07 06:44:18 +00:00
return JSON.stringify(copy(target), null, 2);
2018-02-06 06:34:45 +00:00
}
2018-02-07 20:34:17 +00:00
this.replace = function(layers)
{
if(layers.length != 3){ console.log("Incompatible"); return; }
this.layers = layers;
this.clear();
dotgrid.draw();
dotgrid.history.push(this.layers);
}
2018-02-07 06:44:18 +00:00
this.import = function(layer)
2018-02-06 06:34:45 +00:00
{
2018-02-07 06:44:18 +00:00
this.layers[this.index] = this.layers[this.index].concat(layer)
2018-02-06 06:51:09 +00:00
dotgrid.history.push(this.layers);
this.clear();
dotgrid.draw();
2018-02-06 06:34:45 +00:00
}
2018-02-06 18:42:34 +00:00
2018-02-12 20:29:16 +00:00
this.select_layer = function(id)
2018-02-06 18:42:34 +00:00
{
2018-02-12 20:29:16 +00:00
this.index = clamp(id,0,2);
2018-02-06 18:42:34 +00:00
this.clear();
dotgrid.draw();
console.log(`layer:${this.index}`)
}
2018-02-12 20:29:16 +00:00
this.layer_up = function()
{
this.select_layer(this.index-1);
}
2018-02-06 18:42:34 +00:00
this.layer_down = function()
{
2018-02-12 20:29:16 +00:00
this.select_layer(this.index+1);
2018-02-06 18:42:34 +00:00
}
2018-02-07 06:44:18 +00:00
function copy(data){ return data ? JSON.parse(JSON.stringify(data)) : []; }
2018-02-12 20:29:16 +00:00
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
}