2022-07-15 14:55:18 +02:00
2022-07-15 22:13:18 +02:00
# include "text_file_editor.h"
2022-07-15 14:55:18 +02:00
2022-08-17 13:17:12 +02:00
# include "core/variant/array.h"
2022-08-17 15:35:28 +02:00
# include "core/config/engine.h"
2022-07-16 00:23:07 +02:00
# include "core/math/math_defs.h"
# include "core/os/dir_access.h"
# include "core/os/file_access.h"
# include "core/os/keyboard.h"
# include "core/os/os.h"
# include "text_editor_preview.h"
2022-07-15 22:13:18 +02:00
# include "text_editor_settings.h"
# include "text_editor_vanilla_editor.h"
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
# include "scene/gui/box_container.h"
# include "scene/gui/dialogs.h"
# include "scene/gui/file_dialog.h"
# include "scene/gui/item_list.h"
2022-07-16 00:23:07 +02:00
# include "scene/gui/label.h"
2022-07-15 19:47:59 +02:00
# include "scene/gui/line_edit.h"
# include "scene/gui/menu_button.h"
# include "scene/gui/option_button.h"
# include "scene/gui/popup_menu.h"
2022-07-16 00:23:07 +02:00
# include "scene/gui/separator.h"
2022-07-15 19:47:59 +02:00
# include "scene/gui/split_container.h"
2022-07-16 00:23:07 +02:00
# include "scene/gui/text_edit.h"
2022-07-15 19:47:59 +02:00
# include "scene/resources/dynamic_font.h"
2022-07-15 14:55:18 +02:00
2022-07-15 22:13:18 +02:00
void TextFileEditor : : connect_signals ( ) {
2022-07-15 19:47:59 +02:00
file_list - > connect ( " confirmed " , this , " update_list " ) ;
file_btn_popup - > connect ( " id_pressed " , this , " _on_file_btn_pressed " ) ;
preview_btn_popup - > connect ( " id_pressed " , this , " _on_preview_btn_pressed " ) ;
settings_btn_popup - > connect ( " id_pressed " , this , " _on_settings_btn_pressed " ) ;
_open_file_list - > connect ( " item_selected " , this , " _on_fileitem_pressed " ) ;
wrap_btn - > connect ( " item_selected " , this , " on_wrap_button " ) ;
map_btn - > connect ( " item_selected " , this , " on_minimap_button " ) ;
select_font_dialog - > connect ( " file_selected " , this , " _on_font_selected " ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : create_selected_file ( ) {
2022-07-15 19:47:59 +02:00
update_list ( ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
file_list - > set_mode ( FileDialog : : MODE_SAVE_FILE ) ;
file_list - > set_title ( " Create a new File " ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( file_list - > is_connected ( " file_selected " , this , " delete_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " delete_file " ) ;
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( file_list - > is_connected ( " file_selected " , this , " open_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " open_file " ) ;
}
if ( ! file_list - > is_connected ( " file_selected " , this , " create_new_file " ) ) {
file_list - > connect ( " file_selected " , this , " create_new_file " ) ;
}
open_file_list ( ) ;
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : open_selected_file ( ) {
2022-07-15 19:47:59 +02:00
update_list ( ) ;
file_list - > set_mode ( FileDialog : : MODE_OPEN_FILE ) ;
file_list - > set_title ( " Select a File you want to edit " ) ;
if ( file_list - > is_connected ( " file_selected " , this , " delete_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " delete_file " ) ;
}
if ( file_list - > is_connected ( " file_selected " , this , " create_new_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " create_new_file " ) ;
}
if ( ! file_list - > is_connected ( " file_selected " , this , " open_file " ) ) {
file_list - > connect ( " file_selected " , this , " open_file " ) ;
}
open_file_list ( ) ;
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : delete_selected_file ( ) {
2022-07-15 19:47:59 +02:00
update_list ( ) ;
file_list - > set_mode ( FileDialog : : MODE_OPEN_FILES ) ;
file_list - > set_title ( " Select one or more Files you want to delete " ) ;
if ( file_list - > is_connected ( " file_selected " , this , " open_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " open_file " ) ;
}
if ( file_list - > is_connected ( " file_selected " , this , " create_new_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " create_new_file " ) ;
}
if ( ! file_list - > is_connected ( " files_selected " , this , " delete_file " ) ) {
file_list - > connect ( " files_selected " , this , " delete_file " ) ;
}
open_file_list ( ) ;
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : save_current_file_as ( ) {
2022-07-15 19:47:59 +02:00
update_list ( ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
file_list - > set_mode ( FileDialog : : MODE_SAVE_FILE ) ;
file_list - > set_title ( " Save this File as... " ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( file_list - > is_connected ( " file_selected " , this , " delete_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " delete_file " ) ;
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( file_list - > is_connected ( " file_selected " , this , " open_file " ) ) {
file_list - > disconnect ( " file_selected " , this , " open_file " ) ;
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( ! file_list - > is_connected ( " file_selected " , this , " create_new_file " ) ) {
file_list - > connect ( " file_selected " , this , " create_new_file " ) ;
}
open_file_list ( ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_file_btn_pressed ( const int index ) {
2022-07-15 19:47:59 +02:00
switch ( index ) {
case FILE_MENU_OPTION_NEW : {
create_selected_file ( ) ;
} break ;
case FILE_MENU_OPTION_OPEN : {
open_selected_file ( ) ;
} break ;
case FILE_MENU_OPTION_CLOSE : {
if ( current_file_index ! = - 1 & & current_file_path ! = " " ) {
close_file ( current_file_index ) ;
}
} break ;
case FILE_MENU_OPTION_SAVE : {
if ( current_file_index ! = - 1 & & current_file_path ! = " " ) {
save_as = false ;
save_file ( current_file_path ) ;
}
} break ;
case FILE_MENU_OPTION_SAVE_AS : {
if ( current_file_index ! = - 1 & & current_file_path ! = " " ) {
save_as = true ;
save_file ( current_file_path ) ;
save_current_file_as ( ) ;
}
} break ;
case FILE_MENU_OPTION_DELETE : {
delete_selected_file ( ) ;
} break ;
case FILE_MENU_OPTION_SEARCH : {
current_editor - > open_search_box ( ) ;
} break ;
case FILE_MENU_OPTION_REPLACE : {
current_editor - > open_replace_box ( ) ;
} break ;
default :
break ;
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_preview_btn_pressed ( const int id ) {
2022-07-15 19:47:59 +02:00
if ( id = = 0 ) {
bbcode_preview ( ) ;
} else if ( id = = 1 ) {
markdown_preview ( ) ;
} else if ( id = = 2 ) {
html_preview ( ) ;
} else if ( id = = 3 ) {
csv_preview ( ) ;
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_settings_btn_pressed ( const int index ) {
2022-07-15 19:47:59 +02:00
if ( index = = 0 ) {
select_font_dialog - > popup ( ) ;
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_font_selected ( const String & font_path ) {
2022-07-15 19:47:59 +02:00
current_editor - > set_font ( font_path ) ;
last_opened_files - > store_editor_fonts ( current_file_path . get_file ( ) , font_path ) ;
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_fileitem_pressed ( const int index ) {
2022-07-15 19:47:59 +02:00
current_file_index = index ;
2022-07-16 00:23:07 +02:00
Array selected_item_metadata = _open_file_list - > get_item_metadata ( current_file_index ) ;
Control * c = selected_item_metadata [ 0 ] ;
TextEditorVanillaEditor * e = Object : : cast_to < TextEditorVanillaEditor > ( c ) ;
String extension = e - > get_current_path ( ) . get_file ( ) . get_extension ( ) ;
2022-07-15 19:47:59 +02:00
if ( _open_file_list - > get_item_text ( current_file_index ) . begins_with ( " (*) " ) ) {
editing_file = true ;
} else {
editing_file = false ;
}
2022-07-16 00:23:07 +02:00
current_file_path = e - > get_current_path ( ) ;
2022-07-15 19:47:59 +02:00
2022-07-16 00:23:07 +02:00
if ( current_editor - > is_visible ( ) | | current_editor = = nullptr ) {
2022-07-15 19:47:59 +02:00
if ( current_editor ! = nullptr ) {
2022-07-16 00:23:07 +02:00
current_editor - > hide ( ) ;
2022-07-15 19:47:59 +02:00
}
2022-07-16 00:23:07 +02:00
current_editor = e ;
2022-07-15 19:47:59 +02:00
current_editor - > show ( ) ;
2022-07-16 00:23:07 +02:00
open_file_name - > set_text ( current_editor - > get_current_path ( ) ) ;
2022-07-15 19:47:59 +02:00
if ( wrap_btn - > get_selected_id ( ) = = 1 ) {
current_editor - > set_wrap_enabled ( true ) ;
} else {
current_editor - > set_wrap_enabled ( false ) ;
}
if ( map_btn - > get_selected_id ( ) = = 1 ) {
current_editor - > draw_minimap ( true ) ;
} else {
current_editor - > draw_minimap ( false ) ;
}
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : open_file ( const String & path , const String & font ) {
2022-07-15 19:47:59 +02:00
if ( current_file_path ! = path ) {
current_file_path = path ;
2022-07-15 22:13:18 +02:00
TextEditorVanillaEditor * vanilla_editor = open_in_vanillaeditor ( path ) ;
2022-07-15 19:47:59 +02:00
2022-07-16 00:23:07 +02:00
Ref < Font > edf = vanilla_editor - > get ( " custom_fonts/font " ) ;
//TODO this logic seems wrong
if ( font ! = " null " & & edf . is_valid ( ) ) {
2022-07-15 21:04:02 +02:00
vanilla_editor - > set_font ( font ) ;
2022-07-15 19:47:59 +02:00
}
generate_file_item ( path , vanilla_editor ) ;
2022-07-15 21:04:02 +02:00
last_opened_files - > store_opened_files ( _open_file_list ) ;
2022-07-15 19:47:59 +02:00
}
current_editor - > show ( ) ;
}
2022-07-16 00:23:07 +02:00
void TextFileEditor : : generate_file_item ( const String & path , Control * veditor ) {
2022-07-15 21:04:02 +02:00
open_file_name - > set_text ( path ) ;
_open_file_list - > add_item ( path . get_file ( ) , nullptr , true ) ;
2022-07-16 00:23:07 +02:00
current_file_index = _open_file_list - > get_item_count ( ) - 1 ;
Array arr ;
arr . push_back ( veditor ) ;
_open_file_list - > set_item_metadata ( current_file_index , arr ) ;
2022-07-15 21:04:02 +02:00
_open_file_list - > select ( _open_file_list - > get_item_count ( ) - 1 ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-16 00:23:07 +02:00
TextEditorVanillaEditor * TextFileEditor : : open_in_vanillaeditor ( const String & path ) {
TextEditorVanillaEditor * editor = memnew ( TextEditorVanillaEditor ) ;
editor - > file_list = file_list ;
split_editor_container - > add_child ( editor , true ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( current_editor & & current_editor ! = editor ) {
2022-07-16 00:23:07 +02:00
editor - > show ( ) ;
current_editor - > hide ( ) ;
2022-07-15 19:47:59 +02:00
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
current_editor = editor ;
2022-07-16 00:23:07 +02:00
editor - > connect ( " text_changed " , this , " _on_vanillaeditor_text_changed " ) ;
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
FileAccess * current_file = FileAccess : : open ( path , FileAccess : : READ ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
String current_content = " " ;
2022-07-21 19:48:08 +02:00
OS : : DateTime last_modified ;
2022-07-16 00:23:07 +02:00
2022-07-21 19:48:08 +02:00
if ( current_file ) {
current_content = current_file - > get_as_utf8_string ( ) ;
last_modified = OS : : get_singleton ( ) - > get_datetime_from_unix_time ( current_file - > get_modified_time ( path ) ) ;
current_file - > close ( ) ;
memdelete ( current_file ) ;
}
2022-07-16 00:23:07 +02:00
editor - > new_file_open ( current_content , last_modified , current_file_path ) ;
2022-07-15 19:47:59 +02:00
update_list ( ) ;
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
if ( wrap_btn - > get_selected_id ( ) = = 1 ) {
current_editor - > set_wrap_enabled ( true ) ;
2022-07-15 19:47:59 +02:00
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
return editor ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : close_file ( const int index ) {
2022-07-15 19:47:59 +02:00
if ( editing_file ) {
2022-07-16 00:23:07 +02:00
confirmation_close - > popup ( ) ;
2022-07-15 19:47:59 +02:00
} else {
confirm_close ( index ) ;
}
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : confirm_close ( const int index ) {
2022-07-16 00:23:07 +02:00
last_opened_files - > remove_opened_file ( index , _open_file_list ) ;
_open_file_list - > remove_item ( index ) ;
2022-07-15 19:47:59 +02:00
open_file_name - > clear ( ) ;
2022-07-16 00:23:07 +02:00
current_editor - > queue_delete ( ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( index > 0 ) {
2022-07-16 00:23:07 +02:00
_open_file_list - > select ( index - 1 ) ;
2022-07-15 19:47:59 +02:00
_on_fileitem_pressed ( index - 1 ) ;
}
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_update_file ( ) {
2022-07-16 00:23:07 +02:00
FileAccess * current_file = FileAccess : : open ( current_file_path , FileAccess : : READ ) ;
String current_content = current_file - > get_as_utf8_string ( ) ;
OS : : DateTime last_modified = OS : : get_singleton ( ) - > get_datetime_from_unix_time ( current_file - > get_modified_time ( current_file_path ) ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
current_file - > close ( ) ;
2022-07-16 00:23:07 +02:00
memdelete ( current_file ) ;
2022-07-15 19:47:59 +02:00
current_editor - > new_file_open ( current_content , last_modified , current_file_path ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : delete_file ( const PoolStringArray & files_selected ) {
2022-07-16 00:23:07 +02:00
for ( int i = 0 ; i < files_selected . size ( ) ; + + i ) {
String file = files_selected [ i ] ;
DirAccess : : remove_file_or_error ( file ) ;
2022-07-15 19:47:59 +02:00
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
update_list ( ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : open_new_file_dialogue ( ) {
2022-07-16 00:23:07 +02:00
new_file_dialogue - > popup ( ) ;
new_file_dialogue - > set_position ( OS : : get_singleton ( ) - > get_screen_size ( ) / 2 - new_file_dialogue - > get_size ( ) / 2 ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : open_file_list ( ) {
2022-07-15 19:47:59 +02:00
update_list ( ) ;
2022-07-16 00:23:07 +02:00
file_list - > popup ( ) ;
file_list - > set_position ( OS : : get_singleton ( ) - > get_screen_size ( ) / 2 - file_list - > get_size ( ) / 2 ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : create_new_file ( const String & given_path ) {
2022-07-16 00:23:07 +02:00
FileAccess * current_file = FileAccess : : open ( given_path , FileAccess : : WRITE ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
if ( save_as ) {
2022-07-16 00:23:07 +02:00
current_file - > store_line ( current_editor - > text_editor - > get_text ( ) ) ;
2022-07-15 19:47:59 +02:00
}
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
current_file - > close ( ) ;
memdelete ( current_file ) ;
2022-07-15 19:47:59 +02:00
open_file ( given_path ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : save_file ( const String & current_path ) {
2022-07-15 19:47:59 +02:00
//print("Saving file: ", current_path);
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
FileAccess * current_file = FileAccess : : open ( current_path , FileAccess : : WRITE ) ;
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
//String current_content = "";
//int lines = current_editor.text_editor.get_line_count();
//for (int line = 0; line < lines; ++line) { //line in range(lines)
//if current_editor.text_editor.get_line(line) == "":;
// continue;
// current_content = current_editor->text_editor->get_text();
// current_file->store_line(current_editor->text_editor->get_line(line));
//}
current_file - > store_line ( current_editor - > text_editor - > get_text ( ) ) ;
current_file - > close ( ) ;
memdelete ( current_file ) ;
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
current_file_path = current_path ;
2022-07-16 00:23:07 +02:00
OS : : DateTime last_modified = OS : : get_singleton ( ) - > get_datetime_from_unix_time ( current_file - > get_modified_time ( current_file_path ) ) ;
2022-07-15 19:47:59 +02:00
current_editor - > update_lastmodified ( last_modified , " save " ) ;
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
Array arr ;
arr . push_back ( current_editor ) ;
_open_file_list - > set_item_metadata ( current_file_index , arr ) ;
if ( _open_file_list - > get_item_text ( current_file_index ) . begins_with ( " (*) " ) ) {
_open_file_list - > set_item_text ( current_file_index , _open_file_list - > get_item_text ( current_file_index ) . lstrip ( " (*) " ) ) ;
2022-07-15 19:47:59 +02:00
editing_file = false ;
}
2022-07-15 14:55:18 +02:00
2022-07-15 19:47:59 +02:00
update_list ( ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : clean_editor ( ) {
2022-07-16 00:23:07 +02:00
List < Node * > nodes ;
get_tree ( ) - > get_nodes_in_group ( " vanilla_editor " , & nodes ) ;
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
for ( List < Node * > : : Element * e = nodes . front ( ) ; e ; e = e - > next ( ) ) {
e - > get ( ) - > queue_delete ( ) ;
2022-07-15 19:47:59 +02:00
}
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
open_file_name - > clear ( ) ;
_open_file_list - > clear ( ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : csv_preview ( ) {
TextEditorPreview * preview = memnew ( TextEditorPreview ) ;
2022-07-16 00:23:07 +02:00
get_parent ( ) - > get_parent ( ) - > get_parent ( ) - > add_child ( preview ) ;
2022-07-15 19:47:59 +02:00
preview - > popup ( ) ;
2022-07-16 00:23:07 +02:00
preview - > set_title ( " ( " + current_file_path . get_file ( ) + " ) " ) ;
2022-07-15 19:47:59 +02:00
int lines = current_editor - > text_editor - > get_line_count ( ) ;
2022-07-16 15:32:23 +02:00
Array rows ;
2022-07-15 14:55:18 +02:00
2022-07-16 15:32:23 +02:00
for ( int i = 0 ; i < lines ; + + i ) { //i in range(0, lines-1)
2022-07-15 19:47:59 +02:00
rows . append ( current_editor - > text_editor - > get_line ( i ) . rsplit ( " , " , false ) ) ;
}
2022-07-15 14:55:18 +02:00
2022-07-16 00:23:07 +02:00
preview - > print_csv ( rows ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : bbcode_preview ( ) {
TextEditorPreview * preview = memnew ( TextEditorPreview ) ;
2022-07-16 00:23:07 +02:00
get_parent ( ) - > get_parent ( ) - > get_parent ( ) - > add_child ( preview ) ;
2022-07-15 19:47:59 +02:00
preview - > popup ( ) ;
2022-07-16 00:23:07 +02:00
preview - > set_title ( " ( " + current_file_path . get_file ( ) + " ) " ) ;
preview - > print_bb ( current_editor - > text_editor - > get_text ( ) ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : markdown_preview ( ) {
TextEditorPreview * preview = memnew ( TextEditorPreview ) ;
2022-07-16 00:23:07 +02:00
get_parent ( ) - > get_parent ( ) - > get_parent ( ) - > add_child ( preview ) ;
2022-07-15 19:47:59 +02:00
preview - > popup ( ) ;
2022-07-16 00:23:07 +02:00
preview - > set_title ( " ( " + current_file_path . get_file ( ) + " ) " ) ;
preview - > print_markdown ( current_editor - > text_editor - > get_text ( ) ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : html_preview ( ) {
TextEditorPreview * preview = memnew ( TextEditorPreview ) ;
2022-07-16 00:23:07 +02:00
get_parent ( ) - > get_parent ( ) - > get_parent ( ) - > add_child ( preview ) ;
2022-07-15 19:47:59 +02:00
preview - > popup ( ) ;
2022-07-16 00:23:07 +02:00
preview - > set_title ( " ( " + current_file_path . get_file ( ) + " ) " ) ;
preview - > print_html ( current_editor - > text_editor - > get_text ( ) ) ;
2022-07-15 14:55:18 +02:00
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_vanillaeditor_text_changed ( ) {
2022-07-16 00:23:07 +02:00
if ( ! _open_file_list - > get_item_text ( current_file_index ) . begins_with ( " (*) " ) ) {
_open_file_list - > set_item_text ( current_file_index , " (*) " + _open_file_list - > get_item_text ( current_file_index ) ) ;
2022-07-15 19:47:59 +02:00
editing_file = true ;
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : update_list ( ) {
2022-07-15 19:47:59 +02:00
file_list - > invalidate ( ) ;
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : on_wrap_button ( const int index ) {
2022-07-15 19:47:59 +02:00
switch ( index ) {
case 0 : {
2022-07-16 00:23:07 +02:00
current_editor - > set_wrap_enabled ( false ) ;
2022-07-15 19:47:59 +02:00
} break ;
case 1 : {
2022-07-16 00:23:07 +02:00
current_editor - > set_wrap_enabled ( true ) ;
2022-07-15 19:47:59 +02:00
} break ;
default :
break ;
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : on_minimap_button ( const int index ) {
2022-07-15 19:47:59 +02:00
switch ( index ) {
case 0 : {
2022-07-16 00:23:07 +02:00
current_editor - > draw_minimap ( false ) ;
2022-07-15 19:47:59 +02:00
} break ;
case 1 : {
2022-07-16 00:23:07 +02:00
current_editor - > draw_minimap ( true ) ;
2022-07-15 19:47:59 +02:00
} break ;
default :
break ;
}
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : check_file_preview ( const String & file ) {
2022-07-15 19:47:59 +02:00
// check whether the opened file has a corresponding preview session for its extension;
}
2022-07-15 22:13:18 +02:00
void TextFileEditor : : _on_ConfirmationDialog_confirmed ( ) {
2022-07-15 19:47:59 +02:00
confirm_close ( current_file_index ) ;
}
2022-07-15 22:13:18 +02:00
TextFileEditor : : TextFileEditor ( ) {
2022-07-15 19:47:59 +02:00
last_opened_files . instance ( ) ;
DIRECTORY = " res:// " ;
EXCEPTIONS = " addons " ;
EXTENSIONS . push_back ( " *.txt ; Plain Text File " ) ;
EXTENSIONS . push_back ( " *.rtf ; Rich Text Format File " ) ;
EXTENSIONS . push_back ( " *.log ; Log File " ) ;
2022-07-16 00:23:07 +02:00
EXTENSIONS . push_back ( " *.md ; MD File " ) ;
EXTENSIONS . push_back ( " *.doc ; WordPad Document " ) ;
2022-07-15 19:47:59 +02:00
EXTENSIONS . push_back ( " *.doc ; Microsoft Word Document " ) ;
EXTENSIONS . push_back ( " *.docm ; Word Open XML Macro-Enabled Document " ) ;
EXTENSIONS . push_back ( " *.docx ; Microsoft Word Open XML Document " ) ;
2022-07-16 00:23:07 +02:00
EXTENSIONS . push_back ( " *.bbs ; Bulletin Board System Text " ) ;
EXTENSIONS . push_back ( " *.dat ; Data File " ) ;
EXTENSIONS . push_back ( " *.xml ; XML File " ) ;
2022-07-15 19:47:59 +02:00
EXTENSIONS . push_back ( " *.sql ; SQL database file " ) ;
EXTENSIONS . push_back ( " *.json ; JavaScript Object Notation File " ) ;
EXTENSIONS . push_back ( " *.html ; HyperText Markup Language " ) ;
EXTENSIONS . push_back ( " *.csv ; Comma-separated values " ) ;
2022-07-16 00:23:07 +02:00
EXTENSIONS . push_back ( " *.cfg ; Configuration File " ) ;
2022-07-15 19:47:59 +02:00
EXTENSIONS . push_back ( " *.ini ; Initialization File (same as .cfg Configuration File) " ) ;
EXTENSIONS . push_back ( " *.csv ; Comma-separated values File " ) ;
EXTENSIONS . push_back ( " *.res ; Resource File " ) ;
current_file_index = - 1 ;
save_as = false ;
current_editor = nullptr ;
editing_file = false ;
set_anchors_and_margins_preset ( PRESET_WIDE ) ;
2022-07-16 00:23:07 +02:00
set_v_size_flags ( SIZE_EXPAND_FILL ) ;
set_h_size_flags ( SIZE_EXPAND_FILL ) ;
2022-07-15 19:47:59 +02:00
VBoxContainer * vbc = memnew ( VBoxContainer ) ;
add_child ( vbc ) ;
vbc - > set_anchors_and_margins_preset ( PRESET_WIDE ) ;
HBoxContainer * tob_bar = memnew ( HBoxContainer ) ;
vbc - > add_child ( tob_bar ) ;
file_btn = memnew ( MenuButton ) ;
tob_bar - > add_child ( file_btn ) ;
file_btn - > set_text ( " File " ) ;
file_btn_popup = file_btn - > get_popup ( ) ;
Ref < InputEventKey > hotkey ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_N ) ;
hotkey - > set_control ( true ) ;
file_btn_popup - > add_item ( " New File " , FILE_MENU_OPTION_NEW , hotkey - > get_scancode_with_modifiers ( ) ) ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_O ) ;
hotkey - > set_control ( true ) ;
file_btn_popup - > add_item ( " Open File " , FILE_MENU_OPTION_OPEN , hotkey - > get_scancode_with_modifiers ( ) ) ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_C ) ;
hotkey - > set_control ( true ) ;
hotkey - > set_alt ( true ) ;
file_btn_popup - > add_item ( " Close File " , FILE_MENU_OPTION_CLOSE , hotkey - > get_scancode_with_modifiers ( ) ) ;
file_btn_popup - > add_separator ( ) ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_S ) ;
hotkey - > set_control ( true ) ;
file_btn_popup - > add_item ( " Save File " , FILE_MENU_OPTION_SAVE , hotkey - > get_scancode_with_modifiers ( ) ) ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_S ) ;
hotkey - > set_control ( true ) ;
hotkey - > set_alt ( true ) ;
file_btn_popup - > add_item ( " Save File as... " , FILE_MENU_OPTION_SAVE_AS , hotkey - > get_scancode_with_modifiers ( ) ) ;
//hotkey = InputEventKey.new();
//hotkey.scancode = KEY_D;
//hotkey.control = true;
//file_btn_popup.add_item("Delete File", FileMenuOptions.FILE_MENU_OPTION_DELETE, hotkey.get_scancode_with_modifiers());
file_btn_popup - > add_item ( " Delete File " , FILE_MENU_OPTION_DELETE ) ;
file_btn_popup - > add_separator ( ) ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_F ) ;
hotkey - > set_control ( true ) ;
file_btn_popup - > add_item ( " Search in file... " , FILE_MENU_OPTION_SEARCH , hotkey - > get_scancode_with_modifiers ( ) ) ;
hotkey . instance ( ) ;
hotkey - > set_scancode ( KEY_R ) ;
hotkey - > set_control ( true ) ;
file_btn_popup - > add_item ( " Replace occurencies " , FILE_MENU_OPTION_REPLACE , hotkey - > get_scancode_with_modifiers ( ) ) ;
2022-07-15 22:13:18 +02:00
//TextEditorPreview;
2022-07-15 19:47:59 +02:00
preview_btn = memnew ( MenuButton ) ;
tob_bar - > add_child ( preview_btn ) ;
2022-07-16 00:40:09 +02:00
preview_btn - > set_text ( " Preview " ) ;
2022-07-15 19:47:59 +02:00
preview_btn_popup = preview_btn - > get_popup ( ) ;
2022-07-16 00:40:09 +02:00
preview_btn_popup - > add_item ( " BBCode Preview " ) ;
preview_btn_popup - > add_item ( " Markdown Preview " ) ;
preview_btn_popup - > add_item ( " HTML Preview " ) ;
preview_btn_popup - > add_item ( " CSV Preview " ) ;
2022-07-15 19:47:59 +02:00
//Settings;
settings_btn = memnew ( MenuButton ) ;
tob_bar - > add_child ( settings_btn ) ;
settings_btn - > set_text ( " Settings " ) ;
settings_btn_popup = settings_btn - > get_popup ( ) ;
settings_btn_popup - > add_item ( " Change Font " ) ;
//SplitContainer;
editor_container = memnew ( HSplitContainer ) ;
vbc - > add_child ( editor_container ) ;
editor_container - > set_split_offset ( 150 ) ;
2022-07-16 00:23:07 +02:00
editor_container - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
editor_container - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2022-07-15 19:47:59 +02:00
//Files;
file_container = memnew ( VBoxContainer ) ;
editor_container - > add_child ( file_container ) ;
2022-07-16 00:23:07 +02:00
_open_file_list = memnew ( ItemList ) ;
file_container - > add_child ( _open_file_list ) ;
_open_file_list - > set_allow_reselect ( true ) ;
_open_file_list - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2022-07-15 19:47:59 +02:00
file_container - > add_child ( memnew ( HSeparator ) ) ;
//Editor;
split_editor_container = memnew ( VBoxContainer ) ;
editor_container - > add_child ( split_editor_container ) ;
HBoxContainer * editor_top_bar = memnew ( HBoxContainer ) ;
split_editor_container - > add_child ( editor_top_bar ) ;
Label * edtopbar_label = memnew ( Label ) ;
editor_top_bar - > add_child ( edtopbar_label ) ;
edtopbar_label - > set_text ( " Editing file: " ) ;
open_file_name = memnew ( LineEdit ) ;
editor_top_bar - > add_child ( open_file_name ) ;
open_file_name - > set_editable ( false ) ;
2022-07-16 00:23:07 +02:00
open_file_name - > set_mouse_filter ( Control : : MOUSE_FILTER_PASS ) ;
open_file_name - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
2022-07-15 19:47:59 +02:00
wrap_btn = memnew ( OptionButton ) ;
editor_top_bar - > add_child ( wrap_btn ) ;
wrap_btn - > add_item ( " No Wrap " ) ;
wrap_btn - > add_item ( " Soft Wrap " ) ;
map_btn = memnew ( OptionButton ) ;
editor_top_bar - > add_child ( map_btn ) ;
map_btn - > add_item ( " Hide Map " ) ;
map_btn - > add_item ( " Show Map " ) ;
2022-07-16 00:23:07 +02:00
map_btn - > select ( 1 ) ;
2022-07-15 19:47:59 +02:00
//dialogs;
file_list = memnew ( FileDialog ) ;
add_child ( file_list ) ;
file_list - > set_show_hidden_files ( true ) ;
2022-07-16 00:23:07 +02:00
file_list - > set_hide_on_ok ( true ) ;
file_list - > set_title ( " Save file " ) ;
file_list - > set_exclusive ( true ) ;
file_list - > set_anchors_and_margins_preset ( PRESET_WIDE ) ;
file_list - > set_margin ( MARGIN_LEFT , 222 ) ;
file_list - > set_margin ( MARGIN_TOP , 132 ) ;
file_list - > set_margin ( MARGIN_RIGHT , - 221 ) ;
file_list - > set_margin ( MARGIN_BOTTOM , - 131 ) ;
file_list - > set_custom_minimum_size ( Vector2 ( 200 , 70 ) ) ;
2022-07-15 19:47:59 +02:00
new_file_dialogue = memnew ( AcceptDialog ) ;
add_child ( new_file_dialogue ) ;
2022-07-16 00:23:07 +02:00
new_file_dialogue - > set_title ( " Create new File " ) ;
2022-07-15 19:47:59 +02:00
VBoxContainer * nfd_vbc = memnew ( VBoxContainer ) ;
new_file_dialogue - > add_child ( nfd_vbc ) ;
Label * nfd_name = memnew ( Label ) ;
nfd_vbc - > add_child ( nfd_name ) ;
nfd_name - > set_text ( " Insert file name (no extension needed) " ) ;
nfd_name - > set_align ( Label : : ALIGN_CENTER ) ;
nfd_name - > set_valign ( Label : : VALIGN_CENTER ) ;
2022-07-16 00:23:07 +02:00
nfd_name - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2022-07-15 19:47:59 +02:00
new_file_dialogue_name = memnew ( LineEdit ) ;
nfd_vbc - > add_child ( new_file_dialogue_name ) ;
new_file_dialogue_name - > set_clear_button_enabled ( true ) ;
new_file_dialogue_name - > set_text ( " example " ) ;
2022-07-16 00:23:07 +02:00
new_file_dialogue_name - > set_custom_minimum_size ( Vector2 ( 200 , 0 ) ) ;
new_file_dialogue_name - > set_h_size_flags ( SIZE_EXPAND | SIZE_SHRINK_CENTER ) ;
new_file_dialogue_name - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2022-07-15 19:47:59 +02:00
confirmation_close = memnew ( ConfirmationDialog ) ;
add_child ( confirmation_close ) ;
2022-07-16 00:23:07 +02:00
confirmation_close - > set_text ( " There are some unsaved changes. \n Press \" OK \" if you want to close this tab anyway, or \" cancel \" if you want to keep on editing your file. " ) ;
confirmation_close - > set_title ( " Unsaved changes " ) ;
2022-07-15 19:47:59 +02:00
confirmation_close - > set_anchors_and_margins_preset ( PRESET_CENTER ) ;
select_font_dialog = memnew ( FileDialog ) ;
add_child ( select_font_dialog ) ;
select_font_dialog - > set_mode ( FileDialog : : MODE_OPEN_FILE ) ;
select_font_dialog - > set_access ( FileDialog : : ACCESS_FILESYSTEM ) ;
select_font_dialog - > set_show_hidden_files ( true ) ;
2022-07-16 00:23:07 +02:00
select_font_dialog - > set_title ( " Open a File " ) ;
2022-07-15 19:47:59 +02:00
select_font_dialog - > set_resizable ( true ) ;
select_font_dialog - > set_anchors_and_margins_preset ( PRESET_WIDE ) ;
2022-07-16 00:23:07 +02:00
select_font_dialog - > set_margin ( MARGIN_LEFT , 222 ) ;
select_font_dialog - > set_margin ( MARGIN_TOP , 132 ) ;
select_font_dialog - > set_margin ( MARGIN_RIGHT , - 221 ) ;
select_font_dialog - > set_margin ( MARGIN_BOTTOM , - 131 ) ;
select_font_dialog - > set_custom_minimum_size ( Vector2 ( 200 , 70 ) ) ;
2022-07-15 19:47:59 +02:00
2022-07-16 00:23:07 +02:00
Vector < String > farr ;
2022-07-15 19:47:59 +02:00
farr . push_back ( " *.TTF " ) ;
farr . push_back ( " *.ttf " ) ;
select_font_dialog - > set_filters ( farr ) ;
}
2022-07-15 22:13:18 +02:00
TextFileEditor : : ~ TextFileEditor ( ) {
2022-07-15 19:47:59 +02:00
}
2022-07-16 00:36:35 +02:00
void TextFileEditor : : _notification ( int p_what ) {
if ( p_what = = NOTIFICATION_POST_ENTER_TREE ) {
if ( ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
return ;
}
clean_editor ( ) ;
connect_signals ( ) ;
Array opened_files = last_opened_files - > load_opened_files ( ) ;
for ( int i = 0 ; i < opened_files . size ( ) ; + + i ) {
Array opened_file = opened_files [ i ] ;
open_file ( opened_file [ 1 ] , opened_file [ 2 ] ) ;
}
2022-07-15 19:47:59 +02:00
2022-07-16 00:36:35 +02:00
file_list - > set_filters ( EXTENSIONS ) ;
}
}
void TextFileEditor : : _bind_methods ( ) {
2022-07-16 00:23:07 +02:00
//ClassDB::bind_method(D_METHOD("connect_signals"), &TextFileEditor::connect_signals);
//ClassDB::bind_method(D_METHOD("create_selected_file"), &TextFileEditor::create_selected_file);
//ClassDB::bind_method(D_METHOD("open_selected_file"), &TextFileEditor::open_selected_file);
// ClassDB::bind_method(D_METHOD("delete_selected_file"), &TextFileEditor::delete_selected_file);
//ClassDB::bind_method(D_METHOD("save_current_file_as"), &TextFileEditor::save_current_file_as);
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " _on_file_btn_pressed " , " index " ) , & TextFileEditor : : _on_file_btn_pressed ) ;
ClassDB : : bind_method ( D_METHOD ( " _on_preview_btn_pressed " , " id " ) , & TextFileEditor : : _on_preview_btn_pressed ) ;
ClassDB : : bind_method ( D_METHOD ( " _on_settings_btn_pressed " , " index " ) , & TextFileEditor : : _on_settings_btn_pressed ) ;
ClassDB : : bind_method ( D_METHOD ( " _on_font_selected " , " font_path " ) , & TextFileEditor : : _on_font_selected ) ;
ClassDB : : bind_method ( D_METHOD ( " _on_fileitem_pressed " , " index " ) , & TextFileEditor : : _on_fileitem_pressed ) ;
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " open_file " , " path " , " font " ) , & TextFileEditor : : open_file , " null " ) ;
2022-07-16 00:23:07 +02:00
//ClassDB::bind_method(D_METHOD("generate_file_item", "path", "veditor"), &TextFileEditor::generate_file_item);
//ClassDB::bind_method(D_METHOD("open_in_vanillaeditor", "path"), &TextFileEditor::open_in_vanillaeditor);
2022-07-15 19:47:59 +02:00
2022-07-16 00:23:07 +02:00
//ClassDB::bind_method(D_METHOD("close_file", "index"), &TextFileEditor::close_file);
//ClassDB::bind_method(D_METHOD("confirm_close", "index"), &TextFileEditor::confirm_close);
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " _on_update_file " ) , & TextFileEditor : : _on_update_file ) ;
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " delete_file " , " files_selected " ) , & TextFileEditor : : delete_file ) ;
2022-07-16 00:23:07 +02:00
//ClassDB::bind_method(D_METHOD("open_new_file_dialogue"), &TextFileEditor::open_new_file_dialogue);
//ClassDB::bind_method(D_METHOD("open_file_list"), &TextFileEditor::open_file_list);
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " create_new_file " , " given_path " ) , & TextFileEditor : : create_new_file ) ;
2022-07-16 00:23:07 +02:00
//ClassDB::bind_method(D_METHOD("save_file", "current_path"), &TextFileEditor::save_file);
2022-07-15 19:47:59 +02:00
2022-07-16 00:23:07 +02:00
//ClassDB::bind_method(D_METHOD("clean_editor"), &TextFileEditor::clean_editor);
//ClassDB::bind_method(D_METHOD("csv_preview"), &TextFileEditor::csv_preview);
//ClassDB::bind_method(D_METHOD("bbcode_preview"), &TextFileEditor::bbcode_preview);
//ClassDB::bind_method(D_METHOD("markdown_preview"), &TextFileEditor::markdown_preview);
//ClassDB::bind_method(D_METHOD("html_preview"), &TextFileEditor::html_preview);
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " _on_vanillaeditor_text_changed " ) , & TextFileEditor : : _on_vanillaeditor_text_changed ) ;
ClassDB : : bind_method ( D_METHOD ( " update_list " ) , & TextFileEditor : : update_list ) ;
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " on_wrap_button " , " index " ) , & TextFileEditor : : on_wrap_button ) ;
ClassDB : : bind_method ( D_METHOD ( " on_minimap_button " , " index " ) , & TextFileEditor : : on_minimap_button ) ;
ClassDB : : bind_method ( D_METHOD ( " check_file_preview " , " file " ) , & TextFileEditor : : check_file_preview ) ;
2022-07-15 19:47:59 +02:00
2022-07-15 22:13:18 +02:00
ClassDB : : bind_method ( D_METHOD ( " _on_ConfirmationDialog_confirmed " ) , & TextFileEditor : : _on_ConfirmationDialog_confirmed ) ;
2022-07-15 14:55:18 +02:00
}