touch control completely revamped
authorkoda
Sun, 25 Oct 2009 23:52:36 +0000
changeset 2590 e7e87e3c67db
parent 2589 4329597c85c8
child 2591 c6597b65caea
touch control completely revamped
hedgewars/SDLh.pas
hedgewars/hwengine.pas
hedgewars/options.inc
hedgewars/uKeys.pas
hedgewars/uWorld.pas
--- a/hedgewars/SDLh.pas	Sun Oct 25 18:16:40 2009 +0000
+++ b/hedgewars/SDLh.pas	Sun Oct 25 23:52:36 2009 +0000
@@ -104,10 +104,11 @@
 {$IFDEF SDL13}
         SDL_WINDOWEVENT = 1;
         SDL_TEXTINPUT = 4;
-	SDL_MOUSEMOTION  = 5;
-        SDL_MOUSEBUTTONDOWN = 6;
-	SDL_MOUSEBUTTONUP   = 7;
-        SDL_MOUSEWHEEL = 8;  //different handling, should create SDL_MouseWheelEvent type
+        SDL_TEXTEDITING = 5;
+	SDL_MOUSEMOTION  = 6;
+        SDL_MOUSEBUTTONDOWN = 7;
+	SDL_MOUSEBUTTONUP   = 8;
+        SDL_MOUSEWHEEL = 9;  //different handling, should create SDL_MouseWheelEvent type
 	SDL_JOYAXIS = 10;
 	SDL_JOYHAT = 12;
 	SDL_JOYBUTTONDOWN = 13;
@@ -426,6 +427,7 @@
 
 procedure SDL_PumpEvents; cdecl; external SDLLibName;
 function  SDL_PollEvent(event: PSDL_Event): LongInt; cdecl; external SDLLibName;
+function  SDL_WaitEvent(event: PSDL_Event): LongInt; cdecl; external SDLLibName;
 
 function  SDL_ShowCursor(toggle: LongInt): LongInt; cdecl; external SDLLibName;
 
--- a/hedgewars/hwengine.pas	Sun Oct 25 18:16:40 2009 +0000
+++ b/hedgewars/hwengine.pas	Sun Oct 25 23:52:36 2009 +0000
@@ -166,13 +166,23 @@
 {$IFDEF IPHONEOS}
 type TDirection = (nodir, left, right);
 var x, y: LongInt;
-    oldy_zoom: LongInt = -1;
-    oldy_aim: LongInt = -1;
+        dx, dy, mouseState: LongInt;
+    direction: TDirection = nodir;
+    havetrace: boolean = false;
 {$ENDIF}
 begin
 PrevTime:= SDL_GetTicks;
 repeat
 while SDL_PollEvent(@event) <> 0 do
+{$IFDEF TOUCHINPUT}
+if direction <> nodir then
+begin
+        uKeys.isWalking:= true;
+        if direction = left then uKeys.leftKey:= true
+        else if direction = right then uKeys.rightKey:= true;
+end
+else uKeys.isWalking:= false;
+{$ENDIF}
 	case event.type_ of
 {$IFDEF SDL13}
                 SDL_WINDOWEVENT:
@@ -184,52 +194,101 @@
 				cHasFocus:= event.active.gain = 1;
 		//SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450));
 {$IFDEF TOUCHINPUT}
+                SDL_MOUSEMOTION: begin
+                        AddFileLog('*********************************************       motion');
+                        mouseState:= SDL_GetMouseState(0, @x, @y);
+                        SDL_GetRelativeMouseState(0, @dx, @dy);
+                        
+                        direction:= nodir;
+
+                        if boolean(mouseState) then
+                        begin
+                                AddFileLog('x: ' + inttostr(x) + ' y: ' + inttostr(y) + ' dx: ' + inttostr(dx) + ' dy: ' + inttostr(dy));
+
+                                {* zoom slider *}
+                                if (x <= 50) and (y <= 430) and (y > 50) then 
+                                begin
+                                        if (dy > 0) then uKeys.wheelDown:= true
+                                        else if (dy < 0) then uKeys.wheelUp:= true;
+                                end;
+
+                                {* aim slider *}
+                                if (x > 270) and (y > 50) and (y <= 430) then
+                                begin
+                                        if (dy > 0) then uKeys.downKey:= true
+                                        else if (dy < 0) then uKeys.upKey:= true;
+                                end;
+
+                                {* switch *}
+                                if (x > 50) and (x <= 270) and (y > 400) then
+                                begin
+                                        if (dy <> 0) then uKeys.tabKey:= true
+                                end;
+                        end;
+                end;
         {*MoveCamera is in uWord.pas -- conflicts with other commands*}
-        {*Keys are in uKeys.pas*}
                 SDL_MOUSEBUTTONDOWN: begin
                         AddFileLog('*********************************************       touch down');
 
-                        SDL_GetMouseState(0, @x, @y);
- uKeys.leftClick:= true;
+                        mouseState:= SDL_GetMouseState(0, @x, @y);                       
+
+                        {* attack *}
+                        if (x > 50) and (x <= 270) and (y <= 50) then
+                        begin
+                                AddFileLog('Space DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                                uKeys.spaceKey:= true;
+                                uKeys.isAttacking:= true;
+                        end;
 
-                        {* zoom slider *}
-                        if (x <= 50) and (y <= 430) then 
+                        if (x <= 50) and (y <= 50) then
+                        begin
+                                AddFileLog('Left Arrow Key DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                            //    uKeys.leftKey:= true;
+                            //    uKeys.isWalking:= true;
+                                direction:= left;
+                        end;
+                        
+                        if (x > 270) and (y <= 50) then
+                        begin
+                                AddFileLog('Right Arrow Key DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                            //    uKeys.rightKey:= true;
+                            //    uKeys.isWalking:= true;
+                                direction:= right;
+                        end;
+
+                        {* high jump *}                        
+                        if (x > 160) and (x <= 270) and (y > 400) then
                         begin
 {$IFDEF DEBUGFILE}
-                                AddFileLog('Wheel -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                                AddFileLog('Backspace -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
 {$ENDIF}
-                                if oldy_zoom = -1 then oldy_zoom:= y
-                                else
-                                begin
-                                        if oldy_zoom - y > 10 then uKeys.wheelUp:= true
-                                        else if oldy_zoom -y < -10 then uKeys.wheelDown:= true;
-                                        {* update value only if movement is consistent *}
-                                        if (y > oldy_zoom + 20 ) or (y < oldy_zoom - 20 ) then oldy_zoom:= y
-                                end;
+                                uKeys.backspaceKey:= true;
                         end;
-                        {* reset zoom *}
-                        if (x <= 50) and (y > 430) then
+                end;
+                SDL_MOUSEBUTTONUP: begin
+                        AddFileLog('*********************************************       touch up');
+
+                        mouseState:= SDL_GetMouseState(0, @x, @y);
+                        uKeys.leftClick:= true;
+
+                        {* open ammo menu *}
+                        if (y > 430) and (x > 270) then
                         begin
 {$IFDEF DEBUGFILE}
-                                AddFileLog('Middle Click -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                                AddFileLog('Right Click -- x: ' + inttostr(x) + ' y: ' + inttostr(y) );
 {$ENDIF}
-                                uKeys.middleClick:= true;
+                                uKeys.rightClick:= true;
                         end;
-                        {* aim slider *}
-                        if (x > 230) and (y <= 430) then 
+
+                        {* long jump *}                        
+                        if (x > 50) and (x <= 160) and (y > 400) then
                         begin
 {$IFDEF DEBUGFILE}
-                                AddFileLog('Up&Down -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                                AddFileLog('Enter -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
 {$ENDIF}
-                                if oldy_aim = -1 then oldy_aim:= y
-                                else
-                                begin
-                                        if oldy_aim - y > 10 then uKeys.upKey:= true
-                                        else if oldy_aim -y < -10 then uKeys.downKey:= true;
-                                        {* update value only if movement is consistent *}
-                                        if (y > oldy_aim + 20 ) or (y < oldy_aim - 20 ) then oldy_aim:= y
-                                end;
+                                uKeys.enterKey:= true;
                         end;
+
                         {* reset zoom *}
                         if (x <= 50) and (y > 430) then
                         begin
@@ -238,53 +297,17 @@
 {$ENDIF}
                                 uKeys.middleClick:= true;
                         end;
-                        {* long jump *}                        
-                        if (y > 430) and (x > 50) and (x <= 135) then
-                        begin
-{$IFDEF DEBUGFILE}
-                                AddFileLog('Enter -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
-{$ENDIF}
-                                uKeys.enterKey:= true;
-                        end;
-                        {* high jump *}                        
-                        if (y > 430) and (x > 185) and (x <= 270) then
-                        begin
-{$IFDEF DEBUGFILE}
-                                AddFileLog('Backspace -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
-{$ENDIF}
-                                uKeys.backspaceKey:= true;
 
-                        end;
-                        {* attack *}
-                        if (y <= 50) and (x > 50) and (x <= 270) then
-                        begin
+                        {* end movement and attack *}
 {$IFDEF DEBUGFILE}
-                                AddFileLog('Space DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
+                        AddFileLog('Arrow Keys UP | Space UP -- x: ' + inttostr(x) + ' y: ' + inttostr(y) );
 {$ENDIF}
-                                uKeys.spaceKey:= true;
-                                uKeys.isAttacking:= true;
-                        end
-                        else
-                        begin
-                                AddFileLog('Space UP -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
-                                uKeys.isAttacking:= false;
-                        end;
-                end;
-                SDL_MOUSEBUTTONUP: begin
-                        AddFileLog('*********************************************       touch up');
-
-                        SDL_GetMouseState(0, @x, @y);
-                        {* open ammo menu *}
-                        if (y > 430) and (x > 270) then
-                        begin
-{$IFDEF DEBUGFILE}
-                                AddFileLog('Right Click -- x: ' + inttostr(x) + ' y: ' + inttostr(y) );
-{$ENDIF}
-                                uKeys.rightClick:= true;
-                        end;
+                        direction:= nodir;
+                        uKeys.isAttacking:= false;
                 end;
 {$ENDIF}
 {$IFDEF IPHONEOS}
+{* might be masked by above events
 		SDL_JOYAXIS: begin
                 {* axis 0 = left and right;
                    axis 1 = up and down;
@@ -293,24 +316,22 @@
                         begin
                                 if event.jaxis.value > 1500 then
                                 begin
-                                        uKeys.rightKey:= true;
-                                        uKeys.isWalking:= true;
+                                        direction:= right;
                                 end
                                 else
                                         if event.jaxis.value <= -1500 then
                                         begin 
-                                                uKeys.leftKey:= true;
-                                                uKeys.isWalking:= true;
+                                                direction:= left
                                         end
                                         else
-                                                if (event.jaxis.value  > -1500) and (event.jaxis.value <= 1500) then uKeys.isWalking:= false;
+                                                if (event.jaxis.value  > -1500) and (event.jaxis.value <= 1500) then direction:= nodir;
 {$IFDEF DEBUGFILE}
                                 AddFileLog('Joystick value: ' + inttostr(event.jaxis.value));
 {$ENDIF}
                         end;
                 
                 end;
-
+*}
 {$ELSE}
 		SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
 		SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELDUP then uKeys.wheelUp:= true;
@@ -320,7 +341,8 @@
 		SDL_JOYBUTTONUP: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
 {$ENDIF}
 		SDL_QUITEV: isTerminated:= true
-		end;
+        end;
+
 CurrTime:= SDL_GetTicks;
 if PrevTime + cTimerInterval <= CurrTime then
    begin
@@ -329,6 +351,7 @@
    end else SDL_Delay(1);
 IPCCheckSock
 until isTerminated
+
 end;
 
 /////////////////////
--- a/hedgewars/options.inc	Sun Oct 25 18:16:40 2009 +0000
+++ b/hedgewars/options.inc	Sun Oct 25 23:52:36 2009 +0000
@@ -26,4 +26,8 @@
 {$DEFINE SDL13}
 {$DEFINE GLES11}
 {$DEFINE TOUCHINPUT}
-{$ENDIF}
\ No newline at end of file
+{$ENDIF}
+
+{$IFDEF TOUCHINPUT}
+{$DEFINE SDL13}
+{$ENDIF}
--- a/hedgewars/uKeys.pas	Sun Oct 25 18:16:40 2009 +0000
+++ b/hedgewars/uKeys.pas	Sun Oct 25 23:52:36 2009 +0000
@@ -55,6 +55,7 @@
 backspaceKey: boolean = false;
 spaceKey: boolean = false;
 enterKey: boolean = false;
+tabKey: boolean = false;
 
 isAttacking: boolean = false;
 isWalking: boolean = false;
@@ -144,6 +145,7 @@
 tkbdn[26]:= ord(rightKey);
 
 tkbdn[ 8]:= ord(backspaceKey);
+tkbdn[ 9]:= ord(tabKey);
 tkbdn[13]:= ord(enterKey);
 tkbdn[32]:= ord(spaceKey);
 
@@ -153,6 +155,7 @@
 if isWalking = false then leftKey:= false;
 
 if isAttacking = false then spaceKey:= false;
+tabKey:= false;
 enterKey:= false;
 backspaceKey:= false;
 
@@ -202,7 +205,7 @@
 end;
 
 procedure ResetKbd;
-var i, j, k, t,tmp: LongInt;
+var i, j, k, t: LongInt;
     pkbd: PByteArray;
 begin
 
@@ -249,11 +252,13 @@
 tkbdn[26]:= ord(rightKey);
 
 tkbdn[ 8]:= ord(backspaceKey);
+tkbdn[ 9]:= ord(tabKey);
 tkbdn[13]:= ord(enterKey);
 tkbdn[32]:= ord(spaceKey);
 
 upKey:= false;
 downKey:= false;
+tabKey:= false;
 if isWalking = false then rightKey:= false;
 if isWalking = false then leftKey:= false;
 
@@ -304,7 +309,7 @@
 for i:= 6 to cKeyMaxIndex do
     begin
     s:= SDL_GetKeyName(i);
-	//addfilelog(inttostr(i) + ' ' + s);
+//	addfilelog(inttostr(i) + ' ' + s);
     if s = 'unknown key' then KeyNames[i]:= ''
        else begin
        for t:= 1 to Length(s) do
@@ -348,13 +353,13 @@
 DefaultBinds[  1]:= '/put';
 DefaultBinds[  3]:= 'ammomenu';
 DefaultBinds[  8]:= 'hjump';
+DefaultBinds[  9]:= 'switch';
 DefaultBinds[ 13]:= 'ljump';
-DefaultBinds[ 32]:= '+attack';
-
 DefaultBinds[ 23]:= '+up';
 DefaultBinds[ 24]:= '+down';
 DefaultBinds[ 25]:= '+left';
 DefaultBinds[ 26]:= '+right';
+DefaultBinds[ 32]:= '+attack';
 {$ENDIF}
 	
 DefaultBinds[ 27]:= 'quit';
--- a/hedgewars/uWorld.pas	Sun Oct 25 18:16:40 2009 +0000
+++ b/hedgewars/uWorld.pas	Sun Oct 25 23:52:36 2009 +0000
@@ -345,7 +345,7 @@
 {$IFDEF IPHONEOS}
 {* see the code in MainLoop *}
 SDL_GetMouseState(0, @x, @y);
-if ((x > 50) and (x <= 270) and (y > 50) and (y <= 430)) then
+if ((x > 50) and (x <= 270) and (y > 50) and (y <= 330)) then
 {$ENDIF}
 if not isPaused then MoveCamera;