00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "palette.h"
00024
00025 #include "configuration.h"
00026 #include "game.h"
00027
00028 #include "gui/gui.h"
00029
00030 #include "utils/gettext.h"
00031 #include "utils/stringutils.h"
00032
00033 #include <math.h>
00034
00035 const gcn::Color Palette::BLACK = gcn::Color(0, 0, 0);
00036
00037 const gcn::Color Palette::RAINBOW_COLORS[7] = {
00038 gcn::Color(255, 0, 0),
00039 gcn::Color(255, 153, 0),
00040 gcn::Color(255, 255, 0),
00041 gcn::Color(0, 153, 0),
00042 gcn::Color(0, 204, 204),
00043 gcn::Color(51, 0, 153),
00044 gcn::Color(153, 0, 153)
00045 };
00047 const int Palette::RAINBOW_COLOR_COUNT = 7;
00048
00049 std::string Palette::getConfigName(const std::string &typeName)
00050 {
00051 std::string res = "Color" + typeName;
00052
00053 int pos = 5;
00054 for (size_t i = 0; i < typeName.length(); i++)
00055 {
00056 if (i == 0 || typeName[i] == '_')
00057 {
00058 if (i > 0)
00059 i++;
00060
00061 res[pos] = typeName[i];
00062 }
00063 else
00064 {
00065 res[pos] = tolower(typeName[i]);
00066 }
00067 pos++;
00068 }
00069 res.erase(pos, res.length() - pos);
00070
00071 return res;
00072 }
00073
00074 DEFENUMNAMES(ColorType, COLOR_TYPE);
00075
00076 Palette::Palette() :
00077 mRainbowTime(tick_time),
00078 mColVector(ColVector(TYPE_COUNT))
00079 {
00080 std::string indent = " ";
00081 addColor(TEXT, 0x000000, STATIC, _("Text"));
00082 addColor(SHADOW, 0x000000, STATIC, indent + _("Text Shadow"));
00083 addColor(OUTLINE, 0x000000, STATIC, indent + _("Text Outline"));
00084 addColor(PROGRESS_BAR, 0xffffff, STATIC, indent + _("Progress Bar Labels"));
00085
00086 addColor(BACKGROUND, 0xffffff, STATIC, _("Background"));
00087
00088 addColor(HIGHLIGHT, 0xebc873, STATIC, _("Highlight"), 'H');
00089 addColor(TAB_HIGHLIGHT, 0xff0000, PULSE, indent + _("Tab Highlight"));
00090 addColor(SHOP_WARNING, 0x910000, STATIC, indent + _("Item too expensive"));
00091 addColor(ITEM_EQUIPPED, 0x000091, STATIC, indent + _("Item is equipped"));
00092
00093 addColor(CHAT, 0x000000, STATIC, _("Chat"), 'C');
00094 addColor(GM, 0xff0000, STATIC, indent + _("GM"), 'G');
00095 addColor(PLAYER, 0x1fa052, STATIC, indent + _("Player"), 'Y');
00096 addColor(WHISPER, 0x0000ff, STATIC, indent + _("Whisper"), 'W');
00097 addColor(IS, 0xa08527, STATIC, indent + _("Is"), 'I');
00098 addColor(PARTY, 0xff00d8, STATIC, indent + _("Party"), 'P');
00099 addColor(SERVER, 0x8415e2, STATIC, indent + _("Server"), 'S');
00100 addColor(LOGGER, 0x919191, STATIC, indent + _("Logger"), 'L');
00101 addColor(HYPERLINK, 0xe50d0d, STATIC, indent + _("Hyperlink"), '<');
00102
00103 addColor(BEING, 0xffffff, STATIC, _("Being"));
00104 addColor(PC, 0xffffff, STATIC, indent + _("Other Players' Names"));
00105 addColor(SELF, 0xff8040, STATIC, indent + _("Own Name"));
00106 addColor(GM_NAME, 0x00ff00, STATIC, indent + _("GM Names"));
00107 addColor(NPC, 0xc8c8ff, STATIC, indent + _("NPCs"));
00108 addColor(MONSTER, 0xff4040, STATIC, indent + _("Monsters"));
00109
00110 addColor(UNKNOWN_ITEM, 0x000000, STATIC, _("Unknown Item Type"));
00111 addColor(GENERIC, 0x21a5b1, STATIC, indent + _("Generics"));
00112 addColor(HEAD, 0x527fa4, STATIC, indent + _("Hats"));
00113 addColor(USABLE, 0x268d24, STATIC, indent + _("Usables"));
00114 addColor(TORSO, 0xd12aa4, STATIC, indent + _("Shirts"));
00115 addColor(ONEHAND, 0xf42a2a, STATIC, indent + _("1 Handed Weapons"));
00116 addColor(LEGS, 0x699900, STATIC, indent + _("Pants"));
00117 addColor(FEET, 0xaa1d48, STATIC, indent + _("Shoes"));
00118 addColor(TWOHAND, 0xf46d0e, STATIC, indent + _("2 Handed Weapons"));
00119 addColor(SHIELD, 0x9c2424, STATIC, indent + _("Shields"));
00120 addColor(RING, 0x0000ff, STATIC, indent + _("Rings"));
00121 addColor(NECKLACE, 0xff00ff, STATIC, indent + _("Necklaces"));
00122 addColor(ARMS, 0x9c24e8, STATIC, indent + _("Arms"));
00123 addColor(AMMO, 0x8b6311, STATIC, indent + _("Ammo"));
00124
00125 addColor(PARTICLE, 0xffffff, STATIC, _("Particle Effects"));
00126 addColor(PICKUP_INFO, 0x28dc28, STATIC, indent + _("Pickup Notification"));
00127 addColor(EXP_INFO, 0xffff00, STATIC, indent + _("Exp Notification"));
00128 addColor(HIT_PLAYER_MONSTER, 0x0064ff, STATIC,
00129 indent + _("Player hits Monster"));
00130 addColor(HIT_MONSTER_PLAYER, 0xff3232, STATIC,
00131 indent + _("Monster hits Player"));
00132 addColor(HIT_CRITICAL, 0xff0000, RAINBOW, indent + _("Critical Hit"));
00133 addColor(MISS, 0xffff00, STATIC, indent + _("Misses"));
00134
00135 addColor(HPBAR_FULL, 0x0f6a20, STATIC, _("HP Bar"));
00136 addColor(HPBAR_THREE_QUARTERS, 0xc38948, STATIC, indent + _("3/4 HP Bar"));
00137 addColor(HPBAR_ONE_HALF, 0xe28000, STATIC, indent + _("1/2 HP Bar"));
00138 addColor(HPBAR_ONE_QUARTER, 0xff0000, PULSE, indent + _("1/4 HP Bar"));
00139 commit(true);
00140 }
00141
00142 Palette::~Palette()
00143 {
00144 const std::string *configName;
00145 for (ColVector::iterator col = mColVector.begin(),
00146 colEnd = mColVector.end(); col != colEnd; ++col)
00147 {
00148 configName = &ColorTypeNames[col->type];
00149 config.setValue(*configName + "Gradient", col->committedGrad);
00150
00151 if (col->grad != STATIC)
00152 config.setValue(*configName + "Delay", col->delay);
00153
00154 if (col->grad == STATIC || col->grad == PULSE)
00155 config.setValue(*configName, toString(col->getRGB()));
00156 }
00157 }
00158
00159 const gcn::Color& Palette::getColor(char c, bool &valid)
00160 {
00161 for (ColVector::const_iterator col = mColVector.begin(),
00162 colEnd = mColVector.end(); col != colEnd; ++col)
00163 {
00164 if (col->ch == c)
00165 {
00166 valid = true;
00167 return col->color;
00168 }
00169 }
00170 valid = false;
00171 return BLACK;
00172 }
00173
00174 void Palette::setColor(ColorType type, int r, int g, int b)
00175 {
00176 mColVector[type].color.r = r;
00177 mColVector[type].color.g = g;
00178 mColVector[type].color.b = b;
00179 }
00180
00181 void Palette::setGradient(ColorType type, GradientType grad)
00182 {
00183 ColorElem *elem = &mColVector[type];
00184 if (elem->grad != STATIC && grad == STATIC)
00185 {
00186 for (size_t i = 0; i < mGradVector.size(); i++)
00187 {
00188 if (mGradVector[i] == elem)
00189 {
00190 mGradVector.erase(mGradVector.begin() + i);
00191 break;
00192 }
00193 }
00194 }
00195 else if (elem->grad == STATIC && grad != STATIC)
00196 {
00197 mGradVector.push_back(elem);
00198 }
00199
00200 if (elem->grad != grad)
00201 {
00202 elem->grad = grad;
00203 }
00204 }
00205
00206 std::string Palette::getElementAt(int i)
00207 {
00208 if (i < 0 || i >= getNumberOfElements())
00209 {
00210 return "";
00211 }
00212 return mColVector[i].text;
00213 }
00214
00215 Palette::ColorType Palette::getColorTypeAt(int i)
00216 {
00217 if (i < 0 || i >= getNumberOfElements())
00218 {
00219 return CHAT;
00220 }
00221 return mColVector[i].type;
00222 }
00223
00224 void Palette::commit(bool commitNonStatic)
00225 {
00226 for (ColVector::iterator i = mColVector.begin(), iEnd = mColVector.end();
00227 i != iEnd; ++i)
00228 {
00229 i->committedGrad = i->grad;
00230 i->committedDelay = i->delay;
00231 if (commitNonStatic || i->grad == STATIC)
00232 {
00233 i->committedColor = i->color;
00234 }
00235 else if (i->grad == PULSE)
00236 {
00237 i->committedColor = i->testColor;
00238 }
00239 }
00240 }
00241
00242 void Palette::rollback()
00243 {
00244 for (ColVector::iterator i = mColVector.begin(), iEnd = mColVector.end();
00245 i != iEnd; ++i)
00246 {
00247 if (i->grad != i->committedGrad)
00248 {
00249 setGradient(i->type, i->committedGrad);
00250 }
00251 setGradientDelay(i->type, i->committedDelay);
00252 setColor(i->type, i->committedColor.r, i->committedColor.g,
00253 i->committedColor.b);
00254 if (i->grad == PULSE)
00255 {
00256 i->testColor.r = i->committedColor.r;
00257 i->testColor.g = i->committedColor.g;
00258 i->testColor.b = i->committedColor.b;
00259 }
00260 }
00261 }
00262
00263 void Palette::addColor(Palette::ColorType type, int rgb,
00264 Palette::GradientType grad, const std::string &text,
00265 char c, int delay)
00266 {
00267 const std::string *configName = &ColorTypeNames[type];
00268 gcn::Color trueCol = (int) config.getValue(*configName, rgb);
00269 grad = (GradientType) config.getValue(*configName + "Gradient", grad);
00270 delay = (int) config.getValue(*configName + "Delay", delay);
00271 mColVector[type].set(type, trueCol, grad, text, c, delay);
00272
00273 if (grad != STATIC)
00274 mGradVector.push_back(&mColVector[type]);
00275 }
00276
00277 void Palette::advanceGradient()
00278 {
00279 if (get_elapsed_time(mRainbowTime) > 5)
00280 {
00281 int pos, colIndex, colVal, delay, numOfColors;
00282
00283
00284
00285 int advance = get_elapsed_time(mRainbowTime) / 5;
00286 double startColVal, destColVal;
00287
00288 for (size_t i = 0; i < mGradVector.size(); i++)
00289 {
00290 delay = mGradVector[i]->delay;
00291
00292 if (mGradVector[i]->grad == PULSE)
00293 delay = delay / 20;
00294
00295 numOfColors = (mGradVector[i]->grad == SPECTRUM ? 6 :
00296 mGradVector[i]->grad == PULSE ? 127 :
00297 RAINBOW_COLOR_COUNT);
00298
00299 mGradVector[i]->gradientIndex =
00300 (mGradVector[i]->gradientIndex + advance) %
00301 (delay * numOfColors);
00302
00303 pos = mGradVector[i]->gradientIndex % delay;
00304 colIndex = mGradVector[i]->gradientIndex / delay;
00305
00306 if (mGradVector[i]->grad == PULSE)
00307 {
00308 colVal = (int) (255.0 * sin(M_PI * colIndex / numOfColors));
00309
00310 const gcn::Color* col = &mGradVector[i]->testColor;
00311
00312 mGradVector[i]->color.r = ((colVal * col->r) / 255) % (col->r + 1);
00313 mGradVector[i]->color.g = ((colVal * col->g) / 255) % (col->g + 1);
00314 mGradVector[i]->color.b = ((colVal * col->b) / 255) % (col->b + 1);
00315 }
00316 if (mGradVector[i]->grad == SPECTRUM)
00317 {
00318 if (colIndex % 2)
00319 {
00320 colVal = (int)(255.0 * (cos(M_PI * pos / delay) + 1) / 2);
00321 }
00322 else
00323 {
00324 colVal = (int)(255.0 * (cos(M_PI * (delay - pos) / delay) +
00325 1) / 2);
00326 }
00327
00328 mGradVector[i]->color.r =
00329 (colIndex == 0 || colIndex == 5) ? 255 :
00330 (colIndex == 1 || colIndex == 4) ? colVal : 0;
00331 mGradVector[i]->color.g =
00332 (colIndex == 1 || colIndex == 2) ? 255 :
00333 (colIndex == 0 || colIndex == 3) ? colVal : 0;
00334 mGradVector[i]->color.b =
00335 (colIndex == 3 || colIndex == 4) ? 255 :
00336 (colIndex == 2 || colIndex == 5) ? colVal : 0;
00337 }
00338 else if (mGradVector[i]->grad == RAINBOW)
00339 {
00340 const gcn::Color* startCol = &RAINBOW_COLORS[colIndex];
00341 const gcn::Color* destCol =
00342 &RAINBOW_COLORS[(colIndex + 1) % numOfColors];
00343
00344 startColVal = (cos(M_PI * pos / delay) + 1) / 2;
00345 destColVal = 1 - startColVal;
00346
00347 mGradVector[i]->color.r =(int)(startColVal * startCol->r +
00348 destColVal * destCol->r);
00349
00350 mGradVector[i]->color.g =(int)(startColVal * startCol->g +
00351 destColVal * destCol->g);
00352
00353 mGradVector[i]->color.b =(int)(startColVal * startCol->b +
00354 destColVal * destCol->b);
00355 }
00356 }
00357
00358 mRainbowTime = tick_time;
00359 }
00360 }