Skip to content

Commit

Permalink
Merge pull request #91 from Jeremi360/main
Browse files Browse the repository at this point in the history
AdvancedTextLabel working better in editor
  • Loading branch information
Jeremi360 authored Jan 23, 2023
2 parents 62b5d6a + 0c79e76 commit 00f5cd4
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,66 @@ tool
extends Button
class_name AdvancedTextButton

onready var adv_label := $AdvancedTextLabel

export(String, MULTILINE) var markup_text := "" setget _set_markup_text
export(String, "default", "markdown", "renpy", "bbcode") var markup := "default" setget _set_markup

# onready var adv_label := $AdvancedTextLabel
var adv_label : AdvancedTextLabel
var ready := false


func _ready():
if Engine.editor_hint and !adv_label:
adv_label = AdvancedTextLabel.new()
adv_label.scroll_active = false
adv_label.mouse_filter = MOUSE_FILTER_PASS
adv_label.rect_clip_content = false
add_child(adv_label)
adv_label.owner = self

else:
if !adv_label:
adv_label = $AdvancedTextLabel


adv_label.scroll_active = false
adv_label.mouse_filter = MOUSE_FILTER_PASS
adv_label.rect_clip_content = false
_set_markup(markup)
_set_markup_text(markup_text)

connect("mouse_entered", self, "_on_mouse_entered")
connect("mouse_exited", self, "_on_mouse_exited")
connect("pressed", self, "_on_pressed")
connect("toggled", self, "_on_toggled")
ready = true


func size_update():
rect_min_size = adv_label.rect_min_size


func _set_markup_text(text: String):
if Engine.editor_hint or !ready:
return

if !adv_label:
return


markup_text = text
adv_label.markup_text = text
yield(get_tree(), "idle_frame")
rect_size = adv_label.rect_size
size_update()


func _set_markup(markup: String):
if Engine.editor_hint or !ready:
return

if !adv_label:
return

markup = markup
adv_label.markup = markup
yield(get_tree(), "idle_frame")
rect_size = adv_label.rect_size
size_update()


func _set_label_color(color_name:String):
func _set_label_color(color_name: String):
adv_label.modulate = get_color(color_name, "Button")

func _set(property:String, value)->bool:

func _set(property: String, value) -> bool:
if property in get_property_list():
set(property, value)
else:
Expand All @@ -59,29 +73,32 @@ func _set(property:String, value)->bool:
_set_label_color("font_color_disabled")
else:
_set_label_color("font_color")

return true


func is_toggled():
return pressed and toggle_mode


func _on_mouse_entered():
if not (disabled or is_toggled()):
_set_label_color("font_color_hover")


func _on_mouse_exited():
if not (disabled or is_toggled()):
_set_label_color("font_color")


func _on_pressed():
if not disabled:
_set_label_color("font_color_pressed")

func _on_toggled(value:bool):

func _on_toggled(value: bool):
if not disabled:
if value:
_set_label_color("font_color_pressed")
else:
_set_label_color("font_color")


Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ size_flags_horizontal = 3
theme = ExtResource( 2 )
script = ExtResource( 3 )

[node name="RichTextLabel" type="RichTextLabel" parent="."]
[node name="AdvancedTextLabel" type="RichTextLabel" parent="."]
margin_right = 373.0
margin_bottom = 20.0
rect_clip_content = false
Expand Down
52 changes: 44 additions & 8 deletions addons/advanced-text/examples/EditTextOnClick/EditTextOnClick.gd
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
extends TabContainer

export(String, MULTILINE) var text := "[center][shake rate=5 level=10]**Clik to edit me**[/shake][/center]"
export (NodePath) onready var label = get_node(label)
export (NodePath) onready var save_button = get_node(save_button)
export (NodePath) onready var m_edit = get_node(m_edit)
export (NodePath) onready var button = get_node(button)
export var _button: NodePath
export var _edit: NodePath
export var _buttons_container: NodePath
export var _save_button: NodePath
export var _cancel_button: NodePath

var button: AdvancedTextButton
var m_edit: TextEdit
var buttons_container: Control
var save_button: Button
var cancel_button: Button


func _ready():
label.markup_text = text
button = get_node(_button)
m_edit = get_node(_edit)
buttons_container = get_node(_buttons_container)
save_button = get_node(_save_button)
cancel_button = get_node(_cancel_button)

m_edit.text = text
button.markup_text = text
button.connect("pressed", self, "_on_button_pressed")
save_button.connect("pressed", self, "_on_save_button_pressed")
cancel_button.connect("pressed", self, "_on_cancel_button_pressed")


func _on_button_pressed():
m_edit.text = button.markup_text
m_edit.rect_min_size = button.rect_size
current_tab = 1
buttons_container.show()


func _on_save_button_pressed():
label.markup_text = m_edit.text
button.markup_text = m_edit.text
current_tab = 0
buttons_container.hide()


func _on_cancel_button_pressed():
m_edit.text = button.markup_text
current_tab = 0
buttons_container.hide()


func _process(delta):
if m_edit.visible:
if current_tab == 1:
if Input.is_key_pressed(KEY_ESCAPE):
current_tab = 0
_on_cancel_button_pressed()
return

if Input.is_key_pressed(KEY_ENTER):
if Input.is_key_pressed(KEY_MASK_SHIFT):
m_edit.text += "\n"
return

_on_save_button_pressed()
51 changes: 37 additions & 14 deletions addons/advanced-text/examples/EditTextOnClick/EditTextOnClick.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,42 @@
[ext_resource path="res://addons/advanced-text/nodes/CodeEdit.gd" type="Script" id=2]
[ext_resource path="res://addons/advanced-text/examples/EditTextOnClick/EditTextOnClick.gd" type="Script" id=3]

[node name="EditTextOnClick" type="TabContainer"]
[node name="EditTextOnClick" type="VBoxContainer"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -156.0
margin_top = -18.0
margin_right = 164.0
margin_bottom = 191.0
margin_left = -189.0
margin_top = -104.5
margin_right = 189.0
margin_bottom = 104.5

[node name="Tabs" type="TabContainer" parent="."]
margin_right = 378.0
margin_bottom = 209.0
size_flags_horizontal = 3
size_flags_vertical = 3
tabs_visible = false
script = ExtResource( 3 )
__meta__ = {
"_edit_use_anchors_": false
}
_button = NodePath("AdvancedTextButton")
_edit = NodePath("MarkupEdit")
_buttons_container = NodePath("../HBoxContainer")
_save_button = NodePath("../HBoxContainer/SaveButton")
_cancel_button = NodePath("../HBoxContainer/CancelButton")

[node name="AdvancedTextButton" parent="." instance=ExtResource( 1 )]
[node name="AdvancedTextButton" parent="Tabs" instance=ExtResource( 1 )]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 4.0
margin_top = 8.0
margin_right = -4.0
margin_bottom = -4.0
rect_min_size = Vector2( 0, 0 )

[node name="MarkupEdit" type="TextEdit" parent="."]
[node name="MarkupEdit" type="TextEdit" parent="Tabs"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
Expand All @@ -41,14 +53,25 @@ syntax_highlighting = true
script = ExtResource( 2 )
configs = [ "res://addons/advanced-text/highlights/bbcode.json", "res://addons/advanced-text/highlights/markdown.json" ]

[node name="Button" type="Button" parent="MarkupEdit"]
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -41.0
margin_top = -20.0
[node name="HBoxContainer" type="HBoxContainer" parent="."]
visible = false
margin_top = 38.0
margin_right = 98.0
margin_bottom = 58.0

[node name="SaveButton" type="Button" parent="HBoxContainer"]
margin_right = 41.0
margin_bottom = 20.0
text = "save"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="CancelButton" type="Button" parent="HBoxContainer"]
margin_left = 45.0
margin_right = 98.0
margin_bottom = 20.0
text = "cancel"
__meta__ = {
"_edit_use_anchors_": false
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
tool
extends Control

export(String, MULTILINE) var text := "[center][shake rate=5 level=10]**Clik to edit me**[/shake][/center]"
export (NodePath) onready var label = get_node(label)
export (NodePath) onready var m_edit = get_node(m_edit)
export (NodePath) onready var button = get_node(button)
export(NodePath) onready var m_edit = get_node(m_edit)
export(NodePath) onready var button = get_node(button)


func _ready():
label.markup_text = text
button.markup_text = text
m_edit.text = text
button.connect("toggled", self, "_on_button_toggled")
m_edit.connect("text_changed", self, "_on_text_changed")

func _on_button_toggled(toggled:bool):

func _on_button_toggled(toggled: bool):
if toggled:
var pos = button.rect_global_position
pos.y += button.rect_size.y
var r = $Popup.rect_size
var rect = Rect2(pos, r)
$Popup.popup(rect)

else:
$Popup.hide()


func _on_text_changed():
label.markup_text = m_edit.text
button.markup_text = m_edit.text


func _process(delta):
if $Popup.visible:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@ script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
m_edit = NodePath("Popup/MarkupEdit")
button = NodePath("AdvancedTextButton")

[node name="AdvancedTextButton" parent="." instance=ExtResource( 3 )]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 4.0
margin_top = 8.0
margin_right = -4.0
margin_bottom = -4.0
size_flags_horizontal = 3
margin_right = 91.0
margin_bottom = -2.0
size_flags_vertical = 3
toggle_mode = true

[node name="AdvancedTextLabel" parent="AdvancedTextButton" index="0"]
bbcode_text = "[center][shake rate=5 level=10][b]Clik to edit me[/b][/shake][/center]"
text = "Clik to edit me"
markup_text = "[center][shake rate=5 level=10]**Clik to edit me**[/shake][/center]"

[node name="Popup" type="Popup" parent="."]
visible = true
margin_left = 4.0
Expand All @@ -50,5 +46,3 @@ anchor_bottom = 1.0
syntax_highlighting = true
script = ExtResource( 1 )
configs = [ "res://addons/advanced-text/highlights/bbcode.json", "res://addons/advanced-text/highlights/markdown.json" ]

[editable path="AdvancedTextButton"]

0 comments on commit 00f5cd4

Please sign in to comment.