Files
TierMakerGodot/card.cs
2024-04-21 15:43:32 -05:00

129 lines
2.6 KiB
C#

using Godot;
using System;
public partial class card : Control
{
private Settings Settings => GetNode<Settings>("/root/Settings");
[Export]
private string _CardName;
public string CardName
{
get => _CardName;
set
{
_CardName = value;
if (IsNodeReady())
PropogateCardName();
}
}
private void PropogateCardName()
{
GetNode<Label>("CardNameLabel").Text = _CardName;
}
[Export]
private string _CardId;
public string CardId
{
get => _CardId;
set
{
_CardId = value;
if (IsNodeReady())
PropogateCardId();
}
}
private void PropogateCardId()
{
GetNode<Label>("%CardIdLabel").Text = _CardId;
}
private Texture2D _Texture;
public void SetTexture(Texture2D texture)
{
_Texture = texture;
if (IsNodeReady())
PropogateTexture();
}
private void PropogateTexture()
{
if (_Texture is not null)
{
GetNode<TextureRect>("%CardImage").Texture = _Texture;
_Texture = null;
}
}
public Texture2D GetTexture()
=> GetNode<TextureRect>("%CardImage").Texture;
public Vector2 Center
=> Size / 2;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
PropogateCardName();
PropogateCardId();
PropogateTexture();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override Variant _GetDragData(Vector2 atPosition)
{
GD.Print($"starting to drag {CardId}");
var prev = card_preview.MakePreview(this);
var prev_root = new Control();
//prev.Position = atPosition;
prev.Position = prev.Size / -2;
prev_root.AddChild(prev);
SetDragPreview(prev_root);
return this;
}
public void SetImage(Texture2D texture)
{
Ready += () => InnerSetImage(texture);
if (IsNodeReady())
InnerSetImage(texture);
}
//only called while ready
private void InnerSetImage(Texture2D texture)
{
var node = GetNode<TextureRect>("%CardImage");
node.Texture = texture;
}
// private void HandleInput(InputEvent @event)
// {
// if (@event is InputEventMouseButton iemb)
// {
// if (iemb.ButtonIndex == MouseButton.Left)
// {
// if (iemb.Pressed)
// {
// InputSingleton.Instance.ClickedOn = this;
// StartDrag();
// }
// else
// {
// InputSingleton.Instance.ClickedOn = null;
// StopDrag();
// }
// }
// }
// }
// private void StartDrag()
// {
// }
// private void StopDrag()
// {
// }
public static card MakeCard(SceneTree tree)
{
var scene = GD.Load<PackedScene>("res://card.tscn");
var c = scene.Instantiate<card>();
c.CardId = tree.GetUnusedCardId();
return c;
}
}