diff --git a/CMakeLists.txt b/CMakeLists.txt index d4dd6fe..38d2193 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(PRECOMPILE_HEADERS set(SOURCE_HEADERS include/imgui/imconfig.h include/imgui/imgui.h + include/imgui/imgui_stdlib.h include/imgui/imgui_impl_glfw.h include/imgui/imgui_impl_opengl3.h include/imgui/imgui_impl_opengl3_loader.h @@ -28,10 +29,14 @@ set(SOURCE_HEADERS include/characteristic/endurance.hpp include/characteristic/chance.hpp include/gui/menu/menu.hpp - include/gui/menu/menu_data.hpp include/characteristic/characteristic.hpp) + include/gui/menu/menu_data.hpp + include/characteristic/characteristic.hpp + include/controller.hpp + ) set(SOURCE_FILES src/imgui/imgui.cpp + src/imgui/imgui_stdlib.cpp src/imgui/imgui_draw.cpp src/imgui/imgui_impl_glfw.cpp src/imgui/imgui_impl_opengl3.cpp @@ -46,6 +51,7 @@ set(SOURCE_FILES src/characteristic/endurance.cpp src/characteristic/chance.cpp src/gui/menu/menu.cpp + src/controller.cpp ) set(SOURCES diff --git a/include/character_sheet.hpp b/include/character_sheet.hpp index afc8042..8f6641e 100644 --- a/include/character_sheet.hpp +++ b/include/character_sheet.hpp @@ -7,9 +7,14 @@ #include "characteristic/habilete.hpp" #include +namespace gui { + class Gui; +} + namespace character { class CharacterSheet final { private: + friend gui::Gui; std::mt19937_64 engine{ std::random_device{ "rdseed" }() }; std::string caractere{}; @@ -70,7 +75,7 @@ namespace character { } }; - void to_json(json &j, const CharacterSheet &billy) { + static void to_json(json &j, const CharacterSheet &billy) { j["caractere"] = billy.get_caractere(); j["adresse"] = billy.get_adresse(); j["endurance"] = billy.get_endurance(); diff --git a/include/characteristic/adresse.hpp b/include/characteristic/adresse.hpp index ec85225..b1478e5 100644 --- a/include/characteristic/adresse.hpp +++ b/include/characteristic/adresse.hpp @@ -18,7 +18,7 @@ namespace character::characteristic { } }; - void to_json(json &j, const Adresse &adresse) { + static void to_json(json &j, const Adresse &adresse) { to_json(j, static_cast(adresse)); } } diff --git a/include/characteristic/chance.hpp b/include/characteristic/chance.hpp index e7eb28c..9b7d5ef 100644 --- a/include/characteristic/chance.hpp +++ b/include/characteristic/chance.hpp @@ -15,7 +15,7 @@ namespace character::characteristic { } }; - void to_json(json &j, const Chance &chance) { + static void to_json(json &j, const Chance &chance) { to_json(j, static_cast(chance)); } } diff --git a/include/characteristic/characteristic.hpp b/include/characteristic/characteristic.hpp index 803f432..8e854db 100644 --- a/include/characteristic/characteristic.hpp +++ b/include/characteristic/characteristic.hpp @@ -38,7 +38,7 @@ namespace character::characteristic { } }; - void to_json(json &j, const Characteristic &charac) { + static void to_json(json &j, const Characteristic &charac) { j["base"] = charac.get_base(); j["carac"] = charac.get_carac(); j["materiel"] = charac.get_materiel(); diff --git a/include/characteristic/endurance.hpp b/include/characteristic/endurance.hpp index c012aaa..76bef84 100644 --- a/include/characteristic/endurance.hpp +++ b/include/characteristic/endurance.hpp @@ -20,7 +20,7 @@ namespace character::characteristic { } }; - void to_json(json &j, const Endurance &endurance) { + static void to_json(json &j, const Endurance &endurance) { to_json(j, static_cast(endurance)); } } diff --git a/include/characteristic/habilete.hpp b/include/characteristic/habilete.hpp index 0efd63f..a50d67c 100644 --- a/include/characteristic/habilete.hpp +++ b/include/characteristic/habilete.hpp @@ -16,7 +16,7 @@ namespace character::characteristic { } }; - void to_json(json &j, const Habilete &habilete) { + static void to_json(json &j, const Habilete &habilete) { to_json(j, static_cast(habilete)); } } diff --git a/include/controller.hpp b/include/controller.hpp new file mode 100644 index 0000000..d9033d0 --- /dev/null +++ b/include/controller.hpp @@ -0,0 +1,10 @@ +#ifndef BILLYSHEET_CONTROLLER_HPP +#define BILLYSHEET_CONTROLLER_HPP + + +class Controller { + +}; + + +#endif //BILLYSHEET_CONTROLLER_HPP diff --git a/include/gui/gui.hpp b/include/gui/gui.hpp index cfff485..557fef3 100644 --- a/include/gui/gui.hpp +++ b/include/gui/gui.hpp @@ -18,7 +18,7 @@ namespace gui { menu::Menu menu; - fs::path font; + const fs::path font; bool initialized{ false }; public: diff --git a/include/imgui/imgui_stdlib.h b/include/imgui/imgui_stdlib.h new file mode 100644 index 0000000..f860b0c --- /dev/null +++ b/include/imgui/imgui_stdlib.h @@ -0,0 +1,22 @@ +// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) +// This is also an example of how you may wrap your own similar types. + +// Compatibility: +// - std::string support is only guaranteed to work from C++11. +// If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture) + +// Changelog: +// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string + +#pragma once + +#include + +namespace ImGui +{ + // ImGui::InputText() with std::string + // Because text input needs dynamic resizing, we need to setup a callback to grow the capacity + IMGUI_API bool InputText(const char* label, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextMultiline(const char* label, std::string* str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); +} diff --git a/src/controller.cpp b/src/controller.cpp new file mode 100644 index 0000000..8181d8f --- /dev/null +++ b/src/controller.cpp @@ -0,0 +1 @@ +#include "controller.hpp" diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 87517af..da2a66e 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -1,6 +1,7 @@ #include "gui/gui.hpp" #include "gui/gui_data.hpp" #include "imgui.h" +#include "imgui_stdlib.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include "imgui_impl_opengl3_loader.h" @@ -47,6 +48,44 @@ void gui::Gui::render_gui() { ImGui::Text("Hello world!"); ImGui::Text("Average framerate: %.3f ms/frame (%.1f FPS)", 1000.f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + { + ImGui::BeginChild("left pane", + ImVec2(ImGui::GetWindowWidth() / 2, -ImGui::GetFrameHeightWithSpacing() - + ImGui::GetStyle().ItemSpacing.y * 4 - + ImGui::GetStyle().ChildBorderSize - + ImGui::GetStyle().FrameBorderSize), + true); + ImGui::Text("Caractère"); + // Remove label + ImGui::PushItemWidth(-1); + ImGui::InputTextMultiline("Caractère", &data.billy.caractere); + ImGui::PopItemWidth(); + ImGui::TreePop(); + ImGui::EndChild(); + } + ImGui::SameLine(); + + { + ImGui::BeginChild("Right Pane", + ImVec2(0, + -ImGui::GetFrameHeightWithSpacing() - + ImGui::GetStyle().ItemSpacing.y * 4 - + ImGui::GetStyle().ChildBorderSize - + ImGui::GetStyle().FrameBorderSize), + true); + ImGui::Text("Hello"); + ImGui::EndChild(); + } + + { + ImGui::BeginChild("Bottom pane", ImVec2(0, 0), true); + ImGui::Text("Bottom pane"); + ImGui::Text("Bottom pane1"); + ImGui::Text("Bottom pane2"); + ImGui::Text("Bottom pane3"); + ImGui::Text("Bottom pane4"); + ImGui::EndChild(); + } ImGui::End(); diff --git a/src/gui/menu/menu.cpp b/src/gui/menu/menu.cpp index be50335..34ed09e 100644 --- a/src/gui/menu/menu.cpp +++ b/src/gui/menu/menu.cpp @@ -1,14 +1,22 @@ #include "gui/menu/menu.hpp" #include "imgui.h" #include "gui/menu/menu_data.hpp" +#include +#include void gui::menu::Menu::gui() const noexcept { if (ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem("Save")) { + if (ImGui::MenuItem("Open file")) { } + if (ImGui::MenuItem("Save")) { + std::ofstream file{ data.save_path / data.filename }; + nlohmann::json j; + j.emplace("character_sheet", data) + } + if (ImGui::MenuItem("Save as...")) { } diff --git a/src/imgui/imgui_stdlib.cpp b/src/imgui/imgui_stdlib.cpp new file mode 100644 index 0000000..cb1fe17 --- /dev/null +++ b/src/imgui/imgui_stdlib.cpp @@ -0,0 +1,76 @@ +// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) +// This is also an example of how you may wrap your own similar types. + +// Compatibility: +// - std::string support is only guaranteed to work from C++11. +// If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture) + +// Changelog: +// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string + +#include "imgui.h" +#include "imgui_stdlib.h" + +struct InputTextCallback_UserData +{ + std::string* Str; + ImGuiInputTextCallback ChainCallback; + void* ChainCallbackUserData; +}; + +static int InputTextCallback(ImGuiInputTextCallbackData* data) +{ + InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData; + if (data->EventFlag == ImGuiInputTextFlags_CallbackResize) + { + // Resize string callback + // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want. + std::string* str = user_data->Str; + IM_ASSERT(data->Buf == str->c_str()); + str->resize(data->BufTextLen); + data->Buf = (char*)str->c_str(); + } + else if (user_data->ChainCallback) + { + // Forward to user callback, if any + data->UserData = user_data->ChainCallbackUserData; + return user_data->ChainCallback(data); + } + return 0; +} + +bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + flags |= ImGuiInputTextFlags_CallbackResize; + + InputTextCallback_UserData cb_user_data; + cb_user_data.Str = str; + cb_user_data.ChainCallback = callback; + cb_user_data.ChainCallbackUserData = user_data; + return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data); +} + +bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + flags |= ImGuiInputTextFlags_CallbackResize; + + InputTextCallback_UserData cb_user_data; + cb_user_data.Str = str; + cb_user_data.ChainCallback = callback; + cb_user_data.ChainCallbackUserData = user_data; + return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data); +} + +bool ImGui::InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + flags |= ImGuiInputTextFlags_CallbackResize; + + InputTextCallback_UserData cb_user_data; + cb_user_data.Str = str; + cb_user_data.ChainCallback = callback; + cb_user_data.ChainCallbackUserData = user_data; + return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data); +}