2014-07-15 10:14:49 +03:00
|
|
|
/*
|
|
|
|
* TextControls.h, 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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../gui/CIntObject.h"
|
2023-01-18 15:50:52 +02:00
|
|
|
#include "../gui/TextAlignment.h"
|
2023-02-02 21:15:13 +02:00
|
|
|
#include "../render/Colors.h"
|
2023-07-31 18:50:55 +02:00
|
|
|
#include "../render/EFont.h"
|
2017-07-13 10:26:03 +02:00
|
|
|
#include "../../lib/FunctionList.h"
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "../../lib/filesystem/ResourcePath.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
class IImage;
|
2014-07-15 10:14:49 +03:00
|
|
|
class CSlider;
|
|
|
|
|
|
|
|
/// Base class for all text-related widgets.
|
|
|
|
/// Controls text blitting-related options
|
|
|
|
class CTextContainer : public virtual CIntObject
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// returns size of border, for left- or right-aligned text
|
|
|
|
virtual Point getBorderSize() = 0;
|
|
|
|
/// do actual blitting of line. Text "what" will be placed at "where" and aligned according to alignment
|
2023-06-02 15:42:18 +02:00
|
|
|
void blitLine(Canvas & to, Rect where, std::string what);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
public:
|
2022-11-26 23:12:20 +02:00
|
|
|
ETextAlignment alignment;
|
2014-07-15 10:14:49 +03:00
|
|
|
EFonts font;
|
2023-07-31 18:50:55 +02:00
|
|
|
ColorRGBA color; // default font color. Can be overridden by placing "{}" into the string
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Label which shows text
|
|
|
|
class CLabel : public CTextContainer
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Point getBorderSize() override;
|
|
|
|
virtual std::string visibleText();
|
2024-01-08 00:42:48 +02:00
|
|
|
virtual void trimText();
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
std::shared_ptr<CIntObject> background;
|
2014-07-15 10:14:49 +03:00
|
|
|
std::string text;
|
2024-01-08 00:42:48 +02:00
|
|
|
int maxWidth;
|
2014-07-15 10:14:49 +03:00
|
|
|
bool autoRedraw; //whether control will redraw itself on setTxt
|
|
|
|
|
2022-12-19 22:04:50 +02:00
|
|
|
public:
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
std::string getText();
|
2018-03-20 10:19:33 +02:00
|
|
|
virtual void setAutoRedraw(bool option);
|
2022-05-07 11:56:34 +02:00
|
|
|
virtual void setText(const std::string & Txt);
|
2023-07-31 18:50:55 +02:00
|
|
|
virtual void setColor(const ColorRGBA & Color);
|
2018-03-20 10:19:33 +02:00
|
|
|
size_t getWidth();
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT,
|
2024-01-08 00:42:48 +02:00
|
|
|
const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "", int maxWidth = 0);
|
2023-06-02 15:42:18 +02:00
|
|
|
void showAll(Canvas & to) override; //shows statusbar (with current text)
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Small helper class to manage group of similar labels
|
|
|
|
class CLabelGroup : public CIntObject
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
std::vector<std::shared_ptr<CLabel>> labels;
|
2014-07-15 10:14:49 +03:00
|
|
|
EFonts font;
|
2022-11-26 23:12:20 +02:00
|
|
|
ETextAlignment align;
|
2023-07-31 18:50:55 +02:00
|
|
|
ColorRGBA color;
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
2023-07-31 18:50:55 +02:00
|
|
|
CLabelGroup(EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE);
|
2022-05-07 11:56:34 +02:00
|
|
|
void add(int x = 0, int y = 0, const std::string & text = "");
|
2018-03-22 20:23:18 +02:00
|
|
|
size_t currentSize() const;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Multi-line label that can display multiple lines of text
|
|
|
|
/// If text is too big to fit into requested area remaining part will not be visible
|
|
|
|
class CMultiLineLabel : public CLabel
|
|
|
|
{
|
|
|
|
// text to blit, split into lines that are no longer than widget width
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
|
|
|
|
// area of text that actually will be printed, default is widget size
|
|
|
|
Rect visibleSize;
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void splitText(const std::string & Txt, bool redrawAfter);
|
2014-07-15 10:14:49 +03:00
|
|
|
Rect getTextLocation();
|
|
|
|
public:
|
|
|
|
// total size of text, x = longest line of text, y = total height of lines
|
|
|
|
Point textSize;
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "");
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void setText(const std::string & Txt) override;
|
2023-06-02 15:42:18 +02:00
|
|
|
void showAll(Canvas & to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2019-04-20 18:26:54 +02:00
|
|
|
void setVisibleSize(Rect visibleSize, bool redrawElement = true);
|
2014-07-15 10:14:49 +03:00
|
|
|
// scrolls text visible in widget. Positive value will move text up
|
2019-04-20 18:26:54 +02:00
|
|
|
void scrollTextTo(int distance, bool redrawAfterScroll = true);
|
2014-07-15 10:14:49 +03:00
|
|
|
void scrollTextBy(int distance);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// a multi-line label that tries to fit text with given available width and height;
|
|
|
|
/// if not possible, it creates a slider for scrolling text
|
|
|
|
class CTextBox : public CIntObject
|
|
|
|
{
|
|
|
|
int sliderStyle;
|
|
|
|
public:
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CMultiLineLabel> label;
|
|
|
|
std::shared_ptr<CSlider> slider;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
void resize(Point newSize);
|
2018-04-07 13:34:11 +02:00
|
|
|
void setText(const std::string & Txt);
|
2014-07-15 10:14:49 +03:00
|
|
|
void sliderMoved(int to);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Status bar which is shown at the bottom of the in-game screens
|
2022-11-18 17:54:10 +02:00
|
|
|
class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusBar>, public IStatusBar
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-12 18:23:41 +02:00
|
|
|
std::string hoverText;
|
|
|
|
std::string consoleText;
|
|
|
|
bool enteringText;
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const ColorRGBA & Color = Colors::WHITE);
|
2023-09-26 15:53:44 +02:00
|
|
|
CGStatusBar(int x, int y, const ImagePath & name, int maxw = -1);
|
|
|
|
CGStatusBar(int x, int y);
|
2022-12-19 22:04:50 +02:00
|
|
|
|
|
|
|
//make CLabel API private
|
|
|
|
using CLabel::getText;
|
|
|
|
using CLabel::setAutoRedraw;
|
|
|
|
using CLabel::setText;
|
|
|
|
using CLabel::setColor;
|
|
|
|
using CLabel::getWidth;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
protected:
|
|
|
|
Point getBorderSize() override;
|
|
|
|
|
2023-07-09 16:48:25 +02:00
|
|
|
void clickPressed(const Point & cursorPosition) override;
|
2022-11-20 11:53:46 +02:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
2023-05-05 22:36:33 +02:00
|
|
|
~CGStatusBar();
|
|
|
|
|
2018-08-26 18:09:56 +02:00
|
|
|
template<typename ...Args>
|
|
|
|
static std::shared_ptr<CGStatusBar> create(Args... args)
|
|
|
|
{
|
|
|
|
std::shared_ptr<CGStatusBar> ret{new CGStatusBar{args...}};
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void show(Canvas & to) override;
|
2023-05-05 22:36:33 +02:00
|
|
|
void activate() override;
|
2022-12-19 22:06:01 +02:00
|
|
|
void deactivate() override;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-12-12 18:23:41 +02:00
|
|
|
// IStatusBar interface
|
|
|
|
void write(const std::string & Text) override;
|
|
|
|
void clearIfMatching(const std::string & Text) override;
|
|
|
|
void clear() override;
|
|
|
|
void setEnteringMode(bool on) override;
|
|
|
|
void setEnteredText(const std::string & text) override;
|
2022-11-20 11:53:46 +02:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
class CFocusable;
|
|
|
|
|
|
|
|
class IFocusListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void focusGot() {};
|
|
|
|
virtual void focusLost() {};
|
|
|
|
virtual ~IFocusListener() = default;
|
|
|
|
};
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
/// UIElement which can get input focus
|
|
|
|
class CFocusable : public virtual CIntObject
|
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
private:
|
|
|
|
std::shared_ptr<IFocusListener> focusListener;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
|
|
|
bool focus; //only one focusable control can have focus at one moment
|
|
|
|
|
|
|
|
void giveFocus(); //captures focus
|
|
|
|
void moveFocus(); //moves focus to next active control (may be used for tab switching)
|
2023-10-14 12:08:38 +02:00
|
|
|
void removeFocus(); //remove focus
|
2022-05-07 11:56:34 +02:00
|
|
|
bool hasFocus() const;
|
|
|
|
|
|
|
|
static std::list<CFocusable *> focusables; //all existing objs
|
|
|
|
static CFocusable * inputWithFocus; //who has focus now
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
CFocusable();
|
2022-05-07 11:56:34 +02:00
|
|
|
CFocusable(std::shared_ptr<IFocusListener> focusListener);
|
2014-07-15 10:14:49 +03:00
|
|
|
~CFocusable();
|
|
|
|
};
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
class CTextInput;
|
|
|
|
class CKeyboardFocusListener : public IFocusListener
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static std::atomic<int> usageIndex;
|
|
|
|
CTextInput * textInput;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CKeyboardFocusListener(CTextInput * textInput);
|
|
|
|
void focusGot() override;
|
|
|
|
void focusLost() override;
|
|
|
|
};
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
/// Text input box where players can enter text
|
|
|
|
class CTextInput : public CLabel, public CFocusable
|
|
|
|
{
|
|
|
|
std::string newText;
|
2023-08-27 19:39:45 +02:00
|
|
|
std::string helpBox; //for right-click help
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
protected:
|
|
|
|
std::string visibleText() override;
|
2023-08-27 20:06:38 +02:00
|
|
|
|
|
|
|
public:
|
2023-08-26 12:01:19 +02:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
CFunctionList<void(const std::string &)> cb;
|
|
|
|
CFunctionList<void(std::string &, const std::string &)> filters;
|
2022-12-07 21:50:45 +02:00
|
|
|
void setText(const std::string & nText) override;
|
|
|
|
void setText(const std::string & nText, bool callCb);
|
2023-08-27 19:39:45 +02:00
|
|
|
void setHelpText(const std::string &);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-10-13 23:04:35 +02:00
|
|
|
CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput = true);
|
2023-08-23 14:07:50 +02:00
|
|
|
CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB);
|
2023-01-30 17:18:59 +02:00
|
|
|
CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void clickPressed(const Point & cursorPosition) override;
|
2023-04-27 19:21:06 +02:00
|
|
|
void keyPressed(EShortcut key) override;
|
2023-08-26 12:01:19 +02:00
|
|
|
void showPopupWindow(const Point & cursorPosition) override;
|
2023-02-02 18:02:25 +02:00
|
|
|
|
2023-07-04 17:42:52 +02:00
|
|
|
//bool captureThisKey(EShortcut key) override;
|
2018-04-07 13:34:11 +02:00
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
void textInputed(const std::string & enteredText) override;
|
|
|
|
void textEdited(const std::string & enteredText) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//Filter that will block all characters not allowed in filenames
|
2022-05-07 11:56:34 +02:00
|
|
|
static void filenameFilter(std::string & text, const std::string & oldText);
|
2014-07-15 10:14:49 +03:00
|
|
|
//Filter that will allow only input of numbers in range min-max (min-max are allowed)
|
2014-08-09 15:14:31 +03:00
|
|
|
//min-max should be set via something like std::bind
|
2022-05-07 11:56:34 +02:00
|
|
|
static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
|
|
|
|
|
|
|
|
friend class CKeyboardFocusListener;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|