LanzaSchneider 1 год назад
Родитель
Сommit
fb9af92539

+ 0 - 18
editor/scripts/dev_tool.gd

@@ -1,18 +0,0 @@
-@tool
-class_name DevTool
-extends Resource
-
-@export
-var tool_dock: EditorPlugin.DockSlot
-
-@export_file("*.json")
-var tool_config: String
-
-@export_file("*.rb")
-var tool_script: String
-
-@export
-var tool_name: String
-
-@export
-var tool_command_name: String

+ 1 - 1
editor/tools/common.rb

@@ -17,7 +17,7 @@ CONFIG_ROOT = "#{$res_root}/.tbl"
 def get_options(tool_file)
   options = {}
   name = File.basename(tool_file, ".rb")
-  JSON.parse(File.read("#{CONFIG_ROOT}/#{name}.json")).each do |unit|
+  JSON.parse(File.read("#{CONFIG_ROOT}/#{name}.json"))['options'].each do |unit|
     key, value = unit['name'], unit['value']
     value.gsub!('res:', $res_root) if value.is_a?(String)
     options[key] = value

+ 0 - 11
editor/tools/config-importer.tres

@@ -1,11 +0,0 @@
-[gd_resource type="Resource" script_class="DevTool" load_steps=2 format=3 uid="uid://cgu1tpbnhjpjc"]
-
-[ext_resource type="Script" path="res://addons/godot-TBL-extension-base/editor/scripts/dev_tool.gd" id="1_5dgtc"]
-
-[resource]
-script = ExtResource("1_5dgtc")
-tool_dock = 3
-tool_config = "res://.tbl/config-importer.json"
-tool_script = "res://addons/godot-TBL-extension-base/editor/tools/config-importer.rb"
-tool_name = "配置"
-tool_command_name = "导入配置"

+ 23 - 11
editor/tools/sprite-packer.rb

@@ -33,25 +33,37 @@ csv_sprite << [':id', '@atlas', ':int', ':int']
 Dir["#{sprite_path}/*"].each do |path|
   atlas_name = convert_case(File::basename(path))
   next unless File::directory?(path)
-  atlas_size = 1024
-  sprites = Dir["#{path}/*"].collect do |file| 
-    sprite = ChunkyPNG::Image.from_file(file)
-    sprite.define_singleton_method :file do
-      return file
+  atlas_size = 32
+  bins = []
+  loop do
+    bins.clear
+    begin
+      sprites = Dir["#{path}/*"].collect do |file| 
+        sprite = ChunkyPNG::Image.from_file(file)
+        sprite.define_singleton_method :file do
+          return file
+        end
+        sprite
+      end
+      items = sprites.collect { |sprite| Binpack::Item.new(sprite, sprite.width, sprite.height) }
+      bins = Binpack::Bin.pack(items, [], Binpack::Bin.new(atlas_size, atlas_size, padding))
+      raise 'too small' if bins.size > 1
+      break
+    rescue
+      atlas_size *= 2
+      break if atlas_size >= 2048
     end
-    sprite
   end
-  items = sprites.collect { |sprite| Binpack::Item.new(sprite, sprite.width, sprite.height) }
-  bins = Binpack::Bin.pack(items, [], Binpack::Bin.new(atlas_size, atlas_size, padding))
   bins.each do |bin|
     atlas = ChunkyPNG::Image.new(atlas_size, atlas_size, ChunkyPNG::Color::TRANSPARENT)
     bin.items.each do |item|
       unit, x, y = *item
+      puts "#{x}, #{y}"
       atlas.compose!(unit.obj, x, y)
       csv_sprite << [convert_case(File.basename(unit.obj.file, '.png')), atlas_name, x, y]
     end
-    atlas_path = "#{atlas_path}/#{File::basename(path)}.png"
-    atlas.save atlas_path
-    csv_atlas << [atlas_name, File.basename(atlas_path)]
+    atlas_file = "#{atlas_path}/#{File::basename(path)}.png"
+    atlas.save atlas_file
+    csv_atlas << [atlas_name, File.basename(atlas_file)]
   end
 end

+ 0 - 11
editor/tools/sprite-packer.tres

@@ -1,11 +0,0 @@
-[gd_resource type="Resource" script_class="DevTool" load_steps=2 format=3 uid="uid://cyefuuhoo3do0"]
-
-[ext_resource type="Script" path="res://addons/godot-TBL-extension-base/editor/scripts/dev_tool.gd" id="1_t2kpq"]
-
-[resource]
-script = ExtResource("1_t2kpq")
-tool_dock = 3
-tool_config = "res://.tbl/sprite-packer.json"
-tool_script = "res://addons/godot-TBL-extension-base/editor/tools/sprite-packer.rb"
-tool_name = "图集"
-tool_command_name = "生成图集"

+ 15 - 10
tblext.gd

@@ -1,18 +1,23 @@
 @tool
 extends EditorPlugin
 
+const _tool_json_root_path := "res://.tbl"
+
 func _enter_tree() -> void:
 	var helper := preload('tblext.gd')
 	var base_dir: String = helper.resource_path.get_base_dir()
 	var tools_dir := base_dir.path_join('editor/tools')
 	var dir := DirAccess.open(tools_dir)
 	for file in dir.get_files():
-		if file.get_extension() == 'tres':
-			make_panel(load(tools_dir.path_join(file)))
+		if file.get_extension() == 'rb':
+			var json_path := _tool_json_root_path.path_join(file.get_basename()) + '.json'
+			if FileAccess.file_exists(json_path):
+				make_panel(ProjectSettings.globalize_path(json_path), ProjectSettings.globalize_path(file))
 
-func make_panel(dev_tool:DevTool) -> VBoxContainer:
+func make_panel(tool_json_path:String, tool_script_path:String) -> VBoxContainer:
 	var panel := VBoxContainer.new()
-	var options := JSON.parse_string(FileAccess.get_file_as_string(dev_tool.tool_config))
+	var tool_json = JSON.parse_string(FileAccess.get_file_as_string(tool_json_path))
+	var options = tool_json.options
 	for option in options:
 		if option.value is String:
 			var comment := Label.new()
@@ -58,16 +63,16 @@ func make_panel(dev_tool:DevTool) -> VBoxContainer:
 		else:
 			print("unsupported option value({0}): {1}".format([option.name, typeof(option.value)]))
 	var command := Button.new()
-	command.text = dev_tool.tool_command_name
+	command.text = tool_json.command_name
 	command.button_up.connect(func():
-		OS.execute('ruby', [ProjectSettings.globalize_path(dev_tool.tool_script)])
+		OS.execute('ruby', [tool_script_path])
 		)
 	panel.add_child(command)
-	panel.name = dev_tool.tool_name
-	add_control_to_dock(dev_tool.tool_dock, panel)
+	panel.name = tool_json.title
+	add_control_to_dock(DOCK_SLOT_LEFT_BR, panel)
 	panel.tree_exited.connect(func():
-		var json := FileAccess.open(dev_tool.tool_config, FileAccess.WRITE)
-		json.store_string(JSON.stringify(options, "\t"))
+		var json := FileAccess.open(tool_json_path, FileAccess.WRITE)
+		json.store_string(JSON.stringify(tool_json, "\t"))
 		)
 	tree_exiting.connect(func():
 		remove_control_from_docks(panel)