From 6c1c8c877585c3da2f479537d11faca750487bef Mon Sep 17 00:00:00 2001 From: Feo Wu Date: Sun, 26 Nov 2023 12:19:36 +0800 Subject: [PATCH] feat: buffered rich text area --- .../common/buffered_rich_text.gd | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 addons/panku_console/common/buffered_rich_text.gd diff --git a/addons/panku_console/common/buffered_rich_text.gd b/addons/panku_console/common/buffered_rich_text.gd new file mode 100644 index 0000000..db67648 --- /dev/null +++ b/addons/panku_console/common/buffered_rich_text.gd @@ -0,0 +1,35 @@ +extends VBoxContainer + +# it is like, an infinite scroll game. + +# specifically, the first buffer will be cleared and sent to the last +# when the last buffer is full. + +# with buffers, we can constanly output lots of fancy stuff while keeping a smooth experience. + +const BUFFER_MAX_PARAGRAPHS = 64 +const BUFFERS = 4 + +var cur_label_idx:int = 0 + +func add_text(text:String): + var cur_label:RichTextLabel = get_child(cur_label_idx) + cur_label.text += text + if cur_label.get_paragraph_count() > BUFFER_MAX_PARAGRAPHS: + cur_label_idx += 1 + if cur_label_idx == BUFFERS: + cur_label_idx = BUFFERS - 1 + var first_label:RichTextLabel = get_child(0) + first_label.text = "" + move_child(first_label, BUFFERS - 1) + +func _ready(): + set("theme_override_constants/separation", 0) + for child in get_children(): + child.queue_free() + for i in range(BUFFERS): + var new_buffer:RichTextLabel = RichTextLabel.new() + new_buffer.fit_content = true + new_buffer.bbcode_enabled = true + new_buffer.selection_enabled = true + add_child(new_buffer)