container_log.gd 808 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. extends Control
  2. const MAX_ENTRIES = 100
  3. var _entry_template: Label
  4. func _enter_tree() -> void:
  5. Log.entry_logged.connect(_on_log_entry)
  6. _entry_template = get_child(0)
  7. remove_child(_entry_template)
  8. func _exit_tree() -> void:
  9. _entry_template.free()
  10. func clear() -> void:
  11. while get_child_count():
  12. var entry := get_child(get_child_count() - 1)
  13. remove_child(entry)
  14. entry.queue_free()
  15. func _on_log_entry(message: String, type: Log.LogType) -> void:
  16. var new_entry: Label = _entry_template.duplicate()
  17. new_entry.set_text(message)
  18. if type == Log.LogType.ERROR:
  19. new_entry.modulate = Color.RED
  20. else:
  21. new_entry.modulate = Color.WHITE
  22. if get_child_count() >= MAX_ENTRIES:
  23. var first_entry: Label = get_child(0)
  24. remove_child(first_entry)
  25. first_entry.queue_free()
  26. add_child(new_entry)