1local overlay
2local keypad
3local touchStart = 0
4local updateCursorEvent
5local zoomInButton
6local zoomOutButton
7local keypadButton
8local keypadEvent
9local keypadMousePos = {x=0.5, y=0.5}
10local keypadTicks = 0
11local delayMobileBt = 1000
12
13-- public functions
14function init()
15 if not g_app.isMobile() then return end
16 overlay = g_ui.displayUI('mobile')
17 attackerButton = overlay:recursiveGetChildById('attacker')
18 menuButton = overlay:recursiveGetChildById('menu')
19 optionsButton = overlay:recursiveGetChildById('options')
20 exitButton = overlay:recursiveGetChildById('exitgame')
21 mountButton = overlay:recursiveGetChildById('buttonMount')
22 bugButton = overlay:recursiveGetChildById('buttonBug')
23 keypad = overlay:recursiveGetChildById('keypad')
24
25 buttonChat = overlay:recursiveGetChildById('buttonChat')
26 buttonChat:hide()
27
28 onlineMobile = overlay:recursiveGetChildById('onlineMobile')
29 goldMobile = overlay:recursiveGetChildById('goldMobile')
30 onlineMobile:hide()
31 goldMobile:hide()
32
33 mobileDGTime = overlay:recursiveGetChildById('mobileDGTime')
34 mobileDGDesistir = overlay:recursiveGetChildById('mobileDGDesistir')
35 mobileDGTime:hide()
36 mobileDGDesistir:hide()
37
38 skillBar = overlay:recursiveGetChildById('skillBar')
39 skill1 = overlay:recursiveGetChildById('spell01')
40 skill2 = overlay:recursiveGetChildById('spell02')
41 skill3 = overlay:recursiveGetChildById('spell03')
42 suporte1 = overlay:recursiveGetChildById('sup01')
43 suporte2 = overlay:recursiveGetChildById('sup02')
44 qntSkill = overlay:recursiveGetChildById('qntSkill')
45 suporte3 = overlay:recursiveGetChildById('sup03')
46 skillBar:hide()
47
48 attackerButton:hide()
49 menuButton:hide()
50 exitButton:hide()
51 optionsButton:hide()
52 bugButton:hide()
53 mountButton:hide()
54
55 hpBar = overlay:recursiveGetChildById('hpBar')
56 levelInfo = overlay:recursiveGetChildById('levelInfo')
57 ammoInfo = overlay:recursiveGetChildById('ammoInfo')
58 expBar = overlay:recursiveGetChildById('expBar')
59 condMobile = overlay:recursiveGetChildById('conditionMobilePanel')
60 hpBar:hide()
61 levelInfo:hide()
62 ammoInfo:hide()
63 expBar:hide()
64
65 menuButton:enable()
66
67 menuButton.onClick = function()
68 modules.game_menu.mobileMenuBar()
69 menuButton:disable()
70 scheduleEvent(function()
71 menuButton:enable()
72 end, delayMobileBt)
73 end
74 attackerButton.onClick = function() AttackMobile() scheduleEvent(function() attackerButton:enable() end, 1000) end
75 keypad = overlay.keypad
76 overlay:raise()
77
78 zoomInButton = modules.client_topmenu.addLeftButton('zoomInButton', 'Zoom In', '/images/topbuttons/zoomin', function() g_app.scaleUp() end)
79 zoomOutButton = modules.client_topmenu.addLeftButton('zoomOutButton', 'Zoom Out', '/images/topbuttons/zoomout', function() g_app.scaleDown() end)
80 keypadButton = modules.client_topmenu.addLeftGameToggleButton('keypadButton', 'Keypad', '/images/topbuttons/keypad', function()
81 keypadButton:setChecked(not keypadButton:isChecked())
82 if not g_game.isOnline() then
83 keypad:setVisible(false)
84 return
85 end
86 keypad:setVisible(keypadButton:isChecked())
87 end)
88 keypadButton:setChecked(true)
89
90 scheduleEvent(function()
91 g_app.scale(5.0)
92 end, 10)
93
94 connect(overlay, {
95 onMousePress = onMousePress,
96 onMouseRelease = onMouseRelease,
97 onTouchPress = onMousePress,
98 onTouchRelease = onMouseRelease,
99 onMouseMove = onMouseMove
100 })
101 connect(keypad, {
102 onTouchPress = onKeypadTouchPress,
103 onTouchRelease = onKeypadTouchRelease,
104 onMouseMove = onKeypadTouchMove
105 })
106 connect(g_game, {
107 onGameStart = online,
108 onGameEnd = offline
109 })
110 if g_game.isOnline() then
111 online()
112 end
113end
114
115function onMobileButton(id, type)
116 if id == 01 then
117 return hpBar
118 elseif id == 02 then
119 return expBar
120 elseif id == 03 then
121 return levelInfo
122 elseif id == 04 then
123 return ammoInfo
124 elseif id == 05 then
125 return attackerButton
126 elseif id == 06 then
127 return condMobile
128 elseif id == 07 then
129 return skillBar
130 elseif id == 08 then
131 return skill1
132 elseif id == 09 then
133 return skill2
134 elseif id == 10 then
135 return skill3
136 elseif id == 11 then
137 return suporte1
138 elseif id == 12 then
139 return suporte2
140 elseif id == 13 then
141 return suporte3
142 elseif id == 14 then
143 return qntSkill
144 elseif id == 15 then
145 return onlineMobile
146 elseif id == 16 then
147 return goldMobile
148 elseif id == 17 then
149 return mobileDGTime
150 elseif id == 18 then
151 return mobileDGDesistir
152 end
153end
154function onHideUI(id)
155 if id == 00 then
156 condMobile:hide()
157 mountButton:hide()
158 bugButton:hide()
159 attackerButton:hide()
160 menuButton:hide()
161 exitButton:hide()
162 optionsButton:hide()
163 hpBar:hide()
164 levelInfo:hide()
165 ammoInfo:hide()
166 expBar:hide()
167 skillBar:hide()
168 onlineMobile:hide()
169 goldMobile:hide()
170 keypad:hide()
171 buttonChat:hide()
172 elseif id == 02 then
173 attackerButton:hide()
174 ammoInfo:hide()
175 elseif id == 03 then
176 attackerButton:show()
177 ammoInfo:show()
178 else
179 mountButton:show()
180 bugButton:show()
181 condMobile:show()
182 attackerButton:show()
183 menuButton:show()
184 exitButton:show()
185 optionsButton:show()
186 hpBar:show()
187 levelInfo:show()
188 ammoInfo:show()
189 expBar:show()
190 skillBar:show()
191 onlineMobile:show()
192 goldMobile:show()
193 keypad:show()
194 buttonChat:show()
195 end
196end
197function AttackMobile()
198 local battlePanel = modules.game_battle.battlePanel
199 local child = battlePanel:getFirstChild()
200 local nextChild = nil
201 local attacking = g_game.getAttackingCreature()
202 if child and (not child.creature or not child:isOn()) then
203 child = nil
204 end
205 if child then
206 if not attacking then
207 g_game.attack(child.creature)
208 else
209 local battlePanel = modules.game_battle.battlePanel
210 local attackedCreature = g_game.getAttackingCreature()
211 local nextChild = nil
212 local breakNext = false
213 for i, child in ipairs(battlePanel:getChildren()) do
214 if not child.creature or not child:isOn() then
215 break
216 end
217 nextChild = child
218 if breakNext then
219 break
220 end
221 if child.creature == attackedCreature then
222 breakNext = true
223 nextChild = battlePanel:getFirstChild()
224 end
225 end
226 if not breakNext then
227 nextChild = battlePanel:getFirstChild()
228 end
229 if nextChild and nextChild.creature ~= attackedCreature then
230 g_game.attack(nextChild.creature)
231 end
232 end
233 else
234 g_game.attack(nil)
235 end
236end
237function terminate()
238 if not g_app.isMobile() then return end
239 removeEvent(updateCursorEvent)
240 removeEvent(keypadEvent)
241 keypadEvent = nil
242 disconnect(overlay, {
243 onMousePress = onMousePress,
244 onMouseRelease = onMouseRelease,
245 onTouchPress = onMousePress,
246 onTouchRelease = onMouseRelease,
247 onMouseMove = onMouseMove
248 })
249 disconnect(keypad, {
250 onTouchPress = onKeypadTouchPress,
251 onTouchRelease = onKeypadTouchRelease,
252 onMouseMove = onKeypadTouchMove
253 })
254 disconnect(g_game, {
255 onGameStart = online,
256 onGameEnd = offline
257 })
258 zoomInButton:destroy()
259 zoomOutButton:destroy()
260 keypadButton:destroy()
261 overlay:destroy()
262 overlay = nil
263end
264
265function hide()
266 overlay:hide()
267 attackerButton:hide()
268 menuButton:hide()
269 exitButton:hide()
270 optionsButton:hide()
271 hpBar:hide()
272 levelInfo:hide()
273 ammoInfo:hide()
274 expBar:hide()
275 skillBar:hide()
276 onlineMobile:hide()
277 goldMobile:hide()
278 buttonChat:hide()
279 bugButton:hide()
280 mountButton:hide()
281end
282
283function show()
284 overlay:show()
285 attackerButton:show()
286 menuButton:show()
287 exitButton:show()
288 optionsButton:show()
289 hpBar:show()
290 levelInfo:show()
291 ammoInfo:show()
292 expBar:show()
293 skillBar:show()
294 onlineMobile:show()
295 goldMobile:show()
296 buttonChat:show()
297end
298
299function online()
300 if keypadButton:isChecked() then
301 keypad:raise()
302 keypad:show()
303 end
304 attackerButton:show()
305 menuButton:show()
306 exitButton:show()
307 optionsButton:show()
308 hpBar:show()
309 levelInfo:show()
310 ammoInfo:show()
311 expBar:show()
312 skillBar:show()
313 onlineMobile:show()
314 goldMobile:show()
315 buttonChat:show()
316end
317
318function offline()
319 keypad:hide()
320 attackerButton:hide()
321 menuButton:hide()
322 exitButton:hide()
323 optionsButton:hide()
324 hpBar:hide()
325 levelInfo:hide()
326 ammoInfo:hide()
327 expBar:hide()
328 skillBar:hide()
329 onlineMobile:hide()
330 goldMobile:hide()
331 buttonChat:hide()
332 bugButton:hide()
333 mountButton:hide()
334end
335
336function onMouseMove(widget, pos, offset)
337
338end
339
340function onMousePress(widget, pos, button)
341 overlay:raise()
342 if button == MouseTouch then -- touch
343 overlay:raise()
344 overlay.cursor:show()
345 overlay.cursor:setPosition({x=pos.x - 32, y = pos.y - 32})
346 touchStart = g_clock.millis()
347 updateCursor()
348 else
349 overlay.cursor:hide()
350 removeEvent(updateCursorEvent)
351 end
352end
353
354function onMouseRelease(widget, pos, button)
355 if button == MouseTouch then
356 overlay.cursor:hide()
357 removeEvent(updateCursorEvent)
358 end
359end
360
361function updateCursor()
362 removeEvent(updateCursorEvent)
363 if not g_mouse.isPressed(MouseTouch) then return end
364 local percent = 100 - math.max(0, math.min(100, (g_clock.millis() - touchStart) / 5)) -- 500 ms
365 overlay.cursor:setPercent(percent)
366 if percent > 0 then
367 overlay.cursor:setOpacity(0.5)
368 updateCursorEvent = scheduleEvent(updateCursor, 10)
369 else
370 overlay.cursor:setOpacity(0.8)
371 end
372end
373
374function onKeypadTouchMove(widget, pos, offset)
375 keypadMousePos = {x=(pos.x - widget:getPosition().x) / widget:getWidth(),
376 y=(pos.y - widget:getPosition().y) / widget:getHeight()}
377 return true
378end
379
380function onKeypadTouchPress(widget, pos, button)
381 if button ~= MouseTouch then return false end
382 keypadTicks = 0
383 keypadMousePos = {x=(pos.x - widget:getPosition().x) / widget:getWidth(),
384 y=(pos.y - widget:getPosition().y) / widget:getHeight()}
385 executeWalk()
386 return true
387end
388
389function onKeypadTouchRelease(widget, pos, button)
390 if button ~= MouseTouch then return false end
391 keypadMousePos = {x=(pos.x - widget:getPosition().x) / widget:getWidth(),
392 y=(pos.y - widget:getPosition().y) / widget:getHeight()}
393 executeWalk()
394 removeEvent(keypadEvent)
395 keypad.pointer:setMarginTop(0)
396 keypad.pointer:setMarginLeft(0)
397 return true
398end
399
400function executeWalk()
401 removeEvent(keypadEvent)
402 keypadEvent = nil
403 if not modules.game_walking or not g_mouse.isPressed(MouseTouch) then
404 keypad.pointer:setMarginTop(0)
405 keypad.pointer:setMarginLeft(0)
406 return
407 end
408 keypadEvent = scheduleEvent(executeWalk, 20)
409 keypadMousePos.x = math.min(1, math.max(0, keypadMousePos.x))
410 keypadMousePos.y = math.min(1, math.max(0, keypadMousePos.y))
411 local angle = math.atan2(keypadMousePos.x - 0.5, keypadMousePos.y - 0.5)
412 local maxTop = math.abs(math.cos(angle)) * 75
413 local marginTop = math.max(-maxTop, math.min(maxTop, (keypadMousePos.y - 0.5) * 150))
414 local maxLeft = math.abs(math.sin(angle)) * 75
415 local marginLeft = math.max(-maxLeft, math.min(maxLeft, (keypadMousePos.x - 0.5) * 150))
416 keypad.pointer:setMarginTop(marginTop)
417 keypad.pointer:setMarginLeft(marginLeft)
418 local dir
419 if keypadMousePos.y < 0.3 and keypadMousePos.x < 0.3 then
420 dir = Directions.NorthWest
421 elseif keypadMousePos.y < 0.3 and keypadMousePos.x > 0.7 then
422 dir = Directions.NorthEast
423 elseif keypadMousePos.y > 0.7 and keypadMousePos.x < 0.3 then
424 dir = Directions.SouthWest
425 elseif keypadMousePos.y > 0.7 and keypadMousePos.x > 0.7 then
426 dir = Directions.SouthEast
427 end
428 if not dir and (math.abs(keypadMousePos.y - 0.5) > 0.1 or math.abs(keypadMousePos.x - 0.5) > 0.1) then
429 if math.abs(keypadMousePos.y - 0.5) > math.abs(keypadMousePos.x - 0.5) then
430 if keypadMousePos.y < 0.5 then
431 dir = Directions.North
432 else
433 dir = Directions.South
434 end
435 else
436 if keypadMousePos.x < 0.5 then
437 dir = Directions.West
438 else
439 dir = Directions.East
440 end
441 end
442 end
443 if dir then
444 modules.game_walking.walk(dir, keypadTicks)
445 if keypadTicks == 0 then
446 keypadTicks = 100
447 end
448 end
449end
450