| 1 | /* |
| 2 | DESCRIPTION : Some functions to make colour fades on GUI elements (f.e. used for hero and group icons) |
| 3 | NOTES : |
| 4 | */ |
| 5 | |
| 6 | // Used for storing object names of running color fades in order to stop them, if the fade is restarted before the old ended |
| 7 | var g_colorFade = {}; |
| 8 | g_colorFade["id"] = {}; |
| 9 | g_colorFade["tick"] = {}; |
| 10 | |
| 11 | /** |
| 12 | * starts fading a colour of a GUI object using the sprite argument |
| 13 | * name: name of the object which colour should be faded |
| 14 | * changeInterval: interval in ms when the next colour change should be made |
| 15 | * duration: maximal duration of the complete fade |
| 16 | * colour: RGB + opacity object with keys r,g,b and o |
| 17 | * fun_colorTransform: function which transform the colors; |
| 18 | * arguments: [colour object, tickCounter] |
| 19 | * fun_smoothRestart [optional]: a function, which returns a smooth tick counter, if the fade should be started; |
| 20 | * arguments: [tickCounter of current fade; not smaller than 1 or it restarts at 0] returns: smooth tick counter value |
| 21 | * tickCounter [optional]: should not be set by hand! - how often the function was called recursively |
| 22 | */ |
| 23 | function fadeColour(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, tickCounter) |
| 24 | { |
| 25 | // get the overlay |
| 26 | var overlay = Engine.GetGUIObjectByName(name); |
| 27 | if (!overlay) |
| 28 | return; |
| 29 | |
| 30 | // check, if fade overlay was started just now |
| 31 | if (!tickCounter) |
| 32 | { |
| 33 | tickCounter = 1; |
| 34 | overlay.hidden = false; |
| 35 | |
| 36 | // check, if another animation is running and restart it, if it's the case |
| 37 | if (isColourFadeRunning(name)) |
| 38 | { |
| 39 | restartColourFade(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, g_colorFade.tick[name]); |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // get colors |
| 45 | fun_colorTransform(colour, tickCounter); |
| 46 | |
| 47 | // set new colour |
| 48 | overlay.sprite="colour: "+colour.r+" "+colour.g+" "+colour.b+" "+colour.o; |
| 49 | |
| 50 | // recusive call, if duration is positive |
| 51 | duration-= changeInterval; |
| 52 | if (duration > 0 && colour.o > 0) |
| 53 | { |
| 54 | var id = setTimeout(function() { fadeColour(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, ++tickCounter); }, changeInterval); |
| 55 | g_colorFade.id[name] = id; |
| 56 | g_colorFade.tick[name] = tickCounter; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | overlay.hidden = true; |
| 61 | stopColourFade(name); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * checks, if a colour fade on that object is running |
| 68 | * name: name of the object which colour fade should be checked |
| 69 | * return: true a running fade was found |
| 70 | */ |
| 71 | function isColourFadeRunning(name) |
| 72 | { |
| 73 | return name in g_colorFade.id; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * stops fading a colour |
| 78 | * name: name of the object which colour fade should be stopped |
| 79 | * hideOverlay: hides the overlay, if true |
| 80 | * return: true a running fade was stopped |
| 81 | */ |
| 82 | function stopColourFade(name, hideOverlay) |
| 83 | { |
| 84 | // check, if a colour fade is running |
| 85 | if (!isColourFadeRunning(name)) |
| 86 | return false; |
| 87 | |
| 88 | // delete the timer |
| 89 | clearTimeout(g_colorFade.id[name]); |
| 90 | delete g_colorFade.id[name]; |
| 91 | delete g_colorFade.tick[name]; |
| 92 | |
| 93 | // get the overlay and hide it |
| 94 | if (hideOverlay) |
| 95 | { |
| 96 | var overlay = Engine.GetGUIObjectByName(name); |
| 97 | if(overlay) |
| 98 | overlay.hidden = true; |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * restarts a colour fade |
| 105 | * see paramter in fadeColour function |
| 106 | */ |
| 107 | function restartColourFade(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, tickCounter) |
| 108 | { |
| 109 | // check, if a colour fade is running |
| 110 | if (!isColourFadeRunning(name)) |
| 111 | return false; |
| 112 | |
| 113 | // check, if fade can be restarted smoothly |
| 114 | if (fun_smoothRestart) |
| 115 | { |
| 116 | tickCounter = fun_smoothRestart(colour, tickCounter); |
| 117 | // set new function to existing timer |
| 118 | var fun = function() { fadeColour(name, changeInterval, duration, colour, fun_colorTransform, fun_smoothRestart, tickCounter); }; |
| 119 | setNewTimerFunction(g_colorFade.id[name], fun); |
| 120 | } |
| 121 | // stop it and restart it |
| 122 | else |
| 123 | { |
| 124 | stopColourFade(name, true); |
| 125 | fadeColour(name, changeInterval, duration, colour, fun_colorTransform); |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | /** PREDEFINED FUNCTIONS */ |
| 131 | |
| 132 | //[START] of hero fade functions |
| 133 | |
| 134 | var g_fadeAttackUnit = {}; |
| 135 | g_fadeAttackUnit.blinkingTicks = 50; // how many ticks should first blinking phase be |
| 136 | g_fadeAttackUnit.blinkingChangeInterval = 5; // how often should the colour be changed during the blinking phase |
| 137 | g_fadeAttackUnit.gbColourChangeRate = 3; // how fast should blue and green part of the colour change |
| 138 | g_fadeAttackUnit.fadeOutStart = 100; // when should the fade out start using the opacity |
| 139 | g_fadeAttackUnit.opacityChangeRate = 3; // how fast should opacity change |
| 140 | |
| 141 | /** |
| 142 | * rgb: colour object with keys r,g,b and o |
| 143 | * tickCounter: how often the fade was executed |
| 144 | */ |
| 145 | function colourFade_attackUnit(rgb, tickCounter) |
| 146 | { |
| 147 | // blinking |
| 148 | if (tickCounter < g_fadeAttackUnit.blinkingTicks) |
| 149 | { |
| 150 | // slow that process down |
| 151 | if (tickCounter % g_fadeAttackUnit.blinkingChangeInterval != 0) |
| 152 | return; |
| 153 | |
| 154 | rgb.g = rgb.g == 0 ? 255 : rgb.g = 0; |
| 155 | rgb.b = rgb.b == 0 ? 255 : rgb.b = 0; |
| 156 | } |
| 157 | // wait a short time and then colour fade from red to grey to nothing |
| 158 | else if ( tickCounter >= g_fadeAttackUnit.blinkingTicks + g_fadeAttackUnit.blinkingChangeInterval) |
| 159 | { |
| 160 | rgb.g = rgb.g < 255 ? rgb.g += g_fadeAttackUnit.gbColourChangeRate * Math.sqrt(tickCounter - g_fadeAttackUnit.blinkingTicks) : 255; |
| 161 | rgb.b = rgb.g; |
| 162 | |
| 163 | // start with fading it out |
| 164 | if (rgb.g > g_fadeAttackUnit.fadeOutStart) |
| 165 | rgb.o = rgb.o > g_fadeAttackUnit.opacityChangeRate ? rgb.o -= g_fadeAttackUnit.opacityChangeRate : 0; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * makes a smooth fade, if the attack on the unit has not stopped yet |
| 171 | * rgb: colour object with keys r,g,b and o |
| 172 | * tickCounter: how often the fade was executed |
| 173 | */ |
| 174 | function smoothColourFadeRestart_attackUnit(rgb, tickCounter) |
| 175 | { |
| 176 | // check, if in blinking phase |
| 177 | if (tickCounter < g_fadeAttackUnit.blinkingTicks) |
| 178 | { |
| 179 | // get rgb to current state |
| 180 | for (var i = 1; i <= tickCounter; i++) |
| 181 | colourFade_attackUnit(rgb, i); |
| 182 | // set the tick counter back to start |
| 183 | return (tickCounter % (g_fadeAttackUnit.blinkingChangeInterval * 2)) + 1; |
| 184 | } |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | //[END] of hero fade functions |
| 189 | No newline at end of file |