From d506abbda37fea56fec00fe0a811e70c4b7fb754 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 17 Nov 2017 19:45:15 +1300 Subject: [PATCH] Keyboard operations --- sources/scripts/dotgrid.js | 52 +++++++++++++++++++++++-------------- sources/scripts/keyboard.js | 31 ++++++++++++++++++++-- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/sources/scripts/dotgrid.js b/sources/scripts/dotgrid.js index bf42110..0192c6e 100644 --- a/sources/scripts/dotgrid.js +++ b/sources/scripts/dotgrid.js @@ -147,22 +147,8 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca if(dotgrid.translation){ dotgrid.translation.to = pos; } - if(pos.x>0) { - this.cursor.style.visibility = "hidden" - } else { - if(this.cursor.style.visibility == "hidden") { - this.cursor.style.transition = "initial" - } - this.cursor.style.visibility = "visible" - this.cursor.style.left = Math.floor(-(pos.x-this.grid_width)); - this.cursor.style.top = Math.floor(pos.y+this.grid_height); - this.cursor_coord.className = -pos.x > this.width/2 ? "fl left" : "fl" - this.cursor_coord.textContent = parseInt(-pos.x/this.grid_width)+","+parseInt(pos.y/this.grid_height); - window.setTimeout(() => dotgrid.cursor.style.transition = "all 50ms", 17 /*one frame*/) - } - + dotgrid.move_cursor(pos) dotgrid.guide.update(); - } this.mouse_up = function(e) @@ -181,10 +167,32 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca dotgrid.translation = null; + this.add_point(pos) + this.draw(); + } + + this.move_cursor = function(pos) + { + if(pos.x>0) { + this.cursor.style.visibility = "hidden" + } else { + if(this.cursor.style.visibility == "hidden") { + this.cursor.style.transition = "initial" + } + this.cursor.style.visibility = "visible" + this.cursor.style.left = Math.floor(-(pos.x-this.grid_width)); + this.cursor.style.top = Math.floor(pos.y+this.grid_height); + this.cursor_coord.className = -pos.x > this.width/2 ? "fl left" : "fl" + this.cursor_coord.textContent = parseInt(-pos.x/this.grid_width)+","+parseInt(pos.y/this.grid_height); + window.setTimeout(() => dotgrid.cursor.style.transition = "all 50ms", 17 /*one frame*/) + } + } + + this.add_point = function(pos) + { if(from === null){ this.set_from(pos.scale(1/this.scale)); } else if(to === null){ this.set_to(pos.scale(1/this.scale)); } else{ this.set_end(pos.scale(1/this.scale)); } - this.draw(); } this.handle_at = function(pos) @@ -276,17 +284,23 @@ function Dotgrid(width,height,grid_x,grid_y,block_x,block_y,thickness = 3,lineca this.mod_move = function(move) { if(!to && !end && from){ - this.set_from(new Pos(from.x-(move.x),from.y+(move.y))) + var pos = new Pos(from.x-(move.x),from.y+(move.y)) + this.set_from(pos) + this.move_cursor(pos) this.draw(); return; } if(!end && to){ - this.set_to(new Pos(to.x-(move.x),to.y+(move.y))) + var pos = new Pos(to.x-(move.x),to.y+(move.y)) + this.set_to(pos) + this.move_cursor(pos) this.draw(); return; } if(end){ - this.set_end(new Pos(end.x-(move.x),end.y+(move.y))) + var pos = new Pos(end.x-(move.x),end.y+(move.y)) + this.set_end(pos) + this.move_cursor(pos) this.draw(); return; } diff --git a/sources/scripts/keyboard.js b/sources/scripts/keyboard.js index ffc903f..9a1148e 100644 --- a/sources/scripts/keyboard.js +++ b/sources/scripts/keyboard.js @@ -27,9 +27,15 @@ function Keyboard() return; } - switch(e.key){ - + var numbers = ["0","1","2","3","4","5","6","7","8","9"] + if(numbers.indexOf(e.key) > -1 || e.code == "Digit0" || e.keyCode == 48){ + keyboard.cheatcode(e.key); + return; } + else{ + this.code_history = ""; + } + switch(e.keyCode) { case 65 : dotgrid.draw_arc(e.shiftKey ? "1,0" : "0,0"); break; // 'a/A' case 83 : dotgrid.draw_arc(e.shiftKey ? "1,1" : "0,1"); break; // 's/S' @@ -68,4 +74,25 @@ function Keyboard() } dotgrid.draw(); } + + this.code_history = ""; + + this.cheatcode = function(key) + { + if(key.length != 1){ return; } + this.code_history += key; + + if(this.code_history.length == 2){ + var x = this.code_history.substr(0,2); + var y = 15; + dotgrid.move_cursor(new Pos(x * -15,y * 15)) + } + if(this.code_history.length > 3){ + var x = this.code_history.substr(0,2); + var y = this.code_history.substr(2,2); + dotgrid.add_point(new Pos(x * -15,y * 15)) + dotgrid.move_cursor(new Pos(x * -15,y * 15)) + this.code_history = ""; + } + } }