#ifndef CONTAINER_HPP #define CONTAINER_HPP #include #include #include #include #include #include namespace sftk { class Container; typedef std::shared_ptr pContainer; bool operator==(pContainer &shp, Container *rawp); class Container { public: // Public base functions static pContainer create(pContainer parent = nullptr); static pContainer create(sf::Vector2f pos, sf::Vector2f size, pContainer parent = nullptr); void setParent(pContainer parent); void setPosition(sf::Vector2f pos = sf::Vector2f(0, 0)); void setSize(sf::Vector2f pos); void setBackgroudColor(sf::Color color); pContainer getParent() const; const sf::Vector2f &getPosition() const; const sf::Vector2f &getSize() const; sf::FloatRect getGlobalBouds() const; virtual bool isRoot() const; virtual bool hasInternalChange() const; virtual void update() const; public: // Event hadler register functions template void registerEvent(EventType type, std::function handler); public: // Static and operators static pContainer getRoot(Container *p = nullptr); friend bool sftk::operator==(pContainer &shp, Container *rawp); protected: // Must be overrided by child classes Container(pContainer parent = nullptr); Container(sf::Vector2f pos, sf::Vector2f size, pContainer parent = nullptr); Container(sf::Vector2f pos, sf::Vector2f size, bool root); virtual sf::RenderWindow &windowSFWindow(); // The events bases on sfml's Event, // so the Window class must use this function to // pass event to the root container // (itself's parent class)'s dispatchEvent(Event&) // to process it as Event. void dispatchEvent(sf::Event &event); private: // For this class to use sf::RenderWindow &getSfWindow(); // When this container did deal with the passed event // it returns true. On the oppsite case it returns false. bool dispatchEvent(Event &event); pContainer parent; std::vector childs; sf::Vector2f basepos; sf::Vector2f pos, size; sf::View view; sf::Color backgroudColor; static const int64_t maxClickTime = 300; private: // Called on construction static void rootTest(Container *self); inline static int64_t currentTime(); private: // Event handlers std::function closed; std::function resized; std::function mousePressed; std::function mouseReleased; std::function mouseClicked; std::function mouseEntered; std::function mouseLeft; std::function mouseMoved; std::function mouseDragged; std::function mouseScrolled; std::function textInputed; private: // Second events asistant vars // When the values is not -1, mouse had pressed. int64_t lastMousePressTime[sf::Mouse::Button::ButtonCount]; // When the values is not (-1, -1), mouse had pressed. sf::Vector2f lastMousePressPos[sf::Mouse::Button::ButtonCount]; bool focused; bool mouseLocated; public: // Exceptions class RootContainerNotPresent : public SFTKException { public: RootContainerNotPresent(Container *container); const char *what() const noexcept override; }; class NotAWindowObj : public SFTKException { public: NotAWindowObj(Container *container); const char *what() const noexcept override; }; class HandlerNotMatched : public SFTKException { public: HandlerNotMatched(Container *container); const char *what() const noexcept override; }; }; }; #endif