how dumb, imgui has a repeat feature for shortcuts, removed a lot of nonsense

This commit is contained in:
2025-11-19 22:31:28 -05:00
parent ca719d7dfb
commit ab65eb7c58
6 changed files with 25 additions and 76 deletions

View File

@@ -3,7 +3,6 @@
#include <imgui/imgui_internal.h> #include <imgui/imgui_internal.h>
#include <cmath> #include <cmath>
#include <set>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
@@ -300,66 +299,14 @@ namespace anm2ed::imgui
return ImVec2(ImGui::GetTextLineHeightWithSpacing(), ImGui::GetTextLineHeightWithSpacing()); return ImVec2(ImGui::GetTextLineHeightWithSpacing(), ImGui::GetTextLineHeightWithSpacing());
} }
bool chord_held(ImGuiKeyChord chord) bool shortcut(ImGuiKeyChord chord, shortcut::Type type)
{
auto& io = ImGui::GetIO();
for (constexpr ImGuiKey mods[] = {ImGuiMod_Ctrl, ImGuiMod_Shift, ImGuiMod_Alt, ImGuiMod_Super}; ImGuiKey mod : mods)
{
bool required = (chord & mod) != 0;
if (bool held = io.KeyMods & mod; required && !held) return false;
}
auto main_key = (ImGuiKey)(chord & ~ImGuiMod_Mask_);
if (main_key == ImGuiKey_None) return false;
return ImGui::IsKeyDown(main_key);
}
bool chord_repeating(ImGuiKeyChord chord, float delay, float rate)
{
struct State
{
float timeHeld = 0.f;
float nextRepeat = 0.f;
};
static std::unordered_map<ImGuiKeyChord, State> stateMap;
auto& io = ImGui::GetIO();
auto& state = stateMap[chord];
if (chord_held(chord))
{
state.timeHeld += io.DeltaTime;
if (state.timeHeld <= io.DeltaTime)
{
state.nextRepeat = delay;
return true;
}
if (state.timeHeld >= state.nextRepeat)
{
state.nextRepeat += rate;
return true;
}
}
else
{
state.timeHeld = 0.f;
state.nextRepeat = 0.f;
}
return false;
}
bool shortcut(ImGuiKeyChord chord, shortcut::Type type, bool isRepeat)
{ {
if (ImGui::GetTopMostPopupModal() != nullptr) return false; if (ImGui::GetTopMostPopupModal() != nullptr) return false;
if (isRepeat && !isRenaming) return chord_repeating(chord);
int flags = type == shortcut::GLOBAL || type == shortcut::GLOBAL_SET ? ImGuiInputFlags_RouteGlobal int flags = type == shortcut::GLOBAL || type == shortcut::GLOBAL_SET ? ImGuiInputFlags_RouteGlobal
: ImGuiInputFlags_RouteFocused; : ImGuiInputFlags_RouteFocused;
flags |= ImGuiInputFlags_Repeat;
if (type == shortcut::GLOBAL_SET || type == shortcut::FOCUSED_SET) if (type == shortcut::GLOBAL_SET || type == shortcut::FOCUSED_SET)
{ {
ImGui::SetNextItemShortcut(chord, flags); ImGui::SetNextItemShortcut(chord, flags);

View File

@@ -185,9 +185,7 @@ namespace anm2ed::imgui
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);
ImVec2 icon_size_get(); ImVec2 icon_size_get();
bool chord_held(ImGuiKeyChord); bool shortcut(ImGuiKeyChord, types::shortcut::Type = types::shortcut::FOCUSED_SET);
bool chord_repeating(ImGuiKeyChord, float = ImGui::GetIO().KeyRepeatDelay, float = ImGui::GetIO().KeyRepeatRate);
bool shortcut(ImGuiKeyChord, types::shortcut::Type = types::shortcut::FOCUSED_SET, bool = false);
class MultiSelectStorage : public std::set<int> class MultiSelectStorage : public std::set<int>
{ {

View File

@@ -659,8 +659,8 @@ namespace anm2ed::imgui
auto isKeyDown = isLeftDown || isRightDown || isUpDown || isDownDown; auto isKeyDown = isLeftDown || isRightDown || isUpDown || isDownDown;
auto isKeyReleased = isLeftReleased || isRightReleased || isUpReleased || isDownReleased; auto isKeyReleased = isLeftReleased || isRightReleased || isUpReleased || isDownReleased;
auto isZoomIn = chord_repeating(manager.chords[SHORTCUT_ZOOM_IN]); auto isZoomIn = shortcut(manager.chords[SHORTCUT_ZOOM_IN], shortcut::GLOBAL);
auto isZoomOut = chord_repeating(manager.chords[SHORTCUT_ZOOM_OUT]); auto isZoomOut = shortcut(manager.chords[SHORTCUT_ZOOM_OUT], shortcut::GLOBAL);
auto isBegin = isMouseClicked || isKeyJustPressed; auto isBegin = isMouseClicked || isKeyJustPressed;
auto isDuring = isMouseDown || isKeyDown; auto isDuring = isMouseDown || isKeyDown;

View File

@@ -241,8 +241,8 @@ namespace anm2ed::imgui
auto isKeyDown = isLeftDown || isRightDown || isUpDown || isDownDown; auto isKeyDown = isLeftDown || isRightDown || isUpDown || isDownDown;
auto isKeyReleased = isLeftReleased || isRightReleased || isUpReleased || isDownReleased; auto isKeyReleased = isLeftReleased || isRightReleased || isUpReleased || isDownReleased;
auto isZoomIn = chord_repeating(string_to_chord(settings.shortcutZoomIn)); auto isZoomIn = shortcut(manager.chords[SHORTCUT_ZOOM_IN], shortcut::GLOBAL);
auto isZoomOut = chord_repeating(string_to_chord(settings.shortcutZoomOut)); auto isZoomOut = shortcut(manager.chords[SHORTCUT_ZOOM_OUT], shortcut::GLOBAL);
auto isBegin = isMouseClicked || isKeyJustPressed; auto isBegin = isMouseClicked || isKeyJustPressed;
auto isDuring = isMouseDown || isKeyDown; auto isDuring = isMouseDown || isKeyDown;

View File

@@ -200,10 +200,7 @@ namespace anm2ed::imgui
return anm2::NONE; return anm2::NONE;
}; };
auto item_selection_clear = [&]() auto item_selection_clear = [&]() { itemSelection.clear(); };
{
itemSelection.clear();
};
auto item_selection_sync = [&]() auto item_selection_sync = [&]()
{ {
@@ -1928,20 +1925,23 @@ namespace anm2ed::imgui
{ {
if (shortcut(manager.chords[SHORTCUT_PLAY_PAUSE], shortcut::GLOBAL)) playback.toggle(); if (shortcut(manager.chords[SHORTCUT_PLAY_PAUSE], shortcut::GLOBAL)) playback.toggle();
if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_BACK], shortcut::GLOBAL, true)) if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_BACK], shortcut::GLOBAL))
{ {
playback.decrement(settings.playbackIsClamp ? animation->frameNum : anm2::FRAME_NUM_MAX); playback.decrement(settings.playbackIsClamp ? animation->frameNum : anm2::FRAME_NUM_MAX);
document.frameTime = playback.time; document.frameTime = playback.time;
} }
if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_FORWARD], shortcut::GLOBAL, true)) if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_FORWARD], shortcut::GLOBAL))
{ {
playback.increment(settings.playbackIsClamp ? animation->frameNum : anm2::FRAME_NUM_MAX); playback.increment(settings.playbackIsClamp ? animation->frameNum : anm2::FRAME_NUM_MAX);
document.frameTime = playback.time; document.frameTime = playback.time;
} }
if (shortcut(manager.chords[SHORTCUT_SHORTEN_FRAME], shortcut::GLOBAL)) document.snapshot("Shorten Frame"); static bool isShortenChordHeld = false;
if (shortcut(manager.chords[SHORTCUT_SHORTEN_FRAME], shortcut::GLOBAL, true)) auto isShortenFrame = shortcut(manager.chords[SHORTCUT_SHORTEN_FRAME], shortcut::GLOBAL);
if (isShortenFrame && !isShortenChordHeld) document.snapshot("Shorten Frame");
if (isShortenFrame)
{ {
if (auto frame = document.frame_get()) if (auto frame = document.frame_get())
@@ -1950,9 +1950,12 @@ namespace anm2ed::imgui
document.change(Document::FRAMES); document.change(Document::FRAMES);
} }
} }
isShortenChordHeld = isShortenFrame;
if (shortcut(manager.chords[SHORTCUT_EXTEND_FRAME], shortcut::GLOBAL)) document.snapshot("Extend Frame"); static bool isExtendChordHeld = false;
if (shortcut(manager.chords[SHORTCUT_EXTEND_FRAME], shortcut::GLOBAL, true)) auto isExtendFrame = shortcut(manager.chords[SHORTCUT_EXTEND_FRAME], shortcut::GLOBAL);
if (isExtendFrame && !isExtendChordHeld) document.snapshot("Extend Frame");
if (isExtendFrame)
{ {
if (auto frame = document.frame_get()) if (auto frame = document.frame_get())
@@ -1961,8 +1964,9 @@ namespace anm2ed::imgui
document.change(Document::FRAMES); document.change(Document::FRAMES);
} }
} }
isExtendChordHeld = isExtendFrame;
if (shortcut(manager.chords[SHORTCUT_PREVIOUS_FRAME], shortcut::GLOBAL, true)) if (shortcut(manager.chords[SHORTCUT_PREVIOUS_FRAME], shortcut::GLOBAL))
{ {
if (auto item = document.item_get(); !item->frames.empty()) if (auto item = document.item_get(); !item->frames.empty())
{ {
@@ -1972,7 +1976,7 @@ namespace anm2ed::imgui
} }
} }
if (shortcut(manager.chords[SHORTCUT_NEXT_FRAME], shortcut::GLOBAL, true)) if (shortcut(manager.chords[SHORTCUT_NEXT_FRAME], shortcut::GLOBAL))
{ {
if (auto item = document.item_get(); !item->frames.empty()) if (auto item = document.item_get(); !item->frames.empty())
{ {

View File

@@ -52,7 +52,7 @@ namespace anm2ed::imgui
if (isSelected) ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); if (isSelected) ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
if (shortcut(manager.chords[info.shortcut], shortcut::GLOBAL_SET, true)) tool_use((tool::Type)i); if (shortcut(manager.chords[info.shortcut], shortcut::GLOBAL)) tool_use((tool::Type)i);
if (i == tool::COLOR) if (i == tool::COLOR)
{ {