[LuaGUIState] Textures are now loaded from a path given by mods.

This commit is contained in:
Quentin Bazin 2020-03-10 13:15:23 +01:00
parent 875242568b
commit b3ae21009e
21 changed files with 37 additions and 22 deletions

View File

@ -26,7 +26,7 @@
*/ */
#include "ProgressBarWidget.hpp" #include "ProgressBarWidget.hpp"
ProgressBarWidget::ProgressBarWidget(const std::string &texture, BlockData &blockData, ProgressBarType type, Widget *parent) ProgressBarWidget::ProgressBarWidget(const gk::Texture &texture, BlockData &blockData, ProgressBarType type, Widget *parent)
: Widget(parent), m_blockData(blockData), m_image(texture) : Widget(parent), m_blockData(blockData), m_image(texture)
{ {
m_type = type; m_type = type;

View File

@ -40,7 +40,7 @@ enum class ProgressBarType : u8 {
class ProgressBarWidget : public Widget { class ProgressBarWidget : public Widget {
public: public:
ProgressBarWidget(const std::string &texture, BlockData &blockData, ProgressBarType type, Widget *parent = nullptr); ProgressBarWidget(const gk::Texture &texture, BlockData &blockData, ProgressBarType type, Widget *parent = nullptr);
void init(const gk::FloatRect &clipRect, const gk::Vector2i &position, const std::string &meta, unsigned int maxMetaValue); void init(const gk::FloatRect &clipRect, const gk::Vector2i &position, const std::string &meta, unsigned int maxMetaValue);
void init(const gk::FloatRect &clipRect, const gk::Vector2i &position, const std::string &meta, const std::string &maxMeta); void init(const gk::FloatRect &clipRect, const gk::Vector2i &position, const std::string &meta, const std::string &maxMeta);

View File

@ -29,7 +29,7 @@
#include "Config.hpp" #include "Config.hpp"
#include "ScrollBarWidget.hpp" #include "ScrollBarWidget.hpp"
void ScrollBarWidget::init(const std::string &texture, const gk::FloatRect &clipRect, u16 minY, u16 maxY, InventoryWidget &widget) { void ScrollBarWidget::init(const gk::Texture &texture, const gk::FloatRect &clipRect, u16 minY, u16 maxY, InventoryWidget &widget) {
m_clipRect = clipRect; m_clipRect = clipRect;
m_minY = minY; m_minY = minY;

View File

@ -35,7 +35,7 @@ class ScrollBarWidget : public Widget {
public: public:
ScrollBarWidget(Widget *parent = nullptr) : Widget(12, 15, parent) {} ScrollBarWidget(Widget *parent = nullptr) : Widget(12, 15, parent) {}
void init(const std::string &texture, const gk::FloatRect &clipRect, u16 minY, u16 maxY, InventoryWidget &widget); void init(const gk::Texture &texture, const gk::FloatRect &clipRect, u16 minY, u16 maxY, InventoryWidget &widget);
void onEvent(const SDL_Event &event); void onEvent(const SDL_Event &event);

View File

@ -198,9 +198,11 @@ void LuaGUIState::loadGUI(sf::Packet &packet) {
} }
void LuaGUIState::loadImage(const std::string &, s32 x, s32 y, sf::Packet &packet) { void LuaGUIState::loadImage(const std::string &, s32 x, s32 y, sf::Packet &packet) {
std::string texture; std::string textureFilename;
gk::FloatRect clipRect; gk::FloatRect clipRect;
packet >> texture >> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY; packet >> textureFilename >> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;
gk::Texture &texture = loadTexture(textureFilename);
auto *image = new gk::Image(texture); auto *image = new gk::Image(texture);
image->setPosition(x, y); image->setPosition(x, y);
@ -310,9 +312,9 @@ void LuaGUIState::loadProgressBarWidget(const std::string &, s32 x, s32 y, sf::P
gk::Vector3i block; gk::Vector3i block;
std::string meta, maxMeta; std::string meta, maxMeta;
u32 maxValue; u32 maxValue;
std::string texture; std::string textureFilename;
gk::FloatRect clipRect; gk::FloatRect clipRect;
packet >> type >> block.x >> block.y >> block.z >> meta >> maxMeta >> maxValue >> texture packet >> type >> block.x >> block.y >> block.z >> meta >> maxMeta >> maxValue >> textureFilename
>> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY; >> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;
BlockData *data = m_world.getBlockData(block.x, block.y, block.z); BlockData *data = m_world.getBlockData(block.x, block.y, block.z);
@ -321,6 +323,8 @@ void LuaGUIState::loadProgressBarWidget(const std::string &, s32 x, s32 y, sf::P
return; return;
} }
gk::Texture &texture = loadTexture(textureFilename);
ProgressBarWidget *widget = new ProgressBarWidget(texture, *data, ProgressBarType(type)); ProgressBarWidget *widget = new ProgressBarWidget(texture, *data, ProgressBarType(type));
if (!maxMeta.empty()) if (!maxMeta.empty())
widget->init(clipRect, gk::Vector2i{x, y}, meta, maxMeta); widget->init(clipRect, gk::Vector2i{x, y}, meta, maxMeta);
@ -331,11 +335,13 @@ void LuaGUIState::loadProgressBarWidget(const std::string &, s32 x, s32 y, sf::P
} }
void LuaGUIState::loadScrollBarWidget(const std::string &, s32 x, s32 y, sf::Packet &packet) { void LuaGUIState::loadScrollBarWidget(const std::string &, s32 x, s32 y, sf::Packet &packet) {
std::string texture; std::string textureFilename;
gk::FloatRect clipRect; gk::FloatRect clipRect;
u16 minY, maxY; u16 minY, maxY;
std::string widget; std::string widget;
packet >> texture >> clipRect >> minY >> maxY >> widget; packet >> textureFilename >> clipRect >> minY >> maxY >> widget;
gk::Texture &texture = loadTexture(textureFilename);
ScrollBarWidget *scrollBarWidget = new ScrollBarWidget(&m_mainWidget); ScrollBarWidget *scrollBarWidget = new ScrollBarWidget(&m_mainWidget);
scrollBarWidget->setPosition(x, y); scrollBarWidget->setPosition(x, y);
@ -350,6 +356,17 @@ void LuaGUIState::loadInventory(const std::string &name, sf::Packet &packet) {
packet >> m_inventories.at(name); packet >> m_inventories.at(name);
} }
gk::Texture &LuaGUIState::loadTexture(const std::string &textureFilename) {
auto it = m_textures.find(textureFilename);
if (it == m_textures.end()) {
m_textures.emplace(textureFilename, gk::Texture{textureFilename});
return m_textures.at(textureFilename);
}
else {
return it->second;
}
}
void LuaGUIState::centerMainWidget() { void LuaGUIState::centerMainWidget() {
int x = floor(Config::screenWidth / 2.0f - m_width * Config::guiScale / 2.0f + 0.5f); int x = floor(Config::screenWidth / 2.0f - m_width * Config::guiScale / 2.0f + 0.5f);
int y = floor(Config::screenHeight / 2.0f - m_height * Config::guiScale / 2.0f + 0.5f); int y = floor(Config::screenHeight / 2.0f - m_height * Config::guiScale / 2.0f + 0.5f);

View File

@ -60,6 +60,8 @@ class LuaGUIState : public InterfaceState {
void loadScrollBarWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet); void loadScrollBarWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadInventory(const std::string &name, sf::Packet &packet); void loadInventory(const std::string &name, sf::Packet &packet);
gk::Texture &loadTexture(const std::string &textureFilename);
void centerMainWidget(); void centerMainWidget();
ClientCommandHandler &m_client; ClientCommandHandler &m_client;
@ -74,6 +76,7 @@ class LuaGUIState : public InterfaceState {
std::vector<std::unique_ptr<Widget>> m_widgets; std::vector<std::unique_ptr<Widget>> m_widgets;
std::vector<std::unique_ptr<gk::Drawable>> m_drawables; std::vector<std::unique_ptr<gk::Drawable>> m_drawables;
std::unordered_map<std::string, Inventory> m_inventories; std::unordered_map<std::string, Inventory> m_inventories;
std::unordered_map<std::string, gk::Texture> m_textures;
ClientPlayer &m_player; ClientPlayer &m_player;
ClientWorld &m_world; ClientWorld &m_world;

View File

@ -42,7 +42,7 @@ function show_creative_window(client, screen_width, screen_height, gui_scale)
name = "img_background", name = "img_background",
pos = {x = 0, y = 0}, pos = {x = 0, y = 0},
texture = "texture-creative_window", texture = "mods/default/textures/gui/creative_window.png",
clip = {x = 0, y = 0, width = 195, height = 136}, clip = {x = 0, y = 0, width = 195, height = 136},
} }
@ -72,7 +72,7 @@ function show_creative_window(client, screen_width, screen_height, gui_scale)
name = "scroll_bar", name = "scroll_bar",
pos = {x = 175, y = 18}, pos = {x = 175, y = 18},
texture = "texture-tabs", texture = "mods/default/textures/gui/tabs.png",
clip = {x = 232, y = 0, width = 12, height = 15}, clip = {x = 232, y = 0, width = 12, height = 15},
clip_selected = {x = 244, y = 0, width = 12, height = 15}, clip_selected = {x = 244, y = 0, width = 12, height = 15},

View File

@ -53,7 +53,7 @@ mod:block {
meta = "item_progress", meta = "item_progress",
max_value = 200, max_value = 200,
texture = "texture-furnace", texture = "mods/default/textures/gui/furnace.png",
clip = {x = 176, y = 14, width = 24, height = 17}, clip = {x = 176, y = 14, width = 24, height = 17},
} }
@ -68,7 +68,7 @@ mod:block {
meta = "ticks_remaining", meta = "ticks_remaining",
max_meta = "current_burn_time", max_meta = "current_burn_time",
texture = "texture-furnace", texture = "mods/default/textures/gui/furnace.png",
clip = {x = 176, y = 0, width = 14, height = 14}, clip = {x = 176, y = 0, width = 14, height = 14},
} }
@ -148,7 +148,7 @@ mod:block {
name = "img_background", name = "img_background",
pos = {x = 0, y = 0}, pos = {x = 0, y = 0},
texture = "texture-furnace", texture = "mods/default/textures/gui/furnace.png",
clip = {x = 0, y = 0, width = 176, height = 166}, clip = {x = 0, y = 0, width = 176, height = 166},
} }

View File

@ -71,7 +71,7 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
name = "img_background", name = "img_background",
pos = {x = 0, y = 0}, pos = {x = 0, y = 0},
texture = "texture-inventory", texture = "mods/default/textures/gui/inventory.png",
clip = {x = 0, y = 0, width = 176, height = 166}, clip = {x = 0, y = 0, width = 176, height = 166},
} }

View File

Before

Width:  |  Height:  |  Size: 1005 B

After

Width:  |  Height:  |  Size: 1005 B

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1005 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -83,7 +83,7 @@ mod:block {
name = "img_background", name = "img_background",
pos = {x = 0, y = 0}, pos = {x = 0, y = 0},
texture = "texture-workbench", texture = "mods/default/textures/gui/workbench.png",
clip = {x = 0, y = 0, width = 176, height = 166}, clip = {x = 0, y = 0, width = 176, height = 166},
} }

View File

@ -1,13 +1,8 @@
<textures> <textures>
<texture name="block_destroy" path="resources/textures/block_destroy.png" /> <texture name="block_destroy" path="resources/textures/block_destroy.png" />
<texture name="creative_window" path="resources/textures/creative_window.png" />
<texture name="font" path="resources/textures/font.png" /> <texture name="font" path="resources/textures/font.png" />
<texture name="furnace" path="resources/textures/furnace.png" />
<texture name="inventory" path="resources/textures/inventory.png" />
<texture name="tabs" path="resources/textures/tabs.png" />
<texture name="toasts" path="resources/textures/toasts.png" /> <texture name="toasts" path="resources/textures/toasts.png" />
<texture name="title_screen" path="resources/textures/title_screen.png" /> <texture name="title_screen" path="resources/textures/title_screen.png" />
<texture name="widgets" path="resources/textures/widgets.png" /> <texture name="widgets" path="resources/textures/widgets.png" />
<texture name="workbench" path="resources/textures/workbench.png" />
<texture name="player" path="resources/textures/player.png" /> <texture name="player" path="resources/textures/player.png" />
</textures> </textures>