AutoHotkey Community

It is currently May 27th, 2012, 6:42 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: July 27th, 2011, 7:05 am 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
I am using GuiControl to change out text on a GUI, and it works great but what would really rock is a "fade" effect on the text change, like a 100ms fade-out then a 100ms fade-in of the new text.
Is this possible and how? I know of a few functions that fade GUIs in and out and I'm wondering if it can be applied to GuiControl.

_________________
Some of my scripts :).


Last edited by aaronbewza on July 31st, 2011, 4:23 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 27th, 2011, 12:14 pm 
Offline

Joined: February 6th, 2010, 5:17 pm
Posts: 57
This sort of flickers sometimes, but it works:
Code:
background_color := new Color(0xf0f0f0)
text_color := new Color(0x000000)
fade_color := new Color
text1 := "Original text."
text2 := "Replacement text."

Gui, Color, % background_color.hex
Gui, Add, Text, % "c" . text_color.hex . " w100 vtext", %text1%
Gui, Add, Button, gfade2, Fade to text2.
Gui, Add, Button, gfade1, Fade back to text1.
Gui, Show
return

fade1:
fade(text, text1)
return

fade2:
fade(text, text2)
return

fade(ByRef control, new_text = "", step = 5) {
  global background_color, text_color, fade_color
  GuiControlGet, start_text,, control
  percent = 100
  while percent >= 0
  {
    for k, v in ["R", "G", "B"]
      fade_color[v] := Round(background_color[v] + ((text_color[v] - background_color[v]) * (percent / 100)))
    percent -= step
    GuiControl, % "+C" . fade_color.hex, control
    GuiControl,, control, %start_text%
    Sleep, 1
  }
  percent = 0
  while percent <= 100
  {
    for k, v in ["R", "G", "B"]
      fade_color[v] := Round(background_color[v] + ((text_color[v] - background_color[v]) * (percent / 100)))
    percent += step
    GuiControl, % "+C" . fade_color.hex, control
    GuiControl,, control, %new_text%
    Sleep, 1
  }
}

GuiClose:
GuiEscape:
ExitApp

;modified color class from the AHK_L help file
class Color
{
  __New(aRGB = 0x000000) {
    this.RGB := aRGB
  }

  __Get(aName) {
    if (aName = "R")
      return (this.RGB >> 16) & 255
    if (aName = "G")
      return (this.RGB >> 8) & 255
    if (aName = "B")
      return this.RGB & 255
    if (aName = "hex")
    {
      format_setting := A_FormatInteger
      SetFormat, IntegerFast, h
      hex := SubStr(this.RGB + 0, 3)
      SetFormat, IntegerFast, %format_setting%
      while StrLen(hex) < 6
        hex := "0" . hex
      return, "0x" . hex
    }
  }

  __Set(aName, aValue) {
    if aName in R,G,B
    {
      aValue &= 255
      if      (aName = "R")
        this.RGB := (aValue << 16) | (this.RGB & ~0xff0000)
      else if (aName = "G")
        this.RGB := (aValue << 8)  | (this.RGB & ~0x00ff00)
      else  ; (aName = "B")
        this.RGB :=  aValue        | (this.RGB & ~0x0000ff)
      return aValue
    }
  }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 27th, 2011, 10:04 pm 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
oh my goodness! That is an amazing-looking chunk of code. Did you have this laying around or did you put this together? I am very excited to try this out, it is for Aaron's YouTube Television... I will try this out today, and will be very happy if it works :) Thank you again.

EDIT: I've got a few things going on apparently in my script that seems not to allow this function to be used properly. I will keep experimenting but it looks like I'd have to sacrifice some things in the process. I am also somewhat of a beginner coder so I may be doing it wrong. I'll report back with any progress or with where I'm stuck :)

_________________
Some of my scripts :).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 28th, 2011, 7:17 pm 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
Well, your test GUI works 100% beautifully to change the text, and I even have the correct colors for the texts and background:
Code:
background_color := new Color(0x000000)
text_color := new Color(0xFF0000)
fade_color := new Color
text1 := "Original text."
text2 := "Replacement text."

Gui, Color, % background_color.hex
Gui, Add, Text, % "c" . text_color.hex . " w100 vtext", %text1%
Gui, Add, Button, gfade2, Fade to text2.
Gui, Add, Button, gfade1, Fade back to text1.
Gui, Show
return

fade1:
fade(text, text1)
return

fade2:
fade(text, text2)
return

fade(ByRef control, new_text = "", step = 5) {
  global background_color, text_color, fade_color
  GuiControlGet, start_text,, control
  percent = 100
  while percent >= 0
  {
    for k, v in ["R", "G", "B"]
      fade_color[v] := Round(background_color[v] + ((text_color[v] - background_color[v]) * (percent / 100)))
    percent -= step
    GuiControl, % "+C" . fade_color.hex, control
    GuiControl,, control, %start_text%
    Sleep, 1
  }
  percent = 0
  while percent <= 100
  {
    for k, v in ["R", "G", "B"]
      fade_color[v] := Round(background_color[v] + ((text_color[v] - background_color[v]) * (percent / 100)))
    percent += step
    GuiControl, % "+C" . fade_color.hex, control
    GuiControl,, control, %new_text%
    Sleep, 1
  }
}

GuiClose:
GuiEscape:
ExitApp

;modified color class from the AHK_L help file
class Color
{
  __New(aRGB = 0x000000) {
    this.RGB := aRGB
  }

  __Get(aName) {
    if (aName = "R")
      return (this.RGB >> 16) & 255
    if (aName = "G")
      return (this.RGB >> 8) & 255
    if (aName = "B")
      return this.RGB & 255
    if (aName = "hex")
    {
      format_setting := A_FormatInteger
      SetFormat, IntegerFast, h
      hex := SubStr(this.RGB + 0, 3)
      SetFormat, IntegerFast, %format_setting%
      while StrLen(hex) < 6
        hex := "0" . hex
      return, "0x" . hex
    }
  }

  __Set(aName, aValue) {
    if aName in R,G,B
    {
      aValue &= 255
      if      (aName = "R")
        this.RGB := (aValue << 16) | (this.RGB & ~0xff0000)
      else if (aName = "G")
        this.RGB := (aValue << 8)  | (this.RGB & ~0x00ff00)
      else  ; (aName = "B")
        this.RGB :=  aValue        | (this.RGB & ~0x0000ff)
      return aValue
    }
  }
}


...and it looks awesome! Except this way it seems to introduce problems in the GUIs background color. And I'm using this, mostly:
Code:
GuiControl,, ChannelTitle, %ChannelTitleDisplay%
to change the text on the control. %ChannelTitleDisplay% shows the name of the current channel on the TV.
It does not seem to fade the text out or in when I try to use it on my GUI... but there is a delay that takes the same amount of time the actual fade effect would take, so I know I have to be doing something incorrectly. I will keep trying but I think I've run out of everything I know.

_________________
Some of my scripts :).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 3:23 am 
Offline

Joined: February 6th, 2010, 5:17 pm
Posts: 57
I just wrote this and I've never actually used it before. One thing that I found that could make it not fade is if you don't create a new Color object for the fade_color at the top of the script. I tried to put that in the function and it didn't work, but I just found out that you have to use global Color in a function before you can create a new Color object inside it.

I rewrote it again using a class this time instead of a function. It should be easier to use and you won't have to worry about the Color objects.
Code:
background_color := 0x000000
text_color := 0xFF0000
fade := new Text_fader(text_color, background_color)

text1 := "Original text."
text2 := "Replacement text."

Gui, Color, %background_color%
Gui, Add, Text, c%text_color% w100 vtext_control
Gui, Add, Button, gfade_in, Fade in text1.
Gui, Add, Button, gfade_to, Fade to text2.
Gui, Add, Button, gfade_out, Fade out text.
Gui, Show
return

fade_in:
fade.in(text_control, text1)
return

fade_to:
fade.to(text_control, text2)
return

fade_out:
fade.out(text_control)
return

GuiClose:
GuiEscape:
ExitApp

class Text_fader {
  __New(text_color = 0x000000, background_color = 0xf0f0f0, step = 5) {
    global Color
    this.text_color := new Color(text_color)
    this.background_color := new Color(background_color)
    this.step := step
    this.fade_color := new Color
  }

  to(ByRef control, new_text) {
    start_text := this.out(control)
    this.in(control, new_text)
    return, start_text
  }

  out(ByRef control) {
    GuiControlGet, start_text,, control
    percent = 100
    while percent > 0
    {
      for k, v in ["R", "G", "B"]
        this.fade_color[v] := Round(this.background_color[v] + ((this.text_color[v] - this.background_color[v]) * (percent / 100)))
      percent -= this.step
      GuiControl, % "+C" . this.fade_color.hex, control
      GuiControl,, control, %start_text%
      Sleep, 1
    }
    GuiControl,, control
    return, start_text
  }
 
  in(ByRef control, new_text) {
    percent = 0
    while percent < 100
    {
      for k, v in ["R", "G", "B"]
        this.fade_color[v] := Round(this.background_color[v] + ((this.text_color[v] - this.background_color[v]) * (percent / 100)))
      percent += this.step
      GuiControl, % "+C" . this.fade_color.hex, control
      GuiControl,, control, %new_text%
      Sleep, 1
    }
    GuiControl, % "+C" . this.text_color.hex, control
    GuiControl,, control, %new_text%
  }
}

;modified color class from the AHK_L help file
class Color
{
  __New(aRGB = 0x000000) {
    this.RGB := aRGB
  }

  __Get(aName) {
    if (aName = "R")
      return (this.RGB >> 16) & 255
    if (aName = "G")
      return (this.RGB >> 8) & 255
    if (aName = "B")
      return this.RGB & 255
    if (aName = "hex")
    {
      format_setting := A_FormatInteger
      SetFormat, IntegerFast, h
      hex := SubStr(this.RGB + 0, 3)
      SetFormat, IntegerFast, %format_setting%
      while StrLen(hex) < 6
        hex := "0" . hex
      return, "0x" . hex
    }
  }

  __Set(aName, aValue) {
    if aName in R,G,B
    {
      aValue &= 255
      if      (aName = "R")
        this.RGB := (aValue << 16) | (this.RGB & ~0xff0000)
      else if (aName = "G")
        this.RGB := (aValue << 8)  | (this.RGB & ~0x00ff00)
      else  ; (aName = "B")
        this.RGB :=  aValue        | (this.RGB & ~0x0000ff)
      return aValue
    }
  }
}

If you want to change the speed of the fade you can change the step. Setting it to a higher number makes it faster, but if you set it much over 15 it's so fast you can't tell it's fading.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 2:33 pm 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
Beautiful! I can't wait to try this, and I'm very thankful you provided such knowledgeable help on an obscure request like this. I just finished up a lot of brain-wrecking learning while I added a download function to my script, so I need sleep now. I will attempt to put this into the script sometime tonight when I am awake.
Very impressive and easy on the eyes, this little text fade function :)

_________________
Some of my scripts :).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 30th, 2011, 1:29 pm 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
Beautiful! working 100%, thank you very much. I gave credit to you in the comment title of your function. One question... I have this:
Code:
    Fade.in(Channel, "Power On") ; Calls function to fade texts in ------->>
    Fade.in(ChannelTitle, "Welcome to Aaron's YouTube Television")
    Fade.in(VolumeText, "Volume") ; -------------------------------------<<

and this:
Code:
    Fade.out(Channel) ; Calls function to fade texts out -------->>
    Fade.out(ChannelTitle)
    Fade.out(VolumeText) ; -------------------------------------<<


Is there any way to run those three calls all at once, instead of one after another? It looks really cool the way it is, but the three pieces of text fade in and out, one after each other and not all at the same time.

_________________
Some of my scripts :).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 30th, 2011, 3:09 pm 
Offline

Joined: February 6th, 2010, 5:17 pm
Posts: 57
I modified it to work on multiple controls at the same time. Now you should be able to use:
Code:
Fade.in(Channel, "Power On", ChannelTitle, "Welcome to Aaron's YouTube Television", VolumeText, "Volume")
Fade.out(Channel, ChannelTitle, VolumeText)

Code:
background_color := 0x000000
text_color := 0xFF0000
fade := new Text_fader(text_color, background_color)

text1 := "Original text."
text2 := "Replacement text."

Gui, Color, %background_color%
Gui, Add, Text, c%text_color% w100 vtext_control1
Gui, Add, Text, c%text_color% w100 vtext_control2
Gui, Add, Text, c%text_color% w100 vtext_control3
Gui, Add, Button, gfade_in, Fade in text.
Gui, Add, Button, gfade_to, Fade to text.
Gui, Add, Button, gfade_out, Fade out text.
Gui, Show
return

fade_in:
fade.in(text_control1, text1, text_control2, text2, text_control3, text1)
return

fade_to:
fade.to(text_control1, text2, text_control2, text1, text_control3, text2)
return

fade_out:
fade.out(text_control1, text_control2, text_control3)
return

GuiClose:
GuiEscape:
ExitApp

class Text_fader {
  __New(text_color = 0x000000, background_color = 0xf0f0f0, step = 5) {
    global Color
    this.text_color := new Color(text_color)
    this.background_color := new Color(background_color)
    this.step := step
    this.fade_color := new Color
  }

  to(ByRef control1, new_text1, ByRef control2 = "nil", new_text2 = "", ByRef control3 = "nil", new_text3 = "") {
    this.out(control1, control2, control3)
    this.in(control1, new_text1, control2, new_text2, control3, new_text3)
  }

  out(ByRef control1, ByRef control2 = "nil", ByRef control3 = "nil") {
    percent = 100
    while percent > 0
    {
      percent -= this.step
      for k, v in ["R", "G", "B"]
        this.fade_color[v] := Round(this.background_color[v] + ((this.text_color[v] - this.background_color[v]) * (percent / 100)))
      GuiControl, % "+C" . (percent <= 0 ? this.background_color.hex : this.fade_color.hex), control1
      GuiControl, MoveDraw, control1
      if (control2 != "nil")
      {
        GuiControl, % "+C" . (percent <= 0 ? this.background_color.hex : this.fade_color.hex), control2
        GuiControl, MoveDraw, control2
      }
      if (control3 != "nil")
      {
        GuiControl, % "+C" . (percent <= 0 ? this.background_color.hex : this.fade_color.hex), control3
        GuiControl, MoveDraw, control3
      }
      Sleep, 1
    }
  }
 
  in(ByRef control1, new_text1, ByRef control2 = "nil", new_text2 = "", ByRef control3 = "nil", new_text3 = "") {
    percent = 0
    while percent < 100
    {
      percent += this.step
      for k, v in ["R", "G", "B"]
        this.fade_color[v] := Round(this.background_color[v] + ((this.text_color[v] - this.background_color[v]) * (percent / 100)))
      GuiControl, % "+C" . (percent >= 100 ? this.text_color.hex : this.fade_color.hex), control1
      GuiControl,, control1, %new_text1%
      if (control2 != "nil")
      {
        GuiControl, % "+C" . (percent >= 100 ? this.text_color.hex : this.fade_color.hex), control2
        GuiControl,, control2, %new_text2%
      }
      if (control3 != "nil")
      {
        GuiControl, % "+C" . (percent >= 100 ? this.text_color.hex : this.fade_color.hex), control3
        GuiControl,, control3, %new_text3%
      }
      Sleep, 1
    }
  }
}

;modified color class from the AHK_L help file
class Color
{
  __New(aRGB = 0x000000) {
    this.RGB := aRGB
  }

  __Get(aName) {
    if (aName = "R")
      return (this.RGB >> 16) & 255
    if (aName = "G")
      return (this.RGB >> 8) & 255
    if (aName = "B")
      return this.RGB & 255
    if (aName = "hex")
    {
      format_setting := A_FormatInteger
      SetFormat, IntegerFast, h
      hex := SubStr(this.RGB + 0, 3)
      SetFormat, IntegerFast, %format_setting%
      while StrLen(hex) < 6
        hex := "0" . hex
      return, "0x" . hex
    }
  }

  __Set(aName, aValue) {
    if aName in R,G,B
    {
      aValue &= 255
      if      (aName = "R")
        this.RGB := (aValue << 16) | (this.RGB & ~0xff0000)
      else if (aName = "G")
        this.RGB := (aValue << 8)  | (this.RGB & ~0x00ff00)
      else  ; (aName = "B")
        this.RGB :=  aValue        | (this.RGB & ~0x0000ff)
      return aValue
    }
  }
}

I know that in AHK_L you can make functions accept a variable number of parameters, but I can't figure out how to do that with ByRef variables, and using ByRef variables is the only way I could get it to control the GUI. So for now it only accepts up to 3 controls at a time, but if you need more I can edit it to make it accept more.

[Edit]I just found out that you can pass the control variable names as strings instead of using ByRef. I'll post a better version when I get it working.[/Edit]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 30th, 2011, 4:58 pm 
Offline

Joined: February 6th, 2010, 5:17 pm
Posts: 57
Now it should work with any number of controls at the same time. The only difference is now you have to pass the control variable name to the function as a string.
Code:
Fade.in("Channel", "Power On", "ChannelTitle", "Welcome to Aaron's YouTube Television", "VolumeText", "Volume")
Fade.out("Channel", "ChannelTitle", "VolumeText")

Code:
background_color := 0x000000
text_color := 0xFF0000
fade := new Text_fader(text_color, background_color)

text1 := "Original text."
text2 := "Replacement text."

Gui, Color, %background_color%
Gui, Add, Text, c%text_color% w100 vtext_control1
Gui, Add, Text, c%text_color% w100 vtext_control2
Gui, Add, Text, c%text_color% w100 vtext_control3
Gui, Add, Button, gfade_in, Fade in text.
Gui, Add, Button, gfade_to, Fade to text.
Gui, Add, Button, gfade_out, Fade out text.
Gui, Show
return

fade_in:
fade.in("text_control1", text1, "text_control2", text2, "text_control3", text1)
return

fade_to:
fade.to("text_control1", text2, "text_control2", text1, "text_control3", text2)
return

fade_out:
fade.out("text_control1", "text_control2", "text_control3")
return

GuiClose:
GuiEscape:
ExitApp

class Text_fader {
  __New(text_color = 0x000000, background_color = 0xf0f0f0, step = 5) {
    global Color
    this.text_color := new Color(text_color)
    this.background_color := new Color(background_color)
    this.step := step
    this.fade_color := new Color
  }

  to(params*) {
    controls := []
    for k, v in params
    {
      if (Mod(k, 2) != 0)
        controls.Insert(v)
    }
    this.out(controls*)
    this.in(params*)
  }

  out(controls*) {
    percent = 100
    while percent > 0
    {
      percent -= this.step
      for k, v in ["R", "G", "B"]
        this.fade_color[v] := Round(this.background_color[v] + ((this.text_color[v] - this.background_color[v]) * (percent / 100)))
      for k, control in controls
      {
        GuiControl, % "+C" . (percent <= 0 ? this.background_color.hex : this.fade_color.hex), %control%
        GuiControl, MoveDraw, %control%
      }
      Sleep, 1
    }
  }

  in(params*) {
    controls := {}
    for k, v in params
    {
      if (Mod(k, 2) != 0)
        control := v
      else
        controls[control] := v
    }
    percent = 0
    while percent < 100
    {
      percent += this.step
      for k, v in ["R", "G", "B"]
        this.fade_color[v] := Round(this.background_color[v] + ((this.text_color[v] - this.background_color[v]) * (percent / 100)))
      for control, text in controls
      {
        GuiControl, % "+C" . (percent >= 100 ? this.text_color.hex : this.fade_color.hex), %control%
        GuiControl,, %control%, %text%
      }
      Sleep, 1
    }
  }
}

;modified color class from the AHK_L help file
class Color
{
  __New(aRGB = 0x000000) {
    this.RGB := aRGB
  }

  __Get(aName) {
    if (aName = "R")
      return (this.RGB >> 16) & 255
    if (aName = "G")
      return (this.RGB >> 8) & 255
    if (aName = "B")
      return this.RGB & 255
    if (aName = "hex")
    {
      format_setting := A_FormatInteger
      SetFormat, IntegerFast, h
      hex := SubStr(this.RGB + 0, 3)
      SetFormat, IntegerFast, %format_setting%
      while StrLen(hex) < 6
        hex := "0" . hex
      return, "0x" . hex
    }
  }

  __Set(aName, aValue) {
    if aName in R,G,B
    {
      aValue &= 255
      if      (aName = "R")
        this.RGB := (aValue << 16) | (this.RGB & ~0xff0000)
      else if (aName = "G")
        this.RGB := (aValue << 8)  | (this.RGB & ~0x00ff00)
      else  ; (aName = "B")
        this.RGB :=  aValue        | (this.RGB & ~0x0000ff)
      return aValue
    }
  }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 30th, 2011, 10:38 pm 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
I am very lost. The three texts on your test GUI work 100%, but even if I completely replace what I already have with the new function and new commands, it does not want to work. I'm not exactly sure what I'm doing wrong. I've replaced the function with your new post's contents, and all the stuff up at the top of the script (background_color, text_color etc).

The only thing I can think of that I'm doing wrong is not including
Code:
text1 := "Original text."
text2 := "Replacement text."

....because the contents of 'Channel' and 'ChannelTitle' change every time the User changes the channel. Is this where I'm going wrong?

_________________
Some of my scripts :).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 31st, 2011, 12:15 am 
Offline

Joined: February 6th, 2010, 5:17 pm
Posts: 57
I took your code and pasted my new class into it and I thought I had found the problem, but I didn't realize you had renamed it from Text_fader to TextFader. After I renamed the class, it worked.


Last edited by ih57452 on July 31st, 2011, 3:23 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 31st, 2011, 3:20 am 
Offline

Joined: February 20th, 2011, 9:42 pm
Posts: 433
Location: Cache Creek B.C.
ack! I'm really sorry, I totally forgot I'd done that...
I've got this aversion to underscores, I'll tweak out and remove all the ones I can get away with.
Thanks for catching that, and I apologize for causing complications.

It works 100% and looks very smooth when the "Power" buttons are pressed.
I just followed your tracks and I was able to duplicate implementing your function from scratch.

I've set the step to 15 and this seems to keep the animation smooth, it allows for
changing through the channels at a rate comparable to a real TV.

Thank you very much for your excellent help, and like I've offered others that have been helpful,
I am an artist and could maybe help you on some visual design for any backgrounds you may need, or other stuff. Let me know :)
I may not be able to offer coding skills but I know about "visual" lol

This TV is looking good now... I'll pound on it a little more then post the updated version

_________________
Some of my scripts :).


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: batto, Bing [Bot], JSLover and 60 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group