summaryrefslogtreecommitdiff
path: root/src/bookmouse.cpp
blob: d367af1a2585fa19eb128f785f24cb56029d9700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "bookmouse.hpp"

#include "bookmouse_time.hpp"
#include "file_dialog.hpp"

#include <SDL_opengl.h>
#include <atomic>
#include <fud_array.hpp>
#include <fud_directory.hpp>
#include <spdlog/spdlog.h>
#include <vector>

namespace bookmouse {

using fud::FudStatus;

constexpr const char* OpenDialogHandle = "Open File";

Bookmouse::Bookmouse() :
    m_sdlContext{}, m_mainWindow{m_sdlContext}, m_glContext{m_mainWindow},
    m_imgui{m_glContext, m_sdlContext, m_mainWindow},
    m_directory{m_directoryName}
{
    /*
     * 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() or
     *   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.
     * 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);
     */
}

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();
    }
    return 0;
}

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;
        }
    }
    return event;
}

void Bookmouse::updateState()
{
    ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
    ImGui::SetNextWindowSize(m_imgui.getIO().DisplaySize);
    auto stateResult = ImBegin(
        "My First Tool",
        &m_running, // Is this right?
        ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_MenuBar);
    IM_ASSERT(stateResult);

    // m_imgui.getIO().AddKeyEvent()

    // if (ImGui::IsKeyPressed(ImGuiKey_O) && m_imgui.getIO().KeyCtrl && !m_openDialog) {
    //     m_openDialog = true;
    // }

    if (ImMenuBar menuBar{}) {
        IM_ASSERT(menuBar);
        menuing();
    }

    if (m_openDialog) {
        m_openDialog = false;
        m_getContents = true;
        ImGui::OpenPopup(OpenDialogHandle);
    }

    if (ImPopupModal popup{OpenDialogHandle}) {
        IM_ASSERT(popup);
        openDialog();
    }

    // Edit a color stored as 4 floats
    fud::Array<float, 4> my_color{};
    ImGui::ColorEdit4("Color", my_color.data());

    // 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>(ImGetTime());
        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");
    ImText("%04d: Some text", 42);
    ImEnd();
}

void Bookmouse::menuing()
{
    if (ImMenu menu{"File"}) {
        IM_ASSERT(menu);
        if (ImGui::MenuItem("Open Archive", "Ctrl+O")) { /* Do stuff */
            m_openDialog = true;
        }
        if (ImGui::MenuItem("Close", "Ctrl+Q")) {
            m_running = false;
        }
    }

    if (ImMenu menu{"Help"}) {
        IM_ASSERT(menu);
        if (ImGui::MenuItem("About")) {
            spdlog::debug("About");
        }
        ImGui::EndMenu();
    }
}

void Bookmouse::openDialog()
{
    if (m_fileDialog == nullptr) {
        m_fileDialog = std::make_unique<FileDialog>(m_directoryName, m_timeFormat);
        if (!m_fileDialog->valid()) {
            m_openDialog = false;
            ImGui::CloseCurrentPopup();
        }
    }

    auto result = m_fileDialog->pickFiles();

    if (result != std::nullopt) {
        auto selectedFiles = *result;
        for (const auto& name : selectedFiles) {
            spdlog::info("Selected {}", name.c_str());
        }
        m_openDialog = false;
        ImGui::CloseCurrentPopup();
        return;
    }

    if (m_fileDialog->canceled()) {
        m_openDialog = false;
        ImGui::CloseCurrentPopup();
    }
}

void Bookmouse::renderFrame()
{
    m_imgui.render();
}

} // namespace bookmouse