Files
pointvec/desktop/sources/scripts/guide.js

240 lines
7.1 KiB
JavaScript
Raw Normal View History

2017-11-10 08:47:06 +13:00
function Guide()
{
2017-11-13 15:25:29 +13:00
this.el = document.createElement("canvas");
2017-11-10 08:47:06 +13:00
this.el.id = "guide";
2017-11-13 16:54:56 +13:00
this.el.width = 640;
this.el.height = 640;
this.el.style.width = "320px";
this.el.style.height = "320px";
2018-05-10 20:01:31 +12:00
this.show_extras = true;
2017-11-13 16:54:56 +13:00
2018-08-18 07:58:01 +12:00
this.scale = 2;
2018-05-10 09:18:30 +12:00
2017-11-10 08:47:06 +13:00
this.start = function()
2017-11-22 11:58:07 +13:00
{
this.clear();
2018-05-08 10:59:38 +12:00
this.refresh();
2017-11-22 11:58:07 +13:00
}
2018-08-04 14:36:45 +12:00
this.refresh = function(force = false)
2018-05-10 10:04:02 +12:00
{
this.clear();
2018-08-18 07:58:01 +12:00
this.el.getContext('2d').restore();
2018-05-18 15:45:56 +12:00
if(dotgrid.tool.index == 2){ this.draw_markers() ; this.draw_vertices() }
2018-08-03 11:20:52 +12:00
this.draw_path(new Generator(dotgrid.tool.layers[2],dotgrid.tool.styles[2]).toString({x:0,y:0},this.scale),dotgrid.tool.styles[2])
2018-05-18 15:45:56 +12:00
if(dotgrid.tool.index == 1){ this.draw_markers() ; this.draw_vertices() }
2018-08-03 11:20:52 +12:00
this.draw_path(new Generator(dotgrid.tool.layers[1],dotgrid.tool.styles[1]).toString({x:0,y:0},this.scale),dotgrid.tool.styles[1])
2018-05-18 15:32:35 +12:00
if(dotgrid.tool.index == 0){ this.draw_markers(); this.draw_vertices() }
2018-08-03 11:20:52 +12:00
this.draw_path(new Generator(dotgrid.tool.layers[0],dotgrid.tool.styles[0]).toString({x:0,y:0},this.scale),dotgrid.tool.styles[0])
2018-05-18 15:32:35 +12:00
2018-05-12 10:19:24 +12:00
this.draw_handles()
2018-05-10 10:04:02 +12:00
this.draw_translation();
this.draw_cursor();
2018-05-10 19:49:41 +12:00
this.draw_preview();
2018-05-10 10:04:02 +12:00
}
this.clear = function()
{
2018-08-03 10:57:35 +12:00
this.el.getContext('2d').clearRect(0, 0, this.el.width*this.scale, this.el.height*this.scale);
2018-05-10 10:04:02 +12:00
}
2017-11-22 11:58:07 +13:00
2018-01-13 09:00:53 +13:00
this.toggle = function()
{
2018-05-10 20:01:31 +12:00
this.show_extras = this.show_extras ? false : true;
this.refresh()
2018-01-13 09:00:53 +13:00
}
2017-11-22 11:58:07 +13:00
this.resize = function(size)
{
2018-08-03 13:48:16 +12:00
var offset = 15
2018-08-03 10:57:35 +12:00
this.el.width = (size.width+offset)*this.scale;
2018-08-03 13:48:16 +12:00
this.el.height = (size.height+(offset*2))*this.scale;
2018-05-08 21:05:19 +12:00
this.el.style.width = (size.width+offset)+"px";
2018-08-03 13:48:16 +12:00
this.el.style.height = (size.height+(offset*2))+"px";
2017-11-22 11:58:07 +13:00
2018-05-08 10:59:38 +12:00
this.refresh();
2017-11-22 11:58:07 +13:00
}
2018-05-10 10:04:02 +12:00
this.draw_handles = function()
{
2018-05-10 20:01:31 +12:00
if(!this.show_extras){ return; }
2018-05-12 10:19:24 +12:00
2018-05-10 09:18:30 +12:00
for(segment_id in dotgrid.tool.layer()){
var segment = dotgrid.tool.layer()[segment_id];
for(vertex_id in segment.vertices){
var vertex = segment.vertices[vertex_id];
2018-05-12 10:19:24 +12:00
this.draw_handle(vertex);
2018-05-10 09:18:30 +12:00
}
}
2018-05-10 10:04:02 +12:00
}
2018-05-10 09:18:30 +12:00
2018-05-10 10:04:02 +12:00
this.draw_vertices = function()
{
for(id in dotgrid.tool.vertices){
this.draw_vertex(dotgrid.tool.vertices[id]);
2017-11-10 08:47:06 +13:00
}
2018-05-10 10:04:02 +12:00
}
2018-05-08 11:32:00 +12:00
2018-05-10 10:04:02 +12:00
this.draw_markers = function()
{
2018-05-10 20:01:31 +12:00
if(!this.show_extras){ return; }
2018-08-03 11:41:37 +12:00
var cursor = {x:parseInt(dotgrid.cursor.pos.x/dotgrid.grid_width),y:parseInt(dotgrid.cursor.pos.y/dotgrid.grid_width)}
for (var x = dotgrid.grid_x-1; x >= 0; x--) {
2018-05-10 10:04:02 +12:00
for (var y = dotgrid.grid_y; y >= 0; y--) {
var is_step = x % dotgrid.block_x == 0 && y % dotgrid.block_y == 0;
2018-08-03 11:41:37 +12:00
// Color
var color = is_step ? dotgrid.theme.active.f_med : dotgrid.theme.active.f_low;
if((y == 0 || y == dotgrid.grid_y) && cursor.x == x+1){ color = dotgrid.theme.active.f_high; }
else if((x == 0 || x == dotgrid.grid_x-1) && cursor.y == y+1){ color = dotgrid.theme.active.f_high; }
2018-08-04 14:36:45 +12:00
else if(cursor.x == x+1 && cursor.y == y+1){ color = dotgrid.theme.active.f_high; }
2018-08-03 11:41:37 +12:00
this.draw_marker({
x:parseInt(x * dotgrid.grid_width) + dotgrid.grid_width,
y:parseInt(y * dotgrid.grid_height) + dotgrid.grid_height
},is_step ? 2.5 : 1.5,color);
2018-05-10 10:04:02 +12:00
}
}
2017-11-10 08:47:06 +13:00
}
2017-11-13 15:25:29 +13:00
2018-08-04 14:36:45 +12:00
this.draw_marker = function(pos,radius = 1,color)
{
var ctx = this.el.getContext('2d');
ctx.beginPath();
ctx.lineWidth = 2;
ctx.arc(pos.x * this.scale, pos.y * this.scale, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
this.draw_vertex = function(pos, radius = 5)
{
var ctx = this.el.getContext('2d');
ctx.beginPath();
2018-05-08 10:47:09 +12:00
ctx.lineWidth = 2;
2018-08-03 11:17:29 +12:00
ctx.arc((pos.x * this.scale), (pos.y * this.scale), radius, 0, 2 * Math.PI, false);
2018-02-06 16:27:48 +13:00
ctx.fillStyle = dotgrid.theme.active.f_med;
ctx.fill();
ctx.closePath();
}
2018-05-12 10:19:24 +12:00
this.draw_handle = function(pos, radius = 6)
{
var ctx = this.el.getContext('2d');
ctx.beginPath();
ctx.lineWidth = 3;
ctx.lineCap="round";
2018-08-03 11:17:29 +12:00
ctx.arc(Math.abs(pos.x * -this.scale), Math.abs(pos.y * this.scale), radius+3, 0, 2 * Math.PI, false);
2018-05-12 10:19:24 +12:00
ctx.fillStyle = dotgrid.theme.active.f_high;
ctx.fill();
ctx.strokeStyle = dotgrid.theme.active.f_high;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
2018-08-03 11:17:29 +12:00
ctx.arc((pos.x * this.scale), (pos.y * this.scale), radius, 0, 2 * Math.PI, false);
2018-05-12 10:19:24 +12:00
ctx.fillStyle = dotgrid.theme.active.f_low;
ctx.fill();
ctx.closePath();
ctx.beginPath();
2018-08-03 11:17:29 +12:00
ctx.arc((pos.x * this.scale), (pos.y * this.scale), radius-3, 0, 2 * Math.PI, false);
2018-05-12 10:19:24 +12:00
ctx.fillStyle = dotgrid.theme.active.f_high;
ctx.fill();
ctx.closePath();
}
2018-05-10 09:56:55 +12:00
this.draw_path = function(path,style)
2018-05-10 09:18:30 +12:00
{
var ctx = this.el.getContext('2d');
2018-05-10 09:56:55 +12:00
var p = new Path2D(path);
2018-05-10 09:18:30 +12:00
ctx.strokeStyle = style.color;
2018-08-03 10:57:35 +12:00
ctx.lineWidth = style.thickness * this.scale;
2018-05-10 09:18:30 +12:00
ctx.lineCap = style.strokeLinecap;
ctx.lineJoin = style.strokeLinejoin;
2018-05-10 19:49:41 +12:00
2018-05-10 09:18:30 +12:00
if(style.fill && style.fill != "none"){
ctx.fillStyle = style.color
ctx.fill(p);
}
2018-08-18 08:04:54 +12:00
// Dash
if(style.strokeLineDash){ ctx.setLineDash(style.strokeLineDash); }
else{ ctx.setLineDash([]); }
2018-05-10 09:18:30 +12:00
ctx.stroke(p);
}
2017-11-13 16:54:56 +13:00
this.draw_translation = function()
2018-05-10 10:04:02 +12:00
{
if(!dotgrid.cursor.translation){ return; }
2018-08-03 11:17:29 +12:00
2018-05-08 10:47:09 +12:00
var ctx = this.el.getContext('2d');
2018-08-03 11:17:29 +12:00
ctx.beginPath();
ctx.moveTo((dotgrid.cursor.translation.from.x * this.scale),(dotgrid.cursor.translation.from.y * this.scale));
ctx.lineTo((dotgrid.cursor.translation.to.x * this.scale),(dotgrid.cursor.translation.to.y * this.scale));
ctx.lineCap="round";
ctx.lineWidth = 5;
ctx.strokeStyle = dotgrid.theme.active.b_inv;
ctx.setLineDash([5,10]);
ctx.stroke();
ctx.closePath();
2018-08-18 08:04:54 +12:00
ctx.setLineDash([]);
ctx.restore();
2017-11-13 15:25:29 +13:00
}
2018-05-08 11:32:00 +12:00
2018-05-12 10:19:24 +12:00
this.draw_cursor = function(pos = dotgrid.cursor.pos,radius = dotgrid.tool.style().thickness-1)
2018-05-08 11:32:00 +12:00
{
var ctx = this.el.getContext('2d');
2018-05-12 10:19:24 +12:00
ctx.beginPath();
ctx.lineWidth = 3;
2018-08-03 11:17:29 +12:00
ctx.lineCap = "round";
2018-08-04 14:36:45 +12:00
ctx.arc(Math.abs(pos.x * -this.scale), Math.abs(pos.y * this.scale), 5, 0, 2 * Math.PI, false);
ctx.strokeStyle = dotgrid.theme.active.background;
ctx.stroke();
2018-05-12 10:19:24 +12:00
ctx.closePath();
2018-05-08 11:32:00 +12:00
ctx.beginPath();
2018-08-03 10:57:35 +12:00
ctx.lineWidth = 3;
2018-08-03 11:17:29 +12:00
ctx.lineCap = "round";
ctx.arc(Math.abs(pos.x * -this.scale), Math.abs(pos.y * this.scale), clamp(radius,5,100), 0, 2 * Math.PI, false);
2018-05-08 11:32:00 +12:00
ctx.strokeStyle = dotgrid.theme.active.f_med;
ctx.stroke();
ctx.closePath();
}
2018-05-09 09:33:17 +12:00
2018-05-10 19:49:41 +12:00
this.draw_preview = function()
{
2018-08-18 08:04:54 +12:00
var ctx = this.el.getContext('2d');
2018-08-03 12:53:26 +12:00
var operation = dotgrid.cursor.operation && dotgrid.cursor.operation.cast ? dotgrid.cursor.operation.cast : null
2018-05-10 19:49:41 +12:00
if(!dotgrid.tool.can_cast(operation)){ return; }
if(operation == "close"){ return; }
2018-08-03 11:20:52 +12:00
var path = new Generator([{vertices:dotgrid.tool.vertices,type:operation}]).toString({x:0,y:0},2)
2018-05-10 19:49:41 +12:00
var style = {
2018-05-10 19:53:09 +12:00
color:dotgrid.theme.active.f_med,
2018-05-10 19:49:41 +12:00
thickness:2,
strokeLinecap:"round",
strokeLinejoin:"round",
strokeLineDash:[5, 15]
}
this.draw_path(path,style)
2018-08-18 08:04:54 +12:00
ctx.setLineDash([]);
ctx.restore();
2018-05-10 19:49:41 +12:00
}
2018-05-09 09:33:17 +12:00
function pos_is_equal(a,b){ return a && b && Math.abs(a.x) == Math.abs(b.x) && Math.abs(a.y) == Math.abs(b.y) }
2018-05-12 10:19:24 +12:00
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
2018-05-08 21:23:06 -10:00
}