mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-21 13:04:10 +00:00
Hero / group icon color fade cleanup.
Move globals to the top of the file. Proper case for globals. Rename restartAble to restartable. Use proper JSdoc comments. Whitespace fixes. Abbreviate with rgbToGuiColor. This was SVN commit r18628.
This commit is contained in:
@@ -1,34 +1,48 @@
|
||||
/*
|
||||
DESCRIPTION : Some functions to make color fades on GUI elements (f.e. used for hero and group icons)
|
||||
NOTES :
|
||||
*/
|
||||
|
||||
// Used for storing information about color fades
|
||||
var g_colorFade = {};
|
||||
/**
|
||||
* Some functions to make color fades on GUI elements (f.e. used for hero and group icons)
|
||||
*/
|
||||
|
||||
/**
|
||||
* returns the init RGB color setting
|
||||
* Used for storing information about color fades
|
||||
*/
|
||||
var g_ColorFade = {};
|
||||
|
||||
var g_FadeAttackUnit = {
|
||||
|
||||
// How many ticks should first blinking phase be
|
||||
"blinkingTicks": 50,
|
||||
|
||||
// How often should the color be changed during the blinking phase
|
||||
"blinkingChangeInterval": 5,
|
||||
|
||||
// How fast should blue and green part of the color change
|
||||
"gbcolorChangeRate": 3,
|
||||
|
||||
// When should the fade out start using the opacity
|
||||
"fadeOutStart": 100,
|
||||
|
||||
// How fast should opacity change
|
||||
"opacityChangeRate": 3
|
||||
};
|
||||
|
||||
function getInitColorFadeRGB()
|
||||
{
|
||||
var rgb = {"r": 0, "g": 0, "b": 0, "o": 100};
|
||||
return rgb;
|
||||
return { "r": 0, "g": 0, "b": 0, "o": 100 };
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Starts fading a color of a GUI object using the sprite argument.
|
||||
*
|
||||
* @param {string} name - name of the object which color should be faded
|
||||
* @param {number} tickInterval - interval in ms when the next color change should be made
|
||||
* @param {number} duration - maximum duration of the complete fade (if 0 it runs until it is stopped)
|
||||
* @param {function} fun_colorTransform - function which transform the colors
|
||||
* @param {boolean} [restartable] - if false, the fade can not be restarted; default: true
|
||||
* @param {function} [fun_smoothRestart] - 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 = true, fun_smoothRestart = false)
|
||||
function startColorFade(name, tickInterval, duration, fun_colorTransform, restartable = true, fun_smoothRestart = false)
|
||||
{
|
||||
// get the overlay
|
||||
var overlay = Engine.GetGUIObjectByName(name);
|
||||
if (!overlay)
|
||||
return;
|
||||
@@ -37,52 +51,50 @@ function startColorFade(name, tickInterval, duration, fun_colorTransform, restar
|
||||
if (!isColorFadeRunning(name))
|
||||
{
|
||||
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,
|
||||
"fun_smoothRestart": fun_smoothRestart,
|
||||
"tickCounter": 0,
|
||||
"justStopAtExternCall": duration == 0,
|
||||
"stopFade": false,
|
||||
"rgb": getInitColorFadeRGB()
|
||||
};
|
||||
// store it!
|
||||
g_colorFade[name] = data;
|
||||
g_ColorFade[name] = {
|
||||
"timerId": -1,
|
||||
"tickInterval": tickInterval,
|
||||
"duration": duration,
|
||||
"fun_colorTransform": fun_colorTransform,
|
||||
"restartable": restartable,
|
||||
"fun_smoothRestart": fun_smoothRestart,
|
||||
"tickCounter": 0,
|
||||
"justStopAtExternCall": duration == 0,
|
||||
"stopFade": false,
|
||||
"rgb": getInitColorFadeRGB()
|
||||
};
|
||||
|
||||
// start with fading
|
||||
fadeColorTick(name);
|
||||
}
|
||||
else if (restartAble)
|
||||
else if (restartable)
|
||||
{
|
||||
restartColorFade(name, tickInterval, duration, fun_colorTransform, restartAble, fun_smoothRestart);
|
||||
restartColorFade(name, tickInterval, duration, fun_colorTransform, restartable, fun_smoothRestart);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* makes the color changes in a tick
|
||||
* name: name of the object which color should be faded
|
||||
* Changes the color on tick.
|
||||
*
|
||||
* @param {string} name - name of the object which color should be faded
|
||||
*/
|
||||
function fadeColorTick(name)
|
||||
{
|
||||
// make some checks
|
||||
if (!isColorFadeRunning(name))
|
||||
return;
|
||||
|
||||
|
||||
var overlay = Engine.GetGUIObjectByName(name);
|
||||
if (!overlay)
|
||||
return;
|
||||
var data = g_colorFade[name];
|
||||
|
||||
var data = g_ColorFade[name];
|
||||
|
||||
// change the color
|
||||
data.fun_colorTransform(data);
|
||||
|
||||
// set new color
|
||||
var rgb = data.rgb;
|
||||
overlay.sprite="color: " + rgb.r + " " + rgb.g + " " + rgb.b + " " + rgb.o;
|
||||
|
||||
overlay.sprite = "color:" + rgbToGuiColor(data.rgb);
|
||||
|
||||
// recusive call, if duration is positive
|
||||
if (!data.stopFade && (data.justStopAtExternCall || data.duration - (data.tickInterval * data.tickCounter) > 0))
|
||||
@@ -91,7 +103,7 @@ function fadeColorTick(name)
|
||||
data.timerId = id;
|
||||
data.tickCounter++;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
overlay.hidden = true;
|
||||
stopColorFade(name);
|
||||
@@ -99,20 +111,22 @@ function fadeColorTick(name)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Checks, if a color fade on that object is running.
|
||||
*
|
||||
* @param {string} name - name of the object which color fade should be checked
|
||||
* @returns {boolean} - true a running fade was found
|
||||
*/
|
||||
function isColorFadeRunning(name)
|
||||
{
|
||||
return name in g_colorFade;
|
||||
return name in g_ColorFade;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Stops fading a color.
|
||||
*
|
||||
* @param {string} name - name of the object which color fade should be stopped
|
||||
* @param {boolean} hideOverlay - hides the overlay, if true [default: true]
|
||||
* @returns {boolean} true a running fade was stopped
|
||||
*/
|
||||
function stopColorFade(name, hideOverlay = true)
|
||||
{
|
||||
@@ -121,8 +135,8 @@ function stopColorFade(name, hideOverlay = true)
|
||||
return false;
|
||||
|
||||
// delete the timer
|
||||
clearTimeout(g_colorFade[name].timerId);
|
||||
delete g_colorFade[name];
|
||||
clearTimeout(g_ColorFade[name].timerId);
|
||||
delete g_ColorFade[name];
|
||||
|
||||
// get the overlay and hide it
|
||||
if (hideOverlay)
|
||||
@@ -135,16 +149,16 @@ function stopColorFade(name, hideOverlay = true)
|
||||
}
|
||||
|
||||
/**
|
||||
* restarts a color fade
|
||||
* see paramter in startColorFade function
|
||||
* Restarts a color fade,
|
||||
* see paramter in startColorFade function.
|
||||
*/
|
||||
function restartColorFade(name)
|
||||
{
|
||||
// check, if a color fade is running
|
||||
if (!isColorFadeRunning(name))
|
||||
return false;
|
||||
|
||||
var data = g_colorFade[name];
|
||||
|
||||
var data = g_ColorFade[name];
|
||||
// check, if fade can be restarted smoothly
|
||||
if (data.fun_smoothRestart)
|
||||
{
|
||||
@@ -159,48 +173,38 @@ function restartColorFade(name)
|
||||
else
|
||||
{
|
||||
stopColorFade(name, false);
|
||||
startColorFade(name, data.changeInterval, data.duration, data.fun_colorTransform, data.restartAble, data.fun_smoothRestart);
|
||||
startColorFade(name, data.changeInterval, data.duration, data.fun_colorTransform, data.restartable, data.fun_smoothRestart);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** PREDEFINED FUNCTIONS */
|
||||
|
||||
//[START] of hero fade functions
|
||||
|
||||
var g_fadeAttackUnit = {};
|
||||
g_fadeAttackUnit.blinkingTicks = 50; // how many ticks should first blinking phase be
|
||||
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
|
||||
|
||||
function colorFade_attackUnit(data)
|
||||
{
|
||||
var rgb = data.rgb;
|
||||
|
||||
|
||||
// init color
|
||||
if (data.tickCounter == 0)
|
||||
rgb.r = 175;
|
||||
|
||||
// blinking
|
||||
if (data.tickCounter < g_fadeAttackUnit.blinkingTicks)
|
||||
if (data.tickCounter < g_FadeAttackUnit.blinkingTicks)
|
||||
{
|
||||
// slow that process down
|
||||
if (data.tickCounter % g_fadeAttackUnit.blinkingChangeInterval != 0)
|
||||
if (data.tickCounter % g_FadeAttackUnit.blinkingChangeInterval != 0)
|
||||
return;
|
||||
|
||||
|
||||
rgb.g = rgb.g == 0 ? 255 : 0;
|
||||
}
|
||||
// wait a short time and then color fade from red to grey to nothing
|
||||
else if ( data.tickCounter >= g_fadeAttackUnit.blinkingTicks + g_fadeAttackUnit.blinkingChangeInterval)
|
||||
else if (data.tickCounter >= g_FadeAttackUnit.blinkingTicks + g_FadeAttackUnit.blinkingChangeInterval)
|
||||
{
|
||||
rgb.g += Math.round(g_fadeAttackUnit.gbcolorChangeRate * Math.sqrt(data.tickCounter - g_fadeAttackUnit.blinkingTicks));
|
||||
rgb.g += Math.round(g_FadeAttackUnit.gbcolorChangeRate * Math.sqrt(data.tickCounter - g_FadeAttackUnit.blinkingTicks));
|
||||
if (rgb.g > 255)
|
||||
rgb.g = 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;
|
||||
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;
|
||||
@@ -211,12 +215,10 @@ function colorFade_attackUnit(data)
|
||||
function smoothColorFadeRestart_attackUnit(data)
|
||||
{
|
||||
// check, if in blinking phase
|
||||
if (data.tickCounter < g_fadeAttackUnit.blinkingTicks)
|
||||
if (data.tickCounter < g_FadeAttackUnit.blinkingTicks)
|
||||
{
|
||||
data.tickCounter = data.tickCounter % (g_fadeAttackUnit.blinkingChangeInterval * 2);
|
||||
data.tickCounter = data.tickCounter % (g_FadeAttackUnit.blinkingChangeInterval * 2);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//[END] of hero fade functions
|
||||
|
||||
Reference in New Issue
Block a user