SpaghettiKart
Loading...
Searching...
No Matches
UIWidgets.h
Go to the documentation of this file.
1#ifndef UIWidgets_hpp
2#define UIWidgets_hpp
3
4#include <string>
5#include <vector>
6#include <span>
7#include <stdint.h>
8#define IMGUI_DEFINE_MATH_OPERATORS
9#include <imgui.h>
10#include <libultraship/libultraship.h>
11#include <unordered_map>
12#include "port/ShipUtils.h"
13
14namespace UIWidgets {
15
16 using SectionFunc = void(*)();
17
18 struct TextFilters {
19 static int FilterNumbers(ImGuiInputTextCallbackData* data) {
20 if (data->EventChar < 256 && strchr("1234567890", (char)data->EventChar)) {
21 return 0;
22 }
23 return 1;
24 }
25
26 static int FilterAlphaNum(ImGuiInputTextCallbackData* data) {
27 const char* alphanum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ0123456789";
28 if (data->EventChar < 256 && strchr(alphanum, (char)data->EventChar)) {
29 return 0;
30 }
31 return 1;
32 }
33 };
34
35 std::string WrappedText(const char* text, unsigned int charactersPerLine = 60);
36 std::string WrappedText(const std::string& text, unsigned int charactersPerLine = 60);
37 void Tooltip(const char* text);
38
39 // mostly in order for colors usable by the menu without custom text color
64
65 const std::unordered_map<Colors, ImVec4> ColorValues = {
66 { Colors::Pink, ImVec4(0.87f, 0.3f, 0.87f, 1.0f) },
67 { Colors::Red, ImVec4(0.55f, 0.0f, 0.0f, 1.0f) },
68 { Colors::DarkRed, ImVec4(0.3f, 0.0f, 0.0f, 1.0f) },
69 { Colors::Orange, ImVec4(0.85f, 0.55f, 0.0f, 1.0f) },
70 { Colors::Yellow, ImVec4(0.95f, 0.95f, 0.0f, 1.0f) },
71 { Colors::Green, ImVec4(0.0f, 0.55f, 0.0f, 1.0f) },
72 { Colors::DarkGreen, ImVec4(0.0f, 0.3f, 0.0f, 1.0f) },
73 { Colors::Cyan, ImVec4(0.0f, 0.9f, 0.9f, 1.0f) },
74 { Colors::LightBlue, ImVec4(0.0f, 0.24f, 0.8f, 1.0f) },
75 { Colors::Blue, ImVec4(0.08f, 0.03f, 0.65f, 1.0f) },
76 { Colors::DarkBlue, ImVec4(0.03f, 0.0f, 0.5f, 1.0f) },
77 { Colors::Indigo, ImVec4(0.35f, 0.0f, 0.87f, 1.0f) },
78 { Colors::Violet, ImVec4(0.5f, 0.0f, 0.9f, 1.0f) },
79 { Colors::Purple, ImVec4(0.31f, 0.0f, 0.67f, 1.0f) },
80 { Colors::Brown, ImVec4(0.37f, 0.18f, 0.0f, 1.0f) },
81 { Colors::LightGray, ImVec4(0.75f, 0.75f, 0.75f, 1.0f) },
82 { Colors::Gray, ImVec4(0.45f, 0.45f, 0.45f, 1.0f) },
83 { Colors::DarkGray, ImVec4(0.15f, 0.15f, 0.15f, 1.0f) },
84 { Colors::Black, ImVec4(0.0f, 0.0f, 0.0f, 1.0f)},
85 { Colors::White, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) },
86 { Colors::NoColor, ImVec4(0.0f, 0.0f, 0.0f, 0.0f)},
87 };
88
89 namespace Sizes {
90 const ImVec2 Inline = ImVec2(0.0f, 0.0f);
91 const ImVec2 Fill = ImVec2(-1.0f, 0.0f);
92 }
93
101
106
111
113 const char* tooltip = "";
114 bool disabled = false;
115 const char* disabledTooltip = "";
117
119 color = color = color_;
120 return *this;
121 }
122 WidgetOptions& Tooltip(const char* tooltip_) {
123 tooltip = tooltip_;
124 return *this;
125 }
126 WidgetOptions& Disabled(bool disabled_) {
127 disabled = disabled_;
128 return *this;
129 }
130 };
131
135
136 ButtonOptions& Size(ImVec2 size_) {
137 size = size_;
138 return *this;
139 }
140 ButtonOptions& Tooltip(const char* tooltip_) {
141 WidgetOptions::tooltip = tooltip_;
142 return *this;
143 }
145 WidgetOptions::color = color = color_;
146 return *this;
147 }
148 };
149
151 bool defaultValue = false; // Only applicable to CVarCheckbox
155
156 CheckboxOptions& DefaultValue(bool defaultValue_) {
157 defaultValue = defaultValue_;
158 return *this;
159 }
161 alignment = alignment_;
162 return *this;
163 }
165 labelPosition = labelPosition_;
166 return *this;
167 }
168 CheckboxOptions& Tooltip(const char* tooltip_) {
169 WidgetOptions::tooltip = tooltip_;
170 return *this;
171 }
173 WidgetOptions::color = color = color_;
174 return *this;
175 }
176 };
177
179 std::unordered_map<int32_t, const char*> comboMap = {};
180 uint32_t defaultIndex = 0; // Only applicable to CVarCombobox
183 ImGuiComboFlags flags = 0;
185
186 ComboboxOptions& ComboMap(std::unordered_map<int32_t, const char*> comboMap_) {
187 comboMap = comboMap_;
188 return *this;
189 }
190 ComboboxOptions& DefaultIndex(uint32_t defaultIndex_) {
191 defaultIndex = defaultIndex_;
192 return *this;
193 }
195 alignment = alignment_;
196 return *this;
197 }
199 labelPosition = labelPosition_;
200 return *this;
201 }
202 ComboboxOptions& Tooltip(const char* tooltip_) {
203 WidgetOptions::tooltip = tooltip_;
204 return *this;
205 }
207 WidgetOptions::color = color = color_;
208 return *this;
209 }
210 };
211
213 bool showButtons = true;
214 const char* format = "%d";
215 int32_t step = 1;
216 int32_t min = 1;
217 int32_t max = 10;
218 int32_t defaultValue = 1;
222 ImGuiSliderFlags flags = 0;
223
224 IntSliderOptions& ShowButtons(bool showButtons_) {
225 showButtons = showButtons_;
226 return *this;
227 }
228 IntSliderOptions& Format(const char* format_) {
229 format = format_;
230 return *this;
231 }
232 IntSliderOptions& Step(int32_t step_) {
233 step = step_;
234 return *this;
235 }
236 IntSliderOptions& Min(int32_t min_) {
237 min = min_;
238 return *this;
239 }
240 IntSliderOptions& Max(int32_t max_) {
241 max = max_;
242 return *this;
243 }
244 IntSliderOptions& DefaultValue(int32_t defaultValue_) {
245 defaultValue = defaultValue_;
246 return *this;
247 }
249 alignment = alignment_;
250 return *this;
251 }
253 labelPosition = labelPosition_;
254 return *this;
255 }
256 IntSliderOptions& Tooltip(const char* tooltip_) {
257 WidgetOptions::tooltip = tooltip_;
258 return *this;
259 }
261 WidgetOptions::color = color = color_;
262 return *this;
263 }
264 };
265
267 bool showButtons = true;
268 const char* format = "%f";
269 float step = 0.01f;
270 float min = 0.01f;
271 float max = 10.0f;
272 float defaultValue = 1.0f;
273 bool isPercentage = false; // Multiplies visual value by 100
277 ImGuiSliderFlags flags = 0;
278
279 FloatSliderOptions& ShowButtons(bool showButtons_) {
280 showButtons = showButtons_;
281 return *this;
282 }
283 FloatSliderOptions& Format(const char* format_) {
284 format = format_;
285 return *this;
286 }
287 FloatSliderOptions& Step(float step_) {
288 step = step_;
289 return *this;
290 }
291 FloatSliderOptions& Min(float min_) {
292 min = min_;
293 return *this;
294 }
295 FloatSliderOptions& Max(float max_) {
296 max = max_;
297 return *this;
298 }
299 FloatSliderOptions& DefaultValue(float defaultValue_) {
300 defaultValue = defaultValue_;
301 return *this;
302 }
304 alignment = alignment_;
305 return *this;
306 }
308 labelPosition = labelPosition_;
309 return *this;
310 }
311 FloatSliderOptions& IsPercentage(bool isPercentage_ = true) {
312 isPercentage = isPercentage_;
313 format = "%.0f";
314 min = 0.0f;
315 max = 1.0f;
316 return *this;
317 }
318 FloatSliderOptions& Tooltip(const char* tooltip_) {
319 WidgetOptions::tooltip = tooltip_;
320 return *this;
321 }
323 WidgetOptions::color = color = color_;
324 return *this;
325 }
326 };
327
328 void PushStyleMenu(const ImVec4& color);
329 void PushStyleMenu(Colors color = Colors::LightBlue);
330 void PopStyleMenu();
331 bool BeginMenu(const char* label, Colors color = Colors::LightBlue);
332
333 void PushStyleMenuItem(const ImVec4& color);
334 void PushStyleMenuItem(Colors color = Colors::LightBlue);
335 void PopStyleMenuItem();
336 bool MenuItem(const char* label, const char* shortcut = NULL, Colors color = Colors::LightBlue);
337
338 void PushStyleButton(const ImVec4& color);
339 void PushStyleButton(Colors color = Colors::Gray);
340 void PopStyleButton();
341 bool Button(const char* label, const ButtonOptions& options = {});
342 bool WindowButton(const char* label, const char* cvarName, std::shared_ptr<Ship::GuiWindow> windowPtr, const ButtonOptions& options = {});
343
344 void PushStyleCheckbox(const ImVec4& color);
346 void PopStyleCheckbox();
347 void RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash);
348 bool Checkbox(const char* label, bool* v, const CheckboxOptions& options = {});
349 bool CVarCheckbox(const char* label, const char* cvarName, const CheckboxOptions& options = {});
350
351 void PushStyleCombobox(const ImVec4& color);
353 void PopStyleCombobox();
354
355 /*using ComboVariant = std::variant<const std::unordered_map<int32_t, const char*>&, const std::vector<const char*>&>;
356
357 bool Combobox(const char* label, int32_t* value, ComboVariant comboSource, const ComboboxOptions& options = {}) {
358 bool dirty = false;
359 float startX = ImGui::GetCursorPosX();
360 std::string invisibleLabelStr = "##" + std::string(label);
361 const char* invisibleLabel = invisibleLabelStr.c_str();
362 ImGui::PushID(label);
363 ImGui::BeginGroup();
364 ImGui::BeginDisabled(options.disabled);
365 PushStyleCombobox(options.color);
366 if (options.alignment == ComponentAlignment::Left) {
367 if (options.labelPosition == LabelPosition::Above) {
368 ImGui::Text("%s", label);
369 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
370 } else if (options.labelPosition == LabelPosition::Near) {
371 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x - ImGui::GetStyle().ItemSpacing.x * 2);
372 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
373 ImGui::SetNextItemWidth(ImGui::CalcTextSize(comboMap.at(*value)).x + ImGui::GetStyle().FramePadding.x * 4 + ImGui::GetStyle().ItemSpacing.x);
374 }
375 } else if (options.alignment == ComponentAlignment::Right) {
376 if (options.labelPosition == LabelPosition::Above) {
377 ImGui::NewLine();
378 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
379 ImGui::Text("%s", label);
380 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
381 } else if (options.labelPosition == LabelPosition::Near) {
382 ImGui::SameLine(ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x * 2);
383 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
384 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
385 float width = ImGui::CalcTextSize(comboMap.at(*value)).x + ImGui::GetStyle().FramePadding.x * 4;
386 ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
387 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
388 }
389 }
390 if (ImGui::BeginCombo(invisibleLabel, comboMap.at(*value), options.flags)) {
391 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10.0f, 10.0f));
392 for (const auto& pair : comboMap) {
393 if (strlen(pair.second) > 1) {
394 if (ImGui::Selectable(pair.second, pair.first == *value)) {
395 *value = pair.first;
396 dirty = true;
397 }
398 }
399 }
400 ImGui::PopStyleVar();
401 ImGui::EndCombo();
402 }
403 if (options.alignment == ComponentAlignment::Left) {
404 if (options.labelPosition == LabelPosition::Near) {
405 ImGui::SameLine();
406 ImGui::Text("%s", label);
407 } else if (options.labelPosition == LabelPosition::Far) {
408 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
409 ImGui::Text("%s", label);
410 }
411 } else if (options.alignment == ComponentAlignment::Right) {
412 if (options.labelPosition == LabelPosition::Near || options.labelPosition == LabelPosition::Far) {
413 ImGui::SameLine(startX);
414 ImGui::Text("%s", label);
415 }
416 }
417 PopStyleCombobox();
418 ImGui::EndDisabled();
419 ImGui::EndGroup();
420 if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
421 ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
422 } else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
423 ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
424 }
425 ImGui::PopID();
426 return dirty;
427 }
428
429 bool CVarCombobox(const char* label, const char* cvarName, ComboVariant comboSource, const ComboboxOptions& options = {}) {
430 bool dirty = false;
431 int32_t value = CVarGetInteger(cvarName, options.defaultIndex);
432 if (Combobox(label, &value, comboSource, options)) {
433 CVarSetInteger(cvarName, value);
434 Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
435 ShipInit::Init(cvarName);
436 dirty = true;
437 }
438 return dirty;
439 }*/
440
441 template <typename T>
442 bool Combobox(const char* label, T* value, const std::unordered_map<T, const char*>& comboMap, const ComboboxOptions& options = {}) {
443 bool dirty = false;
444 float startX = ImGui::GetCursorPosX();
445 std::string invisibleLabelStr = "##" + std::string(label);
446 const char* invisibleLabel = invisibleLabelStr.c_str();
447 ImGui::PushID(label);
448 ImGui::BeginGroup();
449 ImGui::BeginDisabled(options.disabled);
450 PushStyleCombobox(options.color);
451 if (options.alignment == ComponentAlignment::Left) {
452 if (options.labelPosition == LabelPosition::Above) {
453 ImGui::Text("%s", label);
454 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
455 } else if (options.labelPosition == LabelPosition::Near) {
456 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x - ImGui::GetStyle().ItemSpacing.x * 2);
457 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
458 ImGui::SetNextItemWidth(ImGui::CalcTextSize(comboMap.at(*value)).x + ImGui::GetStyle().FramePadding.x * 4 + ImGui::GetStyle().ItemSpacing.x);
459 }
460 } else if (options.alignment == ComponentAlignment::Right) {
461 if (options.labelPosition == LabelPosition::Above) {
462 ImGui::NewLine();
463 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
464 ImGui::Text("%s", label);
465 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
466 } else if (options.labelPosition == LabelPosition::Near) {
467 ImGui::SameLine(ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x * 2);
468 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
469 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
470 float width = ImGui::CalcTextSize(comboMap.at(*value)).x + ImGui::GetStyle().FramePadding.x * 4;
471 ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
472 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
473 }
474 }
475 if (ImGui::BeginCombo(invisibleLabel, comboMap.at(*value), options.flags)) {
476 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10.0f, 10.0f));
477 for (const auto& pair : comboMap) {
478 if (strlen(pair.second) > 1) {
479 if (ImGui::Selectable(pair.second, pair.first == *value)) {
480 *value = pair.first;
481 dirty = true;
482 }
483 }
484 }
485 ImGui::PopStyleVar();
486 ImGui::EndCombo();
487 }
488 if (options.alignment == ComponentAlignment::Left) {
489 if (options.labelPosition == LabelPosition::Near) {
490 ImGui::SameLine();
491 ImGui::Text("%s", label);
492 } else if (options.labelPosition == LabelPosition::Far) {
493 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
494 ImGui::Text("%s", label);
495 }
496 } else if (options.alignment == ComponentAlignment::Right) {
497 if (options.labelPosition == LabelPosition::Near || options.labelPosition == LabelPosition::Far) {
498 ImGui::SameLine(startX);
499 ImGui::Text("%s", label);
500 }
501 }
503 ImGui::EndDisabled();
504 ImGui::EndGroup();
505 if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
506 ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
507 } else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
508 ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
509 }
510 ImGui::PopID();
511 return dirty;
512 }
513
514 template <typename T = size_t>
515 bool Combobox(const char* label, T* value, const std::vector<const char*>& comboVector, const ComboboxOptions& options = {}) {
516 bool dirty = false;
517 float startX = ImGui::GetCursorPosX();
518 size_t currentValueIndex = static_cast<size_t>(*value);
519 std::string invisibleLabelStr = "##" + std::string(label);
520 const char* invisibleLabel = invisibleLabelStr.c_str();
521 ImGui::PushID(label);
522 ImGui::BeginGroup();
523 ImGui::BeginDisabled(options.disabled);
524 PushStyleCombobox(options.color);
525 if (options.alignment == ComponentAlignment::Left) {
526 if (options.labelPosition == LabelPosition::Above) {
527 ImGui::Text(label);
528 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
529 } else if (options.labelPosition == LabelPosition::Near) {
530 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x - ImGui::GetStyle().ItemSpacing.x * 2);
531 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
532 ImGui::SetNextItemWidth(ImGui::CalcTextSize(comboVector.at(currentValueIndex)).x + ImGui::GetStyle().FramePadding.x * 4 + ImGui::GetStyle().ItemSpacing.x);
533 }
534 } else if (options.alignment == ComponentAlignment::Right) {
535 if (options.labelPosition == LabelPosition::Above) {
536 ImGui::NewLine();
537 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
538 ImGui::Text(label);
539 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
540 } else if (options.labelPosition == LabelPosition::Near) {
541 ImGui::SameLine(ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x * 2);
542 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
543 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
544 float width = ImGui::CalcTextSize(comboVector.at(currentValueIndex)).x + ImGui::GetStyle().FramePadding.x * 4;
545 ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
546 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
547 }
548 }
549 if (ImGui::BeginCombo(invisibleLabel, comboVector.at(currentValueIndex), options.flags)) {
550 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10.0f, 10.0f));
551 for (size_t i = 0; i < comboVector.size(); ++i) {
552 auto newValue = static_cast<T>(i);
553 if (strlen(comboVector.at(i)) > 1) {
554 if (ImGui::Selectable(comboVector.at(i), newValue == *value)) {
555 *value = newValue;
556 dirty = true;
557 }
558 }
559 }
560 ImGui::PopStyleVar();
561 ImGui::EndCombo();
562 }
563 if (options.alignment == ComponentAlignment::Left) {
564 if (options.labelPosition == LabelPosition::Near) {
565 ImGui::SameLine();
566 ImGui::Text(label);
567 } else if (options.labelPosition == LabelPosition::Far) {
568 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
569 ImGui::Text(label);
570 }
571 } else if (options.alignment == ComponentAlignment::Right) {
572 if (options.labelPosition == LabelPosition::Near || options.labelPosition == LabelPosition::Far) {
573 ImGui::SameLine(startX);
574 ImGui::Text(label);
575 }
576 }
578 ImGui::EndDisabled();
579 ImGui::EndGroup();
580 if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
581 ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
582 } else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
583 ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
584 }
585 ImGui::PopID();
586 return dirty;
587 }
588
589 template <typename T = size_t, size_t N>
590 bool Combobox(const char* label, T* value, const char* (&comboArray)[N], const ComboboxOptions& options = {}) {
591 bool dirty = false;
592 float startX = ImGui::GetCursorPosX();
593 size_t currentValueIndex = static_cast<size_t>(*value);
594 if (currentValueIndex >= N) {
595 currentValueIndex = 0;
596 }
597 std::string invisibleLabelStr = "##" + std::string(label);
598 const char* invisibleLabel = invisibleLabelStr.c_str();
599 ImGui::PushID(label);
600 ImGui::BeginGroup();
601 ImGui::BeginDisabled(options.disabled);
602 PushStyleCombobox(options.color);
603 if (options.alignment == ComponentAlignment::Left) {
604 if (options.labelPosition == LabelPosition::Above) {
605 ImGui::Text("%s", label);
606 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
607 } else if (options.labelPosition == LabelPosition::Near) {
608 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x - ImGui::GetStyle().ItemSpacing.x * 2);
609 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
610 ImGui::SetNextItemWidth(ImGui::CalcTextSize(comboArray[currentValueIndex]).x + ImGui::GetStyle().FramePadding.x * 4 + ImGui::GetStyle().ItemSpacing.x);
611 }
612 } else if (options.alignment == ComponentAlignment::Right) {
613 if (options.labelPosition == LabelPosition::Above) {
614 ImGui::NewLine();
615 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
616 ImGui::Text("%s", label);
617 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
618 } else if (options.labelPosition == LabelPosition::Near) {
619 ImGui::SameLine(ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x * 2);
620 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
621 } else if (options.labelPosition == LabelPosition::Far || options.labelPosition == LabelPosition::None) {
622 float width = ImGui::CalcTextSize(comboArray[currentValueIndex]).x + ImGui::GetStyle().FramePadding.x * 4;
623 ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
624 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
625 }
626 }
627 if (ImGui::BeginCombo(invisibleLabel, comboArray[currentValueIndex], options.flags)) {
628 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10.0f, 10.0f));
629 for (size_t i = 0; i < N; ++i) {
630 auto newValue = static_cast<T>(i);
631 if (strlen(comboArray[i]) > 1) {
632 if (ImGui::Selectable(comboArray[i], newValue == *value)) {
633 *value = newValue;
634 dirty = true;
635 }
636 }
637 }
638 ImGui::PopStyleVar();
639 ImGui::EndCombo();
640 }
641 if (options.alignment == ComponentAlignment::Left) {
642 if (options.labelPosition == LabelPosition::Near) {
643 ImGui::SameLine();
644 ImGui::Text("%s", label);
645 } else if (options.labelPosition == LabelPosition::Far) {
646 ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
647 ImGui::Text("%s", label);
648 }
649 } else if (options.alignment == ComponentAlignment::Right) {
650 if (options.labelPosition == LabelPosition::Near || options.labelPosition == LabelPosition::Far) {
651 ImGui::SameLine(startX);
652 ImGui::Text("%s", label);
653 }
654 }
656 ImGui::EndDisabled();
657 ImGui::EndGroup();
658 if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
659 ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
660 } else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
661 ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
662 }
663 ImGui::PopID();
664 return dirty;
665 }
666
667 template <typename T = int32_t>
668 bool CVarCombobox(const char* label, const char* cvarName, const std::unordered_map<T, const char*>& comboMap, const ComboboxOptions& options = {}) {
669 bool dirty = false;
670 int32_t value = CVarGetInteger(cvarName, options.defaultIndex);
671 if (Combobox<T>(label, &value, comboMap, options)) {
672 CVarSetInteger(cvarName, value);
673 Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
674 dirty = true;
675 }
676 return dirty;
677 }
678
679 template <typename T = int32_t>
680 bool CVarCombobox(const char* label, const char* cvarName, const std::vector<const char*>& comboVector, const ComboboxOptions& options = {}) {
681 bool dirty = false;
682 int32_t value = CVarGetInteger(cvarName, options.defaultIndex);
683 if (Combobox<T>(label, &value, comboVector, options)) {
684 CVarSetInteger(cvarName, value);
685 Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
686 dirty = true;
687 }
688 return dirty;
689 }
690
691 template <typename T = int32_t, size_t N>
692 bool CVarCombobox(const char* label, const char* cvarName, const char* (&comboArray)[N], const ComboboxOptions& options = {}) {
693 bool dirty = false;
694 int32_t value = CVarGetInteger(cvarName, options.defaultIndex);
695 if (Combobox<T>(label, &value, comboArray, options)) {
696 CVarSetInteger(cvarName, value);
697 Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
698 dirty = true;
699 }
700 return dirty;
701 }
702
704 void PopStyleSlider();
705 bool SliderInt(const char* label, int32_t* value, const IntSliderOptions& options = {});
706 bool CVarSliderInt(const char* label, const char* cvarName, const IntSliderOptions& options = {});
707 bool SliderFloat(const char* label, float* value, const FloatSliderOptions& options = {});
708 bool CVarSliderFloat(const char* label, const char* cvarName, const FloatSliderOptions& options = {});
709 bool CVarColorPicker(const char* label, const char* cvarName, Color_RGBA8 defaultColor);
710 void DrawFlagArray32(const std::string& name, uint32_t& flags);
711 void DrawFlagArray16(const std::string& name, uint16_t& flags);
712 void DrawFlagArray8(const std::string& name, uint8_t& flags);
713 void DrawFlagArray8Mask(const std::string& name, uint8_t& flags);
714}
715
716#endif /* UIWidgets_hpp */
bool Ship_IsCStringEmpty(const char *str)
Definition ShipUtils.cpp:10
#define NULL
Definition miniaudio.h:3851
Definition UIWidgets.h:89
const ImVec2 Inline
Definition UIWidgets.h:90
const ImVec2 Fill
Definition UIWidgets.h:91
Definition UIWidgets.cpp:11
bool CVarCheckbox(const char *label, const char *cvarName, const CheckboxOptions &options)
Definition UIWidgets.cpp:289
std::string WrappedText(const char *text, unsigned int charactersPerLine)
Definition UIWidgets.cpp:16
bool MenuItem(const char *label, const char *shortcut, Colors color)
Definition UIWidgets.cpp:94
LabelPosition
Definition UIWidgets.h:94
@ Above
Definition UIWidgets.h:97
@ Far
Definition UIWidgets.h:96
@ Near
Definition UIWidgets.h:95
@ Within
Definition UIWidgets.h:99
@ None
Definition UIWidgets.h:98
ComponentAlignment
Definition UIWidgets.h:102
@ Left
Definition UIWidgets.h:103
@ Right
Definition UIWidgets.h:104
void DrawFlagArray16(const std::string &name, uint16_t &flags)
Definition UIWidgets.cpp:564
bool Checkbox(const char *_label, bool *value, const CheckboxOptions &options)
Definition UIWidgets.cpp:197
void DrawFlagArray8Mask(const std::string &name, uint8_t &flags)
Definition UIWidgets.cpp:610
void PopStyleButton()
Definition UIWidgets.cpp:118
bool WindowButton(const char *label, const char *cvarName, std::shared_ptr< Ship::GuiWindow > windowPtr, const ButtonOptions &options)
Definition UIWidgets.cpp:138
bool SliderInt(const char *label, int32_t *value, const IntSliderOptions &options)
Definition UIWidgets.cpp:344
MenuExtent
Definition UIWidgets.h:107
@ Stretched
Definition UIWidgets.h:109
@ Condensed
Definition UIWidgets.h:108
void PushStyleCheckbox(const ImVec4 &color)
Definition UIWidgets.cpp:156
void PopStyleCheckbox()
Definition UIWidgets.cpp:171
void Tooltip(const char *text)
Definition UIWidgets.cpp:45
void PushStyleSlider(Colors color_)
Definition UIWidgets.cpp:325
bool CVarSliderFloat(const char *label, const char *cvarName, const FloatSliderOptions &options)
Definition UIWidgets.cpp:512
void PushStyleMenuItem(const ImVec4 &color)
Definition UIWidgets.cpp:80
void PushStyleButton(const ImVec4 &color)
Definition UIWidgets.cpp:104
void PushStyleMenu(const ImVec4 &color)
Definition UIWidgets.cpp:51
void DrawFlagArray8(const std::string &name, uint8_t &flags)
Definition UIWidgets.cpp:587
void PopStyleMenu()
Definition UIWidgets.cpp:64
void PopStyleMenuItem()
Definition UIWidgets.cpp:89
bool CVarSliderInt(const char *label, const char *cvarName, const IntSliderOptions &options)
Definition UIWidgets.cpp:402
void PushStyleCombobox(const ImVec4 &color)
Definition UIWidgets.cpp:300
bool SliderFloat(const char *label, float *value, const FloatSliderOptions &options)
Definition UIWidgets.cpp:451
void PopStyleSlider()
Definition UIWidgets.cpp:339
Colors
Definition UIWidgets.h:40
@ Blue
Definition UIWidgets.h:47
@ Red
Definition UIWidgets.h:41
@ DarkRed
Definition UIWidgets.h:42
@ Purple
Definition UIWidgets.h:51
@ DarkGray
Definition UIWidgets.h:54
@ Violet
Definition UIWidgets.h:50
@ LightGray
Definition UIWidgets.h:60
@ Brown
Definition UIWidgets.h:52
@ Green
Definition UIWidgets.h:44
@ Gray
Definition UIWidgets.h:53
@ DarkBlue
Definition UIWidgets.h:48
@ White
Definition UIWidgets.h:61
@ Indigo
Definition UIWidgets.h:49
@ NoColor
Definition UIWidgets.h:62
@ Pink
Definition UIWidgets.h:56
@ Cyan
Definition UIWidgets.h:58
@ Orange
Definition UIWidgets.h:43
@ Yellow
Definition UIWidgets.h:57
@ DarkGreen
Definition UIWidgets.h:45
@ Black
Definition UIWidgets.h:59
@ LightBlue
Definition UIWidgets.h:46
bool Combobox(const char *label, T *value, const std::unordered_map< T, const char * > &comboMap, const ComboboxOptions &options={})
Definition UIWidgets.h:442
const std::unordered_map< Colors, ImVec4 > ColorValues
Definition UIWidgets.h:65
void PopStyleCombobox()
Definition UIWidgets.cpp:320
void RenderText(ImVec2 pos, const char *text, const char *text_end, bool hide_text_after_hash)
Definition UIWidgets.cpp:176
bool CVarColorPicker(const char *label, const char *cvarName, Color_RGBA8 defaultColor)
Definition UIWidgets.cpp:523
void(*)() SectionFunc
Definition UIWidgets.h:16
bool BeginMenu(const char *label, Colors color)
Definition UIWidgets.cpp:69
bool CVarCombobox(const char *label, const char *cvarName, const std::unordered_map< T, const char * > &comboMap, const ComboboxOptions &options={})
Definition UIWidgets.h:668
void DrawFlagArray32(const std::string &name, uint32_t &flags)
Definition UIWidgets.cpp:541
bool Button(const char *label, const ButtonOptions &options)
Definition UIWidgets.cpp:123
size_t strlen(const char *str)
Definition string.c:14
char * strchr(const char *str, s32 ch)
Definition string.c:22
Definition UIWidgets.h:132
ButtonOptions & Size(ImVec2 size_)
Definition UIWidgets.h:136
ButtonOptions & Tooltip(const char *tooltip_)
Definition UIWidgets.h:140
ImVec2 size
Definition UIWidgets.h:133
Colors color
Definition UIWidgets.h:134
ButtonOptions & Color(Colors color_)
Definition UIWidgets.h:144
Definition UIWidgets.h:150
CheckboxOptions & LabelPosition(LabelPosition labelPosition_)
Definition UIWidgets.h:164
LabelPosition labelPosition
Definition UIWidgets.h:153
CheckboxOptions & Color(Colors color_)
Definition UIWidgets.h:172
CheckboxOptions & ComponentAlignment(ComponentAlignment alignment_)
Definition UIWidgets.h:160
Colors color
Definition UIWidgets.h:154
CheckboxOptions & DefaultValue(bool defaultValue_)
Definition UIWidgets.h:156
CheckboxOptions & Tooltip(const char *tooltip_)
Definition UIWidgets.h:168
bool defaultValue
Definition UIWidgets.h:151
ComponentAlignment alignment
Definition UIWidgets.h:152
Definition UIWidgets.h:178
std::unordered_map< int32_t, const char * > comboMap
Definition UIWidgets.h:179
ImGuiComboFlags flags
Definition UIWidgets.h:183
ComboboxOptions & Tooltip(const char *tooltip_)
Definition UIWidgets.h:202
ComboboxOptions & Color(Colors color_)
Definition UIWidgets.h:206
ComponentAlignment alignment
Definition UIWidgets.h:181
Colors color
Definition UIWidgets.h:184
uint32_t defaultIndex
Definition UIWidgets.h:180
ComboboxOptions & ComboMap(std::unordered_map< int32_t, const char * > comboMap_)
Definition UIWidgets.h:186
ComboboxOptions & ComponentAlignment(ComponentAlignment alignment_)
Definition UIWidgets.h:194
ComboboxOptions & LabelPosition(LabelPosition labelPosition_)
Definition UIWidgets.h:198
LabelPosition labelPosition
Definition UIWidgets.h:182
ComboboxOptions & DefaultIndex(uint32_t defaultIndex_)
Definition UIWidgets.h:190
Definition UIWidgets.h:266
const char * format
Definition UIWidgets.h:268
FloatSliderOptions & DefaultValue(float defaultValue_)
Definition UIWidgets.h:299
FloatSliderOptions & Tooltip(const char *tooltip_)
Definition UIWidgets.h:318
FloatSliderOptions & ComponentAlignment(ComponentAlignment alignment_)
Definition UIWidgets.h:303
float min
Definition UIWidgets.h:270
FloatSliderOptions & Format(const char *format_)
Definition UIWidgets.h:283
ComponentAlignment alignment
Definition UIWidgets.h:274
FloatSliderOptions & IsPercentage(bool isPercentage_=true)
Definition UIWidgets.h:311
FloatSliderOptions & Step(float step_)
Definition UIWidgets.h:287
FloatSliderOptions & ShowButtons(bool showButtons_)
Definition UIWidgets.h:279
Colors color
Definition UIWidgets.h:276
FloatSliderOptions & Color(Colors color_)
Definition UIWidgets.h:322
float defaultValue
Definition UIWidgets.h:272
LabelPosition labelPosition
Definition UIWidgets.h:275
bool showButtons
Definition UIWidgets.h:267
ImGuiSliderFlags flags
Definition UIWidgets.h:277
FloatSliderOptions & LabelPosition(LabelPosition labelPosition_)
Definition UIWidgets.h:307
bool isPercentage
Definition UIWidgets.h:273
float step
Definition UIWidgets.h:269
FloatSliderOptions & Min(float min_)
Definition UIWidgets.h:291
float max
Definition UIWidgets.h:271
FloatSliderOptions & Max(float max_)
Definition UIWidgets.h:295
Definition UIWidgets.h:212
Colors color
Definition UIWidgets.h:221
IntSliderOptions & LabelPosition(LabelPosition labelPosition_)
Definition UIWidgets.h:252
IntSliderOptions & ComponentAlignment(ComponentAlignment alignment_)
Definition UIWidgets.h:248
const char * format
Definition UIWidgets.h:214
IntSliderOptions & DefaultValue(int32_t defaultValue_)
Definition UIWidgets.h:244
ImGuiSliderFlags flags
Definition UIWidgets.h:222
IntSliderOptions & Min(int32_t min_)
Definition UIWidgets.h:236
LabelPosition labelPosition
Definition UIWidgets.h:220
IntSliderOptions & Tooltip(const char *tooltip_)
Definition UIWidgets.h:256
int32_t step
Definition UIWidgets.h:215
IntSliderOptions & Color(Colors color_)
Definition UIWidgets.h:260
int32_t max
Definition UIWidgets.h:217
int32_t min
Definition UIWidgets.h:216
IntSliderOptions & ShowButtons(bool showButtons_)
Definition UIWidgets.h:224
IntSliderOptions & Step(int32_t step_)
Definition UIWidgets.h:232
int32_t defaultValue
Definition UIWidgets.h:218
bool showButtons
Definition UIWidgets.h:213
ComponentAlignment alignment
Definition UIWidgets.h:219
IntSliderOptions & Format(const char *format_)
Definition UIWidgets.h:228
IntSliderOptions & Max(int32_t max_)
Definition UIWidgets.h:240
Definition UIWidgets.h:18
static int FilterAlphaNum(ImGuiInputTextCallbackData *data)
Definition UIWidgets.h:26
static int FilterNumbers(ImGuiInputTextCallbackData *data)
Definition UIWidgets.h:19
Definition UIWidgets.h:112
WidgetOptions & Tooltip(const char *tooltip_)
Definition UIWidgets.h:122
const char * disabledTooltip
Definition UIWidgets.h:115
const char * tooltip
Definition UIWidgets.h:113
WidgetOptions & Disabled(bool disabled_)
Definition UIWidgets.h:126
Colors color
Definition UIWidgets.h:116
WidgetOptions & Color(Colors color_)
Definition UIWidgets.h:118
bool disabled
Definition UIWidgets.h:114