//
// NB! This is a file generated from the .4Dino file, changes will be lost
//     the next time the .4Dino file is built
//
#include "gfx4desp32_ESP32_P4_70CT.h"

gfx4desp32_ESP32_P4_70CT gfx = gfx4desp32_ESP32_P4_70CT();

#include "CanvasBuffersExampleConst.h"    // Note. This file will not be created if there are no generated graphics

// Uncomment one of these if using GC* in program flash, depending on whether you're using Workshop4 or Arduino
// #include "CanvasBuffersExampleGCx.h"    // Note. This file will be generated by Workshop4 when graphics destination program flash
// #include "CanvasBuffersExample_copy.h" // Note. This is a copy of the *GCx.h file for when using Arduino IDE directly
                                          // since WS4 may delete the file when changing to microSD
                                          // when using this, uncomment the appropriate gfx.Open4dGFX
// if this is commented out, and if using Arduino IDE, manually copy the GCJ file included in the project to the microSD

#include "canvas_bkg.h"                     // file containing binary jpeg image data used as background image 1600 x 556 pixels
#include "canvas_fg.h"                      // file containing binary jpeg image data used as foreground image 1600 x 556 pixels

/*******************************************************************************************************************************
* Simple example demonstrating basic CANVAS buffer use. The CANVAS buffer functions allows the use of larger than screen size  *
* frame buffers and then able to display it as a window on the display or to another frame buffer. The top area shows a simple *
* JPEG image that can be moved left or right very quickly. Workshop widgets can be placed on this frame buffer and will be     *
* detected by the gfx.imageTouched() function and can be used like a widget used on the main display buffer. The bottom area   *
* shows the blended canvas buffer which shares the same background image drawn to CANVAS_BUFFER1. In addition the alpha frame  *
* CANVAS_BUFFER_ARGB has an image with an entirely balack background. Both buffers are blended and layers rspond to touch left *
* or right with the background moving half the distance as the foreground. As with the CANVAS buffer, workshop widgets can be  *
* used with full touch capability. Drawing functions on the CANVAS_BUFFER_ARGB will be carried out in 24 bit color with alpha. *
* Workshop 4 widgets will be drawn from 16 bit source to 24 bit in the frame buffer. Care needs to be taken when creating      *
* buffers as they will eat into available PSRAM and may have a size restriction. Sizes up 6400 x 400 have been tested without  *
* issues.                                                                                                                      *
*******************************************************************************************************************************/

int canv_x, canv_y;
int canv_blend_x, canv_blend_y;

void setup()
{
  gfx.setJPEGoutputBufferDimension(1600, 556);  // because we are going to be using an image that may exceed the JPEG output buffer
                                                // we need to set it before calling gfx.begin as a default size will be chosen

  gfx.begin();
  gfx.Cls();
  gfx.ScrollEnable(false);
  gfx.BacklightOn(true);
  gfx.Orientation(PORTRAIT);
  gfx.SmoothScrollSpeed(5);
  gfx.TextColor(WHITE, WHITE); gfx.Font(2);  gfx.TextSize(1);
// Uncomment one of the three followin statements depending on the type of graphics file you are using GCI, GCJ, or GC* in program flash.
//gfx.Open4dGFX("CanvasBuffersExample"); // Opens DAT and GCI files for read using filename without extension.
gfx.Open4dGFX("CanvasBuffersExample.gcj"); // Opens GCJ file for read using complete filename.
//gfx.Open4dGFX(CanvasBuffersExample, CanvasBuffersExample_size); // Opens GCJ from program space using array name and size
//gfx.Open4dGFX(CanvasBuffersExample_dat, CanvasBuffersExample_dat_size, CanvasBuffersExample_gci, CanvasBuffersExample_gci_size); // Opens DAT/GCI from program space using array name and size
  gfx.touch_Set(TOUCH_ENABLE);                // Global touch enabled
  gfx.imageTouchEnable(iSlider1, true) ;                     // init_Slider1 enable touch of widget (on Form1)

  gfx.Cls(0x220C);                                                                // clear screan to blue

  gfx.RoundRectFilledAA(20, 40, 760, 560, 10, BLACK);                             // draw some panels
  gfx.RoundRectFilledAA(20, 680, 760, 560, 10, BLACK);
  gfx.RoundRectFlare(20, 40, 779, 599, 10, 15, 100, 0, FLARE_EXTERNAL, BLACK);
  gfx.RoundRectFlare(20, 680, 779, 1239, 10, 15, 100, 0, FLARE_EXTERNAL, BLACK);
  gfx.MoveTo(22, 3); gfx.print("Canvas Buffer");
  gfx.MoveTo(22, 643); gfx.print("Blend Canvas Buffer");

  gfx.SetCanvasDimensions(1600, 556);                                             // set canvas dimensions
  gfx.SetBlendCanvasDimensions(1600, 556);                                        // set blend canvas dimensions
  gfx.PPAblendTransAlpha(BLACK, true, 127, false);                                // set blend characteristics

  gfx.DrawToframebuffer(CANVAS_BUFFER);                                           // change to canvas buffer
  gfx.DrawJPEGarray(canvasBKG, canvasBKG_size, 0, 0, 1600, 556);                  // draw jpeg image from the binary in canvas_bkg.h. File was created with HXD
  gfx.imageSetWord(iSlider1, IMAGE_XPOS, 1050);                                   // Move slider widget off screen. Slider controls blend canvas buffer position.
  gfx.UserImages(iSlider1,0) ;                                                    // init_Slider1 show initially, if required (on Form1)

  gfx.DrawToframebuffer(CANVAS_BUFFER1);                                          // change to canvas buffer 1 which has the same dimensions as canvas buffer. We will use this in the blend canvas
  gfx.DrawJPEGarray(canvasBKG, canvasBKG_size, 0, 0, 1600, 556);                  // draw the same jpeg image from the binary in canvas_bkg.h.

  gfx.DrawToframebuffer(CANVAS_BUFFER_ARGB);                                      // change to blend canvas buffer
  gfx.DrawJPEGarray(canvasFG, canvasFG_size, 0, 0, 1600, 556);                    // draw jpeg image from the binary in canvas_fg.h. File was created with HXD

  gfx.DrawToframebuffer(OUTPUT_BUFFER);                                           // change to main display buffer

  gfx.ShowCanvas(canv_x, canv_y, 756, 556, 22, 42);                               // show canvas buffer in desired location and size. gfx.UpdateCanvas() can be used if this is called once.
  gfx.ShowCanvas(CANVAS_BUFFER1, CANVAS_BUFFER_ARGB, 0, 0, canv_blend_x, canv_blend_y, 756, 556, 22, 682);  // show blend canvas buffer in desired location and size. gfx.UpdateBlendCanvas() can be used if this is called once.
} // end Setup **do not alter, remove or duplicate this line**

void loop()
{
  // put your main code here, to run repeatedly:
  int itouched, val ;
  if(gfx.touch_Update())
  {
    int tx, ty;
    itouched = gfx.imageTouched() ;
    if (itouched == -1){                                                          // Check if no objects have been touched
      if (gfx.touch_GetCanvasTouched() == CANVAS_BUFFER_TOUCHED){                 // Check if the displayed canvas buffer is touched
        tx = gfx.touch_DeltaX();                                                  // get x touch delta value
        canv_x -= tx;                                                             // subtract it from current canvas position
        if (canv_x < 0) canv_x = 0;                                               // check range
        if (canv_x >= (1600 - 756)) canv_x = 1600 - 756;
        gfx.UpdateCanvas(canv_x, canv_y);                                         // update canvas with new x position
      }

      if (gfx.touch_GetCanvasTouched() == CANVAS_BUFFER_ARGB_TOUCHED){            // Check if the displayed blend canvas buffer is touched
        tx = gfx.touch_DeltaX();                                                  // get x touch delta value
        canv_blend_x -= tx;                                                       // subtract it from current blend canvas position
        if (canv_blend_x < 0) canv_blend_x = 0;                                   // check range
        if (canv_blend_x > (1600 - 756)) canv_blend_x = 1600 - 756;
        gfx.ShowCanvas(CANVAS_BUFFER1, CANVAS_BUFFER_ARGB, canv_blend_x >> 1, 0, canv_blend_x, canv_blend_y, 756, 556, 22, 682); // update canvas with new x position and move the backround by half distance
      }

    }
    switch (itouched)
    {                                                         // start touched selection **do not alter, remove or duplicate this line**
      // case statements for Knobs and Sliders go here
      case iSlider1 :                                      // process_Slider1 process (on Form1)
        gfx.DrawToframebuffer(CANVAS_BUFFER);                                           // change to canvas buffer
        val = gfx.imageAutoSlider(iSlider1, HORIZONTAL_SLIDER, gfx.touch_GetX(), 8, 8);
        gfx.DrawToframebuffer(OUTPUT_BUFFER);                                           // change to main display buffer
        gfx.UpdateCanvas(canv_x, canv_y);                                         // update canvas with new x position
        gfx.UpdateBlendCanvas(val, canv_blend_y);                                         // update canvas with new x position
        // process Slider based on val
        break ;
      default :                                               // end touched selection **do not alter, remove or duplicate this line**
        int button = gfx.ImageTouchedAuto();    // use default for keyboards and buttons
        val = gfx.getImageValue(button);
        switch (button)
        {                                                     // start button selection **do not alter, remove or duplicate this line**
          // case, one for each button or keyboard, default should end up as -1
        }                                                     // end button selection **do not alter, remove or duplicate this line**
    }
  }
}

