Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ECS/include/button/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ class Button {
bool pressed() const { return isPressed; }
bool active() const { return isActive; }

sf::Text labelText;

protected:
// State flags
bool isHover = false;
bool isPressed = false;
bool isActive = true;

// Label
sf::Text labelText;

// Helpers
virtual bool contains(const sf::Vector2f& point) const = 0;
Expand Down
2 changes: 1 addition & 1 deletion ECS/include/button/EllipseButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EllipseButton : public Button {
isHover = contains(mouse);
shape.setFillColor(isHover ? hoverColor : idleColor);

if (isHover && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
if (isHover && mouse_pressed) {
isPressed = true;
shape.setFillColor(pressedColor);
}
Expand Down
4 changes: 2 additions & 2 deletions ECS/include/button/RectButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RectButton: public Button {
isHover = contains(mouse);
shape.setFillColor(isHover ? hoverColor : idleColor);

if (isHover && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
if (isHover && mouse_pressed) {
isPressed = true;
shape.setFillColor(pressedColor);
}
Expand All @@ -43,8 +43,8 @@ class RectButton: public Button {
target.draw(labelText);
}

private:
sf::RectangleShape shape;
private:

sf::Color idleColor = sf::Color(200, 200, 200);
sf::Color hoverColor = sf::Color(170, 170, 170);
Expand Down
21 changes: 19 additions & 2 deletions ECS/include/components/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#include "basics.hpp"
#include "button/RectButton.hpp"
#include "entity.hpp"
#include "text_field/RectTextField.hpp"
#include <SFML/Window/Keyboard.hpp>
#include <cstddef>
#include <optional>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <memory>
#include <queue>

namespace comp {
using controllable_signals = sf::Keyboard::Scancode;
using controllable_signals = sf::Keyboard::Key;

struct controllable: public signal_handler<controllable_signals, registry &> {
controllable(const signal_handler::signals &signals = {}): signal_handler(signals) {}
Expand All @@ -29,7 +32,7 @@ namespace comp {
window(unsigned size_x, unsigned size_y, const std::string& title, int refresh_rate = 120):
std::shared_ptr<sf::RenderWindow>(std::make_shared<sf::RenderWindow>(
sf::VideoMode({size_x, size_y}), title
)), mouse_pressed(std::make_shared<bool>(false)), _refresh_rate(refresh_rate) {}
)), keys_list(std::make_shared<std::queue<char>>()) ,mouse_pressed(std::make_shared<bool>(false)), _refresh_rate(refresh_rate) {}

bool need_refresh() {
if (_clock.getElapsedTime() >= sf::seconds(1.0f / this->_refresh_rate)) {
Expand All @@ -40,6 +43,7 @@ namespace comp {
}

std::unordered_set<controllable_signals> keys;
std::shared_ptr<std::queue<char>> keys_list;
std::optional<sf::Event> event;
std::shared_ptr<bool> mouse_pressed;
private:
Expand Down Expand Up @@ -157,6 +161,13 @@ namespace comp {
bool active = false;
};

struct text_field{
std::string name;
RectTextField field;
bool visible;
bool active = false;
};

struct settingsButton {};

struct writable {
Expand Down Expand Up @@ -195,3 +206,9 @@ void button_system(registry &,
sparse_array<comp::button> &,
sparse_array<comp::window> &,
sparse_array<comp::enabled> const &);

void text_field_system(registry &,
sparse_array<comp::position> const &,
sparse_array<comp::text_field> &,
sparse_array<comp::window> &,
sparse_array<comp::enabled> const &);
16 changes: 16 additions & 0 deletions ECS/include/network/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ class client {
networkThread.join();
}

client(client const &) = delete;
client &operator=(client const &) = delete;
client(client &&) = delete;

client &operator=(client &&other) {
if (this == &other)
return *this;

this->~client();
new (this) client(
other.server_endpoint.address().to_string(),
other.server_endpoint.port()
);
return *this;
}

void send(size_t id, uint16_t protocol, uint16_t type, uint16_t size, void const *data, uint16_t uid = 0) {
packet_handler(id, protocol, type, size, data)
.send(this->socket, this->server_endpoint, this->clients_endp, this->pendingPackets, uid);
Expand Down
4 changes: 2 additions & 2 deletions ECS/include/network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ struct clientData {
namespace net {
struct Header {
size_t id;
u_int16_t protocol;
u_int16_t safeUid;
uint16_t protocol;
uint16_t safeUid;
uint16_t size;
uint16_t type;
};
Expand Down
2 changes: 1 addition & 1 deletion ECS/include/network/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class server {
}

void startReceive() {
static u_int16_t safeUdpId = 1;
static uint16_t safeUdpId = 1;

this->socket.async_receive_from(
boost::asio::buffer(this->recvBuffer.data(), 1024),
Expand Down
86 changes: 86 additions & 0 deletions ECS/include/text_field/RectTextField.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
** EPITECH PROJECT, 2026
** R-TypeTEst [WSL: Ubuntu]
** File description:
** RectTextField
*/

#pragma once
#include "TextField.hpp"
#include <SFML/Window/Keyboard.hpp>
#include <iostream>

class RectTextField : public TextField {
public:
RectTextField(const sf::Font& font, const sf::Vector2f& size,
const sf::Vector2f& position) : TextField(font) {
box.setSize(size);
box.setPosition(position);
box.setFillColor(sf::Color::White);
box.setOutlineThickness(2.f);
box.setOutlineColor(sf::Color(120,120,120));

cursor.setFillColor(sf::Color::Black);
updateLayout();
}

void setColors(const sf::Color& idle, const sf::Color& focused) {
idleColor = idle;
focusedColor = focused;
box.setOutlineColor(idleColor);
}

void check_focus(const sf::RenderWindow& window, const bool mouse_pressed) {
if (mouse_pressed) {
auto mouse = sf::Vector2f(sf::Mouse::getPosition(window));
isFocused = contains(mouse);
box.setOutlineColor(isFocused ? focusedColor : idleColor);
}
}

void update(const sf::RenderWindow& window, const bool mouse_pressed, char key) override {
if (mouse_pressed) {
auto mouse = sf::Vector2f(sf::Mouse::getPosition(window));
isFocused = contains(mouse);
box.setOutlineColor(isFocused ? focusedColor : idleColor);
}
if (!isFocused)
return;
if (key >= 32 && key < 127) {
if (value.size() < maxLength) {
value += key;
text.setString(value);
updateCursor();
}
} else if (key == 0 and !value.empty()) {
value.pop_back();
text.setString(value);
updateCursor();
}
}

void draw(sf::RenderTarget& target) const override {
target.draw(box);
if (value.empty() and hasPlaceholder and !isFocused)
target.draw(placeholder);
else
target.draw(text);
if (isFocused)
target.draw(cursor);
}

private:

bool contains(const sf::Vector2f& point) const override {
return box.getGlobalBounds().contains(point);
}

sf::Vector2f getTextPosition() const override {
return box.getPosition() + sf::Vector2f(8.f, 6.f);
}

sf::RectangleShape box;

sf::Color idleColor = sf::Color(120,120,120);
sf::Color focusedColor = sf::Color(80,120,255);
};
94 changes: 94 additions & 0 deletions ECS/include/text_field/TextField.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
** EPITECH PROJECT, 2026
** R-TypeTEst [WSL: Ubuntu]
** File description:
** TextField
*/

#pragma once
#include <SFML/Graphics.hpp>
#include <string>

using Key = sf::Keyboard::Key;

class TextField {
public:
explicit TextField(const sf::Font& font) : text(font), placeholder(font) {}

virtual ~TextField() = default;

virtual void update(const sf::RenderWindow& window,
const bool mouse_pressed, char key) = 0;

virtual void draw(sf::RenderTarget& target) const = 0;

void setTextSize(unsigned int size) {
text.setCharacterSize(size);
placeholder.setCharacterSize(size);
updateLayout();
}

void setTextColor(const sf::Color& color) { text.setFillColor(color); }

void setText(const std::string &str) {
this->value = str;
}

void setPlaceholder(const std::string& str,
const sf::Color& color = sf::Color(150,150,150)) {
placeholder.setString(str);
placeholder.setFillColor(color);
hasPlaceholder = true;
updateLayout();
}

const std::string& getText() const {
return value;
}

void clear() {
value.clear();
text.setString("");
}

void setMaxLength(std::size_t max) { maxLength = max; }

bool focused() const { return isFocused; }

protected:
virtual bool contains(const sf::Vector2f& point) const = 0;
virtual sf::Vector2f getTextPosition() const = 0;

void updateLayout() {
text.setPosition(getTextPosition());
placeholder.setPosition(getTextPosition());

cursor.setSize({1.f, static_cast<float>(text.getCharacterSize())});
cursor.setPosition(text.getPosition());
}

void updateCursor() {
auto bounds = text.getGlobalBounds();
cursor.setPosition({
bounds.position.x + bounds.size.x + 2.f,
bounds.position.y
});
}

bool activeMaj() {
return sf::Keyboard::isKeyPressed(Key::LShift)
or sf::Keyboard::isKeyPressed(Key::RShift);
}

bool isFocused = false;
bool hasPlaceholder = false;

std::size_t maxLength = 64;
std::string value;

sf::Text text;
sf::Text placeholder;

sf::RectangleShape cursor;
bool showCursor = true;
};
19 changes: 19 additions & 0 deletions ECS/src/systems/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "components/graphics.hpp"
#include "iterator.hpp"
#include "registry.hpp"
#include "text_field/TextField.hpp"

void draw_system(registry const &,
sparse_array<comp::position> const &positions,
Expand Down Expand Up @@ -70,3 +71,21 @@ void button_system(registry &reg,
}
}
}

void text_field_system(registry &,
sparse_array<comp::position> const &positions,
sparse_array<comp::text_field> &fields,
sparse_array<comp::window> &windows,
sparse_array<comp::enabled> const &enabled) {
for (auto &&[window, _]: zipper(windows, enabled))
for (auto &&[i, pos, field, _]: indexed_zipper(positions, fields, enabled)) {
if (!field.visible)
continue;
field.field.draw(*window);
field.field.check_focus(*window, *window.mouse_pressed);
while (field.field.focused() and !window.keys_list->empty()) {
field.field.update(*window, *window.mouse_pressed, window.keys_list->front());
window.keys_list->pop();
}
}
}
2 changes: 2 additions & 0 deletions client/include/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ enum Type {

enum State {
LOBBY,
CONNECT,
WAITING,
GAME,
END
Expand Down Expand Up @@ -110,6 +111,7 @@ namespace comp {
void setup_lobby(registry &, window &);
void waiting_setup_entity(registry &, window &, size_t &);
void end_setup_entity(registry &, window &);
void connect_setup_entity(registry &, window &);
RectButton createDefaultButton(const std::string &, const std::string &,
const sf::Vector2f &, const sf::Vector2f &);
}
8 changes: 7 additions & 1 deletion client/include/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

#include "components/graphics.hpp"

struct pressed{
enum ButtonType {
GO,
BACK
};

struct pressed {
bool pressed;
ButtonType type;
};

namespace comp {
Expand Down
Loading