| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 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 | #include "menuitem.hpp" |
| 17 | #include "menu.hpp" |
| 18 | #include "menuitemaction.hpp" |
| 19 | #include "menumanager.hpp" |
| 20 | |
| 21 | namespace MTFH { |
| 22 | |
| 23 | MenuItem::MenuItem(float width, float height, std::wstring text, bool selectable) |
| 24 | : m_menu(nullptr) |
| 25 | , m_text(text) |
| 26 | , m_menuOpenActionMenuId("") |
| 27 | , m_action(nullptr) |
| 28 | , m_actionFunction(nullptr) |
| 29 | , m_view(nullptr) |
| 30 | , m_focused(false) |
| 31 | , m_selected(false) |
| 32 | , m_selectable(selectable) |
| 33 | , m_width(width) |
| 34 | , m_height(height) |
| 35 | , m_x(0) |
| 36 | , m_y(0) |
| 37 | , m_targetX(0) |
| 38 | , m_targetY(0) |
| 39 | , m_lMargin(1) |
| 40 | , m_rMargin(1) |
| 41 | , m_tMargin(1) |
| 42 | , m_bMargin(1) |
| 43 | , m_index(-1) |
| 44 | , m_animationCurve(30, 3) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | float MenuItem::width() const |
| 49 | { |
| 50 | return m_width + m_lMargin + m_rMargin; |
| 51 | } |
| 52 | |
| 53 | float MenuItem::height() const |
| 54 | { |
| 55 | return m_height + m_tMargin + m_bMargin; |
| 56 | } |
| 57 | |
| 58 | void MenuItem::setIndex(int index) |
| 59 | { |
| 60 | m_index = index; |
| 61 | } |
| 62 | |
| 63 | int MenuItem::index() const |
| 64 | { |
| 65 | return m_index; |
| 66 | } |
| 67 | |
| 68 | void MenuItem::setPos(float x, float y) |
| 69 | { |
| 70 | m_x = x; |
| 71 | m_y = y; |
| 72 | m_targetX = x; |
| 73 | m_targetY = y; |
| 74 | } |
| 75 | |
| 76 | void MenuItem::setPos(float x, float y, float targetX, float targetY) |
| 77 | { |
| 78 | m_x = x; |
| 79 | m_y = y; |
| 80 | m_targetX = targetX; |
| 81 | m_targetY = targetY; |
| 82 | } |
| 83 | |
| 84 | float MenuItem::x() const |
| 85 | { |
| 86 | return m_x; |
| 87 | } |
| 88 | |
| 89 | float MenuItem::y() const |
| 90 | { |
| 91 | return m_y; |
| 92 | } |
| 93 | |
| 94 | void MenuItem::resetAnimationCurve(size_t steps, int exp) |
| 95 | { |
| 96 | m_animationCurve = AnimationCurve(steps, exp); |
| 97 | m_animationCurve.reset(); |
| 98 | } |
| 99 | |
| 100 | void MenuItem::setView(MenuItemViewPtr view) |
| 101 | { |
| 102 | m_view = view; |
| 103 | } |
| 104 | |
| 105 | MenuItemViewPtr MenuItem::view() |
| 106 | { |
| 107 | return m_view; |
| 108 | } |
| 109 | |
| 110 | void MenuItem::setAction(MenuItemActionPtr action) |
| 111 | { |
| 112 | m_action = action; |
| 113 | } |
| 114 | |
| 115 | void MenuItem::setAction(ActionFunction actionFunction) |
| 116 | { |
| 117 | m_actionFunction = actionFunction; |
| 118 | } |
| 119 | |
| 120 | void MenuItem::setMenuOpenAction(const std::string & menuId) |
| 121 | { |
| 122 | m_menuOpenActionMenuId = menuId; |
| 123 | } |
| 124 | |
| 125 | MenuItemActionPtr MenuItem::action() const |
| 126 | { |
| 127 | return m_action; |
| 128 | } |
| 129 | |
| 130 | void MenuItem::onLeft() |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | void MenuItem::onRight() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | void MenuItem::onUp() |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | void MenuItem::onDown() |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | void MenuItem::setCurrent() |
| 147 | { |
| 148 | if (m_menu) |
| 149 | { |
| 150 | m_menu->setCurrentIndex(index()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void MenuItem::setSelected(bool flag) |
| 155 | { |
| 156 | if (flag) |
| 157 | { |
| 158 | if (m_action) |
| 159 | { |
| 160 | m_action->fire(); |
| 161 | } |
| 162 | |
| 163 | if (m_actionFunction) |
| 164 | { |
| 165 | m_actionFunction(); |
| 166 | } |
| 167 | |
| 168 | if (!m_menuOpenActionMenuId.empty()) |
| 169 | { |
| 170 | MenuManager::instance().pushMenu(m_menuOpenActionMenuId); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | m_selected = flag; |
| 175 | } |
| 176 | |
| 177 | void MenuItem::setFocused(bool focused) |
| 178 | { |
| 179 | m_focused = focused; |
| 180 | } |
| 181 | |
| 182 | void MenuItem::setContentsMargins(float left, float right, float top, float bottom) |
| 183 | { |
| 184 | m_lMargin = left; |
| 185 | m_rMargin = right; |
| 186 | m_tMargin = top; |
| 187 | m_bMargin = bottom; |
| 188 | } |
| 189 | |
| 190 | void MenuItem::getContentsMargins(float & left, float & right, float & top, float & bottom) const |
| 191 | { |
| 192 | left = m_lMargin; |
| 193 | right = m_rMargin; |
| 194 | top = m_tMargin; |
| 195 | bottom = m_bMargin; |
| 196 | } |
| 197 | |
| 198 | bool MenuItem::focused() const |
| 199 | { |
| 200 | return m_focused; |
| 201 | } |
| 202 | |
| 203 | float MenuItem::targetY() const |
| 204 | { |
| 205 | return m_targetY; |
| 206 | } |
| 207 | |
| 208 | Menu * MenuItem::menu() const |
| 209 | { |
| 210 | return m_menu; |
| 211 | } |
| 212 | |
| 213 | void MenuItem::setMenu(Menu * menu) |
| 214 | { |
| 215 | m_menu = menu; |
| 216 | } |
| 217 | |
| 218 | float MenuItem::targetX() const |
| 219 | { |
| 220 | return m_targetX; |
| 221 | } |
| 222 | |
| 223 | bool MenuItem::selected() const |
| 224 | { |
| 225 | return m_selected && m_selectable; |
| 226 | } |
| 227 | |
| 228 | bool MenuItem::selectable() const |
| 229 | { |
| 230 | return m_selectable; |
| 231 | } |
| 232 | |
| 233 | void MenuItem::render() |
| 234 | { |
| 235 | if (m_view) |
| 236 | { |
| 237 | m_view->render(m_x, m_y); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | const std::wstring & MenuItem::text() const |
| 242 | { |
| 243 | return m_text; |
| 244 | } |
| 245 | |
| 246 | void MenuItem::stepTime(int msecs) |
| 247 | { |
| 248 | if (m_view) |
| 249 | { |
| 250 | m_view->stepTime(msecs); |
| 251 | } |
| 252 | |
| 253 | positionAnimation(msecs); |
| 254 | } |
| 255 | |
| 256 | void MenuItem::positionAnimation(int) |
| 257 | { |
| 258 | m_animationCurve.step(); |
| 259 | |
| 260 | m_x = m_x + (m_targetX - m_x) * m_animationCurve.value(); |
| 261 | m_y = m_y + (m_targetY - m_y) * m_animationCurve.value(); |
| 262 | } |
| 263 | |
| 264 | MenuItem::~MenuItem() = default; |
| 265 | |
| 266 | } // namespace MTFH |