Skip to content

Commit

Permalink
[TexturePackSelectionState] Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 14, 2020
1 parent 468bdc0 commit 7f8f732
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 16 deletions.
9 changes: 9 additions & 0 deletions source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ void TextureAtlas::packTextures() {
gk::Texture::bind(nullptr);
}

void TextureAtlas::clear() {
m_tileSize = 0;

m_textureMap.clear();
m_textures.clear();

m_isReady = false;
}

void TextureAtlas::loadFromRegistry(const std::string &texturePack) {
if (!texturePack.empty() && !gk::Filesystem::fileExists("texturepacks/" + texturePack))
throw EXCEPTION("Texture pack '" + texturePack +"' doesn't exist");
Expand Down
2 changes: 2 additions & 0 deletions source/client/graphics/TextureAtlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class TextureAtlas {
void addFile(const std::string &path, const std::string &filename);
void packTextures();

void clear();

void loadFromRegistry(const std::string &texturePack = "");

u16 getTextureID(const std::string &filename) const;
Expand Down
27 changes: 19 additions & 8 deletions source/client/gui/ScrollableList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

u16 ScrollableListElement::widgetWidth = 200;

ScrollableListElement::ScrollableListElement(const std::string &line1, const std::string &line2, const std::string &line3, Widget *parent) : Widget(widgetWidth, 0, parent) {
ScrollableListElement::ScrollableListElement(u16 id, const std::string &line1, const std::string &line2, const std::string &line3, Widget *parent) : Widget(widgetWidth, 0, parent) {
m_id = id;

m_line1.setString(line1);
m_line1.updateVertexBuffer();
m_line1.setPosition(0, 1);
Expand Down Expand Up @@ -75,20 +77,29 @@ void ScrollableList::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
for (auto &it : m_elements) {
if (it.isPointInWidget(event.button.x, event.button.y)) {
m_cursor.setSize(it.width() + 2, it.height() + 2);
m_cursor.setPosition(-1, it.getPosition().y - 1);

m_selectedElement = ⁢
selectElement(it);
}
}
}
}

void ScrollableList::addElement(const std::string &line1, const std::string &line2, const std::string &line3) {
m_elements.emplace_back(line1, line2, line3, this);
m_elements.back().setPosition(0, (m_elements.size() - 1) * (m_elements.back().height() + 4) + 2);
void ScrollableList::addElement(const std::string &line1, const std::string &line2, const std::string &line3, bool isSelected) {
m_elements.emplace_back(m_elements.size(), line1, line2, line3, this);

ScrollableListElement &element = m_elements.back();
element.setPosition(0, (m_elements.size() - 1) * (m_elements.back().height() + 4) + 2);

m_height += m_elements.back().height();

if (isSelected)
selectElement(element);
}

void ScrollableList::selectElement(ScrollableListElement &element) {
m_cursor.setSize(element.width() + 2, element.height() + 2);
m_cursor.setPosition(-1, element.getPosition().y - 1);

m_selectedElement = &element;
}

void ScrollableList::draw(gk::RenderTarget &target, gk::RenderStates states) const {
Expand Down
9 changes: 7 additions & 2 deletions source/client/gui/ScrollableList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@

class ScrollableListElement : public Widget {
public:
ScrollableListElement(const std::string &line1, const std::string &line2, const std::string &line3, Widget *parent);
ScrollableListElement(u16 id, const std::string &line1, const std::string &line2, const std::string &line3, Widget *parent);

static u16 widgetWidth;

u16 id() const { return m_id; }

const std::string &line1() const { return m_line1.string(); }

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

u16 m_id = 0;

gk::Image m_icon{"texture-world_icon"};

Text m_line1;
Expand All @@ -56,11 +60,12 @@ class ScrollableList : public Widget {

void onEvent(const SDL_Event &event) override;

void addElement(const std::string &line1, const std::string &line2, const std::string &line3);
void addElement(const std::string &line1, const std::string &line2, const std::string &line3, bool isSelected = false);

const ScrollableListElement *selectedElement() const { return m_selectedElement; }

private:
void selectElement(ScrollableListElement &element);
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

std::vector<ScrollableListElement> m_elements;
Expand Down
2 changes: 2 additions & 0 deletions source/client/states/ServerLoadingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ ServerLoadingState::ServerLoadingState(GameState &game, bool showLoadingState, c

gk::Mouse::setCursorVisible(true);
gk::Mouse::setCursorGrabbed(false);

m_game.textureAtlas().clear();
}

void ServerLoadingState::init() {
Expand Down
5 changes: 5 additions & 0 deletions source/client/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "KeyboardHandler.hpp"
#include "Registry.hpp"
#include "SettingsMenuState.hpp"
#include "TexturePackSelectionState.hpp"
#include "World.hpp"

SettingsMenuState::SettingsMenuState(gk::ApplicationState *parent) : InterfaceState(parent) {
Expand Down Expand Up @@ -167,6 +168,10 @@ void SettingsMenuState::addMainButtons() {
addInputButtons();
});

m_menuWidget.addButton("Texture Pack...", [this] (TextButton &) {
m_stateStack->push<TexturePackSelectionState>(this);
});

updateWidgetPosition();
}

Expand Down
160 changes: 160 additions & 0 deletions source/client/states/TexturePackSelectionState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* =====================================================================================
*
* 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 <gk/core/ApplicationStateStack.hpp>

#include <filesystem.hpp>

#include "Config.hpp"
#include "GameConfig.hpp"
#include "TexturePackSelectionState.hpp"

namespace fs = ghc::filesystem;

TexturePackSelectionState::TexturePackSelectionState(gk::ApplicationState *parent) : InterfaceState(parent) {
m_background.setScale(Config::guiScale * 2, Config::guiScale * 2);

m_filter1.setFillColor(gk::Color(0, 0, 0, 192));
m_filter2.setFillColor(gk::Color(0, 0, 0, 120));

m_title.setScale(Config::guiScale, Config::guiScale);
m_title.setString("Select Texture Pack");
m_title.updateVertexBuffer();

m_texturePackList.setScale(Config::guiScale, Config::guiScale);

m_menuWidget.setScale(Config::guiScale, Config::guiScale);
m_menuWidget.setHorizontalSpacing(8);
m_menuWidget.addButton("Select Texture Pack", [this](TextButton &) {
const ScrollableListElement *element = m_texturePackList.selectedElement();
if (element)
GameConfig::texturePack = (element->id()) ? element->line1() : "";

m_stateStack->pop();
}, 150);
m_menuWidget.addButton("Cancel", [this](TextButton &) {
m_stateStack->pop();
}, 150);

updateWidgetPosition();
loadTexturePackList();
}

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

if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
m_stateStack->pop();

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED
&& !m_stateStack->empty() && &m_stateStack->top() != this) {
m_texturePackList.onEvent(event);
}

if (!m_stateStack->empty() && &m_stateStack->top() == this) {
m_texturePackList.onEvent(event);

m_menuWidget.onEvent(event);
}
}

void TexturePackSelectionState::updateWidgetPosition() {
m_background.setPosRect(0, 0, Config::screenWidth / m_background.getScale().x, Config::screenHeight / m_background.getScale().y);
m_background.setClipRect(0, 0, Config::screenWidth / m_background.getScale().x, Config::screenHeight / m_background.getScale().y);

m_filter1.setSize(Config::screenWidth, Config::screenHeight);

const int topBorderSize = 25 * Config::guiScale;
const int bottomBorderSize = 25 * Config::guiScale;
m_filter2.setSize(Config::screenWidth, Config::screenHeight - topBorderSize - bottomBorderSize);
m_filter2.setPosition(0, topBorderSize);

m_title.setPosition(
Config::screenWidth / 2.0f - m_title.getSize().x * Config::guiScale / 2.0f,
topBorderSize / 2.0f - m_title.getSize().y * Config::guiScale / 2.0f
);

m_texturePackList.setPosition(
Config::screenWidth / 2.0f - m_texturePackList.getGlobalBounds().sizeX / 2.0f,
topBorderSize + 2.0f * Config::guiScale
);

m_menuWidget.setPosition(
Config::screenWidth / 2.0f - m_menuWidget.getGlobalBounds().sizeX / 2.0f,
Config::screenHeight - bottomBorderSize / 2.0f - m_menuWidget.getGlobalBounds().sizeY / 2.0f
);
}

void TexturePackSelectionState::loadTexturePackList() {
m_texturePackList.addElement("No Texture Pack", "Use default mod textures", "", GameConfig::texturePack.empty());

if (fs::is_directory("texturepacks")) {
std::vector<fs::directory_entry> dirs;

fs::directory_iterator dir("texturepacks/");
for (const auto &entry : dir) {
if (entry.is_directory()) {
dirs.emplace_back(entry);
}
}

std::sort(dirs.begin(), dirs.end());

for (auto &entry : dirs) {
std::string dirname = entry.path().filename();
std::string textures;
if (fs::exists(entry.path().string() + "/blocks")) {
textures += "Blocks";
}
if (fs::exists(entry.path().string() + "/items")) {
if (!textures.empty()) textures += ", ";
textures += "Items";
}
if (fs::exists(entry.path().string() + "/gui")) {
if (!textures.empty()) textures += ", ";
textures += "GUI";
}

bool isSelected = GameConfig::texturePack == dirname;
m_texturePackList.addElement(dirname, textures, "", isSelected);
}
}
}

void TexturePackSelectionState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (&m_stateStack->top() == this) {
prepareDraw(target, states);

target.draw(m_background, states);
target.draw(m_filter1, states);
target.draw(m_filter2, states);

target.draw(m_title, states);
target.draw(m_texturePackList, states);
target.draw(m_menuWidget, states);
}
}

60 changes: 60 additions & 0 deletions source/client/states/TexturePackSelectionState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* =====================================================================================
*
* 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 TEXTUREPACKSELECTIONSTATE_HPP_
#define TEXTUREPACKSELECTIONSTATE_HPP_

#include <gk/graphics/Image.hpp>

#include "InterfaceState.hpp"
#include "MenuWidget.hpp"
#include "ScrollableList.hpp"
#include "Text.hpp"

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

void onEvent(const SDL_Event &event) override;

private:
void updateWidgetPosition() override;
void loadTexturePackList();

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

gk::Image m_background{"texture-menu_background"};
gk::RectangleShape m_filter1;
gk::RectangleShape m_filter2;

Text m_title;

ScrollableList m_texturePackList;

MenuWidget m_menuWidget{2, 1};
};

#endif // TEXTUREPACKSELECTIONSTATE_HPP_
4 changes: 0 additions & 4 deletions source/client/states/WorldSelectionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ void WorldSelectionState::onEvent(const SDL_Event &event) {
}
}

void WorldSelectionState::update() {
}

void WorldSelectionState::updateWidgetPosition() {
m_background.setPosRect(0, 0, Config::screenWidth / m_background.getScale().x, Config::screenHeight / m_background.getScale().y);
m_background.setClipRect(0, 0, Config::screenWidth / m_background.getScale().x, Config::screenHeight / m_background.getScale().y);
Expand Down Expand Up @@ -159,7 +156,6 @@ void WorldSelectionState::loadSaveList() {
if (fs::is_directory("saves")) {
std::vector<fs::directory_entry> files;

fs::path basePath = fs::current_path();
fs::directory_iterator dir("saves/");
for (const auto &entry : dir) {
if (entry.is_regular_file() && entry.path().filename().generic_string()[0] != '_') {
Expand Down
2 changes: 0 additions & 2 deletions source/client/states/WorldSelectionState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class WorldSelectionState : public InterfaceState {

void onEvent(const SDL_Event &event) override;

void update() override;

private:
void updateWidgetPosition() override;
void loadSaveList();
Expand Down

0 comments on commit 7f8f732

Please sign in to comment.