Tobias Bohnen avatar Tobias Bohnen committed 49e410a

* moved some methods from UISprite to Sprite

Comments (0)

Files changed (2)

Projects/Miyagi/Common/Rendering/Sprite.cs

         #region Public Methods
 
         /// <summary>
+        /// Applies the transformation.
+        /// </summary>
+        public void ApplyTransformation()
+        {
+            this.ForEachQuad(q => q.ApplyTransformation(this.ViewportAspectRatio));
+            this.SpriteRenderer.BufferDirty = true;
+        }
+
+        /// <summary>
         /// Gets the quad at the specified index.
         /// </summary>
         /// <param name="index">The index.</param>
         /// </returns>
         public bool HitTest(Point p)
         {
-            float x = (((float)p.X / this.ViewportSize.Width) * 2) - 1;
-            float y = -((((float)p.Y / this.ViewportSize.Height) * 2) - 1);
+            var pf = p.ToScreenCoordinates(this.ViewportSize);
+            return this.GetQuadAt(pf.X, pf.Y) != null;
+        }
 
-            return this.HitTest(x, y) != null;
+        /// <summary>
+        /// Performs a hit test.
+        /// </summary>
+        /// <param name="p">The coordinate.</param>
+        /// <param name="pixelPerfect">if set to <c>true</c> performs a pixel-perfect hit test.</param>
+        /// <returns>
+        /// <c>true</c> if the quad is hit; otherwise, <c>false</c>.
+        /// </returns>
+        public bool HitTest(Point p, bool pixelPerfect)
+        {
+            if (!pixelPerfect)
+            {
+                return this.HitTest(p);
+            }
+
+            var pf = p.ToScreenCoordinates(this.ViewportSize);
+            return this.PixelPerfectHitTest(pf.X, pf.Y);
         }
 
         /// <summary>
         }
 
         /// <summary>
+        /// Rotates the sprite.
+        /// </summary>
+        /// <param name="angle">The rotation angle in degrees.</param>
+        /// <param name="rotationPivot">The pivot point.</param>
+        /// <param name="doTransform">Indicates whether the rotation should be applied directly.</param>
+        public void Rotate(float angle, Point rotationPivot, bool doTransform)
+        {
+            PointF pivot = rotationPivot.ToScreenCoordinates(this.ViewportSize);
+            this.ForEachQuad(q => q.Rotate(angle, pivot));
+            if (doTransform)
+            {
+                this.ApplyTransformation();
+            }
+        }
+
+        /// <summary>
+        /// Scales the sprite.
+        /// </summary>
+        /// <param name="scaleFactor">The scale factor..</param>
+        /// <param name="scalePivot">The pivot point.</param>
+        /// <param name="doTransform">Indicates whether the scaling should be applied directly.</param>
+        public void Scale(PointF scaleFactor, Point scalePivot, bool doTransform)
+        {
+            PointF pivot = scalePivot.ToScreenCoordinates(this.ViewportSize);
+            this.ForEachQuad(q => q.Scale(scaleFactor, pivot));
+            if (doTransform)
+            {
+                this.ApplyTransformation();
+            }
+        }
+
+        /// <summary>
         /// Sets the colour.
         /// </summary>
         /// <param name="colourDefinition">The colour definition.</param>
         public void SetTexture(string texName)
         {
             this.TextureHandle = !string.IsNullOrEmpty(texName)
-                                     ? this.Backend.LoadTexture(texName) : RenderManager.TransparentTextureHandle;
+                ? this.Backend.LoadTexture(texName) : RenderManager.TransparentTextureHandle;
         }
 
         /// <summary>
             }
         }
 
+        /// <summary>
+        /// Skews the sprite.
+        /// </summary>
+        /// <param name="skewFactor">The skew factor.</param>
+        /// <param name="doTransform">Indicates whether the skewing should be applied directly.</param>
+        public void Skew(PointF skewFactor, bool doTransform)
+        {
+            this.ForEachQuad(q => q.Skew(skewFactor));
+            if (doTransform)
+            {
+                this.ApplyTransformation();
+            }
+        }
+
+        /// <summary>
+        /// Translates the sprite.
+        /// </summary>
+        /// <param name="offset">The offset.</param>
+        /// <param name="doTransform">Indicates whether the translation should be applied directly.</param>
+        public void Translate(Point offset, bool doTransform)
+        {
+            var off = new PointF(
+                ((float)offset.X / this.ViewportSize.Width) * 2,
+                ((float)offset.Y / this.ViewportSize.Height) * 2);
+
+            this.ForEachQuad(q => q.Translate(off));
+            if (doTransform)
+            {
+                this.ApplyTransformation();
+            }
+        }
+
         #endregion Public Methods
 
         #region Protected Methods
         /// <param name="x">The x-coordinate.</param>
         /// <param name="y">The y-coordinate.</param>
         /// <returns><c>true</c> if the quad is hit; otherwise, <c>false</c>.</returns>
-        protected Quad HitTest(float x, float y)
+        protected Quad GetQuadAt(float x, float y)
         {
             for (int i = 0; i < this.quads.Length; i++)
             {
 
         #endregion Protected Methods
 
+        #region Private Methods
+
+        private bool PixelPerfectHitTest(float x, float y)
+        {
+            var quad = (Quad)this.GetQuadAt(x, y);
+
+            if (quad != null)
+            {
+                x -= quad.Left;
+                y -= quad.Top;
+                float x1 = (x / 2) * this.ViewportSize.Width;
+                float y1 = (-y / 2) * this.ViewportSize.Height;
+                float width = quad.Width * this.ViewportSize.Width / 2;
+                float height = quad.Height * this.ViewportSize.Height / 2;
+                var size = new SizeF(width, height);
+                var uvRect = RectangleF.FromLTRB(quad.RenderVertexUV[0].X, quad.RenderVertexUV[0].Y, quad.RenderVertexUV[2].X, quad.RenderVertexUV[2].Y);
+                var texSize = this.Backend.GetTextureSize(this.TextureHandle);
+                var left = (int)((((texSize.Width / size.Width) * uvRect.Width) * x1) + (uvRect.Left * texSize.Width));
+                var top = (int)((((texSize.Height / size.Height) * uvRect.Height) * y1) + (uvRect.Top * texSize.Height));
+
+                return this.Backend.GetTextureAlpha(this.TextureHandle, new Point(left, top)) > 0.01f;
+            }
+
+            return false;
+        }
+
+        #endregion Private Methods
+
         #endregion Methods
 
         #region Nested Types

Projects/Miyagi/UI/UISprite.cs

         #region Public Methods
 
         /// <summary>
-        /// Applies the transformation.
-        /// </summary>
-        public void ApplyTransformation()
-        {
-            this.ForEachQuad(q => q.ApplyTransformation(this.ViewportAspectRatio));
-            this.SpriteRenderer.BufferDirty = true;
-        }
-
-        /// <summary>
-        /// Performs a hit test.
-        /// </summary>
-        /// <param name="p">The coordinate.</param>
-        /// <param name="pixelPerfect">if set to <c>true</c> performs a pixel-perfect hit test.</param>
-        /// <returns>
-        /// <c>true</c> if the quad is hit; otherwise, <c>false</c>.
-        /// </returns>
-        public bool HitTest(Point p, bool pixelPerfect)
-        {
-            if (!pixelPerfect)
-            {
-                return this.HitTest(p);
-            }
-
-            var pf = p.ToScreenCoordinates(this.ViewportSize);
-            return this.PixelPerfectHitTest(pf.X, pf.Y);
-        }
-
-        /// <summary>
         /// Removes the crop.
         /// </summary>
         public void RemoveCrop()
         }
 
         /// <summary>
-        /// Rotates the sprite.
-        /// </summary>
-        /// <param name="angle">The rotation angle in degrees.</param>
-        /// <param name="rotationPivot">The pivot point.</param>
-        /// <param name="doTransform">Indicates whether the rotation should be applied directly.</param>
-        public void Rotate(float angle, Point rotationPivot, bool doTransform)
-        {
-            PointF pivot = rotationPivot.ToScreenCoordinates(this.ViewportSize);
-            this.ForEachQuad(q => q.Rotate(angle, pivot));
-            if (doTransform)
-            {
-                this.ApplyTransformation();
-            }
-        }
-
-        /// <summary>
-        /// Scales the sprite.
-        /// </summary>
-        /// <param name="scaleFactor">The scale factor..</param>
-        /// <param name="scalePivot">The pivot point.</param>
-        /// <param name="doTransform">Indicates whether the scaling should be applied directly.</param>
-        public void Scale(PointF scaleFactor, Point scalePivot, bool doTransform)
-        {
-            PointF pivot = scalePivot.ToScreenCoordinates(this.ViewportSize);
-            this.ForEachQuad(q => q.Scale(scaleFactor, pivot));
-            if (doTransform)
-            {
-                this.ApplyTransformation();
-            }
-        }
-
-        /// <summary>
         /// Sets the quad bounds.
         /// </summary>
         /// <param name="quadIndex">Index of the quad.</param>
         }
 
         /// <summary>
-        /// Skews the sprite.
-        /// </summary>
-        /// <param name="skewFactor">The skew factor.</param>
-        /// <param name="doTransform">Indicates whether the skewing should be applied directly.</param>
-        public void Skew(PointF skewFactor, bool doTransform)
-        {
-            this.ForEachQuad(q => q.Skew(skewFactor));
-            if (doTransform)
-            {
-                this.ApplyTransformation();
-            }
-        }
-
-        /// <summary>
-        /// Translates the sprite.
-        /// </summary>
-        /// <param name="offset">The offset.</param>
-        /// <param name="doTransform">Indicates whether the translation should be applied directly.</param>
-        public void Translate(Point offset, bool doTransform)
-        {
-            var off = new PointF(
-                ((float)offset.X / this.ViewportSize.Width) * 2,
-                ((float)offset.Y / this.ViewportSize.Height) * 2);
-
-            this.ForEachQuad(q => q.Translate(off));
-            if (doTransform)
-            {
-                this.ApplyTransformation();
-            }
-        }
-
-        /// <summary>
         /// Updates the sprite crop.
         /// </summary>
         public void UpdateCrop()
 
         #region Private Methods
 
-        private void ForEachQuad(Action<Quad> action)
-        {
-            int count = this.QuadCount;
-            for (int j = 0; j < count; j++)
-            {
-                action(this.GetQuad(j));
-            }
-        }
-
         private Quad GetBorderQuad(BorderQuad borderQuad)
         {
             return this.GetQuad((int)borderQuad);
         }
 
-        private bool PixelPerfectHitTest(float x, float y)
-        {
-            var quad = (Quad)this.HitTest(x, y);
-
-            if (quad != null)
-            {
-                x -= quad.Left;
-                y -= quad.Top;
-                float x1 = (x / 2) * this.ViewportSize.Width;
-                float y1 = (-y / 2) * this.ViewportSize.Height;
-                float width = quad.Width * this.ViewportSize.Width / 2;
-                float height = quad.Height * this.ViewportSize.Height / 2;
-                var size = new SizeF(width, height);
-                var uvRect = RectangleF.FromLTRB(quad.RenderVertexUV[0].X, quad.RenderVertexUV[0].Y, quad.RenderVertexUV[2].X, quad.RenderVertexUV[2].Y);
-                var texSize = this.Backend.GetTextureSize(this.TextureHandle);
-                var left = (int)((((texSize.Width / size.Width) * uvRect.Width) * x1) + (uvRect.Left * texSize.Width));
-                var top = (int)((((texSize.Height / size.Height) * uvRect.Height) * y1) + (uvRect.Top * texSize.Height));
-
-                return this.Backend.GetTextureAlpha(this.TextureHandle, new Point(left, top)) > 0.01f;
-            }
-
-            return false;
-        }
-
         #endregion Private Methods
 
         #endregion Methods
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.