Update the inventory - fix the size of inventory boxes.

master
Nicole Collings 2019-08-01 02:19:57 -07:00
parent 8c2c842373
commit d0ae343543
13 changed files with 76 additions and 46 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

View File

@ -15,63 +15,67 @@ GameGui::GameGui(glm::vec2 bufferSize, TextureAtlas& atlas) {
add(viginette);
auto root = std::make_shared<GUIRect>("root");
root->create(bufferSize, {}, {0, 0, 0, 0.15});
root->create(bufferSize, {}, {0, 0, 0, 0.25});
add(root);
auto inv_root = std::make_shared<GUIRect>("inv_root");
inv_root->create({576, 252}, {60, 30, 12, 30}, atlas.getTextureRef("inventory"));
inv_root->setPos({bufferSize.x / 2 - 312, bufferSize.y / 2 - 132});
inv_root->create({648, 270}, {60, 30, 24, 30}, atlas.getTextureRef("inventory"));
inv_root->setPos({bufferSize.x / 2 - 354, bufferSize.y / 2 - 90});
root->add(inv_root);
std::array<std::string, 6> mats = {
"materials_stick",
"materials_rock",
"materials_flint",
"basictools_flint_pickaxe",
"basictools_flint_hatchet",
"basictools_flint_shovel"
"materials_stick",
"materials_rock",
"materials_flint",
"basictools_flint_pickaxe",
"basictools_flint_hatchet",
"basictools_flint_shovel"
};
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 12; j++) {
auto inv_slot = std::make_shared<GUIRect>("inv_slot" + to_string(i) + to_string(j));
auto inv_slot = std::make_shared<GUIRect>("inv_slot_" + to_string(i) + "_" + to_string(j));
inv_slot->create({48, 48}, {}, atlas.getTextureRef(mats[rand() % 6]));
inv_slot->setPos({j * 48, i * 48});
inv_slot->setPos({3 + j * 54, 3 + i * 54});
inv_root->add(inv_slot);
}
}
auto shortcuts_root = std::make_shared<GUIRect>("shortcut_root");
shortcuts_root->create({156, 156}, {}, atlas.getTextureRef("inventory_wheel"));
shortcuts_root->setPos({bufferSize.x / 2 - 72, bufferSize.y / 2 + 204});
shortcuts_root->setPos({bufferSize.x / 2 - 72, bufferSize.y - 200});
root->add(shortcuts_root);
std::array<glm::vec2, 6> vec_roots = {
glm::vec2 {133, -11},
glm::vec2 {149, 46},
glm::vec2 {133, 103},
glm::vec2 {-216, -11},
glm::vec2 {-232, 46},
glm::vec2 {-216, 103}
glm::vec2 { 133, -26 },
glm::vec2 { 155, 37 },
glm::vec2 { 133, 100},
glm::vec2 {-241, -26 },
glm::vec2 {-263, 37 },
glm::vec2 {-241, 100}
};
for (auto i = 0; i < 6; i++) {
glm::vec2 vec = vec_roots[i];
// for (auto j = 0; j < 4; j++) {
// auto shortcut_slot = std::make_shared<GUIRect>("shortcut_slot" + to_string(i) + to_string(j));
// shortcut_slot->create({48, 48}, {}, atlas.getTextureRef("inventory_slot"));
// shortcut_slot->setPos(vec + glm::vec2 {j * 48, 0});
// shortcuts_root->add(shortcut_slot);
// }
auto shortcut_slot = std::make_shared<GUIRect>("shortcut_slot" + to_string(i));
shortcut_slot->create({240, 72}, {}, atlas.getTextureRef("inventory_wheel_slot"));
auto shortcut_slot = std::make_shared<GUIRect>("shortcut_slot_" + to_string(i));
shortcut_slot->create({216, 54}, {15, 24, 15, 24}, atlas.getTextureRef("inventory_wheel_slot"));
shortcut_slot->setPos(vec);
shortcuts_root->add(shortcut_slot);
}
root->setVisible(false);
}
void GameGui::bufferResized(glm::vec2 bufferSize) {
get<GUIRect>("crosshair")->setPos({bufferSize.x / 2 - 11, bufferSize.y / 2 - 9});
get<GUIRect>("viginette")->setScale({bufferSize.x, bufferSize.y});
}
void GameGui::setInventoryVisible(bool visible) {
get<GUIRect>("root")->setVisible(visible);
}
bool GameGui::isInventoryVisible() {
return get<GUIRect>("root")->isVisible();
}

View File

@ -15,6 +15,9 @@ class GameGui : public GUIContainer {
public:
explicit GameGui(glm::vec2 bufferSize, TextureAtlas& atlas);
void bufferResized(glm::vec2 bufferSize);
void setInventoryVisible(bool visible);
bool isInventoryVisible();
};

View File

@ -35,7 +35,7 @@ void GUIComponent::setPos(glm::vec2 pos) {
}
entity.setPos({pos.x, pos.y, 0});
for (const auto& child : children) {
child.second->updatePos();
child->updatePos();
}
}
@ -46,17 +46,22 @@ glm::vec2 GUIComponent::getPos() {
void GUIComponent::add(std::shared_ptr<GUIComponent> component) {
component->parent = this;
component->updatePos();
children[component->key] = std::move(component);
children.push_back(std::move(component));
}
void GUIComponent::remove(std::string key) {
children.erase(key);
for (auto it = children.cbegin(); it != children.cend(); it++) {
if (it->get()->key == key) {
children.erase(it);
return;
}
}
}
void GUIComponent::draw(Renderer& renderer) {
entity.draw(renderer);
for (const auto& child : children) {
child.second->draw(renderer);
child->draw(renderer);
}
}
@ -64,7 +69,7 @@ void GUIComponent::setVisible(bool visible) {
Drawable::setVisible(visible);
entity.setVisible(visible);
for (const auto& child : children) {
child.second->setVisible(visible);
child->setVisible(visible);
}
}
@ -77,6 +82,6 @@ void GUIComponent::updatePos() {
}
entity.setPos({realPos.x, realPos.y, 0});
for (const auto& child : children) {
child.second->updatePos();
child->updatePos();
}
}

View File

@ -7,7 +7,8 @@
#include <memory>
#include <unordered_map>
#include <list>
//#include <unordered_map>
#include "../../Entity.h"
class GUIComponent : public Drawable {
@ -25,7 +26,13 @@ public:
virtual glm::vec2 getPos();
void add(std::shared_ptr<GUIComponent> component);
template<class T> std::shared_ptr<T> get(const std::string &key) { return std::static_pointer_cast<T>(children[key]); };
template<class T> std::shared_ptr<T> get(const std::string &key) {
for (auto &it : children) {
if (it.get()->key == key) {
return std::static_pointer_cast<T>(it);
}
}
};
void remove(std::string key);
void setVisible(bool visible) override;
@ -34,7 +41,8 @@ public:
protected:
std::string key = "";
GUIComponent* parent = nullptr;
std::unordered_map<std::string, std::shared_ptr<GUIComponent>> children;
std::list<std::shared_ptr<GUIComponent>> children;
// std::unordered_map<std::string, std::shared_ptr<GUIComponent>> children;
glm::vec2 pos {};
glm::vec2 scale {};
@ -47,4 +55,5 @@ private:
};
#endif //ZEUS_GUICOMPONENT_H

View File

@ -8,6 +8,6 @@ GUIContainer::GUIContainer(const std::string &key) : GUIComponent(key) {}
void GUIContainer::draw(Renderer &renderer) {
for (const auto& child : children) {
child.second->draw(renderer);
child->draw(renderer);
}
}

View File

@ -17,20 +17,20 @@ void GUILabelledGraph::create(glm::vec2 scale, glm::vec4 padding, const std::str
this->graphTextureRef = std::move(graphTextureRef);
this->fontTextureRef = std::move(fontTextureRef);
auto label = std::make_shared<GUIText>("label");
label->create({2, 2}, {}, {}, {}, this->fontTextureRef);
add(label);
label->setPos({TEXT_PAD_X, TEXT_PAD_Y});
auto background = std::make_shared<GUIRect>("background");
background->create(scale, {}, {0.1, 0.1, 0.1, 0.2}, {0.1, 0.1, 0.1, 0.2}, {0.1, 0.1, 0.1, 0.7}, {0.1, 0.1, 0.1, 0.7});
add(background);
background->setPos({0, 0});
auto graph = std::make_shared<GUIGraph>("graph");
graph->create({scale.x / (graphLength + GRAPH_PAD_X), scale.y * 0.4}, {}, this->graphTextureRef, graphLength, graphScale, true);
add(graph);
graph->setPos({GRAPH_PAD_X, GRAPH_PAD_Y});
auto background = std::make_shared<GUIRect>("background");
background->create(scale, {}, {0.1, 0.1, 0.1, 0.2}, {0.1, 0.1, 0.1, 0.2}, {0.1, 0.1, 0.1, 0.7}, {0.1, 0.1, 0.1, 0.7});
add(background);
background->setPos({0, 0});
auto label = std::make_shared<GUIText>("label");
label->create({2, 2}, {}, {}, {}, this->fontTextureRef);
add(label);
label->setPos({TEXT_PAD_X, TEXT_PAD_Y});
for (float &i : history) i = 0;
}

View File

@ -27,8 +27,8 @@ Renderer::Renderer(GLint winWidth, GLint winHeight) :
createWorldShaders();
createGUIShader();
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

View File

@ -161,6 +161,9 @@ void Window::swapBuffers() {
void Window::lockMouse(bool lock) {
forceMouseUnlocked = !lock;
if (lock) mouseIsLocked = true;
mouseIsLocked = lock;
glfwSetCursorPos(mainWindow, centerX, centerY);
glfwSetInputMode(mainWindow, GLFW_CURSOR, (mouseIsLocked ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL));
deltaX = 0;
deltaY = 0;
}

View File

@ -66,6 +66,12 @@ void GameScene::update() {
debugVisible = !debugVisible;
debugGui.changeVisibilityState(hudVisible ? debugVisible ? 0 : 2 : 1);
}
if (window.input.isKeyPressed(GLFW_KEY_E)) {
bool open = !gameGui.isInventoryVisible();
gameGui.setInventoryVisible(open);
window.lockMouse(!open);
}
}
void GameScene::draw() {