Skip to content

Commit

Permalink
Fixed #30 using gk::EventHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 13, 2020
1 parent ceafa8c commit c386d62
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 3 deletions.
36 changes: 36 additions & 0 deletions client/source/event/Events.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef EVENTS_HPP_
#define EVENTS_HPP_

#include <gk/core/IntTypes.hpp>

struct GuiScaleChangedEvent {
u8 guiScale;
};

#endif // EVENTS_HPP_
7 changes: 7 additions & 0 deletions client/source/hud/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ClientCommandHandler.hpp"
#include "ClientPlayer.hpp"
#include "Config.hpp"
#include "Events.hpp"
#include "HUD.hpp"

HUD::HUD(ClientPlayer &player, ClientWorld &world, ClientCommandHandler &client)
Expand Down Expand Up @@ -71,6 +72,12 @@ void HUD::onEvent(const SDL_Event &event) {
m_blockCursor.onEvent(event, m_hotbar);
}

void HUD::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
setScale(event.guiScale, event.guiScale, 1);

setup();
}

void HUD::update() {
// FIXME: Shouldn't be called every tick
if (Config::isHotbarVisible)
Expand Down
3 changes: 3 additions & 0 deletions client/source/hud/HUD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
#include "DebugOverlay.hpp"
#include "Hotbar.hpp"

class GuiScaleChangedEvent;

class HUD : public gk::Transformable, public gk::Drawable {
public:
HUD(ClientPlayer &player, ClientWorld &world, ClientCommandHandler &client);

void setup();

void onEvent(const SDL_Event &event);
void onGuiScaleChanged(const GuiScaleChangedEvent &event);

void update();

Expand Down
9 changes: 9 additions & 0 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <gk/resource/ResourceHandler.hpp>

#include "ChatState.hpp"
#include "Events.hpp"
#include "GameKey.hpp"
#include "GameState.hpp"
#include "LuaGUIState.hpp"
Expand All @@ -61,6 +62,10 @@ GameState::GameState()
m_world.setCamera(m_player.camera());
}

void GameState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&GameState::onGuiScaleChanged, this);
}

void GameState::connect(const std::string &host, int port) {
m_client.connect(host, port);
m_player.setClientID(m_client.id());
Expand Down Expand Up @@ -159,6 +164,10 @@ void GameState::initShaders() {
m_shader.linkProgram();
}

void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
m_hud.onGuiScaleChanged(event);
}

void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
// FIXME: This uniform is not used anymore since water/leaves effects are disabled
// gk::Shader::bind(&m_shader);
Expand Down
4 changes: 4 additions & 0 deletions client/source/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class GameState : public gk::ApplicationState {
public:
GameState();

void init() override;

void connect(const std::string &host, int port);

void onEvent(const SDL_Event &event) override;
Expand All @@ -66,6 +68,8 @@ class GameState : public gk::ApplicationState {
private:
void initShaders();

void onGuiScaleChanged(const GuiScaleChangedEvent &event);

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

gk::Shader m_shader;
Expand Down
16 changes: 16 additions & 0 deletions client/source/states/PauseMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#include <glm/gtc/matrix_transform.hpp>

#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/EventHandler.hpp>
#include <gk/core/Mouse.hpp>

#include "Client.hpp"
#include "Config.hpp"
#include "Events.hpp"
#include "PauseMenuState.hpp"
#include "SettingsMenuState.hpp"
#include "TitleScreenState.hpp"
Expand Down Expand Up @@ -69,6 +71,10 @@ PauseMenuState::PauseMenuState(Client &client, gk::ApplicationState *parent)
});
}

void PauseMenuState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&PauseMenuState::onGuiScaleChanged, this);
}

void PauseMenuState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(event);

Expand All @@ -90,6 +96,16 @@ void PauseMenuState::onEvent(const SDL_Event &event) {
}
}

void PauseMenuState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
m_menuWidget.setScale(event.guiScale, event.guiScale);

// FIXME: Ugly hack to get MenuWidget working, will change soon
SDL_Event e;
e.type = SDL_WINDOWEVENT;
e.window.event = SDL_WINDOWEVENT_SIZE_CHANGED;
m_menuWidget.onEvent(e);
}

void PauseMenuState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);
Expand Down
5 changes: 5 additions & 0 deletions client/source/states/PauseMenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@
#include "MenuWidget.hpp"

class Client;
class GuiScaleChangedEvent;

class PauseMenuState : public InterfaceState {
public:
PauseMenuState(Client &client, gk::ApplicationState *parent = nullptr);

void init() override;

void onEvent(const SDL_Event &event) override;

private:
void onGuiScaleChanged(const GuiScaleChangedEvent &event);

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

MenuWidget m_menuWidget{1, 4};
Expand Down
32 changes: 29 additions & 3 deletions client/source/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,37 @@
#include <gk/core/input/KeyboardHandler.hpp>
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Debug.hpp>
#include <gk/core/EventHandler.hpp>
#include <gk/core/Mouse.hpp>

#include "Config.hpp"
#include "Events.hpp"
#include "SettingsMenuState.hpp"
#include "World.hpp"

SettingsMenuState::SettingsMenuState(gk::ApplicationState *parent) : InterfaceState(parent) {
m_menuWidget.setScale(Config::guiScale, Config::guiScale, 1);

m_doneButton.setPosition(Config::screenWidth / 2.0f - m_doneButton.getGlobalBounds().sizeX * Config::guiScale / 2.0f, Config::screenHeight - 291);
m_doneButton.setScale(Config::guiScale, Config::guiScale, 1);
m_doneButton.setText("Done");
m_doneButton.setCallback([this] (TextButton &) {
doneButtonAction();
});

updateDoneButtonPosition();

addMainButtons();
}

void SettingsMenuState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&SettingsMenuState::onGuiScaleChanged, this);
}

void SettingsMenuState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(event);

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
m_doneButton.setPosition(Config::screenWidth / 2.0f - m_doneButton.getGlobalBounds().sizeX / 2.0f, Config::screenHeight - 291);
updateDoneButtonPosition();

if (&m_stateStack->top() != this)
m_menuWidget.onEvent(event);
Expand All @@ -78,6 +85,23 @@ void SettingsMenuState::onEvent(const SDL_Event &event) {
}
}

void SettingsMenuState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
m_menuWidget.setScale(event.guiScale, event.guiScale);
m_doneButton.setScale(event.guiScale, event.guiScale);

updateDoneButtonPosition();

// FIXME: Ugly hack to get MenuWidget working, will change soon
SDL_Event e;
e.type = SDL_WINDOWEVENT;
e.window.event = SDL_WINDOWEVENT_SIZE_CHANGED;
m_menuWidget.onEvent(e);
}

void SettingsMenuState::updateDoneButtonPosition() {
m_doneButton.setPosition(Config::screenWidth / 2.0f - m_doneButton.getGlobalBounds().sizeX / 2.0f, Config::screenHeight * 0.75);
}

void SettingsMenuState::doneButtonAction() {
if (m_state != MenuState::Main) {
m_state = MenuState::Main;
Expand Down Expand Up @@ -142,9 +166,11 @@ void SettingsMenuState::addGraphicsButtons() {
addToggleButton("Sun Smooth Lighting", Config::isSunSmoothLightingEnabled, true);
addToggleButton("Ambient Occlusion", Config::isAmbientOcclusionEnabled, false);

m_menuWidget.addButton("GUI Scale: " + std::to_string(Config::guiScale), [] (TextButton &button) {
m_menuWidget.addButton("GUI Scale: " + std::to_string(Config::guiScale), [this] (TextButton &button) {
Config::guiScale = 1 + (Config::guiScale + 1) % 3;
button.setText("GUI Scale: " + std::to_string(Config::guiScale));

m_eventHandler->emplaceEvent<GuiScaleChangedEvent>(Config::guiScale);
});

addToggleButton("Fullscreen", Config::isFullscreenModeEnabled, false);
Expand Down
8 changes: 8 additions & 0 deletions client/source/states/SettingsMenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@
#include "InterfaceState.hpp"
#include "MenuWidget.hpp"

class GuiScaleChangedEvent;

class SettingsMenuState : public InterfaceState {
public:
SettingsMenuState(gk::ApplicationState *parent = nullptr);

void init() override;

void onEvent(const SDL_Event &event) override;

private:
void onGuiScaleChanged(const GuiScaleChangedEvent &event);

void updateDoneButtonPosition();

void doneButtonAction();

void addMainButtons();
Expand Down
15 changes: 15 additions & 0 deletions client/source/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <gk/core/ApplicationStateStack.hpp>

#include "Config.hpp"
#include "Events.hpp"
#include "GameState.hpp"
#include "ServerConnectState.hpp"
#include "ServerLoadingState.hpp"
Expand Down Expand Up @@ -68,6 +69,10 @@ void TitleScreenState::centerBackground() {
m_background.setPosition(Config::screenWidth / 2.0 - m_background.width() / 2.0, Config::screenHeight / 2.0 - m_background.height() / 2.0);
}

void TitleScreenState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&TitleScreenState::onGuiScaleChanged, this);
}

void TitleScreenState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(event);

Expand Down Expand Up @@ -107,6 +112,16 @@ void TitleScreenState::startMultiplayer(const std::string &host) {
m_stateStack->push<ServerLoadingState>(game, false, this);
}

void TitleScreenState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
m_menuWidget.setScale(event.guiScale, event.guiScale);

// FIXME: Ugly hack to get MenuWidget working, will change soon
SDL_Event e;
e.type = SDL_WINDOWEVENT;
e.window.event = SDL_WINDOWEVENT_SIZE_CHANGED;
m_menuWidget.onEvent(e);
}

void TitleScreenState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
prepareDraw(target, states);

Expand Down
6 changes: 6 additions & 0 deletions client/source/states/TitleScreenState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
#include "InterfaceState.hpp"
#include "MenuWidget.hpp"

class GuiScaleChangedEvent;

class TitleScreenState : public InterfaceState {
public:
TitleScreenState(u16 port = 4242);
~TitleScreenState();

void centerBackground();

void init() override;

void onEvent(const SDL_Event &event) override;

void update() override;
Expand All @@ -47,6 +51,8 @@ class TitleScreenState : public InterfaceState {
void startMultiplayer(const std::string &host);

private:
void onGuiScaleChanged(const GuiScaleChangedEvent &event);

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

MenuWidget m_menuWidget{1, 3};
Expand Down

0 comments on commit c386d62

Please sign in to comment.