asciiblaster- draw irc art in your web browser |
git clone git://git.acid.vegas/asciiblaster.git |
Log | Files | Refs | Archive | README |
keys.js (6103B)
1 var keys = (function(){ 2 3 var keys = {} 4 keys.bind = function(){ 5 cursor_input.addEventListener('keydown', function(e){ 6 7 // console.log("keycode:", e.keyCode) 8 if (e.altKey) { 9 document.body.classList.add("dropper") 10 } 11 12 switch (e.keyCode) { 13 case 27: // esc 14 if (!selection.hidden && current_canvas === canvas){ 15 selection.hide() 16 selection.show() 17 } else if (focused){ 18 focused.blur() 19 } 20 return 21 } 22 23 if (window.focused && focused.raw_key) { 24 focused.raw_key(e) 25 return 26 } 27 28 switch (e.keyCode) { 29 case 219: // [ 30 if (current_tool.name != "text") { 31 e.preventDefault() 32 brush.contract(1) 33 brush.modified = false 34 check_if_lost_focus() 35 } 36 break 37 case 221: // ] 38 if (current_tool.name != "text") { 39 e.preventDefault() 40 brush.expand(1) 41 brush.modified = false 42 } 43 break 44 case 8: // backspace 45 e.preventDefault() 46 if (current_canvas === canvas) 47 undo.new() 48 current_canvas.focus_add(-1, 0) 49 if (current_canvas === canvas) 50 undo.save_focused_lex() 51 focused.char = " " 52 focused.build() 53 return 54 case 13: // return 55 e.preventDefault() 56 current_canvas.focusLex(focused.y, focused.x+1) 57 return 58 case 38: // up 59 e.preventDefault() 60 current_canvas.focus_add(0, -1) 61 break 62 case 40: // down 63 e.preventDefault() 64 current_canvas.focus_add(0, 1) 65 break 66 case 37: // left 67 e.preventDefault() 68 current_canvas.focus_add(-1, 0) 69 break 70 case 39: // right 71 e.preventDefault() 72 current_canvas.focus_add(1, 0) 73 break 74 // use typical windows and os x shortcuts 75 // undo: ctrl-z or cmd-z 76 // redo: ctrl-y or shift-cmd-z 77 case 89: // y 78 if (!e.ctrlKey && !e.metaKey) break; 79 e.preventDefault(); 80 undo.redo(); 81 break 82 case 90: // z 83 if (!e.ctrlKey && !e.metaKey) break; 84 e.preventDefault(); 85 if (e.shiftKey) 86 undo.redo(); 87 else 88 undo.undo(); 89 break 90 // default: 91 // if (focused) { focused.key(undefined, e.keyCode) } 92 } 93 }) 94 95 cursor_input.addEventListener('input', function(e){ 96 /* 97 if (! e.metaKey && ! e.ctrlKey && ! e.altKey) { 98 e.preventDefault() 99 } 100 */ 101 if (current_tool.name == "shader") { 102 cursor_input.value = "" 103 return 104 } 105 var char = cursor_input.value 106 cursor_input.value = "" 107 108 // console.log("input:", char) 109 110 if (current_tool.name != "text" && ! brush.modified) { 111 brush.char = char 112 if (char == " ") { 113 brush.bg = brush.fg 114 } 115 else if (brush.bg != fillColor) { 116 brush.fg = brush.bg 117 brush.bg = fillColor 118 } 119 brush.rebuild() 120 } 121 122 if (focused && char) { 123 var y = focused.y, x = focused.x 124 if (current_canvas === canvas){ 125 undo.new() 126 undo.save_focused_lex() 127 } 128 var moving = focused.key(char, e.keyCode) 129 if ( ! moving || ! ('y' in focused && 'x' in focused) ) { return } 130 current_canvas.focus_add(1, 0) 131 } 132 }) 133 134 cursor_input.addEventListener("keyup", function(e){ 135 if (! e.altKey) { 136 document.body.classList.remove("dropper") 137 } 138 }) 139 } 140 141 keys.int_key = function (f) { 142 return function (key, keyCode) { 143 var n = parseInt(key) 144 ! isNaN(n) && f(n) 145 } 146 } 147 148 keys.arrow_key = function (fn) { 149 return function (e){ 150 switch (e.keyCode) { 151 case 38: // up 152 e.preventDefault() 153 fn(1) 154 break 155 case 40: // down 156 e.preventDefault() 157 fn(-1) 158 break 159 } 160 } 161 } 162 keys.left_right_key = function (fn) { 163 return function (e){ 164 switch (e.keyCode) { 165 case 39: // right 166 e.preventDefault() 167 fn(1) 168 break 169 case 38: // up 170 case 40: // down 171 e.preventDefault() 172 fn(0) 173 break 174 case 37: // left 175 e.preventDefault() 176 fn(-1) 177 break 178 } 179 } 180 } 181 182 keys.single_numeral_key = function (lex, fn) { 183 return keys.int_key(function(n, keyCode){ 184 if (n == 0) n = 10 185 lex.blur() 186 fn(n) 187 }) 188 } 189 keys.multi_numeral_key = function (lex, digits){ 190 return keys.int_key(function(n, keyCode){ 191 lex.read() 192 if (lex.char.length < digits) { 193 n = parseInt(lex.char) * 10 + n 194 } 195 lex.char = ""+n 196 lex.build() 197 }) 198 } 199 keys.multi_numeral_blur = function (lex, fn){ 200 return function(){ 201 var n = parseInt(lex.char) 202 if (isNaN(n)) return 203 fn(n) 204 } 205 } 206 207 // function cancelZoom() { 208 // var d = document, 209 // viewport, 210 // content, 211 // maxScale = ',maximum-scale=', 212 // maxScaleRegex = /,*maximum\-scale\=\d*\.*\d*/; 213 214 // // this should be a focusable DOM Element 215 // if (!this.addEventListener || !d.querySelector) { 216 // return; 217 // } 218 219 // viewport = d.querySelector('meta[name="viewport"]'); 220 // content = viewport.content; 221 222 // function changeViewport(event) { 223 // // http://nerd.vasilis.nl/prevent-ios-from-zooming-onfocus/ 224 // viewport.content = content + (event.type == 'blur' ? (content.match(maxScaleRegex, '') ? '' : maxScale + 10) : maxScale + 1); 225 // } 226 227 // // We could use DOMFocusIn here, but it's deprecated. 228 // this.addEventListener('focus', changeViewport, true); 229 // this.addEventListener('blur', changeViewport, false); 230 // } 231 232 // cancelZoom.bind(cursor_input)(); 233 234 return keys 235 })() 236 237 function check_if_lost_focus() { 238 if (! window.focused || ! window.focused.span) 239 window.focused = canvas.aa[0][0] 240 }