Files
RoboMovel/simulacao/robo-movel-godot/addons/claude_godot/ui/tool_card.gd
T
mguinhos d17575771b Projeto: app demo, servidor/firmware em software/, simulacao Godot, planta do bloco D, chapas MDF da base, renders e slides
- app/: app demo (Kivy/buildozer) da API do robo
- software/: servidor RoboMovel (SLAM, profundidade, navegacao) e firmware da base movel (movido de firmware/)
- simulacao/: projeto Godot do robo e script de lancamento servidor + simulacao
- modelos/: planta do 5o andar do bloco D, cena Blender do corredor e modelo real do robo
- partes/mdf/: chapas identificadas e nesting da base do robo (DXF/SVG/FCStd)
- projeto/render, relatorio/imagens, slides: renders finais e apresentacoes de progresso
- ferramentas/scripts: encaixe de chapas, planta do bloco D e render do corredor no Blender
- .gitignore: ignora backups do Blender/FreeCAD e .godot/
2026-10-25 23:38:16 -03:00

250 lines
8.1 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## One collapsible card per tool call, holding both the invocation and its
## result — the shape kimi-code's TUI uses: a one-line header carrying a
## status glyph and a short numeric "chip", with the full command and output
## behind an expand toggle.
@tool
class_name ClaudeGodotToolCard
extends VBoxContainer
## Bodies are capped even when expanded: a write_file of a few thousand lines
## would otherwise make the dock unscrollable. The footer says what was cut.
const MAX_BODY_LINES := 200
## Previews are scaled to this width; the dock's minimum is 340.
const PREVIEW_WIDTH := 300
const COLOR_RUNNING := "#e8a33d"
const COLOR_OK := "#4caf50"
const COLOR_FAIL := "#f44336"
const COLOR_DIM := "#8a8a8a"
var tool_name := ""
var _input: Dictionary = {}
var _expanded := false
var _has_result := false
var _is_error := false
var _chip := ""
var _diff_ops: Array = []
var _output_text := ""
var _image: Dictionary = {}
## False when the model can't accept images, so the card can say the user is
## seeing something the model wasn't given.
var _model_sees_image := true
var _header: Button
## Drawn over the header button (which can't do BBCode) with mouse input
## ignored, so the status glyph and stat chip can carry color while clicks
## still reach the button underneath.
var _header_label: RichTextLabel
var _body: VBoxContainer
func setup(name: String, input: Dictionary) -> void:
tool_name = name
_input = input
_header = Button.new()
_header.flat = true
_header.custom_minimum_size = Vector2(0, 22)
_header.pressed.connect(_toggle)
add_child(_header)
_header_label = RichTextLabel.new()
_header_label.bbcode_enabled = true
_header_label.scroll_active = false
_header_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
_header_label.set_anchors_preset(Control.PRESET_FULL_RECT)
_header.add_child(_header_label)
_body = VBoxContainer.new()
_body.visible = false
add_child(_body)
_refresh_header()
func set_result(result: Dictionary, model_sees_image := true) -> void:
_has_result = true
_is_error = bool(result.get("is_error", false))
_output_text = str(result.get("content", ""))
_diff_ops = result.get("diff_ops", [])
_image = result.get("image", {})
_model_sees_image = model_sees_image
_chip = _compute_chip()
_refresh_header()
if _expanded:
_rebuild_body()
## Everything this card shows, for the clipboard.
func to_plain_text() -> String:
var parts := ["%s %s" % [tool_name, JSON.stringify(_input)]]
if _has_result:
parts.append(_output_text)
return "\n".join(parts)
func _toggle() -> void:
_expanded = not _expanded
_body.visible = _expanded
if _expanded:
_rebuild_body()
_refresh_header()
func _refresh_header() -> void:
var status := "[color=%s]●[/color]" % COLOR_RUNNING
if _has_result:
status = "[color=%s]✗[/color]" % COLOR_FAIL if _is_error else "[color=%s]✓[/color]" % COLOR_OK
var line := "[color=%s]%s[/color] %s [b]%s[/b]" % [
COLOR_DIM, "▾" if _expanded else "▸", status, tool_name,
]
var subject := _subject()
if subject != "":
line += " [color=%s]%s[/color]" % [COLOR_DIM, subject.replace("[", "[lb]")]
if _chip != "":
line += " " + _chip
_header_label.clear()
_header_label.append_text(line)
## The argument worth showing next to the name — the path for file tools,
## the setting for settings tools, the filter for the list tools.
func _subject() -> String:
for key in ["path", "addon", "filter"]:
if _input.has(key) and str(_input[key]) != "":
return str(_input[key])
return ""
func _compute_chip() -> String:
if _is_error:
return "[color=%s]failed[/color]" % COLOR_FAIL
if not _diff_ops.is_empty():
var added := 0
var removed := 0
for op in _diff_ops:
match op.get("op", ""):
"add":
added += 1
"del":
removed += 1
if added == 0 and removed == 0:
return "[color=%s]no change[/color]" % COLOR_DIM
var parts: Array = []
if added > 0:
parts.append("[color=%s]+%d[/color]" % [COLOR_OK, added])
if removed > 0:
parts.append("[color=%s]−%d[/color]" % [COLOR_FAIL, removed])
return " ".join(parts)
if not _image.is_empty():
var kb: int = (_image["bytes"] as PackedByteArray).size() / 1024
return "[color=%s]%d KB image[/color]" % [COLOR_DIM, kb]
var lines := _non_empty_line_count(_output_text)
if tool_name == "list_files":
return "[color=%s]%d %s[/color]" % [COLOR_DIM, lines, "entry" if lines == 1 else "entries"]
if _output_text == "":
return ""
return "[color=%s]%d %s[/color]" % [COLOR_DIM, lines, "line" if lines == 1 else "lines"]
static func _non_empty_line_count(text: String) -> int:
var n := 0
for line in text.split("\n"):
if line.strip_edges() != "":
n += 1
return n
func _rebuild_body() -> void:
for child in _body.get_children():
child.queue_free()
_add_section("command", _command_bbcode())
if not _has_result:
_add_section("", "[color=gray]running…[/color]")
return
if not _image.is_empty():
_add_image_section()
elif not _diff_ops.is_empty():
_add_section("output", ClaudeGodotDiff.to_bbcode(_diff_ops))
elif _output_text.strip_edges() == "":
_add_section("output", "[color=gray](no output)[/color]")
else:
var color := "#f44336" if _is_error else "#d4d4d4"
_add_section("output", "[bgcolor=#1e1e1e][color=%s]%s[/color][/bgcolor]" % [color, _capped(_output_text)])
## Decodes the bytes the tool already read rather than loading the file
## again, so what is shown is exactly what was handed to the model.
func _add_image_section() -> void:
_add_section("output", "[color=%s]%s[/color]" % [COLOR_DIM, _output_text.replace("[", "[lb]")])
var bytes: PackedByteArray = _image.get("bytes", PackedByteArray())
var mime: String = str(_image.get("mime", ""))
var image := Image.new()
var err := ERR_UNAVAILABLE
match mime:
"image/png":
err = image.load_png_from_buffer(bytes)
"image/jpeg":
err = image.load_jpg_from_buffer(bytes)
"image/webp":
err = image.load_webp_from_buffer(bytes)
_:
pass
if err != OK or image.is_empty():
_body.add_child(_small_label("(could not decode this %s for preview)" % mime))
return
# Sized here rather than left to the container: EXPAND_FIT_WIDTH_PROPORTIONAL
# derives height from a width the ScrollContainer hasn't resolved yet at
# layout time, so the preview came out zero pixels tall. Downscaling also
# keeps a full-resolution screenshot from becoming a huge texture.
var width := mini(image.get_width(), PREVIEW_WIDTH)
var height := int(round(width * float(image.get_height()) / float(image.get_width())))
if width < image.get_width():
image.resize(width, height, Image.INTERPOLATE_BILINEAR)
var preview := TextureRect.new()
preview.texture = ImageTexture.create_from_image(image)
preview.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
preview.custom_minimum_size = Vector2(width, height)
_body.add_child(preview)
if not _model_sees_image:
_body.add_child(_small_label("Shown here only — the current model isn't marked as accepting images, so it did not receive this."))
func _small_label(text: String) -> Label:
var label := Label.new()
label.text = text
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
label.add_theme_font_size_override("font_size", 9)
return label
func _add_section(label: String, bbcode: String) -> void:
if label != "":
var title := Label.new()
title.text = label
title.add_theme_font_size_override("font_size", 9)
_body.add_child(title)
var rtl := RichTextLabel.new()
rtl.bbcode_enabled = true
rtl.fit_content = true
rtl.scroll_active = false
rtl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
rtl.append_text(bbcode)
_body.add_child(rtl)
## Arguments as pretty JSON, so a write_file shows the contents it wrote
## rather than a single unreadable line.
func _command_bbcode() -> String:
var pretty := JSON.stringify(_input, " ")
return "[bgcolor=#1e1e1e][color=#d4d4d4]%s[/color][/bgcolor]" % _capped(pretty)
func _capped(text: String) -> String:
var escaped := text.replace("[", "[lb]")
var lines := escaped.split("\n")
if lines.size() <= MAX_BODY_LINES:
return escaped
var shown := lines.slice(0, MAX_BODY_LINES)
return "\n".join(shown) + "\n[color=gray]… %d more lines (%d total)[/color]" % [lines.size() - MAX_BODY_LINES, lines.size()]