1
/*
2
 *  Support for non-overlapping floating text
3
 *  Copyright (C) 2008  Douglas Boffey <DougABoffey@netscape.net>
4
 *  Copyright (C) 2008  The Mana World Development Team
5
 *
6
 *  This file is part of The Mana World.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program; if not, write to the Free Software
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
 */
22
23
#include "text.h"
24
25
#include <guichan/font.hpp>
26
27
#include "configuration.h"
28
#include "textmanager.h"
29
#include "resources/resourcemanager.h"
30
#include "resources/image.h"
31
32
#include "gui/gui.h"
33
#include "gui/palette.h"
34
#include "gui/textrenderer.h"
35
36
int Text::mInstances = 0;
37
ImageRect Text::mBubble;
38
Image *Text::mBubbleArrow;
39
40
Text::Text(const std::string &text, int x, int y,
41
           gcn::Graphics::Alignment alignment,
42
           const gcn::Color* color, bool isSpeech) :
43
    mText(text),
44
    mColor(color),
45
    mFont(gui->getFont()),
46
    mIsSpeech(isSpeech)
47
{
48
    if (textManager == 0)
49
    {
50
        textManager = new TextManager;
51
        ResourceManager *resman = ResourceManager::getInstance();
52
        Image *sbImage = resman->getImage("graphics/gui/bubble.png|W:#"
53
            + config.getValue("speechBubblecolor", "000000"));
54
        mBubble.grid[0] = sbImage->getSubImage(0, 0, 5, 5);
55
        mBubble.grid[1] = sbImage->getSubImage(5, 0, 5, 5);
56
        mBubble.grid[2] = sbImage->getSubImage(10, 0, 5, 5);
57
        mBubble.grid[3] = sbImage->getSubImage(0, 5, 5, 5);
58
        mBubble.grid[4] = sbImage->getSubImage(5, 5, 5, 5);
59
        mBubble.grid[5] = sbImage->getSubImage(10, 5, 5, 5);
60
        mBubble.grid[6] = sbImage->getSubImage(0, 10, 5, 5);
61
        mBubble.grid[7] = sbImage->getSubImage(5, 10, 5, 5);
62
        mBubble.grid[8] = sbImage->getSubImage(10, 10, 5, 5);
63
        mBubbleArrow = sbImage->getSubImage(0, 15, 15, 10);
64
        const float bubbleAlpha = config.getValue("speechBubbleAlpha", 1.0);
65
        for (int i = 0; i < 9; i++)
66
        {
67
             mBubble.grid[i]->setAlpha(bubbleAlpha);
68
        }
69
        mBubbleArrow->setAlpha(bubbleAlpha);
70
        sbImage->decRef();
71
    }
72
    ++mInstances;
73
    mHeight = mFont->getHeight();
74
    mWidth = mFont->getWidth(text);
75
76
    switch (alignment)
77
    {
78
        case gcn::Graphics::LEFT:
79
            mXOffset = 0;
80
            break;
81
        case gcn::Graphics::CENTER:
82
            mXOffset = mWidth / 2;
83
            break;
84
        case gcn::Graphics::RIGHT:
85
            mXOffset = mWidth;
86
            break;
87
    }
88
    mX = x - mXOffset;
89
    mY = y;
90
    textManager->addText(this);
91
}
92
93
Text::~Text()
94
{
95
    textManager->removeText(this);
96
    if (--mInstances == 0)
97
    {
98
        delete textManager;
99
        textManager = 0;
100
        delete mBubble.grid[0];
101
        delete mBubble.grid[1];
102
        delete mBubble.grid[2];
103
        delete mBubble.grid[3];
104
        delete mBubble.grid[4];
105
        delete mBubble.grid[5];
106
        delete mBubble.grid[6];
107
        delete mBubble.grid[7];
108
        delete mBubble.grid[8];
109
        delete mBubbleArrow;
110
    }
111
}
112
113
void Text::setColor(const gcn::Color *color)
114
{
115
    mColor = color;
116
}
117
118
void Text::adviseXY(int x, int y)
119
{
120
    textManager->moveText(this, x - mXOffset, y);
121
}
122
123
void Text::draw(gcn::Graphics *graphics, int xOff, int yOff)
124
{
125
    if (mIsSpeech) {
126
        static_cast<Graphics*>(graphics)->drawImageRect(
127
                mX - xOff - 5, mY - yOff - 5, mWidth + 10, mHeight + 10,
128
                mBubble);
129
        /*
130
        if (mWidth >= 15) {
131
            static_cast<Graphics*>(graphics)->drawImage(
132
                    mBubbleArrow, mX - xOff - 7 + mWidth / 2,
133
                    mY - yOff + mHeight + 4);
134
        }
135
        */
136
    }
137
138
    TextRenderer::renderText(graphics, mText,
139
            mX - xOff, mY - yOff, gcn::Graphics::LEFT,
140
            *mColor, mFont, !mIsSpeech, true);
141
}
142
143
FlashText::FlashText(const std::string &text, int x, int y,
144
                     gcn::Graphics::Alignment alignment,
145
                     const gcn::Color *color) :
146
    Text(text, x, y, alignment, color),
147
    mTime(0)
148
{
149
}
150
151
void FlashText::draw(gcn::Graphics *graphics, int xOff, int yOff)
152
{
153
    if (mTime)
154
    {
155
        if ((--mTime & 4) == 0)
156
            return;
157
    }
158
    Text::draw(graphics, xOff, yOff);
159
}