Skip to content

Commit 62684f9

Browse files
authored
Merge pull request #8 from AGulev/master
Replace strings with constants / new type of transactions / information for the focus messages
2 parents c9cf627 + 4ead8d9 commit 62684f9

File tree

5 files changed

+142
-68
lines changed

5 files changed

+142
-68
lines changed

.luacheckrc

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
std = "max"
2+
files['.luacheckrc'].global = false
3+
unused_args = false
4+
5+
globals = {
6+
"sys",
7+
"go",
8+
"gui",
9+
"label",
10+
"render",
11+
"crash",
12+
"sprite",
13+
"sound",
14+
"tilemap",
15+
"spine",
16+
"particlefx",
17+
"physics",
18+
"factory",
19+
"collectionfactory",
20+
"iac",
21+
"msg",
22+
"vmath",
23+
"url",
24+
"http",
25+
"image",
26+
"json",
27+
"zlib",
28+
"iap",
29+
"push",
30+
"facebook",
31+
"hash",
32+
"hash_to_hex",
33+
"pprint",
34+
"init",
35+
"final",
36+
"update",
37+
"on_input",
38+
"on_message",
39+
"on_reload",
40+
"window",
41+
"unityads"
42+
}

example/popup.gui_script

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ function init(self)
88
gui.set_render_order(15)
99

1010
self.transition = transitions.create(gui.get_node("root"))
11-
.show_in(transitions.slide_in_top, gui.EASING_OUTQUAD, 0.6, 0)
12-
.show_out(transitions.slide_out_top, gui.EASING_INQUAD, 0.6, 0)
13-
.back_in(transitions.slide_in_top, gui.EASING_OUTQUAD, 0.6, 0)
14-
.back_out(transitions.slide_out_top, gui.EASING_INQUAD, 0.6, 0)
11+
.show_in(transitions.scale_in, gui.EASING_OUTBACK, 0.3, 0)
12+
.show_out(transitions.scale_out, gui.EASING_INBACK, 0.3, 0)
13+
.back_in(transitions.scale_in, gui.EASING_OUTBACK, 0.3, 0)
14+
.back_out(transitions.scale_out, gui.EASING_INBACK, 0.3, 0)
1515
end
1616

1717
function on_input(self, action_id, action)

monarch/monarch.lua

+37-25
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ local CONTEXT = hash("monarch_context")
88
local PROXY_LOADED = hash("proxy_loaded")
99
local PROXY_UNLOADED = hash("proxy_unloaded")
1010

11-
M.TRANSITION_DONE = hash("transition_done")
11+
local RELEASE_INPUT_FOCUS = hash("release_input_focus")
12+
local ACQUIRE_INPUT_FOCUS = hash("acquire_input_focus")
13+
local ASYNC_LOAD = hash("async_load")
14+
local UNLOAD = hash("unload")
15+
local ENABLE = hash("enable")
16+
17+
M.TRANSITION = {}
18+
M.TRANSITION.DONE = hash("transition_done")
19+
M.TRANSITION.SHOW_IN = hash("transition_show_in")
20+
M.TRANSITION.SHOW_OUT = hash("transition_show_out")
21+
M.TRANSITION.BACK_IN = hash("transition_back_in")
22+
M.TRANSITION.BACK_OUT = hash("transition_back_out")
23+
1224
M.FOCUS_GAINED = hash("monarch_focus_gained")
1325
M.FOCUS_LOST = hash("monarch_focus_lost")
1426

1527

1628
local function screen_from_proxy(proxy)
17-
for _,screen in pairs(screens) do
29+
for _, screen in pairs(screens) do
1830
if screen.proxy == proxy then
1931
return screen
2032
end
@@ -23,15 +35,15 @@ end
2335

2436
local function screen_from_script()
2537
local url = msg.url()
26-
for _,screen in pairs(screens) do
38+
for _, screen in pairs(screens) do
2739
if screen.script == url then
2840
return screen
2941
end
3042
end
3143
end
3244

3345
local function in_stack(id)
34-
for i=1,#stack do
46+
for i = 1, #stack do
3547
if stack[i].id == id then
3648
return true
3749
end
@@ -60,18 +72,18 @@ local function show_out(screen, next_screen, cb)
6072
local co
6173
co = coroutine.create(function()
6274
screen.co = co
63-
msg.post(screen.script, "release_input_focus")
75+
msg.post(screen.script, RELEASE_INPUT_FOCUS)
6476
msg.post(screen.script, CONTEXT)
6577
coroutine.yield()
6678
if not next_screen.popup then
67-
msg.post(screen.transition_url, "transition_show_out")
79+
msg.post(screen.transition_url, M.TRANSITION.SHOW_OUT)
6880
coroutine.yield()
69-
msg.post(screen.proxy, "unload")
81+
msg.post(screen.proxy, UNLOAD)
7082
coroutine.yield()
7183
screen.loaded = false
7284
end
7385
if screen.focus_url then
74-
msg.post(screen.focus_url, M.FOCUS_LOST)
86+
msg.post(screen.focus_url, M.FOCUS_LOST, {id = next_screen.id})
7587
end
7688
screen.co = nil
7789
if cb then cb() end
@@ -86,25 +98,25 @@ local function show_in(screen, reload, cb)
8698
msg.post(screen.script, CONTEXT)
8799
coroutine.yield()
88100
if reload and screen.loaded then
89-
msg.post(screen.proxy, "unload")
101+
msg.post(screen.proxy, UNLOAD)
90102
coroutine.yield()
91103
screen.loaded = false
92104
end
93105
-- the screen could be loaded if the previous screen was a popup
94106
-- and the popup asked to show this screen again
95107
-- in that case we shouldn't attempt to load it again
96108
if not screen.loaded then
97-
msg.post(screen.proxy, "async_load")
109+
msg.post(screen.proxy, ASYNC_LOAD)
98110
coroutine.yield()
99-
msg.post(screen.proxy, "enable")
111+
msg.post(screen.proxy, ENABLE)
100112
screen.loaded = true
101113
end
102114
stack[#stack + 1] = screen
103-
msg.post(screen.transition_url, "transition_show_in")
115+
msg.post(screen.transition_url, M.TRANSITION.SHOW_IN)
104116
coroutine.yield()
105-
msg.post(screen.script, "acquire_input_focus")
117+
msg.post(screen.script, ACQUIRE_INPUT_FOCUS)
106118
if screen.focus_url then
107-
msg.post(screen.focus_url, M.FOCUS_GAINED)
119+
msg.post(screen.focus_url, M.FOCUS_GAINED, {id = screen.id})
108120
end
109121
screen.co = nil
110122
if cb then cb() end
@@ -119,16 +131,16 @@ local function back_in(screen, previous_screen, cb)
119131
msg.post(screen.script, CONTEXT)
120132
coroutine.yield()
121133
if not previous_screen.popup then
122-
msg.post(screen.proxy, "async_load")
134+
msg.post(screen.proxy, ASYNC_LOAD)
123135
coroutine.yield()
124-
msg.post(screen.proxy, "enable")
136+
msg.post(screen.proxy, ENABLE)
125137
screen.loaded = true
126-
msg.post(screen.transition_url, "transition_back_in")
138+
msg.post(screen.transition_url, M.TRANSITION.BACK_IN)
127139
coroutine.yield()
128140
end
129-
msg.post(screen.script, "acquire_input_focus")
141+
msg.post(screen.script, ACQUIRE_INPUT_FOCUS)
130142
if screen.focus_url then
131-
msg.post(screen.focus_url, M.FOCUS_GAINED)
143+
msg.post(screen.focus_url, M.FOCUS_GAINED, {id = previous_screen.id})
132144
end
133145
screen.co = nil
134146
if cb then cb() end
@@ -140,16 +152,16 @@ local function back_out(screen, cb)
140152
local co
141153
co = coroutine.create(function()
142154
screen.co = co
143-
msg.post(screen.script, "release_input_focus")
155+
msg.post(screen.script, RELEASE_INPUT_FOCUS)
144156
msg.post(screen.script, CONTEXT)
145157
coroutine.yield()
146-
msg.post(screen.transition_url, "transition_back_out")
158+
msg.post(screen.transition_url, M.TRANSITION.BACK_OUT)
147159
coroutine.yield()
148-
msg.post(screen.proxy, "unload")
160+
msg.post(screen.proxy, UNLOAD)
149161
coroutine.yield()
150162
screen.loaded = false
151163
if screen.focus_url then
152-
msg.post(screen.focus_url, M.FOCUS_LOST)
164+
msg.post(screen.focus_url, M.FOCUS_LOST, {id = screen.id})
153165
end
154166
screen.co = nil
155167
if cb then cb() end
@@ -254,7 +266,7 @@ function M.on_message(message_id, message, sender)
254266
local screen = screen_from_script()
255267
assert(screen, "Unable to find screen for current script url")
256268
coroutine.resume(screen.co)
257-
elseif message_id == M.TRANSITION_DONE then
269+
elseif message_id == M.TRANSITION.DONE then
258270
local screen = screen_from_script()
259271
assert(screen, "Unable to find screen for current script url")
260272
coroutine.resume(screen.co)
@@ -263,7 +275,7 @@ end
263275

264276
function M.dump_stack()
265277
local s = ""
266-
for i,screen in ipairs(stack) do
278+
for i, screen in ipairs(stack) do
267279
s = s .. ("%d = %s\n"):format(i, tostring(screen.id))
268280
end
269281
return s

monarch/screen.script

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ function on_message(self, message_id, message, sender)
2222
monarch.back()
2323
elseif message_id == hash("back") then
2424
monarch.back()
25-
elseif message_id == hash("transition_show_in")
26-
or message_id == hash("transition_show_out")
27-
or message_id == hash("transition_back_in")
28-
or message_id == hash("transition_back_out") then
29-
msg.post(sender, "transition_done")
30-
else
31-
monarch.on_message(message_id, message, sender)
25+
elseif message_id == monarch.TRANSITION.SHOW_IN
26+
or message_id == monarch.TRANSITION.SHOW_OUT
27+
or message_id == monarch.TRANSITION.BACK_IN
28+
or message_id == monarch.TRANSITION.BACK_OUT then
29+
msg.post(sender, monarch.TRANSITION.DONE)
30+
else
31+
monarch.on_message(message_id, message, sender)
32+
end
3233
end
33-
end

0 commit comments

Comments
 (0)