small refactoring to touch screen buttons, use a record to store values, added 'offset' fields to adjust active button area
authorkoda
Wed, 15 Feb 2012 16:23:59 +0100
changeset 6683 75a1d84ac606
parent 6682 bf3cf6e60673
child 6684 112a0eb8782b
small refactoring to touch screen buttons, use a record to store values, added 'offset' fields to adjust active button area
hedgewars/uTouch.pas
hedgewars/uTypes.pas
hedgewars/uVariables.pas
hedgewars/uWorld.pas
--- a/hedgewars/uTouch.pas	Mon Feb 13 08:18:59 2012 +0100
+++ b/hedgewars/uTouch.pas	Wed Feb 15 16:23:59 2012 +0100
@@ -63,7 +63,8 @@
 function fingerHasMoved(finger: Touch_Finger): boolean;
 function calculateDelta(finger1, finger2: Touch_Finger): hwFloat;
 function getSecondFinger(finger: Touch_Finger): PTouch_Finger;
-function isOnRect(x,y,w,h: LongInt; finger: Touch_Finger): boolean;
+function isOnRect(widget: TOnScreenWidget; finger: Touch_Finger): boolean;
+function isOnRect(rect: TSDL_Rect; finger: Touch_Finger): boolean;
 procedure printFinger(finger: Touch_Finger);
 implementation
 
@@ -111,43 +112,43 @@
                 exit;
             end;
 
-            if isOnRect(fireButtonX, fireButtonY, fireButtonW, fireButtonH, finger^) then
+            if isOnRect(fireButton, finger^) then
             begin
                 stopFiring:= false;
                 spaceKey:= true;
                 exit;
             end;
-            if isOnRect(arrowLeftX, arrowLeftY, arrowLeftW, arrowLeftH, finger^) then
+            if isOnRect(arrowLeft, finger^) then
             begin
                 leftKey:= true;
                 walkingLeft := true;
                 exit;
             end;
-            if isOnRect(arrowRightX, arrowRightY, arrowRightW, arrowRightH, finger^) then
+            if isOnRect(arrowRight, finger^) then
             begin
                 rightKey:= true;
                 walkingRight:= true;
                 exit;
             end;
-            if isOnRect(arrowUpX, arrowUpY, arrowUpW, arrowUpH, finger^) then
+            if isOnRect(arrowUp, finger^) then
             begin
                 upKey:= true;
                 aimingUp:= true;
                 exit;
             end;
-            if isOnRect(arrowDownX, arrowDownY, arrowUpW, arrowUpH, finger^) then
+            if isOnRect(arrowDown, finger^) then
             begin
                 downKey:= true;
                 aimingDown:= true;
                 exit;
             end;
 
-            if isOnRect(backjumpX, backjumpY, backjumpW, backjumpH, finger^) then
+            if isOnRect(backjump, finger^) then
             begin
                 enterKey:= true;
                 exit;
             end;
-            if isOnRect(forwardjumpX, forwardjumpY, forwardjumpW, forwardjumpH, finger^) then
+            if isOnRect(forwardjump, finger^) then
             begin
                 backspaceKey:= true;
                 exit;
@@ -267,7 +268,7 @@
 
 if bShowAmmoMenu then
     begin 
-    if isOnRect(AmmoRect.x, AmmoRect.y, AmmoRect.w, AmmoRect.h, finger) then
+    if isOnRect(AmmoRect, finger) then
         begin
         CursorPoint.X:= finger.x;
         CursorPoint.Y:= finger.y;
@@ -549,12 +550,25 @@
         getSecondFinger := @fingers[0];
 end;
 
-function isOnRect(x,y,w,h: LongInt; finger: Touch_Finger): boolean;
+function isOnRect(rect: TSDL_Rect; finger: Touch_Finger): boolean;
+var widget: TOnScreenWidget;
 begin
-isOnRect:= (finger.x > x)   and
-           (finger.x < x+w) and
-           (cScreenHeight - finger.y > y)   and
-           (cScreenHeight - finger.y < (y+h));
+	widget.x:= rect.x;
+	widget.y:= rect.y;
+	widget.width:= rect.width;
+	widget.height:= rect.height;
+	widget.hOffset:= 0;
+	widget.vOffset:= 0;
+	exit(isOnRect(widget, finger));
+end;
+
+function isOnRect(widget: TOnScreenWidget; finger: Touch_Finger): boolean;
+begin
+with widget do
+	isOnRect:= (finger.x > x + hOffset)   and
+               (finger.x < x + w + hOffset) and
+               (cScreenHeight - finger.y > y + vOffset)   and
+               (cScreenHeight - finger.y < y + h + vOffset));
 end;
 
 procedure printFinger(finger: Touch_Finger);
--- a/hedgewars/uTypes.pas	Mon Feb 13 08:18:59 2012 +0100
+++ b/hedgewars/uTypes.pas	Wed Feb 15 16:23:59 2012 +0100
@@ -423,6 +423,11 @@
     TPreview  = packed array[0..127, 0..31] of byte;
     TDirtyTag = packed array of array of byte;
 
+	TOnScreenWidget = record
+		x, y: LongInt;			 	// graphical coordinates
+		hOffset, width: LongInt;	// horizontal active region
+		vOffset, height: LongInt;	// vertical active region
+		end;
 
 implementation
 
--- a/hedgewars/uVariables.pas	Mon Feb 13 08:18:59 2012 +0100
+++ b/hedgewars/uVariables.pas	Wed Feb 15 16:23:59 2012 +0100
@@ -155,7 +155,7 @@
     WeaponTooltipTex: PTexture;
     AmmoMenuTex     : PTexture;
     AmmoMenuInvalidated: boolean;
-    AmmoRect: TSDL_Rect;
+    AmmoRect		: TSDL_Rect;
     HHTexture       : PTexture;
 
 
@@ -198,14 +198,8 @@
 {$IFDEF USE_TOUCH_INTERFACE}
     buttonScale: GLFloat;
 
-    arrowUpX, arrowUpY, arrowUpW, arrowUpH: LongInt; 
-    arrowDownX, arrowDownY, arrowDownW, arrowDownH: LongInt; 
-    arrowLeftX, arrowLeftY, arrowLeftW, arrowLeftH: LongInt; 
-    arrowRightX, arrowRightY, arrowRightW, arrowRightH: LongInt; 
-
-    firebuttonX, fireButtonY, fireButtonW, fireButtonH: LongInt; 
-    backjumpX, backjumpY, backjumpW, backjumpH: LongInt; 
-    forwardjumpX, forwardjumpY, forwardjumpW, forwardjumpH: LongInt; 
+    arrowUp, arrowDown, arrowLeft, arrowRight: TOnScreenWidget; 
+    firebutton, backjump, forwardjump: TOnScreenWidget; 
 {$ENDIF}
 const
     cHHFileName = 'Hedgehog';
--- a/hedgewars/uWorld.pas	Mon Feb 13 08:18:59 2012 +0100
+++ b/hedgewars/uWorld.pas	Wed Feb 15 16:23:59 2012 +0100
@@ -211,40 +211,56 @@
 {$IFDEF USE_TOUCH_INTERFACE}
 //positioning of the buttons
 buttonScale:= 1.5/cDefaultZoomLevel;
-firebuttonX:= (cScreenWidth shr 1) - Round(spritesData[sprFireButton].Texture^.h * buttonScale);
-firebuttonY:= -Round(spritesData[sprFireButton].Texture^.h* buttonScale) + cScreenHeight;
-firebuttonW:= Round(spritesData[sprFireButton].Texture^.w * buttonScale);
-firebuttonH:= Round(spritesData[sprFireButton].Texture^.h * buttonScale);
+
+//TODO: add sensible hOffset and vOffset values
+firebutton.x:= (cScreenWidth shr 1) - Round(spritesData[sprFireButton].Texture^.h * buttonScale);
+firebutton.y:= -Round(spritesData[sprFireButton].Texture^.h* buttonScale) + cScreenHeight;
+firebutton.width:= Round(spritesData[sprFireButton].Texture^.w * buttonScale);
+firebutton.height:= Round(spritesData[sprFireButton].Texture^.h * buttonScale);
+firebutton.hOffset:= 0;
+firebutton.vOffset:= 0;
 
-backjumpX:= (cScreenWidth shr 1) - Round(spritesData[sprbackjump].Texture^.h * 2 * buttonScale);
-backjumpY:= -Round(spritesData[sprbackjump].Texture^.h * 1.2 *  buttonScale) + cScreenHeight;
-backjumpW:= Round(spritesData[sprbackjump].Texture^.w * buttonScale);
-backjumpH:= Round(spritesData[sprbackjump].Texture^.h * buttonScale);
+backjump.x:= (cScreenWidth shr 1) - Round(spritesData[sprbackjump].Texture^.h * 2 * buttonScale);
+backjump.y:= -Round(spritesData[sprbackjump].Texture^.h * 1.2 *  buttonScale) + cScreenHeight;
+backjump.width:= Round(spritesData[sprbackjump].Texture^.w * buttonScale);
+backjump.height:= Round(spritesData[sprbackjump].Texture^.h * buttonScale);
+backjump.hOffset:= 0;
+backjump.vOffset:= 0;
 
-forwardjumpX:= (cScreenWidth shr 1) - Round(spritesData[sprforwardjump].Texture^.h * 1.2 * buttonScale);
-forwardjumpY:= -Round(spritesData[sprforwardjump].Texture^.h * 2 * buttonScale) + cScreenHeight;
-forwardjumpW:= Round(spritesData[sprforwardjump].Texture^.w * buttonScale);
-forwardjumpH:= Round(spritesData[sprforwardjump].Texture^.h * buttonScale);
+forwardjump.x:= (cScreenWidth shr 1) - Round(spritesData[sprforwardjump].Texture^.h * 1.2 * buttonScale);
+forwardjump.y:= -Round(spritesData[sprforwardjump].Texture^.h * 2 * buttonScale) + cScreenHeight;
+forwardjump.width:= Round(spritesData[sprforwardjump].Texture^.w * buttonScale);
+forwardjump.height:= Round(spritesData[sprforwardjump].Texture^.h * buttonScale);
+forwardjump.hOffset:= 0;
+forwardjump.vOffset:= 0;
 
-arrowLeftX:= -(cScreenWidth shr 1);
-arrowLeftY:= -Round(spritesData[sprArrowLeft].Texture^.h*buttonScale) + cScreenHeight  - Round(spritesData[sprArrowLeft].Texture^.h*buttonScale);
-arrowLeftW:= Round(spritesData[sprArrowLeft].Texture^.h * buttonScale);
-arrowLeftH:= Round(spritesData[sprArrowLeft].Texture^.h * buttonScale);
+arrowLeft.x:= -(cScreenWidth shr 1);
+arrowLeft.y:= -Round(spritesData[sprArrowLeft].Texture^.h*buttonScale) + cScreenHeight  - Round(spritesData[sprArrowLeft].Texture^.h*buttonScale);
+arrowLeft.width:= Round(spritesData[sprArrowLeft].Texture^.h * buttonScale);
+arrowLeft.height:= Round(spritesData[sprArrowLeft].Texture^.h * buttonScale);
+arrowLeft.hOffset:= 0;
+arrowLeft.vOffset:= 0;
 
-arrowRightX:= -(cScreenWidth shr 1)+ Round(spritesData[sprArrowUp].Texture^.h * buttonScale *2);
-arrowRightY:= -Round(spritesData[sprArrowRight].Texture^.h*buttonScale) + cScreenHeight - Round(spritesData[sprArrowRight].Texture^.h*buttonScale);
-arrowRightW:= Round(spritesData[sprArrowRight].Texture^.w * buttonScale);
-arrowRightH:= Round(spritesData[sprArrowRight].Texture^.h * buttonScale);
+arrowRight.x:= -(cScreenWidth shr 1)+ Round(spritesData[sprArrowUp].Texture^.h * buttonScale *2);
+arrowRight.y:= -Round(spritesData[sprArrowRight].Texture^.h*buttonScale) + cScreenHeight - Round(spritesData[sprArrowRight].Texture^.h*buttonScale);
+arrowRight.width:= Round(spritesData[sprArrowRight].Texture^.w * buttonScale);
+arrowRight.height:= Round(spritesData[sprArrowRight].Texture^.h * buttonScale);
+arrowRight.hOffset:= 0;
+arrowRight.vOffset:= 0;
 
-arrowUpX:= -(cScreenWidth shr 1) + Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
-arrowUpY:= -Round(spritesData[sprArrowUp].Texture^.h*buttonScale) + cScreenHeight - Round(spritesData[sprArrowUp].Texture^.h*buttonScale*2);
-arrowUpW:= Round(spritesData[sprArrowUp].Texture^.w * buttonScale);
-arrowUpH:= Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
+arrowUp.x:= -(cScreenWidth shr 1) + Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
+arrowUp.y:= -Round(spritesData[sprArrowUp].Texture^.h*buttonScale) + cScreenHeight - Round(spritesData[sprArrowUp].Texture^.h*buttonScale*2);
+arrowUp.width:= Round(spritesData[sprArrowUp].Texture^.w * buttonScale);
+arrowUp.height:= Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
+arrowUp.hOffset:= 0;
+arrowUp.vOffset:= 0;
 
-arrowDownX:= -(cScreenWidth shr 1) + Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
-arrowDownY:= -Round(spritesData[sprArrowDown].Texture^.h*buttonscale) + cScreenHeight;
-arrowDownW:= Round(spritesData[sprArrowDown].Texture^.w * buttonScale);
-arrowDownH:= Round(spritesData[sprArrowDown].Texture^.h * buttonScale);
+arrowDown.x:= -(cScreenWidth shr 1) + Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
+arrowDown.y:= -Round(spritesData[sprArrowDown].Texture^.h*buttonscale) + cScreenHeight;
+arrowDown.width:= Round(spritesData[sprArrowDown].Texture^.w * buttonScale);
+arrowDown.height:= Round(spritesData[sprArrowDown].Texture^.h * buttonScale);
+arrowDown.hOffset:= 0;
+arrowDown.vOffset:= 0;
 {$ENDIF}
 end;
 
@@ -1095,14 +1111,14 @@
 
 {$IFDEF USE_TOUCH_INTERFACE}
 // Draw buttons Related to the Touch interface
-DrawTexture(arrowLeftX, arrowLeftY,spritesData[sprArrowLeft].Texture, buttonScale);
-DrawTexture(arrowRightX, arrowRightY,spritesData[sprArrowRight].Texture, buttonScale);
-DrawTexture(arrowUpX, arrowUpY,spritesData[sprArrowUp].Texture, buttonScale);
-DrawTexture(arrowDownX, arrowDownY,spritesData[sprArrowDown].Texture, buttonScale);
+DrawTexture(arrowLeft.x, arrowLeft.y, spritesData[sprArrowLeft].Texture, buttonScale);
+DrawTexture(arrowRight.x, arrowRight.y, spritesData[sprArrowRight].Texture, buttonScale);
+DrawTexture(arrowUp.x, arrowUp.y, spritesData[sprArrowUp].Texture, buttonScale);
+DrawTexture(arrowDown.x, arrowDown.y, spritesData[sprArrowDown].Texture, buttonScale);
 
-DrawTexture(fireButtonX, firebuttonY, spritesData[sprFireButton].Texture, buttonScale);
-DrawTexture(backjumpX, backjumpY, spritesData[sprBackjump].Texture, buttonScale);
-DrawTexture(forwardjumpX, forwardjumpY, spritesData[sprForwardjump].Texture, buttonScale);
+DrawTexture(fireButton.x, firebutton.y, spritesData[sprFireButton].Texture, buttonScale);
+DrawTexture(backjump.x, backjump.y, spritesData[sprBackjump].Texture, buttonScale);
+DrawTexture(forwardjump.x, forwardjump.y, spritesData[sprForwardjump].Texture, buttonScale);
 {$ENDIF}
 
 // Teams Healths