functional card/row edit and drag

This commit is contained in:
2024-04-23 23:31:42 -05:00
parent 8e01e9cb9b
commit 56dcbb62af
29 changed files with 674 additions and 48 deletions

74
game.cs
View File

@@ -2,7 +2,9 @@ using Godot;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
public partial class game : Control
{
@@ -30,16 +32,18 @@ public partial class game : Control
{
CardSize = size;
}
protected void PropogateCardSize()
public void PropogateCardSize()
{
foreach (var r in GetNode("%RowContainer").GetChildren().OfType<row>())
r.CardSize = _CardSize;
foreach (var c in GetNode("%UnassignedCardContainer").GetChildren().OfType<card>())
c.CustomMinimumSize = _CardSize;
SetContainerMinima();
}
public bool AllowDragging = true;
public IEnumerable<row> Rows
=> GetNode<VBoxContainer>("%RowContainer").GetChildren().OfType<row>();
=> GetNode("%RowContainer").GetChildren().OfType<row>();
public IEnumerable<card> UnassignedCards
=> GetNode("%UnassignedCardContainer").GetChildren().OfType<card>();
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
@@ -78,38 +82,38 @@ public partial class game : Control
GetViewport().SetInputAsHandled();
}
}
public void Clear()
{
var rowContainer = GetNode("%RowContainer");
var unContainer = GetNode("%UnassignedCardContainer");
foreach (var r in rowContainer.GetChildren())
{
rowContainer.RemoveChild(r);
r.QueueFree();
}
foreach (var c in unContainer.GetChildren())
{
unContainer.RemoveChild(c);
c.QueueFree();
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public void SetContainerMinima()
{
// var node = GetNode<VBoxContainer>("%RowContainer");
// node.CustomMinimumSize = new Vector2(0, _CardSize.Y * node.GetChildCount());
}
public row GetRowById(string id)
=> GetTree().GetNodesInGroup("RowGroup").OfType<row>()
.FirstOrDefault(r => r.RowId == id);
public void AddRow(row row, string after = null)
public void AddRow(row row, int index = -1)
{
if (after is not null)
{
var r = GetRowById(after) ?? throw new Exception("row id does not exist");
r.AddSibling(row);
}
else
{
var node = GetNode<VBoxContainer>("%RowContainer");
node.AddChild(row);
node.MoveChild(row, 0);
}
SetContainerMinima();
var node = GetNode<VBoxContainer>("%RowContainer");
node.AddChild(row);
node.MoveChild(row, index);
}
public void RemoveRow(string id)
{
var r = GetRowById(id);
GetNode<VBoxContainer>("%RowContainer").RemoveChild(r);
SetContainerMinima();
}
public void AddUnassignedCard(card c)
{
@@ -131,6 +135,10 @@ public partial class game : Control
{
GetNode<card_edit_popup>("%CardEditPopup").ActivateForCard(c);
}
public void EditRowInMenu(row r)
{
GetNode<row_edit_popup>("%RowEditPopup").OpenWithRow(r);
}
public card ClaimCard(string id)
{
foreach (row r in Rows)
@@ -157,6 +165,28 @@ public partial class game : Control
return null;
}
public void MenuOpenDisableInteraction()
{
GetNode<Control>("%UiMask").MouseFilter = MouseFilterEnum.Stop;
}
public void MenuClosedEnableInteraction()
{
GetNode<Control>("%UiMask").MouseFilter = MouseFilterEnum.Ignore;
}
public void ImportGame(string filename)
{
GD.Print($"Importing from {filename}");
Serializer.LoadFromSerial(this,
File.ReadAllText(filename)
);
}
public void ExportGame(string filename, ExportSettings es)
{
GD.Print($"Exporting to {filename}");
File.WriteAllText(filename,
Serializer.CreateGameJson(this, es)
);
}
#region Commands
public void MoveCard(string cardId, string targetRowId, int? toIndex = null)
{