Custom module

This commit is contained in:
Marco 2025-03-19 15:56:18 +01:00
commit 0b1ea343dc
25 changed files with 598 additions and 33 deletions

View file

@ -35,4 +35,3 @@ func _custom_fade_in(_time:float) -> bool:
## If you return false (by default) it will attempt to animate the "modulate" property.
func _custom_fade_out(_time:float) -> bool:
return false

View file

@ -0,0 +1,156 @@
@tool
class_name DialogicShowImageEvent
extends DialogicEvent
## Event to show scenes in the image and switch between them.
### Settings
## The scene to use. If empty, this will default to the DefaultBackground.gd scene.
## This scene supports images and fading.
## If you set it to a scene path, then that scene will be instanced.
## Learn more about custom backgrounds in the Subsystem_Background.gd docs.
var scene := ""
## The argument that is passed to the background scene.
## For the default scene it's the path to the image to show.
var argument := ""
## The time the fade animation will take. Leave at 0 for instant change.
var fade: float = 0.0
## Name of the transition to use.
var transition := ""
## Helpers for visual editor
enum ArgumentTypes {IMAGE, CUSTOM}
var _arg_type := ArgumentTypes.IMAGE :
get:
if argument.begins_with("res://"):
return ArgumentTypes.IMAGE
else:
return _arg_type
set(value):
if value == ArgumentTypes.CUSTOM:
if argument.begins_with("res://"):
argument = " "+argument
_arg_type = value
enum SceneTypes {DEFAULT, CUSTOM}
var _scene_type := SceneTypes.DEFAULT :
get:
if scene.is_empty():
return _scene_type
else:
return SceneTypes.CUSTOM
set(value):
if value == SceneTypes.DEFAULT:
scene = ""
_scene_type = value
#region EXECUTION
################################################################################
func _execute() -> void:
var final_fade_duration := fade
if dialogic.Inputs.auto_skip.enabled:
var time_per_event: float = dialogic.Inputs.auto_skip.time_per_event
final_fade_duration = min(fade, time_per_event)
dialogic.get_subsystem("ShowImage").update_image(scene, argument, final_fade_duration, transition)
finish()
#endregion
#region INITIALIZE
################################################################################
func _init() -> void:
event_name = "Image"
set_default_color('Color8')
event_category = "Visuals"
event_sorting_index = 0
#endregion
#region SAVE & LOAD
################################################################################
func get_shortcode() -> String:
return "image"
func get_shortcode_parameters() -> Dictionary:
return {
#param_name : property_info
"scene" : {"property": "scene", "default": ""},
"arg" : {"property": "argument", "default": ""},
"fade" : {"property": "fade", "default": 0},
"transition" : {"property": "transition", "default": "",
"suggestions": get_transition_suggestions},
}
#endregion
#region EDITOR REPRESENTATION
################################################################################
func build_event_editor() -> void:
add_header_edit('_scene_type', ValueType.FIXED_OPTIONS, {
'left_text' :'Show',
'options': [
{
'label': 'Image',
'value': SceneTypes.DEFAULT,
'icon': ["GuiRadioUnchecked", "EditorIcons"]
},
{
'label': 'Custom Scene',
'value': SceneTypes.CUSTOM,
'icon': ["PackedScene", "EditorIcons"]
}
]})
add_header_label("with image", "_scene_type == SceneTypes.DEFAULT")
add_header_edit("scene", ValueType.FILE,
{'file_filter':'*.tscn, *.scn; Scene Files',
'placeholder': "Custom scene",
'editor_icon': ["PackedScene", "EditorIcons"],
}, '_scene_type == SceneTypes.CUSTOM')
add_header_edit('_arg_type', ValueType.FIXED_OPTIONS, {
'left_text' : 'with',
'options': [
{
'label': 'Image',
'value': ArgumentTypes.IMAGE,
'icon': ["Image", "EditorIcons"]
},
{
'label': 'Custom Argument',
'value': ArgumentTypes.CUSTOM,
'icon': ["String", "EditorIcons"]
}
], "symbol_only": true}, "_scene_type == SceneTypes.CUSTOM")
add_header_edit('argument', ValueType.FILE,
{'file_filter':'*.jpg, *.jpeg, *.png, *.webp, *.tga, *svg, *.bmp, *.dds, *.exr, *.hdr; Supported Image Files',
'placeholder': "No Image",
'editor_icon': ["Image", "EditorIcons"],
},
'_arg_type == ArgumentTypes.IMAGE or _scene_type == SceneTypes.DEFAULT')
add_header_edit('argument', ValueType.SINGLELINE_TEXT, {}, '_arg_type == ArgumentTypes.CUSTOM')
add_body_edit("transition", ValueType.DYNAMIC_OPTIONS,
{'left_text':'Transition:',
'empty_text':'Simple Fade',
'suggestions_func':get_transition_suggestions,
'editor_icon':["PopupMenu", "EditorIcons"]})
add_body_edit("fade", ValueType.NUMBER, {'left_text':'Fade time:'})
func get_transition_suggestions(_filter:String="") -> Dictionary:
var transitions := DialogicResourceUtil.list_special_resources("BackgroundTransition")
var suggestions := {}
for i in transitions:
suggestions[DialogicUtil.pretty_name(i)] = {'value': DialogicUtil.pretty_name(i), 'editor_icon': ["PopupMenu", "EditorIcons"]}
return suggestions
#endregion

View file

@ -0,0 +1 @@
uid://dqkhnb7pxqwew

View file

@ -0,0 +1,8 @@
@tool
extends DialogicIndexer
func _get_events() -> Array:
return [this_folder.path_join('event_show_image.gd')]
func _get_subsystems() -> Array:
return [{'name':'ShowImage', 'script':this_folder.path_join('subsystem_show_image.gd')}]

View file

@ -0,0 +1 @@
uid://dgkon3jaqokyk

View file

@ -0,0 +1,186 @@
extends DialogicSubsystem
## Subsystem for managing images.
##
## This subsystem has many different helper methods for managing backgrounds.
## For instance, you can listen to image changes via
## [signal image_changed].
## Whenever a new background is set, this signal is emitted and contains a
## dictionary with the following keys: [br]
## [br]
## Key | Value Type | Value [br]
## ----------- | ------------- | ----- [br]
## `scene` | [type String] | The scene path of the new background. [br]
## `argument` | [type String] | Information given to the background on its update routine. [br]
## `fade_time` | [type float] | The time the background may take to transition in. [br]
## `same_scene`| [type bool] | If the new background uses the same Godot scene. [br]
signal image_changed(info: Dictionary)
## The default background scene Dialogic will use.
var default_background_scene: PackedScene = load("res://Dialogue/CustomScenes/DefaultImageScene/default_image.tscn")
## The default transition Dialogic will use.
var default_transition: String = "res://addons/dialogic/Modules/Background/Transitions/Defaults/simple_fade.gd"
#region STATE
####################################################################################################
## Empties the current background state.
func clear_game_state(_clear_flag := DialogicGameHandler.ClearFlags.FULL_CLEAR) -> void:
update_image()
## Loads the background state from the current state info.
func load_game_state(_load_flag := LoadFlags.FULL_LOAD) -> void:
update_image(dialogic.current_state_info.get('image_scene', ''), dialogic.current_state_info.get('image_argument', ''), 0.0, default_transition, true)
#endregion
#region MAIN METHODS
####################################################################################################
## Method that adds a given scene as child of the DialogicNode_ImageHolder.
## It will call [_update_background()] on that scene with the given argument [argument].
## It will call [_fade_in()] on that scene with the given fade time.
## Will call fade_out on previous backgrounds scene.
##
## If the scene is the same as the last background you can bypass another instantiating
## and use the same scene.
## To do so implement [_should_do_background_update()] on the custom background scene.
## Then [_update_background()] will be called directly on that previous scene.
func update_image(scene := "", argument := "", fade_time := 0.0, transition_path:=default_transition, force := false) -> void:
var background_holder: DialogicNode_ImageHolder
if dialogic.has_subsystem('Styles'):
background_holder = dialogic.Styles.get_first_node_in_layout('dialogic_image_holders')
else:
background_holder = get_tree().get_first_node_in_group('dialogic_image_holders')
var info := {'scene':scene, 'argument':argument, 'fade_time':fade_time, 'same_scene':false}
if background_holder == null:
image_changed.emit(info)
return
var bg_set := false
# First try just updating the existing scene.
if scene == dialogic.current_state_info.get('image_scene', ''):
if not force and argument == dialogic.current_state_info.get('image_argument', ''):
return
for old_bg in background_holder.get_children():
if !old_bg.has_meta('node') or not old_bg.get_meta('node') is DialogicImage:
continue
var prev_bg_node: DialogicImage = old_bg.get_meta('node')
if prev_bg_node._should_do_image_update(argument):
prev_bg_node._update_image(argument, fade_time)
bg_set = true
info['same_scene'] = true
dialogic.current_state_info['image_scene'] = scene
dialogic.current_state_info['image_argument'] = argument
if bg_set:
image_changed.emit(info)
return
var old_viewport: SubViewportContainer = null
if background_holder.has_meta('current_viewport'):
old_viewport = background_holder.get_meta('current_viewport', null)
var new_viewport: SubViewportContainer
if scene.ends_with('.tscn') and ResourceLoader.exists(scene):
new_viewport = add_image_node(load(scene), background_holder)
elif argument:
new_viewport = add_image_node(default_background_scene, background_holder)
else:
new_viewport = null
var trans_script: Script = load(DialogicResourceUtil.guess_special_resource("BackgroundTransition", transition_path, {"path":default_transition}).path)
var trans_node := Node.new()
trans_node.set_script(trans_script)
trans_node = (trans_node as DialogicBackgroundTransition)
trans_node.bg_holder = background_holder
trans_node.time = fade_time
if old_viewport:
trans_node.prev_scene = old_viewport.get_meta('node', null)
trans_node.prev_texture = old_viewport.get_child(0).get_texture()
old_viewport.get_meta('node')._custom_fade_out(fade_time)
old_viewport.hide()
# TODO We have to call this again here because of https://github.com/godotengine/godot/issues/23729
old_viewport.get_child(0).render_target_update_mode = SubViewport.UPDATE_ALWAYS
trans_node.transition_finished.connect(old_viewport.queue_free)
if new_viewport:
trans_node.next_scene = new_viewport.get_meta('node', null)
trans_node.next_texture = new_viewport.get_child(0).get_texture()
new_viewport.get_meta('node')._update_image(argument, fade_time)
new_viewport.get_meta('node')._custom_fade_in(fade_time)
else:
background_holder.remove_meta('current_viewport')
add_child(trans_node)
if fade_time == 0:
trans_node.transition_finished.emit()
_on_transition_finished(background_holder, trans_node)
else:
trans_node.transition_finished.connect(_on_transition_finished.bind(background_holder, trans_node))
# We need to break this connection if the background_holder get's removed during the transition
background_holder.tree_exited.connect(trans_node.disconnect.bind("transition_finished", _on_transition_finished))
trans_node._fade()
image_changed.emit(info)
func _on_transition_finished(background_node:DialogicNode_ImageHolder, transition_node:DialogicBackgroundTransition) -> void:
if background_node.has_meta("current_viewport"):
if background_node.get_meta("current_viewport").get_meta("node", null) == transition_node.next_scene:
background_node.get_meta("current_viewport").show()
background_node.material = null
background_node.color = Color.TRANSPARENT
transition_node.queue_free()
## Adds sub-viewport with the given background scene as child to
## Dialogic scene.
func add_image_node(scene:PackedScene, parent:DialogicNode_ImageHolder) -> SubViewportContainer:
var v_con := SubViewportContainer.new()
var viewport := SubViewport.new()
var b_scene := scene.instantiate()
if not b_scene is DialogicImage:
printerr("[Dialogic] Given background scene was not of type DialogicImage! Make sure the scene has a script that extends DialogicImage.")
v_con.queue_free()
viewport.queue_free()
b_scene.queue_free()
return null
parent.add_child(v_con)
v_con.hide()
v_con.stretch = true
v_con.size = parent.size
v_con.set_anchors_preset(Control.PRESET_FULL_RECT)
v_con.add_child(viewport)
viewport.transparent_bg = true
viewport.disable_3d = true
viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS
viewport.canvas_item_default_texture_filter = ProjectSettings.get_setting("rendering/textures/canvas_textures/default_texture_filter")
viewport.add_child(b_scene)
b_scene.viewport = viewport
b_scene.viewport_container = v_con
parent.set_meta('current_viewport', v_con)
v_con.set_meta('node', b_scene)
return v_con
## Whether a image is set.
func has_image() -> bool:
return !dialogic.current_state_info.get('image_scene', '').is_empty() or !dialogic.current_state_info.get('image_argument','').is_empty()
#endregion

View file

@ -0,0 +1 @@
uid://cmulwa1r03x6g