Skip to content

Commit

Permalink
Pre work on SizeSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
roo-oliv committed Jul 22, 2024
1 parent e2372d1 commit b4b57ff
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MonoDreams.Examples/Screens/MainMenuScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public MainMenuScreen(GraphicsDevice graphicsDevice, ContentManager content, Cam
new CollisionDetectionSystem(World, parallelRunner),
new ButtonSystem(World),
new LayoutSystem(World, renderer),
new SizeSystem(World, parallelRunner),
new PositionSystem(World, parallelRunner),
new BeginDrawSystem(spriteBatch, renderer, camera),
new DrawSystem(World, spriteBatch),
Expand All @@ -65,7 +66,6 @@ public void Load(ScreenController screenController, ContentManager content)
AlignItems = YogaAlign.Center,
JustifyContent = YogaJustify.Center,
Padding = 20,
FlexDirection = YogaFlexDirection.Column,
});

World.CreateButton(rootLayout, "Start", () => { }, Vector2.Zero, font, Color.Pink, Color.Red, DrawLayer.Buttons);
Expand Down
16 changes: 9 additions & 7 deletions MonoDreams/Extensions/UIExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
using System;
using DefaultEcs;
using Facebook.Yoga;
using Microsoft.Xna.Framework;
using MonoDreams.Component;
using MonoGame.Extended;
Expand Down Expand Up @@ -49,13 +50,14 @@ public static Entity CreateButton(
defaultColor ??= Color.White;
selectedColor ??= Color.Black;

var buttonEntity = world.CreateLayoutEntity(parent: layoutParent);

ref var layoutNode = ref buttonEntity.Get<LayoutNode>();
layoutNode.Node.Width = size.X;
layoutNode.Node.Height = size.Y;
layoutNode.Node.MarginTop = 20;
layoutNode.Node.MarginBottom = 20;
var buttonEntity = world.CreateLayoutEntity(
parent: layoutParent,
yogaNode: new YogaNode
{
Width = size.X,
Height = size.Y,
Margin = 20,
});

buttonEntity.Set(new Position(position));
buttonEntity.Set(new Collidable(new Rectangle(size / new Point(-2, -2), size)));
Expand Down
5 changes: 5 additions & 0 deletions MonoDreams/Message/SizeChangeMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using DefaultEcs;

namespace MonoDreams.Message;

public readonly record struct SizeChangeMessage(Entity entity);
12 changes: 12 additions & 0 deletions MonoDreams/Size.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Xna.Framework;

namespace MonoDreams;

public struct Size
{
public Vector2 CurrentSize;
public Vector2 NextSize;
public Vector2 LastSize;

public bool HasUpdates => CurrentSize != NextSize || LastSize != CurrentSize;
}
28 changes: 28 additions & 0 deletions MonoDreams/System/SizeSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using DefaultEcs;
using DefaultEcs.System;
using DefaultEcs.Threading;
using MonoDreams.Message;
using MonoDreams.State;

namespace MonoDreams.System;

public sealed class SizeSystem : AEntitySetSystem<GameState>
{
private readonly World _world;

public SizeSystem(World world, IParallelRunner runner)
: base(world.GetEntities().With((in Size p) => p.HasUpdates).AsSet(), runner)
{
_world = world;
}

protected override void Update(GameState state, in Entity entity)
{
ref var size = ref entity.Get<Size>();
size.LastSize.X = size.CurrentSize.X;
size.LastSize.Y = size.CurrentSize.Y;
size.CurrentSize.X = size.NextSize.X;
size.CurrentSize.Y = size.NextSize.Y;
_world.Publish(new SizeChangeMessage(entity));
}
}

0 comments on commit b4b57ff

Please sign in to comment.