2022-11-08 18:04:41 +00:00
|
|
|
class_name ReferenceImage
|
|
|
|
extends Sprite
|
|
|
|
# A class describing a reference image
|
|
|
|
|
|
|
|
signal properties_changed
|
|
|
|
|
|
|
|
var project = Global.current_project
|
|
|
|
|
2023-08-15 00:56:43 +02:00
|
|
|
var shader: Shader = preload("res://src/Shaders/SilhouetteShader.gdshader")
|
|
|
|
|
2022-11-08 18:04:41 +00:00
|
|
|
var image_path: String = ""
|
|
|
|
|
2022-12-08 05:20:54 +05:00
|
|
|
var filter = false
|
2023-08-15 00:56:43 +02:00
|
|
|
var silhouette := false
|
2022-12-08 05:20:54 +05:00
|
|
|
|
2022-11-08 18:04:41 +00:00
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
project.reference_images.append(self)
|
|
|
|
|
|
|
|
|
2022-12-07 15:38:44 +02:00
|
|
|
func change_properties() -> void:
|
2022-11-08 18:04:41 +00:00
|
|
|
emit_signal("properties_changed")
|
|
|
|
|
|
|
|
|
|
|
|
# Resets the position and scale of the reference image.
|
2022-12-07 15:38:44 +02:00
|
|
|
func position_reset() -> void:
|
2022-11-08 18:04:41 +00:00
|
|
|
position = project.size / 2.0
|
|
|
|
if texture != null:
|
|
|
|
scale = (
|
|
|
|
Vector2.ONE
|
|
|
|
* min(project.size.x / texture.get_width(), project.size.y / texture.get_height())
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
scale = Vector2.ONE
|
|
|
|
|
|
|
|
|
|
|
|
# Serialize details of the reference image.
|
2022-12-07 15:38:44 +02:00
|
|
|
func serialize() -> Dictionary:
|
2022-11-08 18:04:41 +00:00
|
|
|
return {
|
|
|
|
"x": position.x,
|
|
|
|
"y": position.y,
|
|
|
|
"scale_x": scale.x,
|
|
|
|
"scale_y": scale.y,
|
|
|
|
"modulate_r": modulate.r,
|
|
|
|
"modulate_g": modulate.g,
|
|
|
|
"modulate_b": modulate.b,
|
|
|
|
"modulate_a": modulate.a,
|
2022-12-08 05:20:54 +05:00
|
|
|
"filter": filter,
|
2023-08-15 00:56:43 +02:00
|
|
|
"silhouette": silhouette,
|
2022-11-08 18:04:41 +00:00
|
|
|
"image_path": image_path
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Load details of the reference image from a dictionary.
|
|
|
|
# Be aware that new ReferenceImages are created via deserialization.
|
|
|
|
# This is because deserialization sets up some nice defaults.
|
2022-12-07 15:38:44 +02:00
|
|
|
func deserialize(d: Dictionary) -> void:
|
2022-11-08 18:04:41 +00:00
|
|
|
modulate = Color(1, 1, 1, 0.5)
|
|
|
|
if d.has("image_path"):
|
|
|
|
# Note that reference images are referred to by path.
|
|
|
|
# These images may be rather big.
|
|
|
|
# Also
|
|
|
|
image_path = d["image_path"]
|
2022-12-07 15:38:44 +02:00
|
|
|
var img := Image.new()
|
2022-11-08 18:04:41 +00:00
|
|
|
if img.load(image_path) == OK:
|
2022-12-07 15:38:44 +02:00
|
|
|
var itex := ImageTexture.new()
|
2022-11-08 18:04:41 +00:00
|
|
|
# don't do FLAG_REPEAT - it could cause visual issues
|
2022-12-08 05:20:54 +05:00
|
|
|
itex.create_from_image(img, Texture.FLAG_MIPMAPS)
|
2022-11-08 18:04:41 +00:00
|
|
|
texture = itex
|
2023-08-15 00:56:43 +02:00
|
|
|
# Apply the silhouette shader
|
|
|
|
var mat = ShaderMaterial.new()
|
|
|
|
mat.shader = shader
|
|
|
|
# TODO: Lsbt - Add a option in prefrences to customize the color
|
|
|
|
# This color is almost black because it is less harsh
|
|
|
|
mat.set_shader_param("silhouette_color", Color(0.069, 0.069326, 0.074219))
|
|
|
|
set_material(mat)
|
|
|
|
|
2022-11-08 18:04:41 +00:00
|
|
|
# Now that the image may have been established...
|
|
|
|
position_reset()
|
|
|
|
if d.has("x"):
|
|
|
|
position.x = d["x"]
|
|
|
|
if d.has("y"):
|
|
|
|
position.y = d["y"]
|
|
|
|
if d.has("scale_x"):
|
|
|
|
scale.x = d["scale_x"]
|
|
|
|
if d.has("scale_y"):
|
|
|
|
scale.y = d["scale_y"]
|
|
|
|
if d.has("modulate_r"):
|
|
|
|
modulate.r = d["modulate_r"]
|
|
|
|
if d.has("modulate_g"):
|
|
|
|
modulate.g = d["modulate_g"]
|
|
|
|
if d.has("modulate_b"):
|
|
|
|
modulate.b = d["modulate_b"]
|
|
|
|
if d.has("modulate_a"):
|
|
|
|
modulate.a = d["modulate_a"]
|
2022-12-08 05:20:54 +05:00
|
|
|
if d.has("filter"):
|
|
|
|
filter = d["filter"]
|
2023-08-15 00:56:43 +02:00
|
|
|
if d.has("silhouette"):
|
|
|
|
get_material().set_shader_param("show_silhouette", d["silhouette"])
|
2022-11-08 18:04:41 +00:00
|
|
|
change_properties()
|
2022-12-07 15:38:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Useful for HTML5
|
|
|
|
func create_from_image(image: Image) -> void:
|
|
|
|
var itex := ImageTexture.new()
|
|
|
|
# don't do FLAG_REPEAT - it could cause visual issues
|
|
|
|
itex.create_from_image(image, Texture.FLAG_MIPMAPS | Texture.FLAG_FILTER)
|
|
|
|
texture = itex
|
|
|
|
position_reset()
|