added translate/fade animations for OnScreenWidgets
authorXeli
Thu, 16 Feb 2012 16:02:41 +0100
changeset 6688 b2084a00a4cf
parent 6687 98be933770e0
child 6689 98a239848ba2
added translate/fade animations for OnScreenWidgets
hedgewars/uConsts.pas
hedgewars/uRender.pas
hedgewars/uTypes.pas
hedgewars/uWorld.pas
--- a/hedgewars/uConsts.pas	Wed Feb 15 20:59:00 2012 +0100
+++ b/hedgewars/uConsts.pas	Thu Feb 16 16:02:41 2012 +0100
@@ -296,6 +296,11 @@
     AMSlotSize = 32;
 {$ENDIF}
     AMSlotPadding = (AMSlotSize - 32) shr 1;
+
+{$IFDEF USE_TOUCH_INTERFACE}
+    FADE_ANIM_TIME = 500;
+    MOVE_ANIM_TIME = 500;
+{$ENDIF}
 implementation
 
 end.
--- a/hedgewars/uRender.pas	Wed Feb 15 20:59:00 2012 +0100
+++ b/hedgewars/uRender.pas	Thu Feb 16 16:02:41 2012 +0100
@@ -42,6 +42,7 @@
 procedure DrawCircle(X, Y, Radius, Width: LongInt; r, g, b, a: Byte);
 procedure DrawCircle(X, Y, Radius, Width: LongInt);
 procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
+procedure DrawScreenWidget(widget: POnScreenWidget);
 procedure Tint(r, g, b, a: Byte); inline;
 procedure Tint(c: Longword); inline;
 
@@ -447,6 +448,44 @@
     glPopMatrix
 end;
 
+procedure DrawScreenWidget(widget: POnScreenWidget);
+var alpha: byte = $FF;
+begin
+with widget^ do
+    begin
+    if (fadeAnimStart <> 0) then
+        begin
+        if RealTicks > (fadeAnimStart + FADE_ANIM_TIME) then
+            fadeAnimStart:= 0
+        else
+            if show then 
+                alpha:= Byte(trunc((RealTicks - fadeAnimStart)/FADE_ANIM_TIME * $FF))
+            else 
+                alpha:= Byte($FF - trunc((RealTicks - fadeAnimStart)/FADE_ANIM_TIME * $FF));
+        end;
+
+    with moveAnim do
+        if animate then
+            if RealTicks > (startTime + MOVE_ANIM_TIME) then
+                begin
+                startTime:= 0;
+                x:= targetToX;
+                y:= targetToY;
+                end
+            else
+                begin
+                x:= targetFromX + Round((targetToX - targetFromX) * ((RealTicks - startTime) / MOVE_ANIM_TIME));
+                y:= targetFromY + Round((targetToY - targetFromY) * ((RealTicks - startTime) / MOVE_ANIM_TIME));
+                end;
+
+    if show or (fadeAnimStart <> 0) then
+        begin
+        Tint($FF, $FF, $FF, alpha);
+        DrawTexture(x, y, spritesData[sprite].Texture, buttonScale);
+        Tint($FF, $FF, $FF, $FF);
+        end;
+    end;
+end;
 
 procedure Tint(r, g, b, a: Byte); inline;
 const 
--- a/hedgewars/uTypes.pas	Wed Feb 15 20:59:00 2012 +0100
+++ b/hedgewars/uTypes.pas	Thu Feb 16 16:02:41 2012 +0100
@@ -423,10 +423,23 @@
     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
+    PWidgetMovement = ^TWidgetMovement;
+    TWidgetMovement = record
+        animate: Boolean;
+        targetFromX, targetFromY: LongInt;
+        targetToX, targetToY: LongInt;
+        startTime: Longword;
+        end;
+
+    POnScreenWidget = ^TOnScreenWidget;
+    TOnScreenWidget = record
+        show: boolean;
+        sprite: TSprite;
+        x, y: LongInt;	                // graphical coordinates
+        hOffset, width: LongInt;        // horizontal active region
+        vOffset, height: LongInt;       // vertical active region
+        fadeAnimStart: Longword;        //time the fade started, 0 means don't fade
+        moveAnim: TWidgetMovement;
         end;
 
 implementation
--- a/hedgewars/uWorld.pas	Wed Feb 15 20:59:00 2012 +0100
+++ b/hedgewars/uWorld.pas	Thu Feb 16 16:02:41 2012 +0100
@@ -36,6 +36,7 @@
 procedure ShakeCamera(amount: LongInt);
 procedure InitCameraBorders;
 procedure InitTouchInterface;
+procedure animateWidget(widget: POnScreenWidget; fade, showWidget: boolean);
 procedure MoveCamera;
 procedure onFocusStateChanged;
 
@@ -219,6 +220,8 @@
 firebutton.height:= Round(spritesData[sprFireButton].Texture^.h * buttonScale);
 firebutton.hOffset:= 0;
 firebutton.vOffset:= 0;
+firebutton.sprite:= sprFireButton;
+firebutton.show:= true;
 
 backjump.x:= (cScreenWidth shr 1) - Round(spritesData[sprbackjump].Texture^.h * 2 * buttonScale);
 backjump.y:= -Round(spritesData[sprbackjump].Texture^.h * 1.2 *  buttonScale) + cScreenHeight;
@@ -226,6 +229,8 @@
 backjump.height:= Round(spritesData[sprbackjump].Texture^.h * buttonScale);
 backjump.hOffset:= 0;
 backjump.vOffset:= 0;
+backjump.sprite:= sprbackjump;
+backjump.show:= true;
 
 forwardjump.x:= (cScreenWidth shr 1) - Round(spritesData[sprforwardjump].Texture^.h * 1.2 * buttonScale);
 forwardjump.y:= -Round(spritesData[sprforwardjump].Texture^.h * 2 * buttonScale) + cScreenHeight;
@@ -233,34 +238,76 @@
 forwardjump.height:= Round(spritesData[sprforwardjump].Texture^.h * buttonScale);
 forwardjump.hOffset:= 0;
 forwardjump.vOffset:= 0;
+forwardjump.sprite:= sprforwardjump;
+forwardjump.show:= true;
 
-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;
+with arrowLeft do
+    begin
+    show:= true;
+    sprite:= sprArrowLeft;
+    x:= -(cScreenWidth shr 1);
+    y:= -Round(spritesData[sprite].Texture^.h*buttonScale) + cScreenHeight  - Round(spritesData[sprite].Texture^.h*buttonScale);
+    width:= Round(spritesData[sprite].Texture^.h * buttonScale);
+    height:= Round(spritesData[sprite].Texture^.h * buttonScale);
+    hOffset:= 0;
+    vOffset:= 0;
+    end;
+with arrowRight do
+    begin
+    show:= true;
+    sprite:= sprArrowRight;
+    x:= -(cScreenWidth shr 1)+ Round(spritesData[sprite].Texture^.h * buttonScale);
+    y:= -Round(spritesData[sprite].Texture^.h*buttonScale) + cScreenHeight - Round(spritesData[sprite].Texture^.h*buttonScale);
+    width:= Round(spritesData[sprite].Texture^.w * buttonScale);
+    height:= Round(spritesData[sprite].Texture^.h * buttonScale);
+    hOffset:= 0;
+    vOffset:= 0;
+    with moveAnim do
+        begin
+        targetToX:= arrowRight.x;
+        targetToY:= arrowRight.y;
+        targetFromX:= arrowRight.x + Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
+        targetFromY:= arrowRight.y;
+        end;
+    end;
 
-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;
+with arrowUp do
+    begin
+    show:= false;
+    sprite:= sprArrowUp;
+    x:= -(cScreenWidth shr 1) + Round(spritesData[sprite].Texture^.h * buttonScale);
+    y:= arrowRight.y;
+    width:= Round(spritesData[sprite].Texture^.w * buttonScale);
+    height:= Round(spritesData[sprite].Texture^.h * buttonScale);
+    hOffset:= 0;
+    vOffset:= 0;
+    with moveAnim do
+         begin
+         targetToX:= arrowUp.x;
+         targetToY:= arrowUp.y;
+         targetFromX:= arrowUp.x;
+         targetFromY:= -Round(spritesData[sprite].Texture^.h*buttonScale) + cScreenHeight - Round(spritesData[sprite].Texture^.h*buttonScale*2);
+         end;
+    end;
 
-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;
-
-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;
+with arrowDown do
+    begin
+    show:= false;
+    sprite:= sprArrowDown;
+    x:= -(cScreenWidth shr 1) + Round(spritesData[sprArrowUp].Texture^.h * buttonScale);
+    y:= arrowRight.y;
+    width:= Round(spritesData[sprArrowDown].Texture^.w * buttonScale);
+    height:= Round(spritesData[sprArrowDown].Texture^.h * buttonScale);
+    hOffset:= 0;
+    vOffset:= 0;
+    with moveAnim do
+        begin
+        targetToX:= arrowDown.x;
+        targetToY:= arrowDown.y;
+        targetFromX:= arrowDown.x;
+        targetFromY:= -Round(spritesData[sprArrowDown].Texture^.h*buttonscale) + cScreenHeight;
+        end;
+    end;
 
 pauseButton.x:= cScreenWidth div 2 - Round(spritesData[sprPauseButton].Texture^.w * buttonscale);
 pauseButton.y:= 0; //-Round(spritesData[sprPauseButton].Texture^.h * buttonscale);
@@ -268,6 +315,8 @@
 pauseButton.height:= Round(spritesData[sprPauseButton].Texture^.h * buttonScale);;
 pauseButton.hOffset:= 0;
 pauseButton.vOffset:= 0;
+pauseButton.sprite:= sprPauseButton;
+pauseButton.show:= true;
 
 {$ENDIF}
 end;
@@ -569,6 +618,7 @@
                 SetWeapon(Ammo^[Slot, Pos].AmmoType);
                 bSelected:= false;
                 FreeWeaponTooltip;
+
                 exit
                 end;
             end
@@ -1119,6 +1169,16 @@
 
 {$IFDEF USE_TOUCH_INTERFACE}
 // Draw buttons Related to the Touch interface
+DrawScreenWidget(@arrowLeft);
+DrawScreenWidget(@arrowRight);
+DrawScreenWidget(@arrowUp);
+DrawScreenWidget(@arrowDown);
+
+DrawScreenWidget(@fireButton);
+DrawScreenWidget(@backjump);
+DrawScreenWidget(@forwardjump);
+DrawScreenWidget(@pauseButton);
+{
 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);
@@ -1128,7 +1188,7 @@
 DrawTexture(backjump.x, backjump.y, spritesData[sprBackjump].Texture, buttonScale);
 DrawTexture(forwardjump.x, forwardjump.y, spritesData[sprForwardjump].Texture, buttonScale);
 DrawTexture(pauseButton.x, pauseButton.y, spritesData[sprPauseButton].Texture, buttonScale);
-{$ENDIF}
+}{$ENDIF}
 
 // Teams Healths
 if TeamsCount * 20 > Longword(cScreenHeight) div 7 then  // take up less screen on small displays
@@ -1569,6 +1629,27 @@
     ParseCommand('quit', true);
 end;
 
+procedure animateWidget(widget: POnScreenWidget; fade, showWidget: boolean);
+begin
+with widget^ do
+    begin
+    show:= showWidget;
+    if fade then fadeAnimStart:= RealTicks;
+    
+    with moveAnim do
+        begin
+        animate:= true;
+        startTime:= RealTicks;
+        targetFromX:= targetFromX xor targetToX;//swap From <-> To
+        targetToX:= targetFromX xor targetToX;
+        targetFromX:= targetFromX xor targetToX;
+        targetFromY:= targetFromY xor targetToY;
+        targetToY:= targetFromY xor targetToY;
+        targetFromY:= targetFromY xor targetToY;
+        end;
+    end;
+end;
+
 
 procedure initModule;
 begin