Page 1 of 1

Display update

Posted: Sun Jun 06, 2021 10:04 pm
by djg
Hello,

I am making an app for Android using RapaGUI. It seems that widgets will only be updated after WaitEvent() has finished handling an event.
So if I load a file for example, and change the text of a text widget to something like "Loading file...", this will only be visible until *after* the file has been loaded and the function that WaitEvent() calls has finished. Maybe things that take time should not be put in that function? But if not, where else?
As is more often the case, it works fine on Windows, but not on Android.

Re: Display update

Posted: Tue Jun 08, 2021 10:12 am
by Bugala
Without seeing your code it is difficult to know if the problem is unexpected behavior or your code, for I can easily see reasons why this would happen because you have coded it the wrong way.

Re: Display update

Posted: Tue Jun 08, 2021 10:14 pm
by djg
Bugala wrote: Tue Jun 08, 2021 10:12 am Without seeing your code it is difficult to know if the problem is unexpected behavior or your code, for I can easily see reasons why this would happen because you have coded it the wrong way.
Good point. Here it is.

Code: Select all

; Display update test

@REQUIRE "RapaGUI"

Function p_EventHandler(msg)
	Switch(msg.action)
	Case "RapaGUI":
		Switch(msg.id)
		Case "load"
			moai.Set("info", "text", "Loading file...")
			Wait(100)
			moai.Set("info", "text", "File loaded")

		Case "quit"
			moai.FreeApp()
			End
		EndSwitch
	EndSwitch
EndFunction

moai.CreateApp([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window id="mainwindow" title="Display Update Test">
		<vgroup>
			<vgroup>
				<hgroup>
					<label align="left">Info</label>
					<text id="info" frame="true"></text>
				</hgroup>
			</vgroup>
			<hgroup>
				<button id="load">Load</button>
				<button id="quit">Quit</button>
			</hgroup>
		</vgroup>
	</window>
</application>
]])

InstallEventHandler({RapaGUI = p_EventHandler})

While 1
	WaitEvent()
Wend

On Android, when I press the load button I will only see the "File loaded" message.

Re: Display update

Posted: Fri Jun 11, 2021 12:22 am
by amyren
This will make your script do the same thing in Android

Code: Select all

; Display update test

@REQUIRE "RapaGUI"

Function p_EventHandler(msg)
	Switch(msg.action)
	Case "RapaGUI":
		Switch(msg.id)
		Case "load"
			moai.Set("info", "text", "Loading file...")
			RunCallback(Function(msg) 
				Wait(100)
				moai.Set("info", "text", "File Loaded") 
			EndFunction)

		Case "quit"
			moai.FreeApp()
			End
		EndSwitch
	EndSwitch
EndFunction

moai.CreateApp([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window id="mainwindow" title="Display Update Test">
		<vgroup>
			<vgroup>
				<hgroup>
					<label align="left">Info</label>
					<text id="info" frame="true"></text>
				</hgroup>
			</vgroup>
			<hgroup>
				<button id="load">Load</button>
				<button id="quit">Quit</button>
			</hgroup>
		</vgroup>
	</window>
</application>
]])

InstallEventHandler({RapaGUI = p_EventHandler})

While 1
	WaitEvent()
Wend
I can't explain why your first example not work on Android. I even strugle with understanding how WaitEvent() works here, since the main loop never actually loop at all, once it reaches waitevent it will never move on. Just try by removing the while / wend loop and only use WaitEvent() at the end of the script, it will work the same way without the loop.

Re: Display update

Posted: Fri Jun 11, 2021 7:25 pm
by djg
Yes, this works, thanks!

This will have to do for now at least.

I already tried doing stuff after WaitEvent, so I also found out it never returns.
But I never bothered to remove the loop. There is a loop in the documentation, and WaitEvent might work differently later on.

Re: Display update

Posted: Sat Jun 12, 2021 8:56 am
by amyren
You could also use RunCallBack to call it as a separate function if you want it outside the eventhandler

Code: Select all

; Display update test

@REQUIRE "RapaGUI"


moai.CreateApp([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window id="mainwindow" title="Display Update Test">
		<vgroup>
			<vgroup>
				<hgroup>
					<label align="left">Info</label>
					<text id="info" frame="true"></text>
				</hgroup>
			</vgroup>
			<hgroup>
				<button id="load">Load</button>
				<button id="quit">Quit</button>
			</hgroup>
		</vgroup>
	</window>
</application>
]])

Function p_EventHandler(msg)
	Switch(msg.action)
	Case "RapaGUI":
		Switch(msg.id)
		Case "load"
			moai.Set("info", "text", "Loading file..") 
			RunCallback(p_myfunc)
		Case "quit"
			moai.FreeApp()
			End
		EndSwitch
	EndSwitch
EndFunction

Function p_myfunc() 
	Wait(100)
	moai.Set("info", "text", "File loaded") 
EndFunction

InstallEventHandler({RapaGUI = p_EventHandler})

Repeat
	WaitEvent()
Forever

Re: Display update

Posted: Sun Jun 13, 2021 12:00 am
by airsoftsoftwair
Yes, RapaGUI 2.0 on Android will strictly enforce event-based programming so your event callbacks should not block because otherwise the UI will not refresh. So using Wait() in an event callback is a very bad idea. You must return control to WaitEvent() as quickly as possible for the GUI to remain responsive. As Amyren has already pointed out, RunCallback() has been added in Hollywood 9.0 for exactly this purpose.

Re: Display update

Posted: Mon Jun 14, 2021 10:30 am
by djg
Ok, this will take some getting used to, but at least I know now this is how it should be done.

Re: Display update

Posted: Mon Jun 14, 2021 8:28 pm
by airsoftsoftwair
djg wrote: Mon Jun 14, 2021 10:30 am Ok, this will take some getting used to, but at least I know now this is how it should be done.
Keep in mind that this rule only applies when using RapaGUI. When not using RapaGUI, you have full flexibility but with RapaGUI event-based programming will be strictly enforced starting with RapaGUI 2.0.