00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "text.h"
00024
00025 #include <guichan/font.hpp>
00026
00027 #include "configuration.h"
00028 #include "textmanager.h"
00029 #include "resources/resourcemanager.h"
00030 #include "resources/image.h"
00031
00032 #include "gui/gui.h"
00033 #include "gui/palette.h"
00034 #include "gui/textrenderer.h"
00035
00036 int Text::mInstances = 0;
00037 ImageRect Text::mBubble;
00038 Image *Text::mBubbleArrow;
00039
00040 Text::Text(const std::string &text, int x, int y,
00041 gcn::Graphics::Alignment alignment,
00042 const gcn::Color* color, bool isSpeech) :
00043 mText(text),
00044 mColor(color),
00045 mIsSpeech(isSpeech)
00046 {
00047 if (textManager == 0)
00048 {
00049 textManager = new TextManager;
00050 ResourceManager *resman = ResourceManager::getInstance();
00051 Image *sbImage = resman->getImage("graphics/gui/bubble.png|W:#"
00052 + config.getValue("speechBubblecolor", "000000"));
00053 mBubble.grid[0] = sbImage->getSubImage(0, 0, 5, 5);
00054 mBubble.grid[1] = sbImage->getSubImage(5, 0, 5, 5);
00055 mBubble.grid[2] = sbImage->getSubImage(10, 0, 5, 5);
00056 mBubble.grid[3] = sbImage->getSubImage(0, 5, 5, 5);
00057 mBubble.grid[4] = sbImage->getSubImage(5, 5, 5, 5);
00058 mBubble.grid[5] = sbImage->getSubImage(10, 5, 5, 5);
00059 mBubble.grid[6] = sbImage->getSubImage(0, 10, 5, 5);
00060 mBubble.grid[7] = sbImage->getSubImage(5, 10, 5, 5);
00061 mBubble.grid[8] = sbImage->getSubImage(10, 10, 5, 5);
00062 mBubbleArrow = sbImage->getSubImage(0, 15, 15, 10);
00063 const float bubbleAlpha = config.getValue("speechBubbleAlpha", 1.0);
00064 for (int i = 0; i < 9; i++)
00065 {
00066 mBubble.grid[i]->setAlpha(bubbleAlpha);
00067 }
00068 mBubbleArrow->setAlpha(bubbleAlpha);
00069 sbImage->decRef();
00070 }
00071 ++mInstances;
00072 mHeight = boldFont->getHeight();
00073 mWidth = boldFont->getWidth(text);
00074
00075 switch (alignment)
00076 {
00077 case gcn::Graphics::LEFT:
00078 mXOffset = 0;
00079 break;
00080 case gcn::Graphics::CENTER:
00081 mXOffset = mWidth / 2;
00082 break;
00083 case gcn::Graphics::RIGHT:
00084 mXOffset = mWidth;
00085 break;
00086 }
00087 mX = x - mXOffset;
00088 mY = y;
00089 textManager->addText(this);
00090 }
00091
00092 Text::~Text()
00093 {
00094 textManager->removeText(this);
00095 if (--mInstances == 0)
00096 {
00097 delete textManager;
00098 textManager = 0;
00099 delete mBubble.grid[0];
00100 delete mBubble.grid[1];
00101 delete mBubble.grid[2];
00102 delete mBubble.grid[3];
00103 delete mBubble.grid[4];
00104 delete mBubble.grid[5];
00105 delete mBubble.grid[6];
00106 delete mBubble.grid[7];
00107 delete mBubble.grid[8];
00108 delete mBubbleArrow;
00109 }
00110 }
00111
00112 void Text::setColor(const gcn::Color *color)
00113 {
00114 mColor = color;
00115 }
00116
00117 void Text::adviseXY(int x, int y)
00118 {
00119 textManager->moveText(this, x - mXOffset, y);
00120 }
00121
00122 void Text::draw(gcn::Graphics *graphics, int xOff, int yOff)
00123 {
00124 graphics->setFont(boldFont);
00125
00126 if (mIsSpeech) {
00127 static_cast<Graphics*>(graphics)->drawImageRect(
00128 mX - xOff - 5, mY - yOff - 5, mWidth + 10, mHeight + 10,
00129 mBubble);
00130
00131
00132
00133
00134
00135
00136
00137 }
00138
00139 TextRenderer::renderText(graphics, mText,
00140 mX - xOff, mY - yOff, gcn::Graphics::LEFT,
00141 *mColor, boldFont, !mIsSpeech, true);
00142 }
00143
00144 FlashText::FlashText(const std::string &text, int x, int y,
00145 gcn::Graphics::Alignment alignment,
00146 const gcn::Color *color) :
00147 Text(text, x, y, alignment, color),
00148 mTime(0)
00149 {
00150 }
00151
00152 void FlashText::draw(gcn::Graphics *graphics, int xOff, int yOff)
00153 {
00154 if (mTime)
00155 {
00156 if ((--mTime & 4) == 0)
00157 return;
00158 }
00159 Text::draw(graphics, xOff, yOff);
00160 }