manaverse/src/text.cpp

212 lines
5.2 KiB
C++
Raw Permalink Normal View History

/*
2014-05-25 02:00:14 +03:00
* The ManaPlus Client
* Copyright (C) 2008 Douglas Boffey <DougABoffey@netscape.net>
* Copyright (C) 2008-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
2024-02-09 13:36:22 +00:00
* Copyright (C) 2011-2020 The ManaPlus Developers
* Copyright (C) 2020-2023 The ManaVerse Developers
*
2011-02-19 01:13:26 +02:00
* This file is part of The ManaPlus Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "text.h"
#include "configuration.h"
#include "textmanager.h"
#include "gui/gui.h"
2012-12-27 15:58:18 +03:00
#include "gui/theme.h"
2014-05-17 18:31:23 +03:00
#include "gui/fonts/font.h"
2014-05-19 22:50:16 +03:00
#include "resources/imagerect.h"
2016-06-07 15:41:00 +03:00
#include "resources/image/image.h"
2014-03-28 00:39:12 +03:00
#include "utils/delete2.h"
#include "debug.h"
int Text::mInstances = 0;
ImageRect Text::mBubble;
2015-05-27 21:29:06 +03:00
Text::Text(const std::string &text,
const int x, const int y,
const Graphics::Alignment alignment,
2015-05-27 21:29:06 +03:00
const Color *const color,
const Speech isSpeech,
2014-02-16 22:17:22 +03:00
Font *const font) :
mFont(font != nullptr ?
font : (gui != nullptr ? gui->getFont() : nullptr)),
2015-05-26 14:38:25 +03:00
mTextChunk(),
2013-04-20 16:40:24 +03:00
mX(x),
2012-09-08 00:17:45 +03:00
mY(y),
mWidth(mFont != nullptr ? mFont->getWidth(text) : 1),
mHeight(mFont != nullptr ? mFont->getHeight() : 1),
2013-04-20 16:40:24 +03:00
mXOffset(0),
mText(text),
mColor(color),
mOutlineColor(color != nullptr ? (isSpeech == Speech_true ?
2015-06-26 00:41:35 +03:00
*color : theme->getColor(ThemeColorId::OUTLINE, 255)) : Color()),
2015-05-26 14:38:25 +03:00
mIsSpeech(isSpeech),
mTextChanged(true)
{
if (textManager == nullptr)
{
textManager = new TextManager;
if (theme != nullptr)
{
2017-12-27 03:30:58 +03:00
theme->loadRect(mBubble,
"bubble.xml",
"",
0,
8);
}
else
{
for (int f = 0; f < 9; f ++)
2011-11-08 00:44:17 +03:00
mBubble.grid[f] = nullptr;
}
2012-07-15 00:37:34 +03:00
const float bubbleAlpha = config.getFloatValue("speechBubbleAlpha");
for (int i = 0; i < 9; i++)
{
if (mBubble.grid[i] != nullptr)
mBubble.grid[i]->setAlpha(bubbleAlpha);
}
}
++mInstances;
switch (alignment)
{
case Graphics::LEFT:
mXOffset = 0;
break;
case Graphics::CENTER:
mXOffset = mWidth / 2;
break;
case Graphics::RIGHT:
mXOffset = mWidth;
break;
default:
break;
}
mX = x - mXOffset;
if (textManager != nullptr)
textManager->addText(this);
}
Text::~Text()
{
if (textManager != nullptr)
textManager->removeText(this);
if (--mInstances == 0)
{
2018-11-26 00:51:57 +03:00
delete2(textManager)
2012-07-15 00:37:34 +03:00
for (int f = 0; f < 9; f ++)
{
if (mBubble.grid[f] != nullptr)
2012-07-15 00:37:34 +03:00
{
mBubble.grid[f]->decRef();
mBubble.grid[f] = nullptr;
}
}
}
}
2014-02-22 14:21:27 +03:00
void Text::setColor(const Color *const color)
{
2015-05-26 14:38:25 +03:00
mTextChanged = true;
2015-05-27 21:29:06 +03:00
if (mIsSpeech == Speech_true)
2015-05-26 13:23:21 +03:00
{
mColor = color;
2015-05-26 14:38:25 +03:00
mOutlineColor = *color;
2015-05-26 13:23:21 +03:00
}
else
{
mColor = color;
}
}
2015-05-27 21:39:10 +03:00
void Text::adviseXY(const int x, const int y,
const Move move)
{
if ((textManager != nullptr) && move == Move_true)
{
textManager->moveText(this, x - mXOffset, y);
}
else
{
mX = x - mXOffset;
mY = y;
}
}
2012-12-27 15:58:18 +03:00
void Text::draw(Graphics *const graphics, const int xOff, const int yOff)
{
BLOCK_START("Text::draw")
2015-05-27 21:29:06 +03:00
if (mIsSpeech == Speech_true)
{
2014-02-15 22:21:02 +03:00
graphics->drawImageRect(mX - xOff - 5,
mY - yOff - 5,
mWidth + 10,
mHeight + 10,
mBubble);
}
2015-05-26 14:38:25 +03:00
if (mTextChanged)
{
mTextChunk.textFont = mFont;
mTextChunk.deleteImage();
mTextChunk.text = mText;
mTextChunk.color = *mColor;
mTextChunk.color2 = mOutlineColor;
mFont->generate(mTextChunk);
mTextChanged = false;
}
const Image *const image = mTextChunk.img;
if (image != nullptr)
2015-05-26 14:38:25 +03:00
graphics->drawImage(image, mX - xOff, mY - yOff);
BLOCK_END("Text::draw")
}
2015-05-26 13:23:21 +03:00
FlashText::FlashText(const std::string &text,
const int x, const int y,
const Graphics::Alignment alignment,
2015-05-26 13:23:21 +03:00
const Color *const color,
Font *const font) :
2015-05-27 21:29:06 +03:00
Text(text, x, y, alignment, color, Speech_false, font),
mTime(0)
{
}
2012-12-27 15:58:18 +03:00
void FlashText::draw(Graphics *const graphics, const int xOff, const int yOff)
{
BLOCK_START("FlashText::draw")
if (mTime != 0)
{
if ((--mTime & 4) == 0)
{
BLOCK_END("FlashText::draw")
return;
}
}
Text::draw(graphics, xOff, yOff);
BLOCK_END("FlashText::draw")
}