optimize for each frame

This commit is contained in:
Filip Sykala - NTB T15p 2022-12-02 17:57:34 +01:00
parent 57695056ed
commit cb7705ddbd

View File

@ -274,10 +274,12 @@ bool ImGuiWrapper::update_key_data(wxKeyEvent &evt)
// Char event // Char event
const auto key = evt.GetUnicodeKey(); const auto key = evt.GetUnicodeKey();
unsigned int key_u = static_cast<unsigned int>(key); unsigned int key_u = static_cast<unsigned int>(key);
// Release BackSpace, Delete, ... when miss wxEVT_KEY_UP event // Release BackSpace, Delete, ... when miss wxEVT_KEY_UP event
if (key_u >= 0 && key_u < IM_ARRAYSIZE(io.KeysDown) && io.KeysDown[key_u]) { // Already Fixed at begining of new frame
io.KeysDown[key_u] = false; //if (key_u >= 0 && key_u < IM_ARRAYSIZE(io.KeysDown) && io.KeysDown[key_u]) {
} // io.KeysDown[key_u] = false;
//}
if (key != 0) { if (key != 0) {
io.AddInputCharacter(key); io.AddInputCharacter(key);
@ -299,6 +301,7 @@ bool ImGuiWrapper::update_key_data(wxKeyEvent &evt)
return ret; return ret;
} }
#include <array>
void ImGuiWrapper::new_frame() void ImGuiWrapper::new_frame()
{ {
if (m_new_frame_open) { if (m_new_frame_open) {
@ -312,40 +315,33 @@ void ImGuiWrapper::new_frame()
ImGui::NewFrame(); ImGui::NewFrame();
m_new_frame_open = true; m_new_frame_open = true;
ImGuiIO& io = ImGui::GetIO();
// synchronize key states // synchronize key states
// when the application loses the focus it may happen that the key up event is not processed // when the application loses the focus it may happen that the key up event is not processed
// modifier keys // synchronize modifier keys
auto synchronize_mod_key = [](const std::pair<ImGuiKeyModFlags_, wxKeyCode>& key) { constexpr std::array<std::pair<ImGuiKeyModFlags_, wxKeyCode>, 3> imgui_mod_keys{
ImGuiIO& io = ImGui::GetIO(); std::make_pair(ImGuiKeyModFlags_Ctrl, WXK_CONTROL),
std::make_pair(ImGuiKeyModFlags_Shift, WXK_SHIFT),
std::make_pair(ImGuiKeyModFlags_Alt, WXK_ALT)};
for (const std::pair<ImGuiKeyModFlags_, wxKeyCode>& key : imgui_mod_keys) {
if ((io.KeyMods & key.first) != 0 && !wxGetKeyState(key.second)) if ((io.KeyMods & key.first) != 0 && !wxGetKeyState(key.second))
io.KeyMods &= ~key.first; io.KeyMods &= ~key.first;
};
std::vector<std::pair<ImGuiKeyModFlags_, wxKeyCode>> imgui_mod_keys = {
{ ImGuiKeyModFlags_Ctrl, WXK_CONTROL },
{ ImGuiKeyModFlags_Shift, WXK_SHIFT },
{ ImGuiKeyModFlags_Alt, WXK_ALT }
};
for (const std::pair<ImGuiKeyModFlags_, wxKeyCode>& key : imgui_mod_keys) {
synchronize_mod_key(key);
} }
// regular keys // Not sure if it is neccessary
ImGuiIO& io = ImGui::GetIO(); // values from 33 to 126 are reserved for the standard ASCII characters
for (size_t i = 0; i < IM_ARRAYSIZE(io.KeysDown); ++i) { for (size_t i = 33; i <= 126; ++i) {
wxKeyCode keycode = WXK_NONE; wxKeyCode keycode = static_cast<wxKeyCode>(i);
if (33 <= i && i <= 126)
keycode = (wxKeyCode)i;
else {
auto it = std::find(std::begin(io.KeyMap), std::end(io.KeyMap), i);
if (it != std::end(io.KeyMap))
keycode = (wxKeyCode)i;
}
if (io.KeysDown[i] && keycode != WXK_NONE && !wxGetKeyState(keycode)) if (io.KeysDown[i] && keycode != WXK_NONE && !wxGetKeyState(keycode))
io.KeysDown[i] = false; io.KeysDown[i] = false;
}
// special keys: delete, backspace, ...
for (int key: io.KeyMap) {
wxKeyCode keycode = static_cast<wxKeyCode>(key);
if (io.KeysDown[key] && keycode != WXK_NONE && !wxGetKeyState(keycode))
io.KeysDown[key] = false;
} }
} }