Skip to content

Commit

Permalink
animate atlas import
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Nov 3, 2024
1 parent 13eef9c commit 6837de5
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 131 deletions.
39 changes: 21 additions & 18 deletions source/funkin/ui/debug/char/animate/CharSelectAtlasSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,37 @@ class CharSelectAtlasSprite extends FlxAnimate

var canPlayOtherAnims:Bool = true;

public function new(x:Float, y:Float, ?zipBytes:haxe.io.Bytes = null, ?settings:Settings)
public function new(x:Float, y:Float, ?zipBytes:haxe.io.Bytes = null, ?assetPath:String = null, ?settings:Settings)
{
if (settings == null) settings = SETTINGS;

super(x, y, null, settings);
super(x, y, assetPath, settings);

var animData:String = "";
var spritemapArray:Array<String> = [];
var imageMap:Map<String, BitmapData> = [];
if (assetPath == null)
{
var animData:String = "";
var spritemapArray:Array<String> = [];
var imageMap:Map<String, BitmapData> = [];

var zipFiles = funkin.util.FileUtil.readZIPFromBytes(zipBytes);
if (zipFiles.length == 0) return;
var zipFiles = funkin.util.FileUtil.readZIPFromBytes(zipBytes);
if (zipFiles.length == 0) return;

for (file in zipFiles)
{
if (file.fileName.indexOf("/") != -1) file.fileName = haxe.io.Path.withoutDirectory(file.fileName);
for (file in zipFiles)
{
if (file.fileName.indexOf("/") != -1) file.fileName = haxe.io.Path.withoutDirectory(file.fileName);

if (file.fileName.indexOf("Animation.json") != -1) animData = CharCreatorUtil.normalizeJSONText(file.data.toString());
if (file.fileName.indexOf("Animation.json") != -1) animData = CharCreatorUtil.normalizeJSONText(file.data.toString());

if (file.fileName.startsWith("spritemap")
&& file.fileName.endsWith(".json")) spritemapArray.push(CharCreatorUtil.normalizeJSONText(file.data.toString()));
if (file.fileName.startsWith("spritemap")
&& file.fileName.endsWith(".png")) imageMap.set(file.fileName, BitmapData.fromBytes(file.data));
}
if (file.fileName.startsWith("spritemap")
&& file.fileName.endsWith(".json")) spritemapArray.push(CharCreatorUtil.normalizeJSONText(file.data.toString()));
if (file.fileName.startsWith("spritemap")
&& file.fileName.endsWith(".png")) imageMap.set(file.fileName, BitmapData.fromBytes(file.data));
}

if (animData == "" || spritemapArray.length == 0 || imageMap.keys().array().length == 0) return;
if (animData == "" || spritemapArray.length == 0 || imageMap.keys().array().length == 0) return;

this.loadSeparateAtlas(animData, CharSelectAnimateFrames.fromTextureAtlas(spritemapArray, imageMap));
this.loadSeparateAtlas(animData, CharSelectAnimateFrames.fromTextureAtlas(spritemapArray, imageMap));
}

onAnimationComplete.add(cleanupAnimation);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package funkin.ui.debug.char.handlers;

import haxe.io.Path;
import funkin.data.character.CharacterRegistry;
import funkin.ui.debug.char.pages.CharCreatorGameplayPage;
import funkin.ui.debug.char.CharCreatorState;
import funkin.util.FileUtil;
Expand All @@ -10,7 +11,20 @@ using StringTools;
@:access(funkin.ui.debug.char.CharCreatorState)
class CharCreatorImportExportHandler
{
public static function importCharacter(state:CharCreatorState):Void {}
public static function importCharacter(state:CharCreatorState, charId:String):Void
{
var gameplayPage:CharCreatorGameplayPage = cast state.pages[Gameplay];

var data = CharacterRegistry.fetchCharacterData(charId);

if (data == null)
{
trace('No character data for $charId (CharCreatorImportExportHandler.importCharacter)');
return;
}

gameplayPage.currentCharacter.fromCharacterData(CharacterRegistry.fetchCharacterData(charId));
}

public static function exportCharacter(state:CharCreatorState):Void
{
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/ui/debug/char/import.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funkin.ui.debug.char;

import funkin.ui.debug.char.util.CharCreatorUtil;
import funkin.ui.debug.char.util.GhostUtil;

#if !macro
using funkin.ui.debug.char.handlers.CharCreatorStartupWizard;
using funkin.ui.debug.char.handlers.CharCreatorImportExportHandler;
using funkin.ui.debug.char.util.CharacterUtil;
#end
4 changes: 2 additions & 2 deletions source/funkin/ui/debug/char/pages/CharCreatorGameplayPage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ class CharCreatorGameplayPage extends CharCreatorDefaultPage

if (ghostId == "")
{
GhostUtil.copyFromCharacter(ghostCharacter, currentCharacter);
ghostCharacter.fromCharacter(currentCharacter);
}
else
{
var data:CharacterData = CharacterRegistry.fetchCharacterData(ghostId);
if (data == null) return ghostId;

GhostUtil.copyFromCharacterData(ghostCharacter, data);
ghostCharacter.fromCharacterData(data);
}

refreshGhoulAnims();
Expand Down
122 changes: 122 additions & 0 deletions source/funkin/ui/debug/char/util/CharacterUtil.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package funkin.ui.debug.char.util;

import flixel.graphics.frames.FlxAtlasFrames;
import openfl.display.BitmapData;
import funkin.ui.debug.char.animate.CharSelectAtlasSprite;
import funkin.data.character.CharacterData;

@:access(funkin.ui.debug.char.CharCreatorCharacter)
class CharacterUtil
{
public static function fromCharacter(char:CharCreatorCharacter, other:CharCreatorCharacter):Void
{
char.generatedParams = other.generatedParams;
char.animations = [];
char.atlasCharacter = null;
char.loadGraphic(null); // should remove all the frames and animations i think

switch (other.renderType)
{
case CharacterRenderType.Sparrow | CharacterRenderType.MultiSparrow:
if (char.generatedParams.files.length < 2) return; // img and data

var combinedFrames = null;
for (i in 0...Math.floor(char.generatedParams.files.length / 2))
{
var img = BitmapData.fromBytes(char.generatedParams.files[i * 2].bytes);
var data = char.generatedParams.files[i * 2 + 1].bytes.toString();
var sparrow = FlxAtlasFrames.fromSparrow(img, data);
if (combinedFrames == null) combinedFrames = sparrow;
else
combinedFrames.addAtlas(sparrow);
}
char.frames = combinedFrames;

case CharacterRenderType.Packer:
if (char.generatedParams.files.length != 2) return; // img and data

var img = BitmapData.fromBytes(char.generatedParams.files[0].bytes);
var data = char.generatedParams.files[1].bytes.toString();
char.frames = FlxAtlasFrames.fromSpriteSheetPacker(img, data);

case CharacterRenderType.AnimateAtlas: // todo
if (char.generatedParams.files.length != 1) return; // zip file with all the data
char.atlasCharacter = new CharSelectAtlasSprite(0, 0, char.generatedParams.files[0].bytes);

char.atlasCharacter.alpha = 0.0001;
char.atlasCharacter.draw();
char.atlasCharacter.alpha = 1.0;

char.atlasCharacter.x = char.x;
char.atlasCharacter.y = char.y;
char.atlasCharacter.alpha *= char.alpha;
char.atlasCharacter.flipX = char.flipX;
char.atlasCharacter.flipY = char.flipY;
char.atlasCharacter.scrollFactor.copyFrom(char.scrollFactor);
char.atlasCharacter.cameras = char._cameras; // _cameras instead of cameras because get_cameras() will not return null

default: // nothing, what the fuck are you even doing
}

char.globalOffsets = other.globalOffsets.copy();

for (anim in other.animations)
{
char.addAnimation(anim.name, anim.prefix, anim.offsets, anim.frameIndices, anim.frameRate, anim.looped, anim.flipX, anim.flipY);
}
}

public static function fromCharacterData(char:CharCreatorCharacter, data:CharacterData):Void
{
// char.generatedParams = other.generatedParams;
char.animations = [];
char.atlasCharacter = null;
char.loadGraphic(null); // should remove all the frames and animations i think

switch (data.renderType)
{
case CharacterRenderType.Sparrow | CharacterRenderType.MultiSparrow:
var combinedFrames = null;
for (i => assetPath in data.assetPaths)
{
if (combinedFrames == null) combinedFrames = Paths.getSparrowAtlas(assetPath);
else
combinedFrames.addAtlas(Paths.getSparrowAtlas(assetPath));
}
char.frames = combinedFrames;

case CharacterRenderType.Packer:
char.frames = Paths.getPackerAtlas(data.assetPaths[0]);

case CharacterRenderType.AnimateAtlas:
var animLibrary:String = Paths.getLibrary(data.assetPaths[0]);
var animPath:String = Paths.stripLibrary(data.assetPaths[0]);
var assetPath:String = Paths.animateAtlas(animPath, animLibrary);

char.atlasCharacter = new CharSelectAtlasSprite(0, 0, assetPath);
char.atlasCharacter.alpha = 0.0001;
char.atlasCharacter.draw();
char.atlasCharacter.alpha = 1.0;

char.atlasCharacter.x = char.x;
char.atlasCharacter.y = char.y;
char.atlasCharacter.alpha *= char.alpha;
char.atlasCharacter.flipX = char.flipX;
char.atlasCharacter.flipY = char.flipY;
char.atlasCharacter.scrollFactor.copyFrom(char.scrollFactor);
char.atlasCharacter.cameras = char._cameras; // _cameras instead of cameras because get_cameras() will not return null

default: // nuthin
}

char.globalOffsets = data.offsets ?? [0, 0];
char.characterFlipX = data.flipX ?? false;
char.characterScale = data.scale ?? 1;

for (anim in data.animations)
{
char.addAnimation(anim.name, anim.prefix, anim.offsets, anim.frameIndices ?? [], anim.frameRate ?? 24, anim.looped ?? false, anim.flipX ?? false,
anim.flipY ?? false);
}
}
}
109 changes: 0 additions & 109 deletions source/funkin/ui/debug/char/util/GhostUtil.hx

This file was deleted.

0 comments on commit 6837de5

Please sign in to comment.