Larcenist
REP: Respect, Envy, Prosperity?
- Reaction score
- 211
Out of pure boredom I sat down and started to code this little system, and this is the result.
What does the system do?
FadeUnitTimed allows you to make a unit fade from A transparency to B transparency over C seconds, and then fade in/out from B transparency back to A transparency over D seconds.
Confusing? Let's have us an example then will we?
1) GetTriggerUnit(). This is the unit that will be faded.
2) 100, 100, 100. This is the values for red, green and blue of the unit, entered in percentual values to make things easier for GUI users. Unless you've triggered some tinting on your unit just use 100 in each of these three fields.
3) 0, 100. This is the start and end transparency of the unit, also in percent. In my case the unit starts with 0% transparency and ends with 100% transparency.
4) 2, true, 4. The first number here, 2, is how many seconds it will take for the unit to fade from (in my case) 0 to 100%. The next value, true, is a boolean to tell the system if you want the unit to regain it's starting transparency after the fade. The last number is the time it takes to regain the starting transparency (4 seconds to go from 100% to 0% transparency in my case).
System code:
Note: Coded in vJASS and requires NewGen WE in order to work.
Version notes:
What does the system do?
FadeUnitTimed allows you to make a unit fade from A transparency to B transparency over C seconds, and then fade in/out from B transparency back to A transparency over D seconds.
Confusing? Let's have us an example then will we?
JASS:
call FadeUnitTimed(GetTriggerUnit(), 100, 100, 100, 0, 100, 2, true, 4)
1) GetTriggerUnit(). This is the unit that will be faded.
2) 100, 100, 100. This is the values for red, green and blue of the unit, entered in percentual values to make things easier for GUI users. Unless you've triggered some tinting on your unit just use 100 in each of these three fields.
3) 0, 100. This is the start and end transparency of the unit, also in percent. In my case the unit starts with 0% transparency and ends with 100% transparency.
4) 2, true, 4. The first number here, 2, is how many seconds it will take for the unit to fade from (in my case) 0 to 100%. The next value, true, is a boolean to tell the system if you want the unit to regain it's starting transparency after the fade. The last number is the time it takes to regain the starting transparency (4 seconds to go from 100% to 0% transparency in my case).
System code:
Note: Coded in vJASS and requires NewGen WE in order to work.
JASS:
//==============================================================================================================\\
// \\
// FadeUnitTimed library v2.0c by Larcenist. \\
// \\
//==============================================================================================================\\
//==============================================================================================================\\
// \\
// Implementation instructions: Copy this trigger, FadeUnitTimed and place it into your map. \\
// \\
//==============================================================================================================\\
//==============================================================================================================\\
// Available functions: \\
// \\
//==============================================================================================================\\
// \\
//FadeUnitTimed(unit, red, green, blue, start transparency, end transparency, fadetime, turnback, returnrate) \\
// \\
//red, green and blue values: Real values that ranges from 0 to 100. This is the percentual tinting of the unit \\
//that is being called, 100 in each field is normal tinting. Only use other values than 100 if your unit has \\
//triggered tinting since the change will be permanent. \\
// \\
//start transparency: real value that ranges from 0 to 100. The percentual initial fading of the unit that is \\
//being called, 0 means no initial fading. \\
// \\
//end transparency: real value that ranges from 0 to 100. The percentual fading the unit that is being called \\
//will have when the effect ends, 100 means 100% transperancy. \\
// \\
//fadetime: real value that indicates the time for the full effect to finish. Calculated in seconds. if turnback\\
//equal to false this will do nothing. \\
// \\
//turnback: Boolean value, true means that the unit regains normal transperancy when the effect finishes, \\
//false means that the unit retain it's new transparency. \\
// \\
//returnrate: Real value that indicates the time for the unit to return to the entered start transparency. \\
// \\
//==============================================================================================================\\
library FadeUnitTimed
private struct SData
unit fadeunit
real currentfaderate
real endfaderate
real fadeamount
real returnrate
real startrate
integer red
integer green
integer blue
boolean turnback
boolean fadeout
endstruct
globals
private timer array Timer
private SData array Data
private integer Counter = 0
private real TimerPeriod = 0.03125
endglobals
function FadeUnitTimedReturnCallback takes nothing returns nothing
local SData data
local integer i = Counter
local integer Alpha
local boolean Check
loop
exitwhen i <= 0
if Timer<i> == GetExpiredTimer() then
set data = Data<i>
if data.fadeout == true then
set data.currentfaderate = data.currentfaderate - data.fadeamount
set Check = data.currentfaderate < data.endfaderate
else
set data.currentfaderate = data.currentfaderate + data.fadeamount
set Check = data.currentfaderate > data.endfaderate
endif
if Check then
call PauseTimer(Timer<i>)
call data.destroy()
call DestroyTimer(Timer<i>)
set Timer<i> = null
set Timer<i> = Timer[Counter]
set Data<i> = Data[Counter]
set Counter = Counter - 1
else
set Alpha = R2I((100 - data.currentfaderate) * 255 * 0.01)
call SetUnitVertexColor(data.fadeunit, data.red, data.green, data.blue, Alpha)
endif
endif
set i = i - 1
endloop
endfunction
function FadeUnitTimedCallback takes nothing returns nothing
local SData data
local integer i = Counter
local integer Alpha
local boolean Check
loop
exitwhen i <= 0
if Timer<i> == GetExpiredTimer() then
set data = Data<i>
if data.fadeout == true then
set data.currentfaderate = data.currentfaderate + data.fadeamount
set Check = data.currentfaderate > data.endfaderate
else
set data.currentfaderate = data.currentfaderate - data.fadeamount
set Check = data.currentfaderate < data.endfaderate
endif
if Check then
call PauseTimer(Timer<i>)
if data.turnback == true then
set data.endfaderate = data.startrate
if data.fadeout == true then
set data.fadeamount = ((data.currentfaderate - data.endfaderate) / data.returnrate) * TimerPeriod
else
set data.fadeamount = ((data.endfaderate - data.currentfaderate) / data.returnrate) * TimerPeriod
endif
call TimerStart(Timer<i>, TimerPeriod, true, function FadeUnitTimedReturnCallback)
else
call DestroyTimer(Timer<i>)
call data.destroy()
set Timer<i> = null
set Timer<i> = Timer[Counter]
set Data<i> = Data[Counter]
set Counter = Counter - 1
endif
else
set Alpha = R2I((100 - data.currentfaderate) * 255 * 0.01)
call SetUnitVertexColor(data.fadeunit, data.red, data.green, data.blue, Alpha)
endif
endif
set i = i - 1
endloop
endfunction
function FadeUnitTimed takes unit u, real red, real green, real blue, real startfade, real endfade, real fadetime, boolean turnback, real returnrate returns nothing
local SData data
local integer Alpha
local real FadeTime
set Counter = Counter + 1
if Timer[Counter] == null then
set data = SData.create()
set Timer[Counter] = CreateTimer()
set Data[Counter] = data
set data.fadeunit = u
set data.endfaderate = endfade
set data.startrate = startfade
if fadetime < TimerPeriod then
set FadeTime = TimerPeriod
else
set FadeTime = fadetime
endif
if startfade < endfade then
set data.fadeamount = ((endfade - startfade) / FadeTime) * TimerPeriod
set data.currentfaderate = startfade
set data.startrate = startfade
set data.fadeout = true
else
set data.fadeamount = ((startfade - endfade) / FadeTime) * TimerPeriod
set data.currentfaderate = startfade
set data.fadeout = false
endif
set data.red = R2I(red * 255 * 0.01)
set data.green = R2I(green * 255 * 0.01)
set data.blue = R2I(blue * 255 * 0.01)
set data.turnback = turnback
if returnrate < TimerPeriod then
set data.returnrate = TimerPeriod
else
set data.returnrate = returnrate
endif
set Alpha = R2I((100 - startfade) * 255 * 0.01)
if Alpha < 0 then
set Alpha = 0
elseif Alpha > 255 then
set Alpha = 255
endif
call SetUnitVertexColor(u, data.red, data.green, data.blue, Alpha)
call TimerStart(Timer[Counter], TimerPeriod, true, function FadeUnitTimedCallback)
else
call BJDebugMsg("Timer is not nulled or still in use")
endif
endfunction
endlibrary</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>
Version notes:
Version 2.0c: Fixed a hardly noticable mini-bugg with units fading out.
Version 2.0b: Fixed some stupid "bug" with invisible units flashing before starting to fade in.
Version 2.0: System now supports fading both in and out.
Version 1.0: Initial release.
Version 2.0b: Fixed some stupid "bug" with invisible units flashing before starting to fade in.
Version 2.0: System now supports fading both in and out.
Version 1.0: Initial release.