private void SetBelow(SpatialControl reference, SpatialControl actual) { //Size is positive, but we want to go down so we subtract. var y = reference.Translation.y - reference.Size.y; actual.Translation = new Vector3(actual.Translation.x, y, actual.Translation.z); }
float AnchorSX(SpatialControl reference) { if (Right) { return(AnchorRight * reference.Size.x + MarginRight - owner.Translation.x); } return(owner.Size.x); }
float AnchorSY(SpatialControl reference) { if (Bottom) { return(AnchorBottom * reference.Size.y + MarginRight + owner.Translation.y); } return(owner.Size.y); }
float AnchorX(SpatialControl reference) { if (Left) { return(AnchorLeft * reference.Size.x + MarginLeft); } return(owner.Translation.x); }
float AnchorY(SpatialControl reference) { if (Top) { return(AnchorTop * reference.Size.y + MarginTop); } return(owner.Translation.y); }
//This is necessary because something else is referencing this object //and we dont know what. public void OnOwnerDelete() { if (!(parent is null)) { parent.Disconnect(nameof(SpatialControl.SizeChanged), this, nameof(OnReferenceSizeChanged)); } parent = null; owner = null; }
public override void _Ready() { SpatialControl parent = (SpatialControl)GetParent(); parent.Connect(nameof(SpatialControl.SizeChanged), this, nameof(OnSizeChanged), new Godot.Collections.Array { parent }); shape = GetNode <CollisionShape>("CollisionShape"); }
//NOT an override of Godot.Object._init //That doesn't take arguments and this thing needs a reference to its owner. public void Init(SpatialControl o) { owner = o; owner.Connect(nameof(SpatialControl.OnPredelete), this, nameof(OnOwnerDelete)); // p.GetTree().GetEditedSceneRoot(); if (owner.GetParent() is SpatialControl p) { parent = p; parent.Connect(nameof(SpatialControl.SizeChanged), this, nameof(OnReferenceSizeChanged), new Godot.Collections.Array { parent }); OnReferenceSizeChanged(parent.Size, parent); } }
public void OnChildSizeChanged(Vector2 oldSize, SpatialControl child) { if (Math.Abs(oldSize.y - child.Size.y) < 1e-7) { return; //we don't actually care about changes that don't affect vertical size. } GD.Print(child.Name, "; ", child.Translation, "; ", oldSize, " -> ", child.Size); for (int i = child.GetIndex() + 1; i < GetChildCount(); i++) { var nextChild = (SpatialControl)GetChild(i); SetBelow(child, nextChild); child = nextChild; } Size = new Vector2(Size.x, -child.Translation.y + child.Size.y); GD.Print(GetPath() + "size set to: ", Size); }
private void RegisterChild(SpatialControl child) { GD.Print("Registering: ", child.Name, "; ", child.GetIndex()); if (child.GetIndex() == 0) { child.Translation = new Vector3(child.Translation.x, 0, child.Translation.z); } else { var previousSC = (SpatialControl)GetChild(child.GetIndex() - 1); SetBelow(previousSC, child); } child.Connect(nameof(SizeChanged), this, nameof(OnChildSizeChanged), new Godot.Collections.Array { child }); OnChildSizeChanged(Vector2.Zero, child); }
public void InsertSpatialControl(SpatialControl child, int index) { AddChildBelowNode(GetChild(index), child); RegisterChild(child); }
public void AddSpatialControl(SpatialControl child) { AddChild(child); RegisterChild(child); }
public virtual void OnReferenceSizeChanged(Vector2 oldSize, SpatialControl reference) { owner.Translation = new Vector3(AnchorX(reference), AnchorY(reference), owner.Translation.z); owner.Size = new Vector2(AnchorSX(reference), AnchorSY(reference)); }
public void OnSizeChanged(Vector2 oldSize, SpatialControl parent) { shape.Translation = new Vector3(parent.Size.x / 2, -parent.Size.y / 2, 0); ((BoxShape)shape.Shape).Extents = new Vector3(parent.Size.x / 2, parent.Size.y / 2, Thiccness); }