]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/gui/guiButton.h
Merge branch 'master' into master
[dragonfireclient.git] / src / gui / guiButton.h
index 3c353d240459b391edd49ad4bacf97d83862af0e..cd0b4ec5f01e76564053d23592df15858bfdbc71 100644 (file)
@@ -6,89 +6,98 @@
 \r
 #include "IrrCompileConfig.h"\r
 \r
+#include <IGUIStaticText.h>\r
+#include "irrlicht_changes/static_text.h"\r
 #include "IGUIButton.h"\r
 #include "IGUISpriteBank.h"\r
 #include "ITexture.h"\r
 #include "SColor.h"\r
 #include "guiSkin.h"\r
+#include "StyleSpec.h"\r
 \r
 using namespace irr;\r
 \r
 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)\r
-       namespace irr { namespace gui {\r
-\r
-               //! State of buttons used for drawing texture images.\r
-               //! Note that only a single state is active at a time\r
-               //! Also when no image is defined for a state it will use images from another state\r
-               //! and if that state is not set from the replacement for that,etc.\r
-               //! So in many cases setting EGBIS_IMAGE_UP and EGBIS_IMAGE_DOWN is sufficient.\r
-               enum EGUI_BUTTON_IMAGE_STATE {\r
-                       //! When no other states have images they will all use this one.\r
-                                       EGBIS_IMAGE_UP,\r
-                       //! When not set EGBIS_IMAGE_UP is used.\r
-                                       EGBIS_IMAGE_UP_MOUSEOVER,\r
-                       //! When not set EGBIS_IMAGE_UP_MOUSEOVER is used.\r
-                                       EGBIS_IMAGE_UP_FOCUSED,\r
-                       //! When not set EGBIS_IMAGE_UP_FOCUSED is used.\r
-                                       EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER,\r
-                       //! When not set EGBIS_IMAGE_UP is used.\r
-                                       EGBIS_IMAGE_DOWN,\r
-                       //! When not set EGBIS_IMAGE_DOWN is used.\r
-                                       EGBIS_IMAGE_DOWN_MOUSEOVER,\r
-                       //! When not set EGBIS_IMAGE_DOWN_MOUSEOVER is used.\r
-                                       EGBIS_IMAGE_DOWN_FOCUSED,\r
-                       //! When not set EGBIS_IMAGE_DOWN_FOCUSED is used.\r
-                                       EGBIS_IMAGE_DOWN_FOCUSED_MOUSEOVER,\r
-                       //! When not set EGBIS_IMAGE_UP or EGBIS_IMAGE_DOWN are used (depending on button state).\r
-                                       EGBIS_IMAGE_DISABLED,\r
-                       //! not used, counts the number of enumerated items\r
-                                       EGBIS_COUNT\r
-               };\r
-\r
-               //! Names for gui button image states\r
-               const c8 *const GUIButtonImageStateNames[EGBIS_COUNT + 1] =\r
-                               {\r
-                                               "Image",    // not "ImageUp" as it otherwise breaks serialization of old files\r
-                                               "ImageUpOver",\r
-                                               "ImageUpFocused",\r
-                                               "ImageUpFocusedOver",\r
-                                               "PressedImage",    // not "ImageDown" as it otherwise breaks serialization of old files\r
-                                               "ImageDownOver",\r
-                                               "ImageDownFocused",\r
-                                               "ImageDownFocusedOver",\r
-                                               "ImageDisabled",\r
-                                               0    // count\r
-                               };\r
-\r
-       }}\r
+namespace irr\r
+{\r
+namespace gui\r
+{\r
+\r
+//! State of buttons used for drawing texture images.\r
+//! Note that only a single state is active at a time\r
+//! Also when no image is defined for a state it will use images from another state\r
+//! and if that state is not set from the replacement for that,etc.\r
+//! So in many cases setting EGBIS_IMAGE_UP and EGBIS_IMAGE_DOWN is sufficient.\r
+enum EGUI_BUTTON_IMAGE_STATE\r
+{\r
+       //! When no other states have images they will all use this one.\r
+       EGBIS_IMAGE_UP,\r
+       //! When not set EGBIS_IMAGE_UP is used.\r
+       EGBIS_IMAGE_UP_MOUSEOVER,\r
+       //! When not set EGBIS_IMAGE_UP_MOUSEOVER is used.\r
+       EGBIS_IMAGE_UP_FOCUSED,\r
+       //! When not set EGBIS_IMAGE_UP_FOCUSED is used.\r
+       EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER,\r
+       //! When not set EGBIS_IMAGE_UP is used.\r
+       EGBIS_IMAGE_DOWN,\r
+       //! When not set EGBIS_IMAGE_DOWN is used.\r
+       EGBIS_IMAGE_DOWN_MOUSEOVER,\r
+       //! When not set EGBIS_IMAGE_DOWN_MOUSEOVER is used.\r
+       EGBIS_IMAGE_DOWN_FOCUSED,\r
+       //! When not set EGBIS_IMAGE_DOWN_FOCUSED is used.\r
+       EGBIS_IMAGE_DOWN_FOCUSED_MOUSEOVER,\r
+       //! When not set EGBIS_IMAGE_UP or EGBIS_IMAGE_DOWN are used (depending on button\r
+       //! state).\r
+       EGBIS_IMAGE_DISABLED,\r
+       //! not used, counts the number of enumerated items\r
+       EGBIS_COUNT\r
+};\r
+\r
+//! Names for gui button image states\r
+const c8 *const GUIButtonImageStateNames[EGBIS_COUNT + 1] = {\r
+               "Image", // not "ImageUp" as it otherwise breaks serialization of old\r
+                        // files\r
+               "ImageUpOver", "ImageUpFocused", "ImageUpFocusedOver",\r
+               "PressedImage", // not "ImageDown" as it otherwise breaks serialization of\r
+                               // old files\r
+               "ImageDownOver", "ImageDownFocused", "ImageDownFocusedOver",\r
+               "ImageDisabled",\r
+               0 // count\r
+};\r
+\r
+}\r
+}\r
 \r
 #endif\r
 \r
+class ISimpleTextureSource;\r
+\r
 class GUIButton : public gui::IGUIButton\r
 {\r
 public:\r
-\r
        //! constructor\r
-       GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,\r
-                          s32 id, core::rect<s32> rectangle, bool noclip=false);\r
+       GUIButton(gui::IGUIEnvironment *environment, gui::IGUIElement *parent, s32 id,\r
+                       core::rect<s32> rectangle, ISimpleTextureSource *tsrc,\r
+                       bool noclip = false);\r
 \r
        //! destructor\r
        virtual ~GUIButton();\r
 \r
        //! called if an event happened.\r
-       virtual bool OnEvent(const SEventevent) override;\r
+       virtual bool OnEvent(const SEvent &event) override;\r
 \r
        //! draws the element and its children\r
        virtual void draw() override;\r
 \r
-       //! sets another skin independent font. if this is set to zero, the button uses the font of the skin.\r
-       virtual void setOverrideFont(gui::IGUIFont* font=0) override;\r
+       //! sets another skin independent font. if this is set to zero, the button uses\r
+       //! the font of the skin.\r
+       virtual void setOverrideFont(gui::IGUIFont *font = 0) override;\r
 \r
        //! Gets the override font (if any)\r
-       virtual gui::IGUIFontgetOverrideFont() const override;\r
+       virtual gui::IGUIFont *getOverrideFont() const override;\r
 \r
        //! Get the font which is used right now for drawing\r
-       virtual gui::IGUIFontgetActiveFont() const override;\r
+       virtual gui::IGUIFont *getActiveFont() const override;\r
 \r
        //! Sets another color for the button text.\r
        virtual void setOverrideColor(video::SColor color);\r
@@ -96,52 +105,58 @@ class GUIButton : public gui::IGUIButton
        //! Gets the override color\r
        virtual video::SColor getOverrideColor(void) const;\r
 \r
-       //! Sets if the button text should use the override color or the color in the gui skin.\r
+       //! Sets if the button text should use the override color or the color in the gui\r
+       //! skin.\r
        virtual void enableOverrideColor(bool enable);\r
 \r
        //! Checks if an override color is enabled\r
        virtual bool isOverrideColorEnabled(void) const;\r
 \r
        // PATCH\r
-       //! Sets an image which should be displayed on the button when it is in the given state.\r
+       //! Sets an image which should be displayed on the button when it is in the given\r
+       //! state.\r
        virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,\r
-                       video::ITexture* image=nullptr,\r
-                       const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0));\r
+                       video::ITexture *image = nullptr,\r
+                       const core::rect<s32> &sourceRect = core::rect<s32>(0, 0, 0, 0));\r
 \r
-       //! Sets an image which should be displayed on the button when it is in normal state.\r
-       virtual void setImage(video::ITexture* image=nullptr) override;\r
+       //! Sets an image which should be displayed on the button when it is in normal\r
+       //! state.\r
+       virtual void setImage(video::ITexture *image = nullptr) override;\r
 \r
-       //! Sets an image which should be displayed on the button when it is in normal state.\r
-       virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
+       //! Sets an image which should be displayed on the button when it is in normal\r
+       //! state.\r
+       virtual void setImage(\r
+                       video::ITexture *image, const core::rect<s32> &pos) override;\r
 \r
-       //! Sets an image which should be displayed on the button when it is in pressed state.\r
-       virtual void setPressedImage(video::ITexture* image=nullptr) override;\r
+       //! Sets an image which should be displayed on the button when it is in pressed\r
+       //! state.\r
+       virtual void setPressedImage(video::ITexture *image = nullptr) override;\r
 \r
-       //! Sets an image which should be displayed on the button when it is in pressed state.\r
-       virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;\r
+       //! Sets an image which should be displayed on the button when it is in pressed\r
+       //! state.\r
+       virtual void setPressedImage(\r
+                       video::ITexture *image, const core::rect<s32> &pos) override;\r
 \r
-       //! Sets an image which should be displayed on the button when it is in hovered state.\r
-       virtual void setHoveredImage(video::ITexture* image=nullptr);\r
+       //! Sets the text displayed by the button\r
+       virtual void setText(const wchar_t *text) override;\r
        // END PATCH\r
 \r
-       //! Sets an image which should be displayed on the button when it is in hovered state.\r
-       virtual void setHoveredImage(video::ITexture* image, const core::rect<s32>& pos);\r
-\r
        //! Sets the sprite bank used by the button\r
-       virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;\r
+       virtual void setSpriteBank(gui::IGUISpriteBank *bank = 0) override;\r
 \r
        //! Sets the animated sprite for a specific button state\r
-       /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite\r
-       \param state: State of the button to set the sprite for\r
-       \param index: The sprite number from the current sprite bank\r
-       \param color: The color of the sprite\r
+       /** \param index: Number of the sprite within the sprite bank, use -1 for no\r
+       sprite \param state: State of the button to set the sprite for \param index: The\r
+       sprite number from the current sprite bank \param color: The color of the sprite\r
        */\r
        virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,\r
-                                                  video::SColor color=video::SColor(255,255,255,255),\r
-                                                  bool loop=false, bool scale=false);\r
+                       video::SColor color = video::SColor(255, 255, 255, 255),\r
+                       bool loop = false, bool scale = false);\r
 \r
 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)\r
-       void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop) override {\r
+       void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color,\r
+                       bool loop) override\r
+       {\r
                setSprite(state, index, color, loop, false);\r
        }\r
 #endif\r
@@ -149,7 +164,8 @@ class GUIButton : public gui::IGUIButton
        //! Get the sprite-index for the given state or -1 when no sprite is set\r
        virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const;\r
 \r
-       //! Get the sprite color for the given state. Color is only used when a sprite is set.\r
+       //! Get the sprite color for the given state. Color is only used when a sprite is\r
+       //! set.\r
        virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const;\r
 \r
        //! Returns if the sprite in the given state does loop\r
@@ -161,136 +177,147 @@ class GUIButton : public gui::IGUIButton
        //! Sets if the button should behave like a push button. Which means it\r
        //! can be in two states: Normal or Pressed. With a click on the button,\r
        //! the user can change the state of the button.\r
-       virtual void setIsPushButton(bool isPushButton=true) override;\r
+       virtual void setIsPushButton(bool isPushButton = true) override;\r
 \r
        //! Checks whether the button is a push button\r
        virtual bool isPushButton() const override;\r
 \r
        //! Sets the pressed state of the button if this is a pushbutton\r
-       virtual void setPressed(bool pressed=true) override;\r
+       virtual void setPressed(bool pressed = true) override;\r
 \r
        //! Returns if the button is currently pressed\r
        virtual bool isPressed() const override;\r
 \r
+       // PATCH\r
+       //! Returns if this element (or one of its direct children) is hovered\r
+       bool isHovered() const;\r
+       // END PATCH\r
+\r
        //! Sets if the button should use the skin to draw its border\r
-       virtual void setDrawBorder(bool border=true) override;\r
+       virtual void setDrawBorder(bool border = true) override;\r
 \r
        //! Checks if the button face and border are being drawn\r
        virtual bool isDrawingBorder() const override;\r
 \r
-       //! Sets if the alpha channel should be used for drawing images on the button (default is false)\r
-       virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;\r
+       //! Sets if the alpha channel should be used for drawing images on the button\r
+       //! (default is false)\r
+       virtual void setUseAlphaChannel(bool useAlphaChannel = true) override;\r
 \r
        //! Checks if the alpha channel should be used for drawing images on the button\r
        virtual bool isAlphaChannelUsed() const override;\r
 \r
        //! Sets if the button should scale the button images to fit\r
-       virtual void setScaleImage(bool scaleImage=true) override;\r
+       virtual void setScaleImage(bool scaleImage = true) override;\r
 \r
        //! Checks whether the button scales the used images\r
        virtual bool isScalingImage() const override;\r
 \r
        //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event\r
-       virtual bool getClickShiftState() const\r
-       {\r
-               return ClickShiftState;\r
-       }\r
+       virtual bool getClickShiftState() const { return ClickShiftState; }\r
 \r
        //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event\r
-       virtual bool getClickControlState() const\r
-       {\r
-               return ClickControlState;\r
-       }\r
+       virtual bool getClickControlState() const { return ClickControlState; }\r
 \r
        //! Writes attributes of the element.\r
-       virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const override;\r
+       virtual void serializeAttributes(io::IAttributes *out,\r
+                       io::SAttributeReadWriteOptions *options) const override;\r
 \r
        //! Reads attributes of the element\r
-       virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) override;\r
-\r
-\r
+       virtual void deserializeAttributes(io::IAttributes *in,\r
+                       io::SAttributeReadWriteOptions *options) override;\r
 \r
        void setColor(video::SColor color);\r
        // PATCH\r
-       void setHoveredColor(video::SColor color);\r
-       void setPressedColor(video::SColor color);\r
-       // END PATCH\r
+       //! Set element properties from a StyleSpec corresponding to the button state\r
+       void setFromState();\r
 \r
+       //! Set element properties from a StyleSpec\r
+       virtual void setFromStyle(const StyleSpec &style);\r
+\r
+       //! Set the styles used for each state\r
+       void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES> &styles);\r
+       // END PATCH\r
 \r
        //! Do not drop returned handle\r
-       static GUIButton* addButton(gui::IGUIEnvironment *environment, const core::rect<s32>& rectangle,\r
-                                                                       IGUIElement* parent, s32 id, const wchar_t* text, const wchar_t *tooltiptext=L"");\r
+       static GUIButton *addButton(gui::IGUIEnvironment *environment,\r
+                       const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,\r
+                       IGUIElement *parent, s32 id, const wchar_t *text,\r
+                       const wchar_t *tooltiptext = L"");\r
 \r
 protected:\r
-       void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);\r
+       void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime,\r
+                       const core::position2di &center);\r
        gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;\r
 \r
-private:\r
-\r
-       struct ButtonSprite\r
-       {\r
-               ButtonSprite() : Index(-1), Loop(false), Scale(false)\r
-               {\r
-               }\r
-\r
-               bool operator==(const ButtonSprite& other) const\r
-               {\r
-                       return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;\r
-               }\r
-\r
-               s32 Index;\r
-               video::SColor Color;\r
-               bool Loop;\r
-               bool Scale;\r
-       };\r
-\r
-       ButtonSprite ButtonSprites[gui::EGBS_COUNT];\r
-       gui::IGUISpriteBank* SpriteBank;\r
+       ISimpleTextureSource *getTextureSource() { return TSrc; }\r
 \r
        struct ButtonImage\r
        {\r
-               ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
-               {\r
-               }\r
+               ButtonImage() : Texture(0), SourceRect(core::rect<s32>(0, 0, 0, 0)) {}\r
 \r
-               ButtonImage(const ButtonImage& other) : Texture(0), SourceRect(core::rect<s32>(0,0,0,0))\r
+               ButtonImage(const ButtonImage &other) :\r
+                               Texture(0), SourceRect(core::rect<s32>(0, 0, 0, 0))\r
                {\r
                        *this = other;\r
                }\r
 \r
                ~ButtonImage()\r
                {\r
-                       if ( Texture )\r
+                       if (Texture)\r
                                Texture->drop();\r
                }\r
 \r
-               ButtonImage& operator=(const ButtonImage& other)\r
+               ButtonImage &operator=(const ButtonImage &other)\r
                {\r
-                       if ( this == &other )\r
+                       if (this == &other)\r
                                return *this;\r
 \r
                        if (other.Texture)\r
                                other.Texture->grab();\r
-                       if ( Texture )\r
+                       if (Texture)\r
                                Texture->drop();\r
                        Texture = other.Texture;\r
                        SourceRect = other.SourceRect;\r
                        return *this;\r
                }\r
 \r
-               bool operator==(const ButtonImageother) const\r
+               bool operator==(const ButtonImage &other) const\r
                {\r
                        return Texture == other.Texture && SourceRect == other.SourceRect;\r
                }\r
 \r
-\r
-               video::ITexture* Texture;\r
+               video::ITexture *Texture;\r
                core::rect<s32> SourceRect;\r
        };\r
 \r
+       gui::EGUI_BUTTON_IMAGE_STATE getImageState(\r
+                       bool pressed, const ButtonImage *images) const;\r
+\r
+private:\r
+       struct ButtonSprite\r
+       {\r
+               ButtonSprite() : Index(-1), Loop(false), Scale(false) {}\r
+\r
+               bool operator==(const ButtonSprite &other) const\r
+               {\r
+                       return Index == other.Index && Color == other.Color &&\r
+                              Loop == other.Loop && Scale == other.Scale;\r
+               }\r
+\r
+               s32 Index;\r
+               video::SColor Color;\r
+               bool Loop;\r
+               bool Scale;\r
+       };\r
+\r
+       ButtonSprite ButtonSprites[gui::EGBS_COUNT];\r
+       gui::IGUISpriteBank *SpriteBank;\r
+\r
        ButtonImage ButtonImages[gui::EGBIS_COUNT];\r
 \r
-       gui::IGUIFont* OverrideFont;\r
+       std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;\r
+\r
+       gui::IGUIFont *OverrideFont;\r
 \r
        bool OverrideColorEnabled;\r
        video::SColor OverrideColor;\r
@@ -308,7 +335,13 @@ class GUIButton : public gui::IGUIButton
 \r
        video::SColor Colors[4];\r
        // PATCH\r
-       video::SColor HoveredColors[4];\r
-       video::SColor PressedColors[4];\r
+       bool WasHovered = false;\r
+       ISimpleTextureSource *TSrc;\r
+\r
+       gui::IGUIStaticText *StaticText;\r
+\r
+       core::rect<s32> BgMiddle;\r
+       core::rect<s32> Padding;\r
+       core::vector2d<s32> ContentOffset;\r
        // END PATCH\r
 };\r