• Animate a DButton to fade in?
    2 replies, posted
I have this snippet here that I wrote to change the buttons color if the mouse is over it and then change the color to red. [LUA] local buttonCloseColor = Color( 0, 0, 0, 180 ) local isCursorOver = false buttonClose.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, buttonCloseColor ) buttonClose.OnCursorEntered = function() isCursorOver = true end buttonClose.OnCursorExited = function() isCursorOver = false end if isCursorOver then local R = 0 + 1 buttonCloseColor = Color( R, 38, 19, 255 ) chat.AddText( tostring( R ) ) else buttonCloseColor = Color( 0, 0, 0, 180 ) end end[/LUA] isCursorOver is set every frame so logically the var R should increase every frame right? This just makes it an ugly brown color, and doesn't increase R by 1 every frame. So how would add 1 every frame?
Not really sure how to make it change over time, but just a little thing- you can use [B]DButton.Hovered[/B] rather than making a new variable and changing it like you're doing [editline]12th February 2016[/editline] Wait, just noticed what the problem (probably) is: [QUOTE=Austin1346;49725377]isCursorOver is set every frame so logically the var R should increase every frame right? This just makes it an ugly brown color, and doesn't increase R by 1 every frame.[/QUOTE] Yes, isCursorOver is set every frame, but so is the R variable, since you keep setting it and resetting it again and again in the paint hook. Put the R variable OUTSIDE the hook as you did with the buttonCloseColor variable, and it should actually change E.G. [CODE] local buttonCloseColor = Color( 0, 0, 0, 180 ) local R = 0 buttonClose.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, buttonCloseColor ) if self.Hovered then R = R + 1 buttonCloseColor = Color( R, 38, 19, 255 ) else buttonCloseColor = Color( 0, 0, 0, 180 ) R = 0 -- reset this variable too end end [/CODE] You might want to change the number it's adding though, so it doesn't run faster (some computers can run the panel:Paint hook faster than others). You can do this by multiplying the number you're adding (or dividing it by) [URL="http://wiki.garrysmod.com/page/Global/RealFrameTime"]RealFrameTime[/URL].
Thanks a ton, I managed to get it to work. Here is the final code for anyone that needs it. [CODE]local buttonCloseColor = Color( 0, 0, 0, 180 ) local R = 0 buttonClose.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, buttonCloseColor ) if buttonClose:IsHovered() then R = R + math.floor( ( RealFrameTime() * 1250 ) ) R = math.Clamp( R, 0, 242 ) buttonCloseColor = Color( R, 0, 0, 255 ) else buttonCloseColor = Color( 0, 0, 0, 180 ) R = 0 end end[/CODE] I added in a clamp so it doesn't go over a set value.
Sorry, you need to Log In to post a reply to this thread.