Add virtual keyboard, GUI to add a server manually
This commit is contained in:
@@ -794,21 +794,14 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
if (auto position = new_states[new_focused_controller].pointer_position)
|
||||
{
|
||||
io.AddMousePosEvent(position->x, position->y);
|
||||
|
||||
if (focused_change || (last_trigger || last_touching) != (button_pressed || fingertip_touching))
|
||||
{
|
||||
io.AddMouseButtonEvent(0, button_pressed || fingertip_touching);
|
||||
}
|
||||
io.AddMouseButtonEvent(0, button_pressed || fingertip_touching);
|
||||
|
||||
if (glm::length(scroll) > 0.01f)
|
||||
io.AddMouseWheelEvent(scroll.x, scroll.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((last_trigger || last_touching) && !(button_pressed || fingertip_touching))
|
||||
{
|
||||
io.AddMouseButtonEvent(0, button_pressed || fingertip_touching);
|
||||
}
|
||||
io.AddMouseButtonEvent(0, button_pressed || fingertip_touching);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,11 @@ public:
|
||||
// Position of this viewport in the swapchain image
|
||||
glm::ivec2 vp_origin;
|
||||
glm::ivec2 vp_size;
|
||||
|
||||
ImVec2 vp_center() const
|
||||
{
|
||||
return ImVec2(vp_origin.x + int(vp_size.x / 2), vp_origin.y + int(vp_size.y / 2));
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "utils/contains.h"
|
||||
#include "version.h"
|
||||
#include "wivrn_client.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "xr/passthrough.h"
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
|
||||
@@ -192,6 +193,11 @@ scenes::lobby::lobby()
|
||||
spdlog::info("Composition layer color scale/bias supported");
|
||||
else
|
||||
spdlog::info("Composition layer color scale/bias NOT supported");
|
||||
|
||||
strcpy(add_server_window_prettyname, "");
|
||||
strcpy(add_server_window_hostname, "");
|
||||
add_server_window_port = wivrn::default_port;
|
||||
add_server_tcp_only = false;
|
||||
}
|
||||
|
||||
static std::string ip_address_to_string(const in_addr & addr)
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "render/scene_data.h"
|
||||
#include "scene.h"
|
||||
#include "scenes/hand_model.h"
|
||||
#include "scenes/lobby_keyboard.h"
|
||||
#include "wifi_lock.h"
|
||||
#include "wivrn_discover.h"
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
@@ -52,6 +53,7 @@ class lobby : public scene_impl<lobby>
|
||||
char add_server_window_prettyname[200];
|
||||
char add_server_window_hostname[200];
|
||||
int add_server_window_port;
|
||||
bool add_server_tcp_only;
|
||||
|
||||
utils::future<std::unique_ptr<wivrn_session>, std::string> async_session;
|
||||
std::optional<std::string> async_error;
|
||||
@@ -110,14 +112,16 @@ class lobby : public scene_impl<lobby>
|
||||
tab last_current_tab = tab::server_list;
|
||||
ImTextureID about_picture;
|
||||
|
||||
virtual_keyboard keyboard;
|
||||
|
||||
void draw_features_status(XrTime predicted_display_time);
|
||||
void gui_connecting();
|
||||
void gui_server_list();
|
||||
void gui_add_server();
|
||||
void gui_new_server();
|
||||
void gui_settings();
|
||||
void gui_about();
|
||||
void gui_licenses();
|
||||
void gui_keyboard(ImVec2 size);
|
||||
void gui_keyboard();
|
||||
|
||||
void setup_passthrough();
|
||||
|
||||
|
||||
+57
-21
@@ -120,55 +120,67 @@ void scenes::lobby::gui_connecting()
|
||||
vibrate_on_hover();
|
||||
}
|
||||
|
||||
void scenes::lobby::gui_add_server()
|
||||
void scenes::lobby::gui_new_server()
|
||||
{
|
||||
const ImVec2 button_size(220, 80);
|
||||
|
||||
// TODO column widths
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20});
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, {10, 10});
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10});
|
||||
ImGui::Dummy({1000, 1});
|
||||
|
||||
ImGui::BeginTable("table", 2);
|
||||
|
||||
ImGui::TableSetupColumn("Field name", ImGuiTableColumnFlags_WidthFixed);
|
||||
ImGui::TableSetupColumn("Field value", ImGuiTableColumnFlags_WidthStretch);
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", _S("Displayed name"));
|
||||
// Make sure the label is vertically centered wrt the text input
|
||||
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetStyle().FramePadding.y);
|
||||
ImGui::Text("%s", _S("Name"));
|
||||
|
||||
static char buf[100];
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::InputText("##Name", buf, sizeof(buf));
|
||||
if (ImGui::IsWindowAppearing())
|
||||
ImGui::SetKeyboardFocusHere();
|
||||
ImGui::InputTextEx("##Name", nullptr, add_server_window_prettyname, sizeof(add_server_window_prettyname), {ImGui::GetContentRegionAvail().x, 0}, 0);
|
||||
vibrate_on_hover();
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", _S("Host name"));
|
||||
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetStyle().FramePadding.y);
|
||||
ImGui::Text("%s", _S("Address"));
|
||||
|
||||
static char buf2[100];
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::InputText("##Hostname", buf2, sizeof(buf2));
|
||||
ImGui::InputTextEx("##Hostname", nullptr, add_server_window_hostname, sizeof(add_server_window_hostname), {ImGui::GetContentRegionAvail().x, 0}, 0);
|
||||
vibrate_on_hover();
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetStyle().FramePadding.y);
|
||||
ImGui::Text("%s", _S("Port"));
|
||||
|
||||
static int port;
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::InputInt("##Port", &port);
|
||||
ImGui::InputInt("##Port", &add_server_window_port, 1, 1, ImGuiInputTextFlags_CharsDecimal);
|
||||
vibrate_on_hover();
|
||||
|
||||
ImGui::EndTable();
|
||||
|
||||
// TODO virtual keyboard
|
||||
// See https://github.com/qt/qtvirtualkeyboard/tree/dev/src/layouts/fallback
|
||||
// https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fr_FR/main.qml
|
||||
// https://doc.qt.io/qt-6/qtvirtualkeyboard-layouts.html
|
||||
|
||||
gui_keyboard(ImVec2(1000, 280));
|
||||
ImGui::Checkbox("TCP only", &add_server_tcp_only);
|
||||
vibrate_on_hover();
|
||||
|
||||
auto top_left = ImGui::GetWindowContentRegionMin();
|
||||
auto bottom_right = ImGui::GetWindowContentRegionMax();
|
||||
|
||||
ImGui::SetCursorPosX(top_left.x);
|
||||
if (ImGui::Button("Cancel", button_size))
|
||||
{
|
||||
current_tab = tab::server_list;
|
||||
strcpy(add_server_window_prettyname, "");
|
||||
strcpy(add_server_window_hostname, "");
|
||||
add_server_window_port = wivrn::default_port;
|
||||
add_server_tcp_only = false;
|
||||
}
|
||||
vibrate_on_hover();
|
||||
|
||||
ImGui::SameLine(bottom_right.x - button_size.x);
|
||||
@@ -182,14 +194,22 @@ void scenes::lobby::gui_add_server()
|
||||
.name = add_server_window_prettyname,
|
||||
.hostname = add_server_window_hostname,
|
||||
.port = add_server_window_port,
|
||||
.tcp_only = add_server_tcp_only,
|
||||
},
|
||||
};
|
||||
|
||||
auto & config = application::get_config();
|
||||
config.servers.emplace("manual-" + data.service.name, data);
|
||||
config.save();
|
||||
|
||||
strcpy(add_server_window_prettyname, "");
|
||||
strcpy(add_server_window_hostname, "");
|
||||
add_server_window_port = wivrn::default_port;
|
||||
add_server_tcp_only = false;
|
||||
}
|
||||
vibrate_on_hover();
|
||||
|
||||
ImGui::PopStyleVar(3);
|
||||
}
|
||||
|
||||
void scenes::lobby::gui_server_list()
|
||||
@@ -252,12 +272,12 @@ void scenes::lobby::gui_server_list()
|
||||
// ImGui::SetTooltip("Tooltip");
|
||||
// }
|
||||
|
||||
ImVec2 button_position(ImGui::GetWindowContentRegionMax().x - style.WindowPadding.x - 20, pos.y + (list_item_height - button_size.y) / 2);
|
||||
ImVec2 button_position(ImGui::GetWindowContentRegionMax().x - style.WindowPadding.x, pos.y + (list_item_height - button_size.y) / 2);
|
||||
|
||||
button_position.x -= button_size.x + style.WindowPadding.x;
|
||||
ImGui::SetCursorPos(button_position);
|
||||
|
||||
bool enable_connect_button = (data.visible || data.manual) && data.compatible;
|
||||
bool enable_connect_button = (data.visible and data.compatible) or data.manual;
|
||||
ImGui::BeginDisabled(!enable_connect_button);
|
||||
if (enable_connect_button)
|
||||
{
|
||||
@@ -281,7 +301,7 @@ void scenes::lobby::gui_server_list()
|
||||
|
||||
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
||||
{
|
||||
if (!data.compatible)
|
||||
if (!data.compatible && !data.manual)
|
||||
ImGui::SetTooltip("%s", _S("Incompatible server version"));
|
||||
else if (!data.visible && !data.manual)
|
||||
ImGui::SetTooltip("%s", _S("Server not available"));
|
||||
@@ -912,6 +932,11 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
|
||||
}
|
||||
}
|
||||
|
||||
void scenes::lobby::gui_keyboard()
|
||||
{
|
||||
keyboard.display(hovered_item);
|
||||
}
|
||||
|
||||
std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_display_time)
|
||||
{
|
||||
for (const auto & [key, server]: application::get_config().servers)
|
||||
@@ -927,11 +952,18 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
|
||||
auto last_hovered = hovered_item;
|
||||
hovered_item = 0;
|
||||
|
||||
if (ImGui::GetIO().WantTextInput)
|
||||
{
|
||||
ImGui::SetNextWindowPos(imgui_ctx->layers()[2].vp_center(), ImGuiCond_Always, {0.5, 0.5});
|
||||
|
||||
gui_keyboard();
|
||||
}
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0});
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ScrollbarSize, 30);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32(8, 8, 8, 224));
|
||||
|
||||
ImGui::SetNextWindowPos({50, 50});
|
||||
ImGui::SetNextWindowPos(imgui_ctx->layers()[0].vp_center(), ImGuiCond_Always, {0.5, 0.5});
|
||||
ImGui::SetNextWindowSize({1400, 900});
|
||||
|
||||
ImGui::Begin("WiVRn", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
|
||||
@@ -940,7 +972,7 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
|
||||
|
||||
{
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10);
|
||||
ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0));
|
||||
ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX() - 20, 0));
|
||||
ImGui::SetCursorPosY(20);
|
||||
|
||||
switch (current_tab)
|
||||
@@ -951,6 +983,7 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
|
||||
break;
|
||||
|
||||
case tab::new_server:
|
||||
gui_new_server();
|
||||
break;
|
||||
|
||||
case tab::settings:
|
||||
@@ -994,6 +1027,9 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
|
||||
RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Server list"), ¤t_tab, tab::server_list, {TabWidth, 0});
|
||||
vibrate_on_hover();
|
||||
|
||||
RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("New server"), ¤t_tab, tab::new_server, {TabWidth, 0});
|
||||
vibrate_on_hover();
|
||||
|
||||
RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), ¤t_tab, tab::settings, {TabWidth, 0});
|
||||
vibrate_on_hover();
|
||||
|
||||
|
||||
+429
-231
@@ -19,101 +19,206 @@
|
||||
|
||||
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||
|
||||
#include <cctype>
|
||||
#include <limits>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string_view>
|
||||
#include <unicode/uchar.h>
|
||||
|
||||
#include "IconsFontAwesome6.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "lobby.h"
|
||||
#include "lobby_keyboard.h"
|
||||
|
||||
#include <cwchar>
|
||||
#include <ranges>
|
||||
#include <unicode/uchar.h> // u_toupper
|
||||
// #include <stdexcept>
|
||||
// #include <cwchar>
|
||||
// #include <ranges>
|
||||
// #include <unicode/ustring.h> // u_strToUpper
|
||||
|
||||
struct key
|
||||
{
|
||||
float width;
|
||||
const std::u32string_view characters;
|
||||
ImGuiKey key = ImGuiKey_None;
|
||||
bool visible = true;
|
||||
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},
|
||||
}, // 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},
|
||||
}, // 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, u"-"},
|
||||
{1.5, u"", 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},
|
||||
} // 12
|
||||
};
|
||||
|
||||
using keyboard_layout = std::vector<std::vector<key>>;
|
||||
// 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},
|
||||
}, // 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},
|
||||
}, // 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},
|
||||
}, // 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},
|
||||
} // 12
|
||||
};
|
||||
|
||||
keyboard_layout azerty = {
|
||||
virtual_keyboard::layout digits = {
|
||||
{
|
||||
{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"},
|
||||
{2, U"", ImGuiKey_Backslash},
|
||||
},
|
||||
{1, u"1"},
|
||||
{1, u"2"},
|
||||
{1, u"3"},
|
||||
}, // 3
|
||||
{
|
||||
{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"4"},
|
||||
{1, u"5"},
|
||||
{1, u"6"},
|
||||
}, // 3
|
||||
{
|
||||
{2, 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, U"!§"},
|
||||
}};
|
||||
{1, u"7"},
|
||||
{1, u"8"},
|
||||
{1, u"9"},
|
||||
}, // 3
|
||||
{
|
||||
{2, u"0"},
|
||||
{1, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
|
||||
} // 3
|
||||
};
|
||||
|
||||
keyboard_layout symbols = {
|
||||
// // See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fallback/main.qml
|
||||
// virtual_keyboard::layout qwertz = {
|
||||
// };
|
||||
|
||||
std::array layouts = {&qwerty, &azerty};
|
||||
|
||||
virtual_keyboard::layout symbols = {
|
||||
{
|
||||
{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"},
|
||||
{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},
|
||||
}, // 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},
|
||||
}, // 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},
|
||||
}, // 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, U"@"},
|
||||
{1, U"#"},
|
||||
{1, U"%"},
|
||||
{1, U"&"},
|
||||
{1, U"*"},
|
||||
{1, U"_"},
|
||||
{1, U"-"},
|
||||
{1, U"+"},
|
||||
{1, U"("},
|
||||
{1, U")"},
|
||||
},
|
||||
{
|
||||
{1, U"\""},
|
||||
{1, U"<"},
|
||||
{1, U">"},
|
||||
{1, U"'"},
|
||||
{1, U"*"},
|
||||
{1, U":"},
|
||||
{1, U"/"},
|
||||
{1, U"!"},
|
||||
{1, U"?"},
|
||||
{1, U""},
|
||||
}};
|
||||
};
|
||||
|
||||
static std::string to_utf8(char32_t c)
|
||||
{
|
||||
@@ -145,243 +250,336 @@ static std::string to_utf8(char32_t c)
|
||||
return s;
|
||||
}
|
||||
|
||||
static bool button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags)
|
||||
// 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;
|
||||
// }
|
||||
|
||||
// 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)
|
||||
{
|
||||
ImGuiContext & g = *GImGui;
|
||||
ImGuiWindow * window = ImGui::GetCurrentWindow();
|
||||
|
||||
// Default behavior inherited from item flags
|
||||
// Note that _both_ ButtonFlags and ItemFlags are valid sources, so copy one into the item_flags and only check that.
|
||||
ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags);
|
||||
if (flags & ImGuiButtonFlags_AllowOverlap)
|
||||
item_flags |= ImGuiItemFlags_AllowOverlap;
|
||||
if (flags & ImGuiButtonFlags_Repeat)
|
||||
item_flags |= ImGuiItemFlags_ButtonRepeat;
|
||||
if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0)
|
||||
{
|
||||
if (g.IO.MouseSource == ImGuiMouseSource_VRHandTracking)
|
||||
flags |= ImGuiButtonFlags_PressedOnClick;
|
||||
else
|
||||
flags |= ImGuiButtonFlags_PressedOnClickRelease;
|
||||
}
|
||||
|
||||
bool pressed = false;
|
||||
bool hovered = ImGui::ItemHoverable(bb, id, item_flags);
|
||||
bool hovered = g.HoveredWindow == g.CurrentWindow and ImGui::IsMouseHoveringRect(bb.Min, bb.Max);
|
||||
// ImGui::ItemHoverable;
|
||||
|
||||
// Mouse handling
|
||||
const ImGuiID test_owner_id = (flags & ImGuiButtonFlags_NoTestKeyOwner) ? ImGuiKeyOwner_Any : id;
|
||||
if (hovered)
|
||||
{
|
||||
// Poll mouse buttons
|
||||
// - 'mouse_button_clicked' is generally carried into ActiveIdMouseButton when setting ActiveId.
|
||||
// - Technically we only need some values in one code path, but since this is gated by hovered test this is fine.
|
||||
int mouse_button_clicked = -1;
|
||||
int mouse_button_released = -1;
|
||||
for (int button = 0; button < 3; button++)
|
||||
if (flags & (ImGuiButtonFlags_MouseButtonLeft << button)) // Handle ImGuiButtonFlags_MouseButtonRight and ImGuiButtonFlags_MouseButtonMiddle here.
|
||||
{
|
||||
if (ImGui::IsMouseClicked(button, test_owner_id) && mouse_button_clicked == -1)
|
||||
{
|
||||
mouse_button_clicked = button;
|
||||
}
|
||||
if (ImGui::IsMouseReleased(button, test_owner_id) && mouse_button_released == -1)
|
||||
{
|
||||
mouse_button_released = button;
|
||||
}
|
||||
}
|
||||
// Changed wrt original imgui: only 1 button is present
|
||||
bool mouse_button_clicked = ImGui::IsMouseClicked(0, ImGuiInputFlags_None, id);
|
||||
|
||||
// Process initial action
|
||||
if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt))
|
||||
if (mouse_button_clicked && active_id != id)
|
||||
{
|
||||
if (mouse_button_clicked != -1 && g.ActiveId != id)
|
||||
{
|
||||
if (!(flags & ImGuiButtonFlags_NoSetKeyOwner))
|
||||
ImGui::SetKeyOwner(ImGui::MouseButtonToKey(mouse_button_clicked), id);
|
||||
if (flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere))
|
||||
{
|
||||
// ImGui::SetActiveID(id, window);
|
||||
g.ActiveIdMouseButton = mouse_button_clicked;
|
||||
if (!(flags & ImGuiButtonFlags_NoNavFocus))
|
||||
ImGui::SetFocusID(id, window);
|
||||
ImGui::FocusWindow(window);
|
||||
}
|
||||
if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseClickedCount[mouse_button_clicked] == 2))
|
||||
{
|
||||
pressed = true;
|
||||
// if (flags & ImGuiButtonFlags_NoHoldingActiveId)
|
||||
// ImGui::ClearActiveID();
|
||||
// else
|
||||
// ImGui::SetActiveID(id, window); // Hold on ID
|
||||
if (!(flags & ImGuiButtonFlags_NoNavFocus))
|
||||
ImGui::SetFocusID(id, window);
|
||||
g.ActiveIdMouseButton = mouse_button_clicked;
|
||||
ImGui::FocusWindow(window);
|
||||
}
|
||||
}
|
||||
if (flags & ImGuiButtonFlags_PressedOnRelease)
|
||||
{
|
||||
if (mouse_button_released != -1)
|
||||
{
|
||||
const bool has_repeated_at_least_once = (item_flags & ImGuiItemFlags_ButtonRepeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; // Repeat mode trumps on release behavior
|
||||
if (!has_repeated_at_least_once)
|
||||
pressed = true;
|
||||
if (!(flags & ImGuiButtonFlags_NoNavFocus))
|
||||
ImGui::SetFocusID(id, window);
|
||||
// ImGui::ClearActiveID();
|
||||
}
|
||||
}
|
||||
active_id = id;
|
||||
held_duration = 0;
|
||||
|
||||
// 'Repeat' mode acts when held regardless of _PressedOn flags (see table above).
|
||||
// Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings.
|
||||
if (g.ActiveId == id && (item_flags & ImGuiItemFlags_ButtonRepeat))
|
||||
if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && ImGui::IsMouseClicked(g.ActiveIdMouseButton, test_owner_id, ImGuiInputFlags_Repeat))
|
||||
pressed = true;
|
||||
if (flags & ImGuiButtonFlags_PressedOnClick)
|
||||
pressed = true;
|
||||
}
|
||||
|
||||
if (pressed)
|
||||
g.NavDisableHighlight = true;
|
||||
}
|
||||
|
||||
// Process while held
|
||||
bool held = false;
|
||||
if (g.ActiveId == id)
|
||||
if (active_id == id)
|
||||
{
|
||||
if (g.ActiveIdSource == ImGuiInputSource_Mouse)
|
||||
if (ImGui::IsMouseDown(0, id))
|
||||
{
|
||||
if (g.ActiveIdIsJustActivated)
|
||||
g.ActiveIdClickOffset = g.IO.MousePos - bb.Min;
|
||||
held = true;
|
||||
|
||||
const int mouse_button = g.ActiveIdMouseButton;
|
||||
if (mouse_button == -1)
|
||||
float prev_held_duration = held_duration;
|
||||
held_duration += g.IO.DeltaTime;
|
||||
if (flags & ImGuiButtonFlags_Repeat and held_duration > g.IO.KeyRepeatDelay)
|
||||
{
|
||||
// Fallback for the rare situation were g.ActiveId was set programmatically or from another widget (e.g. #6304).
|
||||
// ImGui::ClearActiveID();
|
||||
int n1 = (prev_held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate;
|
||||
int n2 = (held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate;
|
||||
|
||||
if (n2 != n1)
|
||||
pressed = true;
|
||||
}
|
||||
else if (ImGui::IsMouseDown(mouse_button, test_owner_id))
|
||||
{
|
||||
held = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
bool release_in = hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease) != 0;
|
||||
bool release_anywhere = (flags & ImGuiButtonFlags_PressedOnClickReleaseAnywhere) != 0;
|
||||
if ((release_in || release_anywhere) && !g.DragDropActive)
|
||||
{
|
||||
// Report as pressed when releasing the mouse (this is the most common path)
|
||||
bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseReleased[mouse_button] && g.IO.MouseClickedLastCount[mouse_button] == 2;
|
||||
bool is_repeating_already = (item_flags & ImGuiItemFlags_ButtonRepeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps <on release>
|
||||
bool is_button_avail_or_owned = ImGui::TestKeyOwner(ImGui::MouseButtonToKey(mouse_button), test_owner_id);
|
||||
if (!is_double_click_release && !is_repeating_already && is_button_avail_or_owned)
|
||||
pressed = true;
|
||||
}
|
||||
// ImGui::ClearActiveID();
|
||||
}
|
||||
if (!(flags & ImGuiButtonFlags_NoNavFocus))
|
||||
g.NavDisableHighlight = true;
|
||||
}
|
||||
else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad)
|
||||
else
|
||||
{
|
||||
// When activated using Nav, we hold on the ActiveID until activation button is released
|
||||
// if (g.NavActivateDownId != id)
|
||||
// ImGui::ClearActiveID();
|
||||
if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease))
|
||||
pressed = true;
|
||||
|
||||
active_id = 0;
|
||||
}
|
||||
if (pressed)
|
||||
g.ActiveIdHasBeenPressedBefore = true;
|
||||
}
|
||||
|
||||
if (out_hovered)
|
||||
*out_hovered = hovered;
|
||||
|
||||
if (out_held)
|
||||
*out_held = held;
|
||||
|
||||
return pressed;
|
||||
}
|
||||
|
||||
static void draw_single_key(const key & k, int key_id, ImVec2 size_arg)
|
||||
bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered)
|
||||
{
|
||||
std::string label = k.characters == U"" ? "" : to_utf8(k.characters[0]);
|
||||
label += "##virtual_keyboard_key_" + std::to_string(key_id);
|
||||
std::string key_label;
|
||||
|
||||
// ImGui::Button(label.c_str(), size);
|
||||
bool is_shift = k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift;
|
||||
|
||||
int flags = (int)ImGuiButtonFlags_PressedOnClickRelease | (int)ImGuiButtonFlags_MouseButtonLeft | (int)ImGuiButtonFlags_NoNavFocus;
|
||||
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]));
|
||||
}
|
||||
|
||||
std::string key_label_id;
|
||||
if (k.characters.empty())
|
||||
key_label_id = "virtual_keyboard_key_" + std::to_string(k.key);
|
||||
else
|
||||
key_label_id = "virtual_keyboard_" + to_utf8(k.characters[0]);
|
||||
|
||||
ImGuiWindow * window = ImGui::GetCurrentWindow();
|
||||
if (window->SkipItems)
|
||||
return /*false*/;
|
||||
return false;
|
||||
|
||||
ImGuiContext & g = *GImGui;
|
||||
const ImGuiStyle & style = g.Style;
|
||||
const ImGuiID id = window->GetID(label.c_str());
|
||||
const ImVec2 label_size = ImGui::CalcTextSize(label.c_str(), NULL, true);
|
||||
const ImGuiID id = window->GetID(key_label_id.c_str());
|
||||
const ImVec2 label_size = ImGui::CalcTextSize(key_label.c_str(), key_label.c_str() + key_label.size(), false /* Don't hide text after # */);
|
||||
|
||||
ImVec2 pos = window->DC.CursorPos;
|
||||
if ((flags & ImGuiButtonFlags_AlignTextBaseLine) && style.FramePadding.y < window->DC.CurrLineTextBaseOffset) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag)
|
||||
pos.y += window->DC.CurrLineTextBaseOffset - style.FramePadding.y;
|
||||
ImVec2 size = ImGui::CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f);
|
||||
|
||||
const ImRect bb(pos, pos + size);
|
||||
ImGui::ItemSize(size, style.FramePadding.y);
|
||||
if (!ImGui::ItemAdd(bb, id))
|
||||
return /*false*/;
|
||||
return false;
|
||||
|
||||
bool hovered, held;
|
||||
[[maybe_unused]] bool pressed = button_behavior(bb, id, &hovered, &held, flags);
|
||||
ImGuiButtonFlags flags = ImGuiButtonFlags_None;
|
||||
|
||||
if (k.flag & key_flag_repeat)
|
||||
flags |= ImGuiButtonFlags_Repeat;
|
||||
|
||||
bool held;
|
||||
bool pressed = button_behavior(bb, id, &hovered, &held, flags);
|
||||
|
||||
// Render
|
||||
const ImU32 col = ImGui::GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered
|
||||
: ImGuiCol_Button);
|
||||
bool active = (held and 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);
|
||||
ImGui::RenderNavHighlight(bb, id);
|
||||
ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
|
||||
|
||||
// if (g.LogEnabled)
|
||||
// LogSetNextTextDecoration("[", "]");
|
||||
ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label.c_str(), NULL, &label_size, style.ButtonTextAlign, &bb);
|
||||
// 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,
|
||||
bb.Max - style.FramePadding,
|
||||
key_label.c_str(),
|
||||
key_label.c_str() + key_label.size(),
|
||||
&label_size,
|
||||
style.ButtonTextAlign,
|
||||
&bb);
|
||||
|
||||
// Automatically close popups
|
||||
// if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
|
||||
// CloseCurrentPopup();
|
||||
if (pressed)
|
||||
{
|
||||
press_single_key(k);
|
||||
}
|
||||
|
||||
// IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags);
|
||||
// return pressed;
|
||||
return pressed;
|
||||
}
|
||||
|
||||
void scenes::lobby::gui_keyboard(ImVec2 size)
|
||||
void virtual_keyboard::press_single_key(const key & k)
|
||||
{
|
||||
const keyboard_layout & layout = azerty;
|
||||
if (k.key == ImGuiKey_LeftShift or k.key == 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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
int nb_rows = layout.size();
|
||||
case case_mode::upper:
|
||||
current_case_mode = case_mode::lower;
|
||||
[[fallthrough]];
|
||||
|
||||
ImVec2 position = ImGui::GetCursorPos();
|
||||
case case_mode::caps_lock:
|
||||
ImGui::GetIO().AddInputCharactersUTF8(to_utf8(towupper(k.characters[0])).c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void virtual_keyboard::display(ImGuiID & hovered_id)
|
||||
{
|
||||
ImGuiStyle & style = ImGui::GetStyle();
|
||||
|
||||
std::vector<float> keys_width;
|
||||
ImGuiInputTextState * input_state = ImGui::GetInputTextState(ImGui::GetCurrentContext()->ActiveId);
|
||||
bool want_digits = input_state and (input_state->Flags & ImGuiInputTextFlags_CharsDecimal) != 0;
|
||||
|
||||
// Size of a key including margin, scaled by the key width later
|
||||
ImVec2 key_size{FLT_MAX, (size.y + style.FramePadding.y) / nb_rows};
|
||||
auto & layout = want_digits ? digits : symbols_shown ? symbols
|
||||
: *layouts[current_layout];
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8});
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 16);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {8, 8});
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 8);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32(8, 8, 8, 224));
|
||||
if (want_digits)
|
||||
ImGui::SetNextWindowSize({350, 400});
|
||||
else
|
||||
ImGui::SetNextWindowSize({1400, 400});
|
||||
|
||||
ImGui::Begin("VirtualKeyboard", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoFocusOnClick);
|
||||
|
||||
ImVec2 size = ImGui::GetWindowSize();
|
||||
ImVec2 padding = ImGui::GetStyle().WindowPadding;
|
||||
size = {size.x - 2 * padding.x, size.y - 2 * padding.y};
|
||||
|
||||
// Compute keys size;
|
||||
float key_height = [&]() {
|
||||
int N = layout.size();
|
||||
|
||||
// size.y == N * key_height + (N - 1) * ItemSpacing.y
|
||||
return (size.y - (N - 1) * style.ItemSpacing.y) / N;
|
||||
}();
|
||||
|
||||
float base_key_width = std::numeric_limits<float>::max();
|
||||
for (const auto & row: layout)
|
||||
{
|
||||
// key.width is scaled by base_key_width to get the real width of the key:
|
||||
// key_width = base_key_width * key.width - ItemSpacing.x
|
||||
|
||||
// size.x == sum(key_width) + (N - 1) * ItemSpacing.x
|
||||
// == base_key_width * sum(key.width) - ItemSpacing.x
|
||||
|
||||
float total_width = 0;
|
||||
for (auto & key: row)
|
||||
total_width += key.width;
|
||||
keys_width.push_back(total_width);
|
||||
|
||||
float key_width = (size.x + style.FramePadding.x) / total_width;
|
||||
key_size.x = std::min(key_size.x, key_width);
|
||||
base_key_width = std::min(base_key_width, (size.x + style.ItemSpacing.x) / total_width);
|
||||
}
|
||||
|
||||
ImVec2 row_position = ImGui::GetCursorPos();
|
||||
int id = 0;
|
||||
for (const auto & [row, width]: std::views::zip(layout, keys_width))
|
||||
|
||||
for (const auto & row: layout)
|
||||
{
|
||||
ImVec2 key_position = position;
|
||||
ImVec2 key_position = row_position;
|
||||
|
||||
// Align right
|
||||
key_position.x += size.x + style.ItemSpacing.x;
|
||||
for (auto & key: row)
|
||||
key_position.x -= base_key_width * key.width;
|
||||
|
||||
for (const auto & key: row)
|
||||
{
|
||||
if (key.visible)
|
||||
if (!(key.flag & key_flag_hidden))
|
||||
{
|
||||
ImGui::SetCursorPos(key_position);
|
||||
|
||||
draw_single_key(key, id++, ImVec2{key_size.x * key.width - style.FramePadding.x, key_size.y - style.FramePadding.y});
|
||||
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)
|
||||
hovered_id = ImGui::GetItemID();
|
||||
}
|
||||
|
||||
key_position.x += key_size.x * key.width;
|
||||
key_position.x += base_key_width * key.width;
|
||||
}
|
||||
|
||||
position.y += key_size.y;
|
||||
row_position.y += key_height + style.ItemSpacing.y;
|
||||
}
|
||||
|
||||
ImGui::SetCursorPos(position);
|
||||
row_position.y -= style.ItemSpacing.y;
|
||||
|
||||
ImGui::SetCursorPos(row_position);
|
||||
|
||||
if (ImGui::IsWindowHovered())
|
||||
{
|
||||
ImGui::GetIO().MouseDown[0] = false;
|
||||
ImGui::GetIO().MouseClicked[0] = false;
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
ImGui::PopStyleColor(); // ImGuiCol_WindowBg
|
||||
ImGui::PopStyleVar(4); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_WindowRounding, ImGuiStyleVar_ItemSpacing, ImGuiStyleVar_FrameRounding
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "imgui.h"
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
struct ImRect;
|
||||
|
||||
class virtual_keyboard
|
||||
{
|
||||
public:
|
||||
enum key_flags
|
||||
{
|
||||
key_flag_none = 0,
|
||||
key_flag_hidden = 1 << 0,
|
||||
key_flag_repeat = 1 << 1,
|
||||
};
|
||||
|
||||
struct key
|
||||
{
|
||||
float width;
|
||||
const std::u16string_view characters;
|
||||
ImGuiKey key = ImGuiKey_None;
|
||||
const char * glyph = nullptr;
|
||||
key_flags flag = key_flag_none;
|
||||
};
|
||||
|
||||
using layout = std::vector<std::vector<key>>;
|
||||
|
||||
enum class case_mode
|
||||
{
|
||||
lower,
|
||||
upper,
|
||||
caps_lock
|
||||
};
|
||||
|
||||
private:
|
||||
bool button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags = 0);
|
||||
bool draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered);
|
||||
void press_single_key(const key & k);
|
||||
|
||||
ImGuiID active_id = 0;
|
||||
float held_duration = 0;
|
||||
|
||||
case_mode current_case_mode = case_mode::lower;
|
||||
int current_layout = 0;
|
||||
bool symbols_shown = false;
|
||||
|
||||
public:
|
||||
void display(ImGuiID & hovered);
|
||||
};
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
if [ -d ".git" ] ; then
|
||||
git config user.email > /dev/null || git config user.email "git-am@invalid"
|
||||
git config user.name > /dev/null || git config user.name "no name"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
From ebf48c25bdd98cbc7bb0ec01ca19539b7a841b0d Mon Sep 17 00:00:00 2001
|
||||
From 748060ccb60bdc3df47dc44f1b1dcb57dfd5b488 Mon Sep 17 00:00:00 2001
|
||||
From: Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
Date: Wed, 16 Oct 2024 20:51:52 +0200
|
||||
Subject: [PATCH] Change ImGui::ButtonBehavior default flags when using hand
|
||||
tracking
|
||||
Subject: [PATCH 1/2] Change ImGui::ButtonBehavior default flags when using
|
||||
hand tracking
|
||||
|
||||
---
|
||||
imgui.cpp | 2 +-
|
||||
@@ -12,7 +12,7 @@ Subject: [PATCH] Change ImGui::ButtonBehavior default flags when using hand
|
||||
4 files changed, 14 insertions(+), 2 deletions(-)
|
||||
|
||||
diff --git a/imgui.cpp b/imgui.cpp
|
||||
index 50c8d362e..7d67538dc 100644
|
||||
index 3bbaf018f..44eed7900 100644
|
||||
--- a/imgui.cpp
|
||||
+++ b/imgui.cpp
|
||||
@@ -9969,7 +9969,7 @@ static const char* GetInputSourceName(ImGuiInputSource source)
|
||||
@@ -25,7 +25,7 @@ index 50c8d362e..7d67538dc 100644
|
||||
return mouse_source_names[source];
|
||||
}
|
||||
diff --git a/imgui.h b/imgui.h
|
||||
index dc6ac6e59..1b31e56b4 100644
|
||||
index 77ca8e04f..647090b9e 100644
|
||||
--- a/imgui.h
|
||||
+++ b/imgui.h
|
||||
@@ -1838,6 +1838,8 @@ enum ImGuiMouseSource : int
|
||||
@@ -38,7 +38,7 @@ index dc6ac6e59..1b31e56b4 100644
|
||||
};
|
||||
|
||||
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
|
||||
index 7ed778b57..580a662ef 100644
|
||||
index 8df1755fb..69f200218 100644
|
||||
--- a/imgui_demo.cpp
|
||||
+++ b/imgui_demo.cpp
|
||||
@@ -7350,6 +7350,11 @@ static void ShowDemoWindowInputs()
|
||||
@@ -54,7 +54,7 @@ index 7ed778b57..580a662ef 100644
|
||||
// displaying the data for old/new backends.
|
||||
// User code should never have to go through such hoops!
|
||||
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
|
||||
index 837d1c64d..7df2429fc 100644
|
||||
index 322c0846c..26a8ffdcd 100644
|
||||
--- a/imgui_widgets.cpp
|
||||
+++ b/imgui_widgets.cpp
|
||||
@@ -501,7 +501,12 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
From 265d4ef46317d87b8d22926e7ee99fda6e2b940d Mon Sep 17 00:00:00 2001
|
||||
From: Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
Date: Tue, 22 Oct 2024 15:09:14 +0200
|
||||
Subject: [PATCH 2/2] Add ImGuiWindowFlags_NoFocusOnClick flag
|
||||
|
||||
---
|
||||
imgui.cpp | 3 +++
|
||||
imgui.h | 1 +
|
||||
2 files changed, 4 insertions(+)
|
||||
|
||||
diff --git a/imgui.cpp b/imgui.cpp
|
||||
index 44eed7900..46f3ea877 100644
|
||||
--- a/imgui.cpp
|
||||
+++ b/imgui.cpp
|
||||
@@ -4879,6 +4879,9 @@ void ImGui::UpdateMouseMovingWindowEndFrame()
|
||||
// Handle the edge case of a popup being closed while clicking in its empty space.
|
||||
// If we try to focus it, FocusWindow() > ClosePopupsOverWindow() will accidentally close any parent popups because they are not linked together any more.
|
||||
ImGuiWindow* root_window = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL;
|
||||
+ if (root_window && (root_window->Flags & ImGuiWindowFlags_NoFocusOnClick))
|
||||
+ return;
|
||||
+
|
||||
const bool is_closed_popup = root_window && (root_window->Flags & ImGuiWindowFlags_Popup) && !IsPopupOpen(root_window->PopupId, ImGuiPopupFlags_AnyPopupLevel);
|
||||
|
||||
if (root_window != NULL && !is_closed_popup)
|
||||
diff --git a/imgui.h b/imgui.h
|
||||
index 647090b9e..e9e453195 100644
|
||||
--- a/imgui.h
|
||||
+++ b/imgui.h
|
||||
@@ -1079,6 +1079,7 @@ enum ImGuiWindowFlags_
|
||||
ImGuiWindowFlags_NoNavInputs = 1 << 16, // No gamepad/keyboard navigation within the window
|
||||
ImGuiWindowFlags_NoNavFocus = 1 << 17, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB)
|
||||
ImGuiWindowFlags_UnsavedDocument = 1 << 18, // Display a dot next to the title. When used in a tab/docking context, tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar.
|
||||
+ ImGuiWindowFlags_NoFocusOnClick = 1 << 19, // Disable taking focus when clicked
|
||||
ImGuiWindowFlags_NoNav = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus,
|
||||
ImGuiWindowFlags_NoDecoration = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse,
|
||||
ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus,
|
||||
--
|
||||
2.47.0
|
||||
|
||||
Reference in New Issue
Block a user