public void MoveUp(ICollisionEngineContext context, IMovableObject movingObj, int distance) { var movingObjBounds = movingObj.Bounds; var sweptArea = new IntRectangle(movingObjBounds.Left, movingObjBounds.Top - distance, movingObjBounds.Front, movingObjBounds.Width, distance, movingObjBounds.Depth); var areaToCheck = sweptArea.Extend(Constants.BlockSize); foreach (var obj in context.GetCollidableObjectsWithin(areaToCheck)) { if (obj == movingObj) { continue; } var objBounds = CoordinateSystem.Denormalize(obj.Bounds, sweptArea); if (obj.IsSolid && sweptArea.Intersects(objBounds)) { sweptArea = new IntRectangle(sweptArea.Left, objBounds.Bottom, sweptArea.Front, sweptArea.Width, sweptArea.Bottom - objBounds.Bottom, sweptArea.Depth); } } movingObj.Position = movingObj.Position.MoveUp(sweptArea.Height); }
public static IntRectangle Denormalize(this ICoordinateSystem coordinateSystem, IntVector position, IntVector size, IntVector referencePosition, IntVector referenceSize) { // TODO: consider size return(coordinateSystem.Denormalize( IntRectangle.FromPositionAndSize(position, size), IntRectangle.FromPositionAndSize(referencePosition, referenceSize))); }
public void RemoveBlock(IntVector positionOfBlockToRemove) { var areaAroundBlock = IntRectangle.FromPositionAndSize(positionOfBlockToRemove, IntVector.RightDown * Constants.BlockSize).Extend(Constants.BlockSize); foreach (var grid in Grids) { var removedBlock = grid.SetBlock(positionOfBlockToRemove, null); if (removedBlock == null) { continue; } // var removedBlockBounds = IntRectangle.FromPositionAndSize(positionOfBlockToRemove, Constants.PhysicsUnitVector); foreach (var block in grid.GetAllWithin(areaAroundBlock)) { if (block != null && block != removedBlock) { // var blockBounds = IntRectangle.FromPositionAndSize(position, Constants.PhysicsUnitVector); block.OnNeighborChanged( this, new BlockInWorld <Block>(block, grid, positionOfBlockToRemove)); } } } }
public void ContainsTest() { var rectangle = new IntRectangle(IntVector2.Zero, IntVector2.One); Assert.That(rectangle.Contains(rectangle.A)); Assert.That(rectangle.Contains(rectangle.B), Is.False); Assert.That(rectangle.Contains(rectangle.B + IntVector2.One), Is.False); }
private void RenderEntities(DrawingContext dc, IntRectangle visibleArea, Camera camera) { foreach (var entity in ApplicationViewModel.World.Entities) { var bounds = ApplicationViewModel.World.CoordinateSystem.Denormalize(entity.Bounds, visibleArea); dc.DrawImage(entity.Image, ApplicationViewModel.World.Camera.CastRectangle(bounds).ToWindowsRect()); } }
public IntRectangle GetBlockBounds(IntVector position) { position = position - Position; var blockPosition = position.Floor(Constants.BlockSize); blockPosition = CoordinateSystem.Normalize(blockPosition); return(IntRectangle.FromPositionAndSize(Position + blockPosition, Constants.BlockSizeVector)); }
private bool IsInPlayerRange(IntRectangle rectangle) { var playerCenter = Player.Bounds.Center; var x = rectangle.Left > playerCenter.X ? rectangle.Left : rectangle.Right; var y = rectangle.Top > playerCenter.Y ? rectangle.Top : rectangle.Bottom; return(IsInPlayerRange(new IntVector(x, y, Player.Position.Z))); }
private IEnumerable <T> GetFromChild(IEnumerable <T> items, int index, int left, int top, int front, int right, int bottom, int back) { if (Children[index] == null) { return(items); } return(items.Concat(Children[index].GetAllWithin(IntRectangle.FromLeftTopFrontRightBottomBack(left, top, front, right, bottom, back)))); }
public override IEnumerable <T> GetAllWithin(IntRectangle rectangle) { rectangle = IntRectangle.FromPoints( IntVectorMath.MinMax(rectangle.LeftTopFront - Offset, IntVector.Zero, SizeVector), IntVectorMath.MinMax(rectangle.RightBottomBack - Offset, IntVector.Zero, SizeVector) ); return(base.GetAllWithin(rectangle)); }
//private class ScissorContext : IDisposable //{ // private IntRectangle _previous; // private bool _wasEnabled; // private bool _isDisposed = false; // // public ScissorContext(IntRectangle region) // { // this._wasEnabled = Scissor.IsEnabled; // this._previous = Scissor.Region; // Scissor.Region = region; // Scissor.IsEnabled = true; // } // // ~ScissorContext() // { // Async.Throw(new ObjectDisposedIncorrectlyException("A context returned from GRaff.Graphics.Scissor.Context was garbage collected before Dispose was called.")); // } // // public void Dispose() // { // if (!_isDisposed) // { // GC.SuppressFinalize(this); // _isDisposed = true; // Scissor.Region = _previous; // Scissor.IsEnabled = _wasEnabled; // } // else // throw new ObjectDisposedException("Scissor"); // } //} // public static IDisposable Use(IntRectangle region) { return(UseContext.CreateAt( $"{typeof(Scissor).FullName}.{nameof(Use)}", (enabled: Scissor.IsEnabled, region: Scissor.Region), () => { Scissor.IsEnabled = true; Scissor.Region = region; }, previous => { Scissor.IsEnabled = previous.enabled; Scissor.Region = previous.region; } )); }
public override void ForEachWithin(IntRectangle rectangle, EnumerateItemDelegate <T> func) { rectangle = IntRectangle.FromPoints( IntVectorMath.MinMax(rectangle.LeftTopFront - Offset, IntVector.Zero, SizeVector), IntVectorMath.MinMax(rectangle.RightBottomBack - Offset, IntVector.Zero, SizeVector) ); base.ForEachWithin(rectangle, (block, coords) => func(block, coords + Offset)); }
private IEnumerator <object> SelectTask(IntVector2 initialCell, bool selectKeyframes) { var input = Grid.RootWidget.Input; if (!input.IsKeyPressed(Key.Control)) { Operations.ClearGridSelection.Perform(); Core.Operations.ClearRowSelection.Perform(); selectionRectangle = new IntRectangle(); } Grid.OnPostRender += RenderSelectionRect; var showMeasuredFrameDistance = false; while (input.IsMousePressed()) { selectionRectangle.A = initialCell; selectionRectangle.B = Grid.CellUnderMouse(); if (selectionRectangle.Width >= 0) { selectionRectangle.B.X++; } else { selectionRectangle.A.X++; } if (selectionRectangle.Height >= 0) { selectionRectangle.B.Y++; } else { selectionRectangle.A.Y++; } selectionRectangle = selectionRectangle.Normalized; showMeasuredFrameDistance |= selectionRectangle.Width != 1; if (showMeasuredFrameDistance) { Timeline.Instance.Ruler.MeasuredFrameDistance = selectionRectangle.Width; } Window.Current.Invalidate(); yield return(null); } Timeline.Instance.Ruler.MeasuredFrameDistance = 0; Grid.OnPostRender -= RenderSelectionRect; if (selectKeyframes) { SelectKeyframes(selectionRectangle); } else { for (var r = selectionRectangle.A.Y; r < selectionRectangle.B.Y; r++) { Operations.SelectGridSpan.Perform(r, selectionRectangle.A.X, selectionRectangle.B.X); } } }
private Sprite LoadSprite() { // var spritesheet = new Image("assets/p1_spritesheet.png"); // Extract sprites from sheet var submap = new Dictionary <string, Image>(); var data = Files.ReadText("assets/p1_spritesheet.txt"); foreach (var line in data.Split('\n')) { var idx = line.IndexOf('='); if (idx < 0) { throw new InvalidOperationException("spritesheet metadata invalid."); } // Parse frame data var name = line.Substring(0, idx).Trim(); var values = line.Substring(idx + 1).Trim().Split(' ').Select(s => int.Parse(s)).ToArray(); var rect = new IntRectangle(values[0], values[1], values[2], values[3]); // Extract image from spritesheet var image = Extract(spritesheet, rect); submap[name] = image; // Set image origin to bottom center image.Origin = (image.Width / 2, image.Height); } const float FrameDelay = 1 / 10F; var sprite = new Sprite(); sprite.AddAnimation(new SpriteAnimation("idle") { { submap["p1_front"], FrameDelay } }); sprite.AddAnimation(new SpriteAnimation("jump") { { submap["p1_jump"], FrameDelay } }); sprite.AddAnimation(new SpriteAnimation("walk") { { submap["p1_walk01"], FrameDelay }, { submap["p1_walk02"], FrameDelay }, { submap["p1_walk03"], FrameDelay }, { submap["p1_walk04"], FrameDelay }, { submap["p1_walk05"], FrameDelay }, { submap["p1_walk06"], FrameDelay }, { submap["p1_walk07"], FrameDelay } }); return(sprite);
private void RenderBackgroundBlocks(DrawingContext dc, IntRectangle visibleArea, Camera camera) { var visibleAreaPosition = visibleArea.Position; visibleAreaPosition.Z = Constants.BlockSize; var visibleAreaSize = visibleArea.Size; visibleAreaSize.Z = Constants.BlockSize; RenderGrids(dc, IntRectangle.FromPositionAndSize(visibleAreaPosition, visibleAreaSize), camera); }
public void IntersectTest() { var intersection = IntRectangle.Intersect(new IntRectangle(0, 0, 2, 2), new IntRectangle(1, 1, 3, 3)); var expectedIntersection = new IntRectangle(1, 1, 2, 2); Assert.That(intersection, Is.EqualTo(expectedIntersection)); intersection = IntRectangle.Intersect(new IntRectangle(0, 0, 3, 3), new IntRectangle(1, 1, 2, 2)); expectedIntersection = new IntRectangle(1, 1, 2, 2); Assert.That(intersection, Is.EqualTo(expectedIntersection)); }
public void EqualsTest() { var rectangleOne = new IntRectangle(IntVector2.Zero, IntVector2.One); Assert.That(rectangleOne.Equals(new IntRectangle(0, 0, 1, 1))); Assert.That(rectangleOne.Equals((object)new IntRectangle(0, 0, 1, 1))); Assert.That(new IntRectangle(0, 0, 1, 1) == rectangleOne); Assert.That(IntRectangle.Empty != rectangleOne); Assert.That(rectangleOne.GetHashCode(), Is.EqualTo(rectangleOne.GetHashCode())); }
public void NormalizedTest() { var rectangle = new IntRectangle(IntVector2.One, IntVector2.Zero); Assert.That(rectangle.Width < 0); Assert.That(rectangle.Height < 0); rectangle = rectangle.Normalized; Assert.That(rectangle.Width > 0); Assert.That(rectangle.Height > 0); }
private void AddItems(CircularBinaryGrid <string> grid, IntRectangle rect) { for (var x = rect.Left; x < rect.Right; x++) { for (var y = rect.Top; y < rect.Bottom; y++) { AddItem(grid, new IntVector(x, y, 0)); } } }
public IEnumerable <Block> GetAllWithin(IntRectangle rectangle) { var transformedRectangle = rectangle.Move(-Position); transformedRectangle = IntRectangle.FromPositionAndSize( transformedRectangle.Position.DivideRoundDown(Constants.BlockSize), transformedRectangle.Size.DivideRoundUp(Constants.BlockSize) ); return(InnerGrid.GetAllWithin(transformedRectangle)); }
public static IDisposable UseIntersection(IntRectangle region) { if (IsEnabled) { return(Use(region.Intersection(Region) ?? IntRectangle.Zero)); } else { return(Use(region)); } }
public static IntRectangle Denormalize(this ICoordinateSystem coordinateSystem, IntRectangle bounds, IntRectangle referenceBounds) { var center = bounds.Center; var referenceCenter = referenceBounds.Center; return(IntRectangle.FromCenterAndSite( new IntVector( coordinateSystem.DenormalizeX(center.X, referenceCenter.X), coordinateSystem.DenormalizeY(center.Y, referenceCenter.Y), center.Z), bounds.Size)); }
public void IntRectangle_BoundaryIntersections() { var rect = new IntRectangle(0, 0, rand.Integer(2, 100), rand.Integer(2, 100)); Assert.False(rect.Intersects(new IntRectangle(rect.Width, 0, 1, 1))); Assert.False(rect.Intersects(new IntRectangle(0, rect.Height, 1, 1))); Assert.False(rect.Contains(new IntVector(rect.Width, rand.Integer(rect.Height)))); Assert.False(rect.Contains(new IntVector(rand.Integer(rect.Width), rect.Height))); Assert.True(rect.Contains(new IntVector(0, rand.Integer(rect.Height)))); Assert.True(rect.Contains(new IntVector(rand.Integer(rect.Width), 0))); }
private void RenderGrids(DrawingContext dc, IntRectangle visibleArea, Camera camera) { foreach (var grid in ApplicationViewModel.World.Grids) { foreach (var block in grid.GetAllWithin(visibleArea)) { var renderer = _blockRendererRegistry.Get(block.GetType()); var blockRect = ApplicationViewModel.World.CoordinateSystem.Denormalize(block.Bounds, visibleArea); renderer.Render(dc, camera, block, blockRect); } } }
public SpriteAtlas(Texture texture, Stream xmlStream) { var atlasData = (TextureAtlas)serializer.Deserialize(xmlStream); this.Texture = texture; _subTextures = new Dictionary <string, IntRectangle>(atlasData.SubTexture.Length); foreach (var sx in atlasData.SubTexture) { var region = new IntRectangle((int)sx.x, (int)sx.y, (int)sx.width, (int)sx.height); _subTextures.Add(sx.name, region); } }
public void SizeTest() { var rectangle = new IntRectangle(IntVector2.Zero, IntVector2.One); Assert.That(rectangle.Size, Is.EqualTo(new IntVector2(1, 1))); Assert.That(rectangle.Width, Is.EqualTo(1)); Assert.That(rectangle.Height, Is.EqualTo(1)); rectangle.Width++; rectangle.Height++; Assert.That(rectangle.Width, Is.EqualTo(2)); Assert.That(rectangle.Height, Is.EqualTo(2)); }
private IEnumerator <object> Drag(IntRectangle boundaries, DragSide side) { IntVector2?last = null; Save(boundaries); bool isStretchingMarkers = input.IsKeyPressed(Key.Control); using (Document.Current.History.BeginTransaction()) { while (input.IsMousePressed()) { Utils.ChangeCursorIfDefault(MouseCursor.SizeWE); var current = grid.CellUnderMouse(); if (current == last) { yield return(null); continue; } Document.Current.History.RollbackTransaction(); current.X = Math.Max(current.X, 0); if (side == DragSide.Left) { current.X = Math.Min(current.X, boundaries.Right - 1); } else { current.X = Math.Max(current.X, boundaries.Left + 1); } Stretch(boundaries, side, current.X, stretchMarkers: isStretchingMarkers); if (side == DragSide.Left) { boundaries.Left = current.X; } else { boundaries.Right = current.X; } ClearGridSelection.Perform(); for (int i = boundaries.Top; i <= boundaries.Bottom; ++i) { SelectGridSpan.Perform(i, boundaries.Left, boundaries.Right); } SetCurrentColumn.Perform(boundaries.Right); last = current; yield return(null); } Document.Current.History.CommitTransaction(); } yield return(null); }
private object SelectRangeTask(IntVector2 A, IntVector2 B) { Operations.ClearGridSelection.Perform(); Core.Operations.ClearRowSelection.Perform(); var r = new IntRectangle(); r.A.X = Math.Min(A.X, B.X); r.A.Y = Math.Min(A.Y, B.Y); r.B.X = Math.Max(A.X, B.X); r.B.Y = Math.Max(A.Y, B.Y); for (int i = r.A.Y; i <= r.B.Y; i++) { Operations.SelectGridSpan.Perform(i, r.A.X, r.B.X + 1); } return(null); }
/// <summary> /// Constructs a new <see cref="Chatbox"/> with the specified bounds. /// </summary> public Chatbox(IntRectangle bounds) { Bounds = bounds; _emotes = CreateEmotes(); // Hook up keyboard events Input.CharacterTyped += OnCharacterTyped; Input.KeyPressed += OnKeyPressed; Input.KeyRepeat += OnKeyPressed; // SubmitMessage("System", "Press <Enter> to type a chat message."); SubmitMessage("System", "WASD to move player."); SubmitMessage("System", "Type '!help' for more info."); }
public void ForEachWithin(IntRectangle rectangle, EnumerateItemDelegate <T> func) { for (var x = rectangle.Left; x < rectangle.Right; x++) { for (var y = rectangle.Top; y < rectangle.Bottom; y++) { for (var z = rectangle.Front; z < rectangle.Back; z++) { var item = _items[x, y, z]; if (item != null) { func(item, new IntVector(x, y, z)); } } } } }
public static IntRectangle[] Pack(IEnumerable <IntVector> rects, out IntVector bounds) { var result = new IntRectangle[rects.Count()]; var x = 0; var maxH = 0; var i = 0; foreach (var sz in rects) { result[i++] = new IntRectangle(x, 0, sz.X, sz.Y); x += sz.X; maxH = GMath.Max(maxH, sz.Y); } bounds = new IntVector(x, maxH); return(result); }
public unsafe bool TryGetCaptured(IDeviceContext context, IntRectangle clientRectangle, FrameType frameType, int colorDiffThreshold, int mostDetailedMip, out GpuRawFrame capturedFrame) { stopwatch.Restart(); var result = texturePool.Extract(clientRectangle.Width, clientRectangle.Height); var resultTexture = result.Item; d3dDevice.GetFrontBufferData(0, d3dSurface1); var sdxRectangle = new Rectangle(clientRectangle.X, clientRectangle.Y, clientRectangle.X + clientRectangle.Width, clientRectangle.Y + clientRectangle.Height); var lockedRectangle = d3dSurface1.LockRectangle(sdxRectangle, LockFlags.ReadOnly); var mappedSubresource = context.Map(resultTexture, 0, MapType.WriteDiscard, MapFlags.None); { int commonRowPitch = Math.Min(mappedSubresource.RowPitch, lockedRectangle.Pitch); Parallel.For(0, clientRectangle.Height, i => Memory.CopyBulk( (byte*)mappedSubresource.Data + i * mappedSubresource.RowPitch, (byte*)lockedRectangle.DataPointer + i * lockedRectangle.Pitch, commonRowPitch)); } context.Unmap(resultTexture, 0); d3dSurface1.UnlockRectangle(); var frameInfo = new FrameInfo(frameType, (float)Stopwatch.GetTimestamp() / Stopwatch.Frequency, mostDetailedMip, colorDiffThreshold, clientRectangle.Width, clientRectangle.Height, Cursor.Position.X - clientRectangle.X, Cursor.Position.Y - clientRectangle.Y); capturedFrame = new GpuRawFrame(frameInfo, result); stopwatch.Stop(); statistics.OnCapture(stopwatch.Elapsed.TotalMilliseconds); return true; }
public static SharpDX.Rectangle Rectangle(IntRectangle bRectangle) { return new SharpDX.Rectangle(bRectangle.X, bRectangle.Y, bRectangle.Width + bRectangle.X, bRectangle.Height + bRectangle.Y); }