From 3e33660c2e530d875509ebe5d94d975d1b53f907 Mon Sep 17 00:00:00 2001
From: Karol Stasiak <karol.m.stasiak@gmail.com>
Date: Fri, 1 May 2020 15:18:34 +0200
Subject: [PATCH] Improvements to mouse support. Added the y_coord type.
 Renamed x_coord module to coord.

---
 CHANGELOG.md                   |  4 ++++
 docs/lang/preprocessor.md      |  4 ++++
 docs/stdlib/mouse.md           | 23 +++++++++++++++++------
 include/c1531.mfk              |  7 +++++--
 include/coord.mfk              | 12 ++++++++++++
 include/mouse.mfk              | 13 +++++++++----
 include/null_mouse_default.mfk |  7 +++++--
 include/x_coord.mfk            |  7 ++-----
 mkdocs.yml                     |  2 +-
 9 files changed, 59 insertions(+), 20 deletions(-)
 create mode 100644 include/coord.mfk

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05f86cd5..1ab69976 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,10 @@ There are no built-in encodings now, the include path needs to contain the neces
 
 * **Potentially breaking change!** Changed default encoding for CPC to `cpc_en`.
 
+* **Potentially breaking change!** Changed the type of `mouse_lbm` and `mouse_rbm` to `bool`. Added `mouse_mbm`
+
+* Renamed the `x_coord` module to `coord`. Added the `y_coord` type and `TALLSCREEN` preprocessor feature.
+
 * Allow importing modules from subdirectories.
 
 * Allow placing platform definitions in a dedicated subdirectory.
diff --git a/docs/lang/preprocessor.md b/docs/lang/preprocessor.md
index da866df0..91359840 100644
--- a/docs/lang/preprocessor.md
+++ b/docs/lang/preprocessor.md
@@ -97,8 +97,12 @@ Some libraries may require that some of these be defined.
 
 * `WIDESCREEN` – 1 if the horizontal screen resolution, ignoring borders, is greater than 256, 0 otherwise
 
+* `TALLSCREEN` – 1 if the vertical screen resolution, ignoring borders, is greater than 256, 0 otherwise
+
 * `KEYBOARD` – 1 if the target has a keyboard, 0 otherwise
 
+* `USE_MOUSE_MBM` –  set this to 1 if you want to enable middle button support for the mouse. 
+
 * `JOYSTICKS` – the maximum number of joysticks using standard hardware configurations, may be 0
 
 * `HAS_BITMAP_MODE` – 1 if the target has a display mode with every pixel addressable, 0 otherwise
diff --git a/docs/stdlib/mouse.md b/docs/stdlib/mouse.md
index c47840a3..e6876372 100644
--- a/docs/stdlib/mouse.md
+++ b/docs/stdlib/mouse.md
@@ -2,7 +2,7 @@
 
 ## mouse
 
-The `mouse` module automatically imports the `x_coord` module.
+The `mouse` module automatically imports the `coord` module.
 
 The module contains global variables representing the state of the mouse.
 If the program is not using any mouse driver, the state of these variables is undefined.
@@ -13,19 +13,24 @@ To actually use this module, an appropriate mouse module must be used, such as [
 
 Mouse X position.
 
-#### `byte mouse_y`
+#### `y_coord mouse_y`
 
 Mouse Y position.
 
-#### `byte mouse_lbm`
+#### `bool mouse_lbm`
 
-1 if the left mouse button is being pressed, 0 otherwise
+`true` if the left mouse button is being pressed, `false` otherwise
+
+#### `byte mouse_mbm`
+
+`true` if the middle mouse button is being pressed, `false` otherwise.
+Available only if `USE_MOUSE_MBM` is set and non-zero.
 
 #### `byte mouse_rbm`
 
-1 if the right mouse button is being pressed, 0 otherwise
+`true` if the right mouse button is being pressed, `false` otherwise
 
-## x_coord
+## coord
 
 #### `alias x_coord`
 
@@ -33,6 +38,12 @@ The type for representing horizontal screen coordinates.
 It's `byte` if the screen is 256 pixels wide or less,
 or `word` if the screen is more that 256 pixels wide.
 
+#### `alias y_coord`
+
+The type for representing vertical screen coordinates.
+It's `byte` if the screen is 256 pixels tall or less,
+or `word` if the screen is more that 256 pixels tall.
+
 ## null_mouse_default
 
 This module set the default mouse to null mouse.
diff --git a/include/c1531.mfk b/include/c1531.mfk
index 08adcc73..9f889574 100644
--- a/include/c1531.mfk
+++ b/include/c1531.mfk
@@ -64,6 +64,9 @@ void c1531_mouse () {
     byte value
 	poke($dc03, 0)
 	value = peek($dc01)
-	mouse_rbm = (value & 1) ^ 1
-	if value & 16 == 0 { mouse_lbm = 1 } else { mouse_lbm = 0}
+	mouse_rbm = bool((value & 1) ^ 1)
+	mouse_lbm = value & 16 == 0
+#if USE_MOUSE_MBM
+	mouse_mbm = false
+#endif
 }
diff --git a/include/coord.mfk b/include/coord.mfk
new file mode 100644
index 00000000..5d09912f
--- /dev/null
+++ b/include/coord.mfk
@@ -0,0 +1,12 @@
+
+#if WIDESCREEN
+alias x_coord = word
+#else
+alias x_coord = byte
+#endif
+
+#if TALLSCREEN
+alias y_coord = word
+#else
+alias y_coord = byte
+#endif
diff --git a/include/mouse.mfk b/include/mouse.mfk
index 0d266d80..4d3f4698 100644
--- a/include/mouse.mfk
+++ b/include/mouse.mfk
@@ -1,16 +1,21 @@
 // Generic module for mouse support
 // Resolutions up to 512x256 are supported
 
-import x_coord
+import coord
 
 // Mouse X coordinate
 x_coord mouse_x
 
 // Mouse Y coordinate
-byte mouse_y
+y_coord mouse_y
 
 // Left mouse button pressed
-byte mouse_lbm
+bool mouse_lbm
+
+#if USE_MOUSE_MBM
+// Middle mouse button pressed
+bool mouse_mbm
+#endif
 
 // Right mouse button pressed
-byte mouse_rbm
+bool mouse_rbm
diff --git a/include/null_mouse_default.mfk b/include/null_mouse_default.mfk
index ca083704..0d2bd5a6 100644
--- a/include/null_mouse_default.mfk
+++ b/include/null_mouse_default.mfk
@@ -5,6 +5,9 @@ import mouse
 void read_mouse() {
     mouse_x = 0
     mouse_y = 0
-    mouse_lbm = 0
-    mouse_rbm = 0
+    mouse_lbm = false
+#if USE_MOUSE_MBM
+	mouse_mbm = false
+#endif
+    mouse_rbm = false
 }
diff --git a/include/x_coord.mfk b/include/x_coord.mfk
index 95caa9e4..8ce193c2 100644
--- a/include/x_coord.mfk
+++ b/include/x_coord.mfk
@@ -1,6 +1,3 @@
+#warn Module x_coord is deprecated, use coord
 
-#if WIDESCREEN
-alias x_coord = word
-#else
-alias x_coord = byte
-#endif
\ No newline at end of file
+import coord
diff --git a/mkdocs.yml b/mkdocs.yml
index 91fb84ed..1d163961 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -39,7 +39,7 @@ nav:
     - stdio module: stdlib/stdio.md
     - err and random modules: stdlib/other.md
     - keyboard module: stdlib/keyboard.md
-    - mouse and x_coord modules: stdlib/mouse.md
+    - mouse and coord modules: stdlib/mouse.md
     - joy module: stdlib/joy.md
     - encconv module: stdlib/encconv.md
     - Platform-dependent definitions: stdlib/frequent.md