summaryrefslogtreecommitdiff
path: root/js/app.js
blob: bebfec2b5086f955a0778d6422b1b8a3c7db3554 (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
var dragging = false
var drawing = true
var erasing = false
var focused

var canvas, tools, palette, controls, brush, mode, current_tool, current_canvas

function init () {
  build()
  bind()
}
function build () {
	shader.init()
//	shader.run(canvas)
	shader.animate()

	canvas.append(canvas_rapper)
	brush.append(brush_rapper)
	palette.append(palette_rapper)
	
  controls.circle.focus()
//  controls.shader.focus()

	brush.bg = colors.red
	brush.generate()
	brush.build()
}
function bind () {
	canvas.bind()
	palette.bind()
	brush.bind()
	controls.bind()
	
  window.addEventListener('mouseup', function(){
    dragging = erasing = false
		if (current_tool.name != 'shader' && current_tool.name != 'load' && current_tool.name != 'save') { cursor_input.focus() }
  });
  
	window.addEventListener('mousedown', function(e){
		if (current_tool.name == "shader") { return }
		cursor_input.focus()
	})

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

		console.log("keycode:", e.keyCode)
		
		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
					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-1, focused.x)
				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 + direction[0], focused.x + direction[1])
				break
			case 40: // down
				e.preventDefault()
				current_canvas.focusLex(focused.y + direction[0], focused.x + direction[1])
				break
			case 37: // left
				e.preventDefault()
				current_canvas.focusLex(focused.y + direction[0], focused.x + direction[1])
				break
			case 39: // right
				e.preventDefault()
				current_canvas.focusLex(focused.y + direction[0], focused.x + direction[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)
			current_canvas.focusLex(y + direction[0], focused.x + direction[1])
		}
	})

	document.addEventListener('DOMContentLoaded', function(){
		if (current_tool.name != 'shader') { cursor_input.focus() }
		document.body.classList.remove('loading')
	})
}


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

init()