2018-02-18 21:29:07 -05:00
|
|
|
function Keyboard()
|
|
|
|
{
|
|
|
|
this.memory = "";
|
2018-03-06 19:50:41 -05:00
|
|
|
this.is_active = false;
|
2018-02-18 21:29:07 -05:00
|
|
|
|
|
|
|
this.selector = {x:0,y:0};
|
|
|
|
|
2018-02-19 15:20:05 -05:00
|
|
|
this.start = function()
|
|
|
|
{
|
2018-03-06 19:50:41 -05:00
|
|
|
this.is_active = true;
|
2018-02-19 15:20:05 -05:00
|
|
|
dotgrid.controller.set("keyboard");
|
|
|
|
this.select({x:10,y:10})
|
|
|
|
dotgrid.cursor.className = "keyboard";
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stop = function()
|
|
|
|
{
|
2018-03-06 19:50:41 -05:00
|
|
|
this.is_active = false;
|
2018-02-19 15:20:05 -05:00
|
|
|
dotgrid.controller.set();
|
|
|
|
dotgrid.cursor.className = "";
|
|
|
|
}
|
|
|
|
|
2018-02-18 21:29:07 -05:00
|
|
|
this.select = function(pos)
|
|
|
|
{
|
|
|
|
this.selector = {x:pos.x * -dotgrid.grid_width,y:pos.y * dotgrid.grid_height};
|
|
|
|
dotgrid.move_cursor(this.selector)
|
2018-05-07 18:59:38 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-02-18 21:29:07 -05:00
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.deselect = function()
|
|
|
|
{
|
|
|
|
dotgrid.tool.clear();
|
2018-05-07 18:59:38 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-02-18 21:29:07 -05:00
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.confirm = function()
|
|
|
|
{
|
|
|
|
dotgrid.tool.add_vertex({x:this.selector.x * -1,y:this.selector.y});
|
2018-05-07 18:59:38 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-02-18 21:29:07 -05:00
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.erase = function()
|
|
|
|
{
|
|
|
|
dotgrid.tool.remove_segments_at(this.selector);
|
2018-05-07 18:59:38 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-02-18 21:29:07 -05:00
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.move = function(x,y)
|
|
|
|
{
|
2018-02-19 15:20:05 -05:00
|
|
|
this.selector = {x:this.selector.x+(x*dotgrid.grid_width),y:this.selector.y+(-y*dotgrid.grid_height)};
|
|
|
|
|
|
|
|
this.selector.x = this.selector.x > 0 ? 0 : this.selector.x;
|
|
|
|
this.selector.y = this.selector.y < 0 ? 0 : this.selector.y;
|
2018-02-18 21:29:07 -05:00
|
|
|
dotgrid.move_cursor(this.selector)
|
2018-05-07 18:59:38 -04:00
|
|
|
dotgrid.guide.refresh();
|
2018-02-18 21:29:07 -05:00
|
|
|
dotgrid.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.push = function(k)
|
|
|
|
{
|
|
|
|
this.memory = `${this.memory}${k}`;
|
|
|
|
if(this.memory.length > 3){
|
|
|
|
var pos = {x:parseInt(this.memory.substr(0,2)),y:parseInt(this.memory.substr(2,2))};
|
|
|
|
this.select(pos);
|
|
|
|
this.memory = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.reset = function()
|
|
|
|
{
|
|
|
|
this.memory = "";
|
|
|
|
dotgrid.update();
|
|
|
|
}
|
|
|
|
|
2018-02-18 21:37:50 -05:00
|
|
|
this.listen = function(e)
|
|
|
|
{
|
2018-03-06 19:50:41 -05:00
|
|
|
if(!this.is_active){ return; }
|
|
|
|
|
2018-02-18 21:37:50 -05:00
|
|
|
if(e.key == "ArrowRight"){
|
|
|
|
dotgrid.keyboard.move(-1,0);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
if(e.key == "ArrowLeft"){
|
|
|
|
dotgrid.keyboard.move(1,0);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
if(e.key == "ArrowUp"){
|
|
|
|
dotgrid.keyboard.move(0,1);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
if(e.key == "ArrowDown"){
|
|
|
|
dotgrid.keyboard.move(0,-1);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2018-03-06 15:32:31 -05:00
|
|
|
if(e.code && e.code.substr(0,5) == "Digit" && !e.metaKey && !e.ctrlKey){
|
2018-02-18 21:37:50 -05:00
|
|
|
var value = parseInt(e.code.substr(5,1));
|
|
|
|
dotgrid.keyboard.push(value);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 15:20:05 -05:00
|
|
|
// document.onkeyup = function(event){ dotgrid.keyboard.listen(event); };
|
2018-02-18 21:37:50 -05:00
|
|
|
document.onkeydown = function(event){ dotgrid.keyboard.listen(event); };
|
2018-02-18 21:29:07 -05:00
|
|
|
}
|