Skip to content

Commit 6c8ff53

Browse files
authored
Merge pull request #50 from Savapitech/gui
feat(gui/quickmenu): add quickmenu when spacebar is pressed, can increase / decrease the speed of the game, add ibutton interface with button class for implementation of buttons
2 parents edf7121 + 4a2320b commit 6c8ff53

16 files changed

Lines changed: 251 additions & 7 deletions

File tree

gui/assets/minus.png

4.37 KB
Loading

gui/assets/plus.png

4.98 KB
Loading

gui/assets/quickMenu.png

38.5 KB
Loading

gui/assets/speedButton.png

1.18 MB
Loading

gui/src/Buttons/Button.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "Buttons/Button.hpp"
2+
#include <iostream>
3+
namespace Zappy
4+
{
5+
Button::Button(Texture &texture, float x, float y, float width, float height, std::function<void()> function) : _function(function), _width(width), _height(height), _x(x), _y(y), _hovered(false)
6+
{
7+
_sprite = std::make_unique<Sprite>(texture);
8+
_sprite->setPosition(x, y);
9+
_sprite->scale = Zappy::Math::vec3(width, height, 1.0f);
10+
11+
12+
}
13+
14+
void Button::draw(Shader &shader)
15+
{
16+
if (_sprite) {
17+
Zappy::Math::mat4 orthoProjection = Zappy::Math::ortho(0.0f, WIDTH, HEIGHT, 0.0f, -1.0f, 1.0f);
18+
Zappy::Math::mat4 view;
19+
_sprite->draw(shader, view, orthoProjection);
20+
}
21+
}
22+
23+
void Button::setPosition(float x, float y)
24+
{
25+
_x = x;
26+
_y = y;
27+
}
28+
29+
void Button::update(const std::vector<Zappy::Event> &events)
30+
{
31+
for (const auto &event : events)
32+
{
33+
if (event.type == EventType::MouseMoved){
34+
if (event.mouseX >= _x - _width / 2 && event.mouseX <= _x + _width / 2 && event.mouseY >= _y - _height / 2 && event.mouseY <= _y + _height / 2){
35+
_hovered = true;
36+
} else {
37+
_hovered = false;
38+
}
39+
}
40+
if (event.type == EventType::MousePressed && event.button == 1) {
41+
if (_hovered) {
42+
_function();
43+
}
44+
}
45+
}
46+
}
47+
}// namespace Zappy

gui/src/Buttons/Button.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include "Buttons/IButton.hpp"
3+
#include "Sprite/Sprite.hpp"
4+
#include "IScene/IScene.hpp"
5+
#include <functional>
6+
#include <memory>
7+
8+
namespace Zappy
9+
{
10+
class Button : public IButton {
11+
private:
12+
std::unique_ptr<Sprite> _sprite;
13+
std::function<void()> _function;
14+
float _width;
15+
float _height;
16+
float _x;
17+
float _y;
18+
bool _hovered;
19+
public:
20+
Button(Texture& texture, float x, float y, float width, float height, std::function<void()> function);
21+
void draw(Shader &shader);
22+
void setPosition(float x, float y);
23+
void update(const std::vector<Zappy::Event> &events);
24+
};
25+
26+
} // namespace Z

gui/src/Buttons/IButton.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include "Core/Shader/Shader.hpp"
3+
#include "Event.hpp"
4+
#include <vector>
5+
6+
namespace Zappy
7+
{
8+
class IButton {
9+
private:
10+
public:
11+
virtual ~IButton() = default;
12+
virtual void draw(Shader &shader) = 0;
13+
virtual void setPosition(float x, float y) = 0;
14+
virtual void update(const std::vector<Zappy::Event> &events) = 0;
15+
};
16+
}

gui/src/Core/Shader/ui.frag

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 330 core
2+
in vec2 TexCoords;
3+
out vec4 FragColor;
4+
5+
uniform sampler2D ourTexture;
6+
7+
void main()
8+
{
9+
vec4 texColor = texture(ourTexture, TexCoords);
10+
11+
if (texColor.a < 0.1)
12+
discard;
13+
FragColor = texColor;
14+
}

gui/src/Core/Shader/ui.vert

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 330 core
2+
layout (location = 0) in vec2 aPos;
3+
layout (location = 1) in vec2 aTexCoords;
4+
5+
out vec2 TexCoords;
6+
7+
uniform mat4 u_MVP;
8+
9+
void main()
10+
{
11+
TexCoords = vec2(aTexCoords.x, 1.0 - aTexCoords.y);
12+
gl_Position = u_MVP * vec4(aPos, 0.0, 1.0);
13+
}

gui/src/Network/INetworkClient.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class INetworkClient {
77
public:
88
virtual ~INetworkClient() = default;
99
virtual bool connectToServer(const std::string &host, int port) = 0;
10-
virtual void sendCommand(const std::string &cmd) = 0;
1110
virtual std::vector<std::string> fetchLines() = 0;
11+
virtual void sendCommand(const std::string &cmd) = 0;
1212
};

0 commit comments

Comments
 (0)