| 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 MENU_HPP |
| 17 | #define MENU_HPP |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "animationcurve.hpp" |
| 24 | #include "menuitem.hpp" |
| 25 | #include "menumanager.hpp" |
| 26 | |
| 27 | namespace MTFH { |
| 28 | |
| 29 | class MenuItem; |
| 30 | |
| 31 | class Menu |
| 32 | { |
| 33 | public: |
| 34 | //! Default styles |
| 35 | enum class Style |
| 36 | { |
| 37 | VerticalList, |
| 38 | HorizontalList, |
| 39 | ShowOne, // Show only one item at a time |
| 40 | ShowMany |
| 41 | }; |
| 42 | |
| 43 | enum class MouseItemType |
| 44 | { |
| 45 | Quit, |
| 46 | Prev, |
| 47 | Next |
| 48 | }; |
| 49 | |
| 50 | //! Constructor. |
| 51 | Menu(std::string id, int width, int height, Style style = Style::VerticalList); |
| 52 | |
| 53 | //! Destructor. |
| 54 | virtual ~Menu(); |
| 55 | |
| 56 | std::string id() const; |
| 57 | |
| 58 | //! Renders the menu items. See also MenuItemView. |
| 59 | virtual void render(); |
| 60 | |
| 61 | //! Selection up. |
| 62 | virtual void up(); |
| 63 | |
| 64 | //! Selection down. |
| 65 | virtual void down(); |
| 66 | |
| 67 | //! Current item left. |
| 68 | virtual void left(); |
| 69 | |
| 70 | //! Current item right. |
| 71 | virtual void right(); |
| 72 | |
| 73 | //! Select the current item. |
| 74 | virtual void selectCurrentItem(); |
| 75 | |
| 76 | //! Steps time for animations etc. Re-implement in the menu items. |
| 77 | virtual void stepTime(int msecs); |
| 78 | |
| 79 | virtual void positionAnimation(int msecs); |
| 80 | |
| 81 | /*! Handle mouse press at given screen coordinates. |
| 82 | * \return true if on a valid item. */ |
| 83 | virtual bool handleMousePress(int x, int y, int screenWidth, int screenHeight); |
| 84 | |
| 85 | /*! Handle mouse release at given screen coordinates. |
| 86 | * \return true if on a valid item. */ |
| 87 | virtual bool handleMouseRelease(int x, int y, int screenWidth, int screenHeight); |
| 88 | |
| 89 | //! \return current item or nullptr if no items added. |
| 90 | MenuItemPtr currentItem() const; |
| 91 | |
| 92 | /*! \return selected item or nullptr if no item added. |
| 93 | * Pressing enter on an item selects it. */ |
| 94 | MenuItemPtr selectedItem() const; |
| 95 | |
| 96 | //! \return current item at given index or nullptr. |
| 97 | MenuItemPtr item(size_t index) const; |
| 98 | |
| 99 | //! \return number of currently added items. |
| 100 | size_t itemCount() const; |
| 101 | |
| 102 | //! Add item to the menu. |
| 103 | virtual void addItem(MenuItemPtr menuItem); |
| 104 | |
| 105 | //! Reverse the item vector. |
| 106 | virtual void reverseItems(); |
| 107 | |
| 108 | //! Add mouse control item. |
| 109 | virtual void addMouseItem(MouseItemType type, MenuItemPtr menuItem); |
| 110 | |
| 111 | //! Use this if the current menu stack needs to be |
| 112 | //! completely quit, for example when the execution |
| 113 | //! should move from the menu to the actual game. |
| 114 | void setIsDone(bool done); |
| 115 | |
| 116 | //! \returns true if done. |
| 117 | bool isDone() const; |
| 118 | |
| 119 | //! Returns the width of the menu. |
| 120 | int width() const; |
| 121 | |
| 122 | //! Returns the height of the menu. |
| 123 | int height() const; |
| 124 | |
| 125 | void setPos(float x, float y); |
| 126 | |
| 127 | void setPos(float x, float y, float targetX, float targetY); |
| 128 | |
| 129 | float x() const; |
| 130 | |
| 131 | float y() const; |
| 132 | |
| 133 | //! Animation target X |
| 134 | float targetX() const; |
| 135 | |
| 136 | //! Animation target Y |
| 137 | float targetY() const; |
| 138 | |
| 139 | //! Return the current index. |
| 140 | int currentIndex() const; |
| 141 | |
| 142 | //! Set default wrap behavior. |
| 143 | void setWrapAround(bool wrapAround); |
| 144 | |
| 145 | //! Set current index. Clamp if out-of-range. |
| 146 | void setCurrentIndex(int index); |
| 147 | |
| 148 | //! Set current index. Wrap around if out-of-range. |
| 149 | void setCurrentIndexWrapAround(int index); |
| 150 | |
| 151 | //! \see Style::ShowMany |
| 152 | void setItemsToShow(const std::vector<int> & itemsToShow); |
| 153 | |
| 154 | //! Exits and pops the menu stack. The menu can call this also by itself. |
| 155 | virtual void exit(); |
| 156 | |
| 157 | protected: |
| 158 | //! Called when the menu is entered. Call parent implementation if overridden. |
| 159 | virtual void enter(); |
| 160 | |
| 161 | virtual void pushEnter(); |
| 162 | |
| 163 | virtual void pushExit(); |
| 164 | |
| 165 | virtual void popEnter(); |
| 166 | |
| 167 | virtual void popExit(); |
| 168 | |
| 169 | private: |
| 170 | struct MouseItem |
| 171 | { |
| 172 | MenuItemPtr item; |
| 173 | MouseItemType type; |
| 174 | }; |
| 175 | |
| 176 | void incrementIndex(int increment); |
| 177 | |
| 178 | bool isNextAllowed() const; |
| 179 | |
| 180 | bool isPrevAllowed() const; |
| 181 | |
| 182 | bool checkIfHit(MenuItemPtr item, int x, int y); |
| 183 | |
| 184 | bool doHandleMousePressOnMouseItem(MenuItemPtr item, int x, int y); |
| 185 | |
| 186 | bool handleMousePressOnItem(int x, int y); |
| 187 | |
| 188 | bool handleMousePressOnMouseItem(int x, int y); |
| 189 | |
| 190 | bool handleMouseReleaseOnItem(int x, int y); |
| 191 | |
| 192 | bool handleMouseReleaseOnMouseItem(int x, int y); |
| 193 | |
| 194 | void renderItems(); |
| 195 | |
| 196 | void renderMouseItems(); |
| 197 | |
| 198 | void updateFocus(); |
| 199 | |
| 200 | std::vector<MenuItemPtr> m_items; |
| 201 | |
| 202 | std::vector<MouseItem> m_mouseItems; |
| 203 | |
| 204 | std::vector<int> m_itemsToShow; |
| 205 | |
| 206 | std::string m_id; |
| 207 | |
| 208 | int m_width; |
| 209 | |
| 210 | int m_height; |
| 211 | |
| 212 | float m_x; |
| 213 | |
| 214 | float m_y; |
| 215 | |
| 216 | float m_targetX; |
| 217 | |
| 218 | float m_targetY; |
| 219 | |
| 220 | int m_currentIndex; |
| 221 | |
| 222 | int m_selectedIndex; |
| 223 | |
| 224 | Style m_style; |
| 225 | |
| 226 | bool m_isDone; |
| 227 | |
| 228 | bool m_wrapAround; |
| 229 | |
| 230 | AnimationCurve m_animationCurve; |
| 231 | |
| 232 | friend class MenuManager; |
| 233 | }; |
| 234 | |
| 235 | } // namespace MTFH |
| 236 | |
| 237 | #endif // MENU_HPP |