-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·151 lines (130 loc) · 4.87 KB
/
main.cpp
File metadata and controls
executable file
·151 lines (130 loc) · 4.87 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
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
#pragma once
#include "../bml_includes.hpp"
#ifndef WIN32_MEAN_AND_LEAN
#define WIN32_MEAN_AND_LEAN
#include <Windows.h>
#endif // !WIN32_MEAN_AND_LEAN
#include <thread>
#include <memory>
#include "../utils.hpp"
extern "C" {
__declspec(dllexport) IMod* BMLEntry(IBML* bml);
}
class PositionViewer : public IMod {
std::unique_ptr<BGui::Text> sprite;
bool init = false;
CKDataArray *current_level_array{};
float y_pos = 0.9f;
int font_size = 12;
float pos_interval = 0, speed_interval = 0;
IProperty *prop_y{}, *prop_font_size{}, *prop_pos_interval{}, *prop_speed_interval{};
VxVector last_pos{}, ref_pos{};
float last_speed = 0, last_speed_timestamp = 0;
float next_pos_update = 0, next_speed_update = 0;
utils utils;
auto get_player_ball() {
return static_cast<CK3dObject*>(current_level_array->GetElementObject(0, 1));
}
public:
PositionViewer(IBML* bml) : IMod(bml), utils(bml) {}
virtual iCKSTRING GetID() override { return "PositionViewer"; }
virtual iCKSTRING GetVersion() override { return "0.1.0"; }
virtual iCKSTRING GetName() override { return "Position Viewer"; }
virtual iCKSTRING GetAuthor() override { return "BallanceBug"; }
virtual iCKSTRING GetDescription() override { return "Displays the coordinates of your player ball."; }
DECLARE_BML_VERSION;
void OnProcess() override {
if (!(m_bml->IsPlaying() && sprite))
return;
bool pos_update = false, speed_update = false;
float current_time = m_bml->GetTimeManager()->GetTime();
float pos_diff = current_time - next_pos_update, speed_diff = current_time - next_speed_update;
if (pos_diff >= 0) {
pos_update = true;
if (pos_diff >= 10000)
next_pos_update = current_time;
next_pos_update += pos_interval;
}
if (speed_diff >= 0) {
speed_update = true;
if (speed_diff >= 10000)
next_speed_update = current_time;
next_speed_update += speed_interval;
}
if (pos_update)
get_player_ball()->GetPosition(&last_pos);
if (speed_update) {
VxVector current_pos; get_player_ball()->GetPosition(¤t_pos);
last_speed = Magnitude(current_pos - ref_pos) / (current_time - last_speed_timestamp) * 1e3f;
last_speed_timestamp = current_time;
ref_pos = current_pos;
}
if (pos_update || speed_update) {
char text[128];
snprintf(text, sizeof(text), "X: %.3f, Y: %.3f, Z: %.3f\n%.2f m/s",
last_pos.x, last_pos.y, last_pos.z, last_speed);
sprite->SetText(text);
};
}
void OnPostStartMenu() override {
if (init)
return;
current_level_array = m_bml->GetArrayByName("CurrentLevel");
show_ball_pos();
init = true;
}
void OnStartLevel() override {
if (!sprite)
show_ball_pos();
}
void OnPreExitLevel() override {
if (sprite)
hide_ball_pos();
}
void OnLoad() override {
GetConfig()->SetCategoryComment("Main", "Main settings.");
prop_y = GetConfig()->GetProperty("Main", "Y_Position");
prop_y->SetComment("Y position of the top of sprite text.");
prop_y->SetDefaultFloat(0.9f);
prop_font_size = GetConfig()->GetProperty("Main", "Font_Size");
prop_font_size->SetComment("Font size of sprite text.");
prop_font_size->SetDefaultFloat(12);
prop_pos_interval = GetConfig()->GetProperty("Main", "Position_Update_Interval");
prop_pos_interval->SetComment("Minimum update interval for position data. Default: 50.0 (milliseconds)");
prop_pos_interval->SetDefaultFloat(50.0f);
prop_speed_interval = GetConfig()->GetProperty("Main", "Speed_Update_Interval");
prop_speed_interval->SetComment("Minimum update interval for speed data. Default: 100.0 (milliseconds)");
prop_speed_interval->SetDefaultFloat(100.0f);
load_config();
}
void OnModifyConfig(iCKSTRING category, iCKSTRING key, IProperty* prop) override {
load_config();
if (sprite) {
hide_ball_pos();
show_ball_pos();
}
}
private:
void show_ball_pos() {
if (sprite)
return;
sprite = std::make_unique<decltype(sprite)::element_type>("PositionDisplay");
sprite->SetSize({ 0.6f, 0.12f });
sprite->SetPosition({ 0.2f, y_pos });
sprite->SetAlignment(CKSPRITETEXT_CENTER);
sprite->SetTextColor(0xffffffff);
sprite->SetZOrder(128);
sprite->SetFont("Arial", font_size, 400, false, false);
next_pos_update = next_speed_update = last_speed_timestamp = m_bml->GetTimeManager()->GetTime();
}
void hide_ball_pos() { sprite.reset(); }
void load_config() {
y_pos = prop_y->GetFloat();
font_size = utils.get_bgui_font_size(prop_font_size->GetFloat());
pos_interval = prop_pos_interval->GetFloat();
speed_interval = prop_speed_interval->GetFloat();
}
};
IMod* BMLEntry(IBML* bml) {
return new PositionViewer(bml);
}