public override Point GetPreferredSize(Point availableSpace) { // TODO Support scrolling when selecting items and the available height is less than what we need var itemSizes = Items.Select(item => Vector2.Ceiling(_ItemFont.MeasureString(item.Label)).ToPoint()).ToList(); int width = itemSizes.Max(sz => sz.X) + Padding.Horizontal + WindowManager.TILE_SIZE + ItemSpacing; int height = ItemCount * WindowManager.TILE_SIZE + Padding.Vertical; return(new Point(width, height)); }
public CollisionCheckResult Check(Vector2 currentPosition, Vector2 positionDelta, Vector2 objectSize) { Result = new CollisionCheckResult() { Position = currentPosition }; _currentPosition = currentPosition.Ceiling(); _positionDelta = positionDelta.Ceiling(); _objectSize = objectSize.Ceiling(); _finalPosition = new Vector2(_positionDelta.X, _positionDelta.Y); // short-circuit if there is nothing to do if (positionDelta == Vector2.Zero) { return(Result); } // determine if we need to perform multiple checks, e.g. the entitys proposed // position is more than one tile away. var steps = (float)Math.Ceiling(Math.Max(Math.Abs(_positionDelta.X), Math.Abs(_positionDelta.Y)) / _map.TileSize); if (steps > 1) { var stepDelta = _positionDelta / steps; if (stepDelta.X > 0 || stepDelta.Y > 0) { for (var i = 0; i < steps; i++) { PeekStep(stepDelta, i); // if the trace finds a collision anywhere along the way, zero out the offending part of the vector if (Result.XAxis) { stepDelta.X = 0; positionDelta.X = 0; } if (Result.YAxis) { stepDelta.Y = 0; positionDelta.Y = 0; } } } } else { PeekStep(positionDelta, 0f); } return(Result); }
public override Point GetPreferredSize(Point availableSpace) { if (!AutoSize) { return(ExplicitSize); } var size = Vector2.Ceiling(TextFont.MeasureString(Text)).ToPoint(); size.X += SPRITE_PADDING_X * 2 + Padding.Horizontal; size.Y += SPRITE_PADDING_Y * 2 + Padding.Vertical; return(size); }
public void CeilingTest() { const int max = 1000; var r = new Random(987); for (var i = 0; i < 30; i++) { var v = new Vector2(r.NextFloat() * max, r.NextFloat() * max); var c = v.Ceiling(); Assert.AreEqual(Math.Ceiling(v.X), c.X); Assert.AreEqual(Math.Ceiling(v.Y), c.Y); } }
public CollisionCheckResult Check( Vector2 currentPosition, Vector2 positionDelta, Vector2 objectSize ) { Result = new CollisionCheckResult() { Position = currentPosition }; _currentPosition = currentPosition.Ceiling(); _positionDelta = positionDelta.Ceiling(); _objectSize = objectSize.Ceiling(); _finalPosition = new Vector2( _positionDelta.X, _positionDelta.Y ); // short-circuit if there is nothing to do if( positionDelta == Vector2.Zero ) return Result; // determine if we need to perform multiple checks, e.g. the entitys proposed // position is more than one tile away. var steps = (float)Math.Ceiling( Math.Max( Math.Abs( _positionDelta.X ), Math.Abs( _positionDelta.Y ) ) / _map.TileSize ); if( steps > 1 ) { var stepDelta = _positionDelta / steps; if( stepDelta.X > 0 || stepDelta.Y > 0 ) { for( var i = 0; i < steps; i++ ) { PeekStep( stepDelta, i ); // if the trace finds a collision anywhere along the way, zero out the offending part of the vector if( Result.XAxis ) { stepDelta.X = 0; positionDelta.X = 0; } if( Result.YAxis ) { stepDelta.Y = 0; positionDelta.Y = 0; } } } } else { PeekStep( positionDelta, 0f ); } return Result; }
public void Round() { Vector2 vector2 = new Vector2(0.4f, 0.6f); // CEILING Vector2 ceilMember = vector2; ceilMember.Ceiling(); Vector2 ceilResult; Vector2.Ceiling(ref vector2, out ceilResult); Assert.AreEqual(new Vector2(1.0f, 1.0f), ceilMember); Assert.AreEqual(new Vector2(1.0f, 1.0f), Vector2.Ceiling(vector2)); Assert.AreEqual(new Vector2(1.0f, 1.0f), ceilResult); // FLOOR Vector2 floorMember = vector2; floorMember.Floor(); Vector2 floorResult; Vector2.Floor(ref vector2, out floorResult); Assert.AreEqual(new Vector2(0.0f, 0.0f), floorMember); Assert.AreEqual(new Vector2(0.0f, 0.0f), Vector2.Floor(vector2)); Assert.AreEqual(new Vector2(0.0f, 0.0f), floorResult); // ROUND Vector2 roundMember = vector2; roundMember.Round(); Vector2 roundResult; Vector2.Round(ref vector2, out roundResult); Assert.AreEqual(new Vector2(0.0f, 1.0f), roundMember); Assert.AreEqual(new Vector2(0.0f, 1.0f), Vector2.Round(vector2)); Assert.AreEqual(new Vector2(0.0f, 1.0f), roundResult); }
protected virtual void RenderTexture(GuiTexture tex, float x, float y, bool cenetered = false) { if (tex == null) { return; } float width = tex.Size.X * tex.Scale; float height = tex.Size.Y * tex.Scale; Vector2 ratio = new Vector2(width / SharpCraft.Instance.ClientSize.Width, height / SharpCraft.Instance.ClientSize.Height); Vector2 unit = new Vector2(1f / SharpCraft.Instance.ClientSize.Width, 1f / SharpCraft.Instance.ClientSize.Height); Vector2 pos = new Vector2(x, -y); if (!cenetered) { pos.X += width / 2; pos.Y -= height / 2; } Matrix4 mat = MatrixHelper.CreateTransformationMatrix(pos.Ceiling() * unit * 2 + Vector2.UnitY - Vector2.UnitX, ratio); Shader.Bind(); GL.BindVertexArray(GuiRenderer.GuiQuad.VaoID); Shader.UpdateGlobalUniforms(); Shader.UpdateModelUniforms(); Shader.UpdateInstanceUniforms(mat, tex); GL.EnableVertexAttribArray(0); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, tex.ID); GL.DrawArrays(PrimitiveType.Quads, 0, 4); GL.DisableVertexAttribArray(0); Shader.Unbind(); }
public void RenderText(string text, float x, float y, float scale, Vector3 color, float brightness = 1, bool centered = false, bool dropShadow = false, int spacing = 4) //#TODO { brightness = MathHelper.Clamp(brightness, 0, 1); scale *= 0.5f; x = (float)Math.Ceiling(x); y = (float)Math.Ceiling(y); Texture tex = TextureManager.TEXTURE_TEXT; Shader.Bind(); GL.BindVertexArray(GuiRenderer.GuiQuad.VaoID); GL.EnableVertexAttribArray(0); GL.BindTexture(TextureTarget.Texture2D, tex.ID); Vector2 totalSize = Vector2.Zero; Queue <ValueTuple <FontMapCharacter, Vector3> > present = new Queue <ValueTuple <FontMapCharacter, Vector3> >(); MatchCollection matches = Regex.Matches(text, @"\\{(.*?)\}"); Vector3 currentColor = color; for (int index = 0; index < text.Length; index++) { char c = text[index]; FontMapCharacter node = FontManager.GetCharacter(c); if (!node.HasValue) { continue; } Match first = null; foreach (Match m in matches) { if (m.Index == index) { first = m; break; } } if (first != null && first.Length > 0) { try { Color clr = ColorTranslator.FromHtml($"#{first.Value.Replace(@"\{", "").Replace("}", "")}"); currentColor.X = clr.R / 255f; currentColor.Y = clr.G / 255f; currentColor.Z = clr.B / 255f; index += first.Length - 1; continue; } catch { } } totalSize.X += node.Character.W + node.Character.OffsetX; totalSize.Y += node.Character.H + node.Character.OffsetY; present.Enqueue(new ValueTuple <FontMapCharacter, Vector3>(node, currentColor * brightness)); } totalSize.X += (present.Count - 1) * spacing; totalSize *= scale; totalSize.Y /= -present.Count; float positionX = 0f; foreach (ValueTuple <FontMapCharacter, Vector3> tuple in present) { float width = tuple.Item1.Character.W * scale; float height = tuple.Item1.Character.H * scale; Vector2 ratio = new Vector2(width / SharpCraft.Instance.ClientSize.Width, height / SharpCraft.Instance.ClientSize.Height); Vector2 unit = new Vector2(1f / SharpCraft.Instance.ClientSize.Width, 1f / SharpCraft.Instance.ClientSize.Height); Vector2 pos = new Vector2(x + positionX + width / 2, -(y + height / 2)); pos.Y -= tuple.Item1.Character.OffsetY * scale; if (centered) { pos.X -= totalSize.X / 2; pos.Y -= totalSize.Y / 2; } pos = pos.Ceiling(); if (dropShadow) { Matrix4 mat1 = MatrixHelper.CreateTransformationMatrix( (pos + (Vector2.UnitX - Vector2.UnitY) * 4f * scale) * unit * 2 + Vector2.UnitY - Vector2.UnitX, ratio); Shader.SetVector3("colorIn", Vector3.One * 0.1f); Shader.SetMatrix4("transformationMatrix", mat1);//mat1); Shader.SetVector2("UVmin", tuple.Item1.TextureUv.Start); Shader.SetVector2("UVmax", tuple.Item1.TextureUv.End); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); } Matrix4 mat2 = MatrixHelper.CreateTransformationMatrix( pos * unit * 2 + Vector2.UnitY - Vector2.UnitX, ratio); Shader.SetVector3("colorIn", tuple.Item2 == Vector3.One ? color : tuple.Item2); Shader.SetMatrix4("transformationMatrix", mat2); Shader.SetVector2("UVmin", tuple.Item1.TextureUv.Start); Shader.SetVector2("UVmax", tuple.Item1.TextureUv.End); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); positionX += width + (tuple.Item1.Character.OffsetX + spacing) * scale; } GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); GL.DisableVertexAttribArray(0); GL.BindVertexArray(0); Shader.Unbind(); }
public override Point GetPreferredSize(Point availableSpace) { var textSize = Vector2.Ceiling(TextFont.MeasureString(Text)).ToPoint(); return(new Point(textSize.X + Padding.Horizontal, textSize.Y + Padding.Vertical)); }
public void UpdateBalloonAnimation(GameTime gameTime) { switch (textBalloonAnimation) { case TextBalloonAnimation.Spawning: expansionFactor += (float)gameTime.ElapsedGameTime.TotalSeconds * 3; animationTimer = (float)Math.Sin(expansionFactor); Vector2 newBalloonSize = balloonInitialSize + balloonExpandedSize * animationTimer; if (expansionFactor > MathHelper.PiOver2) { textBalloonAnimation = TextBalloonAnimation.SpawningWriting; newBalloonSize = balloonInitialSize + balloonExpandedSize; expansionFactor = MathHelper.PiOver2; animationTimer = 1; } bottomBorder.Scale = topBorder.Scale = newBalloonSize * Vector2.UnitX + Vector2.UnitY; rightBorder.Scale = leftBorder.Scale = newBalloonSize * Vector2.UnitY + Vector2.UnitX; centerSquare.Scale = Vector2.Ceiling(newBalloonSize) + Vector2.One * 2; //Transparency spriteList.ForEach((x) => x.SetTransparency(animationTimer)); break; case TextBalloonAnimation.SpawningWriting: animationTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (animationTimer >= 0.02f) { animationTimer = 0f; bool hasChanged = false; for (int i = 0; i < typingIndexMatrix.Length; i++) { if (typingIndexMatrix[i] < completeText[i].Length) { spriteTextList[i].Text += completeText[i][typingIndexMatrix[i]]; typingIndexMatrix[i]++; hasChanged = true; break; } } if (!hasChanged) { animationTimer = 1f; textBalloonAnimation = TextBalloonAnimation.Spawned; } } break; case TextBalloonAnimation.Spawned: vanishTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (vanishTimer >= 5) { textBalloonAnimation = TextBalloonAnimation.Vanishing; } break; case TextBalloonAnimation.Vanishing: animationTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds; spriteList.ForEach((x) => x.SetTransparency(animationTimer)); spriteTextList.ForEach((x) => x.SetTransparency((animationTimer - 0.2f) / 2)); if (animationTimer <= 0) { textBalloonAnimation = TextBalloonAnimation.Vanished; } break; case TextBalloonAnimation.Vanished: OnVanishBalloon?.Invoke(this); break; } }