Misc UI updates and fixes:
- Vertically center the server name for manually added servers - Add padding for feature icons - Use a popup for adding a new server - Display pointers for both controllers - Avoid jumps in scrolling when the focused controller changes - Grab the GUI by the pointed position instead of the center - Add battery level in lobby - Use the correct width in the server list if there is a scrollbar - Make sure the tooltip does not overflow on other layers - Save the virtual keyboard layout in the config file - Don't display the IP address for USB connections - Allow receiving intents when WiVRn is already running - Add recentering tip
This commit is contained in:
+299
-303
@@ -19,148 +19,148 @@
|
||||
|
||||
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <limits>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string_view>
|
||||
#include <unicode/uchar.h>
|
||||
#include <uni_algo/case.h>
|
||||
#include <uni_algo/ranges_grapheme.h>
|
||||
#include <utility>
|
||||
|
||||
#include "IconsFontAwesome6.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "lobby_keyboard.h"
|
||||
|
||||
// #include <stdexcept>
|
||||
// #include <cwchar>
|
||||
// #include <ranges>
|
||||
// #include <unicode/ustring.h> // u_strToUpper
|
||||
|
||||
static const ImGuiKey key_layout = (ImGuiKey)(ImGuiKey_NamedKey_END + 1);
|
||||
static const ImGuiKey key_symbols_letter = (ImGuiKey)(ImGuiKey_NamedKey_END + 2);
|
||||
|
||||
// See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fallback/main.qml
|
||||
virtual_keyboard::layout qwerty = {
|
||||
{
|
||||
{0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"q"},
|
||||
{1, u"w"},
|
||||
{1, u"eéèêë"},
|
||||
{1, u"rŕrř"},
|
||||
{1, u"tţtŧť"},
|
||||
{1, u"yÿyýŷ"},
|
||||
{1, u"uűūũûüuùú"},
|
||||
{1, u"iîïīĩiìí"},
|
||||
{1, u"oœøõôöòó"},
|
||||
{1, u"p"},
|
||||
{1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
|
||||
{0.5},
|
||||
{1, "q"},
|
||||
{1, "w"},
|
||||
{1, "eéèêë"},
|
||||
{1, "rŕrř"},
|
||||
{1, "tţtŧť"},
|
||||
{1, "yÿyýŷ"},
|
||||
{1, "uűūũûüuùú"},
|
||||
{1, "iîïīĩiìí"},
|
||||
{1, "oœøõôöòó"},
|
||||
{1, "p"},
|
||||
{1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
|
||||
}, // 12
|
||||
{
|
||||
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"aaäåãâàá"},
|
||||
{1, u"sšsşś"},
|
||||
{1, u"ddđď"},
|
||||
{1, u"f"},
|
||||
{1, u"gġgģĝğ"},
|
||||
{1, u"h"},
|
||||
{1, u"j"},
|
||||
{1, u"k"},
|
||||
{1, u"lĺŀłļľl"},
|
||||
{2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1},
|
||||
{1, "aaäåãâàá"},
|
||||
{1, "sšsşś"},
|
||||
{1, "ddđď"},
|
||||
{1, "f"},
|
||||
{1, "gġgģĝğ"},
|
||||
{1, "h"},
|
||||
{1, "j"},
|
||||
{1, "k"},
|
||||
{1, "lĺŀłļľl"},
|
||||
{2},
|
||||
}, // 12
|
||||
{
|
||||
{1.5, u"", ImGuiKey_LeftShift},
|
||||
{1, u"zzžż"},
|
||||
{1, u"x"},
|
||||
{1, u"cçcċčć"},
|
||||
{1, u"v"},
|
||||
{1, u"b"},
|
||||
{1, u"nņńnň"},
|
||||
{1, u"m"},
|
||||
{1, u","},
|
||||
{1, u"."},
|
||||
{1.5, "", ImGuiKey_LeftShift},
|
||||
{1, "zzžż"},
|
||||
{1, "x"},
|
||||
{1, "cçcċčć"},
|
||||
{1, "v"},
|
||||
{1, "b"},
|
||||
{1, "nņńnň"},
|
||||
{1, "m"},
|
||||
{1, ","},
|
||||
{1, "."},
|
||||
// {1, u"-"},
|
||||
{1.5, u"", ImGuiKey_RightShift},
|
||||
{1.5, "", ImGuiKey_RightShift},
|
||||
}, // 12
|
||||
{
|
||||
{2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"", key_symbols_letter, "?123"},
|
||||
{1, u"", key_layout, ICON_FA_GLOBE},
|
||||
{5, u" "},
|
||||
{3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{2},
|
||||
{1, "?123", key_symbols_letter},
|
||||
{1, ICON_FA_GLOBE, key_layout},
|
||||
{5, " "},
|
||||
{3},
|
||||
} // 12
|
||||
};
|
||||
|
||||
// See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fr_FR/main.qml
|
||||
virtual_keyboard::layout azerty = {
|
||||
{
|
||||
{0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"aàâæ"},
|
||||
{1, u"z"},
|
||||
{1, u"eéèêë"},
|
||||
{1, u"r"},
|
||||
{1, u"t"},
|
||||
{1, u"yÿ"},
|
||||
{1, u"uùûü"},
|
||||
{1, u"iîï"},
|
||||
{1, u"oôœ"},
|
||||
{1, u"p"},
|
||||
{1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
|
||||
{0.5},
|
||||
{1, "aàâæ"},
|
||||
{1, "z"},
|
||||
{1, "eéèêë"},
|
||||
{1, "r"},
|
||||
{1, "t"},
|
||||
{1, "yÿ"},
|
||||
{1, "uùûü"},
|
||||
{1, "iîï"},
|
||||
{1, "oôœ"},
|
||||
{1, "p"},
|
||||
{1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
|
||||
}, // 12
|
||||
{
|
||||
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"q"},
|
||||
{1, u"s"},
|
||||
{1, u"d"},
|
||||
{1, u"f"},
|
||||
{1, u"g"},
|
||||
{1, u"h"},
|
||||
{1, u"j"},
|
||||
{1, u"k"},
|
||||
{1, u"l"},
|
||||
{1, u"m"},
|
||||
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1},
|
||||
{1, "q"},
|
||||
{1, "s"},
|
||||
{1, "d"},
|
||||
{1, "f"},
|
||||
{1, "g"},
|
||||
{1, "h"},
|
||||
{1, "j"},
|
||||
{1, "k"},
|
||||
{1, "l"},
|
||||
{1, "m"},
|
||||
// {1, "é"},
|
||||
{1},
|
||||
}, // 12
|
||||
{
|
||||
{1.5, u"", ImGuiKey_LeftShift},
|
||||
{1, u"w"},
|
||||
{1, u"x"},
|
||||
{1, u"cç"},
|
||||
{1, u"v"},
|
||||
{1, u"b"},
|
||||
{1, u"n"},
|
||||
{1, u","},
|
||||
{1, u"."},
|
||||
{1, u"-"},
|
||||
{1.5, u"", ImGuiKey_RightShift},
|
||||
{1.5, "", ImGuiKey_LeftShift},
|
||||
{1, "w"},
|
||||
{1, "x"},
|
||||
{1, "cç"},
|
||||
{1, "v"},
|
||||
{1, "b"},
|
||||
{1, "n"},
|
||||
{1, ","},
|
||||
{1, "."},
|
||||
{1, "-"},
|
||||
{1.5, "", ImGuiKey_RightShift},
|
||||
}, // 12
|
||||
{
|
||||
{2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"", key_symbols_letter, "?123"},
|
||||
{1, u"", key_layout, ICON_FA_GLOBE},
|
||||
{5, u" "},
|
||||
{3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{2},
|
||||
{1, "?123", key_symbols_letter},
|
||||
{1, ICON_FA_GLOBE, key_layout},
|
||||
{5, " "},
|
||||
{3},
|
||||
} // 12
|
||||
};
|
||||
|
||||
virtual_keyboard::layout digits = {
|
||||
{
|
||||
{1, u"1"},
|
||||
{1, u"2"},
|
||||
{1, u"3"},
|
||||
{1, "1"},
|
||||
{1, "2"},
|
||||
{1, "3"},
|
||||
}, // 3
|
||||
{
|
||||
{1, u"4"},
|
||||
{1, u"5"},
|
||||
{1, u"6"},
|
||||
{1, "4"},
|
||||
{1, "5"},
|
||||
{1, "6"},
|
||||
}, // 3
|
||||
{
|
||||
{1, u"7"},
|
||||
{1, u"8"},
|
||||
{1, u"9"},
|
||||
{1, "7"},
|
||||
{1, "8"},
|
||||
{1, "9"},
|
||||
}, // 3
|
||||
{
|
||||
{2, u"0"},
|
||||
{1, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
|
||||
{2, "0"},
|
||||
{1, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
|
||||
} // 3
|
||||
};
|
||||
|
||||
@@ -168,120 +168,126 @@ virtual_keyboard::layout digits = {
|
||||
// virtual_keyboard::layout qwertz = {
|
||||
// };
|
||||
|
||||
std::array layouts = {&qwerty, &azerty};
|
||||
|
||||
virtual_keyboard::layout symbols = {
|
||||
{
|
||||
{0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"1"},
|
||||
{1, u"2"},
|
||||
{1, u"3"},
|
||||
{1, u"4"},
|
||||
{1, u"5"},
|
||||
{1, u"6"},
|
||||
{1, u"7"},
|
||||
{1, u"8"},
|
||||
{1, u"9"},
|
||||
{1, u"0"},
|
||||
{1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
|
||||
{0.5},
|
||||
{1, "1"},
|
||||
{1, "2"},
|
||||
{1, "3"},
|
||||
{1, "4"},
|
||||
{1, "5"},
|
||||
{1, "6"},
|
||||
{1, "7"},
|
||||
{1, "8"},
|
||||
{1, "9"},
|
||||
{1, "0"},
|
||||
{1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
|
||||
}, // 12
|
||||
{
|
||||
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"@"},
|
||||
{1, u"#"},
|
||||
{1, u"%"},
|
||||
{1, u"&"},
|
||||
{1, u"*"},
|
||||
{1, u"_"},
|
||||
{1, u"-"},
|
||||
{1, u"+"},
|
||||
{1, u"("},
|
||||
{1, u")"},
|
||||
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1},
|
||||
{1, "@"},
|
||||
{1, "#"},
|
||||
{1, "%"},
|
||||
{1, "&"},
|
||||
{1, "*"},
|
||||
{1, "_"},
|
||||
{1, "-"},
|
||||
{1, "+"},
|
||||
{1, "("},
|
||||
{1, ")"},
|
||||
{1},
|
||||
}, // 12
|
||||
{
|
||||
{1.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"", ImGuiKey_None, "1/2", virtual_keyboard::key_flag_hidden},
|
||||
{1, u"\""},
|
||||
{1, u"<"},
|
||||
{1, u">"},
|
||||
{1, u"'"},
|
||||
{1, u":"},
|
||||
{1, u"/"},
|
||||
{1, u"!"},
|
||||
{1, u"?"},
|
||||
{1.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1.5},
|
||||
{1}, // {1, "", ImGuiKey_None, "1/2", virtual_keyboard::key_flag_hidden},
|
||||
{1, "\""},
|
||||
{1, "<"},
|
||||
{1, ">"},
|
||||
{1, "'"},
|
||||
{1, ":"},
|
||||
{1, "/"},
|
||||
{1, "!"},
|
||||
{1, "?"},
|
||||
{1.5},
|
||||
}, // 12
|
||||
{
|
||||
{1, u"", key_symbols_letter, "ABC"},
|
||||
{1, u"", key_layout, ICON_FA_GLOBE},
|
||||
{5, u" "},
|
||||
{3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
|
||||
{1, "ABC", key_symbols_letter},
|
||||
{1, ICON_FA_GLOBE, key_layout},
|
||||
{5, " "},
|
||||
{3},
|
||||
},
|
||||
};
|
||||
|
||||
static std::string to_utf8(char32_t c)
|
||||
{
|
||||
std::string s;
|
||||
std::array layouts = {
|
||||
std::make_pair<std::string_view, virtual_keyboard::layout *>("QWERTY", &qwerty),
|
||||
std::make_pair<std::string_view, virtual_keyboard::layout *>("AZERTY", &azerty),
|
||||
};
|
||||
|
||||
if (c < 0x80)
|
||||
virtual_keyboard::key::key(float width, std::string_view characters, ImGuiKey keycode, key_flags flag) :
|
||||
width(width),
|
||||
keycode(keycode),
|
||||
flag(flag)
|
||||
{
|
||||
if (keycode == ImGuiKey_None)
|
||||
{
|
||||
s += (char)c;
|
||||
}
|
||||
else if (c < 0x800)
|
||||
{
|
||||
s += (char)(0xc0 | ((c >> 6) & 0x1f));
|
||||
s += (char)(0x80 | (c & 0x3f));
|
||||
}
|
||||
else if (c < 0x10000)
|
||||
{
|
||||
s += (char)(0xe0 | ((c >> 12) & 0x0f));
|
||||
s += (char)(0x80 | ((c >> 6) & 0x3f));
|
||||
s += (char)(0x80 | (c & 0x3f));
|
||||
// Normal key: the first character is displayed on the key
|
||||
characters_lower = characters;
|
||||
characters_upper = una::cases::to_uppercase_utf8(characters);
|
||||
|
||||
glyph_lower = *una::ranges::views::grapheme::utf8(characters_lower).begin();
|
||||
glyph_upper = *una::ranges::views::grapheme::utf8(characters_upper).begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
s += (char)(0xf0 | ((c >> 18) & 0x07));
|
||||
s += (char)(0x80 | ((c >> 12) & 0x3f));
|
||||
s += (char)(0x80 | ((c >> 6) & 0x3f));
|
||||
s += (char)(0x80 | (c & 0x3f));
|
||||
// Special key: characters contains the key label, except shift which depends
|
||||
// on caps lock
|
||||
assert(not characters.empty() or keycode == ImGuiKey_LeftShift or keycode == ImGuiKey_RightShift);
|
||||
glyph_lower = (std::string)characters;
|
||||
glyph_upper = (std::string)characters;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
// static std::string to_utf8(std::u16string_view str)
|
||||
// {
|
||||
// std::string s;
|
||||
// for(char16_t c: str)
|
||||
// s += to_utf8(c);
|
||||
//
|
||||
// return s;
|
||||
// }
|
||||
//
|
||||
// static std::u16string str_to_upper(std::u16string_view text)
|
||||
// {
|
||||
// UErrorCode err = U_ZERO_ERROR;
|
||||
//
|
||||
// size_t size = u_strToUpper(nullptr, 0, text.data(), text.size(), "" /* locale*/, &err);
|
||||
//
|
||||
// if (U_FAILURE(err))
|
||||
// throw std::runtime_error(std::string("u_strToUpper: ") + u_errorName(err));
|
||||
//
|
||||
// std::u16string upper;
|
||||
// upper.resize(size);
|
||||
//
|
||||
// err = U_ZERO_ERROR;
|
||||
// u_strToUpper(nullptr, 0, text.data(), text.size(), "" /* locale*/, &err);
|
||||
//
|
||||
// if (U_FAILURE(err))
|
||||
// throw std::runtime_error(std::string("u_strToUpper: ") + u_errorName(err));
|
||||
//
|
||||
// return upper;
|
||||
// }
|
||||
virtual_keyboard::key::key(float width) :
|
||||
width(width),
|
||||
keycode(ImGuiKey_None),
|
||||
flag(virtual_keyboard::key_flag_hidden)
|
||||
{
|
||||
}
|
||||
|
||||
// Reimplemented because ImGui::IsWindowHovered() returns false if a popup is open
|
||||
bool virtual_keyboard::is_window_hovered()
|
||||
{
|
||||
int i = 0;
|
||||
ImGuiContext & g = *GImGui;
|
||||
|
||||
if (not ImGui::IsMouseHoveringRect(g.CurrentWindow->Pos, g.CurrentWindow->Pos + g.CurrentWindow->Size))
|
||||
return false;
|
||||
|
||||
// Ignore any window below the keyboard
|
||||
while (g.Windows[i] != g.CurrentWindow and i < g.Windows.Size)
|
||||
i++;
|
||||
|
||||
// Cannot find the keyboard window
|
||||
if (i >= g.Windows.Size)
|
||||
return false;
|
||||
|
||||
// Skip the keyboard window
|
||||
i++;
|
||||
|
||||
// Check if another window is over the keyboard
|
||||
while (i < g.Windows.Size)
|
||||
{
|
||||
if (ImGui::IsMouseHoveringRect(g.Windows[i]->Pos, g.Windows[i]->Pos + g.Windows[i]->Size))
|
||||
return false;
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Mostly copied from ImGui::ButtonBehavior, massively simplified for the keyboard use case, don't take focus when clicked
|
||||
bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags)
|
||||
virtual_keyboard::key_status virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool window_hovered, ImGuiButtonFlags flags)
|
||||
{
|
||||
ImGuiContext & g = *GImGui;
|
||||
|
||||
@@ -294,37 +300,31 @@ bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out
|
||||
}
|
||||
|
||||
bool pressed = false;
|
||||
bool hovered = g.HoveredWindow == g.CurrentWindow and ImGui::IsMouseHoveringRect(bb.Min, bb.Max);
|
||||
// ImGui::ItemHoverable;
|
||||
bool hovered = window_hovered and ImGui::IsMouseHoveringRect(bb.Min, bb.Max);
|
||||
|
||||
bool mouse_down = ImGui::IsMouseDown(0, id);
|
||||
|
||||
// Mouse handling
|
||||
if (hovered)
|
||||
if (hovered and mouse_down and active_id != id)
|
||||
{
|
||||
// Changed wrt original imgui: only 1 button is present
|
||||
bool mouse_button_clicked = ImGui::IsMouseClicked(0, ImGuiInputFlags_None, id);
|
||||
active_id = id;
|
||||
held_duration = 0;
|
||||
|
||||
// Process initial action
|
||||
if (mouse_button_clicked && active_id != id)
|
||||
{
|
||||
active_id = id;
|
||||
held_duration = 0;
|
||||
|
||||
if (flags & ImGuiButtonFlags_PressedOnClick)
|
||||
pressed = true;
|
||||
}
|
||||
if (flags & ImGuiButtonFlags_PressedOnClick)
|
||||
pressed = true;
|
||||
}
|
||||
|
||||
// Process while held
|
||||
bool held = false;
|
||||
if (active_id == id)
|
||||
{
|
||||
if (ImGui::IsMouseDown(0, id))
|
||||
if (mouse_down)
|
||||
{
|
||||
held = true;
|
||||
|
||||
float prev_held_duration = held_duration;
|
||||
held_duration += g.IO.DeltaTime;
|
||||
if (flags & ImGuiButtonFlags_Repeat and held_duration > g.IO.KeyRepeatDelay)
|
||||
if ((flags & ImGuiButtonFlags_Repeat) and held_duration > g.IO.KeyRepeatDelay)
|
||||
{
|
||||
int n1 = (prev_held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate;
|
||||
int n2 = (held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate;
|
||||
@@ -335,54 +335,41 @@ bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease))
|
||||
if (hovered and (flags & ImGuiButtonFlags_PressedOnClickRelease))
|
||||
pressed = true;
|
||||
|
||||
active_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (out_hovered)
|
||||
*out_hovered = hovered;
|
||||
|
||||
if (out_held)
|
||||
*out_held = held;
|
||||
|
||||
return pressed;
|
||||
return key_status{
|
||||
.hovered = hovered,
|
||||
.held = held,
|
||||
.pressed = pressed,
|
||||
.hold_duration = held ? held_duration : 0};
|
||||
}
|
||||
|
||||
bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered)
|
||||
virtual_keyboard::key_status virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool window_hovered)
|
||||
{
|
||||
std::string key_label;
|
||||
bool is_shift = k.keycode == ImGuiKey_LeftShift or k.keycode == ImGuiKey_RightShift;
|
||||
|
||||
bool is_shift = k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift;
|
||||
|
||||
if (k.characters.empty())
|
||||
{
|
||||
if (is_shift)
|
||||
key_label = (current_case_mode == case_mode::lower) ? ICON_FA_CHEVRON_UP : ICON_FA_CIRCLE_CHEVRON_UP;
|
||||
else
|
||||
key_label = k.glyph;
|
||||
}
|
||||
else if (current_case_mode == case_mode::lower)
|
||||
{
|
||||
key_label = to_utf8(k.characters[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// FIXME: use ICU
|
||||
key_label = to_utf8(/*u_*/ towupper(k.characters[0]));
|
||||
}
|
||||
const std::string & key_label = is_shift
|
||||
? (current_case_mode == case_mode::lower)
|
||||
? ICON_FA_CHEVRON_UP
|
||||
: ICON_FA_CIRCLE_CHEVRON_UP
|
||||
: ((current_case_mode == case_mode::lower)
|
||||
? k.glyph_lower
|
||||
: k.glyph_upper);
|
||||
|
||||
std::string key_label_id;
|
||||
if (k.characters.empty())
|
||||
key_label_id = "virtual_keyboard_key_" + std::to_string(k.key);
|
||||
if (k.keycode != ImGuiKey_None)
|
||||
key_label_id = "virtual_keyboard_key_" + std::to_string(k.keycode);
|
||||
else
|
||||
key_label_id = "virtual_keyboard_" + to_utf8(k.characters[0]);
|
||||
key_label_id = "virtual_keyboard_" + std::string(k.glyph_lower);
|
||||
|
||||
ImGuiWindow * window = ImGui::GetCurrentWindow();
|
||||
if (window->SkipItems)
|
||||
return false;
|
||||
return {};
|
||||
|
||||
ImGuiContext & g = *GImGui;
|
||||
const ImGuiStyle & style = g.Style;
|
||||
@@ -395,25 +382,23 @@ bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_ar
|
||||
const ImRect bb(pos, pos + size);
|
||||
ImGui::ItemSize(size, style.FramePadding.y);
|
||||
if (!ImGui::ItemAdd(bb, id))
|
||||
return false;
|
||||
return {};
|
||||
|
||||
ImGuiButtonFlags flags = ImGuiButtonFlags_None;
|
||||
|
||||
if (k.flag & key_flag_repeat)
|
||||
flags |= ImGuiButtonFlags_Repeat;
|
||||
|
||||
bool held;
|
||||
bool pressed = button_behavior(bb, id, &hovered, &held, flags);
|
||||
auto status = button_behavior(bb, id, window_hovered, flags);
|
||||
|
||||
// Render
|
||||
bool active = (held and hovered) or (is_shift and current_case_mode == case_mode::caps_lock);
|
||||
bool active = (status.held and status.hovered) or (is_shift and current_case_mode == case_mode::caps_lock);
|
||||
|
||||
const ImU32 col = ImGui::GetColorU32(active ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered
|
||||
: ImGuiCol_Button);
|
||||
const ImU32 col = ImGui::GetColorU32(active ? ImGuiCol_ButtonActive : status.hovered ? ImGuiCol_ButtonHovered
|
||||
: ImGuiCol_Button);
|
||||
ImGui::RenderNavHighlight(bb, id);
|
||||
ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
|
||||
|
||||
// ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, key_label.c_str(), nullptr, &label_size, style.ButtonTextAlign, &bb);
|
||||
ImGui::RenderTextClippedEx(
|
||||
g.CurrentWindow->DrawList,
|
||||
bb.Min + style.FramePadding,
|
||||
@@ -424,52 +409,61 @@ bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_ar
|
||||
style.ButtonTextAlign,
|
||||
&bb);
|
||||
|
||||
if (pressed)
|
||||
{
|
||||
if (status.pressed)
|
||||
press_single_key(k);
|
||||
}
|
||||
|
||||
return pressed;
|
||||
return status;
|
||||
}
|
||||
|
||||
void virtual_keyboard::press_single_key(const key & k)
|
||||
{
|
||||
if (k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift)
|
||||
switch ((int)k.keycode) // cast to int to avoid -Werror=switch
|
||||
{
|
||||
switch (current_case_mode)
|
||||
{
|
||||
case case_mode::lower:
|
||||
current_case_mode = case_mode::upper;
|
||||
break;
|
||||
case case_mode::upper:
|
||||
current_case_mode = case_mode::caps_lock;
|
||||
break;
|
||||
case case_mode::caps_lock:
|
||||
current_case_mode = case_mode::lower;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (k.key != ImGuiKey_None)
|
||||
{
|
||||
ImGui::GetIO().AddKeyEvent(k.key, true);
|
||||
ImGui::GetIO().AddKeyEvent(k.key, false);
|
||||
}
|
||||
else if (not k.characters.empty())
|
||||
{
|
||||
switch (current_case_mode)
|
||||
{
|
||||
case case_mode::lower:
|
||||
ImGui::GetIO().AddInputCharactersUTF8(to_utf8(k.characters[0]).c_str());
|
||||
break;
|
||||
case ImGuiKey_LeftShift:
|
||||
case ImGuiKey_RightShift:
|
||||
switch (current_case_mode)
|
||||
{
|
||||
case case_mode::lower:
|
||||
current_case_mode = case_mode::upper;
|
||||
break;
|
||||
case case_mode::upper:
|
||||
current_case_mode = case_mode::caps_lock;
|
||||
break;
|
||||
case case_mode::caps_lock:
|
||||
current_case_mode = case_mode::lower;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case case_mode::upper:
|
||||
current_case_mode = case_mode::lower;
|
||||
[[fallthrough]];
|
||||
case key_layout:
|
||||
current_layout = (current_layout + 1) % layouts.size();
|
||||
break;
|
||||
|
||||
case case_mode::caps_lock:
|
||||
ImGui::GetIO().AddInputCharactersUTF8(to_utf8(towupper(k.characters[0])).c_str());
|
||||
break;
|
||||
}
|
||||
case key_symbols_letter:
|
||||
symbols_shown = not symbols_shown;
|
||||
break;
|
||||
|
||||
case ImGuiKey_None:
|
||||
switch (current_case_mode)
|
||||
{
|
||||
case case_mode::lower:
|
||||
ImGui::GetIO().AddInputCharactersUTF8(std::string(*una::ranges::views::grapheme::utf8(k.characters_lower).begin()).c_str());
|
||||
break;
|
||||
|
||||
case case_mode::upper:
|
||||
current_case_mode = case_mode::lower;
|
||||
[[fallthrough]];
|
||||
|
||||
case case_mode::caps_lock:
|
||||
ImGui::GetIO().AddInputCharactersUTF8(std::string(*una::ranges::views::grapheme::utf8(k.characters_upper).begin()).c_str());
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
ImGui::GetIO().AddKeyEvent(k.keycode, true);
|
||||
ImGui::GetIO().AddKeyEvent(k.keycode, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,7 +475,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
bool want_digits = input_state and (input_state->Flags & ImGuiInputTextFlags_CharsDecimal) != 0;
|
||||
|
||||
auto & layout = want_digits ? digits : symbols_shown ? symbols
|
||||
: *layouts[current_layout];
|
||||
: *layouts[current_layout].second;
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8});
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 16);
|
||||
@@ -499,7 +493,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
ImVec2 padding = ImGui::GetStyle().WindowPadding;
|
||||
size = {size.x - 2 * padding.x, size.y - 2 * padding.y};
|
||||
|
||||
// Compute keys size;
|
||||
// Compute keys size:
|
||||
float key_height = [&]() {
|
||||
int N = layout.size();
|
||||
|
||||
@@ -526,6 +520,8 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
ImVec2 row_position = ImGui::GetCursorPos();
|
||||
int id = 0;
|
||||
|
||||
bool window_hovered = is_window_hovered();
|
||||
|
||||
for (const auto & row: layout)
|
||||
{
|
||||
ImVec2 key_position = row_position;
|
||||
@@ -541,25 +537,8 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
{
|
||||
ImGui::SetCursorPos(key_position);
|
||||
|
||||
bool is_hovered;
|
||||
if (draw_single_key(key, id++, ImVec2{base_key_width * key.width - style.ItemSpacing.x, key_height}, is_hovered))
|
||||
{
|
||||
switch ((int)key.key) // cast to int to avoid -Werror=switch
|
||||
{
|
||||
case key_layout:
|
||||
current_layout = (current_layout + 1) % layouts.size();
|
||||
break;
|
||||
|
||||
case key_symbols_letter:
|
||||
symbols_shown = not symbols_shown;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_hovered)
|
||||
auto status = draw_single_key(key, id++, ImVec2{base_key_width * key.width - style.ItemSpacing.x, key_height}, window_hovered);
|
||||
if (status.hovered)
|
||||
hovered_id = ImGui::GetItemID();
|
||||
}
|
||||
|
||||
@@ -573,7 +552,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
|
||||
ImGui::SetCursorPos(row_position);
|
||||
|
||||
if (ImGui::IsWindowHovered())
|
||||
if (is_window_hovered())
|
||||
{
|
||||
ImGui::GetIO().MouseDown[0] = false;
|
||||
ImGui::GetIO().MouseClicked[0] = false;
|
||||
@@ -583,3 +562,20 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
ImGui::PopStyleColor(); // ImGuiCol_WindowBg
|
||||
ImGui::PopStyleVar(4); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_WindowRounding, ImGuiStyleVar_ItemSpacing, ImGuiStyleVar_FrameRounding
|
||||
}
|
||||
|
||||
const std::string_view virtual_keyboard::get_layout() const
|
||||
{
|
||||
return layouts[current_layout].first;
|
||||
}
|
||||
|
||||
void virtual_keyboard::set_layout(std::string_view layout_name)
|
||||
{
|
||||
for (int i = 0, n = layouts.size(); i < n; ++i)
|
||||
{
|
||||
if (layouts[i].first == layout_name)
|
||||
{
|
||||
current_layout = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user