mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
forgot to push for a while, got drag-and-drop mostly working
This commit is contained in:
163
row.cs
Normal file
163
row.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public partial class row : Control
|
||||
{
|
||||
[Export]
|
||||
private Color _RowColor;
|
||||
public Color RowColor
|
||||
{
|
||||
get => _RowColor;
|
||||
set
|
||||
{
|
||||
_RowColor = value;
|
||||
if (IsNodeReady())
|
||||
PropogateColor();
|
||||
}
|
||||
}
|
||||
[Export]
|
||||
private Vector2 _CardSize;
|
||||
public Vector2 CardSize
|
||||
{
|
||||
get => _CardSize;
|
||||
set
|
||||
{
|
||||
_CardSize = value;
|
||||
if (IsNodeReady())
|
||||
PropogateCardSize();
|
||||
}
|
||||
}
|
||||
[Export]
|
||||
private string _RowText;
|
||||
public string RowText
|
||||
{
|
||||
get => _RowText;
|
||||
set
|
||||
{
|
||||
_RowText = value;
|
||||
if (IsNodeReady())
|
||||
PropogateRowText();
|
||||
}
|
||||
}
|
||||
private string _RowId;
|
||||
[Export]
|
||||
public string RowId
|
||||
{
|
||||
get => _RowId;
|
||||
set
|
||||
{
|
||||
_RowId = value;
|
||||
if (IsNodeReady())
|
||||
PropogateRowId();
|
||||
}
|
||||
}
|
||||
public IEnumerable<card> Cards
|
||||
=> GetNode("%RowCardContainer").GetChildren().OfType<card>();
|
||||
private void PropogateColor()
|
||||
{
|
||||
GetNode<RowGrid>("%RowGrid").RowColor = _RowColor;
|
||||
}
|
||||
private void PropogateCardSize()
|
||||
{
|
||||
//CustomMinimumSize = new Vector2(0, _CardSize.Y);
|
||||
GetNode<RowGrid>("%RowGrid").CardSize = _CardSize;
|
||||
GetNode<Panel>("%RowTitleBoxBackground").CustomMinimumSize = _CardSize;
|
||||
}
|
||||
private void PropogateRowText()
|
||||
{
|
||||
GetNode<Label>("%RowTitleBoxLabel").Text = _RowText;
|
||||
}
|
||||
private void PropogateRowId()
|
||||
{
|
||||
GetNode<Label>("%RowTitleBoxIdLabel").Text = _RowId;
|
||||
}
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
if (false)
|
||||
{
|
||||
RowColor = new Color
|
||||
{
|
||||
R = 1,
|
||||
G = 0,
|
||||
B = 0,
|
||||
A = 1,
|
||||
};
|
||||
var scene = GD.Load<PackedScene>("res://card.tscn");
|
||||
var card = scene.Instantiate<card>();
|
||||
//GD.Print(card);
|
||||
card.CardId = "new id";
|
||||
card.CardName = "new name";
|
||||
AddCard(card);
|
||||
}
|
||||
//needs to wait until ready first
|
||||
PropogateColor();
|
||||
PropogateCardSize();
|
||||
PropogateRowText();
|
||||
PropogateRowId();
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
}
|
||||
public void DropOn(Vector2 atPosition, Variant data)
|
||||
{
|
||||
GD.Print($"Dropping at {atPosition}");
|
||||
card c = data.As<card>()
|
||||
?? throw new Exception("invalid drag data");
|
||||
var g = this.GetParentOfType<game>()
|
||||
?? throw new Exception("game instance not found");
|
||||
// if (!g.MoveCard(c.CardId, this, atPosition.X))
|
||||
// throw new Exception("failed to move card");
|
||||
if (g.ClaimCard(c.CardId) is card _c)
|
||||
{
|
||||
if (!ReferenceEquals(c, _c))
|
||||
throw new Exception("card move mismatch");
|
||||
//cards sorted with index
|
||||
foreach (var pair in Cards.Select((c, i) => (c.Position.X, i)))
|
||||
{
|
||||
if (atPosition.X >= (pair.X - c.Size.X / 2) && atPosition.X <= (pair.X + c.Size.X / 2))
|
||||
{
|
||||
GetNode("%RowCardContainer").AddChild(c);
|
||||
GetNode("%RowCardContainer").MoveChild(c, pair.i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
GetNode("%RowCardContainer").AddChild(c);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Can't find card {c.CardId}");
|
||||
}
|
||||
}
|
||||
public void AddCard(card card)
|
||||
{
|
||||
GetNode("%RowCardContainer").AddChild(card);
|
||||
}
|
||||
public card GetCard(string id)
|
||||
{
|
||||
//inefficient to iterate through all children
|
||||
return GetNode("%RowCardContainer").GetChildren().OfType<card>().FirstOrDefault(c => c.CardId == id);
|
||||
}
|
||||
public card TryRemoveCard(string id)
|
||||
{
|
||||
var c = GetCard(id);
|
||||
if (c is not null)
|
||||
{
|
||||
GetNode("%RowCardContainer").RemoveChild(c);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
public static row MakeRow(SceneTree tree)
|
||||
{
|
||||
var scene = GD.Load<PackedScene>("res://row.tscn");
|
||||
var r = scene.Instantiate<row>();
|
||||
r.RowId = tree.GetUnusedRowId();
|
||||
return r;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user