]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/plugin.gd
Squashed commit of the following:
[wolf-seeking-sheep.git] / addons / dialogic / plugin.gd
1 @tool
2 extends EditorPlugin
3
4 ## Preload the main panel scene
5 const MainPanel := preload("res://addons/dialogic/Editor/editor_main.tscn")
6 const PLUGIN_NAME := "Dialogic"
7 const PLUGIN_HANDLER_PATH := "res://addons/dialogic/Core/DialogicGameHandler.gd"
8 const PLUGIN_ICON_PATH := "res://addons/dialogic/Editor/Images/plugin-icon.svg"
9
10 ## References used by various other scripts to quickly reference these things
11 var editor_view: Control  # the root of the dialogic editor
12 var inspector_plugin: EditorInspectorPlugin = null
13
14 ## Initialization
15 func _init() -> void:
16         self.name = "DialogicPlugin"
17
18
19 #region ACTIVATION & EDITOR SETUP
20 ################################################################################
21
22 ## Activation & Editor Setup
23 func _enable_plugin() -> void:
24         add_autoload_singleton(PLUGIN_NAME, PLUGIN_HANDLER_PATH)
25         add_dialogic_default_action()
26
27
28 func _disable_plugin() -> void:
29         remove_autoload_singleton(PLUGIN_NAME)
30
31
32 func _enter_tree() -> void:
33         editor_view = MainPanel.instantiate()
34         editor_view.plugin_reference = self
35         editor_view.hide()
36         get_editor_interface().get_editor_main_screen().add_child(editor_view)
37         _make_visible(false)
38
39         inspector_plugin = load("res://addons/dialogic/Editor/Inspector/inspector_plugin.gd").new()
40         add_inspector_plugin(inspector_plugin)
41
42         # Auto-update the singleton path for alpha users
43         # TODO remove at some point during beta or later
44         if not ProjectSettings.has_setting("autoload/"+PLUGIN_NAME) or not "Core" in ProjectSettings.get_setting("autoload/"+PLUGIN_NAME, ""):
45                 if ProjectSettings.has_setting("autoload/"+PLUGIN_NAME):
46                         remove_autoload_singleton(PLUGIN_NAME)
47                 add_autoload_singleton(PLUGIN_NAME, PLUGIN_HANDLER_PATH)
48
49
50 func _exit_tree() -> void:
51         if editor_view:
52                 remove_control_from_bottom_panel(editor_view)
53                 editor_view.queue_free()
54
55         if inspector_plugin:
56                 remove_inspector_plugin(inspector_plugin)
57
58 #endregion
59
60
61 #region PLUGIN_INFO
62 ################################################################################
63
64 func _has_main_screen() -> bool:
65         return true
66
67
68 func _get_plugin_name() -> String:
69         return PLUGIN_NAME
70
71
72 func _get_plugin_icon() -> Texture2D:
73         return load(PLUGIN_ICON_PATH)
74
75 #endregion
76
77
78 #region EDITOR INTERACTION
79 ################################################################################
80
81 ## Editor Interaction
82 func _make_visible(visible:bool) -> void:
83         if not editor_view:
84                 return
85
86         if editor_view.get_parent() is Window:
87                 if visible:
88                         get_editor_interface().set_main_screen_editor("Script")
89                         editor_view.show()
90                         editor_view.get_parent().grab_focus()
91         else:
92                 editor_view.visible = visible
93
94
95 func _save_external_data() -> void:
96         if _editor_view_and_manager_exist():
97                 editor_view.editors_manager.save_current_resource()
98
99
100 func _get_unsaved_status(for_scene:String) -> String:
101         if for_scene.is_empty():
102                 _save_external_data()
103         return ""
104
105
106 func _handles(object) -> bool:
107         if _editor_view_and_manager_exist() and object is Resource:
108                 return editor_view.editors_manager.can_edit_resource(object)
109         return false
110
111
112 func _edit(object) -> void:
113         if object == null:
114                 return
115         _make_visible(true)
116         if _editor_view_and_manager_exist():
117                 editor_view.editors_manager.edit_resource(object)
118
119
120 ## Helper function to check if editor_view and its manager exist
121 func _editor_view_and_manager_exist() -> bool:
122         return editor_view and editor_view.editors_manager
123
124 #endregion
125
126
127 #region PROJECT SETUP
128 ################################################################################
129
130 ## Special Setup/Updates
131 ## Methods that adds a dialogic_default_action if non exists
132 func add_dialogic_default_action() -> void:
133         if ProjectSettings.has_setting('input/dialogic_default_action'):
134                 return
135
136         var input_enter: InputEventKey = InputEventKey.new()
137         input_enter.keycode = KEY_ENTER
138         var input_left_click: InputEventMouseButton = InputEventMouseButton.new()
139         input_left_click.button_index = MOUSE_BUTTON_LEFT
140         input_left_click.pressed = true
141         input_left_click.device = -1
142         var input_space: InputEventKey = InputEventKey.new()
143         input_space.keycode = KEY_SPACE
144         var input_x: InputEventKey = InputEventKey.new()
145         input_x.keycode = KEY_X
146         var input_controller: InputEventJoypadButton = InputEventJoypadButton.new()
147         input_controller.button_index = JOY_BUTTON_A
148
149         ProjectSettings.set_setting('input/dialogic_default_action', {'deadzone':0.5, 'events':[input_enter, input_left_click, input_space, input_x, input_controller]})
150         ProjectSettings.save()
151
152 # Create cache when project is compiled
153 func _build() -> bool:
154         DialogicResourceUtil.update()
155         return true
156
157 #endregion