checheche/Robo-Bus-A31-HMI/common/base/base_control.gd
2024-12-11 23:26:40 +08:00

181 lines
4.6 KiB
GDScript

@tool
class_name BaseControl
extends Control
signal result_data(value)
signal init_complete()
signal load_anim_complete()
signal on_click(_node)
signal on_long_click(_node)
#@export var long_time = 800
var clickTime = 0
var clickPosition = Vector2.ZERO
var itemRoot : BaseControl = null
enum {
RESULT_OK,
RESULT_CANCELED
}
class GResult :
var code = RESULT_OK
var data = null
func _init(p_code,p_data = null):
self.code = p_code
self.data = p_data
@export var styleBox : StyleBox = null : set = set_styleBox
@export var enter_anim : Animation = null
@export var exit_anim : Animation = null
@export var is_anim = true
@export var on_click_anim : Animation = null
@export var _pivot_offset = "CENTER"
@export var is_Scale = false
@export var is_Rotation = false
@export var OnClickScaleArrNode : Array[Node] = []
var is_OnClickScale = false
@export var size_max : float = 1;
@export var is_OnClickScaleAwt = false
@export var StatusTime = 0.2
@export var scaleMax : Vector2 = Vector2(1,1);
@export var scaleMin : Vector2 = Vector2(0,0);
var baseAnimationPlayer : AnimationPlayer = null
func _ready():
await get_tree().process_frame
OnClickScale()
if is_anim:
init_anim()
playEnter()
await get_tree().create_timer(0.2).timeout
init_complete.emit()
func init_anim():
if enter_anim == null and exit_anim == null :
return
baseAnimationPlayer = AnimationPlayer.new()
var animationLibrary = AnimationLibrary.new()
if enter_anim != null :
animationLibrary.add_animation("enter",enter_anim)
if exit_anim != null :
animationLibrary.add_animation("exit",exit_anim)
if on_click_anim != null:
animationLibrary.add_animation("on_click_anim",on_click_anim)
baseAnimationPlayer.add_animation_library("",animationLibrary)
add_child(baseAnimationPlayer)
#
func playEnter():
if enter_anim != null and baseAnimationPlayer != null :
baseAnimationPlayer.play("enter")
func playExit():
if exit_anim != null and baseAnimationPlayer != null :
baseAnimationPlayer.play("exit")
await baseAnimationPlayer.animation_finished
#func queue_free():
#if is_anim:
#await playExit()
#super.queue_free()
func set_styleBox(value) :
styleBox = value
queue_redraw()
var is_move_r = false
func _gui_input(event):
if event is InputEventScreenTouch :
is_move_r = event.pressed
if event.pressed == true:
clickTime = Time.get_ticks_msec()
clickPosition = event.position
pressed_true()
if event.pressed == false:
var diff = Time.get_ticks_msec() - clickTime
clickTime = 0
#if event.position.distance_to(clickPosition)<2 :
if mouse_filter == Control.MOUSE_FILTER_STOP :
accept_event()
if diff < 800:
emit_signal("on_click",self)
if on_click_anim != null and baseAnimationPlayer != null :
baseAnimationPlayer.play("on_click_anim")
click()
if is_Scale:
onScale()
if is_Rotation:
onRotation()
is_OnClickScale = !is_OnClickScale
OnClickScale()
else :
emit_signal("on_long_click",self)
long_click()
pressed_false()
func OnClickScale():
for node : Node in OnClickScaleArrNode:
if is_OnClickScale:
node.onStart()
else:
node.onEnd()
await get_tree().create_timer(StatusTime).timeout
func onScale():
var tween = create_tween()
tween.tween_property(self, "scale", Vector2(1 + (0.2*size_max),1 + (0.2*size_max)), 0.1)
tween.tween_property(self, "scale", Vector2(1 - (0.05*size_max),1 - (0.05*size_max)), 0.15)
tween.tween_property(self, "scale", Vector2(1,1), 0.1)
func onRotation():
var tween = create_tween()
tween.tween_property(self, "rotation_degrees", 50*size_max, 0.1)
tween.tween_property(self, "rotation_degrees", -60*size_max, 0.15)
tween.tween_property(self, "rotation_degrees", 0, 0.1)
func onStart():
#visible = true
#_pivot_offset = "CENTER_Left"
scale = scaleMin
var tween = create_tween()
tween.tween_property(self, "scale", scaleMin - (scaleMin*0.05), 0.1)
tween.tween_property(self, "scale", scaleMax , StatusTime)
func onEnd():
#_pivot_offset = "CENTER_RIGHT"
scale = scaleMax
var tween = create_tween()
tween.tween_property(self, "scale", scaleMin, StatusTime)
func pressed_true():
pass
func pressed_false():
pass
func _physics_process(delta: float) -> void:
if _pivot_offset == "CENTER":
pivot_offset = size/2
if _pivot_offset == "CENTER_RIGHT":
pivot_offset.x = size.x
pivot_offset.y = size.y/2
if _pivot_offset == "CENTER_Left":
pivot_offset.x = 0
pivot_offset.y = size.y/2
func _process(delta):
if not Rect2(global_position, global_position+size).has_point(get_global_mouse_position()):
pressed_false()
func click():
pass
func long_click():
pass
func _draw():
if styleBox !=null :
draw_style_box(styleBox, Rect2(Vector2(0,0) , size ))