diff options
Diffstat (limited to 'src/bookmouse.cpp')
-rw-r--r-- | src/bookmouse.cpp | 211 |
1 files changed, 127 insertions, 84 deletions
diff --git a/src/bookmouse.cpp b/src/bookmouse.cpp index 9ba09c4..65184ef 100644 --- a/src/bookmouse.cpp +++ b/src/bookmouse.cpp @@ -1,106 +1,149 @@ #include "bookmouse.hpp" -#include "main_window.hpp" + +#include <SDL_opengl.h> +#include <fud_array.hpp> namespace bookmouse { -Bookmouse::Bookmouse() +Bookmouse::Bookmouse() : + m_sdlContext{}, m_mainWindow{m_sdlContext}, m_glContext{m_mainWindow}, + m_imgui{m_glContext, m_sdlContext, m_mainWindow} { - m_layout = new QVBoxLayout(); - - m_layout->addWidget(&m_pageLeft); - m_layout->addWidget(&m_pageRight); - - setLayout(m_layout); + /* + * Load Fonts + * + * - If no fonts are loaded, dear imgui will use the default + * font. You can also load multiple fonts and use + * ImGui::PushFont()/PopFont() to select them. + * - AddFontFromFileTTF() will return the ImFont* so you can store + * it if you need to select the font among multiple. + * - If the file cannot be loaded, the function will return a + * nullptr. Please handle those errors in your application + * (e.g. use an assertion, or display an error and quit). + * - The fonts will be rasterized at a given size (w/ + * oversampling) and stored into a texture when calling + * ImFontAtlas::Build()/GetTexDataAsXXXX(), which + * ImGui_ImplXXXX_NewFrame below will call. + * - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to + * - use Freetype for higher quality font rendering. + * - Read 'docs/FONTS.md' for more instructions and details. + * - Remember that in C/C++ if you want to include a backslash \ + * in a string literal you need to write a double backslash \\ ! + * - Our Emscripten build process allows embedding fonts to be + * - accessible at runtime from the "fonts/" folder. See + * Makefile.emscripten for details. + * auto imguiIO = m_imgui.getIO(); + * imguiIO.Fonts->AddFontDefault(); + * imguiIO.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); + * imguiIO.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); + * imguiIO.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); + * imguiIO.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); + * ImFont* font = imguiIO.Fonts->AddFontFromFileTTF( + * "c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, + * io.Fonts->GetGlyphRangesJapanese()); IM_ASSERT(font != nullptr); + */ } -void Bookmouse::setArchive(Archive* archive) { - if (archive == m_archive) { - return; +int Bookmouse::run() +{ + while (m_running) { + auto event = pollEvent(); + static_cast<void>(event); + + constexpr uint32_t delayMillis = 10; + if (SDL_GetWindowFlags(m_mainWindow.window()) & SDL_WINDOW_MINIMIZED) { + SDL_Delay(delayMillis); + continue; + } + m_imgui.startFrame(); + updateState(); + renderFrame(); } - delete(m_archive); - m_archive = archive; - m_pageNumber = 0; - setPages(); + return 0; } -void Bookmouse::next() { - auto numPages = m_archive->numPages(); - size_t increment = 1; - if (m_pageLayout != PageLayout::Single) { - increment = 2; - } - if ((m_pageNumber + increment) >= numPages) - { - return; +SDL_Event Bookmouse::pollEvent() +{ + /* + * Poll and handle events (inputs, window resize, etc.) + * You can read the io.WantCaptureMouse, io.WantCaptureKeyboard + * flags to tell if dear imgui wants to use your inputs. + * - When io.WantCaptureMouse is true, do not dispatch mouse input + * data to your main application, or clear/overwrite your copy of + * the mouse data. + * - When io.WantCaptureKeyboard is true, do not dispatch keyboard + * input data to your main application, or clear/overwrite your + * copy of the keyboard data. + * Generally you may always pass all inputs to dear imgui, and + * hide them from your application based on those two flags. + */ + SDL_Event event{}; + while (SDL_PollEvent(&event)) { + static_cast<void>(m_imgui.processEvent(event)); + if (event.type == SDL_QUIT) { + m_running = false; + } + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && + event.window.windowID == SDL_GetWindowID(m_mainWindow.window())) { + m_running = false; + } } - m_pageNumber += increment; - setPages(); + return event; } -void Bookmouse::back() { - auto numPages = m_archive->numPages(); - size_t decrement = 1; - if (m_pageLayout != PageLayout::Single) { - decrement = 2; - } - if (m_pageNumber < decrement) - { - m_pageNumber = 0; - decrement = 0; +void Bookmouse::updateState() +{ + bool my_tool_active{true}; + ImGui::Begin("My First Tool", &my_tool_active, ImGuiWindowFlags_MenuBar); + 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(); } - m_pageNumber -= decrement; - setPages(); -} -void Bookmouse::setPages() { - m_pageLeft.setPixmap(QPixmap()); - m_pageRight.setPixmap(QPixmap()); + // Edit a color stored as 4 floats + fud::Array<float, 4> my_color{}; + ImGui::ColorEdit4("Color", my_color.data()); - switch (m_pageLayout) - { - case PageLayout::Dual: - return setPagesDual(); - case PageLayout::Manga: - return setPagesManga(); - case PageLayout::Single: - default: - return setPagesNormal(); + // Generate samples and plot them + float samples[100]; + for (int idx = 0; idx < 100; idx++) { + auto num = static_cast<float>(idx); + auto time = static_cast<float>(ImGui::GetTime()); + samples[idx] = sinf(num * 0.2f + time * 1.5f); } + ImGui::PlotLines("Samples", samples, 100); + + // 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::End(); } -void Bookmouse::setPagesNormal() { - auto page1 = m_archive->getPage(m_pageNumber); - auto& label1 = m_pageLeft; - - if (page1.isOkay()) { - label1.setPixmap(QPixmap::fromImage(page1.getOkay())); - } -} - -void Bookmouse::setPagesDual() { - auto& label1 = m_pageLeft; - auto& label2 = m_pageRight; - setPages(label1, label2); -} - -void Bookmouse::setPagesManga() { - auto& label1 = m_pageRight; - auto& label2 = m_pageLeft; - setPages(label1, label2); -} - -void Bookmouse::setPages(QLabel& label1, QLabel& label2) { - auto page1 = m_archive->getPage(m_pageNumber); - auto page2 = m_archive->getPage(m_pageNumber + 1); - - if (page1.isOkay()) { - label1.setPixmap(QPixmap::fromImage(page1.getOkay())); - } - - - if (page2.isOkay()) { - label2.setPixmap(QPixmap::fromImage(page1.getOkay())); - } +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(); } } // namespace bookmouse |