Skip to content

Commit

Permalink
Added example of pickable objects reacting to controller button event…
Browse files Browse the repository at this point in the history
…s to the grab-cubes in the pickable demo.

Updates based on code review.

Fixed unnecessary use of 'self' argument.
  • Loading branch information
Malcolmnixon committed Feb 22, 2023
1 parent f250ac1 commit 94febdd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions addons/godot-xr-tools/objects/pickable.gd
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ func let_go(p_linear_velocity: Vector3, p_angular_velocity: Vector3) -> void:
emit_signal("dropped", self)


func get_picked_up_by_controller() -> ARVRController:
return by_controller


func _start_ranged_grab() -> void:
# Set state to grabbing at range and enable processing
_state = PickableState.GRABBING_RANGED
Expand Down
60 changes: 60 additions & 0 deletions scenes/pickable_demo/objects/grab_cube.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
extends XRToolsPickable


## Alternate material when button pressed
export var alternate_material : Material


# Original material
var _original_material : Material

# Current controller holding this object
var _current_controller : ARVRController


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# Get the original material
_original_material = $MeshInstance.get_active_material(0)

# Listen for when this object is picked up or dropped
connect("picked_up", self, "_on_picked_up")
connect("dropped", self, "_on_dropped")


# Called when this object is picked up
func _on_picked_up(_pickable) -> void:
# Listen for button events on the associated controller
_current_controller = get_picked_up_by_controller()
if _current_controller:
_current_controller.connect("button_pressed", self, "_on_controller_button_pressed")
_current_controller.connect("button_release", self, "_on_controller_button_released")


# Called when this object is dropped
func _on_dropped(_pickable) -> void:
# Unsubscribe to controller button events when dropped
if _current_controller:
_current_controller.disconnect("button_pressed", self, "_on_controller_button_pressed")
_current_controller.disconnect("button_release", self, "_on_controller_button_released")
_current_controller = null

# Restore original material when dropped
$MeshInstance.set_surface_material(0, _original_material)


# Called when a controller button is pressed
func _on_controller_button_pressed(button : int):
# Handle controller button presses
if button == XRTools.Buttons.VR_BUTTON_AX:
# Set alternate material when button pressed
if alternate_material:
$MeshInstance.set_surface_material(0, alternate_material)


# Called when a controller button is released
func _on_controller_button_released(button : int):
# Handle controller button releases
if button == XRTools.Buttons.VR_BUTTON_AX:
# Restore original material when button released
$MeshInstance.set_surface_material(0, _original_material)
7 changes: 5 additions & 2 deletions scenes/pickable_demo/objects/grab_cube.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=8 format=2]

[ext_resource path="res://addons/godot-xr-tools/objects/pickable.tscn" type="PackedScene" id=1]
[ext_resource path="res://addons/godot-xr-tools/objects/highlight/highlight_ring.tscn" type="PackedScene" id=2]
[ext_resource path="res://assets/wahooney.itch.io/green_grid_triplanar.tres" type="Material" id=3]
[ext_resource path="res://scenes/pickable_demo/objects/grab_cube.gd" type="Script" id=4]
[ext_resource path="res://assets/wahooney.itch.io/blue_grid.tres" type="Material" id=5]

[sub_resource type="BoxShape" id=7]
margin = 0.01
Expand All @@ -12,7 +14,8 @@ extents = Vector3( 0.05, 0.05, 0.05 )
size = Vector3( 0.1, 0.1, 0.1 )

[node name="GrabCube" instance=ExtResource( 1 )]
ranged_grab_method = 0
script = ExtResource( 4 )
alternate_material = ExtResource( 5 )

[node name="CollisionShape" parent="." index="0"]
shape = SubResource( 7 )
Expand Down
2 changes: 1 addition & 1 deletion scenes/pointer_demo/objects/color_change_cube.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ size = Vector3( 0.5, 0.5, 0.5 )
extents = Vector3( 0.25, 0.25, 0.25 )

[node name="ColorChangeCube" type="RigidBody"]
collision_layer = 1048577
mode = 1
script = ExtResource( 1 )

[node name="MeshInstance" type="MeshInstance" parent="."]
mesh = SubResource( 1 )
material/0 = null

[node name="CollisionShape" type="CollisionShape" parent="."]
shape = SubResource( 2 )

0 comments on commit 94febdd

Please sign in to comment.