Skip to content

Commit

Permalink
Setup the Kraken Universal Context.
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed Aug 8, 2024
1 parent 1ba3254 commit 2a88c4a
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 122 deletions.
11 changes: 6 additions & 5 deletions Sources/Kraken/IO/Hydra/KIO.StageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public extension Kraken.IO
}

stage.reload()
stage.save()
save(&stage)
}

/**
Expand All @@ -152,16 +152,17 @@ public extension Kraken.IO
public func save(_ stage: inout UsdStageRefPtr)
{
/* set the timestamp metadata. */
let metadata = "Kraken v\(Kraken.version) | \(timestamp())"
let time = timestamp()
let metadata = "Kraken v\(Kraken.version) | \(time)"

/* set the metadata on the stage. */
stage.getPseudoRoot().set(doc: metadata)
stage.save()
}

private func timestamp() -> String
public func timestamp() -> String
{
formatter.string(from: Date())
formatter.string(from: Date.now)
}

/**
Expand All @@ -174,7 +175,7 @@ public extension Kraken.IO
* - Parameters:
* - file: The file configuration to check.
* - Returns: Whether the file is binary or not. */
public static func isBinary(_ file: FileDocumentConfiguration<Kraken.IO.USD>) -> Bool
public static func isBinary(_ file: ReferenceFileDocumentConfiguration<Kraken.IO.USD>) -> Bool
{
file.fileURL?.pathExtension != "usda"
}
Expand Down
80 changes: 69 additions & 11 deletions Sources/Kraken/IO/Universal/KIO.USD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,36 @@ import CodeLanguages
import CosmoEditor
import SwiftUI
import UniformTypeIdentifiers
import PixarUSD

public extension Kraken.IO
{
struct USD: FileDocument
@Observable
final class USD: ReferenceFileDocument
{
var text: String
var fileURL: URL?
var fileURL: URL
var stage: UsdStageRefPtr

public init(text: String = "", fileURL: URL? = nil)
public init(text: String = "", fileURL: URL = Kraken.IO.Stage.manager.getTmpURL())
{
self.text = text
self.fileURL = fileURL
self.text = text

if FileManager.default.fileExists(atPath: fileURL.path)
{
self.stage = Usd.Stage.open(fileURL.path)
}
else
{
self.stage = Usd.Stage.createNew(fileURL.path)
}

Kraken.IO.Stage.manager.save(&stage)

var contents = ""
self.stage.exportToString(&contents, addSourceFileComment: false)
self.text = contents
}

public static var readableContentTypes: [UTType]
Expand All @@ -65,26 +83,56 @@ public extension Kraken.IO

public init(configuration: ReadConfiguration) throws
{
let url = Kraken.IO.Stage.manager.getTmpURL()
self.stage = Usd.Stage.open(url.path)
self.fileURL = url
self.text = ""

guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else
{
text = ""
fileURL = nil
var contents: String = ""
self.stage.exportToString(&contents, addSourceFileComment: false)
Kraken.IO.Stage.manager.save(
contentsOfFile: contents,
atPath: url.path,
stage: &stage
)

self.text = contents
return
}
text = string

Kraken.IO.Stage.manager.save(
contentsOfFile: string,
atPath: url.path,
stage: &stage
)

var contents: String = ""
self.stage.exportToString(&contents, addSourceFileComment: false)

self.text = contents
}

public func fileWrapper(snapshot: UsdStageRefPtr, configuration: WriteConfiguration) throws -> FileWrapper
{
var usda = ""
stage.exportToString(&usda, addSourceFileComment: false)
text = usda

return .init(regularFileWithContents: usda.data(using: .utf8)!)
}

public func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper
public func snapshot(contentType: UTType) throws -> UsdStageRefPtr
{
let data = text.data(using: .utf8) ?? "".data(using: .utf8)!
return .init(regularFileWithContents: data)
return self.stage
}
}
}

public extension FileDocumentConfiguration<Kraken.IO.USD>
public extension ReferenceFileDocumentConfiguration<Kraken.IO.USD>
{
/**
* Whether the document is in binary format. */
Expand All @@ -106,3 +154,13 @@ public extension FileDocumentConfiguration<Kraken.IO.USD>
return .detectLanguageFrom(url: url)
}
}

extension Kraken.IO.USD: Equatable
{
public static func == (lhs: Kraken.IO.USD, rhs: Kraken.IO.USD) -> Bool
{
return
lhs.fileURL == rhs.fileURL &&
lhs.text == rhs.text
}
}
87 changes: 10 additions & 77 deletions Sources/Kraken/KR/KR.Kraken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public struct Kraken: SwiftUI.App

/* --- xxx --- */

/** The kraken universal scene description context. */
@State private var C = Kraken.IO.USD()

/* --- xxx --- */

/** The bundle identifier for Kraken. */
public static let identifier = "foundation.wabi.Kraken"

Expand All @@ -55,12 +60,6 @@ public struct Kraken: SwiftUI.App
/** Whether to show the splash screen. */
@State public var showSplash = true

/** The currently opened stage. */
@State private var stage: UsdStageRefPtr = Usd.Stage.createNew(Kraken.IO.Stage.manager.getTmpURL().path, ext: .usda)

/** The currently opened usd file. */
@State private var usdFile = Kraken.IO.Stage.manager.makeTmp()

/* --- xxx --- */

/** An action in the environment that presents a new document. */
Expand All @@ -70,7 +69,7 @@ public struct Kraken: SwiftUI.App

public init()
{
Kraken.IO.Stage.manager.save(&stage)
Kraken.IO.Stage.manager.save(&C.stage)

Msg.logger.info("\(Kraken.versionInfo())")
Msg.logger.info("Kraken launched.")
Expand All @@ -95,76 +94,10 @@ public struct Kraken: SwiftUI.App
{
VStack(spacing: 0)
{
Divider()

HStack(spacing: 0)
{
Button
{} label:
{
Image("wabi.hexagon.fill", bundle: .kraken)
}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("File")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("Edit")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("Render")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("Window")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Spacer()
}
.frame(height: 17)
.padding(4)
.zIndex(2)
.background(.black.opacity(0.3))

Divider()

HStack(spacing: 0)
{
Kraken.UI.CodeEditor(
document: $usdFile,
stage: $stage,
fileURL: usdFile.fileURL
)

Divider()

Kraken.UI.SceneView(
fileURL: usdFile.fileURL ?? Kraken.IO.Stage.manager.getTmpURL()
)
}
createMainMenu()

Kraken.UI.UniversalContext()
.environment(C)
}
}
}
Expand Down
93 changes: 93 additions & 0 deletions Sources/Kraken/KR/KR.Menu.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* --------------------------------------------------------------
* :: : K R A K E N : ::
* --------------------------------------------------------------
* @wabistudios :: metaverse :: kraken
*
* This program is free software; you can redistribute it, and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Check out
* the GNU General Public License for more details.
*
* You should have received a copy for this software license, the
* GNU General Public License along with this program; or, if not
* write to the Free Software Foundation, Inc., to the address of
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (C) 2023 Wabi Foundation.
* All Rights Reserved.
* --------------------------------------------------------------
* . x x x . o o o . x x x . : : : . o x o . : : : .
* -------------------------------------------------------------- */

import Foundation
import KrakenKit
import KrakenLib
import SwiftUI

public extension Kraken
{
@ViewBuilder
func createMainMenu() -> some View
{
Divider()

HStack(spacing: 0)
{
Button
{} label:
{
Image("wabi.hexagon.fill", bundle: .kraken)
}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("File")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("Edit")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("Render")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Button("Window")
{}
.buttonStyle(.borderless)
.font(.system(size: 12, weight: .medium))
.padding(.init(top: 4, leading: 12, bottom: 4, trailing: 12))
.foregroundStyle(.white.opacity(0.7))
.background(RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.clear))

Spacer()
}
.frame(height: 17)
.padding(4)
.zIndex(2)
.background(.black.opacity(0.3))

Divider()
}
}
Loading

0 comments on commit 2a88c4a

Please sign in to comment.