From c6f3511d8492b54eac1829ec7a9cc39595b85d7b Mon Sep 17 00:00:00 2001
From: Victor <rhapsodyv@gmail.com>
Date: Thu, 11 Jun 2020 18:13:52 -0300
Subject: [PATCH] Touch-select MarlinUI menu items (#18239)

---
 Marlin/src/feature/touch/xpt2046.cpp | 28 ++++++++++++++++++++++------
 Marlin/src/lcd/ultralcd.cpp          | 23 +++++++++++++++++++++++
 Marlin/src/lcd/ultralcd.h            |  4 ++++
 3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/Marlin/src/feature/touch/xpt2046.cpp b/Marlin/src/feature/touch/xpt2046.cpp
index 26e25950d86..f03afe71e1c 100644
--- a/Marlin/src/feature/touch/xpt2046.cpp
+++ b/Marlin/src/feature/touch/xpt2046.cpp
@@ -23,6 +23,12 @@
 
 #include "xpt2046.h"
 #include "../../inc/MarlinConfig.h"
+#include "../../lcd/dogm/ultralcd_DOGM.h" // for LCD_FULL_PIXEL_WIDTH, etc.
+
+#define BUTTON_AREA_TOP 175
+#define BUTTON_AREA_BOT 234
+#define SCREEN_START_TOP ((LCD_PIXEL_OFFSET_Y) * 240 / (LCD_FULL_PIXEL_HEIGHT))
+#define TOUCHABLE_Y_HEIGHT (BUTTON_AREA_TOP - (SCREEN_START_TOP))
 
 #ifndef TOUCH_INT_PIN
   #define TOUCH_INT_PIN  -1
@@ -78,13 +84,23 @@ uint8_t XPT2046::read_buttons() {
                  y = uint16_t(((uint32_t(getInTouch(XPT2046_Y))) * tsoffsets[2]) >> 16) + tsoffsets[3];
   if (!isTouched()) return 0; // Fingers must still be on the TS for a valid read.
 
-  if (y < 175 || y > 234) return 0;
+  // Touch within the button area simulates an encoder button
+  if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT)
+    return WITHIN(x,  14,  77) ? EN_D
+         : WITHIN(x,  90, 153) ? EN_A
+         : WITHIN(x, 166, 229) ? EN_B
+         : WITHIN(x, 242, 305) ? EN_C
+         : 0;
 
-  return WITHIN(x,  14,  77) ? EN_D
-       : WITHIN(x,  90, 153) ? EN_A
-       : WITHIN(x, 166, 229) ? EN_B
-       : WITHIN(x, 242, 305) ? EN_C
-       : 0;
+  if (x > LCD_FULL_PIXEL_WIDTH || !WITHIN(y, SCREEN_START_TOP, BUTTON_AREA_TOP)) return 0;
+
+  // Column and row above BUTTON_AREA_TOP
+  int8_t col = x * (LCD_WIDTH) / (LCD_FULL_PIXEL_WIDTH),
+         row = (y - (SCREEN_START_TOP)) * (LCD_HEIGHT) / (TOUCHABLE_Y_HEIGHT);
+
+  // Send the touch to the UI (which will simulate the encoder wheel)
+  MarlinUI::screen_click(row, col, x, y);
+  return 0;
 }
 
 bool XPT2046::isTouched() {
diff --git a/Marlin/src/lcd/ultralcd.cpp b/Marlin/src/lcd/ultralcd.cpp
index 15276fcb43f..0869cbf5c5c 100644
--- a/Marlin/src/lcd/ultralcd.cpp
+++ b/Marlin/src/lcd/ultralcd.cpp
@@ -1438,6 +1438,29 @@ void MarlinUI::update() {
 
   #endif
 
+  #if ENABLED(TOUCH_BUTTONS)
+
+    //
+    // Screen Click
+    //  - On menu screens move directly to the touched item
+    //  - On menu screens, right side (last 3 cols) acts like a scroll - half up => prev page, half down = next page
+    //  - On select screens (and others) touch the Right Half for +, Left Half for -
+    //
+    void MarlinUI::screen_click(const uint8_t row, const uint8_t col, const uint8_t x, const uint8_t y) {
+      if (screen_items > 0) {
+        //last 3 cols act as a scroll :-)
+        if (col > (LCD_WIDTH) - 3)
+          //2 * LCD_HEIGHT to scroll to bottom of next page. If we did 1 * LCD_HEIGHT, it just go 1 item down
+          encoderDiff = ENCODER_PULSES_PER_STEP * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * (row < (LCD_HEIGHT) / 2 ? -1 : 1);
+        else
+          encoderDiff = ENCODER_PULSES_PER_STEP * (row - encoderPosition + encoderTopLine);
+      }
+      else if (!on_status_screen())
+        encoderDiff = ENCODER_PULSES_PER_STEP * (col < (LCD_WIDTH) / 2 ? -1 : 1);
+    }
+
+  #endif
+
 #else // !HAS_DISPLAY
 
   //
diff --git a/Marlin/src/lcd/ultralcd.h b/Marlin/src/lcd/ultralcd.h
index b17e11086f5..1b3ffd76607 100644
--- a/Marlin/src/lcd/ultralcd.h
+++ b/Marlin/src/lcd/ultralcd.h
@@ -417,6 +417,10 @@ public:
         static void draw_hotend_status(const uint8_t row, const uint8_t extruder);
       #endif
 
+      #if ENABLED(TOUCH_BUTTONS)
+        static void screen_click(const uint8_t row, const uint8_t col, const uint8_t x, const uint8_t y);
+      #endif
+
       static void status_screen();
 
     #endif