summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-09-25 10:53:18 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-09-25 10:53:18 -0500
commitb6e3cc840e255b78ee53e55b420aeee130e51ce1 (patch)
treea1cc9b47bb2858710358269059afd43808ca36cc
parent348a1bfb244288b1c78d8ce3c8d8a8cb5c1bdebc (diff)
Work on wrapping types.
-rw-r--r--src/bookmouse.cpp89
-rw-r--r--src/bookmouse.hpp4
-rw-r--r--src/demo.cpp2
-rw-r--r--src/imgui_context.cpp18
-rw-r--r--src/imgui_context.hpp58
-rw-r--r--src/main.cpp11
6 files changed, 154 insertions, 28 deletions
diff --git a/src/bookmouse.cpp b/src/bookmouse.cpp
index 65184ef..77c2304 100644
--- a/src/bookmouse.cpp
+++ b/src/bookmouse.cpp
@@ -2,6 +2,9 @@
#include <SDL_opengl.h>
#include <fud_array.hpp>
+#include <spdlog/spdlog.h>
+
+#include <atomic>
namespace bookmouse {
@@ -78,6 +81,9 @@ SDL_Event Bookmouse::pollEvent()
* hide them from your application based on those two flags.
*/
SDL_Event event{};
+ ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
+ ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
+
while (SDL_PollEvent(&event)) {
static_cast<void>(m_imgui.processEvent(event));
if (event.type == SDL_QUIT) {
@@ -93,20 +99,15 @@ SDL_Event Bookmouse::pollEvent()
void Bookmouse::updateState()
{
- bool my_tool_active{true};
- ImGui::Begin("My First Tool", &my_tool_active, ImGuiWindowFlags_MenuBar);
+ ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
+ ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
+ auto stateResult = ImGui::Begin(
+ "My First Tool",
+ &m_running, // Is this right?
+ ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_MenuBar);
+ IM_ASSERT(stateResult);
if (ImGui::BeginMenuBar()) {
- if (ImGui::BeginMenu("File")) {
- if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */
- }
- if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Do stuff */
- }
- if (ImGui::MenuItem("Close", "Ctrl+W")) {
- my_tool_active = false;
- }
- ImGui::EndMenu();
- }
- ImGui::EndMenuBar();
+ menuBar();
}
// Edit a color stored as 4 floats
@@ -124,25 +125,59 @@ void Bookmouse::updateState()
// Display contents in a scrolling region
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Important Stuff");
- ImGui::BeginChild("Scrolling");
- for (int n = 0; n < 50; n++)
- ImGui::Text("%04d: Some text", n);
- ImGui::EndChild();
+ ImGui::Text("%04d: Some text", 42);
ImGui::End();
}
+constexpr const char* OpenDialogHandle = "Open File";
+void Bookmouse::menuBar()
+{
+ bool clickOpen = false;
+ if (ImguiMenu menu{"File"}) {
+ IM_ASSERT(menu);
+ spdlog::debug("HERE {}", menu);
+ if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */
+ spdlog::debug("Open - m_openDialog was {}", m_openDialog);
+ clickOpen = true;
+ }
+ if (ImGui::MenuItem("Close", "Ctrl+Q")) {
+ m_running = false;
+ }
+ spdlog::debug("THERE");
+ }
+
+ if (ImguiMenu menu{"Help"}) {
+ IM_ASSERT(menu);
+ if (ImGui::MenuItem("About")) {
+ spdlog::debug("About");
+ }
+ ImGui::EndMenu();
+ }
+ ImGui::EndMenuBar();
+
+ if (clickOpen) {
+ clickOpen = false;
+ ImGui::OpenPopup(OpenDialogHandle);
+ }
+
+ openDialog();
+}
+
+void Bookmouse::openDialog() {
+ // if (ImGui::Begin("Another Window", &m_openDialog, 0)) {
+ if (ImGui::BeginPopupModal(OpenDialogHandle)) {
+ ImGui::Text("Hello from another window!");
+ if (ImGui::Button("Close Me")) {
+ m_openDialog = false;
+ ImGui::CloseCurrentPopup();
+ }
+ ImGui::EndPopup();
+ // ImGui::End();
+ }
+}
+
void Bookmouse::renderFrame()
{
- ImGui::Render();
- const auto& imguiIO = m_imgui.getIO();
- glViewport(0, 0, static_cast<int>(imguiIO.DisplaySize.x), static_cast<int>(imguiIO.DisplaySize.y));
- ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
- glClearColor(
- clear_color.x * clear_color.w,
- clear_color.y * clear_color.w,
- clear_color.z * clear_color.w,
- clear_color.w);
- glClear(GL_COLOR_BUFFER_BIT);
m_imgui.render();
}
diff --git a/src/bookmouse.hpp b/src/bookmouse.hpp
index 58872d8..a983025 100644
--- a/src/bookmouse.hpp
+++ b/src/bookmouse.hpp
@@ -19,12 +19,16 @@ class Bookmouse {
void updateState();
void renderFrame();
+ void menuBar();
+ void openDialog();
+
SdlContext m_sdlContext;
SdlMainWindow m_mainWindow;
GlContext m_glContext;
ImguiContext m_imgui;
bool m_running{true};
+ bool m_openDialog{false};
};
} // namespace bookmouse
diff --git a/src/demo.cpp b/src/demo.cpp
index 279e59d..40abca5 100644
--- a/src/demo.cpp
+++ b/src/demo.cpp
@@ -225,3 +225,5 @@ int demo(const fud::String& m_filename)
}
} // namespace bookmouse
+// (* 3840 2160) 8294400
+// (* 2 2560 1440) 7372800
diff --git a/src/imgui_context.cpp b/src/imgui_context.cpp
index d0f9206..f45b22b 100644
--- a/src/imgui_context.cpp
+++ b/src/imgui_context.cpp
@@ -1,5 +1,6 @@
#include "imgui_context.hpp"
+#include <SDL_opengl.h>
#include <imgui/imgui_impl_opengl3.h>
#include <imgui/imgui_impl_sdl2.h>
@@ -17,6 +18,14 @@ ImguiContext::ImguiContext(GlContext& glContext, SdlContext& sdlContext, SdlMain
ImGui::StyleColorsDark();
// ImGui::StyleColorsLight();
+ auto& style = ImGui::GetStyle();
+ style.WindowRounding = 0.0f;// <- Set this on init or use ImGui::PushStyleVar()
+ style.ChildRounding = 0.0f;
+ style.FrameRounding = 0.0f;
+ style.GrabRounding = 0.0f;
+ style.PopupRounding = 0.0f;
+ style.ScrollbarRounding = 0.0f;
+
ImGui_ImplSDL2_InitForOpenGL(mainWindow.window(), glContext.context());
ImGui_ImplOpenGL3_Init(sdlContext.glslVersion());
}
@@ -53,6 +62,15 @@ void ImguiContext::startFrame() const
void ImguiContext::render() const
{
ImGui::Render();
+ const auto& imguiIO = getIO();
+ const auto width = static_cast<int>(imguiIO.DisplaySize.x);
+ const auto height = static_cast<int>(imguiIO.DisplaySize.y);
+ glViewport(0, 0, width, height);
+ constexpr ImVec4 clearColor{0.45f, 0.55f, 0.60f, 1.00f};
+ glClearColor(clearColor.x * clearColor.w, clearColor.y * clearColor.w, clearColor.z * clearColor.w, clearColor.w);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(m_mainWindow.window());
}
diff --git a/src/imgui_context.hpp b/src/imgui_context.hpp
index 94bac3d..435db9a 100644
--- a/src/imgui_context.hpp
+++ b/src/imgui_context.hpp
@@ -1,9 +1,10 @@
#ifndef IMGUI_CONTEXT_HPP
#define IMGUI_CONTEXT_HPP
+#include "gl_context.hpp"
#include "sdl_context.hpp"
#include "sdl_main_window.hpp"
-#include "gl_context.hpp"
+
#include <imgui/imgui.h>
namespace bookmouse {
@@ -24,6 +25,61 @@ class ImguiContext {
SdlMainWindow& m_mainWindow;
};
+template <typename Expr, typename Cleanup>
+class ConditionalRaii {
+ public:
+ template <typename... Args>
+ ConditionalRaii(Args&&... args) : m_expr{false}
+ {
+ Expr expr{};
+ m_expr = expr(std::forward<Args>(args)...);
+ }
+ ~ConditionalRaii()
+ {
+ Cleanup cleanup{};
+ if (m_expr) {
+ cleanup();
+ }
+ }
+ ConditionalRaii(const ConditionalRaii& rhs) = delete;
+ ConditionalRaii(ConditionalRaii&& rhs) = delete;
+ ConditionalRaii& operator=(const ConditionalRaii& rhs) = delete;
+ ConditionalRaii& operator=(ConditionalRaii&& rhs) = delete;
+ operator bool() const
+ {
+ return m_expr;
+ }
+
+ private:
+ bool m_expr;
+};
+
+#define STRUCT_FUNCTOR(FUNCTOR_NAME, FUNCTOR_FUNCTION) \
+ struct FUNCTOR_NAME { \
+ auto operator()() -> decltype(FUNCTOR_FUNCTION()) \
+ { \
+ return FUNCTOR_FUNCTION(); \
+ } \
+ };
+
+#define STRUCT_FUNCTOR_ARGS(FUNCTOR_NAME, FUNCTOR_FUNCTION) \
+ struct FUNCTOR_NAME { \
+ template <typename... Args> \
+ auto operator()(Args&&... args) -> /* FORCE BREAK */ \
+ decltype(FUNCTOR_FUNCTION(std::forward<Args>(args)...)) \
+ { \
+ return FUNCTOR_FUNCTION(std::forward<Args>(args)...); \
+ } \
+ };
+
+STRUCT_FUNCTOR_ARGS(ImGuiBeginMenu, ImGui::BeginMenu)
+STRUCT_FUNCTOR(ImGuiEndMenu, ImGui::EndMenu)
+
+using ImguiMenu = ConditionalRaii<ImGuiBeginMenu, ImGuiEndMenu>;
+
+#undef STRUCT_FUNCTOR
+#undef STRUCT_FUNCTOR_ARGS
+
} // namespace bookmouse
#endif
diff --git a/src/main.cpp b/src/main.cpp
index d172586..50ddd7e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,11 +3,14 @@
#include "bookmouse.hpp"
+#include <libfud.hpp>
+
void setupLogging()
{
spdlog::cfg::load_env_levels();
spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v");
+ spdlog::set_level(spdlog::level::debug);
}
int main(int argc, char* argv[])
@@ -17,6 +20,14 @@ int main(int argc, char* argv[])
setupLogging();
+ auto result = fud::getEnv("HOME");
+ if (result.isError()) {
+ spdlog::error("Error getting home variable");
+ } else {
+ auto home = result.getOkay();
+ spdlog::info("Home is {}", home.c_str());
+ }
+
bookmouse::Bookmouse bookmouse{};
return bookmouse.run();
}