add new struct and events

This commit is contained in:
2025-04-08 15:16:20 +03:00
parent ff3122c74d
commit 1ab87dd53d
35 changed files with 264 additions and 624 deletions

View File

@@ -1,11 +1,18 @@
#pragma once
#include "event/event.hpp" // IWYU pragma: keep
#include "event/api.hpp" // IWYU pragma: keep
#include "event/event_manager.hpp" // IWYU pragma: keep
#include "event/type.hpp" // IWYU pragma: keep
#include "gui/gui.hpp" // IWYU pragma: keep
#include "gui/flags.hpp" // IWYU pragma: keep
#include "gui/api/win.hpp" // IWYU pragma: keep
#include "gui/style/icons.hpp" // IWYU pragma: keep
#include "gui/style/fonts.hpp" // IWYU pragma: keep
#include "utils/utils.hpp" // IWYU pragma: keep
#include "application/application.hpp" // IWYU pragma: keep
#include "event/event.hpp" // IWYU pragma: keep
#include "layer/layer.hpp" // IWYU pragma: keep
#include "glfw/glfw.hpp" // IWYU pragma: keep
#include "gui/gui.hpp" // IWYU pragma: keep
#include "gui/style/icons.hpp" // IWYU pragma: keep
#include "gui/style/fonts.hpp" // IWYU pragma: keep
#include "opengl/opengl.hpp" // IWYU pragma: keep

View File

@@ -1,6 +1,6 @@
#include "application.hpp"
#include "utils/utils.hpp" // IWYU pragma: keep
#include "utils/utils.hpp"
namespace VE
{

23
src/event/api.hpp Executable file
View File

@@ -0,0 +1,23 @@
#pragma once
#include "event_manager.hpp"
namespace VE
{
template <typename T>
void CONNECT(T* obj)
{
event_manager::instance().connect(obj, &T::on_event);
}
template <typename TYPE, typename DATA>
void EMIT(TYPE t, DATA d, int id = -1)
{
event_manager::instance().emit(event{ t, d, id });
}
inline void EMIT(event e)
{
event_manager::instance().emit(e);
}
}

View File

@@ -1,32 +1,9 @@
#pragma once
#include "utils/utils.hpp" // IWYU pragma: keep
#include <any>
namespace VE
{
enum class event_type
{
KEY_PRESSED,
KEY_RELEASED,
KEY_REPEATE,
MOUSE_BUTTON_PRESSED,
MOUSE_BUTTON_DOUBLE_PRESSED,
MOUSE_BUTTON_RELEASED,
MOUSE_CURSOR_POSITION,
MOUSE_SCROLL,
WINDOW_RESIZE,
WINDOW_CLOSE,
WINDOW_FOCUS,
};
enum class event_target
{
VE
};
struct event
{
event(std::any type, std::any data, int id) : m_type{ type }, m_data{ data }, m_id { id } {}
@@ -36,52 +13,4 @@ namespace VE
std::any m_data;
int m_id;
};
struct event_manager : public hack::patterns::singleton<event_manager>
{
std::vector<VE::event_fn<VE::event>> m_funcs;
template<typename T>
void connect(T* obj, void (T::*method)(event))
{
m_funcs.push_back(std::bind(method, obj, std::placeholders::_1));
}
void emit(event e)
{
try
{
for (std::size_t i = 0; i < m_funcs.size(); ++i)
{
m_funcs[i](e);
}
}
catch(const std::exception& ex)
{
hack::error()(ex.what());
hack::log()("size funcs", m_funcs.size());
}
catch(...)
{
hack::error()("ooops...");
}
}
};
template <typename T>
void CONNECT(T* obj)
{
VE::event_manager::instance().connect(obj, &T::on_event);
}
template <typename TYPE, typename DATA>
void EMIT(TYPE t, DATA d, int id = -1)
{
VE::event_manager::instance().emit(VE::event{ t, d, id });
}
inline void EMIT(VE::event e)
{
VE::event_manager::instance().emit(e);
}
}

41
src/event/event_manager.hpp Executable file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <hack/patterns/singleton.hpp>
#include <hack/logger/logger.hpp>
#include "event.hpp"
#include "utils/using.hpp"
namespace VE
{
struct event_manager : public hack::patterns::singleton<event_manager>
{
std::vector<event_fn<event>> m_funcs;
template<typename T>
void connect(T* obj, void (T::*method)(event))
{
m_funcs.push_back(std::bind(method, obj, std::placeholders::_1));
}
void emit(event e)
{
try
{
for (std::size_t i = 0; i < m_funcs.size(); ++i)
{
m_funcs[i](e);
}
}
catch(const std::exception& ex)
{
hack::error()(ex.what());
hack::log()("size funcs", m_funcs.size());
}
catch(...)
{
hack::error()("ooops...");
}
}
};
}

28
src/event/type.hpp Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
namespace VE
{
enum class event_type
{
KEY_PRESSED,
KEY_RELEASED,
KEY_REPEATE,
MOUSE_BUTTON_PRESSED,
MOUSE_BUTTON_DOUBLE_PRESSED,
MOUSE_BUTTON_RELEASED,
MOUSE_CURSOR_POSITION,
MOUSE_SCROLL,
WINDOW_RESIZE,
WINDOW_CLOSE,
WINDOW_FOCUS,
};
enum class event_target
{
VE
};
}

View File

@@ -1,6 +1,7 @@
#include "glfw.hpp"
#include "event/event.hpp"
#include "event/type.hpp"
#include "event/api.hpp"
namespace VE
{

View File

@@ -8,8 +8,6 @@ namespace VE
class glfw
{
public:
// HERE
// реализовать остальные конструкторы
glfw();
~glfw();

43
src/gui/api/win.hpp Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include <string>
#include <imgui.h>
namespace VE
{
inline void WinPos(ImVec2 pos)
{
ImGui::SetNextWindowPos(pos);
}
inline void WinSize(ImVec2 size)
{
ImGui::SetNextWindowSize(size);
}
inline void End()
{
ImGui::End();
}
inline void Begin(std::string name, ImVec2 pos, ImVec2 size, bool& open, ImGuiWindowFlags win_flags)
{
WinPos(pos);
WinSize(size);
if (!ImGui::Begin(name.c_str(), &open, win_flags)) End();
}
inline void Begin(std::string name, ImVec2 pos, ImVec2 size, ImGuiWindowFlags win_flags)
{
WinPos(pos);
WinSize(size);
if (!ImGui::Begin(name.c_str(), nullptr, win_flags)) End();
}
inline void Begin(std::string name, ImGuiWindowFlags win_flags)
{
if (!ImGui::Begin(name.c_str(), nullptr, win_flags)) End();
}
}

View File

@@ -8,20 +8,20 @@ namespace VE
{
flags()
{
if (m_no_titlebar) m_window_flags |= ImGuiWindowFlags_NoTitleBar;
if (m_no_scrollbar) m_window_flags |= ImGuiWindowFlags_NoScrollbar;
if (!m_no_menu) m_window_flags |= ImGuiWindowFlags_MenuBar;
if (m_no_move) m_window_flags |= ImGuiWindowFlags_NoMove;
if (m_no_resize) m_window_flags |= ImGuiWindowFlags_NoResize;
if (m_no_collapse) m_window_flags |= ImGuiWindowFlags_NoCollapse;
if (m_no_nav) m_window_flags |= ImGuiWindowFlags_NoNav;
if (m_no_background) m_window_flags |= ImGuiWindowFlags_NoBackground;
if (m_no_bring_to_front) m_window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus;
if (m_no_titlebar) m_win_flags |= ImGuiWindowFlags_NoTitleBar;
if (m_no_scrollbar) m_win_flags |= ImGuiWindowFlags_NoScrollbar;
if (!m_no_menu) m_win_flags |= ImGuiWindowFlags_MenuBar;
if (m_no_move) m_win_flags |= ImGuiWindowFlags_NoMove;
if (m_no_resize) m_win_flags |= ImGuiWindowFlags_NoResize;
if (m_no_collapse) m_win_flags |= ImGuiWindowFlags_NoCollapse;
if (m_no_nav) m_win_flags |= ImGuiWindowFlags_NoNav;
if (m_no_background) m_win_flags |= ImGuiWindowFlags_NoBackground;
if (m_no_bring_to_front) m_win_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus;
}
virtual ~flags() = default;
bool m_p_open = false;
bool m_open = false;
bool m_no_titlebar = true;
bool m_no_scrollbar = true;
@@ -34,6 +34,6 @@ namespace VE
bool m_no_bring_to_front = false;
bool m_no_docking = true;
ImGuiWindowFlags m_window_flags = 0;
ImGuiWindowFlags m_win_flags = 0;
};
}

View File

@@ -1,14 +1,12 @@
#pragma once
#include "event/event.hpp"
#include "gui/flags.hpp"
#include "utils/using.hpp"
namespace VE
{
class layer : public flags
class layer
{
using time = time::timestep<float>;
public:
layer() = default;
virtual ~layer() = default;

View File

@@ -17,7 +17,7 @@ headers = [
'utils/define.hpp',
'utils/include.hpp',
'utils/keycode.hpp',
'utils/time.hpp',
# 'utils/time.hpp',
'utils/using.hpp',
'utils/var.hpp',
'utils/utils.hpp',

View File

@@ -3,7 +3,7 @@
#define BIT(x)\
(1 << x)
#define VE_FN_OVERIDE()\
#define VE_OVERIDE()\
public:\
void on_attach() override;\
void render() override;\

View File

@@ -24,6 +24,7 @@
#include <glad.h>
#include <GLFW/glfw3.h>
#include <hack/logger/logger.hpp>
#include <hack/patterns/singleton.hpp>
#include <imgui.h>

View File

@@ -1,26 +1,27 @@
#pragma once
#include "using.hpp" // IWYU pragma: keep
#include <glad.h>
#include <GLFW/glfw3.h>
namespace VE::time
namespace VE
{
template<typename Time>
template<typename Type = float>
class timestep
{
public:
timestep(Time data_ = 0.0f) : data { data_ } {}
timestep(Type data_ = 0.0f) : data { data_ } {}
operator Time() const { return data; }
operator Type() const { return data; }
Time get_seconds() const { return data; }
Time get_milliseconds() const { return data * 1000.0f; }
Type get_seconds() const { return data; }
Type get_milliseconds() const { return data * 1000.0f; }
private:
Time data;
Type data;
};
}
namespace VE::time
namespace VE
{
inline timestep<float> get_time()
{
@@ -28,7 +29,6 @@ namespace VE::time
float t = (float)glfwGetTime();
timestep<float> ts = t - frame_time;
frame_time = t;
return ts;
}
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include "include.hpp" // IWYU pragma: keep
#include <functional>
namespace VE
{