2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* CMessage.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CMessage.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../render/CAnimation.h"
|
|
|
|
#include "../render/Canvas.h"
|
|
|
|
#include "../render/Graphics.h"
|
|
|
|
#include "../render/IFont.h"
|
2024-02-26 18:53:12 +02:00
|
|
|
#include "../render/IImage.h"
|
|
|
|
#include "../render/IRenderHandler.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/Images.h"
|
|
|
|
#include "../widgets/Slider.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/TextOperations.h"
|
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
constexpr int RIGHT_CLICK_POPUP_MIN_SIZE = 100;
|
2024-02-26 22:04:30 +02:00
|
|
|
constexpr int SIDE_MARGIN = 11;
|
|
|
|
constexpr int TOP_MARGIN = 20;
|
|
|
|
constexpr int BOTTOM_MARGIN = 16;
|
|
|
|
constexpr int INTERVAL_BETWEEN_BUTTONS = 18;
|
|
|
|
constexpr int INTERVAL_BETWEEN_TEXT_AND_BUTTONS = 24;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
static std::array<std::shared_ptr<CAnimation>, PlayerColor::PLAYER_LIMIT_I> dialogBorders;
|
|
|
|
static std::array<std::vector<std::shared_ptr<IImage>>, PlayerColor::PLAYER_LIMIT_I> piecesOfBox;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
void CMessage::init()
|
|
|
|
{
|
2024-02-26 18:53:12 +02:00
|
|
|
for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-07-22 16:50:52 +02:00
|
|
|
dialogBorders[i] = GH.renderHandler().loadAnimation(AnimationPath::builtin("DIALGBOX"), EImageBlitMode::COLORKEY);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
for(int j = 0; j < dialogBorders[i]->size(0); j++)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
auto image = dialogBorders[i]->getImage(j, 0);
|
|
|
|
//assume blue color initially
|
|
|
|
if(i != 1)
|
|
|
|
image->playerColored(PlayerColor(i));
|
|
|
|
piecesOfBox[i].push_back(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMessage::dispose()
|
|
|
|
{
|
|
|
|
for(auto & item : dialogBorders)
|
|
|
|
item.reset();
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
std::vector<std::string> CMessage::breakText(std::string text, size_t maxLineWidth, EFonts font)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2023-11-26 18:55:50 +02:00
|
|
|
assert(maxLineWidth != 0);
|
2024-02-26 18:53:12 +02:00
|
|
|
if(maxLineWidth == 0)
|
|
|
|
return {text};
|
2023-11-26 18:55:50 +02:00
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
boost::algorithm::trim_right_if(text, boost::algorithm::is_any_of(std::string(" ")));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-09-23 17:43:11 +02:00
|
|
|
const auto & fontPtr = GH.renderHandler().loadFont(font);
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
// each iteration generates one output line
|
2024-02-26 18:53:12 +02:00
|
|
|
while(text.length())
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 18:53:12 +02:00
|
|
|
ui32 wordBreak = -1; //last position for line break (last space character)
|
|
|
|
ui32 currPos = 0; //current position in text
|
|
|
|
bool opened = false; //set to true when opening brace is found
|
2024-02-27 13:47:17 +02:00
|
|
|
std::string color; //color found
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
size_t symbolSize = 0; // width of character, in bytes
|
2024-09-03 00:08:59 +02:00
|
|
|
|
|
|
|
std::string printableString;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
// loops till line is full or end of text reached
|
2024-09-23 17:43:11 +02:00
|
|
|
while(currPos < text.length() && text[currPos] != 0x0a && fontPtr->getStringWidth(printableString) <= maxLineWidth)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]);
|
|
|
|
|
|
|
|
// candidate for line break
|
2024-02-26 18:53:12 +02:00
|
|
|
if(ui8(text[currPos]) <= ui8(' '))
|
2023-10-19 16:19:09 +02:00
|
|
|
wordBreak = currPos;
|
|
|
|
|
|
|
|
/* We don't count braces in string length. */
|
2024-02-26 18:53:12 +02:00
|
|
|
if(text[currPos] == '{')
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 18:53:12 +02:00
|
|
|
opened = true;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
std::smatch match;
|
2024-02-26 18:32:15 +02:00
|
|
|
std::regex expr("^\\{(.*?)\\|");
|
2023-10-19 16:19:09 +02:00
|
|
|
std::string tmp = text.substr(currPos);
|
|
|
|
if(std::regex_search(tmp, match, expr))
|
|
|
|
{
|
|
|
|
std::string colorText = match[1].str();
|
|
|
|
if(auto c = Colors::parseColor(colorText))
|
|
|
|
{
|
|
|
|
color = colorText + "|";
|
|
|
|
currPos += colorText.length() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 18:53:12 +02:00
|
|
|
else if(text[currPos] == '}')
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 18:53:12 +02:00
|
|
|
opened = false;
|
2023-10-19 16:19:09 +02:00
|
|
|
color = "";
|
|
|
|
}
|
|
|
|
else
|
2024-12-02 23:36:49 +02:00
|
|
|
printableString.append(text.data() + currPos, symbolSize);
|
2024-02-27 13:47:17 +02:00
|
|
|
currPos += symbolSize;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 22:33:14 +02:00
|
|
|
// not all line has been processed - it turned out to be too long, so erase everything after last word break
|
|
|
|
// if string consists from a single word (or this is Chinese/Korean) - erase only last symbol to bring line back to allowed length
|
2024-02-26 18:53:12 +02:00
|
|
|
if(currPos < text.length() && (text[currPos] != 0x0a))
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 18:53:12 +02:00
|
|
|
if(wordBreak != ui32(-1))
|
2024-09-23 21:07:00 +02:00
|
|
|
{
|
2023-10-19 16:19:09 +02:00
|
|
|
currPos = wordBreak;
|
2024-10-07 18:01:20 +02:00
|
|
|
if(boost::count(text.substr(0, currPos), '{') == boost::count(text.substr(0, currPos), '}'))
|
2024-09-23 21:11:53 +02:00
|
|
|
{
|
2024-09-23 21:07:00 +02:00
|
|
|
opened = false;
|
2024-09-23 21:11:53 +02:00
|
|
|
color = "";
|
|
|
|
}
|
2024-09-23 21:07:00 +02:00
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
else
|
2024-02-27 13:47:17 +02:00
|
|
|
currPos -= symbolSize;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//non-blank line
|
|
|
|
if(currPos != 0)
|
|
|
|
{
|
|
|
|
ret.push_back(text.substr(0, currPos));
|
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
if(opened)
|
2023-10-19 16:19:09 +02:00
|
|
|
/* Close the brace for the current line. */
|
|
|
|
ret.back() += '}';
|
|
|
|
|
|
|
|
text.erase(0, currPos);
|
|
|
|
}
|
|
|
|
else if(text[currPos] == 0x0a)
|
|
|
|
{
|
|
|
|
ret.push_back(""); //add empty string, no extra actions needed
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
if(text.length() != 0 && text[0] == 0x0a)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
/* Remove LF */
|
|
|
|
text.erase(0, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// trim only if line does not starts with LF
|
|
|
|
// FIXME: necessary? All lines will be trimmed before returning anyway
|
2024-02-26 18:53:12 +02:00
|
|
|
boost::algorithm::trim_left_if(text, boost::algorithm::is_any_of(std::string(" ")));
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
if(opened)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
/* Add an opening brace for the next line. */
|
2024-02-26 18:53:12 +02:00
|
|
|
if(text.length() != 0)
|
2023-10-19 16:19:09 +02:00
|
|
|
text.insert(0, "{" + color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim whitespaces of every line. */
|
2024-02-26 18:53:12 +02:00
|
|
|
for(auto & elem : ret)
|
2023-10-19 16:19:09 +02:00
|
|
|
boost::algorithm::trim(elem);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMessage::guessHeader(const std::string & msg)
|
|
|
|
{
|
|
|
|
size_t begin = 0;
|
2024-06-24 03:23:26 +02:00
|
|
|
std::string delimiters = "{}";
|
|
|
|
size_t start = msg.find_first_of(delimiters[0], begin);
|
|
|
|
size_t end = msg.find_first_of(delimiters[1], start);
|
2023-10-19 16:19:09 +02:00
|
|
|
if(start > msg.size() || end > msg.size())
|
|
|
|
return "";
|
|
|
|
return msg.substr(begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
|
|
|
|
{
|
2024-09-23 17:43:11 +02:00
|
|
|
const auto & fontPtr = GH.renderHandler().loadFont(font);
|
2024-02-27 13:47:17 +02:00
|
|
|
const auto lines = CMessage::breakText(txt, width, font);
|
2024-09-23 17:43:11 +02:00
|
|
|
size_t lineHeight = fontPtr->getLineHeight();
|
2024-02-27 13:47:17 +02:00
|
|
|
return lineHeight * lines.size();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CMessage::getEstimatedComponentHeight(int numComps)
|
|
|
|
{
|
2024-02-26 18:53:12 +02:00
|
|
|
if(numComps > 8) //Bigger than 8 components - return invalid value
|
2023-10-19 16:19:09 +02:00
|
|
|
return std::numeric_limits<int>::max();
|
2024-02-27 13:47:17 +02:00
|
|
|
if(numComps > 2)
|
2023-10-19 16:19:09 +02:00
|
|
|
return 160; // 32px * 1 row + 20 to offset
|
2024-02-27 13:47:17 +02:00
|
|
|
if(numComps > 0)
|
2023-10-19 16:19:09 +02:00
|
|
|
return 118; // 118 px to offset
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
|
|
|
|
{
|
2024-02-26 22:04:30 +02:00
|
|
|
// possible sizes of text boxes section of window
|
|
|
|
// game should pick smallest one that can fit text without slider
|
|
|
|
// or, if not possible - pick last one and use slider
|
2024-02-26 18:32:15 +02:00
|
|
|
constexpr std::array textAreaSizes = {
|
2024-02-27 13:47:17 +02:00
|
|
|
// FIXME: this size should only be used for single-line texts: Point(206, 72),
|
2024-02-26 22:04:30 +02:00
|
|
|
Point(270, 72),
|
|
|
|
Point(270, 136),
|
|
|
|
Point(270, 200),
|
|
|
|
Point(400, 136),
|
|
|
|
Point(400, 200),
|
|
|
|
Point(590, 200)
|
2024-02-26 15:48:55 +02:00
|
|
|
};
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
assert(ret && ret->text);
|
2024-02-26 18:32:15 +02:00
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
// STEP 1: DETERMINE SIZE OF ALL ELEMENTS
|
|
|
|
|
2024-02-26 18:53:12 +02:00
|
|
|
for(const auto & area : textAreaSizes)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 18:32:15 +02:00
|
|
|
ret->text->resize(area);
|
2024-02-26 18:53:12 +02:00
|
|
|
if(!ret->text->slider)
|
2024-02-26 18:32:15 +02:00
|
|
|
break; // suitable size found, use it
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
int textHeight = ret->text->pos.h;
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
if(ret->text->slider)
|
|
|
|
ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
|
|
|
|
|
2024-02-26 22:04:30 +02:00
|
|
|
int buttonsWidth = 0;
|
2024-02-26 23:26:06 +02:00
|
|
|
int buttonsHeight = 0;
|
|
|
|
if(!ret->buttons.empty())
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
// Compute total width of buttons
|
2024-02-26 22:04:30 +02:00
|
|
|
buttonsWidth = INTERVAL_BETWEEN_BUTTONS * (ret->buttons.size() - 1); // space between all buttons
|
2024-02-27 13:47:17 +02:00
|
|
|
for(const auto & elem : ret->buttons) //and add buttons width
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 22:04:30 +02:00
|
|
|
buttonsWidth += elem->pos.w;
|
2024-02-26 23:26:06 +02:00
|
|
|
vstd::amax(buttonsHeight, elem->pos.h);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
// STEP 2: COMPUTE WINDOW SIZE
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
if(ret->buttons.empty() && !ret->components)
|
|
|
|
{
|
|
|
|
// use more compact form for right-click popup with no buttons / components
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
ret->pos.w = std::max(RIGHT_CLICK_POPUP_MIN_SIZE, ret->text->label->textSize.x + 2 * SIDE_MARGIN);
|
|
|
|
ret->pos.h = std::max(RIGHT_CLICK_POPUP_MIN_SIZE, ret->text->label->textSize.y + TOP_MARGIN + BOTTOM_MARGIN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int windowContentWidth = ret->text->pos.w;
|
|
|
|
int windowContentHeight = ret->text->pos.h;
|
|
|
|
if(ret->components)
|
|
|
|
{
|
|
|
|
vstd::amax(windowContentWidth, ret->components->pos.w);
|
|
|
|
windowContentHeight += INTERVAL_BETWEEN_TEXT_AND_BUTTONS + ret->components->pos.h;
|
|
|
|
}
|
|
|
|
if(!ret->buttons.empty())
|
|
|
|
{
|
|
|
|
vstd::amax(windowContentWidth, buttonsWidth);
|
|
|
|
windowContentHeight += INTERVAL_BETWEEN_TEXT_AND_BUTTONS + buttonsHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->pos.w = windowContentWidth + 2 * SIDE_MARGIN;
|
|
|
|
ret->pos.h = windowContentHeight + TOP_MARGIN + BOTTOM_MARGIN;
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
// STEP 3: MOVE ALL ELEMENTS IN PLACE
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 22:04:30 +02:00
|
|
|
if(ret->buttons.empty() && !ret->components)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 23:26:06 +02:00
|
|
|
ret->text->trimToFit();
|
|
|
|
ret->text->center(ret->pos.center());
|
2024-02-26 18:32:15 +02:00
|
|
|
}
|
2024-02-26 23:26:06 +02:00
|
|
|
else
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 23:26:06 +02:00
|
|
|
if(ret->components)
|
|
|
|
ret->components->moveBy(Point((ret->pos.w - ret->components->pos.w) / 2, TOP_MARGIN + ret->text->pos.h + INTERVAL_BETWEEN_TEXT_AND_BUTTONS));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 23:26:06 +02:00
|
|
|
ret->text->trimToFit();
|
|
|
|
ret->text->moveBy(Point((ret->pos.w - ret->text->pos.w) / 2, TOP_MARGIN + (textHeight - ret->text->pos.h) / 2 ));
|
|
|
|
|
|
|
|
if(!ret->buttons.empty())
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-26 23:26:06 +02:00
|
|
|
int buttonPosX = ret->pos.w / 2 - buttonsWidth / 2;
|
|
|
|
int buttonPosY = ret->pos.h - BOTTOM_MARGIN - ret->buttons[0]->pos.h;
|
|
|
|
|
2024-02-27 13:47:17 +02:00
|
|
|
for(const auto & elem : ret->buttons)
|
2024-02-26 23:26:06 +02:00
|
|
|
{
|
|
|
|
elem->moveBy(Point(buttonPosX, buttonPosY));
|
|
|
|
buttonPosX += elem->pos.w + INTERVAL_BETWEEN_BUTTONS;
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-26 22:04:30 +02:00
|
|
|
|
|
|
|
ret->backgroundTexture->pos = ret->pos;
|
2024-02-26 23:26:06 +02:00
|
|
|
ret->center();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 18:32:15 +02:00
|
|
|
void CMessage::drawBorder(PlayerColor playerColor, Canvas & to, int w, int h, int x, int y)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if(playerColor.isSpectator())
|
|
|
|
playerColor = PlayerColor(1);
|
|
|
|
auto & box = piecesOfBox.at(playerColor.getNum());
|
|
|
|
|
|
|
|
// Note: this code assumes that the corner dimensions are all the same.
|
|
|
|
|
|
|
|
// Horizontal borders
|
|
|
|
int start_x = x + box[0]->width();
|
|
|
|
const int stop_x = x + w - box[1]->width();
|
2024-02-26 18:53:12 +02:00
|
|
|
const int bottom_y = y + h - box[7]->height() + 1;
|
|
|
|
while(start_x < stop_x)
|
|
|
|
{
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
// Top border
|
2024-02-26 18:32:15 +02:00
|
|
|
to.draw(box[6], Point(start_x, y));
|
2023-10-19 16:19:09 +02:00
|
|
|
// Bottom border
|
2024-02-26 18:32:15 +02:00
|
|
|
to.draw(box[7], Point(start_x, bottom_y));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 18:32:15 +02:00
|
|
|
start_x += box[6]->width();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical borders
|
|
|
|
int start_y = y + box[0]->height();
|
2024-02-26 18:53:12 +02:00
|
|
|
const int stop_y = y + h - box[2]->height() + 1;
|
|
|
|
const int right_x = x + w - box[5]->width();
|
|
|
|
while(start_y < stop_y)
|
|
|
|
{
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
// Left border
|
2024-02-26 18:32:15 +02:00
|
|
|
to.draw(box[4], Point(x, start_y));
|
2023-10-19 16:19:09 +02:00
|
|
|
// Right border
|
2024-02-26 18:32:15 +02:00
|
|
|
to.draw(box[5], Point(right_x, start_y));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-26 18:32:15 +02:00
|
|
|
start_y += box[4]->height();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//corners
|
2024-02-26 18:53:12 +02:00
|
|
|
to.draw(box[0], Point(x, y));
|
|
|
|
to.draw(box[1], Point(x + w - box[1]->width(), y));
|
|
|
|
to.draw(box[2], Point(x, y + h - box[2]->height() + 1));
|
|
|
|
to.draw(box[3], Point(x + w - box[3]->width(), y + h - box[3]->height() + 1));
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|