SpriteFramesToJsonConverter.gd 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. @tool
  2. class_name ThemedToJSONConverter
  3. extends Node
  4. @export var resource: ThemedResource = null
  5. @export_file("*.json") var json_file_path := ""
  6. @export_tool_button("Convert!") var button = convert_to_json
  7. var json := {}
  8. const THEMES := ["Overworld", "Underground", "Desert", "Snow", "Jungle", "Underwater", "Castle", "Sky", "Volcano", "Garden", "Beach"]
  9. const CAMPAIGNS := ["SMB1", "SMBLL", "SMBS", "SMBANN"]
  10. var animation_json := {}
  11. var variation_json := {}
  12. var donor_sprite_frames: SpriteFrames = null
  13. func convert_to_json() -> void:
  14. donor_sprite_frames = null
  15. variation_json.clear()
  16. animation_json.clear()
  17. json.clear()
  18. variation_json = get_variation_values(resource)
  19. if donor_sprite_frames != null:
  20. animation_json = get_animations()
  21. if animation_json.is_empty() == false:
  22. json["animations"] = animation_json
  23. json["variations"] = variation_json
  24. var json_file = JSON.stringify(json, "\t", false)
  25. var file = FileAccess.open(json_file_path, FileAccess.WRITE)
  26. file.store_string(json_file)
  27. file.close()
  28. print("Done!")
  29. func get_variation_values(variation_resource: Resource) -> Dictionary:
  30. var val_dict := {}
  31. for i in get_value_list(variation_resource):
  32. if variation_resource.get(i) != null:
  33. if variation_resource.get(i) is not ThemedResource and variation_resource.get(i) is not TimedResource and variation_resource.get(i) is not CampaignResource:
  34. var value = {}
  35. if variation_resource.get(i) is SpriteFrames and donor_sprite_frames == null:
  36. donor_sprite_frames = variation_resource.get(i)
  37. value["source"] = get_source(variation_resource.get(i))
  38. if variation_resource.get(i) is SpriteFrames:
  39. value["rect"] = get_sprite_frames_rect(variation_resource.get(i))
  40. if variation_resource.get(i) is AtlasTexture:
  41. var rect: Rect2 = variation_resource.get(i).region
  42. value["rect"] = [rect.position.x, rect.position.y, rect.size.x, rect.size.y]
  43. val_dict[i] = value
  44. else:
  45. val_dict[i] = get_variation_values(variation_resource.get(i))
  46. if i == "Overworld":
  47. val_dict["default"] = val_dict[i]
  48. if val_dict.size() == 2:
  49. val_dict.erase("Overworld")
  50. return val_dict
  51. func get_value_list(value_resource: Resource) -> Array:
  52. if value_resource is ThemedResource:
  53. return THEMES
  54. if value_resource is CampaignResource:
  55. return CAMPAIGNS
  56. if value_resource is TimedResource:
  57. return ["Day", "Night"]
  58. else:
  59. return []
  60. func get_sprite_frames_rect(sprite_frames: SpriteFrames) -> Array:
  61. var rects := []
  62. for i in sprite_frames.get_animation_names():
  63. for x in sprite_frames.get_frame_count(i):
  64. var region = sprite_frames.get_frame_texture(i, x).region
  65. rects.append(region)
  66. var min_x = INF
  67. var min_y = INF
  68. var max_x = -INF
  69. var max_y = -INF
  70. for rect in rects:
  71. var pos = rect.position
  72. var end = rect.position + rect.size
  73. min_x = min(min_x, pos.x)
  74. min_y = min(min_y, pos.y)
  75. max_x = max(max_x, end.x)
  76. max_y = max(max_y, end.y)
  77. var max_rect = Rect2(Vector2(min_x, min_y), Vector2(max_x - min_x, max_y - min_y))
  78. return [max_rect.position.x, max_rect.position.y, max_rect.size.x, max_rect.size.y]
  79. func get_animations() -> Dictionary:
  80. var dict := {}
  81. var bound_rect = get_sprite_frames_rect(donor_sprite_frames)
  82. for i in donor_sprite_frames.get_animation_names():
  83. var anim_dict = {}
  84. var frame_arr := []
  85. for x in donor_sprite_frames.get_frame_count(i):
  86. var rect: Rect2 = donor_sprite_frames.get_frame_texture(i, x).region
  87. for y in donor_sprite_frames.get_frame_duration(i, x):
  88. frame_arr.append([rect.position.x - bound_rect[0], rect.position.y - bound_rect[1], rect.size.x, rect.size.y])
  89. anim_dict["frames"] = frame_arr
  90. anim_dict["speed"] = donor_sprite_frames.get_animation_speed(i)
  91. anim_dict["loop"] = donor_sprite_frames.get_animation_loop(i)
  92. dict[i] = anim_dict
  93. return dict
  94. func get_source(source_resource: Resource) -> String:
  95. if source_resource is AtlasTexture:
  96. return source_resource.atlas.resource_path.get_file()
  97. if source_resource is Texture2D:
  98. return source_resource.resource_path.get_file()
  99. if source_resource is SpriteFrames:
  100. var texture = source_resource.get_frame_texture(source_resource.get_animation_names()[0], 0)
  101. if texture is AtlasTexture:
  102. return texture.atlas.resource_path.get_file()
  103. else:
  104. return texture.resource_path.get_file()
  105. return ""