Files
stickman/scripts/test_harness.gd
T
ryan 273090993e feat: Implement facing profiles and bend direction toggles in test harness
- Added functionality for skeleton IK bone switches, allowing users to dynamically change the bend direction of joints via a context menu.
- Introduced facing profiles (LEFT, RIGHT, FORWARD) that modify the bend direction of limbs based on the selected profile.
- Implemented body part z-ordering based on the facing profile, ensuring correct rendering order of limbs relative to the torso.
- Added a coordinates display panel that shows the position and rotation of the Skeleton2D and its bones, as well as IK target positions.
- Created a new script to generate a walk animation for the stickman rig, including keyframe tracks for various IK targets.
- Updated documentation to reflect the new features and their specifications.
2026-08-23 22:21:23 -04:00

834 lines
28 KiB
GDScript

extends Control
## TestHarness - Standalone runtime rig viewer / IK playground (Phase 9).
##
## Loads .stk files via StickmanFactory, spawns a master_rig.tscn instance into
## a SubViewport, and provides a debug overlay (skeleton bones + IK handles)
## plus interactive IK-handle dragging. NOT wired into the editor — run
## standalone via F6 on res://scenes/test_harness.tscn.
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
const MIN_ZOOM: float = 0.1
const MAX_ZOOM: float = 5.0
const ZOOM_STEP: float = 1.10
const HANDLE_HIT_RADIUS_PX: float = 12.0 # screen-space grab radius
const HANDLE_DRAW_RADIUS_PX: float = 6.0 # screen-space marker radius
const JOINT_DOT_RADIUS_PX: float = 4.0 # screen-space bone-joint dot radius
const BONE_LINE_WIDTH_PX: float = 2.0 # screen-space bone line width
const BONE_COLOR_LEFT := Color(0.35, 0.70, 1.00)
const BONE_COLOR_RIGHT := Color(1.00, 0.50, 0.20)
const BONE_COLOR_CENTRAL := Color(1.00, 1.00, 1.00)
const HANDLE_COLOR_HAND := Color(0.00, 1.00, 0.00) # green
const HANDLE_COLOR_FOOT := Color(0.00, 0.50, 1.00) # blue
const HANDLE_COLOR_HEAD := Color(1.00, 1.00, 0.00) # yellow
const HANDLE_COLOR_TORSO := Color(1.00, 0.00, 1.00) # magenta
const SKELETON_PATH := "Skeleton2D"
const BODY_CONTAINER_PATH := "Body"
## Width of the coordinates readout panel (Task 3).
const COORDS_PANEL_WIDTH: float = 320.0
## Bone node paths (relative to Skeleton2D) shown in the coordinates panel, in
## display order. The display name is the last path segment.
const COORD_BONE_PATHS: Array[String] = [
"Torso",
"Torso/Head",
"Torso/LeftUpperArm",
"Torso/LeftUpperArm/LeftLowerArm",
"Torso/RightUpperArm",
"Torso/RightUpperArm/RightLowerArm",
"Torso/LeftUpperLeg",
"Torso/LeftUpperLeg/LeftLowerLeg",
"Torso/RightUpperLeg",
"Torso/RightUpperLeg/RightLowerLeg",
]
## Quick-load buttons: [label, res:// path].
const QUICK_LOADS: Array = [
["Break", "res://stickmen/break.stk"],
["Basic", "res://stickmen/basic.stk"],
["Test", "res://stickmen/test.stk"],
]
## IK handle node paths (relative to rig root), keyed by handle name.
## The four limb targets drive TwoBoneIK; "Head" is the LookAt aim point and
## "Torso" is the hip anchor (dragging it translates the whole rig via its
## RemoteTransform2D — no target-following logic).
const IK_HANDLE_PATHS: Dictionary = {
"Left_Hand": "IK_Targets/Left_Hand",
"Right_Hand": "IK_Targets/Right_Hand",
"Left_Leg": "IK_Targets/Left_Leg",
"Right_Leg": "IK_Targets/Right_Leg",
"Head": "IK_Targets/Head",
"Torso": "IK_Targets/Torso",
}
## Leaf bone → IK target node path (relative to rig root), keyed by bone name.
## Used by the bone overlay to draw the forearm/shin segments out to their
## wrist/ankle targets. The Head leaf is NOT listed here — its IK target is a
## LookAt aim point, so it draws along its own bone direction instead (see
## _draw_bones' leaf fallback).
const LEAF_BONE_IK_PATHS: Dictionary = {
"LeftLowerArm": "IK_Targets/Left_Hand",
"RightLowerArm": "IK_Targets/Right_Hand",
"LeftLowerLeg": "IK_Targets/Left_Leg",
"RightLowerLeg": "IK_Targets/Right_Leg",
}
## Facing profiles for the rig's TwoBoneIK "Flip Bend Direction" flags (Phase
## 9 / Task 1). LEFT = facing left, RIGHT = facing right, FORWARD = facing the
## user. The enum values are used directly as the facing-menu item ids.
enum FacingProfile { LEFT, RIGHT, FORWARD }
const JOINT_HIT_RADIUS_PX: float = 14.0 # screen-space grab radius for bend joints
## Bend joints (upper↔lower limb connectors) whose TwoBoneIK "Flip Bend
## Direction" flag is user-controllable. Keyed by joint name; values are the
## lower bone's NodePath relative to the Skeleton2D.
const BEND_JOINTS: Array[String] = ["LeftArm", "RightArm", "LeftLeg", "RightLeg"]
const BEND_JOINT_BONE_PATHS: Dictionary = {
"LeftArm": "Torso/LeftUpperArm/LeftLowerArm",
"RightArm": "Torso/RightUpperArm/RightLowerArm",
"LeftLeg": "Torso/LeftUpperLeg/LeftLowerLeg",
"RightLeg": "Torso/RightUpperLeg/RightLowerLeg",
}
## flip_bend_direction value per facing profile, keyed by bend-joint name.
const PROFILE_FLAGS: Dictionary = {
FacingProfile.LEFT: { "LeftArm": false, "RightArm": false, "LeftLeg": true, "RightLeg": true },
FacingProfile.RIGHT: { "LeftArm": true, "RightArm": true, "LeftLeg": false, "RightLeg": false },
FacingProfile.FORWARD: { "LeftArm": false, "RightArm": true, "LeftLeg": true, "RightLeg": false },
}
## Body/* visual part node names in draw order (back-to-front) per facing
## profile (Phase 9 / Task 2). First entry draws backmost, last frontmost.
## Upper limbs stay behind lower limbs; on the far (behind-torso) side the arm
## pair draws behind the leg pair, on the near (in-front) side the arm pair
## draws in front of the leg pair; the head is always frontmost.
const Z_ORDER_BY_PROFILE: Dictionary = {
FacingProfile.FORWARD: [
"Body",
"LeftUpperLeg", "RightUpperLeg",
"LeftLowerLeg", "RightLowerLeg",
"LeftUpperArm", "RightUpperArm",
"LeftLowerArm", "RightLowerArm",
"Head",
],
FacingProfile.LEFT: [
"LeftUpperArm", "LeftLowerArm",
"LeftUpperLeg", "LeftLowerLeg",
"Body",
"RightUpperLeg", "RightLowerLeg",
"RightUpperArm", "RightLowerArm",
"Head",
],
FacingProfile.RIGHT: [
"RightUpperArm", "RightLowerArm",
"RightUpperLeg", "RightLowerLeg",
"Body",
"LeftUpperLeg", "LeftLowerLeg",
"LeftUpperArm", "LeftLowerArm",
"Head",
],
}
# ---------------------------------------------------------------------------
# Runtime-built node references
# ---------------------------------------------------------------------------
var _viewport_container: SubViewportContainer
var _viewport: SubViewport
var _world: Node2D
var _camera: Camera2D
var _debug_overlay: Node2D
var _status_label: Label
var _file_dialog: FileDialog
var _coords_panel: PanelContainer
var _coords_label: RichTextLabel
# ---------------------------------------------------------------------------
# State
# ---------------------------------------------------------------------------
var _rig: Node2D = null
var _skeleton: Skeleton2D = null
var _body_container: Node2D = null
var _ik_handles: Dictionary = {} # { String : Marker2D }
var _coord_bones: Dictionary = {} # { String : Bone2D }
var _show_bones: bool = true
var _show_ik: bool = true
var _show_coords: bool = true
var _is_panning: bool = false
var _pan_last: Vector2 = Vector2.ZERO
var _dragging_handle: Marker2D = null
# Task 1: IK bend-direction state (survives respawn; matches authored defaults).
var _facing_profile: int = FacingProfile.FORWARD
var _context_joint: String = ""
var _bend_joint_bones: Dictionary = {} # { String : Bone2D }
var _bend_modifications: Dictionary = {} # { String : SkeletonModification2DTwoBoneIK }
var _facing_button: MenuButton
var _facing_menu: PopupMenu
var _context_menu: PopupMenu
# ---------------------------------------------------------------------------
# Lifecycle
# ---------------------------------------------------------------------------
func _ready() -> void:
_build_ui()
_viewport_container.resized.connect(_on_viewport_container_resized)
call_deferred("_on_viewport_container_resized")
func _input(event: InputEvent) -> void:
if _file_dialog != null and _file_dialog.visible:
return
if event is InputEventMouseButton:
_handle_mouse_button(event as InputEventMouseButton)
elif event is InputEventMouseMotion:
_handle_mouse_motion(event as InputEventMouseMotion)
# ---------------------------------------------------------------------------
# UI construction
# ---------------------------------------------------------------------------
func _build_ui() -> void:
# Dark background behind the transparent SubViewport.
var bg := ColorRect.new()
bg.color = Color(0.10, 0.10, 0.10, 1.0)
bg.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(bg)
# Top UI bar.
var top_bar := PanelContainer.new()
top_bar.set_anchors_and_offsets_preset(Control.PRESET_TOP_WIDE)
top_bar.offset_bottom = 40.0
add_child(top_bar)
var hbox := HBoxContainer.new()
hbox.add_theme_constant_override("separation", 8)
top_bar.add_child(hbox)
_facing_button = MenuButton.new()
_facing_button.text = "Facing"
_facing_menu = _facing_button.get_popup()
_facing_menu.add_item(_facing_menu_label(FacingProfile.LEFT), FacingProfile.LEFT)
_facing_menu.add_item(_facing_menu_label(FacingProfile.RIGHT), FacingProfile.RIGHT)
_facing_menu.add_item(_facing_menu_label(FacingProfile.FORWARD), FacingProfile.FORWARD)
_facing_menu.id_pressed.connect(_on_facing_menu_id_pressed)
_facing_menu.about_to_popup.connect(_update_facing_menu_labels)
hbox.add_child(_facing_button)
var open_btn := Button.new()
open_btn.text = "Open .stk…"
open_btn.pressed.connect(_on_open_pressed)
hbox.add_child(open_btn)
for entry: Array in QUICK_LOADS:
var btn := Button.new()
btn.text = str(entry[0])
btn.pressed.connect(_on_quick_load.bind(str(entry[1])))
hbox.add_child(btn)
var bones_cb := CheckBox.new()
bones_cb.text = "Show Bones"
bones_cb.button_pressed = true
bones_cb.toggled.connect(_on_show_bones_toggled)
hbox.add_child(bones_cb)
var ik_cb := CheckBox.new()
ik_cb.text = "Show IK Handles"
ik_cb.button_pressed = true
ik_cb.toggled.connect(_on_show_ik_toggled)
hbox.add_child(ik_cb)
var coords_cb := CheckBox.new()
coords_cb.text = "Show Coords"
coords_cb.button_pressed = true
coords_cb.toggled.connect(_on_show_coords_toggled)
hbox.add_child(coords_cb)
_status_label = Label.new()
_status_label.text = "No file loaded"
_status_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_status_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
_status_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
_status_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
hbox.add_child(_status_label)
# Viewport area (fills everything below the top bar).
_viewport_container = SubViewportContainer.new()
_viewport_container.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_viewport_container.offset_top = 40.0
_viewport_container.mouse_filter = Control.MOUSE_FILTER_STOP
add_child(_viewport_container)
_viewport = SubViewport.new()
_viewport.transparent_bg = true
_viewport_container.add_child(_viewport)
_world = Node2D.new()
_world.name = "World"
_viewport.add_child(_world)
_camera = Camera2D.new()
_camera.enabled = true
_world.add_child(_camera)
_camera.make_current()
_debug_overlay = Node2D.new()
_debug_overlay.name = "DebugOverlay"
_debug_overlay.draw.connect(_on_debug_overlay_draw)
_world.add_child(_debug_overlay)
# Coordinates readout panel (added after the viewport container so it
# renders in front of it).
_build_coords_panel()
# File dialog for "Open .stk…".
_file_dialog = FileDialog.new()
_file_dialog.title = "Open .stk"
_file_dialog.access = FileDialog.ACCESS_FILESYSTEM
_file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_FILE
_file_dialog.filters = PackedStringArray(["*.stk ; Stickman Files"])
_file_dialog.file_selected.connect(_on_file_selected)
add_child(_file_dialog)
# Right-click context menu for toggling a bend joint's bend direction.
_context_menu = PopupMenu.new()
_context_menu.add_item("Invert Bend", 0)
_context_menu.id_pressed.connect(_on_context_menu_id_pressed)
add_child(_context_menu)
func _build_coords_panel() -> void:
_coords_panel = PanelContainer.new()
_coords_panel.set_anchors_and_offsets_preset(Control.PRESET_TOP_RIGHT)
_coords_panel.offset_top = 40.0
_coords_panel.offset_right = -8.0
_coords_panel.offset_left = -COORDS_PANEL_WIDTH
_coords_panel.grow_vertical = Control.GROW_DIRECTION_END
_coords_panel.grow_horizontal = Control.GROW_DIRECTION_BEGIN
_coords_panel.mouse_filter = Control.MOUSE_FILTER_IGNORE
var style := StyleBoxFlat.new()
style.bg_color = Color(0.0, 0.0, 0.0, 0.55)
style.border_color = Color(1.0, 1.0, 1.0, 0.12)
style.border_width_left = 1
style.border_width_top = 1
style.border_width_right = 1
style.border_width_bottom = 1
style.corner_radius_top_left = 4
style.corner_radius_top_right = 4
style.corner_radius_bottom_right = 4
style.corner_radius_bottom_left = 4
style.content_margin_left = 8.0
style.content_margin_top = 8.0
style.content_margin_right = 8.0
style.content_margin_bottom = 8.0
_coords_panel.add_theme_stylebox_override("panel", style)
_coords_label = RichTextLabel.new()
_coords_label.selection_enabled = true
_coords_label.context_menu_enabled = true
_coords_label.fit_content = true
_coords_label.autowrap_mode = TextServer.AUTOWRAP_OFF
_coords_label.scroll_active = false
_coords_label.focus_mode = Control.FOCUS_CLICK
var font := SystemFont.new()
font.font_names = PackedStringArray(["Consolas", "Menlo", "DejaVu Sans Mono", "Courier New"])
_coords_label.add_theme_font_override("normal_font", font)
_coords_label.add_theme_font_size_override("normal_font_size", 18)
_coords_panel.add_child(_coords_label)
add_child(_coords_panel)
func _on_viewport_container_resized() -> void:
var size := _viewport_container.size
if size.x < 1.0 or size.y < 1.0:
return
_viewport.size = Vector2i(size)
# ---------------------------------------------------------------------------
# Input handling
# ---------------------------------------------------------------------------
func _handle_mouse_button(mb: InputEventMouseButton) -> void:
if not _is_over_viewport(mb.position):
return
if mb.button_index == MOUSE_BUTTON_MIDDLE:
if mb.pressed:
_is_panning = true
_pan_last = mb.position
else:
_is_panning = false
return
if mb.button_index == MOUSE_BUTTON_WHEEL_UP and mb.pressed:
_apply_zoom(mb.position, ZOOM_STEP)
return
if mb.button_index == MOUSE_BUTTON_WHEEL_DOWN and mb.pressed:
_apply_zoom(mb.position, 1.0 / ZOOM_STEP)
return
if mb.button_index == MOUSE_BUTTON_RIGHT:
if mb.pressed:
_handle_right_click(mb.position)
return
if mb.button_index == MOUSE_BUTTON_LEFT:
if mb.pressed:
_dragging_handle = _hit_test_handle(_viewport_to_world(mb.position))
else:
_dragging_handle = null
func _handle_right_click(screen_pos: Vector2) -> void:
if _skeleton == null or not is_instance_valid(_skeleton):
return
var world_pos := _viewport_to_world(screen_pos)
var joint := _hit_test_bend_joint(world_pos)
if joint.is_empty():
return
_context_joint = joint
_context_menu.set_item_text(0, _context_menu_label(joint))
_context_menu.popup(Rect2i(Vector2i(screen_pos), Vector2i(1, 1)))
func _hit_test_bend_joint(world_pos: Vector2) -> String:
var hit_radius := JOINT_HIT_RADIUS_PX / _camera.zoom.x
var best_joint := ""
var best_dist := hit_radius
for joint: String in BEND_JOINTS:
var bone: Bone2D = _bend_joint_bones.get(joint) as Bone2D
if bone == null or not is_instance_valid(bone):
continue
var dist := world_pos.distance_to(bone.global_position)
if dist <= best_dist:
best_joint = joint
best_dist = dist
return best_joint
func _handle_mouse_motion(mm: InputEventMouseMotion) -> void:
if _is_panning:
var delta := mm.position - _pan_last
_pan_last = mm.position
_camera.position -= delta / _camera.zoom
_debug_overlay.queue_redraw()
return
if _dragging_handle != null and is_instance_valid(_dragging_handle):
_dragging_handle.global_position = _viewport_to_world(mm.position)
_debug_overlay.queue_redraw()
func _is_over_viewport(screen_pos: Vector2) -> bool:
return _viewport_container.get_global_rect().has_point(screen_pos)
func _viewport_to_world(screen_pos: Vector2) -> Vector2:
var local := screen_pos - _viewport_container.global_position
return (local - _viewport.size * 0.5) / _camera.zoom + _camera.position
func _apply_zoom(screen_pos: Vector2, factor: float) -> void:
var world_before := _viewport_to_world(screen_pos)
var new_zoom := clampf(_camera.zoom.x * factor, MIN_ZOOM, MAX_ZOOM)
_camera.zoom = Vector2(new_zoom, new_zoom)
var world_after := _viewport_to_world(screen_pos)
_camera.position += world_before - world_after
_debug_overlay.queue_redraw()
func _hit_test_handle(world_pos: Vector2) -> Marker2D:
var hit_radius := HANDLE_HIT_RADIUS_PX / _camera.zoom.x
var best: Marker2D = null
var best_dist := hit_radius
for handle_name: String in _ik_handles:
var handle := _ik_handles[handle_name] as Marker2D
if handle == null or not is_instance_valid(handle):
continue
var dist := world_pos.distance_to(handle.global_position)
if dist <= best_dist:
best = handle
best_dist = dist
return best
# ---------------------------------------------------------------------------
# Debug overlay drawing
# ---------------------------------------------------------------------------
func _on_debug_overlay_draw() -> void:
if _show_bones:
_draw_bones()
if _show_ik:
_draw_ik_handles()
func _draw_bones() -> void:
if _skeleton == null or not is_instance_valid(_skeleton):
return
var zoom := _camera.zoom.x
var line_w := BONE_LINE_WIDTH_PX / zoom
var dot_r := JOINT_DOT_RADIUS_PX / zoom
for i: int in _skeleton.get_bone_count():
var bone := _skeleton.get_bone(i)
if bone == null:
continue
var color := _bone_color(bone.name)
var origin := bone.global_position
_debug_overlay.draw_circle(origin, dot_r, color)
# Collect the bone's Bone2D children (true bone segments, including the
# nested lower bones).
var bone_children: Array[Bone2D] = []
for child in bone.get_children():
if child is Bone2D:
bone_children.append(child as Bone2D)
if bone_children.is_empty():
# Leaf bone — draw out to its IK target (forearm/shin), falling
# back to the bone's own direction (bone_angle + rotation) when
# there is no target (e.g. the Head, whose target is a LookAt aim
# point, not a joint).
var target := _leaf_bone_target(bone)
if target != null:
_debug_overlay.draw_line(origin, target.global_position, color, line_w)
else:
var fallback := origin + Vector2(bone.get_length(), 0.0).rotated(deg_to_rad(bone.bone_angle)).rotated(bone.global_rotation)
_debug_overlay.draw_line(origin, fallback, color, line_w)
else:
for child_bone in bone_children:
_debug_overlay.draw_line(origin, child_bone.global_position, color, line_w)
func _leaf_bone_target(bone: Bone2D) -> Node2D:
if not LEAF_BONE_IK_PATHS.has(bone.name):
return null
if _rig == null or not is_instance_valid(_rig):
return null
return _rig.get_node_or_null(NodePath(str(LEAF_BONE_IK_PATHS[bone.name]))) as Node2D
func _draw_ik_handles() -> void:
var zoom := _camera.zoom.x
var r := HANDLE_DRAW_RADIUS_PX / zoom
var outline_w := 1.5 / zoom
for handle_name: String in _ik_handles:
var handle := _ik_handles[handle_name] as Marker2D
if handle == null or not is_instance_valid(handle):
continue
var color := _handle_color(handle_name)
var pos := handle.global_position
_debug_overlay.draw_circle(pos, r, color)
_debug_overlay.draw_arc(pos, r, 0.0, TAU, 24, Color(1.0, 1.0, 1.0, 0.8), outline_w)
# Head aim line — visual aid showing what the head bone is aiming at.
var head_handle: Marker2D = _ik_handles.get("Head", null) as Marker2D
if head_handle != null and is_instance_valid(head_handle) \
and _skeleton != null and is_instance_valid(_skeleton):
var head_bone := _skeleton.get_node_or_null(NodePath("Torso/Head")) as Bone2D
if head_bone != null and is_instance_valid(head_bone):
_debug_overlay.draw_line(
head_bone.global_position,
head_handle.global_position,
Color(1.0, 1.0, 0.0, 0.5),
1.5 / zoom
)
func _handle_color(handle_name: String) -> Color:
match handle_name:
"Head":
return HANDLE_COLOR_HEAD
"Torso":
return HANDLE_COLOR_TORSO
_:
return HANDLE_COLOR_HAND if handle_name.ends_with("Hand") else HANDLE_COLOR_FOOT
func _bone_color(bone_name: String) -> Color:
if bone_name.begins_with("Left"):
return BONE_COLOR_LEFT
if bone_name.begins_with("Right"):
return BONE_COLOR_RIGHT
return BONE_COLOR_CENTRAL
# ---------------------------------------------------------------------------
# Facing profile & bend-direction toggles
# ---------------------------------------------------------------------------
func _facing_menu_label(profile: int) -> String:
return ("[√] " if profile == _facing_profile else "") + FacingProfile.keys()[profile].capitalize()
func _update_facing_menu_labels() -> void:
if _facing_menu == null:
return
for profile: int in [FacingProfile.LEFT, FacingProfile.RIGHT, FacingProfile.FORWARD]:
var idx := _facing_menu.get_item_index(profile)
if idx >= 0:
_facing_menu.set_item_text(idx, _facing_menu_label(profile))
func _on_facing_menu_id_pressed(id: int) -> void:
_apply_facing_profile(id)
_update_facing_menu_labels()
func _apply_facing_profile(profile: int) -> void:
_facing_profile = profile
for joint: String in BEND_JOINTS:
var mod: SkeletonModification2DTwoBoneIK = _bend_modifications.get(joint) as SkeletonModification2DTwoBoneIK
if mod == null:
continue
mod.flip_bend_direction = bool(PROFILE_FLAGS[profile][joint])
_apply_body_z_order()
_debug_overlay.queue_redraw()
func _apply_body_z_order() -> void:
if _body_container == null or not is_instance_valid(_body_container):
return
var order: Array = Z_ORDER_BY_PROFILE.get(_facing_profile, Z_ORDER_BY_PROFILE[FacingProfile.FORWARD])
for part_name: String in order:
var part := _body_container.get_node_or_null(NodePath(part_name))
if part != null:
_body_container.move_child(part, _body_container.get_child_count() - 1)
func _context_menu_label(joint: String) -> String:
var mod: SkeletonModification2DTwoBoneIK = _bend_modifications.get(joint) as SkeletonModification2DTwoBoneIK
if mod == null:
return "Invert Bend"
return "Normal Bend" if mod.flip_bend_direction else "Invert Bend"
func _on_context_menu_id_pressed(_id: int) -> void:
if _context_joint.is_empty():
return
var mod: SkeletonModification2DTwoBoneIK = _bend_modifications.get(_context_joint) as SkeletonModification2DTwoBoneIK
if mod == null:
return
mod.flip_bend_direction = not mod.flip_bend_direction
_debug_overlay.queue_redraw()
# ---------------------------------------------------------------------------
# Spawn / swap
# ---------------------------------------------------------------------------
func _on_open_pressed() -> void:
_file_dialog.popup_centered_ratio(0.6)
func _on_quick_load(path: String) -> void:
_load_and_spawn(path)
func _on_file_selected(path: String) -> void:
_load_and_spawn(path)
func _load_and_spawn(path: String) -> void:
_free_current_rig()
var rig := StickmanFactory.spawn(path)
if rig == null:
_status_label.text = "Error: failed to load " + path.get_file()
return
_rig = rig
_world.add_child(_rig)
_world.move_child(_rig, 0) # keep the rig behind the debug overlay
_resolve_rig_nodes()
_ensure_modification_stack_enabled()
_apply_facing_profile(_facing_profile)
_status_label.text = path.get_file()
_recenter_camera()
_debug_overlay.queue_redraw()
func _free_current_rig() -> void:
if _rig != null and is_instance_valid(_rig):
_rig.queue_free()
_rig = null
_skeleton = null
_body_container = null
_ik_handles.clear()
_bend_joint_bones.clear()
_bend_modifications.clear()
_coord_bones.clear()
_context_joint = ""
_dragging_handle = null
func _resolve_rig_nodes() -> void:
_skeleton = _rig.get_node_or_null(NodePath(SKELETON_PATH)) as Skeleton2D
if _skeleton == null:
push_warning("TestHarness: missing '%s' node in rig." % SKELETON_PATH)
_body_container = _rig.get_node_or_null(NodePath(BODY_CONTAINER_PATH)) as Node2D
if _body_container == null:
push_warning("TestHarness: missing '%s' node in rig." % BODY_CONTAINER_PATH)
_ik_handles.clear()
for handle_name: String in IK_HANDLE_PATHS:
var handle := _rig.get_node_or_null(NodePath(IK_HANDLE_PATHS[handle_name])) as Marker2D
if handle != null:
_ik_handles[handle_name] = handle
else:
push_warning("TestHarness: missing IK handle '%s'." % IK_HANDLE_PATHS[handle_name])
_resolve_bend_joints()
_resolve_coord_bones()
func _resolve_bend_joints() -> void:
_bend_joint_bones.clear()
_bend_modifications.clear()
if _skeleton == null:
return
for joint: String in BEND_JOINTS:
var path: String = BEND_JOINT_BONE_PATHS[joint]
var bone := _skeleton.get_node_or_null(NodePath(path)) as Bone2D
if bone != null and is_instance_valid(bone):
_bend_joint_bones[joint] = bone
else:
push_warning("TestHarness: missing bend-joint bone '%s'." % path)
var stack: SkeletonModificationStack2D = _skeleton.modification_stack
if stack == null:
return
for i: int in stack.modification_count:
var mod := stack.get_modification(i)
if not (mod is SkeletonModification2DTwoBoneIK):
continue
var ik := mod as SkeletonModification2DTwoBoneIK
for joint: String in BEND_JOINTS:
if ik.joint_two_bone2d_node == NodePath(BEND_JOINT_BONE_PATHS[joint]):
_bend_modifications[joint] = ik
break
for joint: String in BEND_JOINTS:
if not _bend_modifications.has(joint):
push_warning("TestHarness: missing TwoBoneIK modification for bend joint '%s'." % joint)
func _resolve_coord_bones() -> void:
_coord_bones.clear()
if _skeleton == null:
return
for path: String in COORD_BONE_PATHS:
var bone := _skeleton.get_node_or_null(NodePath(path)) as Bone2D
if bone != null and is_instance_valid(bone):
_coord_bones[path.get_file()] = bone
else:
push_warning("TestHarness: missing coordinate bone '%s'." % path)
func _ensure_modification_stack_enabled() -> void:
if _skeleton == null:
return
var stack: SkeletonModificationStack2D = _skeleton.modification_stack
if stack != null:
stack.enabled = true
else:
push_warning("TestHarness: Skeleton2D has no modification_stack assigned.")
func _recenter_camera() -> void:
if _rig == null:
return
# The rig root sits at the hips; nudge the camera up so the whole figure is
# roughly centered in the viewport.
_camera.position = _rig.global_position + Vector2(0.0, -50.0)
_camera.zoom = Vector2.ONE
_debug_overlay.queue_redraw()
# ---------------------------------------------------------------------------
# Checkbox handlers
# ---------------------------------------------------------------------------
func _on_show_bones_toggled(pressed: bool) -> void:
_show_bones = pressed
_debug_overlay.queue_redraw()
func _on_show_ik_toggled(pressed: bool) -> void:
_show_ik = pressed
_debug_overlay.queue_redraw()
func _on_show_coords_toggled(pressed: bool) -> void:
_show_coords = pressed
if _coords_panel != null:
_coords_panel.visible = pressed
# ---------------------------------------------------------------------------
# Coordinates display
# ---------------------------------------------------------------------------
func _process(_delta: float) -> void:
if not _show_coords or _coords_label == null:
return
_update_coords_display()
func _update_coords_display() -> void:
if _rig == null or not is_instance_valid(_rig):
if _coords_label.text != "No rig loaded":
_coords_label.text = "No rig loaded"
return
var lines := PackedStringArray()
# Skeleton2D root.
if _skeleton != null and is_instance_valid(_skeleton):
lines.append("%s pos %s rot %s" % ["Skeleton2D".lpad(14), _fmt_vec2(_skeleton.global_position), _fmt_deg(_skeleton.global_rotation)])
# Bones.
lines.append("Bones")
for path: String in COORD_BONE_PATHS:
var bone := _coord_bones.get(path.get_file(), null) as Bone2D
if bone == null or not is_instance_valid(bone):
continue
lines.append("%s pos %s rot %s" % [path.get_file().lpad(14), _fmt_vec2(bone.global_position), _fmt_deg(bone.global_rotation)])
# IK targets.
lines.append("IK Targets")
for handle_name: String in IK_HANDLE_PATHS:
var handle := _ik_handles.get(handle_name, null) as Marker2D
if handle == null or not is_instance_valid(handle):
continue
lines.append("%s pos %s" % [handle_name.lpad(14), _fmt_vec2(handle.global_position)])
var text := "\n".join(lines)
if _coords_label.text != text:
_coords_label.text = text
func _fmt_vec2(v: Vector2) -> String:
return "(%8.1f, %8.1f)" % [v.x, v.y]
func _fmt_deg(rad: float) -> String:
return "%7.1f°" % rad_to_deg(rad)