Page 1 of 1

How to get variable from a textentry

Posted: Sun Apr 23, 2023 11:21 pm
by papiosaur
Hello!

I would like to get a variable in realtime from a textentry, is possible please ?

Re: How to get variable from a textentry

Posted: Mon Apr 24, 2023 8:26 am
by Bugala
By quick I would say it is doable, but give an example what exactly you are after, since there are quite many possibilities that you might be meaning with "text entry".

Like you could mean user inputting text and using this as variable, or you could mean reading text from a file and using it as variable, and couple other options.

Re: How to get variable from a textentry

Posted: Mon Apr 24, 2023 3:33 pm
by jPV
I guess it means RapaGUI's "Textentry", and the post should rather be in the Plugins/RapaGUI section :)

In that case you can add a notify for "text" in your textentry definition (notice the N applicability in docs), and then catch the event in the event function (msg.TriggerValue contains the updated text contents).

Quick'n'dirty example:

Code: Select all

@REQUIRE "rapagui"

gui$ = [[
<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window title="Test" id="win" notify="closerequest" width="screen:30">
		<vgroup>
            <textentry id="mytextentry" notify="text"/>
		</vgroup>
	</window>
</application>
]]

Function p_EventFunc(msg)
	Switch msg.action
	Case "RapaGUI"
		Switch msg.Class
		Case "Window":
			End
        Case "Textentry":
            Switch msg.ID
            Case "mytextentry":
                ; msg.TriggerValue contains the changed text content.
                ; You can assign it to a global or local variable.
                ConsolePrint(msg.TriggerValue)
            EndSwitch
		EndSwitch
	EndSwitch

EndFunction

moai.CreateApp(gui$)

InstallEventHandler({RapaGUI = p_EventFunc})

Repeat
	WaitEvent
Forever

Re: How to get variable from a textentry

Posted: Mon Apr 24, 2023 6:57 pm
by papiosaur
@Bugala : here it's just to get variable of text writed by the user in realtime in a textbutton.

@jPV: thanks a lot for the example! The good Case is "Text" instead of "Textentry".

Work perfectly!

Re: How to get variable from a textentry

Posted: Tue Apr 25, 2023 7:46 am
by jPV
papiosaur wrote: Mon Apr 24, 2023 6:57 pm The good Case is "Text" instead of "Textentry".
Yea might be, depends what you are comparing with the Switch :)

Re: How to get variable from a textentry

Posted: Tue Apr 25, 2023 9:38 pm
by papiosaur
ah ok :-)

Thanks !

Re: How to get variable from a textentry

Posted: Thu May 04, 2023 1:55 pm
by amyren
@jPV
Thanks for your example. Would it be possible to use that triggervalue to fill in the textentry?
Eg. if the user writes the first letters of a certain word, you update the textentry with the full word.

Re: How to get variable from a textentry

Posted: Thu May 04, 2023 4:31 pm
by jPV
amyren wrote: Thu May 04, 2023 1:55 pm @jPV
Thanks for your example. Would it be possible to use that triggervalue to fill in the textentry?
Eg. if the user writes the first letters of a certain word, you update the textentry with the full word.
Something like this?

Code: Select all

@REQUIRE "rapagui"

gui$ = [[
<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
    <window title="Test" id="win" notify="closerequest" width="screen:30">
        <vgroup>
            <textentry id="mytextentry" notify="text"/>
        </vgroup>
    </window>
</application>
]]

Function p_EventFunc(msg)
    Switch msg.action
    Case "RapaGUI"
    	Switch msg.Class
    	Case "Window":
    		End
        Case "Textentry":
            Switch msg.ID
            Case "mytextentry":
                Local wordlist = {"hollywood", "amiga", "amigarulez"}
                Local usertxt$ = msg.TriggerValue
                Local usertxtlen = StrLen(usertxt$)
                Local prevtxtlen = moai.Get("mytextentry", "userdata")
                ; Let's only do magic if user has entered new characters. This way he can also remove chars.
                If usertxtlen > prevtxtlen
                    ; Let's find if any word in our list starts with what user has entered.
                    For Local i = 0 To ListItems(wordlist) - 1
                        If StartsWith(wordlist[i], usertxt$)
                            Local wordlen = StrLen(wordlist[i])
                            If usertxtlen < wordlen
                                ; Set the found word in textentry, but don't notify it again because this isn't an user input.
                                moai.Set("mytextentry", "text", wordlist[i], "nonotify", True)
                                ; Let's mark rest of the word so that user can replace the end if the suggestion wasn't correct.
                                ; Requires MUI 4.x, for MUI 3.x lengths should be stored in variables/userdata and handled differently.
                                moai.DoMethod("mytextentry", "mark", usertxtlen, wordlen)
                            EndIf
                            ; No need to continue with the For loop, because we found a word, so let's break it.
                            Break
                        EndIf
                    Next
                EndIf
                ; Let's store the previous length as UserData and keep our example free of global warm... variables! :)
                moai.Set("mytextentry", "userdata", usertxtlen)
            EndSwitch
    	EndSwitch
    EndSwitch
EndFunction

moai.CreateApp(gui$)
gui$ = Nil ; Global variables bad, die.

InstallEventHandler({RapaGUI = p_EventFunc})

Repeat
	WaitEvent
Forever

Re: How to get variable from a textentry

Posted: Fri May 05, 2023 11:31 am
by amyren
Thanks, thats exactly what I was thinking of :D