Working Animations

This commit is contained in:
Hariharan Mudaliar 2025-10-11 15:45:58 +05:30
commit b7959b78fc
6 changed files with 126 additions and 69 deletions

View file

@ -13,12 +13,13 @@ import Defaults
struct IdleAnimationView: View {
@Default(.selectedIdleAnimation) var selectedAnimation
@Default(.animationTransformOverrides) var overrides
var body: some View {
Group {
if let animation = selectedAnimation {
AnimationContentView(animation: animation)
.id("\(animation.id)-\(animation.hashValue)") // Force complete recreation when ANY property changes
.id("\(animation.id)-\(overrides[animation.id.uuidString]?.hashValue ?? 0)") // Force recreation when override changes
} else {
// Fallback to original face if nothing selected
MinimalFaceFeatures(height: 20, width: 30)
@ -32,7 +33,11 @@ private struct AnimationContentView: View {
let animation: CustomIdleAnimation
var body: some View {
let config = animation.transformConfig
let config = animation.getTransformConfig()
// Debug logging
let _ = print("🎨 [IdleAnimationView] Rendering animation: \(animation.name)")
let _ = print("🎨 [IdleAnimationView] Config: scale=\(config.scale), offset=(\(config.offsetX), \(config.offsetY)), opacity=\(config.opacity)")
switch animation.source {
case .lottieFile(let url):
@ -72,7 +77,18 @@ private struct AnimationContentView: View {
.clipped()
case .builtInFace:
// Apply transforms to built-in face too!
MinimalFaceFeatures(height: 20, width: 30)
.frame(
width: config.cropWidth * config.scale,
height: config.cropHeight * config.scale
)
.offset(x: config.offsetX, y: config.offsetY)
.rotationEffect(.degrees(config.rotation))
.opacity(config.opacity)
.padding(.bottom, config.paddingBottom)
.frame(width: config.expandWithAnimation ? nil : 30, height: 20)
.clipped()
}
}
}

View file

@ -48,7 +48,9 @@ struct AnimationEditorView: View {
if let existing = existingAnimation {
_name = State(initialValue: existing.name)
_speed = State(initialValue: existing.speed)
let config = existing.transformConfig
// Load from overrides if they exist
let config = Defaults[.animationTransformOverrides][existing.id.uuidString] ?? .default
_scale = State(initialValue: config.scale)
_offsetX = State(initialValue: config.offsetX)
_offsetY = State(initialValue: config.offsetY)
@ -106,17 +108,30 @@ struct AnimationEditorView: View {
.frame(width: 30 * previewScale, height: 20 * previewScale)
// Animation preview with transformations
LottieView(state: LUStateData(
type: .loadedFrom(sourceURL),
speed: speed,
loopMode: loopMode.lottieLoopMode
))
.frame(width: cropWidth * scale * previewScale, height: cropHeight * scale * previewScale)
.offset(x: offsetX * previewScale, y: offsetY * previewScale)
.rotationEffect(.degrees(rotation))
.opacity(opacity)
.padding(.bottom, paddingBottom * previewScale)
.clipped()
if let existing = existingAnimation, case .builtInFace = existing.source {
// Show built-in face
MinimalFaceFeatures(height: 20, width: 30)
.scaleEffect(previewScale)
.frame(width: cropWidth * scale * previewScale, height: cropHeight * scale * previewScale)
.offset(x: offsetX * previewScale, y: offsetY * previewScale)
.rotationEffect(.degrees(rotation))
.opacity(opacity)
.padding(.bottom, paddingBottom * previewScale)
.clipped()
} else {
// Show Lottie animation
LottieView(state: LUStateData(
type: .loadedFrom(sourceURL),
speed: speed,
loopMode: loopMode.lottieLoopMode
))
.frame(width: cropWidth * scale * previewScale, height: cropHeight * scale * previewScale)
.offset(x: offsetX * previewScale, y: offsetY * previewScale)
.rotationEffect(.degrees(rotation))
.opacity(opacity)
.padding(.bottom, paddingBottom * previewScale)
.clipped()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding()
@ -475,18 +490,26 @@ struct AnimationEditorView: View {
loopMode: loopMode
)
// If editing existing animation, create a NEW animation object with updated values
// If editing existing animation, save transform config to overrides
if let existing = existingAnimation {
// Create completely new animation to force view refresh
let updatedAnimation = CustomIdleAnimation(
id: existing.id, // Keep same ID so it replaces the right one
name: name,
source: existing.source,
speed: speed,
isBuiltIn: existing.isBuiltIn,
transformConfig: config
)
animation = updatedAnimation
// Store only the transform config override
var overrides = Defaults[.animationTransformOverrides]
overrides[existing.id.uuidString] = config
Defaults[.animationTransformOverrides] = overrides
print("✅ [AnimationEditor] Saved transform override for: \(existing.name)")
print("✅ [AnimationEditor] Override: \(config)")
// Force view refresh by updating selectedIdleAnimation if this is the selected one
if Defaults[.selectedIdleAnimation]?.id == existing.id {
// Trigger refresh by re-setting the same animation
let current = Defaults[.selectedIdleAnimation]
Defaults[.selectedIdleAnimation] = nil
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
Defaults[.selectedIdleAnimation] = current
}
}
dismiss()
return
}
@ -495,8 +518,7 @@ struct AnimationEditorView: View {
let result = IdleAnimationManager.shared.importLottieFile(
from: sourceURL,
name: name,
speed: speed,
transformConfig: config
speed: speed
)
switch result {

View file

@ -51,16 +51,31 @@ struct IdleAnimationsSettingsSection: View {
selectedForDeletion = animation
showingDeleteAlert = true
},
onEdit: animation.isBuiltIn ? nil : {
onEdit: {
print("🔧 [Edit] Attempting to edit animation: \(animation.name)")
print("🔧 [Edit] Animation source: \(animation.source)")
editingExistingAnimation = animation
if case .lottieFile(let url) = animation.source {
switch animation.source {
case .lottieFile(let url):
print("🔧 [Edit] Lottie file URL: \(url)")
print("🔧 [Edit] File exists: \(FileManager.default.fileExists(atPath: url.path))")
editorSourceURL = url
editorIsRemote = false
showingEditor = true
} else if case .lottieURL(let url) = animation.source {
case .lottieURL(let url):
print("🔧 [Edit] Lottie URL: \(url)")
editorSourceURL = url
editorIsRemote = true
showingEditor = true
case .builtInFace:
print("🔧 [Edit] Built-in face - using dummy URL")
// Use a dummy URL for built-in face (editor only needs URL for preview)
editorSourceURL = URL(string: "builtin://face")!
editorIsRemote = false
showingEditor = true
}
}
)