Browse Source

Ball bouncing properly

tznind 1 year ago
parent
commit
d7a4e0e7c1

+ 1 - 1
UICatalog/Scenarios/Animation.cs → UICatalog/Scenarios/AnimationScenario.cs

@@ -13,7 +13,7 @@ namespace UICatalog.Scenarios;
 [ScenarioMetadata ("Animation", "Demonstration of how to render animated images with threading.")]
 [ScenarioMetadata ("Animation", "Demonstration of how to render animated images with threading.")]
 [ScenarioCategory ("Threading")]
 [ScenarioCategory ("Threading")]
 [ScenarioCategory ("Drawing")]
 [ScenarioCategory ("Drawing")]
-public class Animation : Scenario
+public class AnimationScenario : Scenario
 {
 {
     private bool _isDisposed;
     private bool _isDisposed;
 
 

+ 61 - 28
UICatalog/Scenarios/TextEffectsScenario.cs

@@ -6,14 +6,11 @@ using Terminal.Gui;
 using Terminal.Gui.TextEffects;
 using Terminal.Gui.TextEffects;
 using static UICatalog.Scenario;
 using static UICatalog.Scenario;
 
 
-
 using Color = Terminal.Gui.TextEffects.Color;
 using Color = Terminal.Gui.TextEffects.Color;
 using Animation = Terminal.Gui.TextEffects.Animation;
 using Animation = Terminal.Gui.TextEffects.Animation;
 
 
 namespace UICatalog.Scenarios;
 namespace UICatalog.Scenarios;
 
 
-
-
 [ScenarioMetadata ("Text Effects", "Text Effects.")]
 [ScenarioMetadata ("Text Effects", "Text Effects.")]
 [ScenarioCategory ("Colors")]
 [ScenarioCategory ("Colors")]
 public class TextEffectsScenario : Scenario
 public class TextEffectsScenario : Scenario
@@ -41,7 +38,7 @@ public class TextEffectsScenario : Scenario
             X = 0,
             X = 0,
             Y = 0,
             Y = 0,
             Width = Dim.Fill (),
             Width = Dim.Fill (),
-            Height = Dim.Fill () ,
+            Height = Dim.Fill (),
         };
         };
 
 
         window.Add (emptyView);
         window.Add (emptyView);
@@ -53,7 +50,7 @@ public class TextEffectsScenario : Scenario
             Y = Pos.Center (),
             Y = Pos.Center (),
             Width = 10,
             Width = 10,
             Height = 1,
             Height = 1,
-            Title = "Hello"            
+            Text = "Hello"
         };
         };
         window.Add (label);
         window.Add (label);
 
 
@@ -64,10 +61,29 @@ public class TextEffectsScenario : Scenario
 
 
 internal class TextEffectsExampleView : View
 internal class TextEffectsExampleView : View
 {
 {
+    Ball? _ball;
+    private bool resized;
+
+    protected override void OnViewportChanged (DrawEventArgs e)
+    {
+        base.OnViewportChanged (e);
+        resized = true;
+    }
+
     public override void OnDrawContent (Rectangle viewport)
     public override void OnDrawContent (Rectangle viewport)
     {
     {
         base.OnDrawContent (viewport);
         base.OnDrawContent (viewport);
 
 
+        if (
+            // First time
+            (_ball == null && viewport.Width > 0 && viewport.Height > 0)
+            || resized)
+        {
+            _ball = new Ball (this);
+            _ball.Start ();
+            resized = false;
+        }
+
         // Define the colors of the rainbow
         // Define the colors of the rainbow
         var stops = new List<Color>
         var stops = new List<Color>
         {
         {
@@ -94,24 +110,24 @@ internal class TextEffectsExampleView : View
         // Create the gradient
         // Create the gradient
         var rainbowGradient = new Gradient (stops, steps, loop: true);
         var rainbowGradient = new Gradient (stops, steps, loop: true);
 
 
-
-        for (int x = 0 ; x < viewport.Width; x++)
+        for (int x = 0; x < viewport.Width; x++)
         {
         {
             double fraction = (double)x / (viewport.Width - 1);
             double fraction = (double)x / (viewport.Width - 1);
             Color color = rainbowGradient.GetColorAtFraction (fraction);
             Color color = rainbowGradient.GetColorAtFraction (fraction);
 
 
             // Assuming AddRune is a method you have for drawing at specific positions
             // Assuming AddRune is a method you have for drawing at specific positions
             Application.Driver.SetAttribute (
             Application.Driver.SetAttribute (
-                
                 new Attribute (
                 new Attribute (
-                    new Terminal.Gui.Color(color.R, color.G, color.B),
+                    new Terminal.Gui.Color (color.R, color.G, color.B),
                     new Terminal.Gui.Color (color.R, color.G, color.B)
                     new Terminal.Gui.Color (color.R, color.G, color.B)
-                    )); // Setting color based on RGB
-
+                )); // Setting color based on RGB
 
 
             AddRune (x, 0, new Rune ('█'));
             AddRune (x, 0, new Rune ('█'));
         }
         }
+
+        _ball?.Draw ();
     }
     }
+
     public class Ball
     public class Ball
     {
     {
         public Animation Animation { get; private set; }
         public Animation Animation { get; private set; }
@@ -125,6 +141,7 @@ internal class TextEffectsExampleView : View
             Character = new EffectCharacter (1, "O", 0, 0);
             Character = new EffectCharacter (1, "O", 0, 0);
             Animation = Character.Animation;
             Animation = Character.Animation;
             CreateBouncingScene ();
             CreateBouncingScene ();
+            CreateMotionPath ();
         }
         }
 
 
         private void CreateBouncingScene ()
         private void CreateBouncingScene ()
@@ -132,23 +149,44 @@ internal class TextEffectsExampleView : View
             BouncingScene = Animation.NewScene (isLooping: true);
             BouncingScene = Animation.NewScene (isLooping: true);
             int width = Viewport.Frame.Width;
             int width = Viewport.Frame.Width;
             int height = Viewport.Frame.Height;
             int height = Viewport.Frame.Height;
-            double frequency = 2 * Math.PI / width;
+            double frequency = 4 * Math.PI / width; // Double the frequency
 
 
             for (int x = 0; x < width; x++)
             for (int x = 0; x < width; x++)
             {
             {
-                int y = (int)((height - 1) / 2 * (1 + Math.Sin (frequency * x)));
+                int y = (int)((height) / 2 * (1 + Math.Sin (frequency * x))); // Decrease amplitude
                 BouncingScene.AddFrame ("O", 1);
                 BouncingScene.AddFrame ("O", 1);
-                BouncingScene.Frames [BouncingScene.Frames.Count - 1].CharacterVisual.Position = new Coord (x, y);
             }
             }
 
 
             for (int x = width - 1; x >= 0; x--)
             for (int x = width - 1; x >= 0; x--)
             {
             {
-                int y = (int)((height - 1) / 2 * (1 + Math.Sin (frequency * x)));
+                int y = (int)((height) / 2 * (1 + Math.Sin (frequency * x))); // Decrease amplitude
                 BouncingScene.AddFrame ("O", 1);
                 BouncingScene.AddFrame ("O", 1);
-                BouncingScene.Frames [BouncingScene.Frames.Count - 1].CharacterVisual.Position = new Coord (x, y);
             }
             }
         }
         }
 
 
+        private void CreateMotionPath ()
+        {
+            int width = Viewport.Frame.Width;
+            int height = Viewport.Frame.Height;
+            double frequency = 4 * Math.PI / width; // Double the frequency
+
+            var path = Character.Motion.CreatePath ("sineWavePath", speed: 1, loop: true);
+
+            for (int x = 0; x < width; x++)
+            {
+                int y = (int)((height) / 2 * (1 + Math.Sin (frequency * x))); // Decrease amplitude
+                path.AddWaypoint (new Waypoint ($"waypoint_{x}", new Coord (x, y)));
+            }
+
+            for (int x = width - 1; x >= 0; x--)
+            {
+                int y = (int)((height) / 2 * (1 + Math.Sin (frequency * x))); // Decrease amplitude
+                path.AddWaypoint (new Waypoint ($"waypoint_{x}", new Coord (x, y)));
+            }
+
+            Character.Motion.ActivatePath (path);
+        }
+
         public void Start ()
         public void Start ()
         {
         {
             Animation.ActivateScene (BouncingScene);
             Animation.ActivateScene (BouncingScene);
@@ -156,24 +194,19 @@ internal class TextEffectsExampleView : View
             {
             {
                 while (true)
                 while (true)
                 {
                 {
-                    Draw ();
-                    Thread.Sleep (100); // Adjust the speed of animation
-                    Animation.StepAnimation ();
+                    Thread.Sleep (10); // Adjust the speed of animation
+                    Character.Tick ();
+
+                    Application.Invoke (() => Viewport.SetNeedsDisplay ());
                 }
                 }
             })
             })
             { IsBackground = true }.Start ();
             { IsBackground = true }.Start ();
         }
         }
 
 
-        private void Draw ()
+        public void Draw ()
         {
         {
-            var characterVisual = Animation.CurrentCharacterVisual;
-            var coord = characterVisual.Position;
-            Application.MainLoop.Invoke (() =>
-            {
-                Viewport.Clear ();
-                Viewport.AddRune (coord.X, coord.Y, new Rune ('O'));
-                Application.Refresh ();
-            });
+            Driver.SetAttribute (Viewport.ColorScheme.Normal);
+            Viewport.AddRune (Character.Motion.CurrentCoord.Column, Character.Motion.CurrentCoord.Row, new Rune ('O'));
         }
         }
     }
     }
 }
 }