Skip to content

Commit

Permalink
Update source files and demo project to GDExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
2shady4u committed Mar 12, 2024
1 parent 712f5a5 commit 8320b70
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 143 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# Godot-specific ignores
*.gen.*
.import/
.godot/
/gen/

# Imported translations (automatically generated from CSV files)
*.translation
Expand All @@ -20,8 +23,6 @@ mono_crash.*.json
*.sql
*.sqlite
*.db
# Do not ignore the database that is to be opened in read-only mode!
!data_to_be_packaged.db

# Compiled source #
###################
Expand Down Expand Up @@ -49,8 +50,6 @@ Thumbs.db
# Etc
.sconsign.dblite
demo/build
test_backup_new.json
test_backup_base64_new.json

# VS
vsproj/.vs/
Expand Down
6 changes: 2 additions & 4 deletions demo/Main.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
extends Node2D

const KraImporter = preload("res://addons/godot-krita-importer/bin/libkra_importer.gdns")

func _ready():
var importer := KraImporter.new()
importer.load("res://addons/godot-krita-importer/examples/example.kra")

var options := {
"ignore_invisible_layers": false,
"flags/filter": false
"texture_filter": CanvasItem.TEXTURE_FILTER_NEAREST
}

# We can't instance this class, but we can still access the static methods!
Expand All @@ -19,7 +17,7 @@ func _ready():

match(layer_data.get("type", -1)):
0:
var sprite : Sprite = import_plugin.import_paint_layer(layer_data, options)
var sprite : Sprite2D = import_plugin.import_paint_layer(layer_data, options)
if sprite != null:
add_child(sprite)
1:
Expand Down
6 changes: 3 additions & 3 deletions demo/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://b67rg4hn2tcgh"]

[ext_resource path="res://Main.gd" type="Script" id=1]
[ext_resource type="Script" path="res://Main.gd" id="1"]

[node name="Main" type="Node2D"]
script = ExtResource( 1 )
script = ExtResource("1")
18 changes: 0 additions & 18 deletions demo/addons/godot-krita-importer/bin/libkra_importer.gdnlib

This file was deleted.

8 changes: 0 additions & 8 deletions demo/addons/godot-krita-importer/bin/libkra_importer.gdns

This file was deleted.

4 changes: 2 additions & 2 deletions demo/addons/godot-krita-importer/godot-krita-importer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# See LICENSE in the project root for license information.
# ############################################################################ #

tool
@tool
extends EditorPlugin

var import_plugin = null

func get_name() -> String:
func get_name() -> StringName:
return "Godot Krita Importer"

func _enter_tree():
Expand Down
65 changes: 31 additions & 34 deletions demo/addons/godot-krita-importer/krita_import_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE in the project root for license information.
# ############################################################################ #

tool
@tool
extends EditorImportPlugin

enum VerbosityLevel {
Expand All @@ -14,47 +14,52 @@ enum VerbosityLevel {
VERY_VERBOSE
}

const presets := [
{"name": "ignore_invisible_layers", "default_value": false},
{"name": "flags/filter", "default_value": true},
var presets : Array[Dictionary] = [
{
"name": "ignore_invisible_layers",
"default_value": false
},{
"name": "texture_filter",
"default_value": CanvasItem.TEXTURE_FILTER_PARENT_NODE,
"property_hint": PROPERTY_HINT_ENUM,
"hint_string": ",".join(range(0, CanvasItem.TEXTURE_FILTER_MAX))
},
]

const KraImporter = preload("res://addons/godot-krita-importer/bin/libkra_importer.gdns")

func get_import_options(preset : int) -> Array:
func _get_import_options(path : String, preset : int) -> Array[Dictionary]:
return presets

func get_import_order() -> int:
func _get_import_order() -> int:
return 100

func get_importer_name() -> String:
func _get_importer_name() -> String:
return "godot-krita-importer"

func get_option_visibility(option : String, options : Dictionary) -> bool:
func _get_option_visibility(path: String, option_name: StringName, options: Dictionary) -> bool:
return true

func get_preset_count() -> int:
func _get_preset_count() -> int:
return 1

func get_preset_name(preset : int) -> String:
func _get_preset_name(preset : int) -> String:
return "Default"

func get_priority() -> float:
func _get_priority() -> float:
return 1.0

func get_recognized_extensions() -> Array:
func _get_recognized_extensions() -> PackedStringArray:
return ["kra", "krz"]

func get_resource_type() -> String:
func _get_resource_type() -> String:
return "PackedScene"

func get_save_extension() -> String:
func _get_save_extension() -> String:
return "scn"

func get_visible_name() -> String:
func _get_visible_name() -> String:
return "Scene from Krita"

func import(source_file: String, save_path: String, options: Dictionary, platform_variants: Array, gen_files: Array) -> int:
func _import(source_file: String, save_path: String, options: Dictionary, platform_variants: Array, gen_files: Array) -> int:
var importer = KraImporter.new()
importer.verbosity_level = VerbosityLevel.QUIET

Expand All @@ -69,7 +74,7 @@ func import(source_file: String, save_path: String, options: Dictionary, platfor

match(layer_data.get("type", -1)):
0:
var sprite : Sprite = import_paint_layer(layer_data, options)
var sprite : Sprite2D = import_paint_layer(layer_data, options)
if sprite != null:
node.add_child(sprite)
1:
Expand All @@ -81,7 +86,7 @@ func import(source_file: String, save_path: String, options: Dictionary, platfor
set_owner_recursively(node, node)

scene.pack(node)
var error := ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], scene)
var error := ResourceSaver.save(scene, "%s.%s" % [save_path, _get_save_extension()])
# The node needs to be freed to avoid memory leakage
node.queue_free()
return error
Expand All @@ -96,14 +101,14 @@ static func import_group_layer(importer : KraImporter, layer_data : Dictionary,
return null
node.modulate.a = layer_data.get("opacity", 255.0)/255.0

var child_uuids : PoolStringArray = layer_data.get("child_uuids", PoolStringArray())
var child_uuids : PackedStringArray = layer_data.get("child_uuids", PackedStringArray())
# Needs to be in reverse order as to preserve layer ordering!
for i in range(child_uuids.size() - 1, -1, -1):
var uuid : String = child_uuids[i]
var child_data : Dictionary = importer.get_layer_data_with_uuid(uuid)
match(child_data.get("type", -1)):
0:
var sprite : Sprite = import_paint_layer(child_data, options)
var sprite : Sprite2D = import_paint_layer(child_data, options)
if sprite != null:
sprite.position -= node.position
node.add_child(sprite)
Expand All @@ -116,7 +121,7 @@ static func import_group_layer(importer : KraImporter, layer_data : Dictionary,
return node

static func import_paint_layer(layer_data : Dictionary, options: Dictionary) -> Node2D:
var sprite = Sprite.new()
var sprite = Sprite2D.new()
sprite.name = layer_data.get("name", sprite.name)
sprite.position = layer_data.get("position", Vector2.ZERO)
sprite.centered = false
Expand All @@ -126,19 +131,11 @@ static func import_paint_layer(layer_data : Dictionary, options: Dictionary) ->
return null
sprite.modulate.a = layer_data.get("opacity", 255.0)/255.0

var image = Image.new()
#create_from_data(width: int, height: int, use_mipmaps: bool, format: Format, data: PoolByteArray)
image.create_from_data(layer_data.width, layer_data.height, false, layer_data.format, layer_data.data)

var texture = ImageTexture.new()
texture.create_from_image(image)

# Disable/enable the filter option which is positioned at the second bit position
if options.get("flags/filter", true):
texture.flags = enable_bit(texture.flags, Texture.FLAG_FILTER)
else:
texture.flags = disable_bit(texture.flags, Texture.FLAG_FILTER)
var image = Image.create_from_data(layer_data.width, layer_data.height, false, layer_data.format, layer_data.data)
var texture = ImageTexture.create_from_image(image)

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

return sprite
Expand Down
22 changes: 22 additions & 0 deletions demo/addons/godot-krita-importer/libkra_importer.gdextension
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[configuration]

entry_symbol = "kra_importer_library_init"
compatibility_minimum = 4.2

[libraries]

macos = "res://addons/godot-krita-importer/bin/libkra_importer.macos.template_debug.framework"
macos.template_release = "res://addons/godot-krita-importer/bin/libkra_importer.macos.template_release.framework"
windows.x86_64 = "res://addons/godot-krita-importer/bin/libkra_importer.windows.template_debug.x86_64.dll"
windows.template_release.x86_64 = "res://addons/godot-krita-importer/bin/libkra_importer.windows.template_release.x86_64.dll"
linux.x86_64 = "res://addons/godot-krita-importer/bin/libkra_importer.linux.template_debug.x86_64.so"
linux.template_release.x86_64 = "res://addons/godot-krita-importer/bin/libkra_importer.linux.template_release.x86_64.so"

[dependencies]

macos = {}
macos.template_release = {}
windows.x86_64 = {}
windows.template_release.x86_64 = {}
linux.x86_64 = {}
linux.template_release.x86_64 = {}
6 changes: 3 additions & 3 deletions demo/default_env.tres
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[gd_resource type="Environment" load_steps=2 format=2]
[gd_resource type="Environment" load_steps=2 format=3 uid="uid://c2b0yc570iuee"]

[sub_resource type="ProceduralSky" id=1]
[sub_resource type="Sky" id="1"]

[resource]
background_mode = 2
background_sky = SubResource( 1 )
sky = SubResource("1")
31 changes: 15 additions & 16 deletions demo/icon.png.import
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
type="CompressedTexture2D"
uid="uid://jdoxa5waor7i"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/hdr_compression=1
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
5 changes: 3 additions & 2 deletions demo/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=4
config_version=5

[application]

config/name="Krita Importer Demo"
run/main_scene="res://Main.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.png"

[debug]
Expand All @@ -24,4 +25,4 @@ common/enable_pause_aware_picking=true

[rendering]

environment/default_environment="res://default_env.tres"
environment/defaults/default_environment="res://default_env.tres"
Loading

0 comments on commit 8320b70

Please sign in to comment.