载入中...
搜索中...
未找到
ControlPrimitives.h
浏览该文件的文档.
1#pragma once
2
3#include <imgui.h>
4
5#include <cstdint>
6#include <string>
7#include <vector>
8
10
12enum class ControlEdit : uint8_t { Unchanged = 0, Changed };
13
15inline ControlEdit button(const char *label, float width = 0.f, float height = 0.f) {
16 const bool changed = (width > 0.f || height > 0.f) ? ImGui::Button(label, ImVec2(width, height))
18 : ImGui::Button(label);
20}
21
23inline ControlEdit checkbox(const char *label, bool &value) {
25 return ImGui::Checkbox(label, &value) ? ControlEdit::Changed : ControlEdit::Unchanged;
26}
27
29inline ControlEdit slider(const char *label, float &value, float minimum, float maximum,
30 const char *format = "%.3f") {
32 return ImGui::SliderFloat(label, &value, minimum, maximum, format) ? ControlEdit::Changed
34}
35
37inline ControlEdit combo(const char *label, int &selected,
38 const std::vector<const char *> &items) {
39 const bool changed = !items.empty() &&
41 ImGui::Combo(label, &selected, items.data(),
42 static_cast<int>(items.size()));
44}
45
47inline ControlEdit textLink(const char *label) {
48 ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_CheckMark));
49 ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.f, 0.f, 0.f, 0.f));
50 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.f, 0.f, 0.f, 0.f));
51 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f));
52 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.f, 0.f));
53 const bool clicked = ImGui::Button(label);
54 const ImVec2 lo = ImGui::GetItemRectMin(), hi = ImGui::GetItemRectMax();
55 ImGui::GetWindowDrawList()->AddLine(ImVec2(lo.x, hi.y - 1.f), ImVec2(hi.x, hi.y - 1.f),
56 ImGui::GetColorU32(ImGuiCol_Text));
57 if (ImGui::IsItemHovered()) ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
58 ImGui::PopStyleVar();
59 ImGui::PopStyleColor(4);
61}
62
63namespace detail {
70inline int resizeInput(ImGuiInputTextCallbackData *data) {
71 if (data->EventFlag != ImGuiInputTextFlags_CallbackResize) return 0;
72 auto *value = static_cast<std::string *>(data->UserData);
73 value->resize(static_cast<size_t>(data->BufTextLen));
74 data->Buf = value->data();
75 return 0;
76}
77} // namespace detail
78
80inline ControlEdit inputText(const char *label, std::string &value, bool multiline = false,
81 ImVec2 multilineSize = ImVec2(0.f, 96.f), bool readOnly = false, const char *hint = "") {
82 if (value.capacity() == 0) value.reserve(64);
83 const ImGuiInputTextFlags flags =
84 ImGuiInputTextFlags_CallbackResize | (readOnly ? ImGuiInputTextFlags_ReadOnly : 0);
85 const bool changed = multiline ? ImGui::InputTextMultiline(label, value.data(), value.capacity() + 1, multilineSize,
88 : ImGui::InputTextWithHint(label, hint, value.data(), value.capacity() + 1, flags,
90 value.resize(std::char_traits<char>::length(value.c_str()));
92}
93
95inline ControlEdit inputTextWithHint(const char *label, const char *hint, std::string &value) {
96 if (value.capacity() == 0) value.reserve(64);
97 const bool changed = ImGui::InputTextWithHint(
98 label, hint, value.data(), value.capacity() + 1, ImGuiInputTextFlags_CallbackResize,
100 value.resize(std::char_traits<char>::length(value.c_str()));
102}
103
104} // namespace eve::ui::controls
std::string selected
bool changed
float maximum[3]
float minimum[3]
std::uint32_t height
std::uint32_t width
std::string label
bool readOnly
Object::Ptr value
int detail
uint32_t flags
int resizeInput(ImGuiInputTextCallbackData *data)
Grows an owning string for ImGui's synchronous resize callback.
ControlEdit slider(const char *label, float &value, float minimum, float maximum, const char *format="%.3f")
Shared immediate renderer for scalar sliders.
ControlEdit button(const char *label, float width=0.f, float height=0.f)
Shared immediate renderer for retained and EditorHost buttons.
ControlEdit combo(const char *label, int &selected, const std::vector< const char * > &items)
Shared immediate renderer for indexed combo boxes.
ControlEdit inputText(const char *label, std::string &value, bool multiline=false, ImVec2 multilineSize=ImVec2(0.f, 96.f), bool readOnly=false, const char *hint="")
UTF-8 owning text editor with automatic buffer growth.
ControlEdit inputTextWithHint(const char *label, const char *hint, std::string &value)
UTF-8 owning single-line editor with placeholder text and automatic growth.
ControlEdit textLink(const char *label)
Render a keyboard-accessible text link without launching its target.
ControlEdit checkbox(const char *label, bool &value)
Shared immediate renderer for retained and EditorHost checkboxes.
ControlEdit
Result of drawing one immediate control in the current frame.