added grid buttons to virtual buttons setup screen
authorHolger Schemel <info@artsoft.org>
Tue, 8 May 2018 17:33:15 +0000 (19:33 +0200)
committerHolger Schemel <info@artsoft.org>
Tue, 5 Jun 2018 18:58:41 +0000 (20:58 +0200)
This change defines a grid of virtual buttons that can be configured on
the virtual buttons setup screen for all four directions and two buttons
(but is not used or shown anywhere else yet).

src/libgame/sdl.c
src/libgame/system.c
src/libgame/system.h
src/screens.c

index 3eb2a73704b4f646226ba73e396c10007292b563..d1d14a825bc525c7e35fae31c03f0b3fb9267d6b 100644 (file)
@@ -2987,8 +2987,8 @@ boolean SDLReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
 static void DrawTouchInputOverlay_ShowGrid(int alpha)
 {
   SDL_Rect rect;
-  int grid_xsize = 18;
-  int grid_ysize = 11;
+  int grid_xsize = overlay.grid_xsize;
+  int grid_ysize = overlay.grid_ysize;
   int x, y;
 
   SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, alpha);
@@ -3004,7 +3004,67 @@ static void DrawTouchInputOverlay_ShowGrid(int alpha)
       rect.y = (y + 0) * video.screen_height / grid_ysize;
       rect.h = (y + 1) * video.screen_height / grid_ysize - rect.y;
 
-      SDL_RenderDrawRect(sdl_renderer, &rect);
+      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 DrawTouchInputOverlay_ShowGridButtons(int alpha)
+{
+  static int alpha_max = SDL_ALPHA_OPAQUE / 2;
+  static int alpha_step = 5;
+  static int alpha_direction = 0;
+  static int alpha_highlight = 0;
+  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++)
+  {
+    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] == overlay.grid_button_highlight)
+       SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, alpha_highlight);
+      else
+       SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, alpha);
+
+      if (overlay.grid_button[x][y] != CHAR_GRID_BUTTON_NONE)
+       SDL_RenderFillRect(sdl_renderer, &rect);
     }
   }
 
@@ -3017,6 +3077,7 @@ static void DrawTouchInputOverlay()
   static boolean initialized = FALSE;
   static boolean deactivated = TRUE;
   static boolean show_grid = FALSE;
+  static boolean show_grid_buttons = FALSE;
   static int width = 0, height = 0;
   static int alpha_max = SDL_ALPHA_OPAQUE / 2;
   static int alpha_step = 5;
@@ -3047,12 +3108,19 @@ static void DrawTouchInputOverlay()
   else if (deactivated)
     show_grid = FALSE;
 
+  if (overlay.show_grid_buttons)
+    show_grid_buttons = TRUE;
+  else if (deactivated)
+    show_grid_buttons = FALSE;
+
   if (show_grid)
-  {
     DrawTouchInputOverlay_ShowGrid(alpha);
 
+  if (show_grid_buttons)
+    DrawTouchInputOverlay_ShowGridButtons(alpha);
+
+  if (show_grid || show_grid_buttons)
     return;
-  }
 
   if (!initialized)
   {
index 29c3b24cb6f82df737a596d97b3f27d5b5024f62..33246588592ebf6dab8b5da904fb314f9bd97492 100644 (file)
@@ -325,10 +325,49 @@ void InitTileCursorInfo()
 
 void InitOverlayInfo()
 {
+  static char *default_grid_button[6][2] =
+  {
+    { "      ",        "  ^^  " },
+    { "      ",        "  ^^  " },
+    { "      ",        "<<  >>" },
+    { "      ",        "<<  >>" },
+    { "111222",        "  vv  " },
+    { "111222",        "  vv  " }
+  };
+  int min_xsize, min_ysize;
+  int startx, starty;
+  int x, y;
+
+  min_xsize = MIN(6, DEFAULT_GRID_XSIZE);
+  min_ysize = MIN(6, DEFAULT_GRID_YSIZE);
+
+  startx = DEFAULT_GRID_XSIZE - min_xsize;
+  starty = DEFAULT_GRID_YSIZE - min_ysize;
+
   overlay.enabled = FALSE;
   overlay.active = FALSE;
 
   overlay.show_grid = FALSE;
+  overlay.show_grid_buttons = FALSE;
+
+  overlay.grid_xsize = DEFAULT_GRID_XSIZE;
+  overlay.grid_ysize = DEFAULT_GRID_YSIZE;
+
+  for (x = 0; x < MAX_GRID_XSIZE; x++)
+    for (y = 0; y < MAX_GRID_YSIZE; y++)
+      overlay.grid_button[x][y] = CHAR_GRID_BUTTON_NONE;
+
+  for (x = 0; x < min_xsize; x++)
+    for (y = 0; y < min_ysize; y++)
+      overlay.grid_button[x][starty + y] =
+       default_grid_button[y][0][x];
+
+  for (x = 0; x < min_xsize; x++)
+    for (y = 0; y < min_ysize; y++)
+      overlay.grid_button[startx + x][starty + y] =
+       default_grid_button[y][1][x];
+
+  overlay.grid_button_highlight = CHAR_GRID_BUTTON_NONE;
 
 #if defined(PLATFORM_ANDROID)
   if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
@@ -390,6 +429,7 @@ void SetOverlayActive(boolean active)
 void SetOverlayShowGrid(boolean show_grid)
 {
   overlay.show_grid = show_grid;
+  overlay.show_grid_buttons = show_grid;
 
   SetOverlayActive(show_grid);
 
index 6bf12a6670d2a58f781810a7a1099e7ecac7713d..06a47c3c685fe17fc7eda1692843d7d8c51fa080 100644 (file)
 #define SCREEN_KEYBOARD_POS(h)         ((h) / 2)
 #endif
 
-
 /* default input keys */
 #define DEFAULT_KEY_LEFT               KSYM_Left
 #define DEFAULT_KEY_RIGHT              KSYM_Right
 #define MAX_GLOBAL_ANIMS               32
 #define MAX_GLOBAL_ANIM_PARTS          32
 
+/* minimum/maximum/default x/y grid size for virtual buttons */
+#define MIN_GRID_XSIZE                 3
+#define MIN_GRID_YSIZE                 3
+#define MAX_GRID_XSIZE                 32
+#define MAX_GRID_YSIZE                 32
+#define DEFAULT_GRID_XSIZE             18
+#define DEFAULT_GRID_YSIZE             MIN(MAX(MIN_GRID_YSIZE,         \
+                                               DEFAULT_GRID_XSIZE *    \
+                                               video.screen_height /   \
+                                               video.screen_width),    \
+                                           MAX_GRID_YSIZE)
+
+/* values for grid button characters for virtual buttons */
+#define CHAR_GRID_BUTTON_NONE          ' '
+#define CHAR_GRID_BUTTON_LEFT          '<'
+#define CHAR_GRID_BUTTON_RIGHT         '>'
+#define CHAR_GRID_BUTTON_UP            '^'
+#define CHAR_GRID_BUTTON_DOWN          'v'
+#define CHAR_GRID_BUTTON_SNAP          '1'
+#define CHAR_GRID_BUTTON_DROP          '2'
+
 /* default name for empty highscore entry */
 #define EMPTY_PLAYER_NAME      "no name"
 
@@ -978,6 +998,13 @@ struct OverlayInfo
   boolean active;              /* overlay activated (depending on game mode) */
 
   boolean show_grid;
+  boolean show_grid_buttons;
+
+  int grid_xsize;
+  int grid_ysize;
+
+  char grid_button[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
+  char grid_button_highlight;
 };
 
 struct JoystickInfo
index 1c1efa62b309d5a0482ec6ca513a6fc8b1baa3d2..1444fe8be075de897b9e91c869047097f903bf2f 100644 (file)
@@ -7186,6 +7186,15 @@ boolean ConfigureVirtualButtonsMain()
     "Snap Field",
     "Drop Element"
   };
+  char grid_button[] =
+  {
+    CHAR_GRID_BUTTON_LEFT,
+    CHAR_GRID_BUTTON_RIGHT,
+    CHAR_GRID_BUTTON_UP,
+    CHAR_GRID_BUTTON_DOWN,
+    CHAR_GRID_BUTTON_SNAP,
+    CHAR_GRID_BUTTON_DROP
+  };
   int font_nr = FONT_INPUT_1_ACTIVE;
   int font_height = getFontHeight(font_nr);
   int ypos1 = SYSIZE / 2 - font_height * 2;
@@ -7193,6 +7202,17 @@ boolean ConfigureVirtualButtonsMain()
   boolean success = FALSE;
   boolean finished = FALSE;
   int step_nr = 0;
+  char grid_button_draw = CHAR_GRID_BUTTON_NONE;
+  char grid_button_old[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
+  char grid_button_tmp[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
+  boolean set_grid_button = FALSE;
+  int x, y;
+
+  for (x = 0; x < MAX_GRID_XSIZE; x++)
+    for (y = 0; y < MAX_GRID_YSIZE; y++)
+      grid_button_old[x][y] = grid_button_tmp[x][y] = overlay.grid_button[x][y];
+
+  overlay.grid_button_highlight = grid_button[step_nr];
 
   FadeSetEnterMenu();
   FadeOut(REDRAW_FIELD);
@@ -7222,6 +7242,10 @@ boolean ConfigureVirtualButtonsMain()
            /* press 'Escape' to abort and keep the old key bindings */
            if (key == KSYM_Escape)
            {
+             for (x = 0; x < MAX_GRID_XSIZE; x++)
+               for (y = 0; y < MAX_GRID_YSIZE; y++)
+                 overlay.grid_button[x][y] = grid_button_old[x][y];
+
              FadeSkipNextFadeIn();
 
              finished = TRUE;
@@ -7264,6 +7288,12 @@ boolean ConfigureVirtualButtonsMain()
              break;
            }
 
+           for (x = 0; x < MAX_GRID_XSIZE; x++)
+             for (y = 0; y < MAX_GRID_YSIZE; y++)
+               grid_button_tmp[x][y] = overlay.grid_button[x][y];
+
+           overlay.grid_button_highlight = grid_button[step_nr];
+
            /* query next virtual button */
 
            ClearField();
@@ -7278,15 +7308,69 @@ boolean ConfigureVirtualButtonsMain()
          key_joystick_mapping = 0;
          break;
 
+       case EVENT_BUTTONPRESS:
+       case EVENT_BUTTONRELEASE:
+         {
+           ButtonEvent *button = (ButtonEvent *)&event;
+
+           button->x += video.screen_xoffset;
+           button->y += video.screen_yoffset;
+
+           x = button->x * overlay.grid_xsize / video.screen_width;
+           y = button->y * overlay.grid_ysize / video.screen_height;
+
+           if (button->type == EVENT_BUTTONPRESS)
+           {
+             button_status = button->button;
+
+             grid_button_draw =
+               (overlay.grid_button[x][y] != grid_button[step_nr] ?
+                grid_button[step_nr] : CHAR_GRID_BUTTON_NONE);
+
+             set_grid_button = TRUE;
+           }
+           else
+           {
+             button_status = MB_RELEASED;
+           }
+         }
+         break;
+
+       case EVENT_MOTIONNOTIFY:
+         {
+           MotionEvent *motion = (MotionEvent *)&event;
+
+           motion->x += video.screen_xoffset;
+           motion->y += video.screen_yoffset;
+
+           x = motion->x * overlay.grid_xsize / video.screen_width;
+           y = motion->y * overlay.grid_ysize / video.screen_height;
+
+           set_grid_button = TRUE;
+         }
+         break;
+
         default:
          HandleOtherEvents(&event);
          break;
       }
+
+      if (set_grid_button)
+      {
+       overlay.grid_button[x][y] =
+         (grid_button_draw != CHAR_GRID_BUTTON_NONE ? grid_button_draw :
+          grid_button_tmp[x][y] == grid_button[step_nr] ? CHAR_GRID_BUTTON_NONE :
+          grid_button_tmp[x][y]);
+
+       set_grid_button = FALSE;
+      }
     }
 
     BackToFront();
   }
 
+  overlay.grid_button_highlight = CHAR_GRID_BUTTON_NONE;
+
   SetOverlayShowGrid(FALSE);
 
   return success;