[PauseMenuState] Added. Will be replaced by a generic MenuState later. [MenuWidget|TextButton] Added.

This commit is contained in:
Quentin Bazin 2018-06-28 10:54:17 +02:00
parent e5dbcb565a
commit a3f598c11e
9 changed files with 281 additions and 12 deletions

View File

@ -0,0 +1,31 @@
/*
* =====================================================================================
*
* Filename: MenuWidget.hpp
*
* Description:
*
* Created: 28/06/2018 10:32:26
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef MENUWIDGET_HPP_
#define MENUWIDGET_HPP_
#include "TextButton.hpp"
class MenuWidget : public Widget {
public:
void onEvent(const SDL_Event &event) override;
void addButton(const std::string &text, const TextButton::Callback &callback);
private:
void draw(RenderTarget &target, RenderStates states) const override;
std::vector<TextButton> m_buttons;
};
#endif // MENUWIDGET_HPP_

View File

@ -24,6 +24,8 @@ class Text : public IDrawable, public Transformable {
void setText(const std::string &text) { m_text = text; updateTextSprites(); }
const Vector2i &getSize() const { return m_size; }
private:
void draw(RenderTarget &target, RenderStates states) const override;
@ -37,6 +39,8 @@ class Text : public IDrawable, public Transformable {
Texture &m_texture;
VertexBuffer m_vbo;
Vector2i m_size;
};
#endif // TEXT_HPP_

View File

@ -0,0 +1,45 @@
/*
* =====================================================================================
*
* Filename: TextButton.hpp
*
* Description:
*
* Created: 28/06/2018 10:16:52
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef TEXTBUTTON_HPP_
#define TEXTBUTTON_HPP_
#include <functional>
#include "Image.hpp"
#include "Text.hpp"
#include "Widget.hpp"
class TextButton : public Widget {
public:
using Callback = std::function<void(void)>;
TextButton(const Callback &callback, Widget *parent = nullptr);
void onEvent(const SDL_Event &event) override;
void setText(const std::string &text);
private:
void draw(RenderTarget &target, RenderStates states) const override;
Image m_background{"texture-widgets"};
Image m_hoverBackground{"texture-widgets"};
Text m_text;
Callback m_callback;
bool m_isHovered = false;
};
#endif // TEXTBUTTON_HPP_

View File

@ -0,0 +1,40 @@
/*
* =====================================================================================
*
* Filename: PauseMenuState.hpp
*
* Description:
*
* Created: 28/06/2018 10:14:02
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef PAUSEMENUSTATE_HPP_
#define PAUSEMENUSTATE_HPP_
#include "ApplicationState.hpp"
#include "MenuWidget.hpp"
#include "Shader.hpp"
#include "RectangleShape.hpp"
class PauseMenuState : public ApplicationState {
public:
PauseMenuState(ApplicationState *parent = nullptr);
void onEvent(const SDL_Event &event) override;
void update() override;
private:
void draw(RenderTarget &target, RenderStates states) const override;
MenuWidget m_menuWidget;
Shader m_shader;
RectangleShape m_background;
};
#endif // PAUSEMENUSTATE_HPP_

37
source/gui/MenuWidget.cpp Normal file
View File

@ -0,0 +1,37 @@
/*
* =====================================================================================
*
* Filename: MenuWidget.cpp
*
* Description:
*
* Created: 28/06/2018 10:33:22
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "Config.hpp"
#include "MenuWidget.hpp"
void MenuWidget::onEvent(const SDL_Event &event) {
for (TextButton &button : m_buttons) {
button.onEvent(event);
}
}
void MenuWidget::addButton(const std::string &text, const TextButton::Callback &callback) {
TextButton &button = m_buttons.emplace_back(callback, this);
button.setText(text);
button.setPosition(SCREEN_WIDTH / getScale().x / 2 - button.width() / 2,
SCREEN_HEIGHT / getScale().y / 2 - button.height() / 2, 0);
}
void MenuWidget::draw(RenderTarget &target, RenderStates states) const {
applyTransform(states);
for (const TextButton &button : m_buttons) {
target.draw(button, states);
}
}

View File

@ -53,25 +53,27 @@ void Text::updateTextSprites() {
m_textSprites.emplace_back(std::move(sprite));
x += m_charWidth[(u8)c];
}
m_size.x = x;
m_size.y = 8;
}
// FIXME: Since I use the font from Minecraft assets, I needed to use
// this piece of code to make it look good
// I'll remove it later anyway
void Text::updateCharWidth() {
int width = m_texture.width();
int height = m_texture.height();
const int width = m_texture.width();
const int height = m_texture.height();
unsigned int data[width * height];
Texture::bind(&m_texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &data);
Texture::bind(nullptr);
int charMaxHeight = height / 16;
int charMaxWidth = width / 16;
float lvt_9_1_ = 8.0f / (float)charMaxWidth;
const int charMaxHeight = height / 16;
const int charMaxWidth = width / 16;
for (int i = 0; i < 256 ; ++i) {
for (int i = 0 ; i < 256 ; ++i) {
if (i == ' ') {
m_charWidth[i] = 4;
continue;
@ -91,18 +93,15 @@ void Text::updateCharWidth() {
for (int j2 = 0 ; j2 < charMaxHeight && flag1 ; ++j2) {
int k2 = (charY * charMaxWidth + j2) * width;
if ((data[i2 + k2] & 255) != 0) {
if ((data[i2 + k2] & 255) != 0)
flag1 = false;
}
}
if (!flag1) {
break;
}
if (!flag1) break;
}
++l1;
m_charWidth[i] = (int)(0.5 + (double)((float)l1 * lvt_9_1_)) + 1;
m_charWidth[i] = (int)(0.5 + (double)((float)l1 * (8.0f / (float)charMaxWidth))) + 1;
}
}

44
source/gui/TextButton.cpp Normal file
View File

@ -0,0 +1,44 @@
/*
* =====================================================================================
*
* Filename: TextButton.cpp
*
* Description:
*
* Created: 28/06/2018 10:18:03
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "TextButton.hpp"
TextButton::TextButton(const Callback &callback, Widget *parent) : Widget(200, 20, parent), m_callback(callback) {
m_background.setClipRect(0, 66, 200, 20);
m_hoverBackground.setClipRect(0, 86, 200, 20);
}
void TextButton::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEMOTION) {
m_isHovered = isPointInWidget(event.motion.x, event.motion.y);
}
else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT && m_isHovered) {
m_callback();
}
}
void TextButton::setText(const std::string &text) {
m_text.setText(text);
m_text.setPosition(m_width / 2 - m_text.getSize().x / 2, m_height / 2 - m_text.getSize().y / 2, 0);
}
void TextButton::draw(RenderTarget &target, RenderStates states) const {
applyTransform(states);
if (m_isHovered)
target.draw(m_hoverBackground, states);
else
target.draw(m_background, states);
target.draw(m_text, states);
}

View File

@ -23,6 +23,7 @@
#include "InventoryState.hpp"
#include "Keyboard.hpp"
#include "Mouse.hpp"
#include "PauseMenuState.hpp"
#include "PlayerInventoryWidget.hpp"
GameState::GameState() {
@ -84,6 +85,9 @@ void GameState::update() {
auto &inventoryState = m_stateStack->push<InventoryState>(this);
inventoryState.setupWidget<PlayerInventoryWidget>(m_player.inventory(), m_player.hotbarInventory());
}
else if (Keyboard::isKeyPressedOnce(Keyboard::Return) && &m_stateStack->top() == this) {
m_stateStack->push<PauseMenuState>(this);
}
}
void GameState::initShaders() {

View File

@ -0,0 +1,65 @@
/*
* =====================================================================================
*
* Filename: PauseMenuState.cpp
*
* Description:
*
* Created: 28/06/2018 10:15:27
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "ApplicationStateStack.hpp"
#include "Config.hpp"
#include "Keyboard.hpp"
#include "Mouse.hpp"
#include "PauseMenuState.hpp"
PauseMenuState::PauseMenuState(ApplicationState *parent) : ApplicationState(parent) {
m_shader.createProgram();
m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/basic.v.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/basic.f.glsl");
m_shader.linkProgram();
Mouse::setCursorGrabbed(false);
Mouse::setCursorVisible(true);
Mouse::resetToWindowCenter();
m_background.setColor(Color{0, 0, 0, 127});
m_background.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
m_menuWidget.setScale(GUI_SCALE, GUI_SCALE, 1);
m_menuWidget.addButton("Exit", [this] { while(!m_stateStack->empty()) m_stateStack->pop(); });
}
void PauseMenuState::onEvent(const SDL_Event &event) {
m_menuWidget.onEvent(event);
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
Mouse::setCursorGrabbed(true);
Mouse::setCursorVisible(false);
m_stateStack->pop();
}
}
void PauseMenuState::update() {
if (Keyboard::isKeyPressedOnce(Keyboard::Return)) {
m_stateStack->pop();
}
}
void PauseMenuState::draw(RenderTarget &target, RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);
applyTransform(states);
states.shader = &m_shader;
target.draw(m_background, states);
target.draw(m_menuWidget, states);
}