summaryrefslogtreecommitdiff
path: root/src/ui.moon
blob: 7a06522b939a01e7c9ee5b831bf6c59191bf332b (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
hc = require("party.hardoncollider.init")
win = require("window")
Button = require("ui.button")
Joystick = require("ui.joystick")
Textbox = require("ui.textbox")

ui_world = hc.new(64)

ui = {}
ui.events = {
	touch: {}
	mouse: {}
	controller: {}
	keyboard: {}
}
ui.button = (x,y,width,height,text) ->
	button = Button(x,y,width,height,text)
	ui.node\append(button.node)
	bounds = ui_world\rectangle(x,y,width,height)
	ui.events.touch[bounds] = button
	ui.events.mouse[bounds] = button
	button

ui.click = (x,y) ->
	ui_world\shapesAt(x,y)

ui.joystick = (x,y,r) ->
	joystick = Joystick(x,y,r)
	ui.node\append(joystick.node)
	bounds = ui_world\circle(x,y,r)
	ui.events.touch[bounds] = joystick

ui.textbox = (x,y,width,height,value,placeholder) ->
	value = value or ""
	placeholder = placeholder or ""
	textbox = Textbox(x,y,width,height,value,placeholder)
	ui.node\append(textbox.node)
	bounds = ui_world\rectangle(x,y,width,height)
	ui.events.mouse[bounds] = textbox
	ui.events.keyboard[textbox] = true


ui.node = am.group!

has_fire = (obj) ->
	assert(obj.fire, obj.__class.__name .. " doesn't have a .fire method")

ui.dbg = am.translate(0,0)\append(am.circle(vec2(0,0),5,vec4(0,0,0,1)))
ui.node\append(ui.dbg)

ui.node\action(() ->
	pos = win\mouse_position()
	down = win\mouse_pressed("left")
	up = win\mouse_released("left")
	wheel = win\mouse_wheel_delta()
	keys = win\keys_pressed()
	-- Debugging for mouse position:
	ui.dbg.position2d = pos
	mo_tbl =
		event: "mouse_over"
		data: pos
	md_tbl = 
		event: "mouse_down"
		data: pos
	mu_tbl = 
		event: "mouse_up"
		data: pos
	for collider,_ in pairs(ui_world\shapesAt(pos.x, pos.y))
		match = ui.events.mouse[collider]
		if match
			has_fire(match)
			match\fire(mo_tbl)
			if down
				match\fire(md_tbl)
			if up
				match\fire(mu_tbl)
	if math.length(wheel) > 0
		etbl =
			event: "mouse_scroll"
			data: wheel
		for collider, uiobj in pairs(ui.events.mouse)
			has_fire(uiobj)
			uiobj\fire(etbl)
	if #keys > 0
		etbl = 
			event: "keys_pressed"
			data: keys
			shift: win\key_down("lshift") or win\key_down("rshift")
		for uiobj, _ in pairs(ui.events.keyboard)
			has_fire(uiobj)
			if uiobj\fire(etbl)
				break -- allow any keyboard listener to "trap" the signal by returning true

--	in_touch_events = {
--		"active_touch"
--		"touches_began"
--	}
--	for touch in *win\active_touches!
--		tpos = win\touch_position(touch)
--		etbl = 
--			event: "active_touch"
--			data: tpos
--		for collider,_ in pairs(ui_world\shapesAt(tpos.x, tpos.y))
--			print("Touched collider:", collider)
--			match = ui.events.touch[collider]
--			if match
--				has_fire(match)
--				match\fire(etbl)
--		delta = win\touch_delta(touch)
--		if math.length(delta) > 0
--			dtbl = 
--				event: "touch_delta"
--				data: delta
--			for _, uiobj in pairs(ui.events.touch)
--				has_fire(uiobj)
--				uiobj\fire(dtbl)
--	for touch in *win\touches_ended!
--		etbl = 
--			event: "touches_ended"
--			data: win\touch_position(touch)
--		for _,uiobj in pairs(ui.events.touch)
--			has_fire(uiobj)
--			uiobj\fire(etbl)
	-- todo: expand this with controller support.
)

ui