Blender4iOS/Sources/Workspace/WorkspaceManager.swift

173 lines
5.3 KiB
Swift
Raw Normal View History

// WorkspaceManager.swift
// Controls which workspace layout is active and panel sizing/visibility.
import Observation
import SwiftUI
// MARK: - Workspace definitions
enum WorkspaceType: String, CaseIterable, Identifiable {
case modeling = "Modeling"
case uvTexture = "UV / Texture Bake"
case greasePencil = "Grease Pencil"
case animation = "Animation"
case rendering = "Rendering"
case sculpting = "Sculpting"
var id: String { rawValue }
var icon: String {
switch self {
case .modeling: "cube"
case .uvTexture: "square.grid.3x3.topleft.filled"
case .greasePencil: "pencil.tip"
case .animation: "play.rectangle"
case .rendering: "sun.max.fill"
case .sculpting: "paintbrush.pointed.fill"
}
}
/// Default panel configuration for each workspace.
var defaultPanels: [PanelSlot] {
switch self {
case .modeling:
[
.init(.toolSidebar, edge: .leading, size: 0.04),
.init(.viewport3D, edge: .center, size: 0.66),
.init(.outliner, edge: .trailing, size: 0.15),
.init(.properties, edge: .trailing, size: 0.15),
]
case .uvTexture:
[
.init(.toolSidebar, edge: .leading, size: 0.04),
.init(.viewport3D, edge: .center, size: 0.40),
.init(.uvEditor, edge: .center, size: 0.36),
.init(.textureBake, edge: .trailing, size: 0.20),
]
case .greasePencil:
[
.init(.brushPanel, edge: .leading, size: 0.06),
.init(.gpCanvas, edge: .center, size: 0.64),
.init(.gpLayers, edge: .trailing, size: 0.16),
.init(.onionSkin, edge: .trailing, size: 0.14),
]
case .animation:
[
.init(.toolSidebar, edge: .leading, size: 0.04),
.init(.viewport3D, edge: .center, size: 0.70),
.init(.outliner, edge: .trailing, size: 0.12),
.init(.properties, edge: .trailing, size: 0.14),
]
case .rendering:
[
.init(.viewport3D, edge: .center, size: 0.70),
.init(.renderSettings, edge: .trailing, size: 0.30),
]
case .sculpting:
[
.init(.brushPanel, edge: .leading, size: 0.06),
.init(.viewport3D, edge: .center, size: 0.76),
.init(.properties, edge: .trailing, size: 0.18),
]
}
}
}
// MARK: - Panel identity
enum PanelID: String, Identifiable, CaseIterable {
case toolSidebar = "Tools"
case viewport3D = "3D Viewport"
case uvEditor = "UV Editor"
case textureBake = "Texture Bake"
case brushPanel = "Brushes"
case gpCanvas = "GP Canvas"
case gpLayers = "Layers"
case onionSkin = "Onion Skin"
case outliner = "Outliner"
case properties = "Properties"
case renderSettings = "Render"
case timeline = "Timeline"
var id: String { rawValue }
}
enum PanelEdge {
case leading, center, trailing, bottom
}
struct PanelSlot: Identifiable {
let id: PanelID
let edge: PanelEdge
var size: CGFloat // proportion of total width (01)
var isVisible: Bool = true
var minSize: CGFloat = 0.04
init(_ id: PanelID, edge: PanelEdge, size: CGFloat, minSize: CGFloat = 0.04) {
self.id = id
self.edge = edge
self.size = size
self.minSize = minSize
}
}
// MARK: - Manager
@Observable
final class WorkspaceManager {
var activeWorkspace: WorkspaceType {
didSet { panels = activeWorkspace.defaultPanels }
}
var panels: [PanelSlot]
// Bottom panels (shared across workspaces)
var showTimeline: Bool = false
var timelineHeight: CGFloat = 0.2
init(workspace: WorkspaceType = .modeling) {
self.activeWorkspace = workspace
self.panels = workspace.defaultPanels
}
// MARK: - Panel queries
func panel(_ id: PanelID) -> PanelSlot? {
panels.first { $0.id == id }
}
var leadingPanels: [PanelSlot] {
panels.filter { $0.edge == .leading && $0.isVisible }
}
var centerPanels: [PanelSlot] {
panels.filter { $0.edge == .center && $0.isVisible }
}
var trailingPanels: [PanelSlot] {
panels.filter { $0.edge == .trailing && $0.isVisible }
}
// MARK: - Panel mutation
func togglePanel(_ id: PanelID) {
guard let idx = panels.firstIndex(where: { $0.id == id }) else { return }
panels[idx].isVisible.toggle()
}
func resizePanel(_ id: PanelID, to newSize: CGFloat) {
guard let idx = panels.firstIndex(where: { $0.id == id }) else { return }
panels[idx].size = max(panels[idx].minSize, min(newSize, 0.8))
}
func switchWorkspace(to type: WorkspaceType) {
withAnimation(.easeInOut(duration: 0.25)) {
activeWorkspace = type
Animation Data Model Mirrors Blender's internal structure exactly: Keyframe — stores frame, value, handleLeft/handleRight (absolute SIMD2 coordinates for Bézier control points), handleType (Free/Aligned/Vector/Auto/Auto Clamped), and interpolation mode (Constant/Linear/Bézier/Bounce/Elastic/Back). The autoComputeHandles() method computes smooth tangents from neighboring keyframes, matching Blender's BKE_fcurve_correct_bezpart. moveHandleLeft/moveHandleRight enforce the Aligned constraint (co-linear handles). AnimationTrack (= Blender's FCurve) — one channel like location.X. Contains a sorted keyframe array. The evaluate(at:) method performs full cubic Bézier interpolation using Newton-Raphson iteration (8 steps, matching Blender's precision) to solve for the parametric t given a frame x-coordinate, then evaluates the y-coordinate. Colors follow Blender's convention: X=red, Y=green, Z=blue. AnimAction (= Blender's bAction) — groups multiple tracks into a reusable animation data block. evaluate(at:) returns all channel values as a dictionary. AnimationSystem — owns the action library, playback state (currentFrame, fps, loop mode), and applyToObject() to push evaluated transforms onto a SceneObject. Dope Sheet (DopeSheetView.swift) Faithful to Blender's Dope Sheet layout: Left channel list with colored track indicators (red/green/blue bars), mute (speaker icon) and lock buttons per channel Summary track at top showing all keyframes merged as yellow diamonds Per-channel tracks with diamond keyframes (yellow = normal, white = selected) Frame ruler at top with major ticks every 10 frames, minor every 5, and frame number labels Blue playhead line with triangular marker — tap the ruler to scrub Keyframe interaction: tap to select, drag to move (snaps to integer frames), auto-recomputes handles after drag Pinch to zoom the frame axis, horizontal drag to scroll Graph Editor (GraphEditorView.swift) The most complex view — a full interactive F-Curve editor: Adaptive grid with auto-scaling step sizes (1/2/5/10 pattern), axis lines highlighted, frame numbers on bottom, value numbers on left F-Curve rendering as cubic Bézier paths using SwiftUI's addCurve(to:control1:control2:), with flat extensions before/after the first/last keyframes. Constant interpolation draws step functions, Linear draws straight lines. Keyframe diamonds colored per-channel (red/green/blue), white when selected Bézier handles — visible only on selected keyframes, drawn as circles connected to the diamond by lines. Supports: Drag the diamond to move the keyframe (frame snaps to integer, value is free) Drag the left handle to change the incoming tangent Drag the right handle to change the outgoing tangent Aligned mode: moving one handle mirrors the other Hit testing with 14pt touch radius, prioritizing handles over keyframes over timeline scrubbing Info overlay showing current frame/value coordinates during drag Channel list overlay with per-track visibility toggles Transport Controls (AnimationPanelView.swift) Matches Blender's timeline footer pixel-for-pixel: Editor switcher — segmented control toggling between Dope Sheet and Graph Editor Action name display with film icon Interpolation menu — set selected keyframes to Constant/Linear/Bézier/Bounce/Elastic/Back Handle type menu — set selected handles to Free/Aligned/Vector/Auto/Auto Clamped Insert keyframe button (yellow diamond icon) Transport bar: Jump to Start, Previous Keyframe, Step Back, Play/Pause (highlighted blue when playing), Step Forward, Next Keyframe, Jump to End Frame range display (Start/End) Current frame in blue monospaced bold FPS indicator and loop toggle Playback Engine (AnimationPlayer.swift) CADisplayLink-driven with preferredFrameRateRange set to 30–120fps. Delta-time accumulation advances currentFrame at the configured FPS rate. nextKeyframe()/previousKeyframe() jump between actual keyframe positions. To see it: tap the Animation workspace tab, or tap the play icon in any workspace header to toggle the timeline panel. The default "CubeAction" ships with sample keyframes on all three location channels so you'll see curves immediately in the Graph Editor. Tap a keyframe diamond, then drag its Bézier handles to reshape the easing.
2026-04-10 21:52:58 -07:00
// Animation workspace shows a larger timeline by default
if type == .animation {
showTimeline = true
timelineHeight = 0.45
}
}
}
}