Skip to content

Commit

Permalink
Refactor plugin.gd for improved readability and maintainability (#2012)
Browse files Browse the repository at this point in the history
* Refactor plugin.gd for improved readability and maintainability

This commit includes several changes to improve the readability and maintainability of the plugin.gd file:

1. More descriptive comments have been added to provide better context for the code.
2. Repeated code checking the existence of `editor_view` and `editor_view.editors_manager` has been moved into a helper function `_editor_view_and_manager_exist()`.
3. String literals that were used multiple times in the code have been replaced with constants to avoid magic strings and make future changes easier.

* Update plugin.gd

Updated the requested changes

* Make some style changes

- adds code regions
- uses tabs
- makes a few things statically typed
- simplifies some methods

---------

Co-authored-by: Jowan-Spooner <[email protected]>
  • Loading branch information
Miisan-png and Jowan-Spooner authored Jan 20, 2024
1 parent 7a43111 commit d38ec68
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions addons/dialogic/plugin.gd
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
@tool
extends EditorPlugin

## Main plugin script. Handles communication with the rest of godot.
## Most methods are overridables called by godot.


## Preload the main panel scene
const MainPanel := preload("res://addons/dialogic/Editor/editor_main.tscn")
const PLUGIN_NAME := "Dialogic"
const PLUGIN_HANDLER_PATH := "res://addons/dialogic/Other/DialogicGameHandler.gd"
const PLUGIN_ICON_PATH := "res://addons/dialogic/Editor/Images/plugin-icon.svg"

### References
# used by various other scripts to quickly reference these things
## References used by various other scripts to quickly reference these things
var editor_view: Control # the root of the dialogic editor

# the root of the dialogic editor
var editor_view: Control

# emitted if godot wants us to save
## Signal emitted if godot wants us to save
signal dialogic_save


################################################################################
## INITIALIZING
################################################################################

## Initialization
func _init() -> void:
self.name = 'DialogicPlugin'
self.name = PLUGIN_NAME


################################################################################
## ACTIVATION & EDITOR SETUP
#region ACTIVATION & EDITOR SETUP
################################################################################

## Activation & Editor Setup
func _enable_plugin():
add_autoload_singleton("Dialogic", "res://addons/dialogic/Other/DialogicGameHandler.gd")
add_autoload_singleton(PLUGIN_NAME, PLUGIN_HANDLER_PATH)
add_dialogic_default_action()


func _disable_plugin():
remove_autoload_singleton("Dialogic")
remove_autoload_singleton(PLUGIN_NAME)


func _enter_tree() -> void:
Expand All @@ -52,40 +47,50 @@ func _exit_tree() -> void:
editor_view.queue_free()


#endregion


#region PLUGIN_INFO
################################################################################

func _has_main_screen() -> bool:
return true


func _get_plugin_name() -> String:
return "Dialogic"
return PLUGIN_NAME


func _get_plugin_icon():
return load("res://addons/dialogic/Editor/Images/plugin-icon.svg")
return load(PLUGIN_ICON_PATH)

#endregion

################################################################################
## EDITOR INTERACTION

#region EDITOR INTERACTION
################################################################################

## Editor Interaction
func _make_visible(visible:bool) -> void:
if editor_view:
if editor_view.get_parent() is Window:
if visible:
get_editor_interface().set_main_screen_editor("Script")
editor_view.show()
editor_view.get_parent().grab_focus()
else:
editor_view.visible = visible
if not editor_view:
return

if editor_view.get_parent() is Window:
if visible:
get_editor_interface().set_main_screen_editor("Script")
editor_view.show()
editor_view.get_parent().grab_focus()
else:
editor_view.visible = visible


func _save_external_data() -> void:
if editor_view and editor_view.editors_manager:
if _editor_view_and_manager_exist():
editor_view.editors_manager.save_current_resource()


func _handles(object) -> bool:
if editor_view != null and editor_view.editors_manager != null and object is Resource:
if _editor_view_and_manager_exist() and object is Resource:
return editor_view.editors_manager.can_edit_resource(object)
return false

Expand All @@ -94,30 +99,40 @@ func _edit(object) -> void:
if object == null:
return
_make_visible(true)
if editor_view and editor_view.editors_manager:
if _editor_view_and_manager_exist():
editor_view.editors_manager.edit_resource(object)


## Helper function to check if editor_view and its manager exist
func _editor_view_and_manager_exist() -> bool:
return editor_view and editor_view.editors_manager

################################################################################
## SPECIAL SETUP/UPDATES
#endregion


#region PROJECT SETUP
################################################################################

# methods that adds a dialogic_default_action if non exists
## Special Setup/Updates
## Methods that adds a dialogic_default_action if non exists
func add_dialogic_default_action() -> void:
if !ProjectSettings.has_setting('input/dialogic_default_action'):
var input_enter : InputEventKey = InputEventKey.new()
input_enter.keycode = KEY_ENTER
var input_left_click : InputEventMouseButton = InputEventMouseButton.new()
input_left_click.button_index = MOUSE_BUTTON_LEFT
input_left_click.pressed = true
input_left_click.device = -1
var input_space : InputEventKey = InputEventKey.new()
input_space.keycode = KEY_SPACE
var input_x : InputEventKey = InputEventKey.new()
input_x.keycode = KEY_X
var input_controller : InputEventJoypadButton = InputEventJoypadButton.new()
input_controller.button_index = JOY_BUTTON_A

ProjectSettings.set_setting('input/dialogic_default_action', {'deadzone':0.5, 'events':[input_enter, input_left_click, input_space, input_x, input_controller]})
ProjectSettings.save()
if ProjectSettings.has_setting('input/dialogic_default_action'):
return

var input_enter: InputEventKey = InputEventKey.new()
input_enter.keycode = KEY_ENTER
var input_left_click: InputEventMouseButton = InputEventMouseButton.new()
input_left_click.button_index = MOUSE_BUTTON_LEFT
input_left_click.pressed = true
input_left_click.device = -1
var input_space: InputEventKey = InputEventKey.new()
input_space.keycode = KEY_SPACE
var input_x: InputEventKey = InputEventKey.new()
input_x.keycode = KEY_X
var input_controller: InputEventJoypadButton = InputEventJoypadButton.new()
input_controller.button_index = JOY_BUTTON_A

ProjectSettings.set_setting('input/dialogic_default_action', {'deadzone':0.5, 'events':[input_enter, input_left_click, input_space, input_x, input_controller]})
ProjectSettings.save()

#endregion

0 comments on commit d38ec68

Please sign in to comment.