Skip to content

Commit

Permalink
refs #264: Update scripts created for game GUI.
Browse files Browse the repository at this point in the history
Now the original script attached to a component is copied to the game
folder, and a *_custom.gd is created so that devs can easily modify the
default behavior.

- **upd** Now the `graphic_interface` folder inside the plugin's folder is
  named `gui`.
  • Loading branch information
mapedorr committed Sep 1, 2024
1 parent 61fc76a commit 9868cde
Show file tree
Hide file tree
Showing 193 changed files with 408 additions and 394 deletions.
60 changes: 37 additions & 23 deletions addons/popochiu/editor/helpers/popochiu_gui_templates_helper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,29 @@ static func copy_gui_template(

# ---- Make a copy of the selected GUI template ------------------------------------------------
if _create_scene(scene_path) != OK:
# TODO: Delete the graphic_interface folder and all its contents?
PopochiuUtils.print_error("Couldn't create %s file" % PopochiuResources.GUI_GAME_SCENE)
return

await _wait(2.0)
on_progress.call(10, "Copying a bunch of components")

# Copy the components used by the GUI template to the res://game/graphic_interface/components
# Copy the components used by the GUI template to the res://game/gui/components
# folder so devs can play with them freely -----------------------------------------------------
await copy_components(scene_path, true)

on_progress.call(60, "Creating scripts")

# Create a copy of the corresponding commands template -----------------------------------------
_copy_scripts(commands_template_path, PopochiuResources.GUI_COMMANDS, script_path, scene_path)
_copy_commands_and_gui_scripts(
commands_template_path, PopochiuResources.GUI_COMMANDS, script_path, scene_path
)

await _wait(1.5)
on_progress.call(80, "Assigning scripts")

# Update the script of the created graphic_interface.tscn so it uses the copy created above ----
# Update the script of the created gui.tscn so it uses the copy created above ------------------
if _update_scene_script(script_path) != OK:
PopochiuUtils.print_error("Couldn't update graphic_interface.tscn script")
PopochiuUtils.print_error("Couldn't update gui.tscn script")
return

await _wait()
Expand Down Expand Up @@ -121,7 +122,7 @@ static func copy_component(source_scene_path: String) -> String:


## Makes a copy of the components used by the original GUI template to the
## **res://game/graphic_interface/components/** folder so devs can play with those scenes without
## **res://game/gui/components/** folder so devs can play with those scenes without
## affecting the ones in the plugin's folder.
static func copy_components(source_scene_path: String, is_gui_game_scene := false) -> void:
var dependencies_to_update: Array[Dictionary] = []
Expand Down Expand Up @@ -161,7 +162,7 @@ static func copy_components(source_scene_path: String, is_gui_game_scene := fals
if is_gui_game_scene and _template_theme_path.is_empty():
# Change the name of the GUI template theme so it differs from the original one
dependency_data.target_path = "%s/%s" % [
target_folder, "graphic_interface_theme.tres"
target_folder, "gui_theme.tres"
]
_template_theme_path = dependency_data.target_path
elif not is_gui_game_scene:
Expand Down Expand Up @@ -205,23 +206,23 @@ static func copy_components(source_scene_path: String, is_gui_game_scene := fals
#endregion

#region Private ####################################################################################
## Create the **graphic_interface.tscn** file as a copy of the selected GUI template scene.
## Create the **gui.tscn** file as a copy of the selected GUI template scene.
## If a template change is being made, all components of the previous template are removed along
## with the **.tscn** file before copying the new one.
static func _create_scene(scene_path: String) -> int:
# Create the res://game/graphic_interface/ folder
# Create the res://game/gui/ folder
if not FileAccess.file_exists(PopochiuResources.GUI_GAME_SCENE):
DirAccess.make_dir_recursive_absolute(PopochiuResources.GUI_GAME_FOLDER)
else:
# Remove the graphic_interface.tscn file
# Remove the gui.tscn file
DirAccess.remove_absolute(PopochiuResources.GUI_GAME_SCENE)
EditorInterface.get_resource_filesystem().scan()

for dir_name: String in DirAccess.get_directories_at(PopochiuResources.GUI_GAME_FOLDER):
_remove_components(PopochiuResources.GUI_GAME_FOLDER + dir_name)

# Make a copy of the selected GUI template (.tscn) and save it in
# res://game/graphic_interface/graphic_interface.tscn ------------------------------------------
# res://game/gui/gui.tscn ------------------------------------------
var gi_scene := load(scene_path).duplicate(true)
gi_scene.resource_path = PopochiuResources.GUI_GAME_SCENE

Expand All @@ -246,19 +247,29 @@ static func _remove_components(dir_path: String) -> void:
static func _copy_script(
source_file_path: String, _target_folder: String, target_file_path: String
) -> void:
var file_write = FileAccess.open(target_file_path, FileAccess.WRITE)
# Make a copy of the original script -----------------------------------------------------------
var source_file := FileAccess.open(source_file_path, FileAccess.READ)
var source_code := source_file.get_as_text()
source_file.close()

if load(source_file_path).is_tool():
file_write.store_string("@tool\n")
if "class_name " in source_code:
source_code = source_code.replace("class_name ", "class_name Custom")

file_write.store_string('extends "%s"' % source_file_path)
var file_write := FileAccess.open(target_file_path, FileAccess.WRITE)
file_write.store_string(source_code)
file_write.close()

# Create a script for devs to overwrite the functionality of the original's copy script --------
file_write = FileAccess.open(target_file_path.replace(".gd", "_custom.gd"), FileAccess.WRITE)
if "@tool" in source_code:
file_write.store_string("@tool\n")
file_write.store_string('extends "%s"' % target_file_path.get_file())


static func _copy_file(
source_file_path: String, target_folder: String, target_file_path: String
) -> void:
# ---- Create a copy of the scene file -----------------------------------------------------
# ---- Create a copy of the scene file ---------------------------------------------------------
if source_file_path.get_extension() in ["tscn", "tres"]:
var file_resource := load(source_file_path).duplicate(true)
file_resource.resource_path = target_file_path
Expand All @@ -280,11 +291,14 @@ static func _update_dependencies(scene_path: String, dependencies_to_update: Arr
file_read.close()

for dic: Dictionary in dependencies_to_update:
text = text.replace(dic.source_path, dic.target_path)
var target_path: String = dic.target_path

var target_uid := ResourceUID.id_to_text(
ResourceLoader.get_resource_uid(dic.target_path)
)
if ".gd" in target_path:
target_path = target_path.replace(".gd", "_custom.gd")

text = text.replace(dic.source_path, target_path)

var target_uid := ResourceUID.id_to_text(ResourceLoader.get_resource_uid(target_path))

if "invalid" in target_uid: continue

Expand All @@ -298,14 +312,14 @@ static func _update_dependencies(scene_path: String, dependencies_to_update: Arr
## Copy the commands and graphic interface scripts of the chosen GUI template. The new graphic
## interface scripts inherits from the one originally assigned to the .tscn file of the selected
## template.
static func _copy_scripts(
static func _copy_commands_and_gui_scripts(
commands_template_path: String, commands_path: String, script_path: String, scene_path: String
) -> void:
DirAccess.copy_absolute(commands_template_path, commands_path)

# Create a copy of the graphic interface script template ---------------------------------------
var template_path := (
PopochiuResources.GUI_SCRIPT_TEMPLATES_FOLDER + "graphic_interface_template.gd"
PopochiuResources.GUI_SCRIPT_TEMPLATES_FOLDER + "gui_template.gd"
)
var script_file := FileAccess.open(template_path, FileAccess.READ)
var source_code := script_file.get_as_text()
Expand All @@ -320,7 +334,7 @@ static func _copy_scripts(


## Updates the script of the created [b]res://game/gui/gui.tscn[/b] file so it uses the one created
## in [method _copy_scripts].
## in [method _copy_commands_and_gui_scripts].
static func _update_scene_script(script_path: String) -> int:
# Update the script of the GUI -----------------------------------------------------------------
var scene := (load(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
extends AcceptDialog

const POPOCHIU_POPUP_SCENE :=\
"res://addons/popochiu/engine/objects/graphic_interface/components/popups/popochiu_popup.tscn"
"res://addons/popochiu/engine/objects/gui/components/popups/popochiu_popup.tscn"
const POPOCHIU_POPUP_SCRIPT :=\
"res://addons/popochiu/engine/templates/graphic_interface/popup_template.gd"
const POPUPS_FOLDER := "res://game/graphic_interface/popups/%s/"
"res://addons/popochiu/engine/templates/gui/popup_template.gd"
const POPUPS_FOLDER := "res://game/gui/popups/%s/"

var _popup_id := ""
var _scene_path := POPUPS_FOLDER + "%s.tscn"
Expand Down Expand Up @@ -75,10 +75,10 @@ func _create_popup() -> void:

# Add the popup to the Popups node in the graphic interface scene ------------------------------
var popup_node: PopochiuPopup = load(scene_path).instantiate()
var graphic_interface_node := EditorInterface.get_edited_scene_root()
var gui_node := EditorInterface.get_edited_scene_root()

graphic_interface_node.get_node("Popups").add_child(popup_node)
popup_node.owner = graphic_interface_node
gui_node.get_node("Popups").add_child(popup_node)
popup_node.owner = gui_node
EditorInterface.save_scene()

# Open the scene of the created popup for edition ----------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions addons/popochiu/editor/main_dock/tab_gui/tab_gui.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@tool
extends VBoxContainer

const COMPONENTS_PATH := "res://addons/popochiu/engine/objects/graphic_interface/components/"
const COMPONENTS_PATH := "res://addons/popochiu/engine/objects/gui/components/"

var _opened_scene: Control = null
var _components_basedir := []
var _script_path := PopochiuResources.GUI_GAME_SCENE.replace(".tscn", ".gd")
var _commands_path := PopochiuResources.GUI_GAME_SCENE.replace(
"graphic_interface.tscn", "commands.gd"
"gui.tscn", "gui_commands.gd"
)
var _gui_templates_helper := preload(
"res://addons/popochiu/editor/helpers/popochiu_gui_templates_helper.gd"
Expand Down
6 changes: 3 additions & 3 deletions addons/popochiu/editor/popups/setup/setup.gd
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ func _show_gui_warning() -> void:
_setup_inner_dialog(
warning_dialog,
"GUI template warning",
"The GUI scene (graphic_interface) is currently opened in the Editor.\n\nIn order to change\
the GUI template please close that scene first."
"The GUI scene (gui.tscn) is currently opened in the Editor.\n\n" +\
"In order to change the GUI template please close that scene first."
)

add_child(warning_dialog)
Expand All @@ -228,7 +228,7 @@ func _show_template_change_confirmation() -> void:
confirmation_dialog,
"Confirm GUI template change",
"You changed the GUI template, making this will override any changes you made to the files\
in res://game/graphic_interface/.\n\nAre you sure you want to make the change?"
in res://game/gui/.\n\nAre you sure you want to make the change?"
)

confirmation_dialog.confirmed.connect(
Expand Down
2 changes: 1 addition & 1 deletion addons/popochiu/engine/cursor/cursor_animations.tres
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[gd_resource type="SpriteFrames" load_steps=22 format=3 uid="uid://d0ny3mket72mf"]

[ext_resource type="Texture2D" uid="uid://bl3ecai6lvat1" path="res://addons/popochiu/engine/cursor/sprites/cursor.png" id="1_qk45r"]
[ext_resource type="Texture2D" uid="uid://bocign602kxhh" path="res://addons/popochiu/engine/objects/graphic_interface/templates/simple_click/images/simple_click_cursor.png" id="2_05hav"]
[ext_resource type="Texture2D" uid="uid://bocign602kxhh" path="res://addons/popochiu/engine/objects/gui/templates/simple_click/images/simple_click_cursor.png" id="2_05hav"]

[sub_resource type="AtlasTexture" id="10"]
atlas = ExtResource("1_qk45r")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://dhsfl8ot4j5fj"]

[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_menu/dialog_menu.gd" id="1_3dp72"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/graphic_interface/resources/base_gui_theme.tres" id="1_cvsnu"]
[ext_resource type="PackedScene" uid="uid://dcta4urojglil" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_menu/dialog_menu_option/dialog_menu_option.tscn" id="3_0jfsm"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/dialog_menu/dialog_menu.gd" id="1_3dp72"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="1_cvsnu"]
[ext_resource type="PackedScene" uid="uid://dcta4urojglil" path="res://addons/popochiu/engine/objects/gui/components/dialog_menu/dialog_menu_option/dialog_menu_option.tscn" id="3_0jfsm"]

[node name="DialogMenu" type="Control" groups=["popochiu_gui_component"]]
layout_mode = 3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=6 format=3 uid="uid://dcta4urojglil"]

[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/graphic_interface/resources/base_gui_theme.tres" id="1_y0k4l"]
[ext_resource type="FontFile" uid="uid://dixh1egf7k2fb" path="res://addons/popochiu/engine/objects/graphic_interface/fonts/monkeyisland_1991.ttf" id="2_5iw2f"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_menu/dialog_menu_option/dialog_menu_option.gd" id="2_nywcv"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="1_y0k4l"]
[ext_resource type="FontFile" uid="uid://dixh1egf7k2fb" path="res://addons/popochiu/engine/objects/gui/fonts/monkeyisland_1991.ttf" id="2_5iw2f"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/dialog_menu/dialog_menu_option/dialog_menu_option.gd" id="2_nywcv"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_un1pr"]
content_margin_left = 2.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://bpl3qjbxonfpb"]

[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/dialog_caption/dialog_caption.gd" id="1_17s2p"]
[ext_resource type="FontFile" uid="uid://dixh1egf7k2fb" path="res://addons/popochiu/engine/objects/graphic_interface/fonts/monkeyisland_1991.ttf" id="1_hkh5x"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/images/ico_continue.png" id="3_etbl2"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd" id="1_17s2p"]
[ext_resource type="FontFile" uid="uid://dixh1egf7k2fb" path="res://addons/popochiu/engine/objects/gui/fonts/monkeyisland_1991.ttf" id="1_hkh5x"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/images/ico_continue.png" id="3_etbl2"]

[node name="DialogCaption" type="Control" groups=["popochiu_gui_component"]]
layout_mode = 3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://bn7o13nv11ka1"]

[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/dialog_overhead/dialog_overhead.gd" id="1_a5mdx"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/graphic_interface/resources/base_gui_theme.tres" id="2_t336a"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/images/ico_continue.png" id="3_67j10"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd" id="1_a5mdx"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="2_t336a"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/images/ico_continue.png" id="3_67j10"]

[node name="DialogOverhead" type="Control" groups=["popochiu_gui_component"]]
layout_mode = 3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=5 format=3 uid="uid://33wmak2jumqm"]

[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/dialog_portrait/dialog_portrait.gd" id="1_ejue5"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/graphic_interface/resources/base_gui_theme.tres" id="1_x1xka"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/images/ico_continue.png" id="4_lqu2o"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd" id="1_ejue5"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="1_x1xka"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/images/ico_continue.png" id="4_lqu2o"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_8fbbc"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://cymfte4xe3tnw"]

[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/graphic_interface/resources/base_gui_theme.tres" id="1_mrmwc"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/dialog_text.gd" id="2_adpg7"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/images/ico_continue.png" id="3_fqlmr"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="1_mrmwc"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd" id="2_adpg7"]
[ext_resource type="Texture2D" uid="uid://h156lkhxk5tl" path="res://addons/popochiu/engine/objects/gui/components/dialog_text/images/ico_continue.png" id="3_fqlmr"]

[node name="DialogText" type="Control" groups=["popochiu_gui_component"]]
layout_mode = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
importer="texture"
type="CompressedTexture2D"
uid="uid://h156lkhxk5tl"
path="res://.godot/imported/ico_continue.png-712e89874a0a9e45d6fad99cb4a72e4b.ctex"
path="res://.godot/imported/ico_continue.png-1c56d7b39fdf03dafd5aacca996d24e0.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/popochiu/engine/objects/graphic_interface/components/dialog_text/images/ico_continue.png"
dest_files=["res://.godot/imported/ico_continue.png-712e89874a0a9e45d6fad99cb4a72e4b.ctex"]
source_file="res://addons/popochiu/engine/objects/gui/components/dialog_text/images/ico_continue.png"
dest_files=["res://.godot/imported/ico_continue.png-1c56d7b39fdf03dafd5aacca996d24e0.ctex"]

[params]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://esorelppu4hw"]

[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/graphic_interface/resources/base_gui_theme.tres" id="1_s7kd8"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/graphic_interface/components/hover_text/hover_text.gd" id="2_r2ckj"]
[ext_resource type="Theme" uid="uid://dpequqav4rjaf" path="res://addons/popochiu/engine/objects/gui/resources/base_gui_theme.tres" id="1_s7kd8"]
[ext_resource type="Script" path="res://addons/popochiu/engine/objects/gui/components/hover_text/hover_text.gd" id="2_r2ckj"]

[node name="HoverText" type="Control" groups=["popochiu_gui_component"]]
layout_mode = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func _ready():
panel_container.position.y = hidden_y

# Connect to singletons signals
G.blocked.connect(_on_graphic_interface_blocked)
G.unblocked.connect(_on_graphic_interface_unblocked)
G.blocked.connect(_on_gui_blocked)
G.unblocked.connect(_on_gui_unblocked)
I.item_added.connect(_add_item)
I.item_removed.connect(_remove_item)
I.item_replaced.connect(_replace_item)
Expand Down Expand Up @@ -101,14 +101,14 @@ func _change_cursor(item: PopochiuInventoryItem) -> void:
I.set_active_item(item)


func _on_graphic_interface_blocked() -> void:
func _on_gui_blocked() -> void:
set_process_input(false)

if hide_when_gui_is_blocked:
hide()


func _on_graphic_interface_unblocked() -> void:
func _on_gui_unblocked() -> void:
set_process_input(true)

if hide_when_gui_is_blocked:
Expand Down
Loading

0 comments on commit 9868cde

Please sign in to comment.