Skip to content

Commit

Permalink
Cleanup from Cakes LayeredPortrait PR
Browse files Browse the repository at this point in the history
- lot's of static typing and whitespace improvements
- small adjustments to existing scripts
  • Loading branch information
Jowan-Spooner committed Apr 22, 2024
1 parent ff06706 commit 7a7f4db
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 60 deletions.
3 changes: 2 additions & 1 deletion addons/dialogic/Core/DialogicResourceUtil.gd
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ static func list_special_resources_of_type(type:String) -> Array:
return special_resources.filter(func(x:Dictionary): return type == x.get('type','')).map(func(x:Dictionary): return x.get('path', ''))


static func guess_special_resource(type:String, name:String, default:="") -> String:
static func guess_special_resource(type: String, name: String, default := "") -> String:
if special_resources.is_empty():
update_special_resources()

if name.begins_with('res://'):
return name
for path in list_special_resources_of_type(type):
Expand Down
8 changes: 5 additions & 3 deletions addons/dialogic/Core/DialogicUtil.gd
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static func get_indexers(include_custom := true, force_reload := false) -> Array


enum AnimationType {ALL, IN, OUT, ACTION}
static func get_portrait_animation_scripts(type:=AnimationType.ALL, include_custom:=true) -> Array:
static func get_portrait_animation_scripts(type := AnimationType.ALL, include_custom := true) -> Array:
var animations := DialogicResourceUtil.list_special_resources_of_type("PortraitAnimation")

return animations.filter(
Expand All @@ -121,10 +121,12 @@ static func get_portrait_animation_scripts(type:=AnimationType.ALL, include_cust
if type == AnimationType.ACTION: return not ('_in' in script or '_out' in script))


static func pretty_name(script:String) -> String:
var _name := script.get_file().trim_suffix("."+script.get_extension())
## Turns a [param file_path] from `some_file.png` to `Some File`.
static func pretty_name(file_path: String) -> String:
var _name := file_path.get_file().trim_suffix("." + file_path.get_extension())
_name = _name.replace('_', ' ')
_name = _name.capitalize()

return _name


Expand Down
37 changes: 27 additions & 10 deletions addons/dialogic/Editor/CharacterEditor/character_editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ signal portrait_selected()

# Current state
var loading := false
var current_previewed_scene = null
var current_previewed_scene: Variant = null
var current_scene_path: String = ""

# References
var selected_item: TreeItem
Expand Down Expand Up @@ -536,79 +537,96 @@ func report_name_change(item:TreeItem) -> void:
########### PREVIEW ############################################################

#region Preview
func update_preview(force:=false) -> void:
func update_preview(force := false) -> void:
%ScenePreviewWarning.hide()

if selected_item and is_instance_valid(selected_item) and selected_item.get_metadata(0) != null and !selected_item.get_metadata(0).has('group'):
%PreviewLabel.text = 'Preview of "'+%PortraitTree.get_full_item_name(selected_item)+'"'

var current_portrait_data: Dictionary = selected_item.get_metadata(0)

if not force and current_previewed_scene != null \
and current_previewed_scene.get_meta('path', '') == current_portrait_data.get('scene') \
and scene_file_path == current_portrait_data.get('scene') \
and current_previewed_scene.has_method('_should_do_portrait_update') \
and is_instance_valid(current_previewed_scene.get_script()) \
and current_previewed_scene._should_do_portrait_update(current_resource, selected_item.get_text(0)):
pass # we keep the same scene
# We keep the same scene.
pass
else:

for node in %RealPreviewPivot.get_children():
node.queue_free()

current_previewed_scene = null
current_scene_path = ""

var scene_path := def_portrait_path
if not current_portrait_data.get('scene', '').is_empty():
scene_path = current_portrait_data.get('scene')

if ResourceLoader.exists(scene_path):
if FileAccess.file_exists(scene_path):
current_previewed_scene = load(scene_path).instantiate()
current_scene_path = scene_path

if current_previewed_scene:
if not current_previewed_scene == null:
%RealPreviewPivot.add_child(current_previewed_scene)

if current_previewed_scene != null:
if not current_previewed_scene == null:
var scene: Node = current_previewed_scene

scene.show_behind_parent = true
DialogicUtil.apply_scene_export_overrides(scene, current_portrait_data.get('export_overrides', {}))

var mirror: bool = current_portrait_data.get('mirror', false) != current_resource.mirror
var scale: float = current_portrait_data.get('scale', 1) * current_resource.scale

if current_portrait_data.get('ignore_char_scale', false):
scale = current_portrait_data.get('scale', 1)

var offset: Vector2 = current_portrait_data.get('offset', Vector2()) + current_resource.offset

if is_instance_valid(scene.get_script()) and scene.script.is_tool():

if scene.has_method('_update_portrait'):
## Create a fake duplicate resource that has all the portrait changes applied already
var preview_character := current_resource.duplicate()
preview_character.portraits = get_updated_portrait_dict()
scene._update_portrait(preview_character, %PortraitTree.get_full_item_name(selected_item))

if scene.has_method('_set_mirror'):
scene._set_mirror(mirror)

if !%FitPreview_Toggle.button_pressed:
scene.position = Vector2() + offset
scene.scale = Vector2(1,1)*scale
else:
if is_instance_valid(scene.get_script()) and scene.script.is_tool() and scene.has_method('_get_covered_rect'):
var rect: Rect2= scene._get_covered_rect()

if not scene.get_script() == null and scene.script.is_tool() and scene.has_method('_get_covered_rect'):
var rect: Rect2 = scene._get_covered_rect()
var available_rect: Rect2 = %FullPreviewAvailableRect.get_rect()
scene.scale = Vector2(1,1) * min(available_rect.size.x/rect.size.x, available_rect.size.y/rect.size.y)
%RealPreviewPivot.position = (rect.position)*-1*scene.scale
%RealPreviewPivot.position.x = %FullPreviewAvailableRect.size.x/2
scene.position = Vector2()

else:
%ScenePreviewWarning.show()
else:
%PreviewLabel.text = 'Nothing to preview'

for child in %PortraitSettingsSection.get_children():

if child is DialogicCharacterEditorPortraitSection:
child._recheck(current_portrait_data)

else:
%PreviewLabel.text = 'No portrait to preview.'

for node in %RealPreviewPivot.get_children():
node.queue_free()

current_previewed_scene = null
current_scene_path = ""


func _on_some_resource_saved(file:Variant) -> void:
Expand Down Expand Up @@ -652,4 +670,3 @@ func _on_fit_preview_toggle_toggled(button_pressed):
## Open the reference manager
func _on_reference_manger_button_pressed():
editors_manager.reference_manager.open()

36 changes: 24 additions & 12 deletions addons/dialogic/Editor/Events/Fields/field_file.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ func _load_display_info(info:Dictionary) -> void:
placeholder = info.get('placeholder', '')
resource_icon = info.get('icon', null)
await ready

if resource_icon == null and info.has('editor_icon'):
resource_icon = callv('get_theme_icon', info.editor_icon)


func _set_value(value:Variant) -> void:
func _set_value(value: Variant) -> void:
current_value = value
var text := value
var text: String = value

if file_mode != EditorFileDialog.FILE_MODE_OPEN_DIR:
text = value.get_file()
%Field.tooltip_text = value
Expand All @@ -70,9 +72,11 @@ func _set_value(value:Variant) -> void:
%Field.custom_minimum_size.x = 0
%Field.expand_to_text_length = true

%Field.text = text
if not %Field.text == text:
value_changed.emit(property_name, current_value)
%Field.text = text

%ClearButton.visible = !value.is_empty() and !hide_reset
%ClearButton.visible = not value.is_empty() and not hide_reset


#endregion
Expand All @@ -87,41 +91,49 @@ func _on_OpenButton_pressed() -> void:

func _on_file_dialog_selected(path:String) -> void:
_set_value(path)
emit_signal("value_changed", property_name, path)
value_changed.emit(property_name, path)


func clear_path() -> void:
_set_value("")
emit_signal("value_changed", property_name, "")
value_changed.emit(property_name)

#endregion


#region DRAG AND DROP
################################################################################

func _can_drop_data_fw(at_position: Vector2, data: Variant) -> bool:
func _can_drop_data_fw(_at_position: Vector2, data: Variant) -> bool:
if typeof(data) == TYPE_DICTIONARY and data.has('files') and len(data.files) == 1:

if file_filter:

if '*.'+data.files[0].get_extension() in file_filter:
return true

else: return true

return false

func _drop_data_fw(at_position: Vector2, data: Variant) -> void:
_on_file_dialog_selected(data.files[0])

func _drop_data_fw(_at_position: Vector2, data: Variant) -> void:
var file: String = data.files[0]
_on_file_dialog_selected(file)

#endregion


#region VISUALS FOR FOCUS
################################################################################

func _on_field_focus_entered():
func _on_field_focus_entered() -> void:
$FocusStyle.show()

func _on_field_focus_exited():

func _on_field_focus_exited() -> void:
$FocusStyle.hide()
_on_file_dialog_selected(%Field.text)
var field_text: String = %Field.text
_on_file_dialog_selected(field_text)

#endregion
52 changes: 38 additions & 14 deletions addons/dialogic/Modules/Character/class_dialogic_animation.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,65 @@ signal finished_once
signal finished

## Set at runtime, will be the node to animate.
var node :Node
var node: Node

## Set at runtime, will be the length of the animation.
var time : float
var time: float

## Set at runtime, will be the position at which to end the animation.
var end_position : Vector2
var end_position: Vector2

## Set at runtime. The position the node started at.
var orig_pos : Vector2
var orig_pos: Vector2

## Used to repeate the animation for a number of times.
var repeats : int
var repeats: int

## If `true`, the animation will be reversed.
## This must be implemented by each animation or it will have no effect.
var is_reversed: bool = false


func _ready():
connect('finished_once', finished_one_loop)
func _ready() -> void:
finished_once.connect(finished_one_loop)


## To be overridden. Do the actual animating/tweening in here.
## Use the properties [node], [time], [end_position], [orig_pos].
func animate():
## Use the properties [member node], [member time], [member end_position], [member orig_pos].
func animate() -> void:
pass


func finished_one_loop():
## This method controls whether to repeat the animation or not.
## Animations must call this once they finished an animation.
func finished_one_loop() -> void:
repeats -= 1

if repeats > 0:
animate()
elif repeats == 0:
emit_signal("finished")

else:
finished.emit()


func pause():
func pause() -> void:
if node:
node.process_mode = Node.PROCESS_MODE_DISABLED


func resume():
func resume() -> void:
if node:
node.process_mode = Node.PROCESS_MODE_INHERIT


## If the animation wants to change the modulation, this method
## will return the property to change.
##
## The [class CanvasGroup] can use `self_modulate` instead of `modulate`
## to uniformly change the modulation of all children without additively
## overlaying the modulations.
func get_modulation_property() -> String:
if node is CanvasGroup:
return "self_modulate"
else:
return "modulate"
20 changes: 11 additions & 9 deletions addons/dialogic/Modules/Character/dialogic_portrait.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var portrait: String
################################################################################

## This function can be overridden.
## If this returns true, it won't insatnce a new scene, but call _update_portrait on this one.
## If this returns true, it won't instance a new scene, but call
## [method _update_portrait] on this one.
## This is only relevant if the next portrait uses the same scene.
## This allows implmenting transitions between portraits that use the same scene.
func _should_do_portrait_update(character:DialogicCharacter, portrait:String) -> bool:
## This allows implementing transitions between portraits that use the same scene.
func _should_do_portrait_update(character: DialogicCharacter, portrait: String) -> bool:
return true


Expand All @@ -25,17 +26,18 @@ func _should_do_portrait_update(character:DialogicCharacter, portrait:String) ->
## >>> $Sprite.position = $Sprite.get_rect().size * Vector2(-0.5, -1)
##
## * this depends on the portrait containers, but it will most likely be the bottom center (99% of cases)
func _update_portrait(passed_character:DialogicCharacter, passed_portrait:String) -> void:
func _update_portrait(passed_character: DialogicCharacter, passed_portrait: String) -> void:
pass


## This should be implemented. It is used for sizing in the
## character editor preview and in portrait containers.
## Scale and offset will be applied by dialogic.
## For example for a simple sprite this should work:
## character editor preview and in portrait containers.
## Scale and offset will be applied by Dialogic.
## For example, a simple sprite:
## >>> return Rect2($Sprite.position, $Sprite.get_rect().size)
##
## This will only work as expected if the portrait is positioned so that the root is at the pivot point.
## This will only work as expected if the portrait is positioned so that the
## root is at the pivot point.
##
## If you've used apply_texture this should work automatically.
func _get_covered_rect() -> Rect2:
Expand All @@ -55,7 +57,7 @@ func _set_mirror(mirror:bool) -> void:


## Function to accept and use the extra data, if the custom portrait wants to accept it
func _set_extra_data(data: String) -> void:
func _set_extra_data(_data: String) -> void:
pass

#endregion
Expand Down
Loading

0 comments on commit 7a7f4db

Please sign in to comment.