Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds radial menu #31

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@tool
extends Container


func _draw() -> void:
var child_count = get_child_count()
var pie_slice_radians = 2 * PI / child_count
var child_index = 0

for child in get_children():
if child is Control:
# move child to a pie slice starting from the center of the container
var child_angle = child_index * pie_slice_radians
# var child_radius = min(size.x, size.y) / 2
var child_radius = min(size.x, size.y) / 2
var child_position = Vector2(
child_radius * cos(child_angle),
child_radius * sin(child_angle)
)

child_position.x += size.x / 2 - child.size.x / 2
child_position.y += size.y / 2 - child.size.y / 2

child.set_position(child_position)

child_index += 1


func _enter_tree() -> void:
child_entered_tree.connect(_handle_child_entered_tree)
child_exiting_tree.connect(_handle_child_exiting_tree)


func _handle_child_entered_tree(child: Node) -> void:
_draw()


func _handle_child_exiting_tree(child: Node) -> void:
_draw()


func _process(delta: float) -> void:
if Engine.is_editor_hint():
_draw()
3 changes: 3 additions & 0 deletions addons/godot_gameplay_systems/inventory_system/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const drop_table_script = preload("res://addons/godot_gameplay_systems/inventory
const drop_script = preload("res://addons/godot_gameplay_systems/inventory_system/nodes/drop.gd")
const drop_2d_script = preload("res://addons/godot_gameplay_systems/inventory_system/nodes/drop_2d.gd")
const drop_3d_script = preload("res://addons/godot_gameplay_systems/inventory_system/nodes/drop_3d.gd")
const radial_menu_script = preload("res://addons/godot_gameplay_systems/inventory_system/nodes/radial_menu_container.gd")


func _enter_tree() -> void:
Expand All @@ -28,9 +29,11 @@ func _enter_tree() -> void:
add_custom_type("EquippedItem3D", "Node3D", equipped_item_3d, preload("res://addons/godot_gameplay_systems/inventory_system/assets/Equipped3DIcon.png"))
add_custom_type("PickableArea2D", "Area2D", pickable_item_2d, null)
add_custom_type("PickableArea3D", "Area3D", pickable_item_3d, null)
add_custom_type("RadialMenuContainer", "Container", radial_menu_script, null)


func _exit_tree() -> void:
remove_custom_type("RadialMenuContainer")
remove_custom_type("PickableArea3D")
remove_custom_type("PickableArea2D")
remove_custom_type("EquippedItem3D")
Expand Down
7 changes: 7 additions & 0 deletions docs/inventory/radial_menu_container.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RadialMenuContainer
===================

A convenient `Container` useful to create radial menus.

It inherits from `Container` and adds just a `_draw` method (which is called automatically at `_process` if inside the editor).

11 changes: 10 additions & 1 deletion examples/sot_like/player/sot_like_player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity", 9
@onready var interaction_explanation: Label = $BoxContainer/InteractionExplanation/ExplanationLabel
@onready var right_equipped_item: EquippedItem3D = $CameraNeck/Camera3D/RightEquippedItem
@onready var center_equipped_item: EquippedItem3D = $CameraNeck/Camera3D/CenterEquippedItem
@onready var radial_menu_container: Control = $RadialMenuContainer


var is_dragging: bool = false:
Expand All @@ -41,6 +42,7 @@ var is_blocked_from_moving_while_interacting: bool = false:

func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
radial_menu_container.visible = false


func _handle_interaction(_delta: float) -> void:
Expand All @@ -62,6 +64,13 @@ func _handle_movement(_delta: float) -> void:
if Input.is_action_just_pressed("fps_drop") and is_dragging:
interaction_manager.end_interaction()

radial_menu_container.visible = Input.is_action_pressed("diablo_like_inventory")

if radial_menu_container.visible:
Input.mouse_mode = Input.MOUSE_MODE_CONFINED
else:
Input.mouse_mode = Input.MOUSE_MODE_CONFINED_HIDDEN

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("fps_strafe_left", "fps_strafe_right", "fps_move_forward", "fps_move_backward")
Expand All @@ -88,7 +97,7 @@ func _physics_process(delta: float) -> void:


func _input(event: InputEvent):
if event is InputEventMouseMotion:
if event is InputEventMouseMotion and not radial_menu_container.visible:
var y = event.relative.x * mouse_sensitivity
var x = event.relative.y * mouse_sensitivity
rotate_y(-y)
Expand Down
49 changes: 48 additions & 1 deletion examples/sot_like/player/sot_like_player.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[gd_scene load_steps=8 format=3 uid="uid://c8cywrb5tgm6k"]
[gd_scene load_steps=9 format=3 uid="uid://c8cywrb5tgm6k"]

[ext_resource type="Script" path="res://examples/sot_like/player/sot_like_player.gd" id="1_68dqe"]
[ext_resource type="Script" path="res://addons/godot_gameplay_systems/interactables/nodes/3d/interaction_raycast_3d.gd" id="2_3wm4b"]
[ext_resource type="Script" path="res://addons/godot_gameplay_systems/inventory_system/nodes/equipped_item_3d.gd" id="3_eihw1"]
[ext_resource type="Script" path="res://addons/godot_gameplay_systems/interactables/nodes/interaction_manager.gd" id="3_qgbfi"]
[ext_resource type="Script" path="res://addons/godot_gameplay_systems/inventory_system/nodes/equipment.gd" id="5_cwv7t"]
[ext_resource type="Script" path="res://addons/godot_gameplay_systems/inventory_system/nodes/inventory.gd" id="6_n2esb"]
[ext_resource type="Script" path="res://addons/godot_gameplay_systems/inventory_system/nodes/radial_menu_container.gd" id="7_3hmpf"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_b0i2f"]

Expand Down Expand Up @@ -71,3 +72,49 @@ size_flags_vertical = 4

[node name="ExplanationLabel" type="Label" parent="BoxContainer/InteractionExplanation"]
layout_mode = 2

[node name="RadialMenuContainer" type="Container" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 72.0
offset_top = 32.0
offset_right = -42.0
offset_bottom = -32.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 6
size_flags_vertical = 6
script = ExtResource("7_3hmpf")

[node name="Button" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button2" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button3" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button4" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button5" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button6" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button7" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"

[node name="Button8" type="Button" parent="RadialMenuContainer"]
layout_mode = 2
text = "test item"