1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 21:32:42 +00:00
OpenMW/apps/openmw/mwgui/formatting.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

183 lines
5.2 KiB
C++
Raw Normal View History

#ifndef MWGUI_FORMATTING_H
#define MWGUI_FORMATTING_H
2015-01-10 02:50:43 +01:00
#include <MyGUI_Colour.h>
#include <map>
2018-06-18 13:43:39 +04:00
#include <components/widgets/box.hpp>
namespace MWGui
{
2014-09-20 00:11:04 +02:00
namespace Formatting
2012-05-09 01:34:32 +02:00
{
2014-09-20 00:11:04 +02:00
struct TextStyle
2012-05-09 01:34:32 +02:00
{
2014-09-20 00:11:04 +02:00
TextStyle()
: mColour(0, 0, 0)
2022-07-07 23:20:22 +04:00
, mFont("Journalbook DefaultFont")
2014-09-20 00:11:04 +02:00
, mTextSize(16)
{
}
2012-05-09 01:34:32 +02:00
MyGUI::Colour mColour;
2014-09-20 00:11:04 +02:00
std::string mFont;
int mTextSize;
2014-10-03 21:36:45 +02:00
};
struct BlockStyle
{
BlockStyle()
: mAlign(MyGUI::Align::Left | MyGUI::Align::Top)
{
}
MyGUI::Align mAlign;
2014-09-20 00:11:04 +02:00
};
2012-05-09 01:34:32 +02:00
class BookTextParser
{
public:
typedef std::map<std::string, std::string> Attributes;
enum Events
{
Event_None = -2,
Event_EOF = -1,
Event_BrTag,
Event_PTag,
Event_ImgTag,
Event_DivTag,
2023-10-03 02:04:18 +02:00
Event_FontTag,
Event_PageBreak,
};
2022-09-22 21:26:05 +03:00
2023-10-03 02:04:18 +02:00
BookTextParser(const std::string& text, bool shrinkTextAtLastTag);
2022-09-22 21:26:05 +03:00
Events next();
2022-09-22 21:26:05 +03:00
const Attributes& getAttributes() const;
std::string getReadyText() const;
bool isClosingTag() const;
2022-09-22 21:26:05 +03:00
private:
void registerTag(const std::string& tag, Events type);
void flushBuffer();
void parseTag(std::string tag);
2022-09-22 21:26:05 +03:00
size_t mIndex;
std::string mText;
std::string mReadyText;
2022-09-22 21:26:05 +03:00
bool mIgnoreNewlineTags;
bool mIgnoreLineEndings;
Attributes mAttributes;
std::string mTag;
bool mClosingTag;
std::map<std::string, Events> mTagTypes;
std::string mBuffer;
2022-09-22 21:26:05 +03:00
size_t mPlainTextEnd;
};
2014-09-20 00:11:04 +02:00
class Paginator
{
public:
typedef std::pair<int, int> Page;
typedef std::vector<Page> Pages;
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
Paginator(int pageWidth, int pageHeight)
: mStartTop(0)
, mCurrentTop(0)
, mPageWidth(pageWidth)
, mPageHeight(pageHeight)
, mIgnoreLeadingEmptyLines(false)
2014-09-20 00:11:04 +02:00
{
}
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
int getStartTop() const { return mStartTop; }
int getCurrentTop() const { return mCurrentTop; }
int getPageWidth() const { return mPageWidth; }
int getPageHeight() const { return mPageHeight; }
bool getIgnoreLeadingEmptyLines() const { return mIgnoreLeadingEmptyLines; }
2014-09-20 00:11:04 +02:00
Pages getPages() const { return mPages; }
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
void setStartTop(int top) { mStartTop = top; }
void setCurrentTop(int top) { mCurrentTop = top; }
void setIgnoreLeadingEmptyLines(bool ignore) { mIgnoreLeadingEmptyLines = ignore; }
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
Paginator& operator<<(const Page& page)
{
mPages.push_back(page);
return *this;
}
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
private:
int mStartTop, mCurrentTop;
int mPageWidth, mPageHeight;
bool mIgnoreLeadingEmptyLines;
2014-09-20 00:11:04 +02:00
Pages mPages;
};
/// \brief utilities for parsing book/scroll text as mygui widgets
class BookFormatter
{
public:
2023-10-03 02:04:18 +02:00
Paginator::Pages markupToWidget(MyGUI::Widget* parent, const std::string& markup, const int pageWidth,
const int pageHeight, bool shrinkTextAtLastTag);
Paginator::Pages markupToWidget(MyGUI::Widget* parent, const std::string& markup, bool shrinkTextAtLastTag);
2014-09-20 00:11:04 +02:00
private:
void resetFontProperties();
void handleDiv(const BookTextParser::Attributes& attr);
void handleFont(const BookTextParser::Attributes& attr);
2014-09-20 00:11:04 +02:00
TextStyle mTextStyle;
2014-10-03 21:36:45 +02:00
BlockStyle mBlockStyle;
2014-09-20 00:11:04 +02:00
};
class GraphicElement
{
public:
2014-10-03 21:36:45 +02:00
GraphicElement(MyGUI::Widget* parent, Paginator& pag, const BlockStyle& blockStyle);
2014-09-20 00:11:04 +02:00
virtual int getHeight() = 0;
virtual void paginate();
virtual int pageSplit();
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
protected:
2014-10-25 10:37:57 +11:00
virtual ~GraphicElement() {}
2014-09-20 00:11:04 +02:00
MyGUI::Widget* mParent;
Paginator& mPaginator;
2014-10-03 21:36:45 +02:00
BlockStyle mBlockStyle;
2014-09-20 00:11:04 +02:00
};
class TextElement : public GraphicElement
{
public:
2014-10-03 21:36:45 +02:00
TextElement(MyGUI::Widget* parent, Paginator& pag, const BlockStyle& blockStyle, const TextStyle& textStyle,
const std::string& text);
int getHeight() override;
int pageSplit() override;
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
private:
int currentFontHeight() const;
2014-10-03 21:36:45 +02:00
TextStyle mTextStyle;
2023-10-31 10:18:09 +04:00
Gui::EditBox* mEditBox;
2014-09-20 00:11:04 +02:00
};
class ImageElement : public GraphicElement
{
public:
2014-10-03 21:36:45 +02:00
ImageElement(MyGUI::Widget* parent, Paginator& pag, const BlockStyle& blockStyle, const std::string& src,
int width, int height);
int getHeight() override;
int pageSplit() override;
2022-09-22 21:26:05 +03:00
2014-09-20 00:11:04 +02:00
private:
int mImageHeight;
MyGUI::ImageBox* mImageBox;
};
}
}
#endif