| 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 "textmenuitemview.hpp" |
| 17 | #include "game.hpp" |
| 18 | #include <MenuItem> |
| 19 | |
| 20 | #include <MCAssetManager> |
| 21 | #include <MCRandom> |
| 22 | #include <MCTextureFont> |
| 23 | #include <MCTextureText> |
| 24 | |
| 25 | #include <cmath> |
| 26 | |
| 27 | TextMenuItemView::TextMenuItemView(float textSize, MTFH::MenuItem & owner) |
| 28 | : MenuItemView(owner) |
| 29 | , m_textSize(textSize) |
| 30 | , m_angle(MCRandom::getValue() * 2.0f * 3.1415f) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | void TextMenuItemView::setTextSize(float size) |
| 35 | { |
| 36 | m_textSize = size; |
| 37 | } |
| 38 | |
| 39 | void TextMenuItemView::stepTime(int) |
| 40 | { |
| 41 | m_angle += 0.010f; |
| 42 | } |
| 43 | |
| 44 | void TextMenuItemView::render(float x, float y) |
| 45 | { |
| 46 | MCTextureText text(owner().text()); |
| 47 | |
| 48 | const float amp = 0.05f; |
| 49 | float animatedSize = m_textSize + std::sin(m_angle) * m_textSize * amp; |
| 50 | if (owner().focused()) |
| 51 | { |
| 52 | animatedSize *= 1.25f; |
| 53 | } |
| 54 | text.setGlyphSize(animatedSize, animatedSize); |
| 55 | |
| 56 | if (owner().focused()) |
| 57 | { |
| 58 | const MCGLColor yellow(1.0f, 1.0f, 0.0f, 1.0f); |
| 59 | text.setColor(yellow); |
| 60 | } |
| 61 | else if (owner().selected()) |
| 62 | { |
| 63 | const MCGLColor red(1.0f, 0.0f, 0.0f, 1.0f); |
| 64 | text.setColor(red); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | const MCGLColor white(1.0f, 1.0f, 1.0f, 1.0f); |
| 69 | text.setColor(white); |
| 70 | } |
| 71 | |
| 72 | const float shadowY = -2; |
| 73 | const float shadowX = 2; |
| 74 | text.setShadowOffset(shadowX, shadowY); |
| 75 | |
| 76 | auto && font = MCAssetManager::textureFontManager().font(Game::instance().fontName()); |
| 77 | text.render(x - text.width(font) / 2, y, nullptr, font); |
| 78 | } |
| 79 | |
| 80 | TextMenuItemView::~TextMenuItemView() |
| 81 | { |
| 82 | } |