Skip to content

Commit

Permalink
refactored touch handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RalphSteinhagen committed Aug 24, 2023
1 parent e54571a commit 1dd6be1
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 72 deletions.
6 changes: 5 additions & 1 deletion src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(sources
set(GNURADIO_PREFIX "/usr/" CACHE FILEPATH "Prefix of the GNURadio installation")

add_subdirectory(app_header)
add_subdirectory(utils)

if (EMSCRIPTEN)
message(STATUS "Detected emscripten webassembly build")
Expand Down Expand Up @@ -90,6 +91,7 @@ if (EMSCRIPTEN)
implot
plf_colony
app_header
utils
ui_assets
sample_dashboards
fonts
Expand All @@ -107,7 +109,8 @@ if (EMSCRIPTEN)
else () # native build

set(target_name "opendigitizer-ui")
add_executable(${target_name})
add_executable(${target_name}
utils/TouchHandler.hpp)

target_sources(${target_name} PRIVATE ${sources})

Expand All @@ -123,6 +126,7 @@ else () # native build
core
client
app_header
utils
ui_assets
sample_dashboards
fonts
Expand Down
10 changes: 5 additions & 5 deletions src/ui/cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ FetchContent_Declare(
if (EMSCRIPTEN)
FetchContent_MakeAvailable(imgui implot imgui-node-editor yaml-cpp stb opencmw-cpp plf_colony function2)
else () # native build
# FetchContent_Declare(
# sdl2
# GIT_REPOSITORY "https://github.com/libsdl-org/SDL"
# GIT_TAG release-2.24.2
# )
FetchContent_Declare(
sdl2
GIT_REPOSITORY "https://github.com/libsdl-org/SDL"
GIT_TAG release-2.28.2
)
FetchContent_MakeAvailable(imgui implot imgui-node-editor yaml-cpp plf_colony stb opencmw-cpp function2)
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED COMPONENTS OpenGL)
Expand Down
6 changes: 4 additions & 2 deletions src/ui/dashboardpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "flowgraph/datasink.h"
#include "imguiutils.h"

#include "utils/TouchHandler.hpp"

namespace DigitizerUi {

namespace {
Expand Down Expand Up @@ -470,7 +472,7 @@ void DashboardPage::drawPlots(App *app, DigitizerUi::DashboardPage::Mode mode, D

ImPlot::PushStyleVar(ImPlotStyleVar_PlotPadding, ImVec2{ 0, 0 }); // TODO: make this perhaps a global style setting via ImPlot::GetStyle()
ImPlot::PushStyleVar(ImPlotStyleVar_LabelPadding, ImVec2{ 3, 1 });
if (ImPlot::BeginPlot(plot.name.c_str(), plotSize - ImVec2(2 * offset, 2 * offset), plotFlags)) {
if (fair::TouchHandler<>::BeginZoomablePlot(plot.name, plotSize - ImVec2(2 * offset, 2 * offset), plotFlags)) {
drawPlot(plot);

// allow the main plot area to be a DND target
Expand Down Expand Up @@ -517,7 +519,7 @@ void DashboardPage::drawPlots(App *app, DigitizerUi::DashboardPage::Mode mode, D
}
}

ImPlot::EndPlot();
fair::TouchHandler<>::EndZoomablePlot();
ImPlot::PopStyleVar(2);

if (mode == Mode::Layout) {
Expand Down
6 changes: 6 additions & 0 deletions src/ui/flowgraphitem.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#pragma once

#ifndef IMPLOT_POINT_CLASS_EXTRA
#define IMGUI_DEFINE_MATH_OPERATORS true
#endif

#include <functional>
#include <imgui.h>
#include <imgui_internal.h>
#include <span>
#include <vector>

Expand Down
12 changes: 8 additions & 4 deletions src/ui/imguiutils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifndef IMPLOT_POINT_CLASS_EXTRA
#define IMGUI_DEFINE_MATH_OPERATORS true
#endif

#include <any>
#include <cctype>
#include <charconv>
Expand Down Expand Up @@ -211,7 +215,7 @@ class InputKeypad {

public:
template<typename EdTy>
requires std::integral<EdTy> || std::floating_point<EdTy> || std::same_as<std::string, EdTy>
requires std::integral<EdTy> || std::floating_point<EdTy> || std::same_as<std::string, EdTy>
[[nodiscard]] static bool edit(const char *label, EdTy *value) {
if (!label || !value) {
return false;
Expand Down Expand Up @@ -1182,13 +1186,13 @@ void drawBlockControlsPanel(BlockControlsPanel &ctx, const ImVec2 &pos, const Im

auto listSize = verticalLayout ? ImVec2(size.x, 200) : ImVec2(200, size.y - ImGui::GetFrameHeightWithSpacing());
auto ret = filteredListBox(
"blocks", BlockType::registry().types(), [](auto &it) -> std::pair<BlockType *, std::string> {
"blocks", BlockType::registry().types(), [](auto &it) -> std::pair<BlockType *, std::string> {
if (it.second->inputs.size() != 1 || it.second->outputs.size() != 1) {
return {};
}
return std::pair{ it.second.get(), it.first };
},
listSize);
},
listSize);

{
DisabledGuard dg(!ret.has_value());
Expand Down
27 changes: 11 additions & 16 deletions src/ui/imguiutils.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
#ifndef IMGUIUTILS_H
#define IMGUIUTILS_H

#ifndef IMPLOT_POINT_CLASS_EXTRA
#define IMGUI_DEFINE_MATH_OPERATORS true
#endif

#include <algorithm>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include <imgui.h>
#include <imgui_internal.h>
#include <misc/cpp/imgui_stdlib.h>

#include "flowgraph.h"

inline ImVec2 operator+(const ImVec2 a, const ImVec2 b) {
ImVec2 r = a;
r.x += b.x;
r.y += b.y;
return r;
}

inline ImVec2 operator-(const ImVec2 a, const ImVec2 b) {
ImVec2 r = a;
r.x -= b.x;
r.y -= b.y;
return r;
}

namespace DigitizerUi {
class Block;
class Dashboard;
Expand Down Expand Up @@ -198,15 +189,19 @@ std::optional<T> filteredListBox(const char *id, const ImVec2 &size, Items &&ite
}

template<typename Items, typename ItemGetter>
auto filteredListBox(const char *id, Items &&items, ItemGetter getItem, const ImVec2 &size = { 200, 200 }) requires std::is_invocable_v<ItemGetter, decltype(*items.begin())> {
auto filteredListBox(const char *id, Items &&items, ItemGetter getItem, const ImVec2 &size = { 200, 200 })
requires std::is_invocable_v<ItemGetter, decltype(*items.begin())>
{
using T = decltype(getItem(*items.begin()));
return filteredListBox<T>(id, size, items, getItem, [](auto &&item, bool selected) {
return ImGui::Selectable(item.second.data(), selected);
});
}

template<typename Items, typename ItemGetter, typename ItemDrawer>
auto filteredListBox(const char *id, Items &&items, ItemGetter getItem, ItemDrawer drawItem, const ImVec2 &size = { 200, 200 }) requires std::is_invocable_v<ItemGetter, decltype(*items.begin())> {
auto filteredListBox(const char *id, Items &&items, ItemGetter getItem, ItemDrawer drawItem, const ImVec2 &size = { 200, 200 })
requires std::is_invocable_v<ItemGetter, decltype(*items.begin())>
{
using T = decltype(getItem(*items.begin()));
return filteredListBox<T>(id, size, items, getItem, drawItem);
}
Expand Down
56 changes: 12 additions & 44 deletions src/ui/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifndef IMPLOT_POINT_CLASS_EXTRA
#define IMGUI_DEFINE_MATH_OPERATORS true
#endif

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
Expand Down Expand Up @@ -25,6 +29,7 @@
#include "flowgraph/datasource.h"
#include "flowgraph/fftblock.h"
#include "flowgraphitem.h"
#include "utils/TouchHandler.hpp"

CMRC_DECLARE(ui_assets);
CMRC_DECLARE(fonts);
Expand Down Expand Up @@ -183,7 +188,9 @@ int main(int argc, char **argv) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImPlot::CreateContext();
ImGuiIO &io = ImGui::GetIO();
ImGuiIO &io = ImGui::GetIO();
ImPlot::GetInputMap().Select = ImGuiPopupFlags_MouseButtonLeft;
ImPlot::GetInputMap().Pan = ImGuiPopupFlags_MouseButtonMiddle;

// For an Emscripten build we are disabling file-system access, so let's not
// attempt to do a fopen() of the imgui.ini file. You may manually call
Expand Down Expand Up @@ -287,8 +294,7 @@ static void main_loop(void *arg) {
app->fireCallbacks();

// Poll and handle events (inputs, window resize, etc.)
SDL_Event event;
static std::size_t nFingers = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
switch (event.type) {
Expand Down Expand Up @@ -324,41 +330,11 @@ static void main_loop(void *arg) {
break;
}
break;
case SDL_FINGERDOWN:
++nFingers;
if (!app->touchDiagnostics) {
break;
}
fmt::print("touch: finger down: {} fingerID: {} p:{} @({},{})\n", //
nFingers, event.tfinger.fingerId, event.tfinger.pressure, event.tfinger.x, event.tfinger.y);
break;
case SDL_FINGERUP:
--nFingers;
if (!app->touchDiagnostics) {
break;
}
fmt::print("touch: finger up: {} fingerID: {} p:{} @({},{})\n", //
nFingers, event.tfinger.fingerId, event.tfinger.pressure, event.tfinger.x, event.tfinger.y);
break;
case SDL_FINGERMOTION:
if (!app->touchDiagnostics) {
break;
}
fmt::print("touch: finger motion: {} fingerID: {} p:{} @({},{}) motion (dx,dy): ({}, {})\n", //
nFingers, event.tfinger.fingerId, event.tfinger.pressure, event.tfinger.x, event.tfinger.y, event.tfinger.dx, event.tfinger.dy);
break;
case SDL_MULTIGESTURE:
switch (event.mgesture.type) {
case SDL_MULTIGESTURE:
const auto &gesture = event.mgesture;
fmt::print("detected multi-gesture event -- touchId:{} numFingers: {} @({},{}) dDist:{} dTheta:{}\n", //
gesture.touchId, gesture.numFingers, gesture.x, gesture.y, gesture.dDist, gesture.dTheta);
break;
}
break;
}
fair::TouchHandler<>::processSDLEvent(event);
// Capture events here, based on io.WantCaptureMouse and io.WantCaptureKeyboard
}
fair::TouchHandler<>::updateLogic();

// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
Expand All @@ -369,14 +345,7 @@ static void main_loop(void *arg) {
int width, height;
SDL_GetWindowSize(app->sdlState->window, &width, &height);
ImGui::SetNextWindowSize({ float(width), float(height) });
if (nFingers >= 2) {
fmt::print("touch -- pressed two fingers emulating mouse button two\n");
ImGui::GetIO().MouseDown[0] = false;
ImGui::GetIO().MouseDown[1] = true;
} else {
ImGui::GetIO().MouseDown[1] = false;
}
// TODO: add gesture pinch, stretch and rotate event mappings here
fair::TouchHandler<>::applyToImGui();

ImGui::Begin("Main Window", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus);

Expand All @@ -387,7 +356,6 @@ static void main_loop(void *arg) {
ImGui::BeginDisabled();
}

auto pos = ImGui::GetCursorPos();
ImGuiID viewId = 0;
if (app->mainViewMode == "View" || app->mainViewMode == "") {
viewId = ImGui::GetID("");
Expand Down
5 changes: 5 additions & 0 deletions src/ui/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(
utils INTERFACE TouchHandler.hpp
)
target_include_directories(utils INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(utils INTERFACE fmt)
Loading

0 comments on commit 1dd6be1

Please sign in to comment.