+
+/* ========================================================================= */
+/* touch input overlay functions */
+/* ========================================================================= */
+
+#if defined(USE_TOUCH_INPUT_OVERLAY)
+static void DrawTouchInputOverlay_ShowGrid(int alpha)
+{
+ SDL_Rect rect;
+ int grid_xsize = overlay.grid_xsize;
+ int grid_ysize = overlay.grid_ysize;
+ int x, y;
+
+ SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, alpha);
+ SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_BLEND);
+
+ for (x = 0; x < grid_xsize; x++)
+ {
+ rect.x = (x + 0) * video.screen_width / grid_xsize;
+ rect.w = (x + 1) * video.screen_width / grid_xsize - rect.x;
+
+ for (y = 0; y < grid_ysize; y++)
+ {
+ rect.y = (y + 0) * video.screen_height / grid_ysize;
+ rect.h = (y + 1) * video.screen_height / grid_ysize - rect.y;
+
+ if (overlay.grid_button[x][y] == CHAR_GRID_BUTTON_NONE)
+ SDL_RenderDrawRect(sdl_renderer, &rect);
+ }
+ }
+
+ SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
+}
+
+static void RenderFillRectangle(int x, int y, int width, int height)
+{
+ SDL_Rect rect = { x, y, width, height };
+
+ SDL_RenderFillRect(sdl_renderer, &rect);
+}
+
+static void DrawTouchInputOverlay_ShowGridButtons(int alpha)
+{
+ static int alpha_direction = 0;
+ static int alpha_highlight = 0;
+ int alpha_max = ALPHA_FROM_TRANSPARENCY(setup.touch.transparency);
+ int alpha_step = ALPHA_FADING_STEPSIZE(alpha_max);
+ SDL_Rect rect;
+ int grid_xsize = overlay.grid_xsize;
+ int grid_ysize = overlay.grid_ysize;
+ int x, y;
+
+ if (alpha == alpha_max)
+ {
+ if (alpha_direction < 0)
+ {
+ alpha_highlight = MAX(0, alpha_highlight - alpha_step);
+
+ if (alpha_highlight == 0)
+ alpha_direction = 1;
+ }
+ else
+ {
+ alpha_highlight = MIN(alpha_highlight + alpha_step, alpha_max);
+
+ if (alpha_highlight == alpha_max)
+ alpha_direction = -1;
+ }
+ }
+ else
+ {
+ alpha_direction = 1;
+ alpha_highlight = alpha;
+ }
+
+ SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_BLEND);
+
+ for (x = 0; x < grid_xsize; x++)
+ {
+ for (y = 0; y < grid_ysize; y++)
+ {
+ int grid_button = overlay.grid_button[x][y];
+ int grid_button_action = GET_ACTION_FROM_GRID_BUTTON(grid_button);
+ int alpha_draw = alpha;
+ int outline_border = MV_NONE;
+ int border_size = 2;
+ boolean draw_outlined = setup.touch.draw_outlined;
+ boolean draw_pressed = setup.touch.draw_pressed;
+
+ if (grid_button == CHAR_GRID_BUTTON_NONE)
+ continue;
+
+ if (grid_button == overlay.grid_button_highlight)
+ alpha_draw = alpha_highlight;
+
+ if (draw_pressed && overlay.grid_button_action & grid_button_action)
+ {
+ if (draw_outlined)
+ draw_outlined = FALSE;
+ else
+ alpha_draw = MIN((float)alpha_draw * 1.5, SDL_ALPHA_OPAQUE);
+ }
+
+ SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, alpha_draw);
+
+ rect.x = (x + 0) * video.screen_width / grid_xsize;
+ rect.y = (y + 0) * video.screen_height / grid_ysize;
+ rect.w = (x + 1) * video.screen_width / grid_xsize - rect.x;
+ rect.h = (y + 1) * video.screen_height / grid_ysize - rect.y;
+
+ if (x == 0 || overlay.grid_button[x - 1][y] != grid_button)
+ {
+ rect.x += border_size;
+ rect.w -= border_size;
+
+ outline_border |= MV_LEFT;
+ }
+
+ if (x == grid_xsize - 1 || overlay.grid_button[x + 1][y] != grid_button)
+ {
+ rect.w -= border_size;
+
+ outline_border |= MV_RIGHT;
+ }
+
+ if (y == 0 || overlay.grid_button[x][y - 1] != grid_button)
+ {
+ rect.y += border_size;
+ rect.h -= border_size;
+
+ outline_border |= MV_UP;
+ }
+
+ if (y == grid_ysize - 1 || overlay.grid_button[x][y + 1] != grid_button)
+ {
+ rect.h -= border_size;
+
+ outline_border |= MV_DOWN;
+ }
+
+ if (draw_outlined)
+ {
+ int rect_x = rect.x +
+ (outline_border & MV_LEFT ? border_size : 0);
+ int rect_w = rect.w -
+ (outline_border & MV_LEFT ? border_size : 0) -
+ (outline_border & MV_RIGHT ? border_size : 0);
+
+ if (outline_border & MV_LEFT)
+ RenderFillRectangle(rect.x, rect.y, border_size, rect.h);
+
+ if (outline_border & MV_RIGHT)
+ RenderFillRectangle(rect.x + rect.w - border_size, rect.y,
+ border_size, rect.h);
+
+ if (outline_border & MV_UP)
+ RenderFillRectangle(rect_x, rect.y, rect_w, border_size);
+
+ if (outline_border & MV_DOWN)
+ RenderFillRectangle(rect_x, rect.y + rect.h - border_size,
+ rect_w, border_size);
+ }
+ else
+ {
+ SDL_RenderFillRect(sdl_renderer, &rect);
+ }
+ }
+ }
+
+ SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
+}
+
+static void DrawTouchInputOverlay()
+{
+ static SDL_Texture *texture = NULL;
+ static boolean initialized = FALSE;
+ static boolean deactivated = TRUE;
+ static boolean show_grid = FALSE;
+ static int width = 0, height = 0;
+ static int alpha_last = -1;
+ static int alpha = 0;
+ int alpha_max = ALPHA_FROM_TRANSPARENCY(setup.touch.transparency);
+ int alpha_step = ALPHA_FADING_STEPSIZE(alpha_max);
+ boolean active = (overlay.enabled && overlay.active);
+
+ if (!active && deactivated)
+ return;
+
+ if (active)
+ {
+ if (alpha < alpha_max)
+ alpha = MIN(alpha + alpha_step, alpha_max);
+
+ deactivated = FALSE;
+ }
+ else
+ {
+ alpha = MAX(0, alpha - alpha_step);
+
+ if (alpha == 0)
+ deactivated = TRUE;
+ }
+
+ if (overlay.show_grid)
+ show_grid = TRUE;
+ else if (deactivated)
+ show_grid = FALSE;
+
+ if (show_grid)
+ DrawTouchInputOverlay_ShowGrid(alpha);
+
+ DrawTouchInputOverlay_ShowGridButtons(alpha);
+
+ return;
+
+
+ // !!! VIRTUAL BUTTONS FROM IMAGE FILE NOT USED ANYMORE !!!
+
+ if (!initialized)
+ {
+ char *basename = "overlay/VirtualButtons.png";
+ char *filename = getCustomImageFilename(basename);
+
+ if (filename == NULL)
+ Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
+
+ SDL_Surface *surface;
+
+ if ((surface = IMG_Load(filename)) == NULL)
+ Error(ERR_EXIT, "IMG_Load() failed: %s", SDL_GetError());
+
+ width = surface->w;
+ height = surface->h;
+
+ /* set black pixel to transparent if no alpha channel / transparent color */
+ if (!SDLHasAlpha(surface) &&
+ !SDLHasColorKey(surface))
+ SDL_SetColorKey(surface, SET_TRANSPARENT_PIXEL,
+ SDL_MapRGB(surface->format, 0x00, 0x00, 0x00));
+
+ if ((texture = SDLCreateTextureFromSurface(surface)) == NULL)
+ Error(ERR_EXIT, "SDLCreateTextureFromSurface() failed");
+
+ SDL_FreeSurface(surface);
+
+ SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
+
+ initialized = TRUE;
+ }
+
+ if (alpha != alpha_last)
+ SDL_SetTextureAlphaMod(texture, alpha);
+
+ alpha_last = alpha;
+
+ float ratio_overlay = (float) width / height;
+ float ratio_screen = (float) video.screen_width / video.screen_height;
+ int width_scaled, height_scaled;
+ int xpos, ypos;
+
+ if (ratio_overlay > ratio_screen)
+ {
+ width_scaled = video.screen_width;
+ height_scaled = video.screen_height * ratio_screen / ratio_overlay;
+ xpos = 0;
+ ypos = video.screen_height - height_scaled;
+ }
+ else
+ {
+ width_scaled = video.screen_width * ratio_overlay / ratio_screen;
+ height_scaled = video.screen_height;
+ xpos = (video.screen_width - width_scaled) / 2;
+ ypos = 0;
+ }
+
+ SDL_Rect src_rect = { 0, 0, width, height };
+ SDL_Rect dst_rect = { xpos, ypos, width_scaled, height_scaled };
+
+ SDL_RenderCopy(sdl_renderer, texture, &src_rect, &dst_rect);
+}
+#endif