forked from 0ad/0ad
Clean up the colorfade code. Patch by boeseRaupe, fixes #2395
This was SVN commit r14702.
This commit is contained in:
parent
7395d9c75a
commit
459f13e72b
@ -1,26 +1,32 @@
|
||||
/*
|
||||
DESCRIPTION : Some functions to make colour fades on GUI elements (f.e. used for hero and group icons)
|
||||
DESCRIPTION : Some functions to make color fades on GUI elements (f.e. used for hero and group icons)
|
||||
NOTES :
|
||||
*/
|
||||
|
||||
// Used for storing object names of running color fades in order to stop them, if the fade is restarted before the old ended
|
||||
// Used for storing information about color fades
|
||||
var g_colorFade = {};
|
||||
g_colorFade["id"] = {};
|
||||
g_colorFade["tick"] = {};
|
||||
|
||||
/**
|
||||
* starts fading a colour of a GUI object using the sprite argument
|
||||
* name: name of the object which colour should be faded
|
||||
* changeInterval: interval in ms when the next colour change should be made
|
||||
* duration: maximal duration of the complete fade
|
||||
* colour: RGB + opacity object with keys r,g,b and o
|
||||
* fun_colorTransform: function which transform the colors;
|
||||
* arguments: [colour object, tickCounter]
|
||||
* fun_smoothRestart [optional]: a function, which returns a smooth tick counter, if the fade should be started;
|
||||
* arguments: [tickCounter of current fade; not smaller than 1 or it restarts at 0] returns: smooth tick counter value
|
||||
* tickCounter [optional]: should not be set by hand! - how often the function was called recursively
|
||||
* returns the init RGB color setting
|
||||
*/
|
||||
function fadeColour(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, tickCounter)
|
||||
function getInitColorFadeRGB()
|
||||
{
|
||||
var rgb = {"r": 0, "g": 0, "b": 0, "o": 100};
|
||||
return rgb;
|
||||
}
|
||||
|
||||
/**
|
||||
* starts fading a color of a GUI object using the sprite argument
|
||||
* name: name of the object which color should be faded
|
||||
* tickInterval: interval in ms when the next color change should be made
|
||||
* duration: maximal duration of the complete fade (if 0 it runs until it is stopped)
|
||||
* fun_colorTransform: function which transform the colors;
|
||||
* arguments: [var data]
|
||||
* restartAble [optional: if false, the fade can not be restarted; default: true
|
||||
* fun_smoothRestart [optional]: a function, which returns a smooth tick counter, if the fade should be started;
|
||||
* arguments: [var data]; must return false, if smooth restart was not possible and true, if it was ok
|
||||
*/
|
||||
function startColorFade(name, tickInterval, duration, fun_colorTransform, restartAble, fun_smoothRestart)
|
||||
{
|
||||
// get the overlay
|
||||
var overlay = Engine.GetGUIObjectByName(name);
|
||||
@ -28,101 +34,132 @@ function fadeColour(name, changeInterval, duration, colour, fun_colorTransform,
|
||||
return;
|
||||
|
||||
// check, if fade overlay was started just now
|
||||
if (!tickCounter)
|
||||
if (!isColorFadeRunning(name))
|
||||
{
|
||||
tickCounter = 1;
|
||||
overlay.hidden = false;
|
||||
// store the values into a var to make it more flexible (can be changed from every function)
|
||||
var data = { "timerId": -1,
|
||||
"tickInterval": tickInterval,
|
||||
"duration": duration,
|
||||
"fun_colorTransform": fun_colorTransform,
|
||||
"restartAble": restartAble || restartAble === undefined, //TODO: add default parameter when it is supported (spiderMonkey upgrade #1886)
|
||||
"fun_smoothRestart": fun_smoothRestart,
|
||||
"tickCounter": 0,
|
||||
"justStopAtExternCall": duration == 0,
|
||||
"stopFade": false,
|
||||
"rgb": getInitColorFadeRGB()
|
||||
};
|
||||
// store it!
|
||||
g_colorFade[name] = data;
|
||||
|
||||
// check, if another animation is running and restart it, if it's the case
|
||||
if (isColourFadeRunning(name))
|
||||
// start with fading
|
||||
fadeColorTick(name);
|
||||
}
|
||||
else if (restartAble)
|
||||
{
|
||||
restartColourFade(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, g_colorFade.tick[name]);
|
||||
restartColorFade(name, tickInterval, duration, fun_colorTransform, restartAble, fun_smoothRestart);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get colors
|
||||
fun_colorTransform(colour, tickCounter);
|
||||
/**
|
||||
* makes the color changes in a tick
|
||||
* name: name of the object which color should be faded
|
||||
*/
|
||||
function fadeColorTick(name)
|
||||
{
|
||||
// make some checks
|
||||
if (!isColorFadeRunning(name))
|
||||
return;
|
||||
|
||||
// set new colour
|
||||
overlay.sprite="colour: "+colour.r+" "+colour.g+" "+colour.b+" "+colour.o;
|
||||
var overlay = Engine.GetGUIObjectByName(name);
|
||||
if (!overlay)
|
||||
return;
|
||||
var data = g_colorFade[name];
|
||||
|
||||
// change the color
|
||||
data.fun_colorTransform(data);
|
||||
|
||||
// set new color
|
||||
var rgb = data.rgb;
|
||||
overlay.sprite="colour: " + rgb.r + " " + rgb.g + " " + rgb.b + " " + rgb.o;
|
||||
|
||||
// recusive call, if duration is positive
|
||||
duration-= changeInterval;
|
||||
if (duration > 0 && colour.o > 0)
|
||||
if (!data.stopFade && (data.justStopAtExternCall || data.duration - (data.tickInterval * data.tickCounter) > 0))
|
||||
{
|
||||
var id = setTimeout(function() { fadeColour(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, ++tickCounter); }, changeInterval);
|
||||
g_colorFade.id[name] = id;
|
||||
g_colorFade.tick[name] = tickCounter;
|
||||
var id = setTimeout(function() { fadeColorTick(name); }, data.tickInterval);
|
||||
data.timerId = id;
|
||||
data.tickCounter++;
|
||||
}
|
||||
else
|
||||
{
|
||||
overlay.hidden = true;
|
||||
stopColourFade(name);
|
||||
stopColorFade(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* checks, if a colour fade on that object is running
|
||||
* name: name of the object which colour fade should be checked
|
||||
* checks, if a color fade on that object is running
|
||||
* name: name of the object which color fade should be checked
|
||||
* return: true a running fade was found
|
||||
*/
|
||||
function isColourFadeRunning(name)
|
||||
function isColorFadeRunning(name)
|
||||
{
|
||||
return name in g_colorFade.id;
|
||||
return name in g_colorFade;
|
||||
}
|
||||
|
||||
/**
|
||||
* stops fading a colour
|
||||
* name: name of the object which colour fade should be stopped
|
||||
* hideOverlay: hides the overlay, if true
|
||||
* stops fading a color
|
||||
* name: name of the object which color fade should be stopped
|
||||
* hideOverlay [optional]: hides the overlay, if true [default: true]
|
||||
* return: true a running fade was stopped
|
||||
*/
|
||||
function stopColourFade(name, hideOverlay)
|
||||
function stopColorFade(name, hideOverlay)
|
||||
{
|
||||
// check, if a colour fade is running
|
||||
if (!isColourFadeRunning(name))
|
||||
// check, if a color fade is running
|
||||
if (!isColorFadeRunning(name))
|
||||
return false;
|
||||
|
||||
// delete the timer
|
||||
clearTimeout(g_colorFade.id[name]);
|
||||
delete g_colorFade.id[name];
|
||||
delete g_colorFade.tick[name];
|
||||
clearTimeout(g_colorFade[name].timerId);
|
||||
delete g_colorFade[name];
|
||||
|
||||
// get the overlay and hide it
|
||||
if (hideOverlay)
|
||||
if (hideOverlay || hideOverlay === undefined) //TODO: add default parameter when it is supported (spiderMonkey upgrade #1886)
|
||||
{
|
||||
var overlay = Engine.GetGUIObjectByName(name);
|
||||
if(overlay)
|
||||
if (overlay)
|
||||
overlay.hidden = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* restarts a colour fade
|
||||
* see paramter in fadeColour function
|
||||
* restarts a color fade
|
||||
* see paramter in startColorFade function
|
||||
*/
|
||||
function restartColourFade(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, tickCounter)
|
||||
function restartColorFade(name)
|
||||
{
|
||||
// check, if a colour fade is running
|
||||
if (!isColourFadeRunning(name))
|
||||
// check, if a color fade is running
|
||||
if (!isColorFadeRunning(name))
|
||||
return false;
|
||||
|
||||
var data = g_colorFade[name];
|
||||
// check, if fade can be restarted smoothly
|
||||
if (fun_smoothRestart)
|
||||
if (data.fun_smoothRestart)
|
||||
{
|
||||
tickCounter = fun_smoothRestart(colour, tickCounter);
|
||||
// set new function to existing timer
|
||||
var fun = function() { fadeColour(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, tickCounter); };
|
||||
setNewTimerFunction(g_colorFade.id[name], fun);
|
||||
// if call was too late
|
||||
if (!data.fun_smoothRestart(data))
|
||||
{
|
||||
data.rgb = getInitColorFadeRGB(); // set RGB start values
|
||||
data.tickCounter = 0;
|
||||
}
|
||||
}
|
||||
// stop it and restart it
|
||||
else
|
||||
{
|
||||
stopColourFade(name, true);
|
||||
fadeColour(name, changeInterval, duration, colour, fun_colorTransform);
|
||||
stopColorFade(name, false);
|
||||
startColorFade(name, data.changeInterval, data.duration, data.fun_colorTransform, data.restartAble, data.fun_smoothRestart);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -133,56 +170,51 @@ function restartColourFade(name, changeInterval, duration, colour, fun_colorTran
|
||||
|
||||
var g_fadeAttackUnit = {};
|
||||
g_fadeAttackUnit.blinkingTicks = 50; // how many ticks should first blinking phase be
|
||||
g_fadeAttackUnit.blinkingChangeInterval = 5; // how often should the colour be changed during the blinking phase
|
||||
g_fadeAttackUnit.gbColourChangeRate = 3; // how fast should blue and green part of the colour change
|
||||
g_fadeAttackUnit.blinkingChangeInterval = 5; // how often should the color be changed during the blinking phase
|
||||
g_fadeAttackUnit.gbcolorChangeRate = 3; // how fast should blue and green part of the color change
|
||||
g_fadeAttackUnit.fadeOutStart = 100; // when should the fade out start using the opacity
|
||||
g_fadeAttackUnit.opacityChangeRate = 3; // how fast should opacity change
|
||||
|
||||
/**
|
||||
* rgb: colour object with keys r,g,b and o
|
||||
* tickCounter: how often the fade was executed
|
||||
*/
|
||||
function colourFade_attackUnit(rgb, tickCounter)
|
||||
function colorFade_attackUnit(data)
|
||||
{
|
||||
var rgb = data.rgb;
|
||||
|
||||
// init color
|
||||
if (data.tickCounter == 0)
|
||||
rgb.r = 175;
|
||||
// blinking
|
||||
if (tickCounter < g_fadeAttackUnit.blinkingTicks)
|
||||
if (data.tickCounter < g_fadeAttackUnit.blinkingTicks)
|
||||
{
|
||||
// slow that process down
|
||||
if (tickCounter % g_fadeAttackUnit.blinkingChangeInterval != 0)
|
||||
if (data.tickCounter % g_fadeAttackUnit.blinkingChangeInterval != 0)
|
||||
return;
|
||||
|
||||
rgb.g = rgb.g == 0 ? 255 : rgb.g = 0;
|
||||
rgb.b = rgb.b == 0 ? 255 : rgb.b = 0;
|
||||
rgb.g = rgb.g == 0 ? 255 : 0;
|
||||
}
|
||||
// wait a short time and then colour fade from red to grey to nothing
|
||||
else if ( tickCounter >= g_fadeAttackUnit.blinkingTicks + g_fadeAttackUnit.blinkingChangeInterval)
|
||||
// wait a short time and then color fade from red to grey to nothing
|
||||
else if ( data.tickCounter >= g_fadeAttackUnit.blinkingTicks + g_fadeAttackUnit.blinkingChangeInterval)
|
||||
{
|
||||
rgb.g = rgb.g < 255 ? rgb.g += g_fadeAttackUnit.gbColourChangeRate * Math.sqrt(tickCounter - g_fadeAttackUnit.blinkingTicks) : 255;
|
||||
rgb.b = rgb.g;
|
||||
rgb.g = rgb.g < 255 ? rgb.g += g_fadeAttackUnit.gbcolorChangeRate * Math.sqrt(data.tickCounter - g_fadeAttackUnit.blinkingTicks) : 255;
|
||||
|
||||
// start with fading it out
|
||||
if (rgb.g > g_fadeAttackUnit.fadeOutStart)
|
||||
rgb.o = rgb.o > g_fadeAttackUnit.opacityChangeRate ? rgb.o -= g_fadeAttackUnit.opacityChangeRate : 0;
|
||||
// check for end
|
||||
if (rgb.o == 0)
|
||||
data.stopFade = true;
|
||||
}
|
||||
rgb.b = rgb.g;
|
||||
}
|
||||
|
||||
/**
|
||||
* makes a smooth fade, if the attack on the unit has not stopped yet
|
||||
* rgb: colour object with keys r,g,b and o
|
||||
* tickCounter: how often the fade was executed
|
||||
*/
|
||||
function smoothColourFadeRestart_attackUnit(rgb, tickCounter)
|
||||
function smoothColorFadeRestart_attackUnit(data)
|
||||
{
|
||||
// check, if in blinking phase
|
||||
if (tickCounter < g_fadeAttackUnit.blinkingTicks)
|
||||
if (data.tickCounter < g_fadeAttackUnit.blinkingTicks)
|
||||
{
|
||||
// get rgb to current state
|
||||
for (var i = 1; i <= tickCounter; i++)
|
||||
colourFade_attackUnit(rgb, i);
|
||||
// set the tick counter back to start
|
||||
return (tickCounter % (g_fadeAttackUnit.blinkingChangeInterval * 2)) + 1;
|
||||
data.tickCounter = data.tickCounter % (g_fadeAttackUnit.blinkingChangeInterval * 2);
|
||||
return true;
|
||||
}
|
||||
return 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
//[END] of hero fade functions
|
@ -595,7 +595,7 @@ function updateHero()
|
||||
{
|
||||
g_previousHeroHitPoints = heroState.hitpoints;
|
||||
// trigger the animation
|
||||
fadeColour("heroHitOverlay", 100, 10000, {"r": 175,"g": 0,"b": 0,"o": 100}, colourFade_attackUnit, smoothColourFadeRestart_attackUnit);
|
||||
startColorFade("heroHitOverlay", 100, 0, colorFade_attackUnit, true, smoothColorFadeRestart_attackUnit);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user