summaryrefslogtreecommitdiff
path: root/js/tool.js
blob: 0ad23b96a67726c3dd8b119a1ef9609459b5ce65 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var Tool = Model({
  init: function (el) {
    this.el = el
    this.lex = new Lex (el)
    this.name = el.innerHTML
  },
  bind: function(){
    var tool = this
    tool.el.addEventListener('mousedown', function(e){
      tool.focus()
    })
    if (tool.memorable) {
      // console.log(tool.name, localStorage.getItem("ascii.tools." + tool.name) )
      tool.use( localStorage.getItem("ascii.tools." + tool.name) == "true" )
    }
  },
  use: function(){},
  done: function(){},
  focus: function(){
    // focused && focused.blur()
    current_tool && current_tool.blur()
    current_tool = this
    this.el.classList.add('focused')
    this.use()
    if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
  },
  blur: function(){
    current_tool = null
    this.el.classList.remove('focused')
    this.done()
  }
})

var FileTool = Tool.extend({
  focus: function(){
    if (current_filetool === this) {
      this.blur()
      return
    }
    current_filetool && current_filetool.blur()
    current_filetool = this
    this.el.classList.add('focused')
    this.use()
    if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
  },
  blur: function(){
    current_filetool = null
    this.el.classList.remove('focused')
    this.done()
  }
})

var RadioItem = Tool.extend({
  init: function(group, el){
    this.group = group
    this.el = el
  },
  focus: function(){
    this.el.classList.add('focused')
  },
  blur: function(){
    this.el.classList.remove('focused')
  },
  bind: function(){
    var control = this
    this.el.addEventListener('mousedown', function(){
      control.group.use(control)
    })
  }
})

var RadioGroup = Tool.extend({
  init: function(el){
    this.el = el
    this.controls = {}
    var names = el.innerHTML.split(' ')
    el.innerHTML = ''
    var group = this
    names.forEach(function(value){
      var el = document.createElement('span')
      el.classList.add('radio','tool')
      var control = new RadioItem(group, el)
      if (value.substr(0,1) === '*') {
        control.value = value = value.substr(1)
        group.use(control)
      }
      control.value = el.innerHTML = value
      group.controls[value] = control
      group.el.appendChild(el)
    })
  },
  use: function(control){
    if (typeof control === 'string') {
      control = this.controls[control]
    }
    this.selected_control && this.selected_control.blur()
    this.value = control.value
    this.selected_control = control
    control.focus()
    control.use()
    if (this.memorable){
      localStorage.setItem("ascii.tools." + this.name, this.value)
    } 
  },
  bind: function(){
    var tool = this
    for (var n in this.controls){
      this.controls[n].bind()
    }
    if (tool.memorable) {
      var value = localStorage.getItem("ascii.tools." + tool.name)
      if (value) tool.use(value)
    }
  }
})

var Checkbox = Tool.extend({
  init: function (el){
    this.__init(el)
    var name = this.name.replace(/^[x_] /,"")
    var state = localStorage.getItem("ascii.tools." + name) == "true" || this.name[0] == "x"
    this.name = name
    this.update(state)
  },
  update: function(state){
    if (state) this.el.innerHTML = "x " + this.name
    else       this.el.innerHTML = "_ " + this.name
    if (this.memorable) { localStorage.setItem("ascii.tools." + this.name, !! state) }
  }
})

var BlurredCheckbox = Checkbox.extend({
  focus: function(){
    this.use()
  },
  blur: function(){
    this.el.classList.remove('focused')
    this.done()
  }
})

var BlurredTool = Tool.extend({
  focus: function(){
    this.use()
  },
  blur: function(){
    this.el.classList.remove('focused')
    this.done()
  }
})

var HiddenCheckbox = BlurredCheckbox.extend({
  on: "o",
  off: ".",
  init: function (el){
    this.el = el
    this.lex = new Lex (el)
    this.name = this.el.id
    var state = localStorage.getItem("ascii.tools." + name) == "true" || this.el.innerHTML[0] == this.on
    this.update(state)
  },
  update: function(state){
    this.el.innerHTML = state ? this.on : this.off
    if (this.memorable) { localStorage.setItem("ascii.tools." + this.name, !! state) }
    console.trace()
  }
})