bugfix, desync, and add UI function
This commit is contained in:
@@ -40,77 +40,108 @@ func set_player(p_player: Node3D):
|
||||
local_player = p_player
|
||||
|
||||
func _create_touch_ui():
|
||||
print("[TouchControls] Creating touch UI...")
|
||||
print("[TouchControls] Creating/Finding touch UI...")
|
||||
# Use layer 10 - above regular UI but below pause menu
|
||||
layer = 10
|
||||
|
||||
# Create main container
|
||||
var container = Control.new()
|
||||
container.name = "TouchControls"
|
||||
container.set_anchors_preset(Control.PRESET_FULL_RECT)
|
||||
container.mouse_filter = Control.MOUSE_FILTER_PASS # Pass input to children
|
||||
add_child(container)
|
||||
# Check if container already exists (added in scene)
|
||||
var container = get_node_or_null("TouchControls")
|
||||
|
||||
# Create virtual joystick (bottom-left)
|
||||
var joystick_script = load("res://scripts/ui/virtual_joystick.gd")
|
||||
virtual_joystick = Control.new()
|
||||
virtual_joystick.set_script(joystick_script)
|
||||
virtual_joystick.name = "VirtualJoystick"
|
||||
virtual_joystick.set_anchors_preset(Control.PRESET_BOTTOM_LEFT)
|
||||
if not container:
|
||||
# Create main container if missing
|
||||
container = Control.new()
|
||||
container.name = "TouchControls"
|
||||
container.set_anchors_preset(Control.PRESET_FULL_RECT)
|
||||
container.mouse_filter = Control.MOUSE_FILTER_PASS # Pass input to children
|
||||
add_child(container)
|
||||
else:
|
||||
print("[TouchControls] Found existing TouchControls container")
|
||||
|
||||
# Helper to find or create control
|
||||
var find_or_create_joystick = func():
|
||||
var joy = container.get_node_or_null("VirtualJoystick")
|
||||
if joy:
|
||||
print("[TouchControls] Found existing VirtualJoystick")
|
||||
return joy
|
||||
|
||||
var joystick_script = load("res://scripts/ui/virtual_joystick.gd")
|
||||
joy = Control.new()
|
||||
joy.set_script(joystick_script)
|
||||
joy.name = "VirtualJoystick"
|
||||
joy.set_anchors_preset(Control.PRESET_BOTTOM_LEFT)
|
||||
|
||||
# Use standard size from joystick script defaults (radius 60 -> size 160)
|
||||
var joy_size = Vector2(160, 160)
|
||||
joy.custom_minimum_size = joy_size
|
||||
joy.size = joy_size
|
||||
|
||||
joy.offset_left = 120
|
||||
joy.offset_top = -280
|
||||
joy.offset_right = 280
|
||||
joy.offset_bottom = -120
|
||||
|
||||
container.add_child(joy)
|
||||
return joy
|
||||
|
||||
virtual_joystick = find_or_create_joystick.call()
|
||||
if not virtual_joystick.direction_changed.is_connected(_on_joystick_direction):
|
||||
virtual_joystick.direction_changed.connect(_on_joystick_direction)
|
||||
|
||||
# Use standard size from joystick script defaults (radius 60 -> size 160)
|
||||
var joy_size = Vector2(160, 160)
|
||||
virtual_joystick.custom_minimum_size = joy_size
|
||||
virtual_joystick.size = joy_size
|
||||
|
||||
# Position relative to Bottom-Left anchor
|
||||
# joystick_position (120, -120) interpreted as margin from anchor
|
||||
# x=120 (right from left edge), y=-120 (up from bottom edge - implies bottom margin)
|
||||
# We want the *center* or *bottom-left* corner?
|
||||
# Assuming (120, -120) is top-left corner of the control relative to anchor?
|
||||
# Let's align bottom-left corner of control to (120, -120) from screen bottom-left
|
||||
# Screen Bottom-Left is (0, 1) in normalized anchors.
|
||||
# offset_left = 120
|
||||
# offset_bottom = -120 (120px up from bottom)
|
||||
# offset_top = -120 - 160 = -280
|
||||
# offset_right = 120 + 160 = 280
|
||||
|
||||
virtual_joystick.offset_left = 120
|
||||
virtual_joystick.offset_top = -280
|
||||
virtual_joystick.offset_right = 280
|
||||
virtual_joystick.offset_bottom = -120
|
||||
|
||||
virtual_joystick.direction_changed.connect(_on_joystick_direction)
|
||||
container.add_child(virtual_joystick)
|
||||
# Helper to find or create button logic moved to function _find_or_create_action_button
|
||||
|
||||
# Create action buttons (bottom-right)
|
||||
grab_button = _create_action_button("Grab", "👋", button_positions.grab)
|
||||
put_button = _create_action_button("Put", "📦", button_positions.put)
|
||||
special_button = _create_action_button("Special", "⚡", button_positions.special)
|
||||
|
||||
container.add_child(grab_button)
|
||||
container.add_child(put_button)
|
||||
container.add_child(special_button)
|
||||
grab_button = _find_or_create_action_button(container, "Grab", "👋", button_positions.grab)
|
||||
put_button = _find_or_create_action_button(container, "Put", "📦", button_positions.put)
|
||||
special_button = _find_or_create_action_button(container, "Special", "⚡", button_positions.special)
|
||||
|
||||
# Create settings button (top-right corner)
|
||||
settings_button = Button.new()
|
||||
settings_button.name = "SettingsBtn"
|
||||
settings_button.text = "⚙"
|
||||
settings_button.set_anchors_preset(Control.PRESET_TOP_RIGHT)
|
||||
settings_button.offset_left = -70 # Use offsets instead of position for anchored controls
|
||||
settings_button.offset_right = -20
|
||||
settings_button.offset_top = 70
|
||||
settings_button.offset_bottom = 120
|
||||
settings_button.custom_minimum_size = Vector2(50, 50)
|
||||
settings_button.mouse_filter = Control.MOUSE_FILTER_STOP # Ensure it receives input
|
||||
settings_button.pressed.connect(_on_settings_pressed)
|
||||
_style_button(settings_button, 0.5)
|
||||
container.add_child(settings_button)
|
||||
settings_button = container.get_node_or_null("SettingsBtn")
|
||||
if not settings_button:
|
||||
settings_button = Button.new()
|
||||
settings_button.name = "SettingsBtn"
|
||||
settings_button.text = "⚙"
|
||||
settings_button.set_anchors_preset(Control.PRESET_TOP_RIGHT)
|
||||
settings_button.offset_left = -70
|
||||
settings_button.offset_right = -20
|
||||
settings_button.offset_top = 70
|
||||
settings_button.offset_bottom = 120
|
||||
settings_button.custom_minimum_size = Vector2(50, 50)
|
||||
settings_button.mouse_filter = Control.MOUSE_FILTER_STOP
|
||||
_style_button(settings_button, 0.5)
|
||||
container.add_child(settings_button)
|
||||
|
||||
if not settings_button.pressed.is_connected(_on_settings_pressed):
|
||||
settings_button.pressed.connect(_on_settings_pressed)
|
||||
|
||||
# Always visible now - controlled by settings toggle
|
||||
# Can be hidden via settings if user doesn't want touch controls on desktop
|
||||
visible = true
|
||||
|
||||
func _find_or_create_action_button(container: Control, button_name: String, icon: String, pos: Vector2) -> Button:
|
||||
var btn = container.get_node_or_null(button_name + "Btn")
|
||||
if btn:
|
||||
print("[TouchControls] Found existing %s button" % button_name)
|
||||
# Style it and connect
|
||||
_style_button(btn, button_opacity)
|
||||
# Avoid duplicate signal connections
|
||||
if not btn.button_down.is_connected(_on_button_pressed): # Wait, cannot check lambda easily
|
||||
# Disconnect all to be safe if previously connected
|
||||
for conn in btn.button_down.get_connections():
|
||||
if conn["callable"].get_object() == self:
|
||||
btn.button_down.disconnect(conn["callable"])
|
||||
for conn in btn.button_up.get_connections():
|
||||
if conn["callable"].get_object() == self:
|
||||
btn.button_up.disconnect(conn["callable"])
|
||||
|
||||
btn.button_down.connect(func(): _on_button_pressed(button_name))
|
||||
btn.button_up.connect(func(): _on_button_released(button_name))
|
||||
return btn
|
||||
|
||||
# Create new
|
||||
var new_btn = _create_action_button(button_name, icon, pos)
|
||||
container.add_child(new_btn)
|
||||
return new_btn
|
||||
|
||||
func _create_action_button(button_name: String, icon: String, pos: Vector2) -> Button:
|
||||
var btn = Button.new()
|
||||
btn.name = button_name + "Btn"
|
||||
|
||||
Reference in New Issue
Block a user