How to get variable from a textentry

Find quick help here to get you started with Hollywood
Post Reply
papiosaur
Posts: 217
Joined: Fri Mar 31, 2023 1:34 pm

How to get variable from a textentry

Post by papiosaur »

Hello!

I would like to get a variable in realtime from a textentry, is possible please ?
Bugala
Posts: 1395
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to get variable from a textentry

Post 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.
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: How to get variable from a textentry

Post 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
papiosaur
Posts: 217
Joined: Fri Mar 31, 2023 1:34 pm

Re: How to get variable from a textentry

Post 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!
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: How to get variable from a textentry

Post 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 :)
papiosaur
Posts: 217
Joined: Fri Mar 31, 2023 1:34 pm

Re: How to get variable from a textentry

Post by papiosaur »

ah ok :-)

Thanks !
amyren
Posts: 416
Joined: Thu May 02, 2019 11:53 am

Re: How to get variable from a textentry

Post 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.
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: How to get variable from a textentry

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

Re: How to get variable from a textentry

Post by amyren »

Thanks, thats exactly what I was thinking of :D
Post Reply