199 lines
3.7 KiB
C++
199 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <cstring>
|
|
#include <utility>
|
|
|
|
namespace XCEngine {
|
|
namespace Editor {
|
|
namespace UI {
|
|
|
|
class DeferredPopupState {
|
|
public:
|
|
void RequestOpen() {
|
|
m_openRequested = true;
|
|
}
|
|
|
|
void ConsumeOpenRequest(const char* popupId) {
|
|
if (!m_openRequested) {
|
|
return;
|
|
}
|
|
|
|
ImGui::OpenPopup(popupId);
|
|
m_openRequested = false;
|
|
}
|
|
|
|
bool HasPendingOpenRequest() const {
|
|
return m_openRequested;
|
|
}
|
|
|
|
void Clear() {
|
|
m_openRequested = false;
|
|
}
|
|
|
|
private:
|
|
bool m_openRequested = false;
|
|
};
|
|
|
|
template <typename Target>
|
|
class TargetedPopupState {
|
|
public:
|
|
void RequestOpen(Target target) {
|
|
m_target = std::move(target);
|
|
m_hasTarget = true;
|
|
m_popup.RequestOpen();
|
|
}
|
|
|
|
void ConsumeOpenRequest(const char* popupId) {
|
|
m_popup.ConsumeOpenRequest(popupId);
|
|
}
|
|
|
|
bool HasPendingOpenRequest() const {
|
|
return m_popup.HasPendingOpenRequest();
|
|
}
|
|
|
|
bool HasTarget() const {
|
|
return m_hasTarget;
|
|
}
|
|
|
|
Target& TargetValue() {
|
|
return m_target;
|
|
}
|
|
|
|
const Target& TargetValue() const {
|
|
return m_target;
|
|
}
|
|
|
|
void Clear() {
|
|
m_popup.Clear();
|
|
m_target = Target{};
|
|
m_hasTarget = false;
|
|
}
|
|
|
|
private:
|
|
DeferredPopupState m_popup;
|
|
Target m_target{};
|
|
bool m_hasTarget = false;
|
|
};
|
|
|
|
template <size_t BufferCapacity>
|
|
class TextInputPopupState {
|
|
public:
|
|
void RequestOpen(const char* initialValue = "") {
|
|
SetValue(initialValue);
|
|
m_popup.RequestOpen();
|
|
}
|
|
|
|
void ConsumeOpenRequest(const char* popupId) {
|
|
m_popup.ConsumeOpenRequest(popupId);
|
|
}
|
|
|
|
void SetValue(const char* value) {
|
|
if (value && value[0] != '\0') {
|
|
strcpy_s(m_buffer, value);
|
|
return;
|
|
}
|
|
|
|
m_buffer[0] = '\0';
|
|
}
|
|
|
|
void Clear() {
|
|
m_buffer[0] = '\0';
|
|
}
|
|
|
|
bool Empty() const {
|
|
return m_buffer[0] == '\0';
|
|
}
|
|
|
|
char* Buffer() {
|
|
return m_buffer;
|
|
}
|
|
|
|
const char* Buffer() const {
|
|
return m_buffer;
|
|
}
|
|
|
|
constexpr size_t BufferSize() const {
|
|
return BufferCapacity;
|
|
}
|
|
|
|
private:
|
|
DeferredPopupState m_popup;
|
|
char m_buffer[BufferCapacity] = {};
|
|
};
|
|
|
|
template <typename ItemId, size_t BufferCapacity>
|
|
class InlineTextEditState {
|
|
public:
|
|
void Begin(ItemId itemId, const char* initialValue = "") {
|
|
m_active = true;
|
|
m_itemId = itemId;
|
|
SetValue(initialValue);
|
|
m_focusRequested = true;
|
|
}
|
|
|
|
bool IsActive() const {
|
|
return m_active;
|
|
}
|
|
|
|
bool IsEditing(ItemId itemId) const {
|
|
return m_active && m_itemId == itemId;
|
|
}
|
|
|
|
ItemId Item() const {
|
|
return m_itemId;
|
|
}
|
|
|
|
bool ConsumeFocusRequest() {
|
|
if (!m_focusRequested) {
|
|
return false;
|
|
}
|
|
|
|
m_focusRequested = false;
|
|
return true;
|
|
}
|
|
|
|
void SetValue(const char* value) {
|
|
if (value && value[0] != '\0') {
|
|
strcpy_s(m_buffer, value);
|
|
return;
|
|
}
|
|
|
|
m_buffer[0] = '\0';
|
|
}
|
|
|
|
bool Empty() const {
|
|
return m_buffer[0] == '\0';
|
|
}
|
|
|
|
char* Buffer() {
|
|
return m_buffer;
|
|
}
|
|
|
|
const char* Buffer() const {
|
|
return m_buffer;
|
|
}
|
|
|
|
constexpr size_t BufferSize() const {
|
|
return BufferCapacity;
|
|
}
|
|
|
|
void Cancel() {
|
|
m_active = false;
|
|
m_itemId = ItemId{};
|
|
m_buffer[0] = '\0';
|
|
m_focusRequested = false;
|
|
}
|
|
|
|
private:
|
|
bool m_active = false;
|
|
ItemId m_itemId = ItemId{};
|
|
char m_buffer[BufferCapacity] = {};
|
|
bool m_focusRequested = false;
|
|
};
|
|
|
|
} // namespace UI
|
|
} // namespace Editor
|
|
} // namespace XCEngine
|