summaryrefslogtreecommitdiff
path: root/js/ui/controls.js
blob: f7603531396a98029b41ad78c517bf6c4061bd71 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var controls = (function(){

  var controls = {}

  controls.cross = new Tool (cross_el)
  controls.cross.use = function(){
		brush.mask = blit.cross
    brush.generate()
    drawing = true
    brush.modified = false
  }
  controls.cross.done = function(){
    drawing = false
  }

  controls.circle = new Tool (circle_el)
  controls.circle.use = function(){
		brush.mask = blit.circle
    brush.generate()
    drawing = true
    brush.modified = false
  }
  controls.circle.done = function(){
    drawing = false
  }
  
  controls.square = new Tool (square_el)
  controls.square.use = function(){
		brush.mask = blit.square
    brush.generate()
    brush.modified = false
    drawing = true
  }
  controls.square.done = function(){
    drawing = false
  }
  
  controls.text = new Tool (text_el)
  controls.text.use = function(){
    brush.generate = controls.text.generate
    brush.generate()
  }
  controls.text.generate = function(){
  }

  controls.select = new Tool (select_el)
  controls.select.use = function(){
    selection.show()
  }
  controls.select.done = function(){
    selection.hide()
  }

  controls.fill = new Tool (fill_el)
  controls.fill.use = function(){
    filling = true
    document.body.classList.add("bucket")
  }
  controls.fill.done = function(){
    filling = false
    document.body.classList.remove("bucket")
  }
  
  controls.clear = new BlurredTool (clear_el)
  controls.clear.use = function(){
    if (confirm("really delete this colorcode?")) {
      canvas.erase()
    }
  }

  controls.webcam = new BlurredTool (webcam_el)
  controls.webcam.load = function(){
    this.loaded = true
    webcam_close.addEventListener("click", function(){ controls.webcam.blur() })
    window.addEventListener("message", function(e){
      if (e.origin !== window.location.origin) return
      controls.webcam.blur()
      controls.circle.focus()
      import_textarea.value = e.data
      clipboard.import_colorcode()
    })
  }
  controls.webcam.use = function(){
    if (! this.loaded) {
      this.load()
    }
    webcam_iframe.src = "webcam.html"
    webcam_rapper.style.display = "block"
  }
  controls.webcam.done = function(){
    webcam_iframe.src = ""
    webcam_rapper.style.display = "none"
  }
  
  controls.grid = new BlurredCheckbox (grid_el)
  controls.grid.use = function(){
    document.body.classList.toggle('grid')
  	letters.grid = palette.grid = canvas.grid = document.body.classList.contains("grid")
  	canvas.resize_rapper()
  	palette.resize_rapper()
  	letters.resize_rapper()
    if (!selection.hidden) selection.reposition()
    this.update( document.body.classList.contains("grid") )
  }

  ClipboardTool = Tool.extend({
    blur: function(){
      this.__blur()
      clipboard.hide()
    }
  })
  controls.save = new ClipboardTool (save_el)
  controls.save.use = function(){
    clipboard.show()
    clipboard.export_mode()
  }
  controls.load = new ClipboardTool (load_el)
  controls.load.use = function(){
    clipboard.show()
    clipboard.import_mode()
  }
  
  //

  var ShaderTool = Tool.extend({
    active: false,
    use: function(state){
      this.active = typeof state == "boolean" ? state : ! this.active
      if (this.active) {
        shader_rapper.style.display = "block"
        shader_textarea.focus()
      } else {
        shader_rapper.style.display = "none"
      }
    },
    done: function(){
      this.use(false)
    }
  })
  controls.shader = new ShaderTool (shader_el)
  shader_textarea.value = demo_shader.innerHTML
  shader_textarea.addEventListener("input", function(){
    var fn = shader.build(shader_textarea.value)
    fn && shader.run(canvas)
  })
  controls.animate = new Checkbox (animate_checkbox)
  controls.animate.use = function(state){
    var state = shader.toggle()
    this.update(state)
    controls.shader.focus()
    controls.shader.use(true)
  }
 
  controls.experimental_palette = new HiddenCheckbox (experimental_palette_toggle)
  controls.experimental_palette.use = function(state){
    var state = palette.experimental()
    this.update(state)
  }

  //
  
  controls.fg = new BlurredCheckbox (fg_checkbox)
  controls.fg.use = function(state){
    brush.draw_fg = state || ! brush.draw_fg
    this.update(brush.draw_fg)
  }

  controls.bg = new BlurredCheckbox (bg_checkbox)
  controls.bg.use = function(state){
    brush.draw_bg = state || ! brush.draw_bg
    this.update(brush.draw_bg)
  }

  controls.char = new BlurredCheckbox (char_checkbox)
  controls.char.use = function(state){
    brush.draw_char = state || ! brush.draw_char
    this.update(brush.draw_char)
  }
  
  //

  controls.rotate = new BlurredCheckbox (rotate_checkbox)
  controls.rotate.use = function(state){
    canvas.rotated = typeof state == "boolean" ? state : ! canvas.rotated
    canvas.resize_rapper()
    this.update(canvas.rotated)
  }
  
  //
  
  controls.vertical = new BlurredCheckbox (vertical_checkbox)
  controls.vertical.use = function(state){
    canvas.vertical = typeof state == "boolean" ? state : ! canvas.vertical
    controls.vertical.refresh()
  }
  controls.vertical.refresh = function(){
    if (canvas.vertical) {
      document.body.classList.add("vertical")
    }
    else {
      document.body.classList.remove("vertical")
    }
    palette.repaint()
    letters.repaint()
    this.update(canvas.vertical)
  }

  //
  
  controls.width = new Lex (width_el)
  controls.height = new Lex (height_el)
  controls.canvas_width = new Lex (canvas_width_el)
  controls.canvas_height = new Lex (canvas_height_el)

  // bind  
  
  controls.bind = function(){
    [
      controls.width,
      controls.height,
      controls.canvas_width,
      controls.canvas_height
    ].forEach(function(lex){
      lex.span.addEventListener('mousedown', function(e){
        lex.focus()
      })
    });
    
    [
      controls.square,
      controls.circle,
      controls.cross,
      controls.text,
      controls.fill,
      controls.select,
      controls.clear,
      controls.grid,
      controls.webcam,
      controls.fg,
      controls.bg,
      controls.char,
      controls.rotate,
      controls.vertical,
      controls.shader,
      controls.animate,
      controls.save,
      controls.load,
      controls.experimental_palette
    ].forEach(function(tool){
      tool.span.addEventListener('mousedown', function(e){
        tool.focus()
      })
    })
  
    controls.width.key = keys.single_numeral_key(controls.width, brush, "w", 1, 10)
    controls.width.raw_key = keys.arrow_key(controls.width, brush, "w", "rebuild", 1, 100)

    controls.height.key = keys.single_numeral_key(controls.height, brush, "h", 1, 10)
    controls.height.raw_key = keys.arrow_key(controls.height, brush, "h", "rebuild", 1, 100)

    controls.canvas_width.raw_key = keys.arrow_key(controls.canvas_width, canvas, "w", "resize", 1, 999)
    controls.canvas_width.key = keys.multi_numeral_key(controls.canvas_width, 3)
    controls.canvas_width.onBlur = keys.multi_numeral_blur(controls.canvas_width, canvas, "w", 1, 999)

    controls.canvas_height.raw_key = keys.arrow_key(controls.canvas_height, canvas, "h", "resize", 1, 999)
    controls.canvas_height.key = keys.multi_numeral_key(controls.canvas_height, 3)
    controls.canvas_height.onBlur = keys.multi_numeral_blur(controls.canvas_height, canvas, "h", 1, 999)
        
    add_custom_el.addEventListener("click", function(){
      custom.clone()
    })

  }

  return controls
})()