changeset 37026:b6ff1451035d

Constrain an item's value to be in the range of 0 to 100. Add function contrain() and replace checking the bounds by it. Add it for mouse wheeling, too, where it has been missing so far.
author ib
date Thu, 03 Apr 2014 11:30:53 +0000
parents df052000865c
children c9d36530a55b
files gui/app/app.c gui/ui/main.c gui/ui/playbar.c gui/util/misc.c gui/util/misc.h
diffstat 5 files changed, 33 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/gui/app/app.c	Thu Apr 03 10:16:01 2014 +0000
+++ b/gui/app/app.c	Thu Apr 03 11:30:53 2014 +0000
@@ -24,6 +24,7 @@
 #include "app.h"
 #include "gui.h"
 #include "gui/skin/font.h"
+#include "gui/util/misc.h"
 
 #include "libavutil/common.h"
 
@@ -190,25 +191,13 @@
 
     for (i = 0; i <= guiApp.IndexOfMainItems; i++)
         if (guiApp.mainItems[i].message == event)
-            if (hasValue(guiApp.mainItems[i])) {
-                if (value < 0.0f)
-                    value = 0.0f;
-                if (value > 100.0f)
-                    value = 100.0f;
-
-                guiApp.mainItems[i].value = value;
-            }
+            if (hasValue(guiApp.mainItems[i]))
+                guiApp.mainItems[i].value = constrain(value);
 
     for (i = 0; i <= guiApp.IndexOfPlaybarItems; i++)
         if (guiApp.playbarItems[i].message == event)
-            if (hasValue(guiApp.playbarItems[i])) {
-                if (value < 0.0f)
-                    value = 0.0f;
-                if (value > 100.0f)
-                    value = 100.0f;
-
-                guiApp.playbarItems[i].value = value;
-            }
+            if (hasValue(guiApp.playbarItems[i]))
+                guiApp.playbarItems[i].value = constrain(value);
 }
 
 /**
--- a/gui/ui/main.c	Thu Apr 03 10:16:01 2014 +0000
+++ b/gui/ui/main.c	Thu Apr 03 11:30:53 2014 +0000
@@ -32,6 +32,7 @@
 #include "gui/skin/skin.h"
 #include "gui/util/list.h"
 #include "gui/util/mem.h"
+#include "gui/util/misc.h"
 #include "gui/util/string.h"
 #include "gui/wm/ws.h"
 #include "gui/wm/wsxdnd.h"
@@ -171,7 +172,7 @@
             item=&guiApp.mainItems[currentselected];
             if ( ( item->type == itHPotmeter )||( item->type == itVPotmeter ) )
              {
-              item->value+=value;
+              item->value=constrain(item->value + value);
               uiEvent( item->message,item->value );
              }
            }
@@ -189,13 +190,12 @@
                  if (guiApp.menuIsPresent) guiApp.menuWindow.MouseHandler( 0,RX,RY,0,0 );
                  break;
             case itVPotmeter:
-                 item->value=100.0 - 100.0 * ( Y - item->y ) / item->height;
+                 value=100.0 - 100.0 * ( Y - item->y ) / item->height;
                  goto potihandled;
             case itHPotmeter:
-                 item->value=100.0 * ( X - item->x ) / item->width;
+                 value=100.0 * ( X - item->x ) / item->width;
 potihandled:
-                 if ( item->value > 100.0f ) item->value=100.0f;
-                 if ( item->value < 0.0f ) item->value=0.0f;
+                 item->value=constrain(value);
                  uiEvent( item->message,item->value );
                  break;
            }
--- a/gui/ui/playbar.c	Thu Apr 03 10:16:01 2014 +0000
+++ b/gui/ui/playbar.c	Thu Apr 03 11:30:53 2014 +0000
@@ -30,6 +30,7 @@
 #include "gui/skin/font.h"
 #include "gui/skin/skin.h"
 #include "gui/util/mem.h"
+#include "gui/util/misc.h"
 #include "gui/wm/ws.h"
 
 #include "help_mp.h"
@@ -196,7 +197,7 @@
           item=&guiApp.playbarItems[currentselected];
           if ( ( item->type == itHPotmeter )||( item->type == itVPotmeter ) )
            {
-            item->value+=value;
+            item->value=constrain(item->value + value);
             uiEvent( item->message,item->value );
            }
          }
@@ -210,13 +211,12 @@
                if (guiApp.menuIsPresent) guiApp.menuWindow.MouseHandler( 0,RX,RY,0,0 );
                break;
           case itVPotmeter:
-               item->value=100.0 - 100.0 * ( Y - item->y ) / item->height;
+               value=100.0 - 100.0 * ( Y - item->y ) / item->height;
                goto potihandled;
           case itHPotmeter:
-               item->value=100.0 * ( X - item->x ) / item->width;
+               value=100.0 * ( X - item->x ) / item->width;
 potihandled:
-               if ( item->value > 100.0f ) item->value=100.0f;
-               if ( item->value < 0.0f ) item->value=0.0f;
+               item->value=constrain(value);
                uiEvent( item->message,item->value );
                break;
          }
--- a/gui/util/misc.c	Thu Apr 03 10:16:01 2014 +0000
+++ b/gui/util/misc.c	Thu Apr 03 11:30:53 2014 +0000
@@ -47,3 +47,20 @@
 
     return s;
 }
+
+/**
+ * @brief Constrain a @a value to be in the range of 0 to 100.
+ *
+ * @param value value to be checked
+ *
+ * @return a value in the range of 0 to 100
+ */
+float constrain(float value)
+{
+    if (value < 0.0f)
+        return 0.0f;
+    if (value > 100.0f)
+        return 100.0f;
+
+    return value;
+}
--- a/gui/util/misc.h	Thu Apr 03 10:16:01 2014 +0000
+++ b/gui/util/misc.h	Thu Apr 03 11:30:53 2014 +0000
@@ -21,6 +21,7 @@
 
 #include <stdio.h>
 
+float constrain(float value);
 char *fgetstr(char *str, int size, FILE *file);
 
 #endif /* MPLAYER_GUI_MISC_H */