2019-10-18 01:22:11 +02:00
tool
extends VBoxContainer
2022-07-15 10:20:59 +02:00
var LastOpenedFiles = null
2019-10-18 01:22:11 +02:00
2022-07-14 20:01:51 +02:00
var text_editor : TextEdit = null
2019-10-18 01:22:11 +02:00
2022-07-15 00:50:40 +02:00
var file_list : FileDialog = null
2020-10-25 00:57:16 +02:00
var ClosingFile
2019-10-18 01:22:11 +02:00
2022-07-14 20:01:51 +02:00
var search_box : HBoxContainer = null
var search_box_line_edit : LineEdit = null
var search_box_match_case_cb : CheckBox = null
var search_box_whole_words_cb : CheckBox = null
var search_box_close_button : Button = null
2019-10-18 01:22:11 +02:00
2022-07-14 20:01:51 +02:00
var replace_box : HBoxContainer = null
var replace_box_replace_le : LineEdit = null
var replace_box_with : LineEdit = null
var replace_box_button : Button = null
var replace_box_close : Button = null
2019-10-18 01:22:11 +02:00
2022-07-14 20:01:51 +02:00
var file_info_last_modified_icon : TextureRect = null
var file_info_last_modified : Label = null
var file_info_c_counter : Label = null
var file_info_read_only : CheckBox = null
2019-10-18 01:22:11 +02:00
var current_path = " "
var current_filename = " "
var search_flag = 0
signal text_changed ( )
2022-07-14 20:01:51 +02:00
func _init ( ) :
size_flags_vertical = SIZE_EXPAND_FILL
set_anchors_and_margins_preset ( Control . PRESET_WIDE )
text_editor = TextEdit . new ( )
add_child ( text_editor )
text_editor . highlight_current_line = true
text_editor . syntax_highlighting = true
text_editor . show_line_numbers = true
text_editor . breakpoint_gutter = true
text_editor . highlight_all_occurrences = true
text_editor . override_selected_font_color = true
text_editor . smooth_scrolling = true
text_editor . hiding_enabled = true
#todo look this up from the editor settings
#text_editor.caret_blink = true
#text_editor.caret_blink_speed = 1
text_editor . caret_moving_by_right_click = false
2022-07-14 20:52:45 +02:00
text_editor . minimap_draw = true
2022-07-14 20:01:51 +02:00
text_editor . size_flags_vertical = SIZE_EXPAND_FILL
text_editor . set ( " custom_colors/member_variable_color " , Color ( 0.737255 , 0.882353 , 1 ) )
text_editor . set ( " custom_colors/code_folding_color " , Color ( 1 , 1 , 1 , 0.701961 ) )
text_editor . set ( " custom_colors/function_color " , Color ( 0.341176 , 0.701961 , 1 ) )
text_editor . set ( " custom_colors/safe_line_number_color " , Color ( 0.8 , 0.968627 , 0.827451 , 0.74902 ) )
text_editor . set ( " custom_colors/symbol_color " , Color ( 0.670588 , 0.788235 , 1 ) )
text_editor . set ( " custom_colors/caret_background_color " , Color ( 0 , 0 , 0 ) )
text_editor . set ( " custom_colors/selection_color " , Color ( 0.411765 , 0.611765 , 0.909804 , 0.34902 ) )
text_editor . set ( " custom_colors/caret_color " , Color ( 1 , 1 , 1 ) )
text_editor . set ( " custom_colors/breakpoint_color " , Color ( 1 , 0.470588 , 0.419608 ) )
text_editor . set ( " custom_colors/font_color_selected " , Color ( 0 , 0 , 0 ) )
text_editor . set ( " custom_colors/font_color " , Color ( 1 , 1 , 1 ) )
text_editor . set ( " custom_colors/completion_font_color " , Color ( 1 , 1 , 1 , 0.392157 ) )
text_editor . set ( " custom_colors/completion_scroll_color " , Color ( 1 , 1 , 1 , 0.070588 ) )
text_editor . set ( " custom_colors/background_color " , Color ( 0.121569 , 0.145098 , 0.192157 ) )
text_editor . set ( " custom_colors/number_color " , Color ( 0.631373 , 1 , 0.882353 ) )
text_editor . set ( " custom_colors/completion_background_color " , Color ( 0.196078 , 0.231373 , 0.309804 ) )
text_editor . set ( " custom_colors/brace_mismatch_color " , Color ( 1 , 0.470588 , 0.419608 ) )
text_editor . set ( " custom_colors/current_line_color " , Color ( 1 , 1 , 1 , 0.070588 ) )
text_editor . set ( " custom_colors/completion_selected_color " , Color ( 1 , 1 , 1 , 0.070588 ) )
text_editor . set ( " custom_colors/mark_color " , Color ( 1 , 0.470588 , 0.419608 , 0.301961 ) )
text_editor . set ( " custom_colors/word_highlighted_color " , Color ( 1 , 1 , 1 , 0.392157 ) )
text_editor . set ( " custom_colors/completion_existing_color " , Color ( 1 , 1 , 1 , 0.392157 ) )
text_editor . set ( " custom_constants/completion_lines " , 20 )
text_editor . set ( " custom_constants/completion_max_width " , 20 )
text_editor . set ( " custom_constants/completion_scroll_width " , 20 )
#text_editor.owner = self
search_box = HBoxContainer . new ( )
add_child ( search_box )
search_box . hide ( )
var selabel : Label = Label . new ( )
search_box . add_child ( selabel )
selabel . text = " Search: "
search_box_line_edit = LineEdit . new ( )
search_box . add_child ( search_box_line_edit )
search_box_line_edit . size_flags_horizontal = SIZE_EXPAND_FILL
search_box_line_edit . connect ( " text_changed " , self , " _on_LineEdit_text_changed " )
search_box_line_edit . connect ( " focus_entered " , self , " _on_LineEdit_focus_entered " )
search_box_match_case_cb = CheckBox . new ( )
search_box . add_child ( search_box_match_case_cb )
search_box_match_case_cb . text = " Match Case "
search_box_match_case_cb . connect ( " toggled " , self , " _on_matchcase_toggled " )
search_box_whole_words_cb = CheckBox . new ( )
search_box . add_child ( search_box_whole_words_cb )
search_box_whole_words_cb . text = " Whole Words "
search_box_whole_words_cb . connect ( " toggled " , self , " _on_wholewords_toggled " )
search_box_close_button = Button . new ( )
search_box . add_child ( search_box_close_button )
search_box_close_button . text = " x "
search_box_close_button . flat = true
search_box_whole_words_cb . connect ( " pressed " , self , " _on_close_pressed " )
replace_box = HBoxContainer . new ( )
add_child ( replace_box )
replace_box . hide ( )
var rblabel : Label = Label . new ( )
replace_box . add_child ( rblabel )
rblabel . text = " Replace: "
replace_box_replace_le = LineEdit . new ( )
replace_box . add_child ( replace_box_replace_le )
replace_box_replace_le . size_flags_horizontal = SIZE_EXPAND_FILL
var rb2label : Label = Label . new ( )
replace_box . add_child ( rb2label )
rb2label . text = " With: "
replace_box_with = LineEdit . new ( )
replace_box . add_child ( replace_box_with )
replace_box_with . size_flags_horizontal = SIZE_EXPAND_FILL
replace_box_button = Button . new ( )
replace_box . add_child ( replace_box_button )
replace_box_button . text = " Replace "
replace_box_button . connect ( " pressed " , self , " _on_Button_pressed " )
replace_box_close = Button . new ( )
replace_box . add_child ( replace_box_close )
replace_box_close . text = " x "
replace_box_close . flat = true
replace_box_button . connect ( " pressed " , self , " _on_close2_pressed " )
var file_info : HBoxContainer = HBoxContainer . new ( )
add_child ( file_info )
file_info_last_modified_icon = TextureRect . new ( )
file_info . add_child ( file_info_last_modified_icon )
file_info_last_modified_icon . stretch_mode = TextureRect . STRETCH_KEEP_CENTERED
var filabel : Label = Label . new ( )
file_info . add_child ( filabel )
filabel . text = " Last modified time: "
file_info_last_modified = Label . new ( )
file_info . add_child ( file_info_last_modified )
var fi2label : Label = Label . new ( )
file_info . add_child ( fi2label )
fi2label . text = " Characters counter: "
fi2label . align = Label . ALIGN_RIGHT
fi2label . size_flags_horizontal = SIZE_EXPAND | SIZE_FILL | SIZE_SHRINK_CENTER | SIZE_SHRINK_END
file_info_c_counter = Label . new ( )
file_info . add_child ( file_info_c_counter )
file_info_c_counter . size_flags_horizontal = SIZE_EXPAND
file_info_read_only = CheckBox . new ( )
file_info . add_child ( file_info_read_only )
file_info_read_only . text = " Can Edit "
file_info_read_only . flat = true
file_info_read_only . size_flags_horizontal = SIZE_EXPAND | SIZE_SHRINK_END
2019-10-18 01:22:11 +02:00
func _ready ( ) :
2022-07-15 00:50:40 +02:00
text_editor . connect ( " text_changed " , self , " _on_text_editor_text_changed " )
2022-07-14 20:01:51 +02:00
2022-07-15 00:50:40 +02:00
#FileList = get_parent().get_parent().get_parent().get_parent().get_node("FileList")
2022-07-14 10:23:53 +02:00
2022-07-14 20:01:51 +02:00
file_info_read_only . connect ( " toggled " , self , " _on_Readonly_toggled " )
2022-07-14 10:23:53 +02:00
2022-07-15 00:45:12 +02:00
#file_info_read_only.set("custom_icons/checked",IconLoader.load_icon_from_name("read"))
#file_info_read_only.set("custom_icons/unchecked",IconLoader.load_icon_from_name("edit"))
2022-07-14 10:23:53 +02:00
add_to_group ( " vanilla_editor " )
load_default_font ( )
2019-10-18 01:22:11 +02:00
2020-10-12 18:13:53 +02:00
func set_font ( font_path : String ) - > void :
2022-07-14 10:23:53 +02:00
var dynamic_font : DynamicFont = DynamicFont . new ( )
var dynamic_font_data : DynamicFontData = DynamicFontData . new ( )
dynamic_font_data . set_font_path ( font_path )
dynamic_font . set_font_data ( dynamic_font_data )
2022-07-14 20:01:51 +02:00
text_editor . set ( " custom_fonts/font " , dynamic_font )
2020-10-12 18:13:53 +02:00
2020-10-13 17:17:22 +02:00
func load_default_font ( ) - > void :
2022-07-14 10:23:53 +02:00
var default_font = LastOpenedFiles . get_editor_font ( )
if default_font :
set_font ( default_font )
2020-10-13 17:17:22 +02:00
2019-10-18 01:22:11 +02:00
func set_wrap_enabled ( enabled : bool ) :
2022-07-14 20:01:51 +02:00
text_editor . set_wrap_enabled ( enabled )
text_editor . update ( )
2019-10-18 01:22:11 +02:00
func draw_minimap ( value : bool ) :
2022-07-14 20:01:51 +02:00
text_editor . draw_minimap ( value )
text_editor . update ( )
2019-10-18 01:22:11 +02:00
func color_region ( filextension : String ) : # -----------------------------> dal momento che voglio creare un editor per ogni file, renderò questa funzione singola in base all'estensione del file
2022-07-14 10:23:53 +02:00
match ( filextension ) :
" bbs " :
2022-07-14 20:01:51 +02:00
text_editor . add_color_region ( " [b] " , " [/b] " , Color8 ( 153 , 153 , 255 , 255 ) , false )
text_editor . add_color_region ( " [i] " , " [/i] " , Color8 ( 153 , 255 , 153 , 255 ) , false )
text_editor . add_color_region ( " [s] " , " [/s] " , Color8 ( 255 , 153 , 153 , 255 ) , false )
text_editor . add_color_region ( " [u] " , " [/u] " , Color8 ( 255 , 255 , 102 , 255 ) , false )
text_editor . add_color_region ( " [url " , " [/url] " , Color8 ( 153 , 204 , 255 , 255 ) , false )
text_editor . add_color_region ( " [code] " , " [/code] " , Color8 ( 192 , 192 , 192 , 255 ) , false )
text_editor . add_color_region ( " [img] " , " [/img] " , Color8 ( 255 , 204 , 153 , 255 ) , false )
text_editor . add_color_region ( " [center] " , " [/center] " , Color8 ( 175 , 238 , 238 , 255 ) , false )
text_editor . add_color_region ( " [right] " , " [/right] " , Color8 ( 135 , 206 , 235 , 255 ) , false )
2022-07-14 10:23:53 +02:00
" html " :
2022-07-14 20:01:51 +02:00
text_editor . add_color_region ( " <b> " , " </b> " , Color8 ( 153 , 153 , 255 , 255 ) , false )
text_editor . add_color_region ( " <i> " , " </i> " , Color8 ( 153 , 255 , 153 , 255 ) , false )
text_editor . add_color_region ( " <del> " , " </del> " , Color8 ( 255 , 153 , 153 , 255 ) , false )
text_editor . add_color_region ( " <ins> " , " </ins> " , Color8 ( 255 , 255 , 102 , 255 ) , false )
text_editor . add_color_region ( " <a " , " </a> " , Color8 ( 153 , 204 , 255 , 255 ) , false )
text_editor . add_color_region ( " <img " , " /> " , Color8 ( 255 , 204 , 153 , 255 ) , true )
text_editor . add_color_region ( " <pre> " , " </pre> " , Color8 ( 192 , 192 , 192 , 255 ) , false )
text_editor . add_color_region ( " <center> " , " </center> " , Color8 ( 175 , 238 , 238 , 255 ) , false )
text_editor . add_color_region ( " <right> " , " </right> " , Color8 ( 135 , 206 , 235 , 255 ) , false )
2022-07-14 10:23:53 +02:00
" md " :
2022-07-14 20:01:51 +02:00
text_editor . add_color_region ( " *** " , " *** " , Color8 ( 126 , 186 , 181 , 255 ) , false )
text_editor . add_color_region ( " ** " , " ** " , Color8 ( 153 , 153 , 255 , 255 ) , false )
text_editor . add_color_region ( " * " , " * " , Color8 ( 153 , 255 , 153 , 255 ) , false )
text_editor . add_color_region ( " + " , " " , Color8 ( 255 , 178 , 102 , 255 ) , false )
text_editor . add_color_region ( " - " , " " , Color8 ( 255 , 178 , 102 , 255 ) , false )
text_editor . add_color_region ( " ~~ " , " ~~ " , Color8 ( 255 , 153 , 153 , 255 ) , false )
text_editor . add_color_region ( " __ " , " __ " , Color8 ( 255 , 255 , 102 , 255 ) , false )
text_editor . add_color_region ( " [ " , " ) " , Color8 ( 153 , 204 , 255 , 255 ) , false )
text_editor . add_color_region ( " ` " , " ` " , Color8 ( 192 , 192 , 192 , 255 ) , false )
text_editor . add_color_region ( ' " *. ' , ' " ' , Color8 ( 255 , 255 , 255 , 255 ) , true )
text_editor . add_color_region ( " # " , " " , Color8 ( 105 , 105 , 105 , 255 ) , true )
text_editor . add_color_region ( " ## " , " " , Color8 ( 128 , 128 , 128 , 255 ) , true )
text_editor . add_color_region ( " ### " , " " , Color8 ( 169 , 169 , 169 , 255 ) , true )
text_editor . add_color_region ( " #### " , " " , Color8 ( 192 , 192 , 192 , 255 ) , true )
text_editor . add_color_region ( " ##### " , " " , Color8 ( 211 , 211 , 211 , 255 ) , true )
text_editor . add_color_region ( " ###### " , " " , Color8 ( 255 , 255 , 255 , 255 ) , true )
text_editor . add_color_region ( " > " , " " , Color8 ( 172 , 138 , 79 , 255 ) , true )
2022-07-14 10:23:53 +02:00
" cfg " :
2022-07-14 20:01:51 +02:00
text_editor . add_color_region ( " [ " , " ] " , Color8 ( 153 , 204 , 255 , 255 ) , false )
text_editor . add_color_region ( ' " ' , ' " ' , Color8 ( 255 , 255 , 102 , 255 ) , false )
text_editor . add_color_region ( ' ; ' , ' ' , Color8 ( 128 , 128 , 128 , 255 ) , true )
2022-07-14 10:23:53 +02:00
" ini " :
2022-07-14 20:01:51 +02:00
text_editor . add_color_region ( " [ " , " ] " , Color8 ( 153 , 204 , 255 , 255 ) , false )
text_editor . add_color_region ( ' " ' , ' " ' , Color8 ( 255 , 255 , 102 , 255 ) , false )
text_editor . add_color_region ( ' ; ' , ' ' , Color8 ( 128 , 128 , 128 , 255 ) , true )
2022-07-14 10:23:53 +02:00
_ :
pass
2019-10-18 01:22:11 +02:00
func clean_editor ( ) :
2022-07-14 20:01:51 +02:00
text_editor . set_text ( " " )
2022-07-15 00:45:12 +02:00
#file_info_last_modified_icon.texture = IconLoader.load_icon_from_name("save")
2022-07-14 20:01:51 +02:00
file_info_last_modified . set_text ( " " )
2022-07-15 00:50:40 +02:00
file_list . invalidate ( )
2022-07-14 10:23:53 +02:00
current_filename = " "
current_path = " "
2019-10-18 01:22:11 +02:00
func new_file_open ( file_content : String , last_modified : Dictionary , current_file_path : String ) :
2022-07-14 10:23:53 +02:00
current_path = current_file_path
current_filename = current_file_path . get_file ( )
color_region ( current_filename . get_extension ( ) )
2022-07-14 20:01:51 +02:00
text_editor . set_text ( file_content )
2022-07-14 10:23:53 +02:00
update_lastmodified ( last_modified , " save " )
2022-07-15 00:50:40 +02:00
file_list . invalidate ( )
2022-07-14 10:23:53 +02:00
count_characters ( )
2019-10-18 01:22:11 +02:00
func update_lastmodified ( last_modified : Dictionary , icon : String ) :
2022-07-14 20:01:51 +02:00
file_info_last_modified . set_text ( str ( last_modified . hour ) + " : " + str ( last_modified . minute ) + " " + str ( last_modified . day ) + " / " + str ( last_modified . month ) + " / " + str ( last_modified . year ) )
2022-07-15 00:45:12 +02:00
#file_info_last_modified_icon.texture = IconLoader.load_icon_from_name(icon)
2019-10-18 01:22:11 +02:00
func new_file_create ( file_name ) :
2022-07-14 20:01:51 +02:00
text_editor . set_text ( " " )
2022-07-14 10:23:53 +02:00
2022-07-15 00:50:40 +02:00
file_list . invalidate ( )
2019-10-18 01:22:11 +02:00
func _on_Readonly_toggled ( button_pressed ) :
2022-07-14 10:23:53 +02:00
if button_pressed :
2022-07-14 20:01:51 +02:00
file_info_read_only . set_text ( " Read Only " )
text_editor . readonly = ( true )
2022-07-14 10:23:53 +02:00
else :
2022-07-14 20:01:51 +02:00
file_info_read_only . set_text ( " Can Edit " )
text_editor . readonly = ( false )
2019-10-18 01:22:11 +02:00
2022-07-14 20:01:51 +02:00
func _on_text_editor_text_changed ( ) :
2022-07-15 00:45:12 +02:00
#file_info_last_modified_icon.texture = IconLoader.load_icon_from_name("saveas")
2022-07-14 10:23:53 +02:00
count_characters ( )
emit_signal ( " text_changed " )
2019-10-18 01:22:11 +02:00
func count_characters ( ) :
2022-07-14 10:23:53 +02:00
var counted : int = 0
2022-07-14 20:01:51 +02:00
for line in text_editor . get_line_count ( ) :
counted += text_editor . get_line ( line ) . length ( )
file_info_c_counter . set_text ( str ( counted ) )
2019-10-18 01:22:11 +02:00
func _on_LineEdit_text_changed ( new_text ) :
2022-07-14 20:01:51 +02:00
var linecount = text_editor . get_line_count ( )
2022-07-14 10:23:53 +02:00
if new_text != " " :
var found
var find = false
for line in range ( 0 , linecount ) :
2022-07-14 20:01:51 +02:00
for column in range ( 0 , text_editor . get_line ( line ) . length ( ) ) :
found = text_editor . search ( new_text , search_flag , line , column )
2022-07-14 10:23:53 +02:00
if found . size ( ) :
if found [ 1 ] == line :
2019-10-18 01:22:11 +02:00
# if not find:
2022-07-14 20:01:51 +02:00
text_editor . select ( line , found [ 0 ] , found [ 1 ] , found [ 0 ] + new_text . length ( ) )
2019-10-18 01:22:11 +02:00
# find = true
2022-07-14 10:23:53 +02:00
else :
2022-07-14 20:01:51 +02:00
text_editor . select ( 0 , 0 , 0 , 0 )
2022-07-14 10:23:53 +02:00
else :
2022-07-14 20:01:51 +02:00
text_editor . select ( 0 , 0 , 0 , 0 )
2019-10-18 01:22:11 +02:00
func _on_matchcase_toggled ( button_pressed ) :
2022-07-14 10:23:53 +02:00
if button_pressed :
search_flag = 1
else :
2022-07-14 20:01:51 +02:00
if search_box_whole_words_cb . is_pressed ( ) :
2022-07-14 10:23:53 +02:00
search_flag = 2
else :
search_flag = 0
2022-07-14 20:01:51 +02:00
_on_LineEdit_text_changed ( search_box_line_edit . get_text ( ) )
2019-10-18 01:22:11 +02:00
func _on_wholewords_toggled ( button_pressed ) :
2022-07-14 10:23:53 +02:00
if button_pressed :
search_flag = 2
else :
2022-07-14 20:01:51 +02:00
if search_box_match_case_cb . is_pressed ( ) :
2022-07-14 10:23:53 +02:00
search_flag = 1
else :
search_flag = 0
2022-07-14 20:01:51 +02:00
_on_LineEdit_text_changed ( search_box_line_edit . get_text ( ) )
2019-10-18 01:22:11 +02:00
func _on_close_pressed ( ) :
2022-07-14 20:01:51 +02:00
search_box . hide ( )
2019-10-18 01:22:11 +02:00
2022-07-14 20:01:51 +02:00
func open_search_box ( ) :
if search_box . visible :
search_box . hide ( )
2022-07-14 10:23:53 +02:00
else :
2022-07-14 20:01:51 +02:00
search_box . show ( )
search_box . get_node ( " LineEdit " ) . grab_focus ( )
2019-10-18 01:22:11 +02:00
func _on_Button_pressed ( ) :
2022-07-14 20:01:51 +02:00
var linecount = text_editor . get_line_count ( ) - 1
var old_text = replace_box_replace_le . get_text ( )
var new_text = replace_box_with . get_text ( )
var text = text_editor . get_text ( )
text_editor . set_text ( text . replace ( old_text , new_text ) )
func open_replace_box ( ) :
if replace_box . visible :
replace_box . hide ( )
2022-07-14 10:23:53 +02:00
else :
2022-07-14 20:01:51 +02:00
replace_box . show ( )
replace_box . get_node ( " replace " ) . grab_focus ( )
2019-10-18 01:22:11 +02:00
func _on_close2_pressed ( ) :
2022-07-14 20:01:51 +02:00
replace_box . hide ( )
2019-10-18 01:22:11 +02:00
func _on_LineEdit_focus_entered ( ) :
2022-07-14 20:01:51 +02:00
_on_LineEdit_text_changed ( search_box_line_edit . get_text ( ) )