container_log.gd 767 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. extends Control
  2. const MAX_ENTRIES = 100
  3. var _entry_template
  4. func _enter_tree():
  5. Log.entry_logged.connect(self._on_log_entry)
  6. _entry_template = get_child(0) as Label
  7. remove_child(_entry_template)
  8. func _exit_tree():
  9. _entry_template.free()
  10. func clear():
  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, type):
  16. var new_entry = _entry_template.duplicate() as Label
  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 = get_child(0) as Label
  24. remove_child(first_entry)
  25. first_entry.queue_free()
  26. add_child(new_entry)