@@ -16,218 +16,213 @@ GameOverlay::~GameOverlay() {
16
16
}
17
17
18
18
void GameOverlay::LoadFont (const std::string& name, const std::string& path, float fontSize) {
19
- // ImGuiIO& io = ImGui::GetIO();
20
- // std::shared_ptr<File> font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path);
21
- // if (font->IsLoaded) {
22
- // // TODO: Nothing is ever unloading the font or this fontData array.
23
- // char* fontData = new char[font->Buffer->size()];
24
- // memcpy(fontData, font->Buffer->data(), font->Buffer->size());
25
- // mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize);
26
- // }
19
+ ImGuiIO& io = ImGui::GetIO ();
20
+ std::shared_ptr<File> font = Context::GetInstance ()->GetResourceManager ()->GetArchiveManager ()->LoadFile (path);
21
+ if (font->IsLoaded ) {
22
+ // TODO: Nothing is ever unloading the font or this fontData array.
23
+ char * fontData = new char [font->Buffer ->size ()];
24
+ memcpy (fontData, font->Buffer ->data (), font->Buffer ->size ());
25
+ mFonts [name] = io.Fonts ->AddFontFromMemoryTTF (fontData, font->Buffer ->size (), fontSize);
26
+ }
27
27
}
28
28
29
29
void GameOverlay::TextDraw (float x, float y, bool shadow , ImVec4 color, const char * fmt, ...) {
30
- // char buf[1024];
31
- // va_list args;
32
- // va_start(args, fmt);
33
- // vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
34
- // buf[IM_ARRAYSIZE(buf) - 1] = 0;
35
- // va_end(args);
36
-
37
- // ImGui::PushStyleColor(ImGuiCol_Text, color);
38
- // ImGui::PushFont(mFonts[mCurrentFont]);
39
- // if (shadow) {
40
- // ImGui::SetCursorPos(ImVec2(x + 1, y + 1));
41
- // ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, color.w));
42
- // ImGui::Text(buf, args);
43
- // }
44
- // ImGui::PopStyleColor();
45
- // ImGui::SetCursorPos(ImVec2(x, y));
46
- // ImGui::Text(buf, args);
47
- // ImGui::PopFont();
48
- // ImGui::PopStyleColor();
30
+ char buf[1024 ];
31
+ va_list args;
32
+ va_start (args, fmt);
33
+ vsnprintf (buf, IM_ARRAYSIZE (buf), fmt, args);
34
+ buf[IM_ARRAYSIZE (buf) - 1 ] = 0 ;
35
+ va_end (args);
36
+
37
+ ImGui::PushStyleColor (ImGuiCol_Text, color);
38
+ ImGui::PushFont (mFonts [mCurrentFont ]);
39
+ if (shadow ) {
40
+ ImGui::SetCursorPos (ImVec2 (x + 1 , y + 1 ));
41
+ ImGui::PushStyleColor (ImGuiCol_Text, ImVec4 (.0f , .0f , .0f , color.w ));
42
+ ImGui::Text (buf, args);
43
+ }
44
+ ImGui::PopStyleColor ();
45
+ ImGui::SetCursorPos (ImVec2 (x, y));
46
+ ImGui::Text (buf, args);
47
+ ImGui::PopFont ();
48
+ ImGui::PopStyleColor ();
49
49
}
50
50
51
51
void GameOverlay::TextDrawNotification (float duration, bool shadow , const char * fmt, ...) {
52
- // char buf[1024];
53
- // va_list args;
54
- // va_start(args, fmt);
55
- // vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
56
- // buf[IM_ARRAYSIZE(buf) - 1] = 0;
57
- // va_end(args);
58
- // mRegisteredOverlays[StringHelper::Sprintf("NotificationID:%d%d", rand(), mRegisteredOverlays.size())] =
59
- // Overlay({ OverlayType::NOTIFICATION, buf, duration, duration });
60
- // mNeedsCleanup = true;
52
+ char buf[1024 ];
53
+ va_list args;
54
+ va_start (args, fmt);
55
+ vsnprintf (buf, IM_ARRAYSIZE (buf), fmt, args);
56
+ buf[IM_ARRAYSIZE (buf) - 1 ] = 0 ;
57
+ va_end (args);
58
+ mRegisteredOverlays [StringHelper::Sprintf (" NotificationID:%d%d" , rand (), mRegisteredOverlays .size ())] =
59
+ Overlay ({ OverlayType::NOTIFICATION, buf, duration, duration });
60
+ mNeedsCleanup = true ;
61
61
}
62
62
63
63
void GameOverlay::ClearNotifications () {
64
- // for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) {
65
- // if (it->second.Type == OverlayType::NOTIFICATION) {
66
- // it = mRegisteredOverlays.erase(it);
67
- // } else {
68
- // ++it;
69
- // }
70
- // }
64
+ for (auto it = mRegisteredOverlays .begin (); it != mRegisteredOverlays .end ();) {
65
+ if (it->second .Type == OverlayType::NOTIFICATION) {
66
+ it = mRegisteredOverlays .erase (it);
67
+ } else {
68
+ ++it;
69
+ }
70
+ }
71
71
}
72
72
73
73
void GameOverlay::CleanupNotifications () {
74
- // if (!mNeedsCleanup) {
75
- // return;
76
- // }
77
-
78
- // for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) {
79
- // if (it->second.Type == OverlayType::NOTIFICATION && it->second.duration <= 0.0f) {
80
- // it = mRegisteredOverlays.erase(it);
81
- // } else {
82
- // ++it;
83
- // }
84
- // }
85
- // mNeedsCleanup = false;
74
+ if (!mNeedsCleanup ) {
75
+ return ;
76
+ }
77
+
78
+ for (auto it = mRegisteredOverlays .begin (); it != mRegisteredOverlays .end ();) {
79
+ if (it->second .Type == OverlayType::NOTIFICATION && it->second .duration <= 0 .0f ) {
80
+ it = mRegisteredOverlays .erase (it);
81
+ } else {
82
+ ++it;
83
+ }
84
+ }
85
+ mNeedsCleanup = false ;
86
86
}
87
87
88
88
float GameOverlay::GetScreenWidth () {
89
- return 0 ;
90
- // const ImGuiViewport* viewport = ImGui::GetMainViewport();
91
- // return viewport->Size.x;
89
+ const ImGuiViewport* viewport = ImGui::GetMainViewport ();
90
+ return viewport->Size .x ;
92
91
}
93
92
94
93
float GameOverlay::GetScreenHeight () {
95
- return 0 ;
96
- // const ImGuiViewport* viewport = ImGui::GetMainViewport();
97
- // return viewport->Size.y;
94
+ const ImGuiViewport* viewport = ImGui::GetMainViewport ();
95
+ return viewport->Size .y ;
98
96
}
99
97
100
98
float GameOverlay::GetStringWidth (const char * text) {
101
- return 0 ;
102
- // return CalculateTextSize(text).x;
99
+ return CalculateTextSize (text).x ;
103
100
}
104
101
105
102
ImVec2 GameOverlay::CalculateTextSize (const char * text, const char * textEnd, bool shortenText, float wrapWidth) {
106
- return ImVec2 (0 , 0 );
107
- // ImGuiContext& g = *GImGui;
108
-
109
- // const char* textDisplayEnd;
110
- // if (shortenText) {
111
- // textDisplayEnd = ImGui::FindRenderedTextEnd(text, textEnd); // Hide anything after a '##' string
112
- // } else {
113
- // textDisplayEnd = textEnd;
114
- // }
115
-
116
- // ImFont* font = mCurrentFont == "Default" ? g.Font : mFonts[mCurrentFont];
117
- // const float fontSize = font->FontSize;
118
- // if (text == textDisplayEnd) {
119
- // return ImVec2(0.0f, fontSize);
120
- // }
121
- // ImVec2 textSize = font->CalcTextSizeA(fontSize, FLT_MAX, wrapWidth, text, textDisplayEnd, NULL);
122
-
123
- // // Round
124
- // // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out.
125
- // // FIXME: Investigate using ceilf or e.g.
126
- // // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c
127
- // // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html
128
- // textSize.x = IM_FLOOR(textSize.x + 0.99999f);
129
-
130
- // return textSize;
103
+ ImGuiContext& g = *GImGui;
104
+
105
+ const char * textDisplayEnd;
106
+ if (shortenText) {
107
+ textDisplayEnd = ImGui::FindRenderedTextEnd (text, textEnd); // Hide anything after a '##' string
108
+ } else {
109
+ textDisplayEnd = textEnd;
110
+ }
111
+
112
+ ImFont* font = mCurrentFont == " Default" ? g.Font : mFonts [mCurrentFont ];
113
+ const float fontSize = font->FontSize ;
114
+ if (text == textDisplayEnd) {
115
+ return ImVec2 (0 .0f , fontSize);
116
+ }
117
+ ImVec2 textSize = font->CalcTextSizeA (fontSize, FLT_MAX, wrapWidth, text, textDisplayEnd, NULL );
118
+
119
+ // Round
120
+ // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out.
121
+ // FIXME: Investigate using ceilf or e.g.
122
+ // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c
123
+ // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html
124
+ textSize.x = IM_FLOOR (textSize.x + 0 .99999f );
125
+
126
+ return textSize;
131
127
}
132
128
133
129
void GameOverlay::Init () {
134
- // LoadFont("Press Start 2P", "fonts/PressStart2P-Regular.ttf", 12.0f);
135
- // LoadFont("Fipps", "fonts/Fipps-Regular.otf", 32.0f);
136
- // const std::string defaultFont = mFonts.begin()->first;
137
- // if (!mFonts.empty()) {
138
- // const std::string font = CVarGetString("gOverlayFont", defaultFont.c_str());
139
- // for (auto& [name, _] : mFonts) {
140
- // if (font.starts_with(name)) {
141
- // mCurrentFont = name;
142
- // break;
143
- // }
144
- // mCurrentFont = defaultFont;
145
- // }
146
- // }
130
+ LoadFont (" Press Start 2P" , " fonts/PressStart2P-Regular.ttf" , 12 .0f );
131
+ LoadFont (" Fipps" , " fonts/Fipps-Regular.otf" , 32 .0f );
132
+ const std::string defaultFont = mFonts .begin ()->first ;
133
+ if (!mFonts .empty ()) {
134
+ const std::string font = CVarGetString (" gOverlayFont" , defaultFont.c_str ());
135
+ for (auto & [name, _] : mFonts ) {
136
+ if (font.starts_with (name)) {
137
+ mCurrentFont = name;
138
+ break ;
139
+ }
140
+ mCurrentFont = defaultFont;
141
+ }
142
+ }
147
143
}
148
144
149
145
void GameOverlay::DrawSettings () {
150
- // ImGui::Text("Overlays Text Font");
151
- // if (ImGui::BeginCombo("##TextFont", mCurrentFont.c_str())) {
152
- // for (auto& [name, font] : mFonts) {
153
- // if (ImGui::Selectable(name.c_str(), name == mCurrentFont)) {
154
- // mCurrentFont = name;
155
- // CVarSetString("gOverlayFont", name.c_str());
156
- // LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
157
- // }
158
- // }
159
- // ImGui::EndCombo();
160
- // }
146
+ ImGui::Text (" Overlays Text Font" );
147
+ if (ImGui::BeginCombo (" ##TextFont" , mCurrentFont .c_str ())) {
148
+ for (auto & [name, font] : mFonts ) {
149
+ if (ImGui::Selectable (name.c_str (), name == mCurrentFont )) {
150
+ mCurrentFont = name;
151
+ CVarSetString (" gOverlayFont" , name.c_str ());
152
+ LUS::Context::GetInstance ()->GetWindow ()->GetGui ()->SaveConsoleVariablesOnNextTick ();
153
+ }
154
+ }
155
+ ImGui::EndCombo ();
156
+ }
161
157
}
162
158
163
159
void GameOverlay::Draw () {
164
- // const ImGuiViewport* viewport = ImGui::GetMainViewport();
165
-
166
- // ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always);
167
- // ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always);
168
- // ImGui::Begin("GameOverlay", nullptr,
169
- // ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
170
- // ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar |
171
- // ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs);
172
-
173
- // CleanupNotifications();
174
-
175
- // float textY = 50;
176
- // float notY = 0;
177
-
178
- // for (auto& [key, overlay] : mRegisteredOverlays) {
179
-
180
- // if (overlay.Type == OverlayType::TEXT) {
181
- // const char* text = overlay.Value.c_str();
182
- // const auto var = CVarGet(text);
183
- // ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
184
-
185
- // switch (var->Type) {
186
- // case ConsoleVariableType::Float:
187
-
188
- // TextDraw(30, textY, true, color, "%s %.2f", text, var->Float);
189
- // break;
190
- // case ConsoleVariableType::Integer:
191
- // TextDraw(30, textY, true, color, "%s %d", text, var->Integer);
192
- // break;
193
- // case ConsoleVariableType::String:
194
- // TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str());
195
- // break;
196
- // case ConsoleVariableType::Color:
197
- // TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g,
198
- // var->Color.b, var->Color.a);
199
- // break;
200
- // case ConsoleVariableType::Color24:
201
- // TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g,
202
- // var->Color24.b);
203
- // break;
204
- // }
205
-
206
- // free((void*)text);
207
- // textY += 30;
208
- // }
209
-
210
- // if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) {
211
- // const char* text = overlay.Value.c_str();
212
- // const float duration = overlay.duration / overlay.fadeTime;
213
-
214
- // const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration);
215
- // #if defined(__WIIU__) || defined(__ANDROID__)
216
- // const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f;
217
- // const float textOffset = 40.0f * 2.0f;
218
- // #else
219
- // const float textWidth = GetStringWidth(overlay.Value.c_str());
220
- // const float textOffset = 40.0f;
221
- // #endif
222
-
223
- // TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true,
224
- // color,
225
- // text);
226
- // notY += 30;
227
- // overlay.duration -= .05f;
228
- // }
229
- // }
230
-
231
- // ImGui::End();
160
+ const ImGuiViewport* viewport = ImGui::GetMainViewport ();
161
+
162
+ ImGui::SetNextWindowPos (viewport->Pos , ImGuiCond_Always);
163
+ ImGui::SetNextWindowSize (viewport->Size , ImGuiCond_Always);
164
+ ImGui::Begin (" GameOverlay" , nullptr ,
165
+ ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
166
+ ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings |
167
+ ImGuiWindowFlags_NoInputs);
168
+
169
+ CleanupNotifications ();
170
+
171
+ float textY = 50 ;
172
+ float notY = 0 ;
173
+
174
+ for (auto & [key, overlay] : mRegisteredOverlays ) {
175
+
176
+ if (overlay.Type == OverlayType::TEXT) {
177
+ const char * text = overlay.Value .c_str ();
178
+ const auto var = CVarGet (text);
179
+ ImVec4 color = ImVec4 (1 .0f , 1 .0f , 1 .0f , 1 .0f );
180
+
181
+ switch (var->Type ) {
182
+ case ConsoleVariableType::Float:
183
+
184
+ TextDraw (30 , textY, true , color, " %s %.2f" , text, var->Float );
185
+ break ;
186
+ case ConsoleVariableType::Integer:
187
+ TextDraw (30 , textY, true , color, " %s %d" , text, var->Integer );
188
+ break ;
189
+ case ConsoleVariableType::String:
190
+ TextDraw (30 , textY, true , color, " %s %s" , text, var->String .c_str ());
191
+ break ;
192
+ case ConsoleVariableType::Color:
193
+ TextDraw (30 , textY, true , color, " %s (%u, %u, %u, %u)" , text, var->Color .r , var->Color .g ,
194
+ var->Color .b , var->Color .a );
195
+ break ;
196
+ case ConsoleVariableType::Color24:
197
+ TextDraw (30 , textY, true , color, " %s (%u, %u, %u)" , text, var->Color24 .r , var->Color24 .g ,
198
+ var->Color24 .b );
199
+ break ;
200
+ }
201
+
202
+ free ((void *)text);
203
+ textY += 30 ;
204
+ }
205
+
206
+ if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0 ) {
207
+ const char * text = overlay.Value .c_str ();
208
+ const float duration = overlay.duration / overlay.fadeTime ;
209
+
210
+ const ImVec4 color = ImVec4 (1 .0f , 1 .0f , 1 .0f , duration);
211
+ #if defined(__WIIU__) || defined(__ANDROID__)
212
+ const float textWidth = GetStringWidth (overlay.Value .c_str ()) * 2 .0f ;
213
+ const float textOffset = 40 .0f * 2 .0f ;
214
+ #else
215
+ const float textWidth = GetStringWidth (overlay.Value .c_str ());
216
+ const float textOffset = 40 .0f ;
217
+ #endif
218
+
219
+ TextDraw (GetScreenWidth () - textWidth - textOffset, GetScreenHeight () - textOffset - notY, true , color,
220
+ text);
221
+ notY += 30 ;
222
+ overlay.duration -= .05f ;
223
+ }
224
+ }
225
+
226
+ ImGui::End ();
232
227
}
233
228
} // namespace LUS
0 commit comments