Elevate Engine 1
Loading...
Searching...
No Matches
CustomImGuiCommand.cpp
Go to the documentation of this file.
1#include "eepch.h"
3
4#include <imgui.h>
5#include <imgui_internal.h>
6
7void Elevate::UI::InputField(const char* label, std::string& data)
8{
9 ImGui::Text("%s", label);
10 ImGui::SameLine();
11
12 char buffer[255];
13 strncpy(buffer, data.c_str(), sizeof(buffer));
14 buffer[sizeof(buffer) - 1] = '\0';
15
16 if (ImGui::InputText("##InputField", buffer, sizeof(buffer)))
17 {
18 data = buffer;
19 }
20}
21
22bool Elevate::UI::EECollapsingHeader(const char* label, bool canOpen, const void* icon, std::function<float()> headerCustomContent)
23{
24 using namespace ImGui;
25
26 ImGuiStyle& style = GetStyle();
27 ImGuiWindow* window = GetCurrentWindow();
28 if (window->SkipItems)
29 return false;
30
31 ImGuiID id = window->GetID(label);
32
33 float btnWidth;
34 if (headerCustomContent)
35 {
36 SetNextItemAllowOverlap();
37 ImVec2 prevCursor = GetCursorScreenPos();
38 btnWidth = ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - headerCustomContent() - style.FramePadding.x;
39
40 SetCursorScreenPos(prevCursor);
41 }
42
43 bool* p_open = GetStateStorage()->GetBoolRef(id, true);
44
45 std::string btn_id = std::string("##EECollapsingHeaderBtn_") + label;
46 bool clicked = Button(btn_id.c_str(), ImVec2(btnWidth, 0.0f));
47 if (clicked)
48 *p_open = !*p_open;
49
50 ImVec2 r_min = GetItemRectMin();
51 ImVec2 r_max = GetItemRectMax();
52 float btn_height = r_max.y - r_min.y;
53
54 float arrow_size = 7.0f;
55 ImVec2 arrow_pos = ImVec2(
56 r_min.x + arrow_size * 0.5f,
57 r_min.y + style.FramePadding.y
58 );
59
60 float icon_size = icon ? btn_height - style.FramePadding.y * 2 : 0.0f;
61 icon_size = ImMin(icon_size, 32.0f);
62
63 float icon_y_offset = (btn_height - icon_size) * 0.5f;
64 ImVec2 icon_pos = ImVec2(
65 r_min.x + style.FramePadding.x + arrow_size * 2 + style.ItemInnerSpacing.x,
66 r_min.y + icon_y_offset
67 );
68
69 ImVec2 text_pos = ImVec2(
70 r_min.x + style.FramePadding.x + arrow_size * 2 + 2 * style.ItemInnerSpacing.x + icon_size,
71 r_min.y + (btn_height - GetFontSize()) * 0.5f
72 );
73
74 ImDrawList* dl = window->DrawList;
75
76 if (canOpen)
77 RenderArrow(dl, arrow_pos, GetColorU32(ImGuiCol_Text),
78 *p_open ? ImGuiDir_Down : ImGuiDir_Right, 1.0f);
79
80 if (icon)
81 {
82 dl->AddImage(
83 (ImTextureID)icon,
84 icon_pos,
85 icon_pos + ImVec2(icon_size, icon_size),
86 ImVec2(0, 0), ImVec2(1, 1),
87 GetColorU32(ImGuiCol_Text)
88 );
89 }
90
91 dl->AddText(GetFont(), GetFontSize(), text_pos, GetColorU32(ImGuiCol_Text), label);
92
93 return *p_open;
94}
void InputField(const char *label, std::string &data)
bool EECollapsingHeader(const char *label, bool canOpen=true, const void *icon=nullptr, std::function< float()> headerCustomContent=nullptr)