Setting up run Component

This commit is contained in:
noffie 2024-12-28 01:51:07 +01:00
parent d375f9ca7d
commit 5c8504288b
13 changed files with 586 additions and 157 deletions

View file

@ -121,6 +121,8 @@ add_executable(${GUI_TARGET_NAME}
${SVA_GUI_FILES} ${SVA_GUI_FILES}
) )
source_group(TREE ${PROJECT_SOURCE_DIR}/src/ PREFIX "Source" FILES ${SVA_GUI_FILES})
# --------------- # ---------------
# RAYLIB # RAYLIB
# --------------- # ---------------
@ -209,4 +211,5 @@ if(${SVA_CREATE_INCLUDES_TARGET})
SOURCES SOURCES
${INCLUDE_FILES} ${INCLUDE_FILES}
) )
source_group(TREE ${PROJECT_SOURCE_DIR}/include/ PREFIX "Source" FILES ${INCLUDE_FILES})
endif() endif()

View file

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <assert.h> #include <assert.h>
#include <types.hpp>
struct timer_result struct timer_result
{ {

View file

@ -0,0 +1,129 @@
#include "BigSortComponent.hpp"
#include <raylibs/raygui.h>
#include "raylibs/raygui.h"
BigSortComponent::BigSortComponent() : m_Sorter(2000)
{
}
int BigSortComponent::draw()
{
// raygui: controls drawing
//----------------------------------------------------------------------------------
if (m_FunctionDropdown_EditMode)
GuiLock();
GuiGroupBox({ anchor01.x + 0, anchor01.y + 8, 456, 304 }, "Run Sorting");
{
if (GuiTextBox({ anchor02.x + 8, anchor02.y + 8, 216, 24 }, m_FunctionTextBox_Text, 128, m_FunctionTextBox_EditMode)) m_FunctionTextBox_EditMode = !m_FunctionTextBox_EditMode;
GuiGroupBox({ anchor02.x + 0, anchor02.y + 0, 232, 72 }, "Add & Check Function");
{
if (GuiButton({ anchor02.x + 72, anchor02.y + 40, 72, 24 }, "Check"))
{
// FATAL: asserts
sol::optional<sol::protected_function> func = m_LuaState[m_FunctionTextBox_Text];
if (!func)
{
m_ErrorWindow.error("Function does not exits", "A function was selected, that does not exist.");
// TODO: formating works differently
spdlog::error("BigSortComponent: Function does not exist %s", m_FunctionTextBox_Text);
repack_function_list();
}
}
if (GuiButton({ anchor02.x + 152, anchor02.y + 40, 72, 24 }, "Add"))
{
// TODO: Check for validity
add_function(m_FunctionTextBox_Text);
}
}
if (GuiButton({ anchor01.x + 8, anchor01.y + 48, 56, 24 }, "Run"))
{
// TODO: Run Test
}
GuiLine({ anchor01.x + 8, anchor01.y + 96, 440, 12 }, nullptr);
// TODO: format infos into strings;
GuiLabel({ anchor01.x + 8, anchor01.y + 176, 144, 24 }, "Is Sorted: <bool>");
GuiLabel({ anchor01.x + 8, anchor01.y + 200, 144, 24 }, "Time elapsed: <double>: ");
GuiGroupBox({ anchor03.x + 0, anchor03.y + 0, 288, 104 }, "Calls");
GuiLabel({ anchor03.x + 8, anchor03.y + 8, 120, 24 }, "Swap: <size_t>");
GuiLabel({ anchor03.x + 8, anchor03.y + 32, 120, 24 }, "Size: <size_t>");
GuiLabel({ anchor03.x + 136, anchor03.y + 8, 120, 24 }, "Greater: <size_t>");
GuiLabel({ anchor03.x + 136, anchor03.y + 32, 120, 24 }, "Lesser: <size_t>");
GuiLabel({ anchor03.x + 136, anchor03.y + 56, 120, 24 }, "Equal: <size_t>");
GuiLabel({ anchor01.x + 8, anchor01.y + 152, 144, 24 }, "Array size: <size_t>");
GuiLabel({ anchor01.x + 8, anchor01.y + 112, 144, 24 }, "Tests ran: <size_t>");
GuiLabel({ anchor01.x + 152, anchor01.y + 112, 144, 24 }, "Test number: <size_t>");
if (GuiButton({ anchor01.x + 112, anchor01.y + 264, 88, 24 }, "Next"))
{
// TODO: implement Test result stack movement
}
if (GuiButton({ anchor01.x + 8, anchor01.y + 264, 88, 24 }, "Previous"))
{
// TODO: Display previous result
}
if (GuiButton({ anchor01.x + 320, anchor01.y + 264, 120, 24 }, "Clear All Test"))
{
m_SortResults.clear();
}
if (GuiDropdownBox({ anchor01.x + 8, anchor01.y + 16, 184, 24 }, m_FunctionList.c_str(), &m_FunctionDropdown_Active, m_FunctionDropdown_EditMode)) m_FunctionDropdown_EditMode = !m_FunctionDropdown_EditMode;
}
GuiUnlock();
//----------------------------------------------------------------------------------
return m_ErrorWindow.draw();
}
int BigSortComponent::input()
{
return m_ErrorWindow.input();
}
void BigSortComponent::onAttach()
{
sva::Sorter::setup(m_LuaState);
m_LuaState["list"] = m_Sorter;
}
void BigSortComponent::run_test()
{
std::string function_name = m_FunctionNames[m_FunctionDropdown_Active];
spdlog::debug("%s: Running sorting function: %s", function_name.c_str(), function_name.c_str());
m_SortResults.push_back(m_Sorter.run(m_LuaState[function_name]));
if (!m_SortResults.back().is_sorted)
{
spdlog::debug("%s: Could not sort the array", function_name.c_str());
}
}
void BigSortComponent::add_function(const std::string& function_name)
{
m_FunctionNames.push_back(function_name);
if (m_FunctionList.empty())
{
m_FunctionList = function_name;
return;
}
m_FunctionList += ';' + function_name;
}
void BigSortComponent::repack_function_list()
{
m_FunctionList.clear();
for (const auto& fn : m_FunctionNames)
{
m_FunctionList += fn + ';';
}
m_FunctionList.pop_back();
}

View file

@ -0,0 +1,48 @@
#pragma once
#include "../gui/GuiComponent.hpp"
#include "../Sorter.hpp"
#include <raylibs/raygui.h>
#include <string>
#include "../gui/CoreComponents.hpp"
class BigSortComponent : public sva::GuiComponent
{
Vector2 m_ComponentOffset = { 0,0 };
// BigSortComponent: controls initialization
//----------------------------------------------------------------------------------
Vector2 anchor01 = { 8, 8 };
Vector2 anchor02 = { 224, 24 };
Vector2 anchor03 = { 160, 152 };
bool m_FunctionDropdown_EditMode = false;
int m_FunctionDropdown_Active = 0;
bool m_FunctionTextBox_EditMode = false;
char m_FunctionTextBox_Text[256] = "Name own Function";
sva::ErrorWindow m_ErrorWindow;
//----------------------------------------------------------------------------------
public:
BigSortComponent();
int draw() override;
int input() override;
void onAttach() override;
private:
sol::state m_LuaState;
std::vector<std::string> m_FunctionNames;
std::string m_FunctionList;
sva::Sorter m_Sorter;
std::vector<sva::Sorter::SortResult> m_SortResults;
sva::Sorter::SortResult* m_ActiveSortResult = nullptr;
void run_test();
void add_function(const std::string&);
void repack_function_list();
};

View file

@ -0,0 +1,83 @@
#include "./SaveClosePopup.hpp"
SafeClosePopup::~SafeClosePopup()
{
CloseWindow();
}
void SafeClosePopup::onAttach()
{
m_WindowOpen = false;
}
int SafeClosePopup::draw()
{
if (WindowShouldClose())
{
if (m_WindowOpen) {
CloseWindow();
}
else {
OpenWindow();
}
onResize();
}
if (m_WindowOpen)
{
GuiUnlock();
anchor03 = { .x = m_WndRect.x + 168, .y = m_WndRect.y + 88 };
m_WindowOpen = !GuiWindowBox(m_WndRect, "#191# Are you sure you want to close this program?");
{ // Drawing in different colours
int border_color = GuiGetStyle(DEFAULT, BORDER_COLOR_NORMAL);
int base_color = GuiGetStyle(DEFAULT, BASE_COLOR_NORMAL);
GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, 0x7192C2FF);
GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, 0xBBCDD3FF);
m_CQB_YesButton = GuiButton({ anchor03.x + -152, anchor03.y + 32, 120, 24 }, "#112#Yes");
GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, border_color);
GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, base_color);
}
m_CQB_NoButton = GuiButton({ anchor03.x + 24, anchor03.y + 32, 120, 24 }, "#113#No");
GuiLabel({ anchor03.x + -104, anchor03.y + -40, 208, 24 }, "Are you sure you want to close this?");
GuiLabel({ anchor03.x + -56, anchor03.y + -8, 120, 24 }, "Press \"Yes\" to close");
if (m_CQB_YesButton)
{
*m_WndRunning = false;
return 1;
}
if (m_CQB_NoButton) CloseWindow();
}
return 0;
}
int SafeClosePopup::rinput(Vector2& mouse_position)
{
if (IsKeyReleased(KEY_ENTER)) {
*m_WndRunning = false;
return 1;
}
GuiLock();
return 0;
}
void SafeClosePopup::onResize()
{
anchor03 = { static_cast<float>(m_WndWidth) / 2 , static_cast<float>(m_WndHeight) / 2 };
m_WndRect = { anchor03.x + -168, anchor03.y + -88, 328, 160 };
}
void SafeClosePopup::OpenWindow()
{
GuiLock();
m_WindowOpen = true;
}
void SafeClosePopup::CloseWindow()
{
m_WindowOpen = false;
GuiUnlock();
}

View file

@ -0,0 +1,37 @@
#pragma once
#include "../gui/CoreComponents.hpp"
/****************************************************
* A "Are you sure you want to exit?" Window.
*
* Yes is default
****************************************************/
class SafeClosePopup final : public sva::GuiMovableWindow
{
Vector2 anchor03 = { 0,0 };
bool m_CQB_YesButton = false;
bool m_CQB_NoButton = false;
public:
~SafeClosePopup() override;
void onAttach() override;
int draw() override;
int rinput(Vector2& mouse_position) override;
void onResize() override;
public:
void OpenWindow();
void CloseWindow();
};

118
src/Sorter.cpp Normal file
View file

@ -0,0 +1,118 @@
#include "Sorter.hpp"
#include "Profiling/Timer.hpp"
#define BOOL_FMT "%s"
#define BOOL_ARG(exp) (exp) ? "true" : "false"
namespace sva {
Sorter::Sorter(size_t size) : m_SortArray(size)
{
populate(size);
spdlog::debug("Sorter: created -- size: %lld, is-sorted: " BOOL_FMT, size, BOOL_ARG(is_sorted()));
}
Sorter::SortResult Sorter::run(const sol::protected_function& sort_func)
{
SortResult res;
timer_result tres;
if (m_ActiveSortResult) spdlog::error("Sorter: A Sort already running...");
m_ActiveSortResult = &res;
Timer timer(tres, timer.paused);
spdlog::debug("Sorter: running function");
timer.set_state(timer.running);
sol::function_result result = sort_func();
timer.set_state(timer.paused);
if (!result.valid())
{
spdlog::critical("Sol.Lua: Function result is invalid");
}
if (m_ActiveSortResult) spdlog::error("Sorter: Something went wrong while running the test. There is no active result");
m_ActiveSortResult = nullptr;
return res;
}
void Sorter::setup(sol::state& state)
{
state.open_libraries(sol::lib::coroutine, sol::lib::table);
state.new_usertype<Sorter>("Sorter",
"size", &Sorter::list_size,
"swap", &Sorter::list_swap,
"greater", &Sorter::list_greater,
"less", &Sorter::list_less,
"equal", &Sorter::list_equal
);
}
bool Sorter::list_greater(size_t a, size_t b) const
{
if (m_ActiveSortResult)
m_ActiveSortResult->calls.nGreater++;
return m_SortArray[a] > m_SortArray[b];
}
bool Sorter::list_less(size_t a, size_t b) const
{
if (m_ActiveSortResult)
m_ActiveSortResult->calls.nLesser++;
return m_SortArray[a] < m_SortArray[b];
}
bool Sorter::list_equal(size_t a, size_t b) const
{
if (m_ActiveSortResult)
m_ActiveSortResult->calls.nEqual++;
return m_SortArray[a] == m_SortArray[b];
}
void Sorter::list_swap(size_t a, size_t b)
{
if (m_ActiveSortResult)
m_ActiveSortResult->calls.nSwap++;
// list[index1] ^= list[index2];
// list[index2] ^= list[index1];
// list[index1] ^= list[index2];
auto tmp = m_SortArray[a];
m_SortArray[a] = m_SortArray[b];
m_SortArray[b] = tmp;
}
size_t Sorter::list_size() const
{
if (m_ActiveSortResult)
m_ActiveSortResult->calls.nSize++;
return m_SortArray.size();
}
bool Sorter::is_sorted() const
{
if (m_SortArray.size() <= 1)
return true;
for (size_t i = 1; i < m_SortArray.size(); i++)
{
if (m_SortArray[i - 1] > m_SortArray[i])
return false;
}
return true;
}
void Sorter::populate(size_t size)
{
std::random_device dev;
std::mt19937_64 rng(dev());
std::uniform_int_distribution<std::mt19937_64::result_type> dist(0, SIZE_MAX);
for (size_t i = 0; i < size; i++)
{
m_SortArray.at(i) = dist(rng);
}
}
}

47
src/Sorter.hpp Normal file
View file

@ -0,0 +1,47 @@
#pragma once
#include <random>
#include <sol/sol.hpp>
#include <spdlog/spdlog.h>
namespace sva {
class Sorter
{
public:
struct SortResult
{
bool is_sorted;
double elapsed_time;
struct
{
size_t nSwap;
size_t nSize;
size_t nGreater;
size_t nLesser;
size_t nEqual;
} calls;
};
private:
std::vector<size_t> m_SortArray;
SortResult* m_ActiveSortResult;
public:
Sorter(size_t size);
SortResult run(const sol::protected_function& sort_func);
static void setup(sol::state& state);
protected:
void list_swap(size_t, size_t);
bool list_greater(size_t, size_t) const;
bool list_less(size_t, size_t) const;
bool list_equal(size_t, size_t) const;
size_t list_size() const;
bool is_sorted() const;
void populate(size_t);
};
}

View file

@ -0,0 +1,85 @@
#include "./CoreComponents.hpp"
namespace sva {
GuiMovableWindow::GuiMovableWindow()
{
m_Title = "Example movable Window";
}
int GuiMovableWindow::input()
{
if (!m_WindowOpen) return 0;
Vector2 mouse_pos = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !m_DragWindow)
{
if (CheckCollisionPointRec(mouse_pos, { m_WndRect.x, m_WndRect.y, m_WndRect.width - 24, 20 }))
{
m_DragWindow = true;
m_PanOffset = { mouse_pos.x - m_WndRect.x, mouse_pos.y - m_WndRect.y };
}
}
if (m_DragWindow)
{
m_WndRect.x = (mouse_pos.x - m_PanOffset.x);
m_WndRect.y = (mouse_pos.y - m_PanOffset.y);
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) m_DragWindow = false;
}
return rinput(mouse_pos);
}
int GuiMovableWindow::rinput(Vector2& mouse_position)
{
return 0;
}
int GuiMovableWindow::rdraw()
{
return 0;
}
int GuiMovableWindow::draw()
{
if (!m_WindowOpen) return 0;
m_WindowOpen = !GuiWindowBox(m_WndRect, m_Title.c_str());
return rdraw();
}
void ErrorWindow::onAttach()
{
m_WindowOpen = false;
}
int ErrorWindow::rdraw()
{
GuiLabel({ m_WndRect.x + m_TextAnchor.x, m_WndRect.y + m_TextAnchor.y, m_WndRect.width, m_WndRect.height }, m_Msg.c_str());
return 0;
}
void ErrorWindow::onResize()
{
m_WndRect.x = (static_cast<float>(m_WndWidth) - m_WndRect.width) / 2;
m_WndRect.y = (static_cast<float>(m_WndHeight) - m_WndRect.height) / 2;
}
void ErrorWindow::error(std::string title, std::string msg)
{
m_Msg = std::move(msg);
float text_padding = 20;
m_WndRect.width = static_cast<float>(MeasureText(m_Msg.c_str(), GuiGetStyle(DEFAULT, TEXT_SIZE))) + text_padding;
m_WndRect.height = (m_WndRect.width * 2) / 3;
m_TextAnchor = {
.x = text_padding / 2,
.y = m_WndRect.height / 2 - static_cast<float>(GuiGetStyle(DEFAULT, TEXT_SIZE))
};
m_WindowOpen = true;
m_Title = std::move(title);
}
}

View file

@ -3,7 +3,7 @@
#include <raylibs/raygui.h> #include <raylibs/raygui.h>
#include <string> #include <string>
namespace sva {
class GuiMovableWindow : public sva::GuiComponent class GuiMovableWindow : public sva::GuiComponent
{ {
private: private:
@ -12,154 +12,29 @@ private:
protected: protected:
Rectangle m_WndRect = { 20,20, 200, 100 }; Rectangle m_WndRect = { 20,20, 200, 100 };
bool m_WindowOpen = true; bool m_WindowOpen = true;
std::string m_Title;
public: public:
GuiMovableWindow();
virtual int input() override virtual int input() override;
{
if (!m_WindowOpen) return 0;
Vector2 mouse_pos = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !m_DragWindow) virtual int rinput(Vector2& mouse_position);
{ virtual int rdraw();
if (CheckCollisionPointRec(mouse_pos, { m_WndRect.x, m_WndRect.y, m_WndRect.width - 24, 20 }))
{
m_DragWindow = true;
m_PanOffset = { mouse_pos.x - m_WndRect.x, mouse_pos.y - m_WndRect.y };
}
}
if (m_DragWindow) virtual int draw() override;
{
m_WndRect.x = (mouse_pos.x - m_PanOffset.x);
m_WndRect.y = (mouse_pos.y - m_PanOffset.y);
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) m_DragWindow = false;
}
return rinput(mouse_pos);
}
virtual int rinput(Vector2& mouse_position) { return 0; };
virtual int draw() override
{
if (!m_WindowOpen) return 0;
m_WindowOpen = !GuiWindowBox(m_WndRect, "Example Movable Window");
return 0;
}
}; };
class ErrorWindow : public GuiMovableWindow
/****************************************************
* A "Are you sure you want to exit?" Window.
*
* Yes is default
* TODO: Make the Yes-Button standout as the default
****************************************************/
class SafeClosePopup final : public GuiMovableWindow
{ {
Vector2 anchor03 = { 0,0 }; std::string m_Msg;
Vector2 m_TextAnchor = { 0,0 };
bool m_CQB_YesButton = false;
bool m_CQB_NoButton = false;
public: public:
void onAttach() override;
~SafeClosePopup() { int rdraw() override;
CloseWindow();
}
void onAttach() override void onResize() override;
{
m_WindowOpen = false;
}
int draw() override void error(std::string title, std::string msg);
{
if (WindowShouldClose())
{
if (m_WindowOpen) {
CloseWindow();
}
else {
OpenWindow();
}
onResize();
}
if (m_WindowOpen)
{
GuiUnlock();
anchor03 = { m_WndRect.x + 168, m_WndRect.y + 88 };
m_WindowOpen = !GuiWindowBox(m_WndRect, "#191# Are you sure you want to close this program?");
int border_color = GuiGetStyle(DEFAULT, BORDER_COLOR_NORMAL);
int base_color = GuiGetStyle(DEFAULT, BASE_COLOR_NORMAL);
GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, 0x7192C2FF);
GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, 0xBBCDD3FF);
m_CQB_YesButton = GuiButton({ anchor03.x + -152, anchor03.y + 32, 120, 24 }, "#112#Yes");
GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, border_color);
GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, base_color);
m_CQB_NoButton = GuiButton({ anchor03.x + 24, anchor03.y + 32, 120, 24 }, "#113#No");
GuiLabel({ anchor03.x + -104, anchor03.y + -40, 208, 24 }, "Are you sure you want to close this?");
GuiLabel({ anchor03.x + -56, anchor03.y + -8, 120, 24 }, "Press \"Yes\" to close");
if (m_CQB_YesButton)
{
*m_WndRunning = false;
return 1;
}
if (m_CQB_NoButton) CloseWindow();
}
return 0;
}
int rinput(Vector2& mouse_position) override
{
if (IsKeyReleased(KEY_ENTER)) {
*m_WndRunning = false;
return 1;
}
GuiLock();
return 0;
}
void onResize() override
{
anchor03 = { static_cast<float>(m_WndWidth) / 2 , static_cast<float>(m_WndHeight) / 2 };
m_WndRect = { anchor03.x + -168, anchor03.y + -88, 328, 160 };
}
public:
void OpenWindow() {
GuiLock();
m_WindowOpen = true;
}
void CloseWindow() {
m_WindowOpen = false;
GuiUnlock();
}
}; };
class SettingsComponent final : public sva::GuiComponent {
public:
struct {
bool borderlessFullscreen;
uint32_t state;
} values;
void onAttach() override {
values.borderlessFullscreen = false;
values.state = 0;
} }
int draw() override {
if (GuiButton({ 100, 100, 300, 30 }, "This should not work when the safe window is open!")) {
std::cout << "got pressed\n";
}
return 0;
}
};

View file

@ -21,7 +21,10 @@
#include <sol/debug.hpp> #include <sol/debug.hpp>
#include "gui/ComponentStack.hpp" #include "gui/ComponentStack.hpp"
#include "gui/CoreComponents.hpp"
// gui components
#include "Components/SaveClosePopup.hpp"
#include "Components/BigSortComponent.hpp"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
@ -74,7 +77,7 @@ int main(int argc, char** argv)
int run_result = 0; int run_result = 0;
ComponentStack::push<SettingsComponent>(); ComponentStack::push<BigSortComponent>();
// always on top... // always on top...
SafeClosePopup safe_close_popup; SafeClosePopup safe_close_popup;