Skip to content

Commit

Permalink
changed InputControls strings to StringName and implemented drag drop…
Browse files Browse the repository at this point in the history
… sistem for 2D and 3D
  • Loading branch information
ninetailsrabbit committed Oct 30, 2024
1 parent 7f7f0c9 commit f77d98b
Show file tree
Hide file tree
Showing 19 changed files with 438 additions and 50 deletions.
2 changes: 1 addition & 1 deletion autoload/general/global_day_night_clock.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ signal time_tick(day: int, hour: int, minute: int)

const MinutesPerDay: int = 1440
const MinutesPerHour: int = 60
const InGameToRealMinuteDuration: int = TAU / MinutesPerDay
const InGameToRealMinuteDuration := TAU / MinutesPerDay

## This value when it's 1.0 means that one minute in real time translates into one second in-game, so modify this value as is needed
@export var in_game_speed: float = 1.0
Expand Down
55 changes: 31 additions & 24 deletions autoload/persistence/settings/input_controls.gd
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
class_name InputControls

const MoveRight: String = "move_right"
const MoveLeft: String = "move_left"
const MoveForward: String = "move_forward"
const MoveBack: String = "move_back"

const Interact: String = "interact"
const CancelInteraction: String = "cancel_interact"

const Pull: String = "pull"
const PullArea: String = "pull_area"
const Drop: String = "drop"
const Throw: String = "throw"
const PushWave: String = "push_wave"

const Aim: String = "aim"
const Shoot: String = "shoot"

const PrimaryWeapon: String = "primary_weapon"
const SecondaryWeapon: String = "secondary_weapon"
const HeavyWeapon: String = "heavy_weapon"
const MeleeWeapon: String = "melee_weapon"

const PerformanceMetrics: String = "performance_metrics"
const PauseGame: String = "pause"
const MoveRight: StringName = &"move_right"
const MoveLeft: StringName = &"move_left"
const MoveForward: StringName = &"move_forward"
const MoveBack: StringName = &"move_back"

const CrouchAction: StringName = &"crouch"
const CrawlAction: StringName = &"crawl"
const RunAction: StringName = &"run"
const JumpAction: StringName = &"jump"

const Interact: StringName = &"interact"
const CancelInteraction: StringName = &"cancel_interact"

const Pull: StringName = &"pull"
const PullArea: StringName = &"pull_area"
const Drop: StringName = &"drop"
const Throw: StringName = &"throw"
const PushWave: StringName = &"push_wave"

const Aim: StringName = &"aim"
const Shoot: StringName = &"shoot"

const PrimaryWeapon: StringName = &"primary_weapon"
const SecondaryWeapon: StringName = &"secondary_weapon"
const HeavyWeapon: StringName = &"heavy_weapon"
const MeleeWeapon: StringName = &"melee_weapon"

const Drag: StringName = &"drag"

const PerformanceMetrics: StringName = &"performance_metrics"
const PauseGame: StringName = &"pause"
8 changes: 8 additions & 0 deletions components/drag_drop/2D/draggable_2d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions components/drag_drop/2D/draggable_2d.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dwj1m1bssf43v"
path="res://.godot/imported/draggable_2d.svg-58c1b693d1f6c4fd4492ed8cf0516dbf.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://components/drag_drop/2D/draggable_2d.svg"
dest_files=["res://.godot/imported/draggable_2d.svg-58c1b693d1f6c4fd4492ed8cf0516dbf.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
105 changes: 105 additions & 0 deletions components/drag_drop/2D/draggable_sprite_2d.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
@icon("res://components/drag_drop/2D/draggable_2d.svg")
class_name DraggableSprite2D extends Sprite2D

signal drag_started
signal drag_ended
signal drag_enabled
signal drag_disabled
signal mouse_released
signal picked_up_changed(picked: bool)


@export var reset_position_on_release: bool = true
@export var one_click_drag: bool = false
@export var smooth_factor: float = 20.0
@export var drag_input_action: StringName = InputControls.Drag:
set(value):
drag_input_action = value

set_process(InputMap.has_action(drag_input_action))

var mouse_region: Button
var current_position: Vector2 = Vector2.ZERO
var m_offset: Vector2 = Vector2.ZERO

var original_global_position: Vector2 = Vector2.ZERO
var original_position: Vector2 = Vector2.ZERO

var drag_active: bool = false:
set(value):
if drag_active != value:
drag_active = value

if drag_active:
drag_enabled.emit()
else:
drag_disabled.emit()


var picked_up: bool = false:
set(value):
if picked_up != value:
picked_up = value

picked_up_changed.emit(picked_up)

if picked_up:
drag_started.emit()
else:
drag_ended.emit()
reset_position()


func _ready() -> void:
original_global_position = global_position
original_position = position

if mouse_region == null:
mouse_region = Button.new()
mouse_region.self_modulate.a8 = 0
add_child(mouse_region)


resize_mouse_region()
mouse_region.button_down.connect(on_mouse_region_pressed)
mouse_released.connect(on_mouse_released)
texture_changed.connect(on_texture_changed)

set_process(InputMap.has_action(drag_input_action))


func _process(delta: float) -> void:
if InputMap.has_action(drag_input_action) and Input.is_action_just_released(drag_input_action) and picked_up:
mouse_released.emit()

elif mouse_region.button_pressed:
global_position = global_position.lerp(get_global_mouse_position(), smooth_factor * delta) if smooth_factor > 0 else get_global_mouse_position()
current_position = global_position + m_offset


func reset_position() -> void:
if is_inside_tree() and reset_position_on_release:
global_position = original_global_position
position = original_position


func resize_mouse_region() -> void:
if mouse_region:
mouse_region.position = Vector2.ZERO
mouse_region.anchors_preset = Control.PRESET_FULL_RECT


func on_mouse_region_pressed() -> void:
picked_up = true

if is_inside_tree():
m_offset = transform.origin - get_global_mouse_position()


func on_mouse_released() -> void:
picked_up = false


func on_texture_changed() -> void:
if texture:
resize_mouse_region()
79 changes: 79 additions & 0 deletions components/drag_drop/3D/drag_and_drop_3d.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@icon("res://components/drag_drop/3D/drag_drop_3d.svg")
class_name DragAndDrop3D extends Node3D


@export var origin_camera: Camera3D
@export var drag_distance_from_camera: float = 20.0

var current_draggable: Draggable3D:
set(value):
if value != current_draggable:
current_draggable = value

set_process_unhandled_input(current_draggable is Draggable3D)


func _unhandled_input(event: InputEvent) -> void:
if InputHelper.is_mouse_visible() and event is InputEventMouseMotion and current_draggable:
handle_drag_motion()


func _ready() -> void:
await get_tree().current_scene.ready

for draggable: Draggable3D in get_tree().get_nodes_in_group(Draggable3D.GroupName):
draggable.drag_started.connect(on_draggable_drag_started.bind(draggable))
draggable.drag_ended.connect(on_draggable_drag_ended.bind(draggable))

get_tree().root.child_entered_tree.connect(on_child_entered_tree)
get_tree().root.child_exiting_tree.connect(on_child_exiting_tree)


func handle_drag_motion():
if origin_camera and current_draggable:
var mouse_position: Vector2 = get_viewport().get_mouse_position()

var world_space := get_world_3d().direct_space_state
var from := origin_camera.project_ray_origin(mouse_position)
var to := origin_camera.project_position(mouse_position, drag_distance_from_camera)

var ray_query = PhysicsRayQueryParameters3D.create(
from,
to,
)

ray_query.exclude = [current_draggable.get_rid()]

ray_query.collide_with_areas = false
ray_query.collide_with_bodies = true

var result := world_space.intersect_ray(ray_query)

if result.has("position"):
current_draggable.target.global_position = result.position


#region Signal callbacks
func on_draggable_drag_started(draggable: Draggable3D) -> void:
current_draggable = draggable


func on_draggable_drag_ended(draggable: Draggable3D) -> void:
current_draggable = null


func on_child_entered_tree(child: Node) -> void:
if child is Draggable3D:
child.drag_started.connect(on_draggable_drag_started.bind(child))
child.drag_ended.connect(on_draggable_drag_ended.bind(child))


func on_child_exiting_tree(child: Node) -> void:
if child is Draggable3D:
if child.drag_started.is_connected(on_draggable_drag_started):
child.drag_started.disconnect(on_draggable_drag_started)

if child.drag_ended.is_connected(on_draggable_drag_started):
child.drag_ended.disconnect(on_draggable_drag_ended)

#endregion
7 changes: 7 additions & 0 deletions components/drag_drop/3D/drag_drop_3d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions components/drag_drop/3D/drag_drop_3d.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://djue3vbtyuq73"
path="res://.godot/imported/drag_drop_3d.svg-8f3ab5ff1ea8f347f604b93391d16b3c.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://components/drag_drop/3D/drag_drop_3d.svg"
dest_files=["res://.godot/imported/drag_drop_3d.svg-8f3ab5ff1ea8f347f604b93391d16b3c.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
Loading

0 comments on commit f77d98b

Please sign in to comment.