/// <summary> /// Sets the position, rotation and scale of a sprite based on the data in a TiledObject, /// and then sets the origin of the sprite. /// </summary> /// <param name="newSprite">The sprite to be changed</param> /// <param name="obj">The Tiled Object with the transform data</param> /// <param name="normalizedOriginX">the new x-origin, normalized (=typically between 0 and 1)</param> /// <param name="normalizedOriginY">the new y-origin, normalized (=typically between 0 and 1)</param> public static void SetPositionRotationScaleOrigin(Sprite newSprite, TiledObject obj, float normalizedOriginX = 0.5f, float normalizedOriginY = 0.5f) { newSprite.scale = 1; // SetOrigin according to Tiled's weird and inconsistent conventions: float originY = obj.ImageID >= 0 ? 1 : 0; newSprite.SetOrigin(0, originY * newSprite.height); // Set transform data using this origin: newSprite.x = obj.X; newSprite.y = obj.Y; newSprite.rotation = obj.Rotation; newSprite.scaleX = obj.Width / newSprite.width; newSprite.scaleY = obj.Height / newSprite.height; // Set the desired origin: ChangeOrigin(newSprite, normalizedOriginX, normalizedOriginY, 0, originY); }
//-------------------------------------------------------------------------------------------- // REGION: public static utility methods, related to creating and placing GXPEngine objects // based on data from Tiled Objects. //-------------------------------------------------------------------------------------------- /// <summary> /// Creates an EasyDraw for displaying text, based on the configuration parameters of a /// Tiled object. (Including font, text alignment, color) /// </summary> /// <param name="obj">The Tiled (text) object</param> /// <returns></returns> public static EasyDraw CreateTextField(TiledObject obj, bool addCollider = true, bool highQualityText = true) { float scaleMultiplier = highQualityText ? 2 : 1; // 1=as is. 2=better antialiasing, but 4 x size EasyDraw message = new EasyDraw((int)Mathf.Ceiling(obj.Width * scaleMultiplier), (int)Mathf.Ceiling(obj.Height * scaleMultiplier), addCollider); // TODO: Cache fonts? // Set Font: FontStyle f = FontStyle.Regular; if (obj.textField.bold == 1 && obj.textField.italic == 1) { f = FontStyle.Bold | FontStyle.Italic; } else if (obj.textField.bold == 0 && obj.textField.italic == 1) { f = FontStyle.Italic; } else if (obj.textField.bold == 1 && obj.textField.italic == 0) { f = FontStyle.Bold; } message.TextFont(new Font(obj.textField.font, Mathf.Round(obj.textField.fontSize * scaleMultiplier), f, GraphicsUnit.Pixel)); message.graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; //AntiAlias; // Set text alignment: message.TextAlign( obj.textField.horizontalAlign == "left" ? CenterMode.Min : (obj.textField.horizontalAlign == "right" ? CenterMode.Max : CenterMode.Center), obj.textField.verticalAlign == "top" ? CenterMode.Min : (obj.textField.verticalAlign == "bottom" ? CenterMode.Max : CenterMode.Center) ); // Set color: uint col = obj.textField.Color; message.Fill(Color.FromArgb((int)(col & 255), (int)((col >> 24) & 255), (int)((col >> 16) & 255), (int)((col >> 8) & 255)), (int)(col & 255)); return(message); }