Skip to content

Commit

Permalink
Add first button
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 11, 2024
1 parent 79afecb commit b2cfb7d
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 22 deletions.
Binary file added client/assets/icons/house.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions client/compile_flags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-Isrc
-Isrc/menu/firstmenu
-Isrc/utils
-Ibuild/_deps/date-src/include
-Ibuild/_deps/raylibcpp-src/include
-std=c++20
3 changes: 2 additions & 1 deletion client/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/Logger.cpp
)

add_subdirectory(firstmenu)
add_subdirectory(utils)
add_subdirectory(menu)
21 changes: 0 additions & 21 deletions client/src/firstmenu/FirstMenu.cpp

This file was deleted.

3 changes: 3 additions & 0 deletions client/src/menu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.15)

add_subdirectory(firstmenu)
File renamed without changes.
33 changes: 33 additions & 0 deletions client/src/menu/firstmenu/FirstMenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "FirstMenu.hpp"
#include <algorithm>
#include <memory>
#include "PathResolver.hpp"
#include "Logger.hpp"

FirstMenu::FirstMenu(raylib::Window & /*unused*/) : _icon(PathResolver::resolve("assets/icons/favicon.png"))
{
_buttons["ok"] = std::make_unique<Button>(raylib::Vector2(0, 0), PathResolver::resolve("assets/icons/house.png"), "test");
}

void FirstMenu::update(raylib::Window &window)
{
for (const auto &button : _buttons) {
if (button.first == "ok" && button.second->isClicked(window)) {
Logger::error("TEST");
}
}
}

void FirstMenu::draw(raylib::Window &window)
{
_icon.Draw(
window.GetWidth() / 2 - FirstMenu::_icon.GetWidth() / 2,
window.GetHeight() / 2 - FirstMenu::_icon.GetHeight() / 2);
for (const auto &button : _buttons) {
button.second->draw(window);
}
}

void FirstMenu::free(raylib::Window & /*unused*/)
{
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <memory>
#include <unordered_map>
#include "raylib-cpp.hpp"
#include "Button.hpp"

class FirstMenu {
public:
Expand All @@ -11,4 +14,5 @@ class FirstMenu {

private:
raylib::Texture _icon;
std::unordered_map<std::string, std::unique_ptr<Button>> _buttons;
};
46 changes: 46 additions & 0 deletions client/src/utils/Button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Button.hpp"
#include "Logger.hpp"

Button::Button(raylib::Vector2 position, std::string texture, std::string text)
: _position(position),
_texture(texture)
{
_text.text = text;
}

bool Button::isClicked(raylib::Window &window) const
{
if (!window.IsFocused()) {
return false;
}
if (!raylib::Mouse::IsButtonPressed(MOUSE_BUTTON_LEFT)) {
return false;
}
const auto x = raylib::Mouse::GetX();
const auto y = raylib::Mouse::GetY();
const auto size_y = _texture.GetHeight();
const auto size_x = _texture.GetWidth();
if (y < _position.GetY() || x < _position.GetX() || y > _position.GetY() + size_y
|| x > _position.GetX() + size_x) {
return false;
}
return true;
}

void Button::draw(raylib::Window & /*unused*/) const
{
_texture.Draw(_position);
const auto size_text = _text.MeasureEx();
auto pos_text = _position;
pos_text.SetX(pos_text.GetX() + (size_text.GetX() / 2));
pos_text.SetY(pos_text.GetY() + (size_text.GetY() / 2));
_text.Draw(pos_text);
}

Button &Button::operator=(const Button &src)
{
this->_text.text = src._text.text;
this->_position = src._position;
this->_texture = raylib::Texture(src._texture.GetData());
return *this;
}
21 changes: 21 additions & 0 deletions client/src/utils/Button.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <list>
#include <string>
#include <vector>
#include "raylib-cpp.hpp"

class Button {
public:
Button(raylib::Vector2 position, std::string texture, std::string text);

bool isClicked(raylib::Window &window) const;
void draw(raylib::Window &window) const;

Button &operator=(const Button &src);

private:
raylib::Vector2 _position;
raylib::Texture _texture;
raylib::Text _text;
};
13 changes: 13 additions & 0 deletions client/src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Button.cpp
)

0 comments on commit b2cfb7d

Please sign in to comment.