shithub: choc

Download patch

ref: 92412e7ef7dea707875ac360016030f7b87d9210
parent: 9972c330a53315b3cf7e20dcca562f4fa8c26b89
author: Simon Howard <fraggle@soulsphere.org>
date: Mon Mar 25 06:07:40 EDT 2019

Refactor mouse speed box.

There was a lot of conditional code here to handle the two different
modes in which it can operate, and these are best served as just two
different functions. Get rid of box_x/box_y variables and replace with
constants.

As part of this I also got rid of the yellow scale indicators in the
non-accelerating case and just made it scale according to the largest
seen mouse speed. They didn't really add anything that I could see,
since the thermometer is really only there to aid calibration in the
case where acceleration is used.

--- a/src/v_video.c
+++ b/src/v_video.c
@@ -895,76 +895,31 @@
 
 #define MOUSE_SPEED_BOX_WIDTH  120
 #define MOUSE_SPEED_BOX_HEIGHT 9
+#define MOUSE_SPEED_BOX_X (SCREENWIDTH - MOUSE_SPEED_BOX_WIDTH - 10)
+#define MOUSE_SPEED_BOX_Y 15
 
 //
 // V_DrawMouseSpeedBox
 //
 
-// If box is only to calibrate speed, testing relative speed (as a measure
-// of game pixels to movement units) is important whether physical mouse DPI
-// is high or low. Line resolution starts at 1 pixel per 1 move-unit: if
-// line maxes out, resolution becomes 1 pixel per 2 move-units, then per
-// 3 move-units, etc.
-
-static int linelen_multiplier = 1;
-
-void V_DrawMouseSpeedBox(int speed)
+static void DrawAcceleratingBox(int speed)
 {
-    extern int usemouse;
-    int bgcolor, bordercolor, red, black, white, yellow;
-    int box_x, box_y;
+    int red, white, yellow;
     int original_speed;
     int redline_x;
     int linelen;
-    int i;
-    boolean draw_acceleration = false;
 
-    // Get palette indices for colors for widget. These depend on the
-    // palette of the game being played.
-
-    bgcolor = I_GetPaletteIndex(0x77, 0x77, 0x77);
-    bordercolor = I_GetPaletteIndex(0x55, 0x55, 0x55);
     red = I_GetPaletteIndex(0xff, 0x00, 0x00);
-    black = I_GetPaletteIndex(0x00, 0x00, 0x00);
-    yellow = I_GetPaletteIndex(0xff, 0xff, 0x00);
     white = I_GetPaletteIndex(0xff, 0xff, 0xff);
+    yellow = I_GetPaletteIndex(0xff, 0xff, 0x00);
 
-    // If the mouse is turned off, don't draw the box at all.
-    if (!usemouse)
-    {
-        return;
-    }
-
-    // If acceleration is used, draw a box that helps to calibrate the
-    // threshold point.
-    if (mouse_threshold > 0 && fabs(mouse_acceleration - 1) > 0.01)
-    {
-        draw_acceleration = true;
-    }
-
-    // Calculate box position
-
-    box_x = SCREENWIDTH - MOUSE_SPEED_BOX_WIDTH - 10;
-    box_y = 15;
-
-    V_DrawFilledBox(box_x, box_y,
-                    MOUSE_SPEED_BOX_WIDTH, MOUSE_SPEED_BOX_HEIGHT, bgcolor);
-    V_DrawBox(box_x, box_y,
-              MOUSE_SPEED_BOX_WIDTH, MOUSE_SPEED_BOX_HEIGHT, bordercolor);
-
     // Calculate the position of the red threshold line when calibrating
     // acceleration.  This is 1/3 of the way along the box.
 
     redline_x = MOUSE_SPEED_BOX_WIDTH / 3;
 
-    // Calculate line length
-
-    if (!draw_acceleration)
+    if (speed >= mouse_threshold)
     {
-        linelen = speed / linelen_multiplier;
-    }
-    else if (speed >= mouse_threshold)
-    {
         // Undo acceleration and get back the original mouse speed
         original_speed = speed - mouse_threshold;
         original_speed = (int) (original_speed / mouse_acceleration);
@@ -977,47 +932,95 @@
         linelen = (speed * redline_x) / mouse_threshold;
     }
 
-    // Draw horizontal "thermometer" 
-
+    // Horizontal "thermometer"
     if (linelen > MOUSE_SPEED_BOX_WIDTH - 1)
     {
         linelen = MOUSE_SPEED_BOX_WIDTH - 1;
-        if (!draw_acceleration)
-        {
-            linelen_multiplier++;
-        }
     }
 
-    V_DrawHorizLine(box_x + 1, box_y + 4, MOUSE_SPEED_BOX_WIDTH - 2, black);
-
-    if (!draw_acceleration || linelen < redline_x)
+    if (linelen < redline_x)
     {
-        V_DrawHorizLine(box_x + 1, box_y + MOUSE_SPEED_BOX_HEIGHT / 2,
+        V_DrawHorizLine(MOUSE_SPEED_BOX_X + 1,
+                        MOUSE_SPEED_BOX_Y + MOUSE_SPEED_BOX_HEIGHT / 2,
                         linelen, white);
     }
     else
     {
-        V_DrawHorizLine(box_x + 1, box_y + MOUSE_SPEED_BOX_HEIGHT / 2,
+        V_DrawHorizLine(MOUSE_SPEED_BOX_X + 1,
+                        MOUSE_SPEED_BOX_Y + MOUSE_SPEED_BOX_HEIGHT / 2,
                         redline_x, white);
-        V_DrawHorizLine(box_x + redline_x, box_y + MOUSE_SPEED_BOX_HEIGHT / 2,
+        V_DrawHorizLine(MOUSE_SPEED_BOX_X + redline_x,
+                        MOUSE_SPEED_BOX_Y + MOUSE_SPEED_BOX_HEIGHT / 2,
                         linelen - redline_x, yellow);
     }
 
-    if (draw_acceleration)
+    // Draw acceleration threshold line
+    V_DrawVertLine(MOUSE_SPEED_BOX_X + redline_x, MOUSE_SPEED_BOX_Y + 1,
+                   MOUSE_SPEED_BOX_HEIGHT - 2, red);
+}
+
+// Highest seen mouse turn speed. We scale the range of the thermometer
+// according to this value, so that it never exceeds the range. Initially
+// this is set to a 1:1 setting where 1 pixel = 1 unit of speed.
+static int max_seen_speed = MOUSE_SPEED_BOX_WIDTH - 1;
+
+static void DrawNonAcceleratingBox(int speed)
+{
+    int white, yellow;
+    int linelen;
+
+    yellow = I_GetPaletteIndex(0xff, 0xff, 0x00);
+    white = I_GetPaletteIndex(0xff, 0xff, 0xff);
+
+    if (speed > max_seen_speed)
     {
-        // Draw acceleration threshold line
-        V_DrawVertLine(box_x + redline_x, box_y + 1,
-                       MOUSE_SPEED_BOX_HEIGHT - 2, red);
+        max_seen_speed = speed;
     }
+
+    // Draw horizontal "thermometer":
+    linelen = speed * (MOUSE_SPEED_BOX_WIDTH - 1) / max_seen_speed;
+
+    V_DrawHorizLine(MOUSE_SPEED_BOX_X + 1,
+                    MOUSE_SPEED_BOX_Y + MOUSE_SPEED_BOX_HEIGHT / 2,
+                    linelen, white);
+}
+
+void V_DrawMouseSpeedBox(int speed)
+{
+    extern int usemouse;
+    int bgcolor, bordercolor, black;
+
+    // If the mouse is turned off, don't draw the box at all.
+    if (!usemouse)
+    {
+        return;
+    }
+
+    // Get palette indices for colors for widget. These depend on the
+    // palette of the game being played.
+
+    bgcolor = I_GetPaletteIndex(0x77, 0x77, 0x77);
+    bordercolor = I_GetPaletteIndex(0x55, 0x55, 0x55);
+    black = I_GetPaletteIndex(0x00, 0x00, 0x00);
+
+    // Calculate box position
+
+    V_DrawFilledBox(MOUSE_SPEED_BOX_X, MOUSE_SPEED_BOX_Y,
+                    MOUSE_SPEED_BOX_WIDTH, MOUSE_SPEED_BOX_HEIGHT, bgcolor);
+    V_DrawBox(MOUSE_SPEED_BOX_X, MOUSE_SPEED_BOX_Y,
+              MOUSE_SPEED_BOX_WIDTH, MOUSE_SPEED_BOX_HEIGHT, bordercolor);
+    V_DrawHorizLine(MOUSE_SPEED_BOX_X + 1, MOUSE_SPEED_BOX_Y + 4,
+                    MOUSE_SPEED_BOX_WIDTH - 2, black);
+
+    // If acceleration is used, draw a box that helps to calibrate the
+    // threshold point.
+    if (mouse_threshold > 0 && fabs(mouse_acceleration - 1) > 0.01)
+    {
+        DrawAcceleratingBox(speed);
+    }
     else
     {
-        // Draw multiplier lines to indicate current resolution
-        for (i = 1; i < linelen_multiplier; i++)
-        {
-            V_DrawVertLine(
-                box_x + (i * MOUSE_SPEED_BOX_WIDTH / linelen_multiplier),
-                box_y + 1, MOUSE_SPEED_BOX_HEIGHT - 2, yellow);
-        }
+        DrawNonAcceleratingBox(speed);
     }
 }