UTFT icon pointer

hi, I am working on a code where I am having a grid menu on a TFT, every icon or element in the screen will have a rectangle surrounding it when the pointer is on that icon. I decided to do that by giving each icon a position ID where the first one has ID = 1 and the last icon has the ID = 8 (8 icons).

switching between the icons is done by four push buttons (right, left, up & down), and I have if statments to test which button is pressed and then another if statment inside it to test the position and determine where to draw the next pointer (rectangle). the problem is when I am on position 1 and press the 'right' button the pointer switches to position 3, not 2.. and when I am on position 3 and press 'down' it goes to position 5, which it should not do as the it should go to position 5 only when it is on position 1 and 'down' button is pressed.

does anybody have an idea on how to fix that? next is attached the code I have written so far, and a screen shot to make understanding it easier.

#include <Bounce.h>
#include <UTFT.h>

        // This code block is only needed to support multiple
        // MCU architectures in a single sketch.

        #if defined(__AVR__)
        #define imagedatatype  unsigned int
        #elif defined(__PIC32MX__)
        #define imagedatatype  unsigned short
        #elif defined(__arm__)
        #define imagedatatype  unsigned short
        #endif
// End of multi-architecture block
               
        // Declare which fonts we will be using
        extern uint8_t BigFont[];
        extern uint8_t SevenSegNumFont[];
        extern uint8_t arial_normal[];
        
        // Declare which bitmaps we will be using
        
        extern imagedatatype Axis[];
        extern imagedatatype Info[];
        extern imagedatatype Leveling[];
        extern imagedatatype Notification[];
        extern imagedatatype Settings[];
        extern imagedatatype WIFI[];
        extern imagedatatype Back[];
        extern imagedatatype Sd_Card[];
        
        // Set up UTFT...
        // Set the pins to the correct ones for your development board
        // -----------------------------------------------------------
        // Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
        UTFT myGLCD(CTE32HR,38,39,40,41);

        #define button1 7
        #define button2 6
        #define button3 5
        #define button4 4
        int current_position = 1;
        Bounce buttonR = Bounce(button1, 5);  // 5 ms debounce
        Bounce buttonL = Bounce(button2, 5);  // 5 ms debounce
        Bounce buttonU = Bounce(button3, 5);  // 5 ms debounce
        Bounce buttonD = Bounce(button4, 5);  // 5 ms debounce



void setup() {

    Serial.begin(57600);
  Serial.println("Current menu cursor position test:");


          pinMode(button1, INPUT_PULLUP);
          pinMode(button2, INPUT_PULLUP);
          pinMode(button3, INPUT_PULLUP);
          pinMode(button4, INPUT_PULLUP);

   
          //Initialize the screen
          myGLCD.InitLCD();
          myGLCD.clrScr();
          myGLCD.fillScr(45, 62, 80);
          myGLCD.setBackColor(45, 62, 80);
          myGLCD.drawBitmap( 45, 64, 64, 64, Sd_Card);
          myGLCD.drawBitmap( 154, 64, 64, 64, Leveling);
          myGLCD.drawBitmap( 262, 64, 64, 64, Axis);
          myGLCD.drawBitmap( 369, 64, 64, 64, WIFI);
          myGLCD.drawBitmap( 45, 192, 64, 64, Settings);
          myGLCD.drawBitmap( 154, 192, 64, 64, Notification);
          myGLCD.drawBitmap( 262, 192, 64, 64, Info);
          myGLCD.drawBitmap( 369, 192, 64, 64, Back);
          myGLCD.setColor(222, 222, 222);
          myGLCD.drawRect(30,49,124, 148);
          /*myGLCD.setFont(arial_normal);
          myGLCD.print("SD Card", 30, 145);
         */
         
        }

        void loop() {

        buttonR.update ( );
        buttonL.update ( );
        buttonU.update ( );
        buttonD.update ( );
        
        int buttonR_state = buttonR.read();
        int buttonL_state = buttonL.read();
        int buttonU_state = buttonU.read();
        int buttonD_state = buttonD.read();
        
        
        if (buttonR_state == LOW){
         if(current_position = 1){
         position1a();
         }
        }
        
         if (buttonD_state == LOW) {
          if(current_position = 1){
         position1b();
         }
        }

        if (buttonR_state == LOW) {
         if(current_position = 2){
          position2a();
          }
         }
        
         
        }

        void position1a()
        {
         myGLCD.setColor(45, 62, 80);
         myGLCD.drawRect(30,49,124, 148);
         myGLCD.setColor(222, 222, 222);
         myGLCD.drawRect(139,49,233, 148);
         current_position = 2;
         Serial.print(current_position);

          }
        
         void position1b()
         {
         myGLCD.setColor(45, 62, 80);
         myGLCD.drawRect(30,49,124, 148);
         myGLCD.setColor(222, 222, 222);
         myGLCD.drawRect(30,177,124, 276); 
         current_position = 5;
         Serial.print(current_position);
         }

         void position2a()
         {
         myGLCD.setColor(45, 62, 80);
         myGLCD.drawRect(139,49,233, 148);
         myGLCD.setColor(222, 222, 222);
         myGLCD.drawRect(248,49,342, 148); 
         current_position = 3;
         Serial.print(current_position);
         }

arial_normal.c (16.1 KB)

Axis.c (37.2 KB)

Back.c (37.2 KB)

Info.c (37.2 KB)

Leveling.c (37.2 KB)

Notification.c (37.2 KB)

Sd_Card.c (37.2 KB)

Settings.c (37.2 KB)

trial_6.ino (4.28 KB)

WIFI.c (37.2 KB)

he problem is when I am on position 1 and press the 'right' button the pointer switches to position 3, not 2..

You are changing the cursor position when the button is pressed rather than when it becomes pressed. Have a look at the StateChangeDetection example in the IDE to see how to detect a change of input state.

You could also do yourself a favour by having the cursor coordinates in arrays indexed by the current position and writing a function that takes the previous and current positions and erases and draws the rectangles..