forked from GDQuest/learn-gdscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIContentBlock.gd
74 lines (57 loc) · 2.25 KB
/
UIContentBlock.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Displays text and scenes or other visuals (optional) side-by-side.
#
# The block has a transparent background by default, except when inside a Revealer.
class_name UIContentBlock
extends Control
# Margin to apply to the panel in pixels when the block is inside a revealer.
const MARGIN := 16
var min_width_to_show_image := 500.0
var _visual_element: CanvasItem
onready var _rich_text_label := $HBoxContainer/MarginContainer/RichTextLabel as RichTextLabel
onready var _margin := $HBoxContainer/MarginContainer as MarginContainer
func _ready() -> void:
connect("resized", self, "_on_resized")
func setup(content_block: ContentBlock) -> void:
if not is_inside_tree():
yield(self, "ready")
_rich_text_label.bbcode_text = TextUtils.bbcode_add_code_color(content_block.text)
_margin.visible = not content_block.text.empty()
if content_block.visual_element_path != "":
# If the path isn't absolute, we try to load the file from the current directory
var path := content_block.visual_element_path
if path.is_rel_path():
path = content_block.resource_path.get_base_dir().plus_file(path)
var resource := load(path)
if resource is Texture:
var texture_rect := TextureRect.new()
texture_rect.texture = resource
add_child(texture_rect)
_visual_element = texture_rect
elif resource is PackedScene:
var instance = (resource as PackedScene).instance()
add_child(instance)
_visual_element = instance
else:
printerr(
(
"ContentBlock visual element is not a Texture or a PackedScene. Loaded type: "
+ resource.get_class()
)
)
# As this is a box container, we can reverse the order of elements by
# raising the panel.
if content_block.reverse_blocks:
_rich_text_label.raise()
func set_draw_panel(do_draw_panel: bool) -> void:
if not is_inside_tree():
yield(self, "ready")
if do_draw_panel:
add_stylebox_override("panel", preload("theme/panel_content_in_spoiler.tres"))
_margin.add_constant_override("margin_left", MARGIN)
_margin.add_constant_override("margin_right", MARGIN)
_margin.add_constant_override("margin_top", MARGIN)
_margin.add_constant_override("margin_bottom", MARGIN)
func _on_resized() -> void:
var width = rect_size.x
if _visual_element:
_visual_element.visible = width >= min_width_to_show_image