Skip to content

Commit

Permalink
More Swift means fewer helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cochrane committed May 5, 2024
1 parent 78af1ff commit b9cf36c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 73 deletions.
4 changes: 4 additions & 0 deletions GLLara.xcodeproj/project.pbxproj
Expand Up @@ -231,6 +231,7 @@
5272709C2BE77A7D00EE52B5 /* GLLTexture.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5272709A2BE600C300EE52B5 /* GLLTexture.swift */; };
527270A32BE7E0F100EE52B5 /* GLLItem+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 527270A12BE7E0F100EE52B5 /* GLLItem+Extensions.swift */; };
527270A52BE8064100EE52B5 /* GLLDataReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52C3AD8D29A2241E002EC334 /* GLLDataReader.swift */; };
527270A72BE810A600EE52B5 /* GLLItemMesh+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 527270A62BE810A600EE52B5 /* GLLItemMesh+Extensions.swift */; };
5274446427FCC9C100E5A3FD /* GLLModelMesh.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5274446327FCC9C100E5A3FD /* GLLModelMesh.swift */; };
5274446627FD64F000E5A3FD /* GLLModelMeshObj.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5274446527FD64F000E5A3FD /* GLLModelMeshObj.swift */; };
5274446827FD6F7F00E5A3FD /* GLLVertexAttribAccessorSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5274446727FD6F7F00E5A3FD /* GLLVertexAttribAccessorSet.swift */; };
Expand Down Expand Up @@ -672,6 +673,7 @@
526E0F9B1C169E3500F198BF /* testStaticTRUDiffuseLightmap.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = testStaticTRUDiffuseLightmap.png; sourceTree = "<group>"; };
5272709A2BE600C300EE52B5 /* GLLTexture.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLTexture.swift; sourceTree = "<group>"; };
527270A12BE7E0F100EE52B5 /* GLLItem+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GLLItem+Extensions.swift"; sourceTree = "<group>"; };
527270A62BE810A600EE52B5 /* GLLItemMesh+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GLLItemMesh+Extensions.swift"; sourceTree = "<group>"; };
5274446327FCC9C100E5A3FD /* GLLModelMesh.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLModelMesh.swift; sourceTree = "<group>"; };
5274446527FD64F000E5A3FD /* GLLModelMeshObj.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLModelMeshObj.swift; sourceTree = "<group>"; };
5274446727FD6F7F00E5A3FD /* GLLVertexAttribAccessorSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLVertexAttribAccessorSet.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1371,6 +1373,7 @@
5214470916DBF206003E260F /* GLLItemMesh+MeshExport.swift */,
5214470C16DC2312003E260F /* GLLItem+MeshExport.swift */,
521102EE2899C430001BE4BC /* GLLItemBoneExtensions.swift */,
527270A62BE810A600EE52B5 /* GLLItemMesh+Extensions.swift */,
);
name = "Scene members";
sourceTree = "<group>";
Expand Down Expand Up @@ -1910,6 +1913,7 @@
527270A32BE7E0F100EE52B5 /* GLLItem+Extensions.swift in Sources */,
52D3D72916029BD9006CB743 /* GLLRenderAccessoryViewController.m in Sources */,
52CE280C1605FE98005A86E9 /* GLLLogarithmicValueTransformer.m in Sources */,
527270A72BE810A600EE52B5 /* GLLItemMesh+Extensions.swift in Sources */,
5275F6821607293000978779 /* GLLItemController.m in Sources */,
52C516FF2871998C000EB8C2 /* GLLPipelineStateInformation.swift in Sources */,
5275F6891607399800978779 /* GLLItemListController.m in Sources */,
Expand Down
37 changes: 37 additions & 0 deletions GLLara/GLLItemMesh+Extensions.swift
@@ -0,0 +1,37 @@
//
// GLLItemMesh+Extensions.swift
// GLLara
//
// Created by Torsten Kammer on 05.05.24.
// Copyright © 2024 Torsten Kammer. All rights reserved.
//

import Foundation

extension GLLItemMesh {
@objc func renderParameter(name: String) -> GLLRenderParameter? {
return renderParameters.first { $0.name == name }
}

@objc func texture(identifier: String) -> GLLItemMeshTexture? {
return textures.first { $0.identifier == identifier }
}

@objc var isUsingBlending: Bool {
get {
if isCustomBlending {
return isBlended
} else {
return mesh.usesAlphaBlending
}
}
set {
isCustomBlending = true
isBlended = newValue
}
}

@objc var mesh: GLLModelMesh {
return item.model.meshes[Int(meshIndex)]
}
}
4 changes: 2 additions & 2 deletions GLLara/GLLItemMesh+MeshExport.swift
Expand Up @@ -43,15 +43,15 @@ extension GLLItemMesh {

// 3 - write required parameters
for parameterNames in shaderDescription.parameterUniformsInOrder {
let param = renderParameter(withName: parameterNames[0]) as! GLLFloatRenderParameter
let param = renderParameter(name: parameterNames[0]) as! GLLFloatRenderParameter
name += "_\(param.value)"
}

return name
}

private func textureUrls(description: XnaLaraShaderDescription) -> [URL] {
return description.textureUniformsInOrder.map { texture(withIdentifier: $0).textureURL! as URL }
return description.textureUniformsInOrder.map { texture(identifier: $0)!.textureURL! as URL }
}

func writeASCII() throws -> String {
Expand Down
5 changes: 0 additions & 5 deletions GLLara/GLLItemMesh.h
Expand Up @@ -53,11 +53,6 @@
// Called only internally and from child objects, in case some setting changed that requires a shader recompile
- (void)updateShader;

@property (nonatomic) BOOL isUsingBlending;

- (GLLRenderParameter *)renderParameterWithName:(NSString *)parameterName;
- (GLLItemMeshTexture *)textureWithIdentifier:(NSString *)textureIdentifier;

@end

@interface GLLItemMesh (CoreDataGeneratedAccessors)
Expand Down
38 changes: 0 additions & 38 deletions GLLara/GLLItemMesh.m
Expand Up @@ -17,9 +17,6 @@
#import "GLLara-Swift.h"

@interface GLLItemMesh ()
{
__weak GLLModelMesh *underlyingMesh;
}

/*!
* Creates the GLLShaderData object for this mesh, given the current values.
Expand Down Expand Up @@ -48,7 +45,6 @@ @implementation GLLItemMesh
@dynamic mesh;
@dynamic meshIndex;
@dynamic displayName;
@dynamic isUsingBlending;

@synthesize shader;

Expand Down Expand Up @@ -264,40 +260,6 @@ - (NSUInteger)meshIndex
return [self.item.meshes indexOfObject:self];
}

- (GLLModelMesh *)mesh
{
if (!underlyingMesh)
underlyingMesh = self.item.model.meshes[self.meshIndex];
return underlyingMesh;
}

- (GLLRenderParameter *)renderParameterWithName:(NSString *)parameterName;
{
return [self.renderParameters anyObjectMatching:^BOOL(GLLRenderParameter *parameter){
return [parameter.name isEqual:parameterName];
}];
}
- (GLLItemMeshTexture *)textureWithIdentifier:(NSString *)textureIdentifier;
{
return [self.textures anyObjectMatching:^BOOL(GLLItemMeshTexture *texture){
return [texture.identifier isEqual:textureIdentifier];
}];
}

- (BOOL)isUsingBlending
{
if (self.isCustomBlending)
return self.isBlended;
else
return self.mesh.usesAlphaBlending;
}

- (void)setIsUsingBlending:(BOOL)isUsingBlending
{
self.isCustomBlending = YES;
self.isBlended = isUsingBlending;
}

#pragma mark - Private

- (GLLShaderData *)_createShaderData {
Expand Down
10 changes: 5 additions & 5 deletions GLLara/GLLItemMeshState.swift
Expand Up @@ -117,7 +117,7 @@ class GLLItemMeshState {
}

private func loadTexture(identifier: String) async throws -> GLLTexture {
let textureAssignment = itemMesh.texture(withIdentifier: identifier)
let textureAssignment = itemMesh.texture(identifier: identifier)

if let url = textureAssignment?.textureURL {
// Load from the given URL (where possible
Expand Down Expand Up @@ -156,7 +156,7 @@ class GLLItemMeshState {
}

private func parameterColor(name: String, defaultValue: SIMD4<Float32>) -> SIMD4<Float32> {
guard let parameter = itemMesh.renderParameter(withName: name) else {
guard let parameter = itemMesh.renderParameter(name: name) else {
return defaultValue
}

Expand All @@ -169,7 +169,7 @@ class GLLItemMeshState {
}

private func parameterFloat(name: String, defaultValue: Float32) -> Float32 {
guard let parameter = itemMesh.renderParameter(withName: name) else {
guard let parameter = itemMesh.renderParameter(name: name) else {
return defaultValue
}

Expand Down Expand Up @@ -240,7 +240,7 @@ class GLLItemMeshState {
}

private func displayUrl(for identifier: String) -> URL? {
let textureAssignment = itemMesh.texture(withIdentifier: identifier)
let textureAssignment = itemMesh.texture(identifier: identifier)

if let url = textureAssignment?.textureURL {
// Use URL in model
Expand Down Expand Up @@ -301,7 +301,7 @@ class GLLItemMeshState {
var texCoordAssignments: [Int: Int] = [:]
for identifier in shader.textureUniforms {
let index = textureIndex(for: identifier)
if let assignment = itemMesh.texture(withIdentifier: identifier) {
if let assignment = itemMesh.texture(identifier: identifier) {
if (assignment.texCoordSet < 0) {
if let texture = itemMesh.mesh.textures[identifier], texture.texCoordSet > 0 {
texCoordAssignments[index] = min(texture.texCoordSet, itemMesh.mesh.countOfUVLayers - 1)
Expand Down
1 change: 1 addition & 0 deletions GLLara/GLLRenderParameterSelectionPlaceholder.m
Expand Up @@ -10,6 +10,7 @@

#import "GLLItemMesh.h"
#import "GLLRenderParameter.h"
#import "GLLara-Swift.h"

@interface GLLRenderParameterSelectionPlaceholder ()

Expand Down
3 changes: 0 additions & 3 deletions GLLara/NSArray+Map.h
Expand Up @@ -13,14 +13,12 @@
- (NSArray *)map:(id (^)(ObjectType))block;
- (NSMutableArray *)mapMutable:(id (^)(ObjectType))block;
- (NSArray *)mapAndJoin:(NSArray *(^)(id))block;
- (ObjectType)firstObjectMatching:(BOOL(^)(ObjectType))predicate;

@end

@interface NSOrderedSet<__covariant ObjectType> (Map)

- (NSArray *)map:(id (^)(ObjectType))block;
- (ObjectType)firstObjectMatching:(BOOL(^)(ObjectType))predicate;

@end

Expand All @@ -34,7 +32,6 @@
@interface NSSet<__covariant ObjectType> (Map)

- (NSArray *)map:(id (^)(ObjectType))block;
- (ObjectType)anyObjectMatching:(BOOL(^)(ObjectType))predicate;

@end

Expand Down
20 changes: 0 additions & 20 deletions GLLara/NSArray+Map.m
Expand Up @@ -8,14 +8,6 @@

#import "NSArray+Map.h"

static id firstObjectMatching(id<NSFastEnumeration> collection, BOOL(^predicate)(id))
{
for (id object in collection)
if (predicate(object))
return object;
return nil;
}

static id mapMutable(id<NSFastEnumeration> collection, NSUInteger count, id(^function)(id))
{
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:count];
Expand Down Expand Up @@ -58,10 +50,6 @@ - (NSArray *)mapAndJoin:(NSArray *(^)(id))block;

return result;
}
- (id)firstObjectMatching:(BOOL(^)(id))predicate;
{
return firstObjectMatching(self, predicate);
}

@end

Expand All @@ -71,10 +59,6 @@ - (NSArray *)map:(id (^)(id))block;
{
return map(self, self.count, block);
}
- (id)firstObjectMatching:(BOOL(^)(id))predicate;
{
return firstObjectMatching(self, predicate);
}

@end

Expand All @@ -84,10 +68,6 @@ - (NSArray *)map:(id (^)(id))block;
{
return map(self, self.count, block);
}
- (id)anyObjectMatching:(BOOL(^)(id))predicate;
{
return firstObjectMatching(self, predicate);
}

@end

Expand Down

0 comments on commit b9cf36c

Please sign in to comment.