fix/clean-up mousewheel code
authorsheepluva
Sun, 10 Jan 2016 17:39:27 +0100
changeset 11491 2b30283a402c
parent 11490 e5bc40acdfaa
child 11492 8e221d2a368e
fix/clean-up mousewheel code
hedgewars/SDLh.pas
hedgewars/hwengine.pas
hedgewars/uInputHandler.pas
--- a/hedgewars/SDLh.pas	Sun Jan 10 12:59:03 2016 +0100
+++ b/hedgewars/SDLh.pas	Sun Jan 10 17:39:27 2016 +0100
@@ -91,8 +91,8 @@
     SDL_BUTTON_LEFT      = 1;
     SDL_BUTTON_MIDDLE    = 2;
     SDL_BUTTON_RIGHT     = 3;
-    SDL_BUTTON_WHEELUP   = 4;
-    SDL_BUTTON_WHEELDOWN = 5;
+    SDL_BUTTON_X1        = 4;
+    SDL_BUTTON_X2        = 5;
 
 
     SDL_TEXTEDITINGEVENT_TEXT_SIZE = 32;
--- a/hedgewars/hwengine.pas	Sun Jan 10 12:59:03 2016 +0100
+++ b/hedgewars/hwengine.pas	Sun Jan 10 17:39:27 2016 +0100
@@ -155,11 +155,13 @@
     PrevTime, CurrTime: LongWord;
     isTerminated: boolean;
     previousGameState: TGameState;
+    wheelEvent: boolean;
 begin
     isTerminated:= false;
     PrevTime:= SDL_GetTicks;
     while isTerminated = false do
     begin
+        wheelEvent:= false;
         SDL_PumpEvents();
 
         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
@@ -187,7 +189,10 @@
                     if (GameState >= gsGame) then ProcessMouse(event.button, false);
 
                 SDL_MOUSEWHEEL:
+                    begin
+                    wheelEvent:= true;
                     ProcessMouseWheel(event.wheel.x, event.wheel.y);
+                    end;
 
                 SDL_TEXTINPUT: uChat.TextInput(event.text);
 
@@ -239,6 +244,9 @@
             end; //end case event.type_ of
         end; //end while SDL_PollEvent(@event) <> 0 do
 
+        if (not wheelEvent) then
+            ResetMouseWheel();
+
         if (CursorMovementX <> 0) or (CursorMovementY <> 0) then
             handlePositionUpdate(CursorMovementX * cameraKeyboardSpeed, CursorMovementY * cameraKeyboardSpeed);
 
--- a/hedgewars/uInputHandler.pas	Sun Jan 10 12:59:03 2016 +0100
+++ b/hedgewars/uInputHandler.pas	Sun Jan 10 17:39:27 2016 +0100
@@ -35,6 +35,7 @@
 procedure ProcessKey(code: LongInt; KeyDown: boolean);
 
 procedure ResetKbd;
+procedure ResetMouseWheel;
 procedure FreezeEnterKey;
 procedure InitKbdKeyTable;
 
@@ -226,20 +227,40 @@
             ProcessKey(KeyNameToCode('mousem'), ButtonDown);
         SDL_BUTTON_RIGHT:
             ProcessKey(KeyNameToCode('mouser'), ButtonDown);
-        SDL_BUTTON_WHEELDOWN:
-            ProcessKey(KeyNameToCode('wheeldown'), ButtonDown);
-        SDL_BUTTON_WHEELUP:
-            ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
         end;
 end;
 
+var mwheelupCode, mwheeldownCode: Integer;
+
 procedure ProcessMouseWheel(x, y: LongInt);
 begin
     //writelntoconsole('[MOUSEWHEEL] '+inttostr(x)+', '+inttostr(y));
     if y > 0 then
-        ProcessKey(KeyNameToCode('wheelup'), true)
+        begin
+        // reset other direction
+        if tkbd[mwheeldownCode] then
+            ProcessKey(mwheeldownCode, false);
+        // trigger "button down" event
+        if (not tkbd[mwheelupCode]) then
+            ProcessKey(mwheelupCode, true);
+        end
     else if y < 0 then
-        ProcessKey(KeyNameToCode('wheeldown'), true);
+        begin
+        // reset other direction
+        if tkbd[mwheelupCode] then
+            ProcessKey(mwheelupCode, false);
+        // trigger "button down" event
+        if (not tkbd[mwheeldownCode]) then
+            ProcessKey(mwheeldownCode, true);
+        end;
+end;
+
+procedure ResetMouseWheel();
+begin
+    if tkbd[mwheelupCode] then
+        ProcessKey(mwheelupCode, false);
+    if tkbd[mwheeldownCode] then
+        ProcessKey(mwheeldownCode, false);
 end;
 
 procedure ResetKbd;
@@ -311,8 +332,10 @@
     KeyNames[cKeyMaxIndex    ]:= 'mousel';
     KeyNames[cKeyMaxIndex - 1]:= 'mousem';
     KeyNames[cKeyMaxIndex - 2]:= 'mouser';
-    KeyNames[cKeyMaxIndex - 3]:= 'wheelup';
-    KeyNames[cKeyMaxIndex - 4]:= 'wheeldown';
+    mwheelupCode:= cKeyMaxIndex - 3;
+    KeyNames[mwheelupCode]:= 'wheelup';
+    mwheeldownCode:= cKeyMaxIndex - 4;
+    KeyNames[mwheeldownCode]:= 'wheeldown';
 
     for i:= 0 to cKeyMaxIndex - 5 do
         begin
@@ -596,6 +619,10 @@
 
 procedure initModule;
 begin
+    // assign 0 until InitKbdKeyTable is called
+    mwheelupCode:= 0;
+    mwheeldownCode:= 0;
+
     RegisterVariable('dbind', @chDefaultBind, true );
 end;