Skip to content

Commit 0fcbf65

Browse files
committed
Add a BaseApp class
1 parent 2dae72e commit 0fcbf65

File tree

6 files changed

+118
-81
lines changed

6 files changed

+118
-81
lines changed

ficsit-companion/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
project(ficsit-companion)
22

33
set(HEADER_FILES
4-
include/app.hpp
4+
include/base_app.hpp
55
include/building.hpp
66
include/fractional_number.hpp
77
include/game_data.hpp
88
include/json.hpp
99
include/link.hpp
1010
include/node.hpp
1111
include/pin.hpp
12+
include/production_app.hpp
1213
include/recipe.hpp
1314
include/utils.hpp
1415
)
1516

1617
set(SOURCE_FILES
1718
${imgui_SOURCE}
1819

19-
src/app.cpp
20+
src/base_app.cpp
2021
src/building.cpp
2122
src/fractional_number.cpp
2223
src/game_data.cpp
2324
src/json.cpp
2425
src/link.cpp
2526
src/node.cpp
2627
src/pin.cpp
28+
src/production_app.cpp
2729
src/recipe.cpp
2830
src/utils.cpp
2931

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <chrono>
4+
5+
class BaseApp
6+
{
7+
public:
8+
BaseApp();
9+
virtual ~BaseApp();
10+
void Render();
11+
12+
virtual void SaveSession() = 0;
13+
14+
bool HasRecentInteraction() const;
15+
16+
protected:
17+
virtual void RenderImpl() = 0;
18+
19+
private:
20+
std::chrono::steady_clock::time_point last_time_interacted;
21+
};

ficsit-companion/include/app.hpp renamed to ficsit-companion/include/production_app.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#include <chrono>
43
#include <map>
54
#include <memory>
65
#include <string>
@@ -9,25 +8,24 @@
98

109
#include <imgui_node_editor.h>
1110

11+
#include "base_app.hpp"
1212
#include "fractional_number.hpp"
1313

1414
struct Link;
1515
struct Node;
1616
struct Pin;
1717
struct Recipe;
1818

19-
class App
19+
class ProductionApp : public BaseApp
2020
{
2121
public:
22-
App();
23-
~App();
24-
void Render();
25-
26-
public:
22+
ProductionApp();
23+
virtual ~ProductionApp();
2724
/// @brief Save current session (should NOT require an active ImGui context)
28-
void SaveSession();
25+
virtual void SaveSession() override;
2926

30-
bool HasRecentInteraction() const;
27+
protected:
28+
virtual void RenderImpl() override;
3129

3230
private:
3331
/// @brief Load saved session if present
@@ -165,8 +163,6 @@ class App
165163

166164
unsigned int somersloop_texture_id;
167165

168-
std::chrono::steady_clock::time_point last_time_interacted;
169-
170166
float error_time;
171167

172168
// Used to cycle through the nodes when clicking on the corresponding

ficsit-companion/src/base_app.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "base_app.hpp"
2+
3+
#include <imgui.h>
4+
5+
BaseApp::BaseApp()
6+
{
7+
last_time_interacted = std::chrono::steady_clock::now();
8+
}
9+
10+
BaseApp::~BaseApp()
11+
{
12+
13+
}
14+
15+
void BaseApp::Render()
16+
{
17+
RenderImpl();
18+
19+
// If any user input happened, reset last time interaction
20+
const ImGuiIO& io = ImGui::GetIO();
21+
for (const auto& k : io.KeysData)
22+
{
23+
if (k.Down)
24+
{
25+
last_time_interacted = std::chrono::steady_clock::now();
26+
break;
27+
}
28+
}
29+
30+
if (ImGui::IsAnyMouseDown() || io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f)
31+
{
32+
last_time_interacted = std::chrono::steady_clock::now();
33+
}
34+
}
35+
36+
bool BaseApp::HasRecentInteraction() const
37+
{
38+
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - last_time_interacted).count() < 10000;
39+
}

ficsit-companion/src/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
#endif
2626
#endif
2727

28-
#include "app.hpp"
28+
#include "production_app.hpp"
2929
#include "game_data.hpp"
3030

31-
bool Render(SDL_Window* window, App* app)
31+
bool Render(SDL_Window* window, BaseApp* app)
3232
{
3333
SDL_Event event;
3434
while (SDL_PollEvent(&event))
@@ -207,7 +207,7 @@ int main(int argc, char* argv[])
207207

208208
Data::LoadData("satisfactory");
209209

210-
App app;
210+
ProductionApp app;
211211
#if !defined(__EMSCRIPTEN__)
212212
while (Render(window, &app))
213213
{
@@ -217,12 +217,12 @@ int main(int argc, char* argv[])
217217
// Write to localStorage when quitting
218218
emscripten_set_beforeunload_callback(static_cast<void*>(&app), [](int event_type, const void* reserved, void* user_data) {
219219
// Save current session to disk
220-
static_cast<App*>(user_data)->SaveSession();
220+
static_cast<BaseApp*>(user_data)->SaveSession();
221221
// return empty string does not trigger the popup asking if we *really* want to quit
222222
return "";
223223
});
224224

225-
struct WindowApp { SDL_Window* window; App* app; };
225+
struct WindowApp { SDL_Window* window; BaseApp* app; };
226226
WindowApp arg{ window, &app };
227227
emscripten_set_main_loop_arg([](void* arg) {
228228
WindowApp* window_app = static_cast<WindowApp*>(arg);

0 commit comments

Comments
 (0)