Recast Navigation
Navigation-mesh Toolset for Games
Loading...
Searching...
No Matches
imguiHelpers.h
Go to the documentation of this file.
1#pragma once
2
3#include "AppState.h"
4
5#include <imgui.h>
6#include <stdio.h>
7
8extern AppState app;
9
10inline void DrawScreenspaceText(float x, float y, ImU32 color, const char* text, bool centered = false)
11{
12 if (centered)
13 {
14 const ImVec2 textSize = ImGui::CalcTextSize(text);
15 x -= textSize.x * 0.5f;
16 }
17
18 ImGui::GetForegroundDrawList()->AddText({x, y}, color, text);
19}
20
21inline void DrawWorldspaceText(float x, float y, float z, ImU32 color, const char* text, bool centered = false, float offsetY = 0.0f)
22{
23 float screenX;
24 float screenY;
25 app.worldToScreen(x, y, z, &screenX, &screenY);
26 screenY += offsetY;
27
28 if (centered)
29 {
30 const ImVec2 textSize = ImGui::CalcTextSize(text);
31 screenX -= textSize.x * 0.5f;
32 }
33
34 ImGui::GetForegroundDrawList()->AddText({screenX, screenY}, color, text);
35}
36
37inline void DrawFloatSlider(
38 float* value,
39 float min,
40 float max,
41 const char* id,
42 const char* label,
43 const char* valueFormat = "%.2f")
44{
45 // Draw the slider
46 ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
47 ImGui::SliderFloat(id, value, min, max, "");
48 ImGui::PopItemWidth();
49
50 // Get the bounds of the slider
51 ImVec2 sliderMin = ImGui::GetItemRectMin();
52 ImVec2 sliderMax = ImGui::GetItemRectMax();
53 ImVec2 sliderSize = ImGui::GetItemRectSize();
54
55 ImDrawList* drawList = ImGui::GetWindowDrawList();
56 ImU32 color = ImGui::GetColorU32(ImGuiCol_Text);
57
58 // Draw the label left-aligned inside the slider
59 constexpr int textPadding = 6;
60 ImVec2 labelSize = ImGui::CalcTextSize(label);
61 float textY = sliderMin.y + (sliderSize.y - labelSize.y) * 0.5f;
62 drawList->AddText(ImVec2(sliderMin.x + textPadding, textY), color, label);
63
64 // Draw the value label right-aligned inside the slider
65 char valueLabel[32];
66 snprintf(valueLabel, sizeof(valueLabel), valueFormat, *value);
67 ImVec2 valueLabelSize = ImGui::CalcTextSize(valueLabel);
68 drawList->AddText(ImVec2(sliderMax.x - valueLabelSize.x - textPadding, textY), color, valueLabel);
69}
70
71inline void DrawIntSlider(int* value, int min, int max, const char* id, const char* label, const char* valueFormat = "%d")
72{
73 // Draw the slider
74 ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
75 ImGui::SliderInt(id, value, min, max, "");
76 ImGui::PopItemWidth();
77
78 // Get the bounds of the slider
79 ImVec2 sliderMin = ImGui::GetItemRectMin();
80 ImVec2 sliderMax = ImGui::GetItemRectMax();
81 ImVec2 sliderSize = ImGui::GetItemRectSize();
82
83 ImDrawList* drawList = ImGui::GetWindowDrawList();
84 ImU32 color = ImGui::GetColorU32(ImGuiCol_Text);
85
86 // Draw the label left-aligned inside the slider
87 constexpr int textPadding = 6;
88 ImVec2 labelSize = ImGui::CalcTextSize(label);
89 float textY = sliderMin.y + (sliderSize.y - labelSize.y) * 0.5f;
90 drawList->AddText(ImVec2(sliderMin.x + textPadding, textY), color, label);
91
92 // Draw the value label right-aligned inside the slider
93 char valueLabel[32];
94 snprintf(valueLabel, sizeof(valueLabel), valueFormat, *value);
95 ImVec2 valueLabelSize = ImGui::CalcTextSize(valueLabel);
96 drawList->AddText(ImVec2(sliderMax.x - valueLabelSize.x - textPadding, textY), color, valueLabel);
97}
98
99inline void DrawRightAlignedText(const char* format, ...)
100{
101 static char text[2048];
102
103 va_list args;
104 va_start(args, format);
105 vsnprintf(text, sizeof(text), format, args);
106 va_end(args);
107
108 float textWidth = ImGui::CalcTextSize(text).x;
109 float parentWidth = ImGui::GetContentRegionAvail().x;
110
111 ImGui::SetCursorPosX(ImGui::GetCursorPosX() + parentWidth - textWidth);
112 ImGui::Text("%s", text);
113}
void DrawIntSlider(int *value, int min, int max, const char *id, const char *label, const char *valueFormat="%d")
Definition imguiHelpers.h:71
void DrawScreenspaceText(float x, float y, ImU32 color, const char *text, bool centered=false)
Definition imguiHelpers.h:10
void DrawWorldspaceText(float x, float y, float z, ImU32 color, const char *text, bool centered=false, float offsetY=0.0f)
Definition imguiHelpers.h:21
void DrawRightAlignedText(const char *format,...)
Definition imguiHelpers.h:99
AppState app
Definition main.cpp:71
void DrawFloatSlider(float *value, float min, float max, const char *id, const char *label, const char *valueFormat="%.2f")
Definition imguiHelpers.h:37
Definition AppState.h:15
void worldToScreen(float x, float y, float z, float *screenX, float *screenY) const
Definition AppState.cpp:62