Display update

Discuss GUI programming with the RapaGUI plugin here
Post Reply
djg
Posts: 16
Joined: Wed Feb 12, 2020 10:37 pm

Display update

Post 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.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Display update

Post 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.
djg
Posts: 16
Joined: Wed Feb 12, 2020 10:37 pm

Re: Display update

Post 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.
amyren
Posts: 352
Joined: Thu May 02, 2019 11:53 am

Re: Display update

Post 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.
djg
Posts: 16
Joined: Wed Feb 12, 2020 10:37 pm

Re: Display update

Post 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.
amyren
Posts: 352
Joined: Thu May 02, 2019 11:53 am

Re: Display update

Post 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
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Display update

Post 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.
djg
Posts: 16
Joined: Wed Feb 12, 2020 10:37 pm

Re: Display update

Post by djg »

Ok, this will take some getting used to, but at least I know now this is how it should be done.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Display update

Post 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.
Post Reply