kick
|
00001 // 00002 // Created by morten on 19/07/14. 00003 // 00004 00005 00006 #pragma once 00007 #include "kick/scene/updatable.h" 00008 #include "kick/scene/component.h" 00009 #include "kick/2d/sprite_mouse_listener.h" 00010 #include "kick/2d/sprite.h" 00011 #include "label.h" 00012 #include <string> 00013 #include <functional> 00014 #include <set> 00015 00016 namespace kick { 00017 class TextureAtlas; 00018 00019 enum class ButtonState { 00020 normal, hover 00021 }; 00022 00023 class Button : public Sprite, public SpriteMouseListener { 00024 public: 00025 Button(GameObject *gameObject, std::shared_ptr<Canvas> canvas); 00026 00027 std::string normalSprite() const; 00028 void setNormalSprite(std::string const &normal); 00029 std::string hoverSprite() const; 00030 void setHoverSprite(std::string const &hover); 00031 std::string pressedSprite() const; 00032 void setPressedSprite(std::string const &pressed); 00033 std::string text() const; 00034 void setText(std::string const &text); 00035 std::function<void(Button*)> const & getOnClick() const; 00036 void setOnClick(std::function<void(Button*)> const &onClick); 00037 00038 virtual void deactivated() override; 00039 virtual void setOrder(int order) override; 00040 00041 glm::vec4 const &normalColor() const; 00042 void setNormalColor(glm::vec4 const &normalColor); 00043 glm::vec4 const &hoverColor() const; 00044 void setHoverColor(glm::vec4 const &hoverColor); 00045 glm::vec4 const &pressedColor() const; 00046 void setPressedColor(glm::vec4 const &pressedColor); 00047 protected: 00048 virtual std::string currentSpriteName(); 00049 virtual void updateTextureAndTxtColor(); 00050 virtual void down(int button) override; 00051 virtual void pressed(int button) override; 00052 virtual void up(int button) override; 00053 virtual void over() override; 00054 virtual void out() override; 00055 00056 virtual void invokeClick(); 00057 00058 std::string mNormalName; 00059 std::string mHoverName; 00060 std::string mPressedName; 00061 00062 ButtonState mState = ButtonState::normal; 00063 00064 glm::vec4 mNormalColor = glm::vec4{1,1,1,1}; 00065 glm::vec4 mHoverColor = glm::vec4{1,1,1,1}; 00066 glm::vec4 mPressedColor = glm::vec4{0,0,0,1}; 00067 std::string mText; 00068 std::function<void(Button*)> onClick; 00069 std::shared_ptr<Label> mTextComponent; 00070 std::set<int> mPressedButtons; 00071 }; 00072 } 00073 00074