Skip to content

Commit

Permalink
🗯️ Add tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrijnbeek committed Oct 1, 2023
1 parent 51d3864 commit 2893c41
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 21 deletions.
22 changes: 18 additions & 4 deletions entities/itemselect/ItemSelect.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
using Godot;

public sealed class ItemSelect : Area2D
public sealed class ItemSelect : Node
{
[Signal]
public delegate void ItemChosen(Item item);

private Node2D cursor = null!;
private BottomHud hud = null!;
private Vector2 mousePos = Vector2.Zero;
private Inventory? inventory;

public override void _Ready()
{
mousePos = GetGlobalMousePosition();
Position = mousePos;
cursor = GetNode<Node2D>("Cursor");
hud = GetNode<BottomHud>("BottomHud");
mousePos = cursor.GetGlobalMousePosition();
cursor.Position = mousePos;
}

public override void _Process(float delta)
{
base._Process(delta);
Position = mousePos;
cursor.Position = mousePos;

if (inventory?.TryFindItem(mousePos, out var item) ?? false)
{
hud.SetItem(item.Type);
}
else
{
hud.ClearItem();
}
}

public override void _Input(InputEvent @event)
Expand Down Expand Up @@ -50,6 +63,7 @@ private void onAreaExited(Area2D other)
if (other == inventory)
{
inventory = null;
hud.ClearItem();
}
}
}
16 changes: 11 additions & 5 deletions entities/itemselect/itemselect.tscn
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://entities/itemselect/ItemSelect.cs" type="Script" id=1]
[ext_resource path="res://scenes/main/BottomHud.tscn" type="PackedScene" id=2]

[sub_resource type="CircleShape2D" id=1]
radius = 1.0

[node name="ItemSelect" type="Area2D"]
[node name="ItemSelect" type="Node"]
script = ExtResource( 1 )

[node name="Cursor" type="CollisionShape2D" parent="."]
[node name="BottomHud" parent="." instance=ExtResource( 2 )]
grow_vertical = 0

[node name="Cursor" type="Area2D" parent="."]

[node name="CursorShape" type="CollisionShape2D" parent="Cursor"]
shape = SubResource( 1 )

[connection signal="area_entered" from="." to="." method="onAreaEntered"]
[connection signal="area_exited" from="." to="." method="onAreaExited"]
[connection signal="area_entered" from="Cursor" to="." method="onAreaEntered"]
[connection signal="area_exited" from="Cursor" to="." method="onAreaExited"]
27 changes: 27 additions & 0 deletions scenes/main/BottomHud.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Godot;

public class BottomHud : Control
{
public void ClearItem()
{
var label = GetNode<RichTextLabel>("Label");
var frame = GetNode<NinePatchRect>("Frame");

label.BbcodeText = "";
frame.RectMinSize = Vector2.Zero;
Visible = false;
}

public void SetItem(ItemLibrary.ItemType item)
{
var label = GetNode<RichTextLabel>("Label");
var frame = GetNode<NinePatchRect>("Frame");

var name = item.ToString();
var desc = item.Description();

label.BbcodeText = $"[color=yellow]{name}[/color] {desc}";
frame.RectMinSize = new Vector2(0, label.RectSize.y + 3);
Visible = true;
}
}
28 changes: 20 additions & 8 deletions scenes/main/BottomHud.tscn
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://assets/images/frame.png" type="Texture" id=1]
[ext_resource path="res://assets/Minimal4.tres" type="DynamicFont" id=2]
[ext_resource path="res://scenes/main/BottomHud.cs" type="Script" id=3]

[node name="BottomHud" type="Control"]
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 73.0
margin_top = 96.0
margin_right = -3.0
margin_left = -120.0
margin_top = -12.0
margin_right = -4.0
margin_bottom = -4.0
grow_horizontal = 0
grow_vertical = 0
size_flags_vertical = 3
script = ExtResource( 3 )

[node name="Frame" type="NinePatchRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
grow_vertical = 0
texture = ExtResource( 1 )
region_rect = Rect2( 0, 0, 7, 7 )
patch_margin_left = 2
patch_margin_top = 2
patch_margin_right = 2
patch_margin_bottom = 2

[node name="RichTextLabel" type="RichTextLabel" parent="."]
[node name="Label" type="RichTextLabel" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 2.0
margin_top = 2.0
margin_right = -2.0
margin_bottom = -2.0
margin_bottom = -1.0
grow_vertical = 0
size_flags_horizontal = 0
size_flags_vertical = 0
custom_fonts/normal_font = ExtResource( 2 )
bbcode_enabled = true
bbcode_text = "Sword or something. [color=red]4 damage[/color]"
bbcode_text = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"
meta_underlined = false
text = "Sword or something. 4 damage"
text = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"
fit_content_height = true
scroll_active = false
__meta__ = {
"_editor_description_": ""
Expand Down
5 changes: 1 addition & 4 deletions scenes/main/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=2]
[gd_scene load_steps=10 format=2]

[ext_resource path="res://scenes/inventory/inventory.tscn" type="PackedScene" id=1]
[ext_resource path="res://scenes/story/Dialogue.tscn" type="PackedScene" id=2]
Expand All @@ -7,7 +7,6 @@
[ext_resource path="res://scenes/main/TopHud.tscn" type="PackedScene" id=5]
[ext_resource path="res://entities/player/player.tscn" type="PackedScene" id=6]
[ext_resource path="res://scenes/main/ShakeCamera.cs" type="Script" id=7]
[ext_resource path="res://scenes/main/BottomHud.tscn" type="PackedScene" id=8]

[sub_resource type="Animation" id=1]
resource_name = "InventoryClose"
Expand Down Expand Up @@ -58,8 +57,6 @@ margin_top = 5.0
margin_right = -111.0
margin_bottom = -86.0

[node name="BottomHud" parent="." instance=ExtResource( 8 )]

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
method_call_mode = 1
anims/InventoryClose = SubResource( 1 )
Expand Down

0 comments on commit 2893c41

Please sign in to comment.