TinyGPU
Loading...
Searching...
No Matches
TouchDriverSDL.h
Go to the documentation of this file.
1#pragma once
2#include <SDL.h>
3
4#include <cstdint>
5#include <cstdlib>
6
7#include "TinyGPU/Input/TouchDriverCommon.h"
8
9namespace tinygpu {
10
11/**
12 * @brief Maps the desktop mouse to a TouchDriver, for exercising touch-driven
13 * UI (buttons, sliders, ...) on the SDL desktop backend without any physical
14 * touch hardware.
15 *
16 * The left mouse button stands in for a finger: button down = touched,
17 * cursor position = touch point. SDL_QUIT (closing the window) exits the
18 * process, since there is no hardware equivalent to hand back to the sketch.
19 *
20 * When the window is larger than the display (DisplayDriverSDL::setScale(),
21 * a resized window), the mouse position is mapped back to display pixels.
22 *
23 * Like every TouchDriver, isTouched() and getPoint() do not implicitly loop
24 * or block. isTouched() is where the SDL event queue is drained (via
25 * SDL_PollEvent), so - as with the other drivers in this file - call it
26 * exactly once per loop() iteration.
27 */
29 public:
30 TouchDriverSDL() = default;
31
32 bool begin() override {
33 SDL_InitSubSystem(SDL_INIT_VIDEO);
34 return true;
35 }
36
37 bool isTouched() override {
38 SDL_Event event;
39 while (SDL_PollEvent(&event)) {
40 if (event.type == SDL_QUIT) {
41 exit(0);
42 }
43 }
44
45 int x = 0;
46 int y = 0;
47 const uint32_t buttons = SDL_GetMouseState(&x, &y);
49 lastX_ = static_cast<int16_t>(x);
50 lastY_ = static_cast<int16_t>(y);
51 down_ = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
52 return down_;
53 }
54
55 bool getPoint(Point& outPoint) override {
56 if (!down_) return false;
57 outPoint = mapCoordinates(lastX_, lastY_, 255);
58 return true;
59 }
60
61 /// Window coordinates -> display coordinates (the renderer of the window
62 /// under the mouse has the display size as logical size, see
63 /// DisplayDriverSDL); positions outside the display are clamped.
64 static void windowToDisplay(int& x, int& y) {
65#if SDL_VERSION_ATLEAST(2, 0, 18)
66 SDL_Window* window = SDL_GetMouseFocus();
67 SDL_Renderer* renderer = window != nullptr ? SDL_GetRenderer(window) : nullptr;
68 if (renderer == nullptr) return;
69 int w = 0, h = 0;
70 SDL_RenderGetLogicalSize(renderer, &w, &h);
71 if (w <= 0 || h <= 0) return;
72 float lx = 0, ly = 0;
73 SDL_RenderWindowToLogical(renderer, x, y, &lx, &ly);
74 x = lx < 0 ? 0 : lx >= w ? w - 1 : static_cast<int>(lx);
75 y = ly < 0 ? 0 : ly >= h ? h - 1 : static_cast<int>(ly);
76#else
77 (void)x;
78 (void)y;
79#endif
80 }
81
82 private:
83 int16_t lastX_ = 0;
84 int16_t lastY_ = 0;
85 bool down_ = false;
86};
87
88} // namespace tinygpu
Maps the desktop mouse to a TouchDriver, for exercising touch-driven UI (buttons, sliders,...
Definition: TouchDriverSDL.h:28
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverSDL.h:37
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverSDL.h:55
static void windowToDisplay(int &x, int &y)
Definition: TouchDriverSDL.h:64
bool begin() override
Initialize the controller.
Definition: TouchDriverSDL.h:32
Base class for touch controllers.
Definition: TouchDriverCommon.h:72
Point mapCoordinates(int16_t rawX, int16_t rawY, uint16_t pressure=255) const
Convert raw controller coordinates into logical display space.
Definition: TouchDriverCommon.h:208
Definition: DSIBusESP32.h:19
Touch point.
Definition: TouchDriverCommon.h:32