Progressive removal of text in Text Control?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kunkel321
Posts: 1126
Joined: 30 Nov 2015, 21:19

Progressive removal of text in Text Control?

08 May 2024, 14:21

The code makes a gui with some text. I'd like to (upon keypress) progressively remove the text string, character by character, left-to-right, and have them replaced with space characters. With the below code, the first character works, but then it stops. It seems like each call of the function should progressively add another space, and trim another text character.

Any idea what I'm doing wrong?

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; disappearing text experiment

guiTitle := 'Color Change'
str := 'the quick brown fox jumps over'

mg := Gui(, guiTitle)
mg.SetFont('s16 cBlue','Consolas')
sBlue := mg.Add('text', 'w400', str)
mg.Show()

#hotif WinActive(guiTitle)
Space::Changer()   
#hotif
Changer(*)
{	
	static val 
	val := sBlue.Text
	sBlue.Text := ' ' . SubStr(val, 2)
}
ste(phen|ve) kunkel
RussF
Posts: 1290
Joined: 05 Aug 2021, 06:36

Re: Progressive removal of text in Text Control?

08 May 2024, 14:58

Try this Steve:

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; disappearing text experiment

guiTitle := 'Color Change'
str := 'the quick brown fox jumps over'

mg := Gui(, guiTitle)
mg.SetFont('s16 cBlue','Consolas')
sBlue := mg.Add('text', 'w400', str)
mg.Show()

#hotif WinActive(guiTitle)
Space::
{
	sBlue.Text := Changer(sBlue.Text)   
}

#hotif
Changer(txt)
{	
	sp_pos := 1
	While SubStr(txt,sp_pos,1) = ' '
		sp_pos += 1
	txt := SubStr(txt,1,sp_pos-1) . ' ' . SubStr(txt,sp_pos + 1,StrLen(txt)- 1)
	Return txt
}

Russ
User avatar
kunkel321
Posts: 1126
Joined: 30 Nov 2015, 21:19

Re: Progressive removal of text in Text Control?

08 May 2024, 19:35

RussF wrote:
08 May 2024, 14:58
Awesome Thanks! Really trippy how you got it to work.
Sorry for "changing the question" mid thread, but I'm trying to use the parts like Lego blocks and rearrange them so that there's a second function that trims the same text control... But right-to-left.

It does trim the first word (rather the LAST word), but it stops when it gets to the space char. Also... It's important for the still-visible letters to stay in the same spot (hence the monospace font). So I need the spaces filled in (padded?) to the right as the right-to-left trim happens.

Thoughts?

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; disappearing text experiment

guiTitle := 'Color Change'
str := 'the quick brown fox jumps over'

mg := Gui(, guiTitle)
mg.SetFont('s16 cBlue','Consolas')
sBlue := mg.Add('text', 'w400', str)
mg.Show()

#hotif WinActive(guiTitle)
Right::sBlue.Text := ChangerL(sBlue.Text)   
Left::sBlue.Text := ChangerR(sBlue.Text)   
#hotif

ChangerL(txt) ; Russ made (Left to right)
{	
	sp_pos := 1
	While SubStr(txt,sp_pos,1) = ' '
		sp_pos += 1
	txt := SubStr(txt,1,sp_pos-1) . ' ' . SubStr(txt,sp_pos + 1,StrLen(txt)- 1)
	Return txt
}

ChangerR(txt) ; New one...  want right to left
{	
	sp_pos := StrLen(txt)
	While SubStr(txt, StrLen(txt), 1) = ' '
		sp_pos -= 1
	txt := SubStr(txt, StrLen(txt)+1, sp_pos-1) . ' ' . SubStr(txt, 1, sp_pos-1) 
	Return txt
}
ste(phen|ve) kunkel
teadrinker
Posts: 4364
Joined: 29 Mar 2015, 09:41
Contact:

Re: Progressive removal of text in Text Control?

08 May 2024, 22:15

Code: Select all

#Requires AutoHotkey v2+
; disappearing text experiment

guiTitle := 'Color Change'
str := 'the quick brown fox jumps over'

mg := Gui(, guiTitle)
mg.SetFont('s16 cBlue','Consolas')
sBlue := mg.Add('text', 'w400', str)
mg.Show()

#hotif WinActive(guiTitle)
Right::
Left:: ((txtCtrl, side) => (
    p := side ? '^\s*\S' : '\S\s*$',
    RegExMatch(txtCtrl.Text, p, &m) && 
        txtCtrl.Text := RegExReplace(txtCtrl.Text, p, Format('{:' . m.Len . '}', ''))
))(sBlue, A_ThisHotkey = 'Right')
#hotif
Something like this?
Although, when deleting characters on the right, they don't have to be replaced with spaces.
User avatar
kunkel321
Posts: 1126
Joined: 30 Nov 2015, 21:19

Re: Progressive removal of text in Text Control?

09 May 2024, 12:02

teadrinker wrote:
08 May 2024, 22:15
Thanks for this, Teadrinker! It works well. Very enigmatic though.
ste(phen|ve) kunkel
User avatar
kunkel321
Posts: 1126
Joined: 30 Nov 2015, 21:19

Re: Progressive removal of text in Text Control?

09 May 2024, 14:06

Teadrinker pointed out that, for the right-to-left trimming, the spaces don't really need to be added back. Keeping that in mind, I came up with the below...
It's not as elegant as the Teadrinker regex solution, and probably not as efficient/fast, but it is easier to understand -- LOL.

I made the GUI background transparent, then put a duplicate (non-trimming) gui behind it, with gray letters. Now, rather than "trimming" it has the visual effect of changing colors from the outside-in. Pretty cool effect--Check it out. Also added an undo.

Now I need to figure out the best way to "lock" the GUIs together. I'll post a different thread for that though. :D
Thanks again Fellas!

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; disappearing text experiment
Esc::ExitApp()

guiTitle := 'Color Change'
str := 'the quick brown fox jumps over the lazy dog'
undoArr := []

mgb := Gui(, guiTitle)
mg := Gui(, guiTitle)
mgb.SetFont('s16 q3','Consolas') ; q3 = NONANTIALIASED_QUALITY
mg.SetFont('s16 q3','Consolas')
mg.BackColor := "EEAA99"
WinSetTransColor("EEAA99", mg)
sGray := mgb.Add('text', ' cGray', str)
sBlue := mg.Add('text', ' cBlue', str)
mgb.Show()
mg.Show()

#hotif WinActive(guiTitle)
Right::sBlue.Text := ChangerL(sBlue.Text)   
Left::sBlue.Text := ChangerR(sBlue.Text)   
^z::sBlue.Text := goUndo(sBlue.Text) 
#hotif

ChangerL(txt) ; Russ' Left to right
{	undoArr.push(txt) 
	sp_pos := 1
	While SubStr(txt,sp_pos,1) = ' '
		sp_pos += 1
	txt := SubStr(txt,1,sp_pos-1) . ' ' . SubStr(txt,sp_pos + 1,StrLen(txt)- 1)
	Return txt
}

ChangerR(txt) ; right to left
{	undoArr.push(txt) 
	txt := SubStr(txt, 1, -1)
	Return txt
}

goUndo(*)
{    If undoArr.Length > 1
    {   undoArr.pop
        txt := undoArr[undoArr.Length]
        Return txt
    }
}
ste(phen|ve) kunkel

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: mcl, TAC109 and 41 guests