summaryrefslogtreecommitdiff
path: root/js/ui/controls.js
blob: bd475c180e4b003b4111f37cc2222729b1ea5c62 (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
var controls = (function(){

	var controls = {}
	
	controls.circle = new Tool (circle_el)
	controls.circle.use = function(){
		brush.generate = controls.circle.generate
		brush.generate()
		drawing = true
		brush.modified = false
	}
	controls.circle.generate = function(){
		var fg = brush.fg, bg = brush.bg
		var hw = brush.w/2|0, hh = brush.h/2|0
		brush.forEach(function(lex,x,y) {
			var len = Math.sqrt(Math.pow(x-hw,2)+Math.pow(y-hh,2))
			if (len > Math.abs(hw)) {
				lex.clear()
			}
			else {
				lex.fill(fg,bg)
			}
		})
	}
	
	controls.square = new Tool (square_el)
	controls.square.use = function(){
		brush.generate = controls.square.generate
		brush.generate()
		drawing = true
		brush.modified = false
	}
	controls.square.generate = function(){
		var fg = brush.fg, bg = brush.bg
		brush.fill(fg,bg)
	}
	
	controls.text = new Tool (text_el)
	controls.text.use = function(){
		brush.generate = controls.text.generate
		brush.generate()
		drawing = false
	}
	controls.text.generate = function(){
	}
	
	controls.clear = new Tool (clear_el)
	controls.clear.use = function(){
		canvas.clear()
	}
	
	controls.grid = new Tool (grid_el)
	controls.grid.use = function(){
		document.body.classList.toggle('grid')
	}

	controls.shader = new Tool (shader_el)
	controls.shader.use = function(){
		shader_textarea.style.display = "block"
		// setTimeout(function(){ shader_textarea.focus() })
		shader_textarea.focus()
	}
	controls.shader.blur = function(){
		Tool.prototype.blur.call(this)
		shader_textarea.style.display = "none"		
	}
	shader_textarea.value = demo_shader.innerHTML
	shader_textarea.addEventListener("input", function(){
		var fn = shader.build(shader_textarea.value)
		fn && shader.run(canvas)
	})
	
	controls.save = new Tool (save_el)
	controls.save.use = function(){
		clipboard.show()
		clipboard.export_mode()
	}
	controls.save.blur = function(){
		Tool.prototype.blur.call(this)
		clipboard.hide()
	}
	controls.load = new Tool (load_el)
	controls.load.use = function(){
		clipboard.show()
		clipboard.import_mode()
	}
	controls.load.blur = function(){
		Tool.prototype.blur.call(this)
		clipboard.hide()
	}
		
	controls.animate = new Tool (animate_checkbox)
	controls.animate.use = function(){
		var state = shader.toggle()
		if (state) this.el.innerHTML = "x animate"
		else       this.el.innerHTML = "_ animate"
	}
	
	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.text,
			controls.clear,
			controls.grid,
			controls.shader,
			controls.animate,
			controls.save,
			controls.load
		].forEach(function(tool){
			tool.span.addEventListener('mousedown', function(e){
				tool.focus()
			})
		})
	
		controls.width.key = int_key(function(n, keyCode){
      controls.width.blur()
			controls.width.char = ""+n
			controls.width.build()
			brush.w = n
			brush.rebuild()
		})
		controls.height.key = int_key(function(n, keyCode){
      controls.height.blur()
			controls.height.char = ""+n
			controls.height.build()
			brush.h = n
			brush.rebuild()
		})
	
		controls.canvas_width.key = int_key(function(n, keyCode){
			controls.canvas_width.read()
			if (controls.canvas_width.char.length == 1) {
				n = parseInt(controls.canvas_width.char) * 10 + n
			}
			controls.canvas_width.char = ""+n
			controls.canvas_width.build()
		})
		controls.canvas_width.onBlur = function(){
		  var w = parseInt(controls.canvas_width.char) || 1
		  controls.canvas_width.char = w+""
		  controls.canvas_width.build()
			canvas.resize(w, canvas.h)
		}
		controls.canvas_height.onBlur = function(){
		  var h = parseInt(controls.canvas_height.char) || 1
		  controls.canvas_height.char = h+""
		  controls.canvas_height.build()
			canvas.resize(canvas.w, h)
		}

		controls.canvas_height.key = int_key(function(n, keyCode){
			controls.canvas_height.read()
			if (controls.canvas_height.char.length == 1) {
				n = parseInt(controls.canvas_height.char) * 10 + n
			}
			controls.canvas_height.char = ""+n
			controls.canvas_height.build()
			canvas.resize(canvas.w, n)
		})
	}

  function int_key (f) {
    return function (key, keyCode) {
      var n = parseInt(key)
      ! isNaN(n) && f(n)
    }
  }

	return controls
})()