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-02-01 20:42:06 +02:00
|
|
|
#include "../render/Graphics.h"
|
2017-07-13 10:26:03 +02:00
|
|
|
#include "../../lib/FunctionList.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-01-30 19:55:32 +02:00
|
|
|
#include <SDL_pixels.h>
|
|
|
|
|
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
|
|
|
|
void blitLine(SDL_Surface * to, Rect where, std::string what);
|
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
CTextContainer(ETextAlignment alignment, EFonts font, SDL_Color 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;
|
|
|
|
SDL_Color color; // default font color. Can be overridden by placing "{}" into the string
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Label which shows text
|
|
|
|
class CLabel : public CTextContainer
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Point getBorderSize() override;
|
|
|
|
virtual std::string visibleText();
|
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
std::shared_ptr<CIntObject> background;
|
2014-07-15 10:14:49 +03:00
|
|
|
std::string text;
|
|
|
|
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);
|
2018-03-20 10:19:33 +02:00
|
|
|
virtual void setColor(const SDL_Color & Color);
|
|
|
|
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,
|
2022-05-07 11:56:34 +02:00
|
|
|
const SDL_Color & Color = Colors::WHITE, const std::string & Text = "");
|
2015-10-12 15:47:10 +02:00
|
|
|
void showAll(SDL_Surface * 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;
|
2014-07-15 10:14:49 +03:00
|
|
|
SDL_Color color;
|
|
|
|
public:
|
2022-11-26 23:12:20 +02:00
|
|
|
CLabelGroup(EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const SDL_Color & 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;
|
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const SDL_Color & 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;
|
2015-10-12 15:47:10 +02:00
|
|
|
void showAll(SDL_Surface * 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
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const SDL_Color & 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;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void init();
|
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const SDL_Color & Color = Colors::WHITE);
|
2018-08-26 18:09:56 +02:00
|
|
|
CGStatusBar(int x, int y, std::string name, int maxw = -1);
|
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;
|
|
|
|
|
2022-11-20 11:53:46 +02:00
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
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...}};
|
|
|
|
ret->init();
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-12-12 18:23:41 +02:00
|
|
|
void show(SDL_Surface * to) 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)
|
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;
|
|
|
|
protected:
|
|
|
|
std::string visibleText() override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
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);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB);
|
|
|
|
CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & 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
|
|
|
|
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
2023-04-27 19:21:06 +02:00
|
|
|
void keyPressed(EShortcut key) override;
|
2023-02-02 18:02:25 +02:00
|
|
|
|
2023-04-27 19:21:06 +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
|
|
|
};
|