@@ -11,8 +11,11 @@ var picker_popup: Popup
11
11
12
12
13
13
func _enter_tree () -> void :
14
- initialize_picker ()
15
14
initialize_gutter ()
15
+ initialize_picker ()
16
+ # the shader editor does not exist until opened for the first time
17
+ # so, to get it consistently, check if it's there whenever the focus changes
18
+ get_viewport ().gui_focus_changed .connect (get_shader_editor_code_edit )
16
19
17
20
18
21
func _exit_tree () -> void :
@@ -26,7 +29,7 @@ func initialize_picker() -> void:
26
29
picker_popup .connect ("popup_hide" , on_picker_popup_close )
27
30
picker_popup .hide ()
28
31
29
- var picker = picker_popup .get_node ("ColorPicker" )
32
+ var picker : ColorPicker = picker_popup .get_node ("ColorPicker" )
30
33
if not picker .is_connected ("color_changed" , picker_color_changed ):
31
34
picker .connect ("color_changed" , picker_color_changed )
32
35
@@ -65,24 +68,30 @@ func get_all_text_editors(parent : Node) -> void:
65
68
if child .get_child_count ():
66
69
get_all_text_editors (child )
67
70
68
- if child is TextEdit :
69
- editors .append (child )
70
-
71
- if child .is_connected ("text_changed" , text_changed ):
72
- child .disconnect ("text_changed" , text_changed )
73
- child .text_changed .connect (text_changed .bind (child ))
74
-
75
- if child .is_connected ("caret_changed" , caret_changed ):
76
- child .disconnect ("caret_changed" , caret_changed )
77
- child .caret_changed .connect (caret_changed .bind (child ))
71
+ if child is CodeEdit :
72
+ add_code_edit_to_editors_array (child )
73
+
74
+
75
+ func get_shader_editor_code_edit (node : Node ):
76
+ if not node is CodeEdit or not node .get_parent ().get_class () == "ShaderTextEditor" :
77
+ return
78
+
79
+ if not editors .has (node ):
80
+ add_code_edit_to_editors_array (node )
81
+ initialize_gutter ()
82
+ initialize_picker ()
78
83
79
84
80
- func caret_changed ( textedit : TextEdit ) -> void :
81
- handle_change ( textedit )
85
+ func add_code_edit_to_editors_array ( node : CodeEdit ) -> void :
86
+ editors . append ( node )
82
87
88
+ if node .is_connected ("text_changed" , handle_change ):
89
+ node .disconnect ("text_changed" , handle_change )
90
+ node .text_changed .connect (handle_change .bind (node ))
83
91
84
- func text_changed (textedit : TextEdit ) -> void :
85
- handle_change (textedit )
92
+ if node .is_connected ("caret_changed" , handle_change ):
93
+ node .disconnect ("caret_changed" , handle_change )
94
+ node .caret_changed .connect (handle_change .bind (node ))
86
95
87
96
88
97
func handle_change (textedit : TextEdit ) -> void :
@@ -98,8 +107,8 @@ func handle_change(textedit : TextEdit) -> void:
98
107
99
108
100
109
func editor_script_changed (script : Script ) -> void :
101
- initialize_picker ()
102
110
initialize_gutter ()
111
+ initialize_picker ()
103
112
if current_textedit :
104
113
if current_textedit .is_connected ("gui_input" , textedit_clicked ):
105
114
current_textedit .disconnect ("gui_input" , textedit_clicked )
@@ -254,7 +263,17 @@ func on_picker_popup_close() -> void:
254
263
var text := current_textedit .get_line (hovering_line )
255
264
var color_match : RegExMatch = match_color_in_string (text )
256
265
var new_color = get_line_color (current_textedit , hovering_line )
257
- text = text .replace (color_match .get_string (), "Color" + str (new_color ))
266
+
267
+ if color_from_regex_match (color_match ) == new_color :
268
+ return # don't change if equal -> doesn't mess up constants, strings etc.
269
+
270
+ if color_match .get_string ('const' ): # replace the whole color string
271
+ text = text .replace (color_match .get_string (), "Color" + str (new_color ))
272
+
273
+ else : # only replace inside parenthesis to cover Color(...) and vec4(...)
274
+ var color_string = str (new_color ).replace ("(" , "" ).replace (")" , "" )
275
+ text = text .replace (color_match .get_string ("params" ), color_string )
276
+
258
277
current_textedit .set_line (hovering_line , text )
259
278
260
279
@@ -315,10 +334,19 @@ func named_or_hex_color(string: String): # Color or null
315
334
316
335
func match_color_in_string (string : String ) -> RegExMatch :
317
336
var re = RegEx .new ()
337
+ var color
338
+
318
339
re .compile ("Color\\ ((?<params>(?R)*.*?)\\ )" )
319
- var color = re .search (string )
340
+ color = re .search (string )
320
341
if color != null :
321
342
return color
343
+
322
344
re .compile ("Color\\ .(?<const>[A-Z_]+)\\ b" )
323
- return re .search (string )
345
+ color = re .search (string )
346
+ if color != null :
347
+ return color
348
+
349
+ re .compile ("source_color.*?vec4\\ ((?<params>(?R)*.*?)\\ )" )
350
+ color = re .search (string )
351
+ return color
324
352
0 commit comments