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"
|
|
|
|
#include "../gui/SDL_Extensions.h"
|
|
|
|
#include "../../lib/FunctionList.h"
|
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);
|
|
|
|
|
|
|
|
CTextContainer(EAlignment alignment, EFonts font, SDL_Color color);
|
|
|
|
|
|
|
|
public:
|
|
|
|
EAlignment alignment;
|
|
|
|
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();
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CPicture> background;
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
std::string text;
|
|
|
|
bool autoRedraw; //whether control will redraw itself on setTxt
|
|
|
|
|
|
|
|
std::string getText();
|
2018-03-20 10:19:33 +02:00
|
|
|
virtual void setAutoRedraw(bool option);
|
2014-07-15 10:14:49 +03: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
|
|
|
|
|
|
|
CLabel(int x=0, int y=0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT,
|
|
|
|
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;
|
|
|
|
EAlignment align;
|
|
|
|
SDL_Color color;
|
|
|
|
public:
|
|
|
|
CLabelGroup(EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE);
|
|
|
|
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;
|
|
|
|
|
2019-04-20 18:26:54 +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;
|
|
|
|
|
|
|
|
CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color &Color = Colors::WHITE, const std::string &Text = "");
|
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void setText(const std::string &Txt) override;
|
|
|
|
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
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, EAlignment Align = 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
|
2018-08-26 18:09:56 +02:00
|
|
|
class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusBar>
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
bool textLock; //Used for blocking changes to the text
|
|
|
|
void init();
|
|
|
|
|
2018-08-26 18:09:56 +02:00
|
|
|
CGStatusBar(std::shared_ptr<CPicture> background_, EFonts Font = FONT_SMALL, EAlignment Align = CENTER, const SDL_Color & Color = Colors::WHITE);
|
|
|
|
CGStatusBar(int x, int y, std::string name, int maxw = -1);
|
2014-07-15 10:14:49 +03:00
|
|
|
protected:
|
|
|
|
Point getBorderSize() override;
|
|
|
|
|
|
|
|
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
|
|
|
void clear();//clears statusbar and refreshes
|
|
|
|
void setText(const std::string & Text) override; //prints text and refreshes statusbar
|
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void show(SDL_Surface * to) override; //shows statusbar (with current text)
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
void lock(bool shouldLock); //If true, current text cannot be changed until lock(false) is called
|
|
|
|
};
|
|
|
|
|
|
|
|
/// UIElement which can get input focus
|
|
|
|
class CFocusable : public virtual CIntObject
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual void focusGot(){};
|
|
|
|
virtual void focusLost(){};
|
|
|
|
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)
|
|
|
|
|
|
|
|
static std::list<CFocusable*> focusables; //all existing objs
|
|
|
|
static CFocusable *inputWithFocus; //who has focus now
|
|
|
|
CFocusable();
|
|
|
|
~CFocusable();
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Text input box where players can enter text
|
|
|
|
class CTextInput : public CLabel, public CFocusable
|
|
|
|
{
|
|
|
|
std::string newText;
|
|
|
|
protected:
|
|
|
|
std::string visibleText() override;
|
|
|
|
|
|
|
|
void focusGot() override;
|
|
|
|
void focusLost() override;
|
2017-07-23 19:23:48 +02:00
|
|
|
|
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
void notifyAndroidTextInputChanged(std::string & text);
|
|
|
|
#endif
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
|
|
|
CFunctionList<void(const std::string &)> cb;
|
|
|
|
CFunctionList<void(std::string &, const std::string &)> filters;
|
|
|
|
void setText(const std::string &nText, bool callCb = false);
|
|
|
|
|
|
|
|
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);
|
|
|
|
CTextInput(const Rect &Pos, SDL_Surface *srf = nullptr);
|
|
|
|
|
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
|
|
|
void keyPressed(const SDL_KeyboardEvent & key) override;
|
|
|
|
bool captureThisEvent(const SDL_KeyboardEvent & key) override;
|
2018-04-07 13:34:11 +02:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void textInputed(const SDL_TextInputEvent & event) override;
|
|
|
|
void textEdited(const SDL_TextEditingEvent & event) override;
|
|
|
|
|
|
|
|
//Filter that will block all characters not allowed in filenames
|
|
|
|
static void filenameFilter(std::string &text, const std::string & oldText);
|
|
|
|
//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
|
2014-07-15 10:14:49 +03:00
|
|
|
static void numberFilter(std::string &text, const std::string & oldText, int minValue, int maxValue);
|
|
|
|
};
|