summaryrefslogtreecommitdiff
path: root/js/ui/keys.js
blob: f76d56287c0235dabdb63645d05e0e369dbd6cd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var keys = (function(){

  var keys = {}
  var direction = [0,1]
  keys.bind = function(){
    cursor_input.addEventListener('keydown', function(e){

      // console.log("keycode:", e.keyCode)
      if (e.altKey) {
        document.body.classList.add("dropper")
      }
    
      switch (e.keyCode) {
        case 27: // esc
          if (focused) focused.blur()
          break
        case 219: // [
          if (current_tool.name != "text") {
            e.preventDefault()
            brush.contract(1)
            brush.modified = false
            check_if_lost_focus()
            break
          }
        case 221: // ]
          if (current_tool.name != "text") {
            e.preventDefault()
            brush.expand(1)
            brush.modified = false
            break
          }
        case 8:
          e.preventDefault()
          current_canvas.focusLex(focused.y, focused.x - 1)
          focused.char = " "
          focused.build()
          return
        case 13: // return
          e.preventDefault()
          current_canvas.focusLex(focused.y, focused.x+1)
          return
        case 38: // up
          e.preventDefault()
          current_canvas.focusLex(focused.y - 1, focused.x + 0)
          break
        case 40: // down
          e.preventDefault()
          current_canvas.focusLex(focused.y + 1, focused.x + 0)
          break
        case 37: // left
          e.preventDefault()
          current_canvas.focusLex(focused.y + 0, focused.x - 1)
          break
        case 39: // right
          e.preventDefault()
          current_canvas.focusLex(focused.y + 0, focused.x + 1)
          break
  //      default:
  //        if (focused) { focused.key(undefined, e.keyCode) }
      }
    })

    cursor_input.addEventListener('input', function(e){
  /*
      if (! e.metaKey && ! e.ctrlKey && ! e.altKey) {
        e.preventDefault()
      }
  */
      if (current_tool.name == "shader") {
        cursor_input.value = ""
        return
      }
      var char = cursor_input.value
      cursor_input.value = ""

      // console.log("input:", char)

      if (focused && char) {
        var y = focused.y, x = focused.x
        focused.key(char, e.keyCode)
        if (! ('y' in focused && 'x' in focused) ) { return }
        // console.log(y, direction[0], x, direction[1])
        current_canvas.focusLex(y + direction[0], x + direction[1])
      }
    })
    
    cursor_input.addEventListener("keyup", function(e){
      if (! e.altKey) {
        document.body.classList.remove("dropper")
      }
    })

  }
  
  return keys
})()

function check_if_lost_focus() {
  if (! focused || ! focused.span)
  focused = canvas.aa[0][0]
}