-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtimer.cpp
More file actions
125 lines (100 loc) · 2.69 KB
/
Copy pathtimer.cpp
File metadata and controls
125 lines (100 loc) · 2.69 KB
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
#include "prism/timer.h"
#include "prism/memoryhandler.h"
#include "prism/datastructures.h"
#include "prism/stlutil.h"
#ifdef _WIN32
#include <imgui/imgui.h>
#include "prism/windows/debugimgui_win.h"
#endif
using namespace std;
namespace prism {
typedef struct TimerElement_internal {
Duration mNow;
Duration mDuration;
TimerCB mCB;
void* mCaller;
} TimerElement;
static struct {
map<int, TimerElement> mList;
} gTimerData;
void removeTimer(int tID);
#ifdef _WIN32
void imguiTimer()
{
static bool isWindowShown = false;
imguiPrismAddTab("Prism", "Timer", &isWindowShown);
if (!isWindowShown) return;
ImGui::Begin("Timer", &isWindowShown);
ImGui::Text("Active Timers = %d", (int)gTimerData.mList.size());
ImGui::Separator();
static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg;
if (ImGui::BeginTable("Timers", 5, flags))
{
ImGui::TableSetupColumn("ID");
ImGui::TableSetupColumn("Now");
ImGui::TableSetupColumn("Duration");
ImGui::TableSetupColumn("Progress");
ImGui::TableSetupColumn("");
ImGui::TableHeadersRow();
int timerToRemove = -1;
for (auto& entryPair : gTimerData.mList)
{
const int id = entryPair.first;
TimerElement& e = entryPair.second;
ImGui::PushID(id);
ImGui::TableNextRow(); ImGui::TableNextColumn();
ImGui::Text("%d", id); ImGui::TableNextColumn();
ImGui::Text("%.1f", e.mNow); ImGui::TableNextColumn();
ImGui::Text("%.1f", e.mDuration); ImGui::TableNextColumn();
const float fraction = e.mDuration > 0 ? (float)(e.mNow / e.mDuration) : 0.0f;
ImGui::ProgressBar(fraction); ImGui::TableNextColumn();
if (ImGui::SmallButton("Remove")) timerToRemove = id;
ImGui::PopID();
}
ImGui::EndTable();
if (timerToRemove != -1) removeTimer(timerToRemove);
}
ImGui::End();
}
#endif
int addTimerCB(Duration tDuration, TimerCB tCB, void* tCaller) {
TimerElement e;
e.mNow = 0;
e.mDuration = tDuration;
e.mCB = tCB;
e.mCaller = tCaller;
return stl_int_map_push_back(gTimerData.mList, e);
}
void removeTimer(int tID)
{
gTimerData.mList.erase(tID);
}
void setupTimer() {
gTimerData.mList.clear();
}
static int updateCB(void* tCaller, TimerElement& tData) {
(void)tCaller;
TimerElement* cur = &tData;
int isOver = handleDurationAndCheckIfOver(&cur->mNow, cur->mDuration);
if (isOver) {
if (cur->mCB)
{
cur->mCB(cur->mCaller);
}
return 1;
}
return 0;
}
void updateTimer() {
stl_int_map_remove_predicate(gTimerData.mList, updateCB);
}
void clearTimer() {
gTimerData.mList.clear();
}
void shutdownTimer() {
clearTimer();
}
int hasTimerFinished(int tID) {
return gTimerData.mList.find(tID) == gTimerData.mList.end();
}
}