yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
property_inspector.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_WIDGETS_PROPERTY_INSPECTOR_H_
2#define YAZE_APP_GUI_WIDGETS_PROPERTY_INSPECTOR_H_
3
4#include <cstdint>
5#include <string>
6#include <type_traits>
7
9#include "imgui/imgui.h"
10
11namespace yaze {
12namespace gui {
13
14// Typed property editor widgets. Each DrawProperty/DrawPropertyHex overload
15// returns true on commit (matching the ImGui::Input* contract). When called
16// inside a BeginPropertyTable(...) / EndPropertyTable() block, the label is
17// placed in the "Property" column and the editor in the "Value" column
18// automatically. When called outside a property table, the widget renders
19// inline with ImGui's default label-on-right placement.
20//
21// Undo integration: these functions do NOT post undo commands directly (no
22// dependency from the widget library into editor code). Wrap the return
23// value at the call site to bind into your editor's command manager:
24//
25// if (gui::DrawProperty("Retention", &settings.retention_count,
26// {.min = 1, .max = 365})) {
27// undo_manager_.Post(std::make_unique<SetRetentionCommand>(...));
28// }
29
31 // Numeric range. When max > min, the committed value is clamped. Otherwise
32 // no clamp is applied (still used as slider bounds if a slider is selected).
33 double min = 0.0;
34 double max = 0.0;
35
36 // Step controls for InputInt / InputFloat. Defaults produce ImGui standard
37 // step of 1 (step) and 10 (step_fast).
38 double step = 1.0;
39 double step_fast = 10.0;
40
41 // Printf format override for numeric types. If null, the widget uses
42 // ImGui's default ("%d" for int, "%.3f" for float, etc.).
43 const char* format = nullptr;
44
45 // Optional tooltip shown when hovering the label cell.
46 const char* tooltip = nullptr;
47
48 // Disable interaction; the widget still renders but greyed out.
49 bool read_only = false;
50
51 // Extra ImGui input flags (e.g., ImGuiInputTextFlags_EnterReturnsTrue).
52 ImGuiInputTextFlags text_flags = 0;
53};
54
55// Numeric / boolean / string / color property editors.
56bool DrawProperty(const char* label, bool* value,
57 const PropertyOptions& opts = {});
58bool DrawProperty(const char* label, int* value,
59 const PropertyOptions& opts = {});
60bool DrawProperty(const char* label, std::uint8_t* value,
61 const PropertyOptions& opts = {});
62bool DrawProperty(const char* label, std::uint16_t* value,
63 const PropertyOptions& opts = {});
64bool DrawProperty(const char* label, std::uint32_t* value,
65 const PropertyOptions& opts = {});
66bool DrawProperty(const char* label, float* value,
67 const PropertyOptions& opts = {});
68bool DrawProperty(const char* label, double* value,
69 const PropertyOptions& opts = {});
70bool DrawProperty(const char* label, std::string* value,
71 const PropertyOptions& opts = {});
72bool DrawProperty(const char* label, ImVec4* value,
73 const PropertyOptions& opts = {});
74bool DrawProperty(const char* label, Color* value,
75 const PropertyOptions& opts = {});
76
77// Hex-formatted numeric editors. Hex input restricts character set to
78// [0-9A-F] and formats output via %02X/%04X/%08X by default.
79bool DrawPropertyHex(const char* label, std::uint8_t* value,
80 const PropertyOptions& opts = {});
81bool DrawPropertyHex(const char* label, std::uint16_t* value,
82 const PropertyOptions& opts = {});
83bool DrawPropertyHex(const char* label, std::uint32_t* value,
84 const PropertyOptions& opts = {});
85
86// Enum combo. `items` is a null-terminated array of display labels indexed
87// by the enum's underlying integer value. Example:
88//
89// enum class Role { kReader, kWriter, kAdmin };
90// static constexpr const char* kRoles[] = {"Reader", "Writer", "Admin",
91// nullptr};
92// Role role = Role::kReader;
93// if (gui::DrawPropertyCombo("Role", &role, kRoles)) { ... }
94template <typename EnumT>
95bool DrawPropertyCombo(const char* label, EnumT* value,
96 const char* const* items,
97 const PropertyOptions& opts = {});
98
99// ---------------------------------------------------------------------------
100// Internal helpers exposed for tests and for template instantiations below.
101// ---------------------------------------------------------------------------
102namespace property_inspector_internal {
103
104// True if a BeginPropertyTable table is currently active on the ImGui stack.
105bool IsInPropertyTable();
106
107// Emit the label cell (when in a table) or no-op (when inline). Sets the
108// next item width to fill the value column when in a table.
109void BeginRow(const char* label, const char* tooltip);
110
111// Construct the ImGui widget ID. Inside a table we hide the label ("##") so
112// the label cell is the only visible label; otherwise we pass it through.
113std::string ResolveWidgetId(const char* label);
114
115} // namespace property_inspector_internal
116
117// ---------------------------------------------------------------------------
118// DrawPropertyCombo<EnumT> definition (header-only template).
119// ---------------------------------------------------------------------------
120template <typename EnumT>
121bool DrawPropertyCombo(const char* label, EnumT* value,
122 const char* const* items, const PropertyOptions& opts) {
123 static_assert(std::is_enum_v<EnumT>,
124 "DrawPropertyCombo requires an enum type");
125 int count = 0;
126 while (items[count] != nullptr)
127 ++count;
128 int current = static_cast<int>(*value);
129 if (current < 0 || current >= count)
130 current = 0;
131
133 const std::string id = property_inspector_internal::ResolveWidgetId(label);
134
135 ImGui::BeginDisabled(opts.read_only);
136 const bool changed = ImGui::Combo(id.c_str(), &current, items, count);
137 ImGui::EndDisabled();
138
139 if (changed && !opts.read_only) {
140 *value = static_cast<EnumT>(current);
141 }
142 return changed && !opts.read_only;
143}
144
145} // namespace gui
146} // namespace yaze
147
148#endif // YAZE_APP_GUI_WIDGETS_PROPERTY_INSPECTOR_H_
void BeginRow(const char *label, const char *tooltip)
std::string ResolveWidgetId(const char *label)
bool DrawProperty(const char *label, bool *value, const PropertyOptions &opts)
bool DrawPropertyCombo(const char *label, EnumT *value, const char *const *items, const PropertyOptions &opts={})
bool DrawPropertyHex(const char *label, std::uint8_t *value, const PropertyOptions &opts)
ImGuiInputTextFlags text_flags