Skip to content

Commit

Permalink
Add an option for crop_to_visible rect
Browse files Browse the repository at this point in the history
  • Loading branch information
timkrief authored Aug 1, 2024
1 parent e3e6ba7 commit a617dde
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions demo/addons/godot-krita-importer/krita_import_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var presets : Array[Dictionary] = [
"default_value": CanvasItem.TEXTURE_FILTER_PARENT_NODE,
"property_hint": PROPERTY_HINT_ENUM,
"hint_string": ",".join(range(0, CanvasItem.TEXTURE_FILTER_MAX))
},{
"name": "crop_to_visible",
"default_value": true
},
]

Expand Down Expand Up @@ -116,6 +119,7 @@ static func import_group_layer(importer : KraImporter, layer_data : Dictionary,
static func import_paint_layer(layer_data : Dictionary, options: Dictionary) -> Node2D:
var sprite = Sprite2D.new()
sprite.name = layer_data.get("name", sprite.name)
sprite.position = layer_data.get("position", Vector2.ZERO)
sprite.centered = false

sprite.visible = layer_data.get("visible", true)
Expand All @@ -125,11 +129,13 @@ static func import_paint_layer(layer_data : Dictionary, options: Dictionary) ->

#create_from_data(width: int, height: int, use_mipmaps: bool, format: Format, data: PoolByteArray)
var image = Image.create_from_data(layer_data.width, layer_data.height, false, layer_data.format, layer_data.data)
var visible_region = image.get_used_rect()
var cropped_image = image.get_region(visible_region);
var texture = ImageTexture.create_from_image(cropped_image)

if options.get("crop_to_visible", true):
var visible_region = image.get_used_rect()
image = image.get_region(visible_region)
sprite.position += Vector2(visible_region.position)

sprite.position = layer_data.get("position", Vector2.ZERO) + Vector2(visible_region.position)
var texture = ImageTexture.create_from_image(image)

sprite.texture_filter = options.get("texture_filter", CanvasItem.TEXTURE_FILTER_PARENT_NODE)
sprite.texture = texture
Expand Down

0 comments on commit a617dde

Please sign in to comment.