Reworked the ogfx Window subsystem

This commit is contained in:
OmniaX-Dev 2026-03-31 04:55:17 +02:00
parent 2bec8e1b39
commit 1d071786d5
22 changed files with 748 additions and 929 deletions

View file

@ -87,8 +87,8 @@ list(APPEND OGFX_SOURCE_FILES
# gui # gui
${CMAKE_CURRENT_LIST_DIR}/src/ogfx/gui/RawTextInput.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/gui/RawTextInput.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ogfx/gui/WindowBase.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/gui/Window.cpp
${CMAKE_CURRENT_LIST_DIR}/src/ogfx/gui/WindowBaseOutputHandler.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/gui/WindowOutputHandler.cpp
# render # render
${CMAKE_CURRENT_LIST_DIR}/src/ogfx/render/BasicRenderer.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/render/BasicRenderer.cpp
@ -102,7 +102,8 @@ list(APPEND OGFX_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/src/ogfx/utils/Animation.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/utils/Animation.cpp
) )
list(APPEND TEST_SOURCE_FILES list(APPEND TEST_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/src/test.cpp # ${CMAKE_CURRENT_LIST_DIR}/src/test/GraphicsWindowTest.cpp
${CMAKE_CURRENT_LIST_DIR}/src/test/GuiTest.cpp
) )
#----------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------

View file

@ -1 +1 @@
2038 2039

76
src/ogfx/gui/Events.hpp Normal file
View file

@ -0,0 +1,76 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ostd/data/BaseObject.hpp>
namespace ogfx
{
class WindowCore;
class WindowResizedData : public ostd::BaseObject
{
public:
inline WindowResizedData(WindowCore& parent, int32_t _oldx, int32_t _oldy, int32_t _newx, int32_t _newy) : parentWindow(parent), old_width(_oldx), old_height(_oldy), new_width(_newx), new_height(_newy)
{
setTypeName("ogfx::WindowResizedData");
validate();
}
public:
int32_t new_width;
int32_t new_height;
int32_t old_width;
int32_t old_height;
WindowCore& parentWindow;
};
class MouseEventData : public ostd::BaseObject
{
public: enum class eButton { None = 0, Left, Middle, Right };
public:
inline MouseEventData(WindowCore& parent, float mousex, float mousey, eButton btn) : parentWindow(parent), position_x(mousex), position_y(mousey), button(btn)
{
setTypeName("ogfx::MouseEventData");
validate();
}
public:
float position_x;
float position_y;
eButton button;
WindowCore& parentWindow;
};
class KeyEventData : public ostd::BaseObject
{
public: enum class eKeyEvent { Pressed = 0, Released, Text };
public:
inline KeyEventData(WindowCore& parent, int32_t key, char _text, eKeyEvent evt) : parentWindow(parent), keyCode(key), text(_text), eventType(evt)
{
setTypeName("ogfx::KeyEventData");
validate();
}
public:
int32_t keyCode;
char text;
eKeyEvent eventType;
WindowCore& parentWindow;
};
}

View file

@ -1,211 +0,0 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#include "GuiWindow.hpp"
namespace ogfx
{
namespace gui
{
Window::~Window(void)
{
onDestroy();
SDL_DestroyCursor(m_cursor_IBeam);
SDL_DestroyCursor(m_cursor_Arrow);
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
TTF_Quit();
}
void Window::initialize(int32_t width, int32_t height, const ostd::String& windowTitle)
{
if (m_initialized) return;
m_windowWidth = width;
m_windowHeight = height;
m_title = windowTitle;
if (!SDL_Init(SDL_INIT_VIDEO))
{
printf( "SDL could not initialize! Error: %s\n", SDL_GetError() );
exit(ErrorCodes::SDLInitFailed);
}
m_window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOW_RESIZABLE);
setSize(m_windowWidth, m_windowHeight);
m_renderer = SDL_CreateRenderer(m_window, nullptr);
SDL_SetWindowMinimumSize(m_window, m_windowWidth, m_windowHeight);
SDL_SetWindowTitle(m_window, m_title.c_str());
SDL_StartTextInput(m_window);
m_cursor_Arrow = SDL_CreateSystemCursor(SDL_SystemCursor::SDL_SYSTEM_CURSOR_DEFAULT);
m_cursor_IBeam = SDL_CreateSystemCursor(SDL_SystemCursor::SDL_SYSTEM_CURSOR_TEXT);
m_initialized = true;
m_running = true;
setTypeName("ogfx::gui::Window");
enableSignals(true);
validate();
onInitialize();
}
void Window::close(void)
{
m_running = false;
onClose();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::RealTime, *this);
}
int32_t Window::startMainLoop(void)
{
if (!m_initialized) return ErrorCodes::Uninitialized;
while (m_running)
{
__handle_events();
SDL_SetRenderDrawColor(m_renderer, m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
SDL_RenderClear(m_renderer);
SDL_RenderPresent(m_renderer);
}
return ErrorCodes::ExitSuccess;
}
void Window::setSize(int32_t width, int32_t height)
{
if (!isInitialized()) return;
SDL_SetWindowSize(m_window, width, height);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowResized, ostd::tSignalPriority::RealTime);
}
void Window::setTitle(const ostd::String& title)
{
if (!isInitialized()) return;
m_title = title;
SDL_SetWindowTitle(m_window, m_title.c_str());
}
void Window::setCursor(eCursor cursor)
{
switch (cursor)
{
case eCursor::Arrow:
SDL_SetCursor(m_cursor_Arrow);
break;
case eCursor::IBeam:
SDL_SetCursor(m_cursor_IBeam);
break;
default:
SDL_SetCursor(m_cursor_Arrow);
break;
}
}
void Window::enableResizable(bool enable)
{
SDL_SetWindowResizable(m_window, enable);
m_resizeable = enable;
}
void Window::setIcon(const ostd::String& iconFilePath)
{
SDL_Surface* appIcon = IMG_Load(iconFilePath);
if (appIcon == nullptr)
{
//TODO: Error
return;
}
SDL_SetWindowIcon(m_window, appIcon);
SDL_DestroySurface(appIcon);
}
void Window::__handle_events(void)
{
if (!m_initialized) return;
auto l_getMouseState = [this](void) -> MouseEventData {
float mx = 0, my = 0;
uint32_t btn = SDL_GetMouseState(&mx, &my);
MouseEventData::eButton button = MouseEventData::eButton::None;
switch (btn)
{
case SDL_BUTTON_MASK(1): button = MouseEventData::eButton::Left; break;
case SDL_BUTTON_MASK(2): button = MouseEventData::eButton::Middle; break;
case SDL_BUTTON_MASK(3): button = MouseEventData::eButton::Right; break;
default: button = MouseEventData::eButton::None; break;
}
MouseEventData mmd(*this, mx, my, button);
return mmd;
};
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_EVENT_QUIT)
{
m_running = false;
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::Normal, *this);
}
else if (event.type == SDL_EVENT_WINDOW_RESIZED)
{
WindowResizedData wrd(*this, m_windowWidth, m_windowHeight, 0, 0);
SDL_GetWindowSize(m_window, &m_windowWidth, &m_windowHeight);
wrd.new_width = m_windowWidth;
wrd.new_height = m_windowHeight;
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowResized, ostd::tSignalPriority::RealTime, wrd);
}
else if (event.type == SDL_EVENT_MOUSE_MOTION)
{
MouseEventData mmd = l_getMouseState();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseMoved, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
{
MouseEventData mmd = l_getMouseState();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MousePressed, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP)
{
MouseEventData mmd = l_getMouseState();
switch (event.button.button)
{
case SDL_BUTTON_MASK(1): mmd.button = MouseEventData::eButton::Left; break;
case SDL_BUTTON_MASK(2): mmd.button = MouseEventData::eButton::Middle; break;
case SDL_BUTTON_MASK(3): mmd.button = MouseEventData::eButton::Right; break;
default: mmd.button = MouseEventData::eButton::None; break;
}
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseReleased, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_KEY_DOWN)
{
KeyEventData ked(*this, (int32_t)event.key.key, 0, KeyEventData::eKeyEvent::Pressed);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::KeyPressed, ostd::tSignalPriority::RealTime, ked);
}
else if (event.type == SDL_EVENT_KEY_UP)
{
KeyEventData ked(*this, (int32_t)event.key.key, 0, KeyEventData::eKeyEvent::Released);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::KeyReleased, ostd::tSignalPriority::RealTime, ked);
}
else if (event.type == SDL_EVENT_TEXT_INPUT)
{
KeyEventData ked(*this, 0, event.text.text[0], KeyEventData::eKeyEvent::Text);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::TextEntered, ostd::tSignalPriority::RealTime, ked);
}
}
}
}
}

View file

@ -1,141 +0,0 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ogfx/utils/SDLInclude.hpp>
#include <ostd/utils/Signals.hpp>
#include <ostd/utils/Time.hpp>
#include <ostd/io/IOHandlers.hpp>
namespace ogfx
{
namespace gui
{
class Window : public ostd::BaseObject
{
public: enum class eCursor { Arrow = 0, IBeam };
public: struct ErrorCodes {
inline static constexpr uint16_t ExitSuccess { 0 };
inline static constexpr uint16_t SDLInitFailed { 1 };
inline static constexpr uint16_t SDLImageInitFailed { 2 };
inline static constexpr uint16_t Uninitialized { 3 };
};
public:
inline Window(void) { }
~Window(void);
inline Window(int32_t width, int32_t height, const ostd::String& windowTitle) { initialize(width, height, windowTitle); }
void initialize(int32_t width, int32_t height, const ostd::String& windowTitle);
void close(void);
int32_t startMainLoop(void);
void setSize(int32_t width, int32_t height);
void setTitle(const ostd::String& title);
void setCursor(eCursor cursor);
void enableResizable(bool enable = true);
void setIcon(const ostd::String& iconFilePath);
inline virtual void onInitialize(void) { }
inline virtual void onDestroy(void) { }
inline virtual void onClose(void) { }
inline bool isInitialized(void) const { return m_initialized; }
inline bool isRunning(void) const { return m_running; }
inline bool isVisible(void) const { return m_visible; }
inline bool isResizeable(void) const { return m_resizeable; }
inline void hide(void) { SDL_HideWindow(m_window); m_visible = false; }
inline void show(void) { SDL_ShowWindow(m_window); m_visible = true; }
inline ostd::String getTitle(void) const { return m_title; }
inline int32_t getWindowWidth(void) const { return m_windowWidth; }
inline int32_t getWindowHeight(void) const { return m_windowHeight; }
inline ostd::Color getClearColor(void) const { return m_clearColor; }
inline void setClearColor(const ostd::Color& color) { m_clearColor = color; }
inline SDL_Renderer* getSDLRenderer(void) const { return m_renderer; }
private:
void __handle_events(void);
protected:
SDL_Window* m_window { nullptr };
SDL_Renderer* m_renderer { nullptr };
private:
ostd::Color m_clearColor { 10, 10, 10, 255 };
int32_t m_windowWidth { 0 };
int32_t m_windowHeight { 0 };
ostd::String m_title { "" };
bool m_running { false };
bool m_initialized { false };
bool m_visible { true };
bool m_resizeable { true };
SDL_Cursor* m_cursor_IBeam { nullptr };
SDL_Cursor* m_cursor_Arrow { nullptr };
};
class WindowResizedData : public ostd::BaseObject
{
public:
inline WindowResizedData(Window& parent, int32_t _oldx, int32_t _oldy, int32_t _newx, int32_t _newy) : parentWindow(parent), old_width(_oldx), old_height(_oldy), new_width(_newx), new_height(_newy)
{
setTypeName("ogfx::gui::WindowResizedData");
validate();
}
public:
int32_t new_width;
int32_t new_height;
int32_t old_width;
int32_t old_height;
Window& parentWindow;
};
class MouseEventData : public ostd::BaseObject
{
public: enum class eButton { None = 0, Left, Middle, Right };
public:
inline MouseEventData(Window& parent, int32_t mousex, int32_t mousey, eButton btn) : parentWindow(parent), position_x(mousex), position_y(mousey), button(btn)
{
setTypeName("ogfx::gui::MouseEventData");
validate();
}
public:
int32_t position_x;
int32_t position_y;
eButton button;
Window& parentWindow;
};
class KeyEventData : public ostd::BaseObject
{
public: enum class eKeyEvent { Pressed = 0, Released, Text };
public:
inline KeyEventData(Window& parent, int32_t key, char _text, eKeyEvent evt) : parentWindow(parent), keyCode(key), text(_text), eventType(evt)
{
setTypeName("ogfx::gui::KeyEventData");
validate();
}
public:
int32_t keyCode;
char text;
eKeyEvent eventType;
Window& parentWindow;
};
}
}

View file

@ -19,7 +19,7 @@
*/ */
#include "RawTextInput.hpp" #include "RawTextInput.hpp"
#include "../gui/WindowBase.hpp" #include "../gui/Window.hpp"
#include "../../io/Logger.hpp" #include "../../io/Logger.hpp"
namespace ogfx namespace ogfx
@ -292,13 +292,13 @@ namespace ogfx
if (getParent().isMouseInside()) if (getParent().isMouseInside())
{ {
if (!m_ibeamCursorSet) if (!m_ibeamCursorSet)
window.setCursor(ogfx::WindowBase::eCursor::IBeam); window.setCursor(ogfx::WindowCore::eCursor::IBeam);
m_ibeamCursorSet = true; m_ibeamCursorSet = true;
} }
else else
{ {
if (m_ibeamCursorSet) if (m_ibeamCursorSet)
window.setCursor(ogfx::WindowBase::eCursor::Arrow); window.setCursor(ogfx::WindowCore::eCursor::Arrow);
m_ibeamCursorSet = false; m_ibeamCursorSet = false;
} }
} }

View file

@ -170,11 +170,11 @@ namespace ogfx
class RawTextInputEventListener : public RawTextInput::EventListener class RawTextInputEventListener : public RawTextInput::EventListener
{ {
public: public:
inline RawTextInputEventListener(ogfx::gui::RawTextInput& _parent, ogfx::WindowBase& _window) : ogfx::gui::RawTextInput::EventListener::EventListener(_parent), window(_window) { } inline RawTextInputEventListener(ogfx::gui::RawTextInput& _parent, ogfx::WindowCore& _window) : ogfx::gui::RawTextInput::EventListener::EventListener(_parent), window(_window) { }
void onSignalHandled(ostd::tSignal& signal) override; void onSignalHandled(ostd::tSignal& signal) override;
public: public:
ogfx::WindowBase& window; ogfx::WindowCore& window;
bool m_ibeamCursorSet { false }; bool m_ibeamCursorSet { false };
}; };
class RawTextInputNumberCharacterFilter : public RawTextInput::CharacterFilter class RawTextInputNumberCharacterFilter : public RawTextInput::CharacterFilter

321
src/ogfx/gui/Window.cpp Normal file
View file

@ -0,0 +1,321 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Window.hpp"
#include "../../ostd/utils/Time.hpp"
#include <SDL2/SDL_render.h>
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_mouse.h>
namespace ogfx
{
WindowCore::~WindowCore(void)
{
__on_window_destroy();
SDL_DestroyCursor(m_cursor_IBeam);
SDL_DestroyCursor(m_cursor_Arrow);
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
TTF_Quit();
}
void WindowCore::initialize(int32_t width, int32_t height, const ostd::String& title)
{
if (m_initialized) return;
m_windowWidth = width;
m_windowHeight = height;
m_title = title;
if (!SDL_Init(SDL_INIT_VIDEO))
{
printf( "SDL could not initialize! Error: %s\n", SDL_GetError() );
exit(1);
}
m_window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOW_RESIZABLE);
m_renderer = SDL_CreateRenderer(m_window, nullptr);
SDL_SetWindowMinimumSize(m_window, m_windowWidth, m_windowHeight);
SDL_SetWindowTitle(m_window, m_title.c_str());
SDL_StartTextInput(m_window);
m_cursor_Arrow = SDL_CreateSystemCursor(SDL_SystemCursor::SDL_SYSTEM_CURSOR_DEFAULT);
m_cursor_IBeam = SDL_CreateSystemCursor(SDL_SystemCursor::SDL_SYSTEM_CURSOR_TEXT);
m_initialized = true;
m_running = true;
setTypeName("ogfx::WindowCore");
enableSignals(true);
validate();
setSize(m_windowWidth, m_windowHeight);
connectSignal(ostd::tBuiltinSignals::KeyPressed);
connectSignal(ostd::tBuiltinSignals::KeyReleased);
connectSignal(ostd::tBuiltinSignals::TextEntered);
connectSignal(ostd::tBuiltinSignals::MousePressed);
connectSignal(ostd::tBuiltinSignals::MouseReleased);
connectSignal(ostd::tBuiltinSignals::MouseMoved);
connectSignal(ostd::tBuiltinSignals::OnGuiEvent);
connectSignal(ostd::tBuiltinSignals::WindowClosed);
connectSignal(ostd::tBuiltinSignals::WindowResized);
__on_window_init(width, height, title);
}
void WindowCore::close(void)
{
__on_window_close();
m_running = false;
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::Normal, *this);
}
void WindowCore::setSize(int32_t width, int32_t height)
{
if (!isInitialized()) return;
SDL_SetWindowSize(m_window, width, height);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowResized, ostd::tSignalPriority::RealTime);
}
void WindowCore::setTitle(const ostd::String& title)
{
if (!isInitialized()) return;
m_title = title;
SDL_SetWindowTitle(m_window, m_title.c_str());
}
void WindowCore::setCursor(eCursor cursor)
{
switch (cursor)
{
case eCursor::Arrow:
SDL_SetCursor(m_cursor_Arrow);
break;
case eCursor::IBeam:
SDL_SetCursor(m_cursor_IBeam);
break;
default:
SDL_SetCursor(m_cursor_Arrow);
break;
}
}
void WindowCore::enableResizable(bool enable)
{
SDL_SetWindowResizable(m_window, enable);
m_resizeable = enable;
}
void WindowCore::setIcon(const ostd::String& iconFilePath)
{
SDL_Surface* appIcon = IMG_Load(iconFilePath);
if (appIcon == nullptr)
{
//TODO: Error
return;
}
SDL_SetWindowIcon(m_window, appIcon);
SDL_DestroySurface(appIcon);
}
MouseEventData WindowCore::get_mouse_state(void)
{
float mx = 0, my = 0;
uint32_t btn = SDL_GetMouseState(&mx, &my);
MouseEventData::eButton button = MouseEventData::eButton::None;
switch (btn)
{
case SDL_BUTTON_MASK(1): button = MouseEventData::eButton::Left; break;
case SDL_BUTTON_MASK(2): button = MouseEventData::eButton::Middle; break;
case SDL_BUTTON_MASK(3): button = MouseEventData::eButton::Right; break;
default: button = MouseEventData::eButton::None; break;
}
MouseEventData mmd(*this, mx, my, button);
return mmd;
}
void WindowCore::handle_events(void)
{
if (!isInitialized()) return;
SDL_Event event;
if (isBlockingEventsEnabled())
{
if (SDL_WaitEvent(&event))
__handle_event(event);
}
else
{
while (SDL_PollEvent(&event))
__handle_event(event);
}
}
void WindowCore::__handle_event(SDL_Event& event)
{
if (event.type == SDL_EVENT_QUIT)
{
close();
}
else if (event.type == SDL_EVENT_WINDOW_RESIZED)
{
WindowResizedData wrd(*this, m_windowWidth, m_windowHeight, 0, 0);
SDL_GetWindowSize(m_window, &m_windowWidth, &m_windowHeight);
wrd.new_width = m_windowWidth;
wrd.new_height = m_windowHeight;
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowResized, ostd::tSignalPriority::RealTime, wrd);
}
else if (event.type == SDL_EVENT_MOUSE_MOTION)
{
MouseEventData mmd = get_mouse_state();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseMoved, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
{
MouseEventData mmd = get_mouse_state();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MousePressed, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP)
{
MouseEventData mmd = get_mouse_state();
switch (event.button.button)
{
case SDL_BUTTON_MASK(1): mmd.button = MouseEventData::eButton::Left; break;
case SDL_BUTTON_MASK(2): mmd.button = MouseEventData::eButton::Middle; break;
case SDL_BUTTON_MASK(3): mmd.button = MouseEventData::eButton::Right; break;
default: mmd.button = MouseEventData::eButton::None; break;
}
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseReleased, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_KEY_DOWN)
{
KeyEventData ked(*this, (int32_t)event.key.key, 0, KeyEventData::eKeyEvent::Pressed);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::KeyPressed, ostd::tSignalPriority::RealTime, ked);
}
else if (event.type == SDL_EVENT_KEY_UP)
{
KeyEventData ked(*this, (int32_t)event.key.key, 0, KeyEventData::eKeyEvent::Released);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::KeyReleased, ostd::tSignalPriority::RealTime, ked);
}
else if (event.type == SDL_EVENT_TEXT_INPUT)
{
KeyEventData ked(*this, 0, event.text.text[0], KeyEventData::eKeyEvent::Text);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::TextEntered, ostd::tSignalPriority::RealTime, ked);
}
__on_event(event);
}
void GraphicsWindow::__on_window_init(int32_t width, int32_t height, const ostd::String& title)
{
SDL_SetWindowResizable(m_window, false);
m_fixedUpdateTImer.create(60.0, [this](double frameTime_s){
this->onFixedUpdate(frameTime_s);
});
m_fpsUpdateTimer.create(1.0, [this](double frameTime_s){
if (this->m_frameCount == 0) return;
if (this->m_frameTimeAcc == 0) return;
this->m_fps = (int32_t)(1.0f / (static_cast<double>(this->m_frameTimeAcc) / static_cast<double>(this->m_frameCount)));
this->m_frameTimeAcc = 0;
this->m_frameCount = 0;
});
setTypeName("ogfx::GraphicsWindow");
onInitialize();
}
void GraphicsWindow::__on_window_close(void)
{
onClose();
}
void GraphicsWindow::__main_loop(void)
{
handle_events();
SDL_SetRenderDrawColor(m_renderer, getClearColor().r, getClearColor().g, getClearColor().b, getClearColor().a);
if (m_refreshScreen)
SDL_RenderClear(m_renderer);
m_fixedUpdateTImer.update();
onUpdate();
onRender();
SDL_RenderPresent(m_renderer);
m_frameTimeAcc += m_fpsUpdateClock.restart(ostd::eTimeUnits::Seconds);
m_frameCount++;
m_fpsUpdateTimer.update();
}
void GraphicsWindow::__on_event(SDL_Event& event)
{
onSDLEvent(event);
}
void GraphicsWindow::__on_window_destroy(void)
{
onDestroy();
}
namespace gui
{
void Window::__on_window_init(int32_t width, int32_t height, const ostd::String& title)
{
enableBlockingEvents();
setTypeName("ogfx::gui::Window");
onInitialize();
}
void Window::__on_window_close(void)
{
onClose();
}
void Window::__main_loop(void)
{
while (isRunning())
{
handle_events();
SDL_SetRenderDrawColor(m_renderer, getClearColor().r, getClearColor().g, getClearColor().b, getClearColor().a);
SDL_RenderClear(m_renderer);
SDL_RenderPresent(m_renderer);
}
}
void Window::__on_event(SDL_Event& event)
{
if (event.type == SDL_EVENT_KEY_UP)
{
if (event.key.key == SDLK_ESCAPE)
close();
}
onSDLEvent(event);
}
void Window::__on_window_destroy(void)
{
onDestroy();
}
}
}

152
src/ogfx/gui/Window.hpp Normal file
View file

@ -0,0 +1,152 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <SDL3/SDL_events.h>
#include <ogfx/utils/SDLInclude.hpp>
#include <ostd/utils/Signals.hpp>
#include <ostd/utils/Time.hpp>
#include <ostd/io/IOHandlers.hpp>
#include <ogfx/gui/Events.hpp>
namespace ogfx
{
class WindowCore : public ostd::BaseObject
{
public: enum class eCursor { Arrow = 0, IBeam };
public:
inline WindowCore(void) { }
virtual ~WindowCore(void);
inline WindowCore(int32_t width, int32_t height, const ostd::String& title) { initialize(width, height, title); }
void initialize(int32_t width, int32_t height, const ostd::String& title);
inline void mainLoop(void) { if (isInitialized()) __main_loop(); }
void close(void);
void setSize(int32_t width, int32_t height);
void setTitle(const ostd::String& title);
void setCursor(eCursor cursor);
void enableResizable(bool enable = true);
void setIcon(const ostd::String& iconFilePath);
inline bool isInitialized(void) const { return m_initialized; }
inline bool isRunning(void) const { return m_running; }
inline bool isVisible(void) const { return m_visible; }
inline bool isResizeable(void) const { return m_resizeable; }
inline void hide(void) { SDL_HideWindow(m_window); m_visible = false; }
inline void show(void) { SDL_ShowWindow(m_window); m_visible = true; }
inline ostd::String getTitle(void) const { return m_title; }
inline int32_t getWindowWidth(void) const { return m_windowWidth; }
inline int32_t getWindowHeight(void) const { return m_windowHeight; }
inline ostd::Color getClearColor(void) const { return m_clearColor; }
inline void setClearColor(const ostd::Color& color) { m_clearColor = color; }
inline SDL_Renderer* getSDLRenderer(void) { return m_renderer; }
inline void enableBlockingEvents(bool enable = true) { m_blockingEvents = enable; }
inline bool isBlockingEventsEnabled(void) const { return m_blockingEvents; }
protected:
MouseEventData get_mouse_state(void);
virtual void handle_events(void);
inline virtual void __on_event(SDL_Event& event) { }
inline virtual void __on_window_init(int32_t width, int32_t height, const ostd::String& title) { }
inline virtual void __on_window_destroy(void) { }
inline virtual void __on_window_close(void) { }
inline virtual void __main_loop(void) = 0;
private:
void __handle_event(SDL_Event& event);
protected:
SDL_Window* m_window { nullptr };
SDL_Renderer* m_renderer { nullptr };
private:
ostd::Color m_clearColor { 10, 10, 10, 255 };
int32_t m_windowWidth { 0 };
int32_t m_windowHeight { 0 };
ostd::String m_title { "" };
bool m_running { false };
bool m_initialized { false };
bool m_visible { true };
bool m_blockingEvents { false };
bool m_resizeable { true };
SDL_Cursor* m_cursor_IBeam { nullptr };
SDL_Cursor* m_cursor_Arrow { nullptr };
};
class GraphicsWindow : public WindowCore
{
public: enum class eCursor { Arrow = 0, IBeam };
public:
inline GraphicsWindow(void) { }
inline GraphicsWindow(int32_t width, int32_t height, const ostd::String& title) { initialize(width, height, title); }
inline virtual void onRender(void) { }
inline virtual void onUpdate(void) { }
inline virtual void onFixedUpdate(double frameTime_s) { }
inline virtual void onInitialize(void) { }
inline virtual void onDestroy(void) { }
inline virtual void onClose(void) { }
inline virtual void onSDLEvent(SDL_Event& event) { }
inline int32_t getFPS(void) const { return m_fps; }
protected:
void __on_window_init(int32_t width, int32_t height, const ostd::String& title) override;
void __on_event(SDL_Event& event) override;
void __on_window_destroy(void) override;
void __on_window_close(void) override;
void __main_loop(void) override;
private:
int32_t m_fps { 0 };
ostd::StepTimer m_fixedUpdateTImer;
ostd::StepTimer m_fpsUpdateTimer;
ostd::Timer m_fpsUpdateClock;
uint64_t m_frameTimeAcc { 0 };
int32_t m_frameCount { 0 };
bool m_refreshScreen { true };
};
namespace gui
{
class Window : public WindowCore
{
public: enum class eCursor { Arrow = 0, IBeam };
public:
inline Window(void) { }
inline Window(int32_t width, int32_t height, const ostd::String& title) { initialize(width, height, title); }
inline virtual void onInitialize(void) { }
inline virtual void onDestroy(void) { }
inline virtual void onClose(void) { }
inline virtual void onSDLEvent(SDL_Event& event) { }
protected:
void __on_window_init(int32_t width, int32_t height, const ostd::String& title) override;
void __on_event(SDL_Event& event) override;
void __on_window_destroy(void) override;
void __on_window_close(void) override;
void __main_loop(void) override;
private:
};
}
}

View file

@ -1,229 +0,0 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#include "WindowBase.hpp"
#include "../../ostd/utils/Time.hpp"
#include <SDL2/SDL_render.h>
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_mouse.h>
namespace ogfx
{
WindowBase::~WindowBase(void)
{
onDestroy();
SDL_DestroyCursor(m_cursor_IBeam);
SDL_DestroyCursor(m_cursor_Arrow);
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
TTF_Quit();
}
void WindowBase::initialize(int32_t width, int32_t height, const ostd::String& windowTitle)
{
if (m_initialized) return;
m_windowWidth = width;
m_windowHeight = height;
m_title = windowTitle;
if (!SDL_Init(SDL_INIT_VIDEO))
{
printf( "SDL could not initialize! Error: %s\n", SDL_GetError() );
exit(1);
}
m_window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOW_RESIZABLE);
SDL_SetWindowResizable(m_window, false);
m_renderer = SDL_CreateRenderer(m_window, nullptr);
SDL_SetWindowMinimumSize(m_window, m_windowWidth, m_windowHeight);
SDL_SetWindowTitle(m_window, m_title.c_str());
SDL_StartTextInput(m_window);
m_cursor_Arrow = SDL_CreateSystemCursor(SDL_SystemCursor::SDL_SYSTEM_CURSOR_DEFAULT);
m_cursor_IBeam = SDL_CreateSystemCursor(SDL_SystemCursor::SDL_SYSTEM_CURSOR_TEXT);
m_initialized = true;
m_running = true;
m_fixedUpdateTImer.create(60.0, [this](double frameTime_s){
this->onFixedUpdate(frameTime_s);
});
m_fpsUpdateTimer.create(1.0, [this](double frameTime_s){
if (this->m_frameCount == 0) return;
if (this->m_frameTimeAcc == 0) return;
this->m_fps = (int32_t)(1.0f / (static_cast<double>(this->m_frameTimeAcc) / static_cast<double>(this->m_frameCount)));
this->m_frameTimeAcc = 0;
this->m_frameCount = 0;
});
setTypeName("ogfx::WindowBase");
enableSignals(true);
validate();
setSize(m_windowWidth, m_windowHeight);
onInitialize();
}
void WindowBase::close(void)
{
m_running = false;
onClose();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::RealTime, *this);
}
void WindowBase::update(void)
{
if (!m_initialized) return;
__handle_events();
SDL_SetRenderDrawColor(m_renderer, m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
if (m_refreshScreen)
SDL_RenderClear(m_renderer);
m_fixedUpdateTImer.update();
onUpdate();
onRender();
SDL_RenderPresent(m_renderer);
m_frameTimeAcc += m_fpsUpdateClock.restart(ostd::eTimeUnits::Seconds);
m_frameCount++;
m_fpsUpdateTimer.update();
}
void WindowBase::setSize(int32_t width, int32_t height)
{
if (!isInitialized()) return;
SDL_SetWindowSize(m_window, width, height);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowResized, ostd::tSignalPriority::RealTime);
}
void WindowBase::setTitle(const ostd::String& title)
{
if (!isInitialized()) return;
m_title = title;
SDL_SetWindowTitle(m_window, m_title.c_str());
}
void WindowBase::setCursor(eCursor cursor)
{
switch (cursor)
{
case eCursor::Arrow:
SDL_SetCursor(m_cursor_Arrow);
break;
case eCursor::IBeam:
SDL_SetCursor(m_cursor_IBeam);
break;
default:
SDL_SetCursor(m_cursor_Arrow);
break;
}
}
void WindowBase::enableResizable(bool enable)
{
SDL_SetWindowResizable(m_window, enable);
}
void WindowBase::setIcon(const ostd::String& iconFilePath)
{
SDL_Surface* appIcon = IMG_Load(iconFilePath);
if (appIcon == nullptr)
{
//TODO: Error
return;
}
SDL_SetWindowIcon(m_window, appIcon);
SDL_DestroySurface(appIcon);
}
void WindowBase::__handle_events(void)
{
if (!m_initialized) return;
auto l_getMouseState = [this](void) -> MouseEventData {
float mx = 0, my = 0;
uint32_t btn = SDL_GetMouseState(&mx, &my);
MouseEventData::eButton button = MouseEventData::eButton::None;
switch (btn)
{
case SDL_BUTTON_MASK(1): button = MouseEventData::eButton::Left; break;
case SDL_BUTTON_MASK(2): button = MouseEventData::eButton::Middle; break;
case SDL_BUTTON_MASK(3): button = MouseEventData::eButton::Right; break;
default: button = MouseEventData::eButton::None; break;
}
MouseEventData mmd(*this, mx, my, button);
return mmd;
};
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_EVENT_QUIT)
{
m_running = false;
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::Normal, *this);
}
else if (event.type == SDL_EVENT_WINDOW_RESIZED)
{
WindowResizedData wrd(*this, m_windowWidth, m_windowHeight, 0, 0);
SDL_GetWindowSize(m_window, &m_windowWidth, &m_windowHeight);
wrd.new_width = m_windowWidth;
wrd.new_height = m_windowHeight;
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowResized, ostd::tSignalPriority::RealTime, wrd);
}
else if (event.type == SDL_EVENT_MOUSE_MOTION)
{
MouseEventData mmd = l_getMouseState();
if (isMouseDragEventEnabled() && mmd.button != MouseEventData::eButton::None)
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseDragged, ostd::tSignalPriority::RealTime, mmd);
else
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseMoved, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
{
MouseEventData mmd = l_getMouseState();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MousePressed, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP)
{
MouseEventData mmd = l_getMouseState();
switch (event.button.button)
{
case SDL_BUTTON_MASK(1): mmd.button = MouseEventData::eButton::Left; break;
case SDL_BUTTON_MASK(2): mmd.button = MouseEventData::eButton::Middle; break;
case SDL_BUTTON_MASK(3): mmd.button = MouseEventData::eButton::Right; break;
default: mmd.button = MouseEventData::eButton::None; break;
}
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::MouseReleased, ostd::tSignalPriority::RealTime, mmd);
}
else if (event.type == SDL_EVENT_KEY_DOWN)
{
KeyEventData ked(*this, (int32_t)event.key.key, 0, KeyEventData::eKeyEvent::Pressed);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::KeyPressed, ostd::tSignalPriority::RealTime, ked);
}
else if (event.type == SDL_EVENT_KEY_UP)
{
KeyEventData ked(*this, (int32_t)event.key.key, 0, KeyEventData::eKeyEvent::Released);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::KeyReleased, ostd::tSignalPriority::RealTime, ked);
}
else if (event.type == SDL_EVENT_TEXT_INPUT)
{
KeyEventData ked(*this, 0, event.text.text[0], KeyEventData::eKeyEvent::Text);
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::TextEntered, ostd::tSignalPriority::RealTime, ked);
}
}
}
}

View file

@ -1,202 +0,0 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ogfx/utils/SDLInclude.hpp>
#include <ostd/utils/Signals.hpp>
#include <ostd/utils/Time.hpp>
#include <ostd/io/IOHandlers.hpp>
namespace ogfx
{
// class WindowCore : public ostd::BaseObject
// {
// public: enum class eCursor { Arrow = 0, IBeam };
// public:
// inline WindowCore(void) { }
// virtual ~WindowCore(void);
// inline WindowCore(int32_t width, int32_t height, const ostd::String& windowTitle) { initialize(width, height, windowTitle); }
// void initialize(int32_t width, int32_t height, const ostd::String& windowTitle);
// void close(void);
// void update(void);
// void setSize(int32_t width, int32_t height);
// void setTitle(const ostd::String& title);
// void setCursor(eCursor cursor);
// void enableResizable(bool enable = true);
// void setIcon(const ostd::String& iconFilePath);
// inline virtual void onWindowInitialize(void) { }
// inline virtual void onWindowDestroy(void) { }
// inline virtual void onWindowClose(void) { }
// inline bool isInitialized(void) const { return m_initialized; }
// inline bool isRunning(void) const { return m_running; }
// inline bool isVisible(void) const { return m_visible; }
// inline void hide(void) { SDL_HideWindow(m_window); m_visible = false; }
// inline void show(void) { SDL_ShowWindow(m_window); m_visible = true; }
// inline ostd::String getTitle(void) const { return m_title; }
// inline int32_t getWindowWidth(void) const { return m_windowWidth; }
// inline int32_t getWindowHeight(void) const { return m_windowHeight; }
// inline ostd::Color getClearColor(void) const { return m_clearColor; }
// inline void setClearColor(const ostd::Color& color) { m_clearColor = color; }
// inline SDL_Renderer* getSDLRenderer(void) { return m_renderer; }
// private:
// void __handle_events(void);
// protected:
// ostd::ConsoleOutputHandler out;
// SDL_Window* m_window { nullptr };
// SDL_Renderer* m_renderer { nullptr };
// bool m_refreshScreen { true };
// private:
// ostd::Color m_clearColor { 10, 10, 10, 255 };
// int32_t m_windowWidth { 0 };
// int32_t m_windowHeight { 0 };
// ostd::String m_title { "" };
// bool m_running { false };
// bool m_initialized { false };
// bool m_visible { true };
// SDL_Cursor* m_cursor_IBeam { nullptr };
// SDL_Cursor* m_cursor_Arrow { nullptr };
// };
class WindowBase : public ostd::BaseObject
{
public: enum class eCursor { Arrow = 0, IBeam };
public:
inline WindowBase(void) { }
~WindowBase(void);
inline WindowBase(int32_t width, int32_t height, const ostd::String& windowTitle) { initialize(width, height, windowTitle); }
void initialize(int32_t width, int32_t height, const ostd::String& windowTitle);
void close(void);
void update(void);
void setSize(int32_t width, int32_t height);
void setTitle(const ostd::String& title);
void setCursor(eCursor cursor);
void enableResizable(bool enable = true);
void setIcon(const ostd::String& iconFilePath);
inline virtual void onRender(void) { }
inline virtual void onUpdate(void) { }
inline virtual void onFixedUpdate(double frameTime_s) { }
inline virtual void onInitialize(void) { }
inline virtual void onDestroy(void) { }
inline virtual void onClose(void) { }
inline bool isInitialized(void) const { return m_initialized; }
inline bool isRunning(void) const { return m_running; }
inline bool isVisible(void) const { return m_visible; }
inline void hide(void) { SDL_HideWindow(m_window); m_visible = false; }
inline void show(void) { SDL_ShowWindow(m_window); m_visible = true; }
inline ostd::String getTitle(void) const { return m_title; }
inline int32_t getFPS(void) const { return m_fps; }
inline int32_t getWindowWidth(void) const { return m_windowWidth; }
inline int32_t getWindowHeight(void) const { return m_windowHeight; }
inline bool isMouseDragEventEnabled(void) const { return m_deagEventEnabled; }
inline void enableMouseDragEvent(bool enable = true) { m_deagEventEnabled = enable; }
inline ostd::Color getClearColor(void) const { return m_clearColor; }
inline void setClearColor(const ostd::Color& color) { m_clearColor = color; }
inline SDL_Renderer* getSDLRenderer(void) { return m_renderer; }
private:
void __handle_events(void);
protected:
ostd::ConsoleOutputHandler out;
SDL_Window* m_window { nullptr };
SDL_Renderer* m_renderer { nullptr };
bool m_refreshScreen { true };
private:
ostd::Color m_clearColor { 10, 10, 10, 255 };
int32_t m_windowWidth { 0 };
int32_t m_windowHeight { 0 };
ostd::String m_title { "" };
int32_t m_fps { 0 };
ostd::StepTimer m_fixedUpdateTImer;
ostd::StepTimer m_fpsUpdateTimer;
ostd::Timer m_fpsUpdateClock;
uint64_t m_frameTimeAcc { 0 };
int32_t m_frameCount { 0 };
bool m_deagEventEnabled { false };
bool m_running { false };
bool m_initialized { false };
bool m_visible { true };
SDL_Cursor* m_cursor_IBeam { nullptr };
SDL_Cursor* m_cursor_Arrow { nullptr };
};
class WindowResizedData : public ostd::BaseObject
{
public:
inline WindowResizedData(WindowBase& parent, int32_t _oldx, int32_t _oldy, int32_t _newx, int32_t _newy) : parentWindow(parent), old_width(_oldx), old_height(_oldy), new_width(_newx), new_height(_newy)
{
setTypeName("ogfx::WindowResizedData");
validate();
}
public:
int32_t new_width;
int32_t new_height;
int32_t old_width;
int32_t old_height;
WindowBase& parentWindow;
};
class MouseEventData : public ostd::BaseObject
{
public: enum class eButton { None = 0, Left, Middle, Right };
public:
inline MouseEventData(WindowBase& parent, float mousex, float mousey, eButton btn) : parentWindow(parent), position_x(mousex), position_y(mousey), button(btn)
{
setTypeName("ogfx::MouseEventData");
validate();
}
public:
float position_x;
float position_y;
eButton button;
WindowBase& parentWindow;
};
class KeyEventData : public ostd::BaseObject
{
public: enum class eKeyEvent { Pressed = 0, Released, Text };
public:
inline KeyEventData(WindowBase& parent, int32_t key, char _text, eKeyEvent evt) : parentWindow(parent), keyCode(key), text(_text), eventType(evt)
{
setTypeName("ogfx::KeyEventData");
validate();
}
public:
int32_t keyCode;
char text;
eKeyEvent eventType;
WindowBase& parentWindow;
};
}

View file

@ -18,7 +18,7 @@
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>. along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "WindowBaseOutputHandler.hpp" #include "WindowOutputHandler.hpp"
#include "../render/BasicRenderer.hpp" #include "../render/BasicRenderer.hpp"
#include "../../string/TextStyleParser.hpp" #include "../../string/TextStyleParser.hpp"
@ -26,25 +26,25 @@ namespace ogfx
{ {
using namespace ostd; using namespace ostd;
WindowBaseOutputHandler::WindowBaseOutputHandler(void) GraphicsWindowOutputHandler::GraphicsWindowOutputHandler(void)
{ {
} }
void WindowBaseOutputHandler::attachWindow(ogfx::WindowBase& window) void GraphicsWindowOutputHandler::attachWindow(ogfx::GraphicsWindow& window)
{ {
if (m_window != nullptr) return; if (m_window != nullptr) return;
m_window = &window; m_window = &window;
m_renderer.init(window); m_renderer.init(window);
} }
void WindowBaseOutputHandler::setMonospaceFont(const String& filePath) void GraphicsWindowOutputHandler::setMonospaceFont(const String& filePath)
{ {
m_renderer.setFont(filePath); m_renderer.setFont(filePath);
m_renderer.setFontSize(m_fontSize); m_renderer.setFontSize(m_fontSize);
__update_char_size(); __update_char_size();
} }
Vec2 WindowBaseOutputHandler::getStringSize(const String& str) Vec2 GraphicsWindowOutputHandler::getStringSize(const String& str)
{ {
Vec2 size = { 0.0f, m_charSize.y }; Vec2 size = { 0.0f, m_charSize.y };
if (str.len() == 0) return size; if (str.len() == 0) return size;
@ -52,23 +52,23 @@ namespace ogfx
return size; return size;
} }
bool WindowBaseOutputHandler::isReady(void) bool GraphicsWindowOutputHandler::isReady(void)
{ {
return m_window != nullptr && m_renderer.getTTFRenderer().hasOpenFont() && m_fontSize > 0; return m_window != nullptr && m_renderer.getTTFRenderer().hasOpenFont() && m_fontSize > 0;
} }
void WindowBaseOutputHandler::resetCursorPosition(void) void GraphicsWindowOutputHandler::resetCursorPosition(void)
{ {
m_curosrPosition = { 0, 0 }; m_curosrPosition = { 0, 0 };
} }
void WindowBaseOutputHandler::resetColors(void) void GraphicsWindowOutputHandler::resetColors(void)
{ {
m_foregroundColor = m_defaultForegroundColor; m_foregroundColor = m_defaultForegroundColor;
m_backgroundColor = m_defaultBackgroundColor; m_backgroundColor = m_defaultBackgroundColor;
} }
void WindowBaseOutputHandler::beginFrame(void) void GraphicsWindowOutputHandler::beginFrame(void)
{ {
resetColors(); resetColors();
resetCursorPosition(); resetCursorPosition();
@ -77,37 +77,37 @@ namespace ogfx
void WindowBaseOutputHandler::setConsoleMaxCharacters(const IPoint& size) void GraphicsWindowOutputHandler::setConsoleMaxCharacters(const IPoint& size)
{ {
m_consoleSize = size; m_consoleSize = size;
} }
void WindowBaseOutputHandler::setConsolePosition(const Vec2& pos) void GraphicsWindowOutputHandler::setConsolePosition(const Vec2& pos)
{ {
m_consolePosition = pos; m_consolePosition = pos;
} }
void WindowBaseOutputHandler::setWrapMode(eWrapMode wrapMode) void GraphicsWindowOutputHandler::setWrapMode(eWrapMode wrapMode)
{ {
m_wrapMode = wrapMode; m_wrapMode = wrapMode;
} }
void WindowBaseOutputHandler::setPadding(const Rectangle& rect) void GraphicsWindowOutputHandler::setPadding(const Rectangle& rect)
{ {
m_padding = rect; m_padding = rect;
} }
void WindowBaseOutputHandler::setDefaultBackgorundColor(const Color& color) void GraphicsWindowOutputHandler::setDefaultBackgorundColor(const Color& color)
{ {
m_defaultBackgroundColor = color; m_defaultBackgroundColor = color;
} }
void WindowBaseOutputHandler::setDefaultForegroundColor(const Color& color) void GraphicsWindowOutputHandler::setDefaultForegroundColor(const Color& color)
{ {
m_defaultForegroundColor = color; m_defaultForegroundColor = color;
} }
void WindowBaseOutputHandler::setTabWidth(uint8_t tw) void GraphicsWindowOutputHandler::setTabWidth(uint8_t tw)
{ {
m_tabWidth = tw; m_tabWidth = tw;
} }
@ -115,19 +115,19 @@ namespace ogfx
void WindowBaseOutputHandler::setFontSize(int32_t fontSize) void GraphicsWindowOutputHandler::setFontSize(int32_t fontSize)
{ {
m_fontSize = fontSize; m_fontSize = fontSize;
m_renderer.setFontSize(m_fontSize); m_renderer.setFontSize(m_fontSize);
__update_char_size(); __update_char_size();
} }
int32_t WindowBaseOutputHandler::getFontSize(void) int32_t GraphicsWindowOutputHandler::getFontSize(void)
{ {
return m_fontSize; return m_fontSize;
} }
Vec2 WindowBaseOutputHandler::getCharacterSize(int32_t fontSize) Vec2 GraphicsWindowOutputHandler::getCharacterSize(int32_t fontSize)
{ {
if (fontSize > 0) if (fontSize > 0)
{ {
@ -137,37 +137,37 @@ namespace ogfx
return m_charSize; return m_charSize;
} }
Vec2 WindowBaseOutputHandler::getConsolePosition(void) Vec2 GraphicsWindowOutputHandler::getConsolePosition(void)
{ {
return m_consolePosition; return m_consolePosition;
} }
WindowBaseOutputHandler::eWrapMode WindowBaseOutputHandler::getWrapMode(void) GraphicsWindowOutputHandler::eWrapMode GraphicsWindowOutputHandler::getWrapMode(void)
{ {
return m_wrapMode; return m_wrapMode;
} }
Rectangle WindowBaseOutputHandler::getPadding(void) Rectangle GraphicsWindowOutputHandler::getPadding(void)
{ {
return m_padding; return m_padding;
} }
Color WindowBaseOutputHandler::getDefaultBackgroundColor(void) Color GraphicsWindowOutputHandler::getDefaultBackgroundColor(void)
{ {
return m_defaultBackgroundColor; return m_defaultBackgroundColor;
} }
Color WindowBaseOutputHandler::getDefaultForegroundColor(void) Color GraphicsWindowOutputHandler::getDefaultForegroundColor(void)
{ {
return m_defaultForegroundColor; return m_defaultForegroundColor;
} }
uint8_t WindowBaseOutputHandler::getTabWidth(void) uint8_t GraphicsWindowOutputHandler::getTabWidth(void)
{ {
return m_tabWidth; return m_tabWidth;
} }
Rectangle WindowBaseOutputHandler::getConsoleBounds(void) Rectangle GraphicsWindowOutputHandler::getConsoleBounds(void)
{ {
float console_w = ((float)m_consoleSize.x * getCharacterSize().x) + getPadding().x + getPadding().w; float console_w = ((float)m_consoleSize.x * getCharacterSize().x) + getPadding().x + getPadding().w;
float console_h = ((float)m_consoleSize.y * getCharacterSize().y) + getPadding().y + getPadding().h; float console_h = ((float)m_consoleSize.y * getCharacterSize().y) + getPadding().y + getPadding().h;
@ -177,54 +177,54 @@ namespace ogfx
WindowBaseOutputHandler& WindowBaseOutputHandler::bg(const Color& color) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::bg(const Color& color)
{ {
m_backgroundColor = color; m_backgroundColor = color;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::bg(const ConsoleColors::tConsoleColor& color) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::bg(const ConsoleColors::tConsoleColor& color)
{ {
m_backgroundColor = color.fullColor; m_backgroundColor = color.fullColor;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::bg(const String& color) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::bg(const String& color)
{ {
m_backgroundColor = ConsoleColors::getFromName(color).fullColor; m_backgroundColor = ConsoleColors::getFromName(color).fullColor;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::fg(const Color& color) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::fg(const Color& color)
{ {
m_foregroundColor = color; m_foregroundColor = color;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::fg(const ConsoleColors::tConsoleColor& color) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::fg(const ConsoleColors::tConsoleColor& color)
{ {
m_foregroundColor = color.fullColor; m_foregroundColor = color.fullColor;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::fg(const String& color) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::fg(const String& color)
{ {
m_foregroundColor = ConsoleColors::getFromName(color).fullColor; m_foregroundColor = ConsoleColors::getFromName(color).fullColor;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::pChar(char c) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::pChar(char c)
{ {
__print_string(ostd::String("").addChar(c)); __print_string(ostd::String("").addChar(c));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::pStyled(const String& styled) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::pStyled(const String& styled)
{ {
return pStyled(TextStyleParser::parse(styled, { m_backgroundColor, "", true }, { m_foregroundColor, "", false })); return pStyled(TextStyleParser::parse(styled, { m_backgroundColor, "", true }, { m_foregroundColor, "", false }));
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::pStyled(const TextStyleParser::tStyledString& styled) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::pStyled(const TextStyleParser::tStyledString& styled)
{ {
if (!styled.validate()) return *this; if (!styled.validate()) return *this;
Color oldBgCol = m_backgroundColor; Color oldBgCol = m_backgroundColor;
@ -236,7 +236,7 @@ namespace ogfx
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::pStyled(TextStyleBuilder::IRichStringBase& styled) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::pStyled(TextStyleBuilder::IRichStringBase& styled)
{ {
auto oldBg = styled.getDefaultBackgroundColor(); auto oldBg = styled.getDefaultBackgroundColor();
auto oldFg = styled.getDefaultForegroundColor(); auto oldFg = styled.getDefaultForegroundColor();
@ -248,79 +248,79 @@ namespace ogfx
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::pObject(const BaseObject& bo) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::pObject(const BaseObject& bo)
{ {
__print_string(bo.toString()); __print_string(bo.toString());
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(const String& se) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(const String& se)
{ {
__print_string(se); __print_string(se);
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(uint8_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(uint8_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(int8_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(int8_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(uint16_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(uint16_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(int16_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(int16_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(uint32_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(uint32_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(int32_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(int32_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(uint64_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(uint64_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(int64_t i) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(int64_t i)
{ {
__print_string(ostd::String("").add(i)); __print_string(ostd::String("").add(i));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(float f, uint8_t precision) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(float f, uint8_t precision)
{ {
__print_string(ostd::String("").add(f, precision)); __print_string(ostd::String("").add(f, precision));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::p(double f, uint8_t precision) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::p(double f, uint8_t precision)
{ {
__print_string(ostd::String("").add(f, precision)); __print_string(ostd::String("").add(f, precision));
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::nl(void) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::nl(void)
{ {
if (m_curosrPosition.y >= getConsoleSize().y) return *this; if (m_curosrPosition.y >= getConsoleSize().y) return *this;
m_curosrPosition.y++; m_curosrPosition.y++;
@ -328,7 +328,7 @@ namespace ogfx
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::tab(void) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::tab(void)
{ {
if (m_curosrPosition.x == 0) if (m_curosrPosition.x == 0)
{ {
@ -343,68 +343,68 @@ namespace ogfx
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::flush(void) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::flush(void)
{ {
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::clear(void) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::clear(void)
{ {
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::reset(void) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::reset(void)
{ {
resetColors(); resetColors();
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::xy(IPoint position) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::xy(IPoint position)
{ {
m_curosrPosition = { (float)position.x, (float)position.y }; m_curosrPosition = { (float)position.x, (float)position.y };
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::xy(int32_t x, int32_t y) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::xy(int32_t x, int32_t y)
{ {
m_curosrPosition = { (float)x, (float)y }; m_curosrPosition = { (float)x, (float)y };
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::x(int32_t x) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::x(int32_t x)
{ {
m_curosrPosition.x = (float)x; m_curosrPosition.x = (float)x;
return *this; return *this;
} }
WindowBaseOutputHandler& WindowBaseOutputHandler::y(int32_t y) GraphicsWindowOutputHandler& GraphicsWindowOutputHandler::y(int32_t y)
{ {
m_curosrPosition.y = (float)y; m_curosrPosition.y = (float)y;
return *this; return *this;
} }
IPoint WindowBaseOutputHandler::getCursorPosition(void) IPoint GraphicsWindowOutputHandler::getCursorPosition(void)
{ {
return { (int32_t)std::round(m_curosrPosition.x), (int32_t)std::round(m_curosrPosition.y) }; return { (int32_t)std::round(m_curosrPosition.x), (int32_t)std::round(m_curosrPosition.y) };
} }
void WindowBaseOutputHandler::getCursorPosition(int32_t& outX, int32_t& outY) void GraphicsWindowOutputHandler::getCursorPosition(int32_t& outX, int32_t& outY)
{ {
outX = (int32_t)std::round(m_curosrPosition.x); outX = (int32_t)std::round(m_curosrPosition.x);
outY = (int32_t)std::round(m_curosrPosition.y); outY = (int32_t)std::round(m_curosrPosition.y);
} }
int32_t WindowBaseOutputHandler::getCursorX(void) int32_t GraphicsWindowOutputHandler::getCursorX(void)
{ {
return (int32_t)std::round(m_curosrPosition.x); return (int32_t)std::round(m_curosrPosition.x);
} }
int32_t WindowBaseOutputHandler::getCursorY(void) int32_t GraphicsWindowOutputHandler::getCursorY(void)
{ {
return (int32_t)std::round(m_curosrPosition.y); return (int32_t)std::round(m_curosrPosition.y);
} }
void WindowBaseOutputHandler::getConsoleSize(int32_t& outColumns, int32_t& outRows) void GraphicsWindowOutputHandler::getConsoleSize(int32_t& outColumns, int32_t& outRows)
{ {
int32_t console_rows = std::numeric_limits<int>::max(); int32_t console_rows = std::numeric_limits<int>::max();
int32_t console_cols = std::numeric_limits<int>::max(); int32_t console_cols = std::numeric_limits<int>::max();
@ -414,7 +414,7 @@ namespace ogfx
outRows = console_rows; outRows = console_rows;
} }
IPoint WindowBaseOutputHandler::getConsoleSize(void) IPoint GraphicsWindowOutputHandler::getConsoleSize(void)
{ {
int32_t console_rows = std::numeric_limits<int>::max(); int32_t console_rows = std::numeric_limits<int>::max();
int32_t console_cols = std::numeric_limits<int>::max(); int32_t console_cols = std::numeric_limits<int>::max();
@ -423,13 +423,13 @@ namespace ogfx
return { console_cols, console_rows }; return { console_cols, console_rows };
} }
void WindowBaseOutputHandler::__update_char_size(void) void GraphicsWindowOutputHandler::__update_char_size(void)
{ {
auto size = m_renderer.getStringSize("A", m_fontSize); auto size = m_renderer.getStringSize("A", m_fontSize);
m_charSize = { (float)size.x, (float)size.y }; m_charSize = { (float)size.x, (float)size.y };
} }
void WindowBaseOutputHandler::__print_string(const String& str) void GraphicsWindowOutputHandler::__print_string(const String& str)
{ {
if (!isReady()) return; if (!isReady()) return;
auto l_endOfConsole = [&](void) -> bool { auto l_endOfConsole = [&](void) -> bool {

View file

@ -26,12 +26,12 @@
namespace ogfx namespace ogfx
{ {
class WindowBaseOutputHandler : public ostd::OutputHandlerBase class GraphicsWindowOutputHandler : public ostd::OutputHandlerBase
{ {
public: enum class eWrapMode { TripleDots, NewLine }; public: enum class eWrapMode { TripleDots, NewLine };
public: public:
WindowBaseOutputHandler(void); GraphicsWindowOutputHandler(void);
void attachWindow(WindowBase& window); void attachWindow(GraphicsWindow& window);
void setMonospaceFont(const ostd::String& filePath); void setMonospaceFont(const ostd::String& filePath);
ostd::Vec2 getStringSize(const ostd::String& str); ostd::Vec2 getStringSize(const ostd::String& str);
bool isReady(void); bool isReady(void);
@ -58,41 +58,41 @@ namespace ogfx
uint8_t getTabWidth(void); uint8_t getTabWidth(void);
ostd::Rectangle getConsoleBounds(void); ostd::Rectangle getConsoleBounds(void);
WindowBaseOutputHandler& bg(const ostd::Color& color) override; GraphicsWindowOutputHandler& bg(const ostd::Color& color) override;
WindowBaseOutputHandler& bg(const ostd::ConsoleColors::tConsoleColor& color) override; GraphicsWindowOutputHandler& bg(const ostd::ConsoleColors::tConsoleColor& color) override;
WindowBaseOutputHandler& bg(const ostd::String& color) override; GraphicsWindowOutputHandler& bg(const ostd::String& color) override;
WindowBaseOutputHandler& fg(const ostd::Color& color) override; GraphicsWindowOutputHandler& fg(const ostd::Color& color) override;
WindowBaseOutputHandler& fg(const ostd::ConsoleColors::tConsoleColor& color) override; GraphicsWindowOutputHandler& fg(const ostd::ConsoleColors::tConsoleColor& color) override;
WindowBaseOutputHandler& fg(const ostd::String& color) override; GraphicsWindowOutputHandler& fg(const ostd::String& color) override;
WindowBaseOutputHandler& pChar(char c) override; GraphicsWindowOutputHandler& pChar(char c) override;
WindowBaseOutputHandler& pStyled(const ostd::String& styled) override; GraphicsWindowOutputHandler& pStyled(const ostd::String& styled) override;
WindowBaseOutputHandler& pStyled(const ostd::TextStyleParser::tStyledString& styled) override; GraphicsWindowOutputHandler& pStyled(const ostd::TextStyleParser::tStyledString& styled) override;
WindowBaseOutputHandler& pStyled(ostd::TextStyleBuilder::IRichStringBase& styled) override; GraphicsWindowOutputHandler& pStyled(ostd::TextStyleBuilder::IRichStringBase& styled) override;
WindowBaseOutputHandler& pObject(const ostd::BaseObject& bo) override; GraphicsWindowOutputHandler& pObject(const ostd::BaseObject& bo) override;
WindowBaseOutputHandler& p(const ostd::String& se) override; GraphicsWindowOutputHandler& p(const ostd::String& se) override;
WindowBaseOutputHandler& p(uint8_t i) override; GraphicsWindowOutputHandler& p(uint8_t i) override;
WindowBaseOutputHandler& p(int8_t i) override; GraphicsWindowOutputHandler& p(int8_t i) override;
WindowBaseOutputHandler& p(uint16_t i) override; GraphicsWindowOutputHandler& p(uint16_t i) override;
WindowBaseOutputHandler& p(int16_t i) override; GraphicsWindowOutputHandler& p(int16_t i) override;
WindowBaseOutputHandler& p(uint32_t i) override; GraphicsWindowOutputHandler& p(uint32_t i) override;
WindowBaseOutputHandler& p(int32_t i) override; GraphicsWindowOutputHandler& p(int32_t i) override;
WindowBaseOutputHandler& p(uint64_t i) override; GraphicsWindowOutputHandler& p(uint64_t i) override;
WindowBaseOutputHandler& p(int64_t i) override; GraphicsWindowOutputHandler& p(int64_t i) override;
WindowBaseOutputHandler& p(float f, uint8_t precision = 0) override; GraphicsWindowOutputHandler& p(float f, uint8_t precision = 0) override;
WindowBaseOutputHandler& p(double f, uint8_t precision = 0) override; GraphicsWindowOutputHandler& p(double f, uint8_t precision = 0) override;
WindowBaseOutputHandler& nl(void) override; GraphicsWindowOutputHandler& nl(void) override;
WindowBaseOutputHandler& tab(void) override; GraphicsWindowOutputHandler& tab(void) override;
WindowBaseOutputHandler& flush(void) override; GraphicsWindowOutputHandler& flush(void) override;
WindowBaseOutputHandler& clear(void) override; GraphicsWindowOutputHandler& clear(void) override;
WindowBaseOutputHandler& reset(void) override; GraphicsWindowOutputHandler& reset(void) override;
WindowBaseOutputHandler& xy(ostd::IPoint position) override; GraphicsWindowOutputHandler& xy(ostd::IPoint position) override;
WindowBaseOutputHandler& xy(int32_t x, int32_t y) override; GraphicsWindowOutputHandler& xy(int32_t x, int32_t y) override;
WindowBaseOutputHandler& x(int32_t x) override; GraphicsWindowOutputHandler& x(int32_t x) override;
WindowBaseOutputHandler& y(int32_t y) override; GraphicsWindowOutputHandler& y(int32_t y) override;
ostd::IPoint getCursorPosition(void) override; ostd::IPoint getCursorPosition(void) override;
void getCursorPosition(int32_t& outX, int32_t& outY) override; void getCursorPosition(int32_t& outX, int32_t& outY) override;
@ -113,7 +113,7 @@ namespace ogfx
ostd::Color m_defaultForegroundColor { 0, 220, 0, 255 }; ostd::Color m_defaultForegroundColor { 0, 220, 0, 255 };
ostd::Vec2 m_curosrPosition; ostd::Vec2 m_curosrPosition;
BasicRenderer2D m_renderer; BasicRenderer2D m_renderer;
WindowBase* m_window { nullptr }; GraphicsWindow* m_window { nullptr };
int32_t m_fontSize { 20 }; int32_t m_fontSize { 20 };
ostd::Vec2 m_charSize; ostd::Vec2 m_charSize;
ostd::IPoint m_consoleSize { 0, 0 }; ostd::IPoint m_consoleSize { 0, 0 };

View file

@ -23,8 +23,8 @@
#include <ostd/ostd.hpp> #include <ostd/ostd.hpp>
#include <ogfx/gui/RawTextInput.hpp> #include <ogfx/gui/RawTextInput.hpp>
#include <ogfx/gui/WindowBase.hpp> #include <ogfx/gui/Window.hpp>
#include <ogfx/gui/WindowBaseOutputHandler.hpp> #include <ogfx/gui/WindowOutputHandler.hpp>
#include <ogfx/render/BasicRenderer.hpp> #include <ogfx/render/BasicRenderer.hpp>
#include <ogfx/render/PixelRenderer.hpp> #include <ogfx/render/PixelRenderer.hpp>

View file

@ -19,11 +19,11 @@
*/ */
#include "BasicRenderer.hpp" #include "BasicRenderer.hpp"
#include "../gui/WindowBase.hpp" #include "../gui/Window.hpp"
namespace ogfx namespace ogfx
{ {
void BasicRenderer2D::init(WindowBase& window) void BasicRenderer2D::init(WindowCore& window)
{ {
m_window = &window; m_window = &window;
m_ttfr.init(window.getSDLRenderer()); m_ttfr.init(window.getSDLRenderer());

View file

@ -20,6 +20,7 @@
#pragma once #pragma once
#include "gui/Window.hpp"
#include <ogfx/render/FontUtils.hpp> #include <ogfx/render/FontUtils.hpp>
#include <ogfx/utils/Animation.hpp> #include <ogfx/utils/Animation.hpp>
#include <ostd/math/Geometry.hpp> #include <ostd/math/Geometry.hpp>
@ -27,16 +28,16 @@
namespace ogfx namespace ogfx
{ {
class WindowBase; class WindowCore;
class BasicRenderer2D class BasicRenderer2D
{ {
public: public:
BasicRenderer2D(void) = default; BasicRenderer2D(void) = default;
void init(WindowBase& window); void init(WindowCore& window);
inline TTFRenderer& getTTFRenderer(void) { return m_ttfr; } inline TTFRenderer& getTTFRenderer(void) { return m_ttfr; }
inline ostd::IPoint getStringSize(const ostd::String str, int32_t fontSize = 0) { return m_ttfr.getStringDimensions(str, fontSize); } inline ostd::IPoint getStringSize(const ostd::String str, int32_t fontSize = 0) { return m_ttfr.getStringDimensions(str, fontSize); }
inline WindowBase& getWindow(void) { return *m_window; } inline WindowCore& getWindow(void) { return *m_window; }
inline bool isInitialized(void) { return m_initialized; } inline bool isInitialized(void) { return m_initialized; }
void setFont(const ostd::String& fontFilePath); void setFont(const ostd::String& fontFilePath);
void setFontSize(int32_t fontSize); void setFontSize(int32_t fontSize);
@ -63,7 +64,7 @@ namespace ogfx
private: private:
TTFRenderer m_ttfr; TTFRenderer m_ttfr;
WindowBase* m_window { nullptr }; WindowCore* m_window { nullptr };
bool m_initialized { false }; bool m_initialized { false };
}; };
} }

View file

@ -19,7 +19,7 @@
*/ */
#include "PixelRenderer.hpp" #include "PixelRenderer.hpp"
#include "../gui/WindowBase.hpp" #include "../gui/Window.hpp"
#include "../../ostd/io/Memory.hpp" #include "../../ostd/io/Memory.hpp"
#include <SDL3/SDL_render.h> #include <SDL3/SDL_render.h>
@ -132,7 +132,7 @@ namespace ogfx
SDL_DestroyTexture(m_texture); SDL_DestroyTexture(m_texture);
} }
void PixelRenderer::initialize(WindowBase& parent) void PixelRenderer::initialize(WindowCore& parent)
{ {
if (isValid()) return; //TODO: Error if (isValid()) return; //TODO: Error
if (!parent.isValid() || !parent.isInitialized()) if (!parent.isValid() || !parent.isInitialized())

View file

@ -28,7 +28,7 @@
namespace ogfx namespace ogfx
{ {
class WindowBase; class WindowCore;
class PixelRenderer : public ostd::BaseObject class PixelRenderer : public ostd::BaseObject
{ {
public: class TextRenderer public: class TextRenderer
@ -83,7 +83,7 @@ namespace ogfx
public: public:
inline PixelRenderer(void) { invalidate(); } inline PixelRenderer(void) { invalidate(); }
~PixelRenderer(void); ~PixelRenderer(void);
void initialize(WindowBase& parent); void initialize(WindowCore& parent);
void handleSignal(ostd::tSignal& signal) override; void handleSignal(ostd::tSignal& signal) override;
void updateBuffer(void); void updateBuffer(void);
void displayBuffer(void); void displayBuffer(void);
@ -94,7 +94,7 @@ namespace ogfx
private: private:
uint32_t* m_pixels { nullptr }; uint32_t* m_pixels { nullptr };
SDL_Texture* m_texture { nullptr }; SDL_Texture* m_texture { nullptr };
WindowBase* m_parent { nullptr }; WindowCore* m_parent { nullptr };
int32_t m_windowWidth { 0 }; int32_t m_windowWidth { 0 };
int32_t m_windowHeight { 0 }; int32_t m_windowHeight { 0 };
}; };

View file

@ -21,7 +21,7 @@
#include "Image.hpp" #include "Image.hpp"
#include "../../io/Logger.hpp" #include "../../io/Logger.hpp"
#include "../render/BasicRenderer.hpp" #include "../render/BasicRenderer.hpp"
#include "../gui/WindowBase.hpp" #include "../gui/Window.hpp"
namespace ogfx namespace ogfx
{ {

View file

@ -27,7 +27,7 @@
namespace ogfx namespace ogfx
{ {
class WindowBase; class WindowCore;
class BasicRenderer2D; class BasicRenderer2D;
}; };

View file

@ -18,36 +18,16 @@
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>. along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/ */
/*
* Label
* Button
* Panel / Container
* Checkbox
* Radio Button (Group)
* Text Input
* Horizontal Slider
* Image / Icon
* ScrollView
* ListBox
* ComboBox
* TreeView
*/
#include <ogfx/ogfx.hpp> #include <ogfx/ogfx.hpp>
ostd::ConsoleOutputHandler out; ostd::ConsoleOutputHandler out;
class Window : public ogfx::WindowBase class Window : public ogfx::GraphicsWindow
{ {
public: public:
inline Window(void) : m_sigHandler(m_textInput, *this) { } inline Window(void) : m_sigHandler(m_textInput, *this) { }
inline void onInitialize(void) override inline void onInitialize(void) override
{ {
enableSignals();
connectSignal(ostd::tBuiltinSignals::KeyReleased);
connectSignal(ogfx::gui::RawTextInput::actionEventSignalID); connectSignal(ogfx::gui::RawTextInput::actionEventSignalID);
m_gfx.init(*this); m_gfx.init(*this);
@ -117,7 +97,7 @@ int main(int argc, char** argv)
while (window.isRunning()) while (window.isRunning())
{ {
window.update(); window.mainLoop();
} }
return 0; return 0;
} }

71
src/test/GuiTest.cpp Normal file
View file

@ -0,0 +1,71 @@
/*
OmniaFramework - A collection of useful functionality
Copyright (C) 2025 OmniaX-Dev
This file is part of OmniaFramework.
OmniaFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmniaFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Label
* Button
* Panel / Container
* Checkbox
* Radio Button (Group)
* Text Input
* Horizontal Slider
* Image / Icon
* ScrollView
* ListBox
* ComboBox
* TreeView
*/
#include <ogfx/ogfx.hpp>
ostd::ConsoleOutputHandler out;
class Window : public ogfx::gui::Window
{
public:
inline Window(void) { }
inline void onInitialize(void) override
{
}
inline void handleSignal(ostd::tSignal& signal) override
{
if (signal.ID == ostd::tBuiltinSignals::KeyReleased)
{
auto& evtData = (ogfx::KeyEventData&)signal.userData;
if (evtData.keyCode == SDLK_ESCAPE)
close();
}
}
private:
};
int main(int argc, char** argv)
{
Window window;
window.initialize(800, 600, "OmniaFramework - Test Window");
window.setClearColor({ 0, 0, 0 });
window.mainLoop();
return 0;
}