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 * Like every TouchDriver, isTouched() and getPoint() do not implicitly loop
21 * or block. isTouched() is where the SDL event queue is drained (via
22 * SDL_PollEvent), so - as with the other drivers in this file - call it
23 * exactly once per loop() iteration.
24 */
26 public:
27 TouchDriverSDL() = default;
28
29 bool begin() override {
30 SDL_InitSubSystem(SDL_INIT_VIDEO);
31 return true;
32 }
33
34 bool isTouched() override {
35 SDL_Event event;
36 while (SDL_PollEvent(&event)) {
37 if (event.type == SDL_QUIT) {
38 exit(0);
39 }
40 }
41
42 int x = 0;
43 int y = 0;
44 const uint32_t buttons = SDL_GetMouseState(&x, &y);
45 lastX_ = static_cast<int16_t>(x);
46 lastY_ = static_cast<int16_t>(y);
47 down_ = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
48 return down_;
49 }
50
51 bool getPoint(Point& outPoint) override {
52 if (!down_) return false;
53 outPoint = mapCoordinates(lastX_, lastY_, 255);
54 return true;
55 }
56
57 private:
58 int16_t lastX_ = 0;
59 int16_t lastY_ = 0;
60 bool down_ = false;
61};
62
63} // namespace tinygpu
Maps the desktop mouse to a TouchDriver, for exercising touch-driven UI (buttons, sliders,...
Definition: TouchDriverSDL.h:25
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverSDL.h:34
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverSDL.h:51
bool begin() override
Initialize the controller.
Definition: TouchDriverSDL.h:29
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