|
| 1 | +@tool |
| 2 | +class_name UiHandler |
| 3 | +extends RefCounted |
| 4 | + |
| 5 | +## Handles UI-specific (Control) layout helpers: anchor presets, etc. |
| 6 | +## |
| 7 | +## Anchors/offsets are the worst part of Control layout to set one-property-at-a-time. |
| 8 | +## This handler wraps Godot's built-in presets (FULL_RECT, CENTER, TOP_LEFT, ...) so |
| 9 | +## callers can set a whole layout with one command, with proper undo. |
| 10 | + |
| 11 | +var _undo_redo: EditorUndoRedoManager |
| 12 | + |
| 13 | + |
| 14 | +const _PRESETS := { |
| 15 | + "top_left": Control.PRESET_TOP_LEFT, |
| 16 | + "top_right": Control.PRESET_TOP_RIGHT, |
| 17 | + "bottom_left": Control.PRESET_BOTTOM_LEFT, |
| 18 | + "bottom_right": Control.PRESET_BOTTOM_RIGHT, |
| 19 | + "center_left": Control.PRESET_CENTER_LEFT, |
| 20 | + "center_top": Control.PRESET_CENTER_TOP, |
| 21 | + "center_right": Control.PRESET_CENTER_RIGHT, |
| 22 | + "center_bottom": Control.PRESET_CENTER_BOTTOM, |
| 23 | + "center": Control.PRESET_CENTER, |
| 24 | + "left_wide": Control.PRESET_LEFT_WIDE, |
| 25 | + "top_wide": Control.PRESET_TOP_WIDE, |
| 26 | + "right_wide": Control.PRESET_RIGHT_WIDE, |
| 27 | + "bottom_wide": Control.PRESET_BOTTOM_WIDE, |
| 28 | + "vcenter_wide": Control.PRESET_VCENTER_WIDE, |
| 29 | + "hcenter_wide": Control.PRESET_HCENTER_WIDE, |
| 30 | + "full_rect": Control.PRESET_FULL_RECT, |
| 31 | +} |
| 32 | + |
| 33 | +const _RESIZE_MODES := { |
| 34 | + "minsize": Control.PRESET_MODE_MINSIZE, |
| 35 | + "keep_width": Control.PRESET_MODE_KEEP_WIDTH, |
| 36 | + "keep_height": Control.PRESET_MODE_KEEP_HEIGHT, |
| 37 | + "keep_size": Control.PRESET_MODE_KEEP_SIZE, |
| 38 | +} |
| 39 | + |
| 40 | +const _ANCHOR_OFFSET_PROPS := [ |
| 41 | + "anchor_left", "anchor_top", "anchor_right", "anchor_bottom", |
| 42 | + "offset_left", "offset_top", "offset_right", "offset_bottom", |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +func _init(undo_redo: EditorUndoRedoManager) -> void: |
| 47 | + _undo_redo = undo_redo |
| 48 | + |
| 49 | + |
| 50 | +## Apply a Control layout preset (anchors + offsets) to a UI node. |
| 51 | +## |
| 52 | +## Params: |
| 53 | +## path - scene path to a Control node (required) |
| 54 | +## preset - preset name: full_rect, center, top_left, ... (required) |
| 55 | +## resize_mode - minsize | keep_width | keep_height | keep_size (default: minsize) |
| 56 | +## margin - integer margin in pixels from the anchor edges (default: 0) |
| 57 | +func set_anchor_preset(params: Dictionary) -> Dictionary: |
| 58 | + var node_path: String = params.get("path", "") |
| 59 | + if node_path.is_empty(): |
| 60 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Missing required param: path") |
| 61 | + |
| 62 | + var preset_name: String = str(params.get("preset", "")).to_lower() |
| 63 | + if preset_name.is_empty(): |
| 64 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Missing required param: preset") |
| 65 | + if not _PRESETS.has(preset_name): |
| 66 | + var names := _PRESETS.keys() |
| 67 | + names.sort() |
| 68 | + return McpErrorCodes.make( |
| 69 | + McpErrorCodes.INVALID_PARAMS, |
| 70 | + "Unknown preset '%s'. Valid: %s" % [preset_name, ", ".join(names)] |
| 71 | + ) |
| 72 | + |
| 73 | + var resize_mode_name: String = str(params.get("resize_mode", "minsize")).to_lower() |
| 74 | + if not _RESIZE_MODES.has(resize_mode_name): |
| 75 | + var names := _RESIZE_MODES.keys() |
| 76 | + names.sort() |
| 77 | + return McpErrorCodes.make( |
| 78 | + McpErrorCodes.INVALID_PARAMS, |
| 79 | + "Unknown resize_mode '%s'. Valid: %s" % [resize_mode_name, ", ".join(names)] |
| 80 | + ) |
| 81 | + |
| 82 | + var margin: int = int(params.get("margin", 0)) |
| 83 | + |
| 84 | + var scene_root := EditorInterface.get_edited_scene_root() |
| 85 | + if scene_root == null: |
| 86 | + return McpErrorCodes.make(McpErrorCodes.EDITOR_NOT_READY, "No scene open") |
| 87 | + |
| 88 | + var node := ScenePath.resolve(node_path, scene_root) |
| 89 | + if node == null: |
| 90 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Node not found: %s" % node_path) |
| 91 | + if not node is Control: |
| 92 | + return McpErrorCodes.make( |
| 93 | + McpErrorCodes.INVALID_PARAMS, |
| 94 | + "Node %s is not a Control (got %s)" % [node_path, node.get_class()] |
| 95 | + ) |
| 96 | + |
| 97 | + var control := node as Control |
| 98 | + var preset_value: int = _PRESETS[preset_name] |
| 99 | + var resize_mode_value: int = _RESIZE_MODES[resize_mode_name] |
| 100 | + |
| 101 | + # Snapshot before so we can undo every property the preset may have touched. |
| 102 | + var before: Dictionary = {} |
| 103 | + for prop in _ANCHOR_OFFSET_PROPS: |
| 104 | + before[prop] = control.get(prop) |
| 105 | + |
| 106 | + _undo_redo.create_action("MCP: Set %s anchor preset %s" % [control.name, preset_name]) |
| 107 | + _undo_redo.add_do_method( |
| 108 | + control, "set_anchors_and_offsets_preset", preset_value, resize_mode_value, margin |
| 109 | + ) |
| 110 | + for prop in _ANCHOR_OFFSET_PROPS: |
| 111 | + _undo_redo.add_undo_property(control, prop, before[prop]) |
| 112 | + _undo_redo.commit_action() |
| 113 | + |
| 114 | + var after: Dictionary = {} |
| 115 | + for prop in _ANCHOR_OFFSET_PROPS: |
| 116 | + after[prop] = control.get(prop) |
| 117 | + |
| 118 | + return { |
| 119 | + "data": { |
| 120 | + "path": node_path, |
| 121 | + "preset": preset_name, |
| 122 | + "resize_mode": resize_mode_name, |
| 123 | + "margin": margin, |
| 124 | + "anchors": { |
| 125 | + "left": after.anchor_left, |
| 126 | + "top": after.anchor_top, |
| 127 | + "right": after.anchor_right, |
| 128 | + "bottom": after.anchor_bottom, |
| 129 | + }, |
| 130 | + "offsets": { |
| 131 | + "left": after.offset_left, |
| 132 | + "top": after.offset_top, |
| 133 | + "right": after.offset_right, |
| 134 | + "bottom": after.offset_bottom, |
| 135 | + }, |
| 136 | + "undoable": true, |
| 137 | + } |
| 138 | + } |
0 commit comments