Skip to content

Commit

Permalink
[SettingsMenuState] Now using sliders for some settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 8, 2020
1 parent e3dc327 commit 18d0fa8
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 109 deletions.
34 changes: 0 additions & 34 deletions config/client.example.lua

This file was deleted.

6 changes: 0 additions & 6 deletions config/server.example.lua

This file was deleted.

3 changes: 0 additions & 3 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ u16 Config::screenWidth = 1600;
u16 Config::screenHeight = 1050;
u8 Config::guiScale = 3;
u8 Config::mipmapLevels = 3;
float Config::aoStrength = 1.0f;

// Input
u8 Config::mouseSensitivity = 8;
Expand Down Expand Up @@ -99,7 +98,6 @@ void Config::loadConfigFromFile(const char *filename) {
screenHeight = lua["screenHeight"].get_or(screenHeight);
guiScale = lua["guiScale"].get_or(guiScale);
mipmapLevels = lua["mipmapLevels"].get_or(mipmapLevels);
aoStrength = lua["aoStrength"].get_or(aoStrength);

mouseSensitivity = lua["mouseSensitivity"].get_or(mouseSensitivity);

Expand Down Expand Up @@ -135,7 +133,6 @@ void Config::saveConfigToFile(const char *filename) {
file << "screenHeight = " << screenHeight << std::endl;
file << "guiScale = " << (u16)guiScale << std::endl;
file << "mipmapLevels = " << (u16)mipmapLevels << std::endl;
file << "aoStrength = " << aoStrength << std::endl;
file << std::endl;
file << "mouseSensitivity = " << (u16)mouseSensitivity << std::endl;
file << std::endl;
Expand Down
1 change: 0 additions & 1 deletion source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace Config {
extern u16 screenHeight;
extern u8 guiScale;
extern u8 mipmapLevels;
extern float aoStrength;

// Input
extern u8 mouseSensitivity;
Expand Down
83 changes: 56 additions & 27 deletions source/client/gui/MenuWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,76 +37,105 @@ void MenuWidget::reset(u16 width, u16 height) {
m_height = height;

m_buttons.clear();
m_buttons.reserve(m_width * m_height);
m_sliders.clear();

Widget::m_width = 0;
Widget::m_height = 0;
}

void MenuWidget::onEvent(const SDL_Event &event) {
for (std::size_t i = 0 ; i < m_buttons.size() ; ++i) {
m_buttons.at(i).onEvent(event);
for (auto &it : m_buttons) {
it.first.onEvent(event);

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
int x = i % m_width;
int y = i / m_width;
updateWidgetPosition(it.first, it.second.x, it.second.y);
}
}

for (auto &it : m_sliders) {
it.first.onEvent(event);

updateButtonPosition(m_buttons.at(i), x, y);
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
updateWidgetPosition(it.first, it.second.x, it.second.y);
}
}
}

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

for (std::size_t i = 0 ; i < m_buttons.size() ; ++i) {
int x = i % m_width;
int y = i / m_width;
for (auto &it : m_buttons) {
updateWidgetPosition(it.first, it.second.x, it.second.y);
}

updateButtonPosition(m_buttons.at(i), x, y);
for (auto &it : m_sliders) {
updateWidgetPosition(it.first, it.second.x, it.second.y);
}
}

TextButton &MenuWidget::addButton(const std::string &text, const TextButton::CppCallback &callback, u16 width) {
int x = m_buttons.size() % m_width;
int y = m_buttons.size() / m_width;
int x = (m_buttons.size() + m_sliders.size()) % m_width;
int y = (m_buttons.size() + m_sliders.size()) / m_width;

m_buttons.emplace_back(width, this);
m_buttons.emplace_back(std::piecewise_construct,
std::forward_as_tuple(width, this), std::forward_as_tuple(x, y));

TextButton &button = m_buttons.back();
TextButton &button = m_buttons.back().first;
button.setText(text);
button.setCallback(callback);

updateButtonPosition(button, x, y);
updateWidgetPosition(button, x, y);

return button;
}

void MenuWidget::setButtonEnabled(const std::string &text, bool isEnabled) {
for (auto &it : m_buttons) {
if (it.text() == text)
it.setEnabled(isEnabled);
if (it.first.text() == text)
it.first.setEnabled(isEnabled);
}
}

void MenuWidget::updateButtonPosition(TextButton &button, int x, int y) {
button.setPosition(x * (button.width() + m_horizontalSpacing),
y * (button.height() + m_verticalSpacing));
SliderWidget &MenuWidget::addSlider(const std::string &text, const SliderWidget::CppCallback &callback, int min, int max, int initialValue) {
int x = (m_buttons.size() + m_sliders.size()) % m_width;
int y = (m_buttons.size() + m_sliders.size()) / m_width;

m_sliders.emplace_back(std::piecewise_construct,
std::forward_as_tuple(this), std::forward_as_tuple(x, y));

SliderWidget &slider = m_sliders.back().first;
slider.setText(text);
slider.setCallback(callback);
slider.setMinMaxValues(min, max);
slider.setCurrentValue(initialValue);

updateWidgetPosition(slider, x, y);

return slider;
}

void MenuWidget::updateWidgetPosition(Widget &widget, int x, int y) {
widget.setPosition(x * (widget.width() + m_horizontalSpacing),
y * (widget.height() + m_verticalSpacing));

if (button.getPosition().x + button.width() > Widget::m_width) {
Widget::m_width = button.getPosition().x + button.width();
if (widget.getPosition().x + widget.width() > Widget::m_width) {
Widget::m_width = widget.getPosition().x + widget.width();
}
if (button.getPosition().y + button.height() > Widget::m_height) {
Widget::m_height = button.getPosition().y + button.height();
if (widget.getPosition().y + widget.height() > Widget::m_height) {
Widget::m_height = widget.getPosition().y + widget.height();
}
}

void MenuWidget::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();

for (const TextButton &button : m_buttons) {
if (!button.text().empty())
target.draw(button, states);
for (auto &it : m_buttons) {
if (!it.first.text().empty())
target.draw(it.first, states);
}

for (auto &it : m_sliders) {
target.draw(it.first, states);
}
}

8 changes: 6 additions & 2 deletions source/client/gui/MenuWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef MENUWIDGET_HPP_
#define MENUWIDGET_HPP_

#include "SliderWidget.hpp"
#include "TextButton.hpp"

struct GuiScaleChangedEvent;
Expand All @@ -45,11 +46,13 @@ class MenuWidget : public Widget {
TextButton &addButton(const std::string &text, const TextButton::CppCallback &callback, u16 width = 200);
void setButtonEnabled(const std::string &text, bool isEnabled);

SliderWidget &addSlider(const std::string &text, const SliderWidget::CppCallback &callback, int min, int max, int initialValue);

void setVerticalSpacing(u16 verticalSpacing) { m_verticalSpacing = verticalSpacing; }
void setHorizontalSpacing(u16 horizontalSpacing) { m_horizontalSpacing = horizontalSpacing; }

private:
void updateButtonPosition(TextButton &button, int x, int y);
void updateWidgetPosition(Widget &widget, int x, int y);

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

Expand All @@ -59,7 +62,8 @@ class MenuWidget : public Widget {
u16 m_width = 1;
u16 m_height = 1;

std::vector<TextButton> m_buttons;
std::vector<std::pair<TextButton, gk::Vector2i>> m_buttons;
std::vector<std::pair<SliderWidget, gk::Vector2i>> m_sliders;
};

#endif // MENUWIDGET_HPP_
111 changes: 111 additions & 0 deletions source/client/gui/SliderWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* =====================================================================================
*
* 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
*
* =====================================================================================
*/
#include "Config.hpp"
#include "SliderWidget.hpp"

SliderWidget::SliderWidget(Widget *parent) : SliderWidget(200, parent) {
}

SliderWidget::SliderWidget(u16 width, Widget *parent) : Widget(width, 20, parent) {
m_text.setColor(m_defaultTextColor);
m_text.setShadowColor({56, 56, 56});

m_slider.setClipRect(0, 66, 8, 20);
m_background.setClipRect(0, 46, width, 20);

m_sliderBorder.setClipRect(200 - 2, 66, 2, 20);
m_backgroundBorder.setClipRect(200 - 2, 46, 2, 20);

m_sliderBorder.setPosition(8 - 2, 0);
m_backgroundBorder.setPosition(width - 2, 0);
}

void SliderWidget::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEMOTION) {
if (m_isDragging || isPointInWidget(event.motion.x, event.motion.y)) {
m_text.setColor(m_hoverColor);

updatePercentage(event.motion.x);
}
else
m_text.setColor(m_defaultTextColor);

if (m_isDragging)
m_callback(*this, event.type);
}
else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
m_isDragging = isPointInWidget(event.button.x, event.button.y);
updatePercentage(event.button.x);
if (m_isDragging)
m_callback(*this, event.type);
}
else if (event.type == SDL_MOUSEBUTTONUP && event.button.button == SDL_BUTTON_LEFT) {
if (m_isDragging)
m_callback(*this, event.type);
m_isDragging = false;
}
}

void SliderWidget::setCurrentValue(int currentValue) {
m_percentage = (currentValue - m_min) / float(m_max - m_min);

updateSliderPosition();
}

void SliderWidget::setText(const std::string &text) {
m_text.setString(text);
m_text.updateVertexBuffer();
m_text.setPosition(m_width / 2 - m_text.getSize().x / 2,
m_height / 2 - m_text.getSize().y / 2, 0);
}

void SliderWidget::updatePercentage(s32 mouseX) {
if (m_isDragging) {
m_percentage = (mouseX - getGlobalBounds().x) / Config::guiScale / m_width;
m_percentage = std::clamp(m_percentage, 0.f, 1.f);

updateSliderPosition();
}
}

void SliderWidget::updateSliderPosition() {
m_slider.setPosition(m_percentage * (m_width - 8), 0);
m_sliderBorder.setPosition(m_percentage * (m_width - 8) + 6, 0);
}

void SliderWidget::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();

target.draw(m_background, states);
target.draw(m_backgroundBorder, states);

target.draw(m_slider, states);
target.draw(m_sliderBorder, states);

target.draw(m_text, states);
}

Loading

0 comments on commit 18d0fa8

Please sign in to comment.