]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Editor/Events/Fields/field_file.gd
Initial Godot project with Dialogic 2.0-Alpha-17
[wolf-seeking-sheep.git] / addons / dialogic / Editor / Events / Fields / field_file.gd
1 @tool
2 extends DialogicVisualEditorField
3
4 ## Event block field for selecting a file or directory.
5
6 #region VARIABLES
7 ################################################################################
8
9 @export var file_filter := ""
10 @export var placeholder := ""
11 @export var file_mode: EditorFileDialog.FileMode = EditorFileDialog.FILE_MODE_OPEN_FILE
12 var resource_icon: Texture:
13         get:
14                 return resource_icon
15         set(new_icon):
16                 resource_icon = new_icon
17                 %Icon.texture = new_icon
18                 if new_icon == null:
19                         %Field.theme_type_variation = ""
20                 else:
21                         %Field.theme_type_variation = "LineEditWithIcon"
22
23 var max_width := 200
24 var current_value: String
25 var hide_reset := false
26
27 #endregion
28
29
30 #region MAIN METHODS
31 ################################################################################
32
33 func _ready() -> void:
34         $FocusStyle.add_theme_stylebox_override('panel', get_theme_stylebox('focus', 'DialogicEventEdit'))
35
36         %OpenButton.icon = get_theme_icon("Folder", "EditorIcons")
37         %OpenButton.button_down.connect(_on_OpenButton_pressed)
38
39         %ClearButton.icon = get_theme_icon("Reload", "EditorIcons")
40         %ClearButton.button_up.connect(clear_path)
41         %ClearButton.visible = !hide_reset
42
43         %Field.set_drag_forwarding(Callable(), self._can_drop_data_fw, self._drop_data_fw)
44         %Field.placeholder_text = placeholder
45
46
47 func _load_display_info(info:Dictionary) -> void:
48         file_filter = info.get('file_filter', '')
49         placeholder = info.get('placeholder', '')
50         resource_icon = info.get('icon', null)
51         await ready
52
53         if resource_icon == null and info.has('editor_icon'):
54                 resource_icon = callv('get_theme_icon', info.editor_icon)
55
56
57 func _set_value(value: Variant) -> void:
58         current_value = value
59         var text: String = value
60
61         if file_mode != EditorFileDialog.FILE_MODE_OPEN_DIR:
62                 text = value.get_file()
63                 %Field.tooltip_text = value
64
65         if %Field.get_theme_font('font').get_string_size(
66                 text, 0, -1,
67                 %Field.get_theme_font_size('font_size')).x > max_width:
68                 %Field.expand_to_text_length = false
69                 %Field.custom_minimum_size.x = max_width
70                 %Field.size.x = 0
71         else:
72                 %Field.custom_minimum_size.x = 0
73                 %Field.expand_to_text_length = true
74
75         if not %Field.text == text:
76                 value_changed.emit(property_name, current_value)
77                 %Field.text = text
78
79         %ClearButton.visible = not value.is_empty() and not hide_reset
80
81
82 #endregion
83
84
85 #region BUTTONS
86 ################################################################################
87
88 func _on_OpenButton_pressed() -> void:
89         find_parent('EditorView').godot_file_dialog(_on_file_dialog_selected, file_filter, file_mode, "Open "+ property_name)
90
91
92 func _on_file_dialog_selected(path:String) -> void:
93         _set_value(path)
94         value_changed.emit(property_name, path)
95
96
97 func clear_path() -> void:
98         _set_value("")
99         value_changed.emit(property_name, "")
100
101 #endregion
102
103
104 #region DRAG AND DROP
105 ################################################################################
106
107 func _can_drop_data_fw(_at_position: Vector2, data: Variant) -> bool:
108         if typeof(data) == TYPE_DICTIONARY and data.has('files') and len(data.files) == 1:
109
110                 if file_filter:
111
112                         if '*.'+data.files[0].get_extension() in file_filter:
113                                 return true
114
115                 else: return true
116
117         return false
118
119
120 func _drop_data_fw(_at_position: Vector2, data: Variant) -> void:
121         var file: String = data.files[0]
122         _on_file_dialog_selected(file)
123
124 #endregion
125
126
127 #region VISUALS FOR FOCUS
128 ################################################################################
129
130 func _on_field_focus_entered() -> void:
131         $FocusStyle.show()
132
133
134 func _on_field_focus_exited() -> void:
135         $FocusStyle.hide()
136         var field_text: String = %Field.text
137         if current_value == field_text or (file_mode != EditorFileDialog.FILE_MODE_OPEN_DIR and current_value.get_file() == field_text):
138                 return
139         _on_file_dialog_selected(field_text)
140
141 #endregion