pointvec/desktop/sources/scripts/cursor.js

110 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-08-02 22:57:35 +00:00
function Cursor()
{
2018-08-03 00:53:26 +00:00
this.pos = {x:0,y:0};
this.translation = null;
this.operation = null;
2018-08-02 22:57:35 +00:00
this.translate = function(from = null,to = null, multi = false)
2018-08-02 23:50:09 +00:00
{
2018-08-02 22:57:35 +00:00
if((from || to) && this.translation == null){ this.translation = {multi:multi}; console.log("Begin translation") }
if(from){ this.translation.from = from; }
if(to){ this.translation.to = to; }
if(!from && !to){
this.translation = null;
}
}
this.down = function(e)
{
this.pos = this.pos_from_event(e)
// Translation
if(dotgrid.tool.vertex_at(this.pos)){
this.translate(this.pos,this.pos,e.shiftKey)
}
dotgrid.guide.refresh();
dotgrid.interface.refresh();
e.preventDefault();
}
2018-08-04 02:36:45 +00:00
this.last_pos = {x:0,y:0}
2018-08-02 22:57:35 +00:00
this.move = function(e)
{
this.pos = this.pos_from_event(e)
// Translation
if(this.translation){
this.translate(null,this.pos)
}
2018-08-04 02:36:45 +00:00
if(this.last_pos.x != this.pos.x || this.last_pos.y != this.pos.y){
dotgrid.guide.refresh();
}
2018-08-02 22:57:35 +00:00
dotgrid.interface.refresh();
e.preventDefault();
2018-08-04 02:36:45 +00:00
this.last_pos = this.pos;
2018-08-02 22:57:35 +00:00
}
this.up = function(e)
{
this.pos = this.pos_from_event(e)
2018-08-04 02:36:45 +00:00
if(e.altKey){ dotgrid.tool.remove_segments_at(this.pos); this.translate(); return; }
2018-08-02 22:57:35 +00:00
2018-08-04 19:56:13 +00:00
if(this.translation && !is_equal(this.translation.from,this.translation.to)){
2018-08-02 22:57:35 +00:00
if(this.translation.multi){ dotgrid.tool.translate_multi(this.translation.from,this.translation.to); }
else{ dotgrid.tool.translate(this.translation.from,this.translation.to); }
}
2018-08-02 23:50:09 +00:00
else if(e.target.id == "guide"){
2018-08-02 22:57:35 +00:00
dotgrid.tool.add_vertex({x:this.pos.x,y:this.pos.y});
}
2018-08-04 19:56:13 +00:00
2018-08-02 22:57:35 +00:00
this.translate();
dotgrid.interface.refresh();
dotgrid.guide.refresh();
e.preventDefault();
}
this.alt = function(e)
{
this.pos = this.pos_from_event(e)
dotgrid.tool.remove_segments_at(this.pos);
e.preventDefault();
setTimeout(() => { dotgrid.tool.clear(); },150);
}
// Position Mods
this.pos_from_event = function(e)
{
return this.pos_snap(this.pos_relative({x:e.clientX,y:e.clientY}))
}
this.pos_relative = function(pos)
{
return {
2018-08-02 23:17:29 +00:00
x:pos.x - dotgrid.guide.el.offsetLeft,
y:pos.y - dotgrid.guide.el.offsetTop
2018-08-02 22:57:35 +00:00
};
}
this.pos_snap = function(pos)
{
2018-08-26 19:39:15 +00:00
let grid = dotgrid.tool.settings.size.width/dotgrid.grid_x;
2018-08-02 22:57:35 +00:00
return {
2018-08-02 23:50:09 +00:00
x:clamp(step(pos.x,grid),grid,dotgrid.tool.settings.size.width),
2018-08-02 23:41:37 +00:00
y:clamp(step(pos.y,grid),grid,dotgrid.tool.settings.size.height+grid)
2018-08-02 22:57:35 +00:00
};
}
2018-08-04 19:56:13 +00:00
function is_equal(a,b){ return a.x == b.x && a.y == b.y; }
2018-08-02 22:57:35 +00:00
}