Skip to content

Commit

Permalink
Merge pull request #32 from willnationsdev/cleanup
Browse files Browse the repository at this point in the history
Apply GDScript formatting. Use static type hints. Use EditorInterface singleton.
  • Loading branch information
willnationsdev authored Dec 9, 2024
2 parents b8cdac0 + 012e976 commit 09452b7
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 120 deletions.
53 changes: 29 additions & 24 deletions addons/godot-plugin-refresher/plugin_refresher.gd
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
@tool
extends HBoxContainer

signal request_refresh_plugin(p_name)
signal confirm_refresh_plugin(p_name)
signal request_refresh_plugin(p_name: String)
signal confirm_refresh_plugin(p_name: String)

@onready var options = $OptionButton
@onready var options: OptionButton = $OptionButton

func _ready():

func _ready() -> void:
if get_tree().edited_scene_root == self:
return # This is the scene opened in the editor!
return # This is the scene opened in the editor!
$RefreshButton.icon = EditorInterface.get_editor_theme().get_icon("Reload", "EditorIcons")


func update_items(p_plugins):
func update_items(p_plugins: Dictionary) -> void:
if not options:
return
options.clear()
var plugin_dirs = p_plugins.keys()
var plugin_dirs: Array[String] = []
plugin_dirs.assign(p_plugins.keys())
for idx in plugin_dirs.size():
var plugin_dirname = plugin_dirs[idx]
var plugin_name = p_plugins[plugin_dirname]
var plugin_dirname := plugin_dirs[idx] as String
var plugin_name := p_plugins[plugin_dirname] as String
options.add_item(plugin_name, idx)
options.set_item_metadata(idx, plugin_dirname)


func select_plugin(p_name):
if not options:
return
if p_name == null or p_name.is_empty():
# Note: For whatever reason, statically typing `p_name` inexplicably causes
# an error about converting from Nil to String, even if the value is converted.
func select_plugin(p_name) -> void:
if not options or not p_name:
return

for idx in options.get_item_count():
var plugin = options.get_item_metadata(idx)
if plugin == p_name:
var plugin := str(options.get_item_metadata(idx))
if plugin == str(p_name):
options.selected = options.get_item_id(idx)
break


func _on_RefreshButton_pressed():
func _on_RefreshButton_pressed() -> void:
if options.selected == -1:
return # nothing selected
return # nothing selected

var plugin = options.get_item_metadata(options.selected)
if not plugin or plugin.is_empty():
var plugin := str(options.get_item_metadata(options.selected))
if not plugin:
return
emit_signal("request_refresh_plugin", plugin)


func show_warning(p_name):
$ConfirmationDialog.dialog_text = """
func show_warning(p_name: String) -> void:
$ConfirmationDialog.dialog_text = (
"""
Plugin `%s` is currently disabled.\n
Do you want to enable it now?
""" % [p_name]
"""
% [p_name]
)
$ConfirmationDialog.popup_centered()


func _on_ConfirmationDialog_confirmed():
var plugin = options.get_item_metadata(options.selected)
func _on_ConfirmationDialog_confirmed() -> void:
var plugin := options.get_item_metadata(options.selected) as String
emit_signal("confirm_refresh_plugin", plugin)
96 changes: 50 additions & 46 deletions addons/godot-plugin-refresher/plugin_refresher_plugin.gd
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
@tool
extends EditorPlugin

const ADDONS_PATH = "res://addons/"
const PLUGIN_CONFIG_DIR = "plugins/plugin_refresher"
const PLUGIN_CONFIG = "settings.cfg"
const SETTINGS = "settings"
const SETTING_RECENT = "recently_used"
const ADDONS_PATH := "res://addons/"
const PLUGIN_CONFIG_DIR := "plugins/plugin_refresher"
const PLUGIN_CONFIG := "settings.cfg"
const SETTINGS := "settings"
const SETTING_RECENT := "recently_used"
const Refresher := preload("plugin_refresher.gd")

var plugin_config = ConfigFile.new()
var refresher
var plugin_config := ConfigFile.new()
var refresher: Refresher = null

func _enter_tree():
refresher = preload("plugin_refresher.tscn").instantiate()

func _enter_tree() -> void:
refresher = preload("plugin_refresher.tscn").instantiate() as Refresher
add_control_to_container(CONTAINER_TOOLBAR, refresher)

# Watch whether any plugin is changed, added or removed on the filesystem
var efs = get_editor_interface().get_resource_filesystem()
var efs := EditorInterface.get_resource_filesystem()
efs.filesystem_changed.connect(_on_filesystem_changed)

refresher.request_refresh_plugin.connect(_on_request_refresh_plugin)
Expand All @@ -25,31 +27,30 @@ func _enter_tree():
_load_settings()


func _exit_tree():
func _exit_tree() -> void:
remove_control_from_container(CONTAINER_TOOLBAR, refresher)
refresher.free()


func _reload_plugins_list():
var refresher_dir = get_plugin_path().get_file()
var plugins = {}
var origins = {}
func _reload_plugins_list() -> void:
var refresher_dir := get_plugin_path().get_file()
var plugins := {}
var origins := {}

var dir = DirAccess.open(ADDONS_PATH)
var dir := DirAccess.open(ADDONS_PATH)
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
var file_name := dir.get_next()
while file_name != "":

var addon_dir = ADDONS_PATH.path_join(file_name)
var addon_dir := ADDONS_PATH.path_join(file_name)
if dir.dir_exists(addon_dir) and file_name != refresher_dir:
var display_name = file_name
var plugin_config_path = addon_dir.path_join("plugin.cfg")
var display_name := file_name

var plugin_config_path := addon_dir.path_join("plugin.cfg")
if not dir.file_exists(plugin_config_path):
file_name = dir.get_next()
continue # not a plugin
var plugin_cfg = ConfigFile.new()
continue # not a plugin
var plugin_cfg := ConfigFile.new()
plugin_cfg.load(plugin_config_path)
display_name = plugin_cfg.get_value("plugin", "name", file_name)
if not display_name in origins:
Expand All @@ -68,70 +69,73 @@ func _reload_plugins_list():

refresher.update_items(plugins)

func _load_settings():
var path = get_config_path()

func _load_settings() -> void:
var path := get_config_path()

if not FileAccess.file_exists(path):
# Create new if running for the first time
var config = ConfigFile.new()
var config := ConfigFile.new()
DirAccess.make_dir_recursive_absolute(path.get_base_dir())
config.save(path)
else:
plugin_config.load(path)

func _save_settings():

func _save_settings() -> void:
plugin_config.save(get_config_path())


func get_config_path() -> String:
var editor_paths = EditorInterface.get_editor_paths()
var dir = editor_paths.get_project_settings_dir()
var home = dir.path_join(PLUGIN_CONFIG_DIR)
var path = home.path_join(PLUGIN_CONFIG)
var editor_paths := EditorInterface.get_editor_paths()
var dir := editor_paths.get_project_settings_dir()

var home := dir.path_join(PLUGIN_CONFIG_DIR)
var path := home.path_join(PLUGIN_CONFIG)

return path

func _on_filesystem_changed():

func _on_filesystem_changed() -> void:
if refresher:
_reload_plugins_list()
refresher.select_plugin(get_recent_plugin())


func get_recent_plugin():
func get_recent_plugin() -> String:
if not plugin_config.has_section_key(SETTINGS, SETTING_RECENT):
return null # not saved yet
return "" # not saved yet

var recent = plugin_config.get_value(SETTINGS, SETTING_RECENT)
return recent
return recent if recent != null and not recent.is_empty() else ""


func _on_request_refresh_plugin(p_name):
func _on_request_refresh_plugin(p_name: String) -> void:
assert(not p_name.is_empty())

var disabled = not get_editor_interface().is_plugin_enabled(p_name)
var disabled := not EditorInterface.is_plugin_enabled(p_name)
if disabled:
refresher.show_warning(p_name)
else:
refresh_plugin(p_name)


func _on_confirm_refresh_plugin(p_name):
func _on_confirm_refresh_plugin(p_name: String) -> void:
refresh_plugin(p_name)


func get_plugin_path():
func get_plugin_path() -> String:
return get_script().resource_path.get_base_dir()


func refresh_plugin(p_name):
func refresh_plugin(p_name: String) -> void:
print("Refreshing plugin: ", p_name)

var enabled = get_editor_interface().is_plugin_enabled(p_name)
if enabled: # can only disable an active plugin
get_editor_interface().set_plugin_enabled(p_name, false)
var enabled := EditorInterface.is_plugin_enabled(p_name)
if enabled: # can only disable an active plugin
EditorInterface.set_plugin_enabled(p_name, false)

get_editor_interface().set_plugin_enabled(p_name, true)
EditorInterface.set_plugin_enabled(p_name, true)

plugin_config.set_value(SETTINGS, SETTING_RECENT, p_name)
_save_settings()
32 changes: 16 additions & 16 deletions icon.png.import
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
type="CompressedTexture2D"
uid="uid://c1hfjj7vmjkda"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/hdr_compression=1
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
Loading

0 comments on commit 09452b7

Please sign in to comment.