OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / gate_collection_focus.gd

138 lines · gd ref:
extends SceneTree
## GATE — the collection keys reach the map whatever holds the keyboard (user 2026-10-18:
## "when i press m to create a new group from selected it doesnt do anything"). Keys are
## sent the way the game receives them — Viewport.push_input, with the unicode a real
## keypress carries — not into the map directly (gate_collection_keys' path).
##
##   C0  nothing focused: M opens the Move-to-collection picker
##   C1  the Outliner's Tree focused (you clicked rows to select): M opens the picker
##   C2  nothing focused: Ctrl+G asks for the name; Enter makes a collection
##   C3  the Tree focused: the same
##
## RUN: CI=true HYPERTPS_DAEMON_TCP=127.0.0.1:1 "$GODOT" --headless --path . --script res://scenes/test/gate_collection_focus.gd
var _frame := 0
var _map: Node = null
var _persist: Node = null
var fails := 0
var checks := 0
var _step := 0
var _brush := -1
var _ncols := 0


func _p(c: bool, m: String, got: Variant = "") -> void:
	checks += 1
	if not c:
		fails += 1
	print("  %-66s %s  %s" % [m, "OK" if c else "**FAIL**", str(got)])


func _key(code: int, ch: String, ctrl := false, pressed := true) -> InputEventKey:
	var e := InputEventKey.new()
	e.physical_keycode = code
	e.keycode = code
	e.ctrl_pressed = ctrl
	e.unicode = 0 if ctrl else ch.unicode_at(0)
	e.pressed = pressed
	return e


func _box(lo: Vector3, hi: Vector3) -> Array:
	return [1, 0, 0, hi.x, -1, 0, 0, -lo.x, 0, 1, 0, hi.y, 0, -1, 0, -lo.y, 0, 0, 1, hi.z, 0, 0, -1, -lo.z]


func _setup() -> void:
	_persist = _map.get("_persist")
	var dot: String = _persist.record("brush.create", {"planes": _box(Vector3(0, 0, 0), Vector3(2, 1, 1)),
		"proj": [], "material_id": 0, "level_id": 0, "sky": false})
	_persist.refold()
	_brush = _map.get("level").brush_by_key(dot)


func _focus(tree: bool) -> void:
	var f := root.gui_get_focus_owner()
	if f != null:
		f.release_focus()
	_map.select_brushes(PackedInt32Array([_brush]))
	if tree:
		var panel: Node = _map.get("_outliner")
		var t: Tree = panel.get("_tree") if panel != null else null
		if t != null:
			t.grab_focus()
	print("  focus owner: %s" % str(root.gui_get_focus_owner()))


func _send(code: int, ch: String, ctrl := false) -> void:
	_ncols = (_persist.collection_tree()["cols"] as Dictionary).size()
	root.push_input(_key(code, ch, ctrl))
	root.push_input(_key(code, ch, ctrl, false))


func _picker_open() -> bool:
	var q: Node = _map.get("_q_menu")
	var open: bool = q != null and q.is_open()
	if open:
		q.get("_menu").hide()
		root.get_node("/root/Keymap").pop_context("menu")
	return open


## The name prompt is up: say so, and confirm it (Enter).
func _submit(label: String) -> void:
	var q: Node = _map.get("_q_menu")
	var open: bool = q != null and q.name_open()
	_p(open, label)
	if open:
		q.submit_name("Group")


func _made() -> bool:
	_persist.refold()
	return (_persist.collection_tree()["cols"] as Dictionary).size() == _ncols + 1


func _process(_d: float) -> bool:
	_frame += 1
	if _frame == 1:
		root.get_node("/root/GameMode").set_mode("build")
		var keet: Node = root.get_node_or_null("/root/KeetService")
		if keet != null:
			keet.current_member_id = "gate-author"
		var gs: Node = root.get_node_or_null("/root/GameState")
		if gs != null:
			gs.route("world:level:*", 1, "peer")
		_map = (load("res://scenes/maps/map_flat.tscn") as PackedScene).instantiate()
		root.add_child(_map)
		return false
	if _frame < 30 or _frame % 4 != 0:
		return false
	match _step:
		0: _setup()
		1:
			_focus(false)
			_send(KEY_M, "m")
		2: _p(_picker_open(), "C0 nothing focused: M opens the picker")
		3:
			_focus(true)
			_send(KEY_M, "m")
		4: _p(_picker_open(), "C1 the Tree focused: M opens the picker", str(root.gui_get_focus_owner()))
		5:
			_focus(false)
			_send(KEY_G, "g", true)
		6: _submit("C2 nothing focused: Ctrl+G asks for the name")
		7: _p(_made(), "C2 ... Enter makes the collection")
		8:
			_focus(true)
			_send(KEY_G, "g", true)
		9: _submit("C3 the Tree focused: Ctrl+G asks for the name")
		10: _p(_made(), "C3 ... Enter makes the collection")
		_:
			if checks < 6:
				print("  **FAIL** only %d checks ran -- setup died before the assertions" % checks)
				fails += 1
			print("\nGATE collection_focus: %s (%d checks, %d failed)" % ["PASS" if fails == 0 else "FAIL", checks, fails])
			quit(0 if fails == 0 else 1)
			return true
	_step += 1
	return false