Fixed issues with renaming/synchronizing state

This commit is contained in:
2025-11-16 00:11:27 -05:00
parent a968371ac6
commit 131934df1a
7 changed files with 54 additions and 23 deletions

View File

@@ -138,6 +138,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE
external/lunasvg external/lunasvg
external/SDL external/SDL
external/SDL_mixer external/SDL_mixer
external/SDL_mixer/include
include include
include/glad include/glad
src src

View File

@@ -12,6 +12,7 @@ using namespace glm;
namespace anm2ed::imgui namespace anm2ed::imgui
{ {
void theme_set(theme::Type theme) void theme_set(theme::Type theme)
{ {
switch (theme) switch (theme)
@@ -79,39 +80,42 @@ namespace anm2ed::imgui
} }
bool selectable_input_text(const std::string& label, const std::string& id, std::string& text, bool isSelected, bool selectable_input_text(const std::string& label, const std::string& id, std::string& text, bool isSelected,
ImGuiSelectableFlags flags, bool* isRenamed, bool isBeginEditing) ImGuiSelectableFlags flags, RenameState& state)
{ {
static std::string editID{}; static std::string editID{};
static bool isJustEdit{}; auto isRename = editID == id;
const bool isEditing = editID == id;
bool isActivated{}; bool isActivated{};
if (isEditing) if (isRename)
{ {
if (isJustEdit) auto finish = [&]()
{
editID.clear();
isActivated = true;
state = RENAME_FINISHED;
};
if (state == RENAME_BEGIN)
{ {
ImGui::SetKeyboardFocusHere(); ImGui::SetKeyboardFocusHere();
isJustEdit = false; state = RENAME_EDITING;
} }
ImGui::SetNextItemWidth(-FLT_MIN); ImGui::SetNextItemWidth(-FLT_MIN);
if (input_text_string("##Edit", &text, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)) if (input_text_string("##Edit", &text, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll))
{ finish();
editID.clear(); if (ImGui::IsItemDeactivatedAfterEdit() || ImGui::IsKeyPressed(ImGuiKey_Escape)) finish();
isActivated = true;
if (isRenamed) *isRenamed = true;
}
if (ImGui::IsItemDeactivatedAfterEdit() || ImGui::IsKeyPressed(ImGuiKey_Escape)) editID.clear();
} }
else else
{ {
if (ImGui::Selectable(label.c_str(), isSelected, flags)) isActivated = true; if (ImGui::Selectable(label.c_str(), isSelected, flags)) isActivated = true;
if (isBeginEditing || (ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_F2) && isSelected) || if (state == RENAME_FORCE_EDIT || (ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_F2)) ||
(ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))) (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)))
{ {
state = RENAME_BEGIN;
editID = id; editID = id;
isJustEdit = true; isActivated = true;
} }
} }

View File

@@ -36,6 +36,15 @@ namespace anm2ed::imgui
POPUP_BY_CURSOR POPUP_BY_CURSOR
}; };
enum RenameState
{
RENAME_SELECTABLE,
RENAME_BEGIN,
RENAME_EDITING,
RENAME_FINISHED,
RENAME_FORCE_EDIT
};
constexpr float POPUP_MULTIPLIERS[] = { constexpr float POPUP_MULTIPLIERS[] = {
#define X(name, multiplier, isHeightSet) multiplier, #define X(name, multiplier, isHeightSet) multiplier,
POPUP_LIST POPUP_LIST
@@ -170,8 +179,8 @@ namespace anm2ed::imgui
bool input_float_range(const char*, float&, float, float, float = STEP, float = STEP_FAST, const char* = "%.3f", bool input_float_range(const char*, float&, float, float, float = STEP, float = STEP_FAST, const char* = "%.3f",
ImGuiInputTextFlags = 0); ImGuiInputTextFlags = 0);
bool combo_negative_one_indexed(const std::string&, int*, std::vector<const char*>&); bool combo_negative_one_indexed(const std::string&, int*, std::vector<const char*>&);
bool selectable_input_text(const std::string&, const std::string&, std::string&, bool = false, bool selectable_input_text(const std::string& label, const std::string& id, std::string& text, bool isSelected,
ImGuiSelectableFlags = 0, bool* = nullptr, bool = false); ImGuiSelectableFlags flags, RenameState& state);
void set_item_tooltip_shortcut(const char*, const std::string& = {}); void set_item_tooltip_shortcut(const char*, const std::string& = {});
void external_storage_set(ImGuiSelectionExternalStorage*, int, bool); void external_storage_set(ImGuiSelectionExternalStorage*, int, bool);
void render_checker_background(ImDrawList*, ImVec2, ImVec2, glm::vec2, float); void render_checker_background(ImDrawList*, ImVec2, ImVec2, glm::vec2, float);

View File

@@ -54,7 +54,7 @@ namespace anm2ed::imgui
auto isDefault = anm2.animations.defaultAnimation == animation.name; auto isDefault = anm2.animations.defaultAnimation == animation.name;
auto isReferenced = reference.animationIndex == (int)i; auto isReferenced = reference.animationIndex == (int)i;
auto isNewSelection = newAnimationSelectedIndex == (int)i; auto isNewAnimation = newAnimationSelectedIndex == (int)i;
auto font = isDefault && isReferenced ? font::BOLD_ITALICS auto font = isDefault && isReferenced ? font::BOLD_ITALICS
: isDefault ? font::BOLD : isDefault ? font::BOLD
@@ -64,15 +64,21 @@ namespace anm2ed::imgui
ImGui::PushFont(resources.fonts[font].get(), font::SIZE); ImGui::PushFont(resources.fonts[font].get(), font::SIZE);
ImGui::SetNextItemSelectionUserData((int)i); ImGui::SetNextItemSelectionUserData((int)i);
if (isNewAnimation) renameState = RENAME_FORCE_EDIT;
if (selectable_input_text(animation.name, std::format("###Document #{} Animation #{}", manager.selected, i), if (selectable_input_text(animation.name, std::format("###Document #{} Animation #{}", manager.selected, i),
animation.name, selection.contains((int)i), ImGuiSelectableFlags_None, nullptr, animation.name, selection.contains((int)i), ImGuiSelectableFlags_None, renameState))
isNewSelection))
{ {
reference = {(int)i}; reference = {(int)i};
document.frames.clear(); document.frames.clear();
if (renameState == RENAME_BEGIN)
document.snapshot("Rename Animation");
else if (renameState == RENAME_FINISHED)
document.change(Document::ANIMATIONS);
} }
if (ImGui::IsItemHovered()) hovered = (int)i; if (ImGui::IsItemHovered()) hovered = (int)i;
if (isNewSelection)
if (isNewAnimation)
{ {
ImGui::SetScrollHereY(0.5f); ImGui::SetScrollHereY(0.5f);
newAnimationSelectedIndex = -1; newAnimationSelectedIndex = -1;

View File

@@ -11,6 +11,7 @@ namespace anm2ed::imgui
{ {
PopupHelper mergePopup{PopupHelper("Merge Animations")}; PopupHelper mergePopup{PopupHelper("Merge Animations")};
int newAnimationSelectedIndex{-1}; int newAnimationSelectedIndex{-1};
RenameState renameState{RENAME_SELECTABLE};
public: public:
void update(Manager&, Settings&, Resources&, Clipboard&); void update(Manager&, Settings&, Resources&, Clipboard&);

View File

@@ -32,12 +32,21 @@ namespace anm2ed::imgui
for (auto& [id, event] : anm2.content.events) for (auto& [id, event] : anm2.content.events)
{ {
auto isNewEvent = (newEventId == id);
ImGui::PushID(id); ImGui::PushID(id);
ImGui::SetNextItemSelectionUserData(id); ImGui::SetNextItemSelectionUserData(id);
const bool isNewEvent = (newEventId == id); if (isNewEvent) renameState = RENAME_FORCE_EDIT;
if (selectable_input_text(event.name, std::format("###Document #{} Event #{}", manager.selected, id), if (selectable_input_text(event.name, std::format("###Document #{} Event #{}", manager.selected, id),
event.name, selection.contains(id), ImGuiSelectableFlags_None, nullptr, isNewEvent)) event.name, selection.contains(id), ImGuiSelectableFlags_None, renameState))
{
if (renameState == RENAME_BEGIN)
document.snapshot("Rename Event");
else if (renameState == RENAME_FINISHED)
document.change(Document::EVENTS);
}
if (ImGui::IsItemHovered()) hovered = id; if (ImGui::IsItemHovered()) hovered = id;
if (isNewEvent) if (isNewEvent)
{ {
ImGui::SetScrollHereY(0.5f); ImGui::SetScrollHereY(0.5f);

View File

@@ -10,6 +10,7 @@ namespace anm2ed::imgui
class Events class Events
{ {
int newEventId{-1}; int newEventId{-1};
RenameState renameState{RENAME_SELECTABLE};
public: public:
void update(Manager&, Settings&, Resources&, Clipboard&); void update(Manager&, Settings&, Resources&, Clipboard&);