00001 #ifndef GUI_MANAGER_HPP 00002 #define GUI_MANAGER_HPP 00003 00004 #include "gui_eventreceiver.hpp" 00005 #include "gui_anchor.hpp" 00006 #include <utils_exception.hpp> 00007 #include <utils_refptr.hpp> 00008 #include <utils_wptr.hpp> 00009 #include <string> 00010 #include <vector> 00011 #include <map> 00012 #include <array> 00013 #include <functional> 00014 00015 namespace lua { 00016 class state; 00017 } 00018 00019 namespace input { 00020 class handler; 00021 class manager; 00022 } 00023 00024 namespace gui 00025 { 00026 class uiobject; 00027 class region; 00028 class layered_region; 00029 class frame; 00030 class focus_frame; 00031 class sprite; 00032 class material; 00033 class render_target; 00034 class font; 00035 class color; 00036 struct quad; 00037 struct vertex; 00038 00039 class manager_impl; 00040 00042 00047 class exception : public utils::exception 00048 { 00049 public : 00050 00051 explicit exception(const std::string& sMessage) : utils::exception(sMessage) 00052 { 00053 } 00054 00055 exception(const std::string& sClassName, const std::string& sMessage) : utils::exception(sClassName, sMessage) 00056 { 00057 } 00058 }; 00059 00061 struct addon 00062 { 00063 addon() : bEnabled(true) {} 00064 00065 std::string sName; 00066 std::string sVersion; 00067 std::string sUIVersion; 00068 std::string sAuthor; 00069 00070 bool bEnabled; 00071 00072 std::string sMainDirectory; 00073 std::string sDirectory; 00074 00075 std::vector<std::string> lFileList; 00076 std::vector<std::string> lSavedVariableList; 00077 }; 00078 00079 enum frame_strata 00080 { 00081 STRATA_PARENT, 00082 STRATA_BACKGROUND, 00083 STRATA_LOW, 00084 STRATA_MEDIUM, 00085 STRATA_HIGH, 00086 STRATA_DIALOG, 00087 STRATA_FULLSCREEN, 00088 STRATA_FULLSCREEN_DIALOG, 00089 STRATA_TOOLTIP 00090 }; 00091 00093 struct level 00094 { 00095 std::vector<frame*> lFrameList; 00096 }; 00097 00099 struct strata 00100 { 00101 strata(); 00102 ~strata(); 00103 00104 uint uiID; 00105 std::map<int, level> lLevelList; 00106 mutable bool bRedraw; 00107 utils::refptr<render_target> pRenderTarget; 00108 utils::refptr<sprite> pSprite; 00109 mutable uint uiRedrawCount; 00110 }; 00111 00113 class manager : public event_receiver 00114 { 00115 private : 00116 00117 template <class T> 00118 static frame* create_new_frame(manager* pMgr) 00119 { 00120 return new T(pMgr); 00121 } 00122 00123 template <class T> 00124 static layered_region* create_new_layered_region(manager* pMgr) 00125 { 00126 return new T(pMgr); 00127 } 00128 00129 public : 00130 00132 00138 manager(const input::handler& mInputHandler, const std::string& sLocale, 00139 uint uiScreenWidth, uint uiScreenHeight, utils::refptr<manager_impl> pImpl); 00140 00142 ~manager(); 00143 00145 manager(const manager& mMgr) = delete; 00146 00148 manager& operator = (const manager& mMgr) = delete; 00149 00151 00157 uint get_screen_width() const; 00158 00160 00166 uint get_screen_height() const; 00167 00169 00171 void add_addon_directory(const std::string& sDirectory); 00172 00174 00178 void clear_addon_directory_list(); 00179 00181 00184 bool check_uiobject_name(const std::string& sName) const; 00185 00187 00192 uiobject* create_uiobject(const std::string& sClassName); 00193 00195 00200 frame* create_frame(const std::string& sClassName); 00201 00203 00215 frame* create_frame( 00216 const std::string& sClassName, const std::string& sName, 00217 frame* pParent = nullptr, const std::string& sInheritance = "" 00218 ); 00219 00221 00226 layered_region* create_layered_region(const std::string& sClassName); 00227 00229 00232 utils::refptr<sprite> create_sprite(utils::refptr<material> pMat) const; 00233 00235 00245 utils::refptr<sprite> create_sprite(utils::refptr<material> pMat, float fWidth, float fHeight) const; 00246 00248 00260 utils::refptr<sprite> create_sprite(utils::refptr<material> pMat, 00261 float fU, float fV, float fWidth, float fHeight) const; 00262 00264 00269 utils::refptr<material> create_material(const std::string& sFileName) const; 00270 00272 00275 utils::refptr<material> create_material(const color& mColor) const; 00276 00278 00281 utils::refptr<material> create_material(utils::refptr<render_target> pRenderTarget) const; 00282 00284 00287 utils::refptr<render_target> create_render_target(uint uiWidth, uint uiHeight) const; 00288 00290 00296 utils::refptr<font> create_font(const std::string& sFontFile, uint uiSize) const; 00297 00299 00302 bool add_uiobject(uiobject* pObj); 00303 00305 00309 void remove_uiobject(uiobject* pObj); 00310 00312 00318 void notify_object_has_parent(uiobject* pObj); 00319 00321 00324 const uiobject* get_uiobject(uint uiID) const; 00325 00327 00330 uiobject* get_uiobject(uint uiID); 00331 00333 00337 const uiobject* get_uiobject_by_name(const std::string& sName, bool bVirtual = false) const; 00338 00340 00344 uiobject* get_uiobject_by_name(const std::string& sName, bool bVirtual = false); 00345 00347 void fire_build_strata_list(); 00348 00350 void print_statistics(); 00351 00353 00355 std::string print_ui() const; 00356 00358 00360 addon* get_current_addon(); 00361 00363 00372 void set_current_addon(addon* pAddOn); 00373 00375 00382 std::string parse_file_name(const std::string& sFileName) const; 00383 00385 00388 void set_key_binding(uint uiKey, const std::string& sLuaString); 00389 00391 00395 void set_key_binding(uint uiKey, uint uiModifier, const std::string& sLuaString); 00396 00398 00403 void set_key_binding( 00404 uint uiKey, uint uiModifier1, uint uiModifier2, const std::string& sLuaString 00405 ); 00406 00408 00410 void remove_key_binding(uint uiKey); 00411 00413 00416 void remove_key_binding(uint uiKey, uint uiModifier); 00417 00419 00423 void remove_key_binding(uint uiKey, uint uiModifier1, uint uiModifier2); 00424 00426 00428 utils::wptr<lua::state> get_lua(); 00429 00431 00440 void create_lua(std::function<void()> pLuaRegs = nullptr); 00441 00443 00447 void read_files(); 00448 00450 00452 void load_ui(); 00453 00455 void close_ui(); 00456 00458 00460 void reload_ui(); 00461 00463 void render_ui() const; 00464 00466 00470 void render_quad(const quad& mQuad) const; 00471 00473 00480 void render_quads(const quad& mQuad, const std::vector<std::array<vertex,4>>& lQuadList) const; 00481 00483 00485 bool is_loading_ui() const; 00486 00488 00494 void start_moving(uiobject* pObj, anchor* pAnchor = nullptr, constraint mConstraint = CONSTRAINT_NONE, std::function<void()> pApplyConstraintFunc = nullptr); 00495 00497 00499 void stop_moving(uiobject* pObj); 00500 00502 00505 bool is_moving(uiobject* pObj) const; 00506 00508 00513 void start_sizing(uiobject* pObj, anchor_point mPoint); 00514 00516 00518 void stop_sizing(uiobject* pObj); 00519 00521 00524 bool is_sizing(uiobject* pObj) const; 00525 00527 00531 int get_movement_x() const; 00532 00534 00538 int get_movement_y() const; 00539 00541 void notify_object_moved(); 00542 00544 void fire_redraw(frame_strata mStrata) const; 00545 00547 00550 void enable_caching(bool bEnable); 00551 00553 00556 void toggle_caching(); 00557 00559 00561 bool is_caching_enabled() const; 00562 00564 00567 void enable_input(bool bEnable); 00568 00570 00573 void toggle_input(); 00574 00576 00581 bool is_input_enabled() const; 00582 00584 00589 void clear_fonts_on_close(bool bClear); 00590 00592 00605 void create_strata_render_target(frame_strata mframe_strata); 00606 00608 00610 const frame* get_overed_frame() const; 00611 00613 00615 void request_focus(focus_frame* pFocusFrame); 00616 00618 00621 int get_highest_level(frame_strata mframe_strata) const; 00622 00624 00626 void update(float fDelta); 00627 00629 00631 void on_event(const event& mEvent); 00632 00634 00636 void begin(utils::refptr<render_target> pTarget = nullptr) const; 00637 00639 00642 void end() const; 00643 00645 00648 template<class T> 00649 void register_frame_type(const std::string& sClassName) 00650 { 00651 lCustomFrameList_[sClassName] = &create_new_frame<T>; 00652 T::register_glue(pLua_); 00653 } 00654 00656 00659 template<class T> 00660 void register_region_type(const std::string& sClassName) 00661 { 00662 lCustomRegionList_[sClassName] = &create_new_layered_region<T>; 00663 T::register_glue(pLua_); 00664 } 00665 00667 00669 utils::wptr<const manager_impl> get_impl() const; 00670 00672 00674 utils::wptr<manager_impl> get_impl(); 00675 00677 00679 utils::wptr<event_manager> get_event_manager(); 00680 00682 00684 utils::wptr<input::manager> get_input_manager(); 00685 00687 00689 const std::string& get_locale() const; 00690 00692 00694 static manager* get_manager(lua::state* pState); 00695 00696 private : 00697 00698 void register_lua_manager_(); 00699 00700 void load_addon_toc_(const std::string& sAddOnName, const std::string& sAddOnDirectory); 00701 void load_addon_files_(addon* pAddOn); 00702 void load_addon_directory_(const std::string& sDirectory); 00703 00704 void save_variables_(addon* pAddOn); 00705 00706 void set_overed_frame_(frame* pFrame, int iX = 0, int iY = 0); 00707 00708 void create_strata_render_target_(strata& mStrata); 00709 void render_strata_(strata& mStrata); 00710 00711 void parse_xml_file_(const std::string& sFile, addon* pAddOn); 00712 00713 void remove_uiobject_(uiobject* pObj); 00714 00715 std::string sUIVersion_; 00716 uint uiScreenWidth_; 00717 uint uiScreenHeight_; 00718 00719 00720 bool bClearFontsOnClose_; 00721 00722 utils::refptr<lua::state> pLua_; 00723 std::function<void()> pLuaRegs_; 00724 bool bClosed_; 00725 bool bLoadingUI_; 00726 bool bFirstIteration_; 00727 00728 bool bInputEnabled_; 00729 utils::refptr<input::manager> pInputManager_; 00730 std::map<uint, std::map<uint, std::map<uint, std::string>>> lKeyBindingList_; 00731 00732 std::map<std::string, uiobject*> lNamedObjectList_; 00733 std::map<std::string, uiobject*> lNamedVirtualObjectList_; 00734 00735 std::map<uint, uiobject*> lObjectList_; 00736 std::map<uint, uiobject*> lMainObjectList_; 00737 00738 std::vector<uiobject*> lRemovedObjectList_; 00739 00740 std::vector<std::string> lGUIDirectoryList_; 00741 addon* pCurrentAddOn_; 00742 std::map<std::string, std::map<std::string, addon>> lAddOnList_; 00743 00744 std::map<uint, frame*> lFrameList_; 00745 std::map<frame_strata, strata> lStrataList_; 00746 bool bBuildStrataList_; 00747 bool bObjectMoved_; 00748 frame* pOveredFrame_; 00749 bool bUpdateOveredFrame_; 00750 focus_frame* pFocusedFrame_; 00751 00752 uiobject* pMovedObject_; 00753 uiobject* pSizedObject_; 00754 float fMouseMovementX_; 00755 float fMouseMovementY_; 00756 00757 anchor* pMovedAnchor_; 00758 int iMovementStartPositionX_; 00759 int iMovementStartPositionY_; 00760 constraint mConstraint_; 00761 std::function<void()> pApplyConstraintFunc_; 00762 00763 uint uiResizeStartW_; 00764 uint uiResizeStartH_; 00765 bool bResizeWidth_; 00766 bool bResizeHeight_; 00767 bool bResizeFromRight_; 00768 bool bResizeFromBottom_; 00769 00770 uint uiFrameNumber_; 00771 00772 bool bEnableCaching_; 00773 00774 utils::refptr<render_target> pRenderTarget_; 00775 utils::refptr<sprite> pSprite_; 00776 00777 std::map<std::string, frame*(*)(manager*)> lCustomFrameList_; 00778 std::map<std::string, layered_region*(*)(manager*)> lCustomRegionList_; 00779 00780 std::string sLocale_; 00781 utils::refptr<event_manager> pEventManager_; 00782 utils::refptr<manager_impl> pImpl_; 00783 }; 00784 00786 class manager_impl 00787 { 00788 public : 00789 00791 manager_impl(); 00792 00794 virtual ~manager_impl(); 00795 00797 00800 void set_parent(manager* pParent); 00801 00803 00805 virtual void begin(utils::refptr<render_target> pTarget = nullptr) const = 0; 00806 00808 virtual void end() const = 0; 00809 00811 00815 virtual void render_quad(const quad& mQuad) const = 0; 00816 00818 00825 virtual void render_quads(const quad& mQuad, const std::vector<std::array<vertex,4>>& lQuadList) const = 0; 00826 00828 00835 virtual utils::refptr<sprite> create_sprite(utils::refptr<material> pMat) const; 00836 00838 00852 virtual utils::refptr<sprite> create_sprite(utils::refptr<material> pMat, float fWidth, float fHeight) const; 00853 00855 00871 virtual utils::refptr<sprite> create_sprite(utils::refptr<material> pMat, 00872 float fU, float fV, float fWidth, float fHeight) const; 00873 00875 00880 virtual utils::refptr<material> create_material(const std::string& sFileName) const = 0; 00881 00883 00886 virtual utils::refptr<material> create_material(const color& mColor) const = 0; 00887 00889 00892 virtual utils::refptr<material> create_material(utils::refptr<render_target> pRenderTarget) const = 0; 00893 00895 00898 virtual utils::refptr<render_target> create_render_target(uint uiWidth, uint uiHeight) const = 0; 00899 00901 00907 virtual utils::refptr<font> create_font(const std::string& sFontFile, uint uiSize) const = 0; 00908 00909 protected : 00910 00911 manager* pParent_; 00912 }; 00913 } 00914 00915 00916 #endif