2019-09-24 00:42:50 +02:00
tool
extends VBoxContainer
onready var ReadOnly = $ FileInfo / Readonly
onready var TextEditor = $ TextEditor
onready var LastModified = $ FileInfo / lastmodified
onready var FileList = get_parent ( ) . get_parent ( ) . get_parent ( ) . get_node ( " FileList " )
2019-10-03 16:06:46 +02:00
onready var ClosingFile = get_parent ( ) . get_parent ( ) . get_parent ( ) . get_node ( " ConfirmationDialog " )
onready var LastModifiedIcon = $ FileInfo / lastmodified_icon
2019-10-11 21:53:20 +02:00
onready var SearchBox = $ SearchBox
onready var ReplaceBox = $ ReplaceBox
onready var c_counter = $ FileInfo / c_counter
2019-09-24 00:42:50 +02:00
var current_path = " "
var current_filename = " "
2019-09-24 17:03:49 +02:00
var Preview = load ( " res://addons/file-editor/scenes/Preview.tscn " )
2019-10-11 21:53:20 +02:00
var search_flag = 0
2019-10-08 00:25:16 +02:00
signal text_changed ( )
2019-09-24 00:42:50 +02:00
func _ready ( ) :
2019-10-03 16:06:46 +02:00
ClosingFile . connect ( " confirmed " , self , " queue_free " )
2019-09-24 00:42:50 +02:00
ReadOnly . connect ( " toggled " , self , " _on_Readonly_toggled " )
2019-10-03 16:06:46 +02:00
ReadOnly . set ( " custom_icons/checked " , IconLoader . load_icon_from_name ( " read " ) )
ReadOnly . set ( " custom_icons/unchecked " , IconLoader . load_icon_from_name ( " edit " ) )
2019-10-11 21:53:20 +02:00
add_to_group ( " vanilla_editor " )
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
match ( filextension ) :
" bbs " :
TextEditor . add_color_region ( " [b] " , " [/b] " , Color8 ( 153 , 153 , 255 , 255 ) , false )
TextEditor . add_color_region ( " [i] " , " [/i] " , Color8 ( 153 , 255 , 153 , 255 ) , false )
TextEditor . add_color_region ( " [s] " , " [/s] " , Color8 ( 255 , 153 , 153 , 255 ) , false )
TextEditor . add_color_region ( " [u] " , " [/u] " , Color8 ( 255 , 255 , 102 , 255 ) , false )
TextEditor . add_color_region ( " [url " , " [/url] " , Color8 ( 153 , 204 , 255 , 255 ) , false )
TextEditor . add_color_region ( " [code] " , " [/code] " , Color8 ( 192 , 192 , 192 , 255 ) , false )
TextEditor . add_color_region ( " [img] " , " [/img] " , Color8 ( 255 , 204 , 153 , 255 ) , false )
TextEditor . add_color_region ( " [center] " , " [/center] " , Color8 ( 175 , 238 , 238 , 255 ) , false )
TextEditor . add_color_region ( " [right] " , " [/right] " , Color8 ( 135 , 206 , 235 , 255 ) , false )
" html " :
TextEditor . add_color_region ( " <b> " , " </b> " , Color8 ( 153 , 153 , 255 , 255 ) , false )
TextEditor . add_color_region ( " <i> " , " </i> " , Color8 ( 153 , 255 , 153 , 255 ) , false )
TextEditor . add_color_region ( " <del> " , " </del> " , Color8 ( 255 , 153 , 153 , 255 ) , false )
TextEditor . add_color_region ( " <ins> " , " </ins> " , Color8 ( 255 , 255 , 102 , 255 ) , false )
TextEditor . add_color_region ( " <a " , " </a> " , Color8 ( 153 , 204 , 255 , 255 ) , false )
TextEditor . add_color_region ( " <img " , " /> " , Color8 ( 255 , 204 , 153 , 255 ) , true )
TextEditor . add_color_region ( " <pre> " , " </pre> " , Color8 ( 192 , 192 , 192 , 255 ) , false )
TextEditor . add_color_region ( " <center> " , " </center> " , Color8 ( 175 , 238 , 238 , 255 ) , false )
TextEditor . add_color_region ( " <right> " , " </right> " , Color8 ( 135 , 206 , 235 , 255 ) , false )
" md " :
TextEditor . add_color_region ( " ** " , " ** " , Color8 ( 153 , 153 , 255 , 255 ) , false )
TextEditor . add_color_region ( " * " , " * " , Color8 ( 153 , 255 , 153 , 255 ) , false )
TextEditor . add_color_region ( " + " , " " , Color8 ( 255 , 178 , 102 , 255 ) , false )
TextEditor . add_color_region ( " - " , " " , Color8 ( 255 , 178 , 102 , 255 ) , false )
TextEditor . add_color_region ( " ~~ " , " ~~ " , Color8 ( 255 , 153 , 153 , 255 ) , false )
TextEditor . add_color_region ( " __ " , " __ " , Color8 ( 255 , 255 , 102 , 255 ) , false )
TextEditor . add_color_region ( " [ " , " ) " , Color8 ( 153 , 204 , 255 , 255 ) , false )
TextEditor . add_color_region ( " ` " , " ` " , Color8 ( 192 , 192 , 192 , 255 ) , false )
TextEditor . add_color_region ( ' " *. ' , ' " ' , Color8 ( 255 , 255 , 255 , 255 ) , true )
TextEditor . add_color_region ( " # " , " " , Color8 ( 105 , 105 , 105 , 255 ) , true )
TextEditor . add_color_region ( " ## " , " " , Color8 ( 128 , 128 , 128 , 255 ) , true )
TextEditor . add_color_region ( " ### " , " " , Color8 ( 169 , 169 , 169 , 255 ) , true )
TextEditor . add_color_region ( " #### " , " " , Color8 ( 192 , 192 , 192 , 255 ) , true )
TextEditor . add_color_region ( " ##### " , " " , Color8 ( 211 , 211 , 211 , 255 ) , true )
TextEditor . add_color_region ( " ###### " , " " , Color8 ( 255 , 255 , 255 , 255 ) , true )
" cfg " :
TextEditor . add_color_region ( " [ " , " ] " , Color8 ( 153 , 204 , 255 , 255 ) , false )
TextEditor . add_color_region ( ' " ' , ' " ' , Color8 ( 255 , 255 , 102 , 255 ) , false )
_ :
pass
2019-09-24 00:42:50 +02:00
2019-10-08 00:25:16 +02:00
func clean_editor ( ) :
TextEditor . set_text ( " " )
LastModifiedIcon . texture = IconLoader . load_icon_from_name ( " save " )
LastModified . set_text ( " " )
2019-09-24 00:42:50 +02:00
FileList . invalidate ( )
2019-10-11 21:53:20 +02:00
current_filename = " "
current_path = " "
2019-09-24 00:42:50 +02:00
2019-10-11 21:53:20 +02:00
func new_file_open ( file_content , last_modified , current_file_path ) :
current_path = current_file_path
current_filename = current_file_path . get_file ( )
color_region ( current_filename . get_extension ( ) )
2019-10-08 00:25:16 +02:00
TextEditor . set_text ( file_content )
update_lastmodified ( last_modified , " save " )
2019-09-24 00:42:50 +02:00
FileList . invalidate ( )
2019-10-11 21:53:20 +02:00
count_characters ( )
2019-09-24 00:42:50 +02:00
2019-10-08 00:25:16 +02:00
func update_lastmodified ( last_modified : Dictionary , icon : String ) :
2019-09-24 00:42:50 +02:00
LastModified . set_text ( str ( last_modified . hour ) + " : " + str ( last_modified . minute ) + " " + str ( last_modified . day ) + " / " + str ( last_modified . month ) + " / " + str ( last_modified . year ) )
2019-10-08 00:25:16 +02:00
LastModifiedIcon . texture = IconLoader . load_icon_from_name ( icon )
2019-09-24 00:42:50 +02:00
2019-10-08 00:25:16 +02:00
func new_file_create ( file_name ) :
TextEditor . set_text ( " " )
2019-09-24 00:42:50 +02:00
FileList . invalidate ( )
func _on_Readonly_toggled ( button_pressed ) :
if button_pressed :
ReadOnly . set_text ( " Read Only " )
TextEditor . readonly = ( true )
else :
ReadOnly . set_text ( " Can Edit " )
2019-10-03 16:06:46 +02:00
TextEditor . readonly = ( false )
func _on_TextEditor_text_changed ( ) :
LastModifiedIcon . texture = IconLoader . load_icon_from_name ( " saveas " )
2019-10-11 21:53:20 +02:00
count_characters ( )
2019-10-08 00:25:16 +02:00
emit_signal ( " text_changed " )
2019-10-11 21:53:20 +02:00
func count_characters ( ) :
var counted : int = 0
for line in TextEditor . get_line_count ( ) :
counted += TextEditor . get_line ( line ) . length ( )
c_counter . set_text ( str ( counted ) )
func _on_LineEdit_text_changed ( new_text ) :
var linecount = TextEditor . get_line_count ( )
if new_text != " " :
var found
var find = false
for line in range ( 0 , linecount ) :
for column in range ( 0 , TextEditor . get_line ( line ) . length ( ) ) :
found = TextEditor . search ( new_text , search_flag , line , column )
if found . size ( ) :
if found [ 1 ] == line :
# if not find:
TextEditor . select ( line , found [ 0 ] , found [ 1 ] , found [ 0 ] + new_text . length ( ) )
# find = true
else :
TextEditor . select ( 0 , 0 , 0 , 0 )
else :
TextEditor . select ( 0 , 0 , 0 , 0 )
func _on_matchcase_toggled ( button_pressed ) :
if button_pressed :
search_flag = 1
else :
if $ SearchBox / wholewords . is_pressed ( ) :
search_flag = 2
else :
search_flag = 0
_on_LineEdit_text_changed ( $ SearchBox / LineEdit . get_text ( ) )
func _on_wholewords_toggled ( button_pressed ) :
if button_pressed :
search_flag = 2
else :
if $ SearchBox / matchcase . is_pressed ( ) :
search_flag = 1
else :
search_flag = 0
_on_LineEdit_text_changed ( $ SearchBox / LineEdit . get_text ( ) )
func _on_close_pressed ( ) :
SearchBox . hide ( )
func open_searchbox ( ) :
if SearchBox . visible :
SearchBox . hide ( )
else :
SearchBox . show ( )
SearchBox . get_node ( " LineEdit " ) . grab_focus ( )
func _on_Button_pressed ( ) :
var linecount = TextEditor . get_line_count ( ) - 1
var old_text = $ ReplaceBox / replace . get_text ( )
var new_text = $ ReplaceBox / with . get_text ( )
var text = TextEditor . get_text ( )
TextEditor . set_text ( text . replace ( old_text , new_text ) )
func open_replacebox ( ) :
if ReplaceBox . visible :
ReplaceBox . hide ( )
else :
ReplaceBox . show ( )
ReplaceBox . get_node ( " replace " ) . grab_focus ( )
func _on_close2_pressed ( ) :
ReplaceBox . hide ( )
func _on_LineEdit_focus_entered ( ) :
_on_LineEdit_text_changed ( $ SearchBox / LineEdit . get_text ( ) )