update cursor detection
This commit is contained in:
parent
fa1870bb3c
commit
454c15f146
1 changed files with 54 additions and 20 deletions
|
@ -12,10 +12,14 @@ script/source = "extends Node2D
|
||||||
const Speed:float = 900
|
const Speed:float = 900
|
||||||
|
|
||||||
var spaceState
|
var spaceState
|
||||||
var query
|
var query : PhysicsPointQueryParameters2D
|
||||||
var CastResult
|
var CastResult
|
||||||
var old_result
|
|
||||||
var selectedSticker
|
var hoveredObjects : Array
|
||||||
|
var hoveredObject
|
||||||
|
var hoveredSticker
|
||||||
|
var grabbedSticker
|
||||||
|
|
||||||
enum CURSOR_STATE {DEFAULT, CLICK, GRAB, GRABBED}
|
enum CURSOR_STATE {DEFAULT, CLICK, GRAB, GRABBED}
|
||||||
var currentCursorState:CURSOR_STATE
|
var currentCursorState:CURSOR_STATE
|
||||||
var oldCursorState:CURSOR_STATE
|
var oldCursorState:CURSOR_STATE
|
||||||
|
@ -60,35 +64,55 @@ func getTags(selectedObject:Node):
|
||||||
func pointcast():
|
func pointcast():
|
||||||
query.collide_with_areas = true
|
query.collide_with_areas = true
|
||||||
query.position = get_global_mouse_position()
|
query.position = get_global_mouse_position()
|
||||||
CastResult = spaceState.direct_space_state.intersect_point(query)
|
CastResult = spaceState.direct_space_state.intersect_point(query)
|
||||||
if ( (CastResult.size() > 0) and (old_result != CastResult) ):
|
# CastResult is not reliable. Objects are added randomly in the array
|
||||||
old_result = CastResult
|
# so we need to filter/sort the trace result
|
||||||
#if (CastResult.size()> 2):
|
if ( CastResult.size() > 0):
|
||||||
#breakpoint # should not detect multiple areas
|
hoveredObjects.clear()
|
||||||
for _result in CastResult:
|
for _object in CastResult:
|
||||||
if (_result.collider.input_pickable == true):
|
if(_object.collider.input_pickable == true):
|
||||||
selectedSticker = _result.collider.get_parent()
|
hoveredObjects.append(_object.collider.get_parent())
|
||||||
if (isSticker(selectedSticker)):
|
if (hoveredObjects.size() > 0):
|
||||||
print(\"selected sticker %\",selectedSticker)
|
if (hoveredObjects.size() >= 2):
|
||||||
else:
|
sortByPosY(hoveredObjects,false)
|
||||||
selectedSticker = null
|
hoveredObject = hoveredObjects[0]
|
||||||
if (CastResult.size() == 0 ):
|
print(\"Current hovered object :\", hoveredObject)
|
||||||
|
if (CastResult.size() == 0 and !grabbedSticker ):
|
||||||
CastResult.clear()
|
CastResult.clear()
|
||||||
old_result = null
|
hoveredSticker = null
|
||||||
selectedSticker = null
|
hoveredObjects.clear()
|
||||||
|
hoveredObject = null
|
||||||
|
|
||||||
|
|
||||||
|
func sortByPosY(objects: Array, ascending_order: bool = true):
|
||||||
|
var _tempArray :Array
|
||||||
|
_tempArray = objects
|
||||||
|
# Inline comparison function
|
||||||
|
_tempArray.sort_custom(comparePosY)
|
||||||
|
# Reverse if descending order is required
|
||||||
|
if not ascending_order:
|
||||||
|
_tempArray.reverse()
|
||||||
|
objects = _tempArray
|
||||||
|
|
||||||
|
func comparePosY(a, b):
|
||||||
|
#print(\"Compare \",a,\" at \",a.position.y, \" and \",b,\" at \",b.position.y )
|
||||||
|
return a.position.y < b.position.y
|
||||||
|
|
||||||
func cursor_look():
|
func cursor_look():
|
||||||
|
|
||||||
if (Input.is_action_pressed(\"select\")):
|
if (Input.is_action_pressed(\"select\")):
|
||||||
if (selectedSticker):
|
if (hoveredSticker):
|
||||||
currentCursorState = CURSOR_STATE.GRABBED
|
currentCursorState = CURSOR_STATE.GRABBED
|
||||||
|
if ($GrabTimer.is_stopped()):
|
||||||
|
$GrabTimer.start()
|
||||||
else:
|
else:
|
||||||
currentCursorState = CURSOR_STATE.CLICK
|
currentCursorState = CURSOR_STATE.CLICK
|
||||||
else:
|
else:
|
||||||
if (selectedSticker):
|
if (hoveredSticker):
|
||||||
currentCursorState = CURSOR_STATE.GRAB
|
currentCursorState = CURSOR_STATE.GRAB
|
||||||
else:
|
else:
|
||||||
currentCursorState = CURSOR_STATE.DEFAULT
|
currentCursorState = CURSOR_STATE.DEFAULT
|
||||||
|
$GrabTimer.stop()
|
||||||
|
|
||||||
if (currentCursorState != oldCursorState):
|
if (currentCursorState != oldCursorState):
|
||||||
oldCursorState = currentCursorState
|
oldCursorState = currentCursorState
|
||||||
|
@ -101,6 +125,11 @@ func cursor_look():
|
||||||
$AnimatedSprite2D.play(\"grab_intro\")
|
$AnimatedSprite2D.play(\"grab_intro\")
|
||||||
CURSOR_STATE.GRABBED:
|
CURSOR_STATE.GRABBED:
|
||||||
$AnimatedSprite2D.play(\"grab\")
|
$AnimatedSprite2D.play(\"grab\")
|
||||||
|
|
||||||
|
|
||||||
|
func _on_grab_timer_timeout():
|
||||||
|
if (hoveredSticker):
|
||||||
|
grabbedSticker = hoveredSticker
|
||||||
"
|
"
|
||||||
|
|
||||||
[sub_resource type="SpriteFrames" id="SpriteFrames_u3xkr"]
|
[sub_resource type="SpriteFrames" id="SpriteFrames_u3xkr"]
|
||||||
|
@ -158,3 +187,8 @@ frame = 2
|
||||||
frame_progress = 1.0
|
frame_progress = 1.0
|
||||||
centered = false
|
centered = false
|
||||||
offset = Vector2(-80, -190)
|
offset = Vector2(-80, -190)
|
||||||
|
|
||||||
|
[node name="GrabTimer" type="Timer" parent="."]
|
||||||
|
wait_time = 0.5
|
||||||
|
|
||||||
|
[connection signal="timeout" from="GrabTimer" to="." method="_on_grab_timer_timeout"]
|
||||||
|
|
Loading…
Reference in a new issue