lovyan03 / LovyanGFX

SPI LCD graphics library for ESP32 (ESP-IDF/ArduinoESP32) / ESP8266 (ArduinoESP8266) / SAMD51(Seeed ArduinoSAMD51)
Other
1.02k stars 187 forks source link

User function and plugins support for LGFX_Base and LGFX_Sprite #517

Closed tobozo closed 3 months ago

tobozo commented 3 months ago

This change adds the possibility to call LGFX object with a user function, it can make life easier when creating plugins that work on top of LovyanGFX.

Example:


void my_circle_function( LovyanGFX* dst, int32_t x, int32_t y, uint32_t radius,  uint32_t color1 , uint32_t color2 )
{
   dst->fillCircle( x, y, radius, color1 );
   dst->fillCircle( x, y, radius/2, color2 );
}

// optional macro to use tft.funCircle(...) without calling 'userFunc'
#define funCircle(...)  userFunc( my_circle_function, __VA_ARGS__);

void setup()
{
  tft.init();

  tft.userFunc( my_circle_function, 100, 100, 50, 0xff0000u, 0x00ffffu );
  // or if using the optional macro:
  tft.funCircle( 100, 100, 50, 0xff0000u, 0x00ffffu );
}
tobozo commented 3 months ago

Mixing C macros with cpp templates was a bad idea so I got rid of the macro and moved the template to a single location instead:

  /// @brief LovyanGFX class.
  /// that depend on the include order of the environment, such as file system, are inherited from LGFX_FILESYSTEM_Support.
  class LovyanGFX : public
  #ifdef LGFX_FILESYSTEM_SUPPORT_HPP_
      LGFX_FILESYSTEM_Support<
  #endif
       LGFXBase
  #ifdef LGFX_FILESYSTEM_SUPPORT_HPP_
      >
  #endif
  {
    public:
      template<typename Fun, typename ...Args>
      auto userFunc(Fun& fn, Args&&... arg) -> void{ fn(this, std::forward<Args>(arg)...); }
  };

I'll close this PR and work on a plugin example.