The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.4KB

  1. /**
  2. @page getting_touch_events Getting touch events
  3. Touch events are communicated from BLOCKS devices to your application code via TouchSurface objects.
  4. You can obtain a pointer to the %TouchSurface associated with a specific BLOCKS device from its corresponding Block object using the Block::getTouchSurface() method --- see the @ref discovering_blocks page for an example of how to obtain %Block objects.
  5. For devices without a touch surface (such as the Control %Block) this method will return @s_code{nullptr}, but if the device is capable of sending touch events then the pointer to the %TouchSurface will be valid for the lifetime of the %Block object.
  6. Once you have a %TouchSurface you must register as a TouchSurface::Listener to get touch events.
  7. The process for doing this is to have one of your application's classes inherit from %TouchSurface::Listener and override the pure virtual method TouchSurface::Listener::touchChanged().
  8. Then, when you register your derived class as a listener to a particular %TouchSurface, your overriden method will be called when the corresponding device is touched.
  9. A safe way of registering a class derived from %TouchSurface::Listener with a %TouchSurface is as follows.
  10. @code{.cpp}
  11. class TouchSurfaceListenerExample : public TouchSurface::Listener
  12. {
  13. public:
  14. TouchSurfaceListenerExample (Block& block)
  15. {
  16. if (auto touchSurface = block.getTouchSurface())
  17. touchSurface->addListener (this);
  18. }
  19. void touchChanged (TouchSurface& sourceTouchSurface, const TouchSurface::Touch& touchEvent) override
  20. {
  21. // Do something with touchEvent here!
  22. }
  23. };
  24. @endcode
  25. When your overriden @s_projcode{touchChanged()} method is called you have access to two parameters: a reference to the %TouchSurface that generated this event and a reference to a TouchSurface::Touch.
  26. The %TouchSurface::Touch class contains member variables describing the position, pressure, velocity, timestamp and more.
  27. @section getting_touch_events_example_usage Example usage
  28. To add this functionality to the BlockFinder example project, add %TouchSurface::Listener as a base class to the BlockFinder class and override the @s_projcode{touchChanged()} function as follows:
  29. @code{.cpp}
  30. class BlockFinder : private TopologySource::Listener,
  31. private TouchSurface::Listener
  32. {
  33. //...
  34. private:
  35. //...
  36. void touchChanged (TouchSurface& sourceTouchSurface, const TouchSurface::Touch& touchEvent) override
  37. {
  38. Logger::writeToLog ("Touch Changed!");
  39. }
  40. //...
  41. };
  42. @endcode
  43. Then in the @s_projcode{topologyChanged()} callback, add the BlockFinder class as a listener to the connected Blocks in the current topology to receive touch change callbacks as shown below:
  44. @code{.cpp}
  45. void topologyChanged() override
  46. {
  47. //...
  48. for (auto& block : currentTopology.blocks)
  49. {
  50. //...
  51. if (auto touchSurface = block->getTouchSurface())
  52. touchSurface->addListener (this);
  53. }
  54. }
  55. @endcode
  56. If you run the application now and connect a Lightpad, you should see text in the logger whenever the surface is touched.
  57. You can also find multiple examples of using TouchSurfaces in the @ref example_applications pages.
  58. Learn more about other Block methods from the following pages:
  59. @ref getting_control_button_events
  60. @ref controlling_led_grids
  61. @ref controlling_led_strips
  62. @ref controlling_control_buttons
  63. */