diff options
author | Ekdohibs <nathanael.courant@laposte.net> | 2016-05-31 17:30:11 +0200 |
---|---|---|
committer | Ekdohibs <nathanael.courant@laposte.net> | 2016-05-31 17:34:29 +0200 |
commit | 14ef2b445adcec770defe1abf83af9d22ccf39d8 (patch) | |
tree | be434ea35d6134f4e7b90a74283a21815ed079ee /src/irrlicht_changes/static_text.h | |
parent | 1d40385d4aacf0cbea4b19ff06940e8c9bebaf47 (diff) | |
download | hax-minetest-server-14ef2b445adcec770defe1abf83af9d22ccf39d8.tar.gz hax-minetest-server-14ef2b445adcec770defe1abf83af9d22ccf39d8.zip |
Add colored text (not only colored chat).
Add documentation, move files to a proper place and avoid memory leaks.
Make it work with most kind of texts, and allow backgrounds too.
Diffstat (limited to '')
-rw-r--r-- | src/irrlicht_changes/static_text.h (renamed from src/util/statictext.h) | 122 |
1 files changed, 120 insertions, 2 deletions
diff --git a/src/util/statictext.h b/src/irrlicht_changes/static_text.h index 8d2f879e7..408a12784 100644 --- a/src/util/statictext.h +++ b/src/irrlicht_changes/static_text.h @@ -1,4 +1,6 @@ // Copyright (C) 2002-2012 Nikolaus Gebhardt +// Copyright (C) 2016 Nathanaƫl Courant +// Modified this class to work with EnrichedStrings too // This file is part of the "Irrlicht Engine". // For conditions of distribution and use, see copyright notice in irrlicht.h @@ -11,18 +13,30 @@ #include "IGUIStaticText.h" #include "irrArray.h" +#include "log.h" + #include <vector> +#include "util/enriched_string.h" +#include "config.h" +#include <IGUIEnvironment.h> + +#if USE_FREETYPE + namespace irr { + namespace gui { + + const EGUI_ELEMENT_TYPE EGUIET_ENRICHED_STATIC_TEXT = (EGUI_ELEMENT_TYPE)(0x1000); + class StaticText : public IGUIStaticText { public: //! constructor - StaticText(const wchar_t* text, bool border, IGUIEnvironment* environment, + StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::rect<s32>& rectangle, bool background = false); @@ -121,6 +135,16 @@ namespace gui //! Reads attributes of the element virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options); + virtual bool hasType(EGUI_ELEMENT_TYPE t) const { + return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT); + }; + + virtual bool hasType(EGUI_ELEMENT_TYPE t) { + return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT); + }; + + void setText(const EnrichedString &text); + private: //! Breaks the single text line. @@ -139,12 +163,106 @@ namespace gui gui::IGUIFont* OverrideFont; gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated. - core::array< core::stringw > BrokenText; + EnrichedString cText; + core::array< EnrichedString > BrokenText; }; + } // end namespace gui + } // end namespace irr +inline irr::gui::IGUIStaticText *addStaticText( + irr::gui::IGUIEnvironment *guienv, + const EnrichedString &text, + const core::rect< s32 > &rectangle, + bool border = false, + bool wordWrap = true, + irr::gui::IGUIElement *parent = NULL, + s32 id = -1, + bool fillBackground = false) +{ + if (parent == NULL) { + // parent is NULL, so we must find one, or we need not to drop + // result, but then there will be a memory leak. + // + // What Irrlicht does is to use guienv as a parent, but the problem + // is that guienv is here only an IGUIEnvironment, while it is a + // CGUIEnvironment in Irrlicht, which inherits from both IGUIElement + // and IGUIEnvironment. + // + // A solution would be to dynamic_cast guienv to a + // IGUIElement*, but Irrlicht is shipped without rtti support + // in some distributions, causing the dymanic_cast to segfault. + // + // Thus, to find the parent, we create a dummy StaticText and ask + // for its parent, and then remove it. + irr::gui::IGUIStaticText *dummy_text = + guienv->addStaticText(L"", rectangle, border, wordWrap, + parent, id, fillBackground); + parent = dummy_text->getParent(); + dummy_text->remove(); + } + irr::gui::IGUIStaticText *result = new irr::gui::StaticText( + text, border, guienv, parent, + id, rectangle, fillBackground); + + result->setWordWrap(wordWrap); + result->drop(); + return result; +} + +inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text) +{ + // dynamic_cast not possible due to some distributions shipped + // without rtti support in irrlicht + if (static_text->hasType(irr::gui::EGUIET_ENRICHED_STATIC_TEXT)) { + irr::gui::StaticText* stext = static_cast<irr::gui::StaticText*>(static_text); + stext->setText(text); + } else { + static_text->setText(text.c_str()); + } +} + +#else // USE_FREETYPE + +inline irr::gui::IGUIStaticText *addStaticText( + irr::gui::IGUIEnvironment *guienv, + const EnrichedString &text, + const core::rect< s32 > &rectangle, + bool border = false, + bool wordWrap = true, + irr::gui::IGUIElement *parent = NULL, + s32 id = -1, + bool fillBackground = false) +{ + return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground); +} + +inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text) +{ + static_text->setText(text.c_str()); +} + +#endif + +inline irr::gui::IGUIStaticText *addStaticText( + irr::gui::IGUIEnvironment *guienv, + const wchar_t *text, + const core::rect< s32 > &rectangle, + bool border = false, + bool wordWrap = true, + irr::gui::IGUIElement *parent = NULL, + s32 id = -1, + bool fillBackground = false) { + return addStaticText(guienv, EnrichedString(text), rectangle, border, wordWrap, parent, id, fillBackground); +} + +inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text) +{ + setStaticText(static_text, EnrichedString(text)); +} + #endif // _IRR_COMPILE_WITH_GUI_ #endif // C_GUI_STATIC_TEXT_H_INCLUDED |