| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #ifndef MENUITEM_HPP |
| 17 | #define MENUITEM_HPP |
| 18 | |
| 19 | #include <functional> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "animationcurve.hpp" |
| 24 | #include "menuitemaction.hpp" |
| 25 | #include "menuitemview.hpp" |
| 26 | |
| 27 | namespace MTFH { |
| 28 | |
| 29 | class Menu; |
| 30 | class MenuItemAction; |
| 31 | |
| 32 | //! Base class for items used in menus. |
| 33 | class MenuItem |
| 34 | { |
| 35 | public: |
| 36 | MenuItem(float width, float height, std::wstring text = L"", bool selectable = false); |
| 37 | |
| 38 | virtual ~MenuItem(); |
| 39 | |
| 40 | void setMenu(Menu * menu); |
| 41 | |
| 42 | Menu * menu() const; |
| 43 | |
| 44 | void setIndex(int index); |
| 45 | |
| 46 | int index() const; |
| 47 | |
| 48 | float width() const; |
| 49 | |
| 50 | float height() const; |
| 51 | |
| 52 | void setPos(float x, float y); |
| 53 | |
| 54 | void setPos(float x, float y, float targetX, float targetY); |
| 55 | |
| 56 | float x() const; |
| 57 | |
| 58 | float y() const; |
| 59 | |
| 60 | void resetAnimationCurve(size_t steps, int exp); |
| 61 | |
| 62 | const std::wstring & text() const; |
| 63 | |
| 64 | void setView(MenuItemViewPtr view); |
| 65 | |
| 66 | MenuItemViewPtr view(); |
| 67 | |
| 68 | void setAction(MenuItemActionPtr action); |
| 69 | |
| 70 | typedef std::function<void()> ActionFunction; |
| 71 | void setAction(ActionFunction actionFunction); |
| 72 | |
| 73 | void setMenuOpenAction(const std::string & menuId); |
| 74 | |
| 75 | MenuItemActionPtr action() const; |
| 76 | |
| 77 | virtual void render(); |
| 78 | |
| 79 | virtual void onLeft(); |
| 80 | |
| 81 | virtual void onRight(); |
| 82 | |
| 83 | virtual void onUp(); |
| 84 | |
| 85 | virtual void onDown(); |
| 86 | |
| 87 | virtual void setCurrent(); |
| 88 | |
| 89 | virtual void setSelected(bool flag); |
| 90 | |
| 91 | virtual void setFocused(bool focused); |
| 92 | |
| 93 | virtual void stepTime(int msecs); |
| 94 | |
| 95 | virtual void positionAnimation(int msecs); |
| 96 | |
| 97 | void setContentsMargins(float left, float right, float top, float bottom); |
| 98 | |
| 99 | void getContentsMargins(float & left, float & right, float & top, float & bottom) const; |
| 100 | |
| 101 | bool selected() const; |
| 102 | |
| 103 | bool selectable() const; |
| 104 | |
| 105 | bool focused() const; |
| 106 | |
| 107 | //! Animation target X |
| 108 | float targetX() const; |
| 109 | |
| 110 | //! Animation target Y |
| 111 | float targetY() const; |
| 112 | |
| 113 | private: |
| 114 | Menu * m_menu; |
| 115 | |
| 116 | std::wstring m_text; |
| 117 | |
| 118 | std::string m_menuOpenActionMenuId; |
| 119 | |
| 120 | MenuItemActionPtr m_action; |
| 121 | |
| 122 | ActionFunction m_actionFunction; |
| 123 | |
| 124 | MenuItemViewPtr m_view; |
| 125 | |
| 126 | bool m_focused; |
| 127 | |
| 128 | bool m_selected; |
| 129 | |
| 130 | bool m_selectable; |
| 131 | |
| 132 | float m_width; |
| 133 | |
| 134 | float m_height; |
| 135 | |
| 136 | float m_x; |
| 137 | |
| 138 | float m_y; |
| 139 | |
| 140 | float m_targetX; |
| 141 | |
| 142 | float m_targetY; |
| 143 | |
| 144 | float m_lMargin; |
| 145 | |
| 146 | float m_rMargin; |
| 147 | |
| 148 | float m_tMargin; |
| 149 | |
| 150 | float m_bMargin; |
| 151 | |
| 152 | int m_index; |
| 153 | |
| 154 | AnimationCurve m_animationCurve; |
| 155 | }; |
| 156 | |
| 157 | typedef std::shared_ptr<MTFH::MenuItem> MenuItemPtr; |
| 158 | |
| 159 | } // namespace MTFH |
| 160 | |
| 161 | #endif // MENUITEM_HPP |