A WIP automation game
1using Electropunk.Containers;
2using Electropunk.I18n;
3using Raylib_cs;
4
5namespace Electropunk.UI;
6
7public class UIItemSlot : UINode
8{
9 public Slot Item;
10 public bool RenderSlot = true;
11
12 public UIItemSlot(Slot item, int x, int y, UINode? parent = null) : base(x, y, parent)
13 {
14 Item = item;
15 Width = 16;
16 Height = 16;
17 }
18
19 public override void Update()
20 {
21 if (IsClicked() && (Program.MouseItem.IsSomething() || Item.IsSomething()))
22 {
23 if (Program.MouseItem.IsSomething() && Item.IsSomething())
24 {
25 (Program.MouseItem, Item.Value) = (Item.Value, Program.MouseItem);
26 }
27 if (Program.MouseItem.IsEmpty())
28 {
29 Program.MouseItem = Item.Copy();
30 Item.Clear();
31 }
32 else if (Item.IsEmpty())
33 {
34 Item.SetItem(Program.MouseItem.Copy());
35 Program.MouseItem.Clear();
36 }
37 }
38
39 base.Update();
40 }
41
42 public override void Render()
43 {
44 if (RenderSlot)
45 {
46 Raylib.DrawRectangleRec(GetRectangle(), Color.Blue);
47 Raylib.DrawRectangleLinesEx(GetRectangle(), 2f, Color.SkyBlue);
48 }
49
50 if (Item.IsSomething())
51 Item.Item.Render(
52 Program.Level!,
53 Program.View,
54 new(RelativeX, RelativeY, Width, Height)
55 );
56
57 base.Render();
58 }
59
60 public override bool RenderHoverBox()
61 {
62 if (IsHovered() && Item.IsSomething())
63 {
64 string name = Lang.T($"item.{Item.Item.Key}");
65 Lang.TE($"item.{Item.Item.Key}.desc", out string? desc);
66
67 Rectangle rect = new(
68 Raylib.GetMouseX() + 8,
69 Raylib.GetMouseY() + 8,
70 Math.Max(Raylib.MeasureText(name, 20), desc != null ? Raylib.MeasureText(desc, 20) : 0) + 16,
71 20 + (desc != null ? 20 : 0) + 16
72 );
73 Raylib.DrawRectangleRec(rect, Color.Blue);
74 Raylib.DrawRectangleLinesEx(rect, 2f, Color.SkyBlue);
75
76 Raylib.DrawText(name, (int)rect.X + 8, (int)rect.Y + 8, 20, Color.White);
77 if (desc != null)
78 Raylib.DrawText(desc, (int)rect.X + 8, (int)rect.Y + 28, 20, Color.White);
79
80 return true;
81 }
82
83 return base.RenderHoverBox();
84 }
85}