-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export enumeration support Any enumeration that is blessed with `CaseIterable` can be then exported to Godot by using `@Export(.enum)` on it, like this: ``` enum MyEnumeration: Int, CaseIterable { case first case second } ``` To export, use the `.enum` parameter to Export: ``` @godot class Demo: Node { @export(.enum) var myState: MyEnumeration } ``` One limitation of the current change is that this works by using `CaseIterable`, either by manually typing it, or using the `PickerNameProvider` macro.
- Loading branch information
1 parent
e7e8de3
commit 04e47f5
Showing
8 changed files
with
228 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
Tests/SwiftGodotMacrosTests/MacroGodotExportEnumTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// MacroGodotExportEnumTests.swift | ||
// SwiftGodotMacrosTests | ||
// | ||
// Created by Estevan Hernandez on 11/29/23. | ||
// | ||
|
||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
import SwiftGodotMacroLibrary | ||
|
||
final class MacroGodotExportEnumTests: XCTestCase { | ||
let testMacros: [String: Macro.Type] = [ | ||
"Godot": GodotMacro.self, | ||
"Export": GodotExport.self, | ||
] | ||
|
||
func testExportEnumGodot() { | ||
assertMacroExpansion( | ||
""" | ||
enum Demo: Int, CaseIterable { | ||
case first | ||
} | ||
enum Demo64: Int64, CaseIterable { | ||
case first | ||
} | ||
@Godot | ||
class SomeNode: Node { | ||
@Export(.enum) var demo: Demo | ||
@Export(.enum) var demo64: Demo64 | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
enum Demo: Int, CaseIterable { | ||
case first | ||
} | ||
enum Demo64: Int64, CaseIterable { | ||
case first | ||
} | ||
class SomeNode: Node { | ||
var demo: Demo | ||
func _mproxy_set_demo (args: [Variant]) -> Variant? { | ||
if let iv = Int (args [0]), let ev = Demo(rawValue: numericCast (iv)) { | ||
self.demo = ev | ||
} | ||
return nil | ||
} | ||
func _mproxy_get_demo (args: [Variant]) -> Variant? { | ||
return Variant (demo.rawValue) | ||
} | ||
var demo64: Demo64 | ||
func _mproxy_set_demo64 (args: [Variant]) -> Variant? { | ||
if let iv = Int (args [0]), let ev = Demo64(rawValue: numericCast (iv)) { | ||
self.demo64 = ev | ||
} | ||
return nil | ||
} | ||
func _mproxy_get_demo64 (args: [Variant]) -> Variant? { | ||
return Variant (demo64.rawValue) | ||
} | ||
override open class var classInitializer: Void { | ||
let _ = super.classInitializer | ||
return _initializeClass | ||
} | ||
private static var _initializeClass: Void = { | ||
let className = StringName("SomeNode") | ||
assert(ClassDB.classExists(class: className)) | ||
let classInfo = ClassInfo<SomeNode> (name: className) | ||
let _pdemo = PropInfo ( | ||
propertyType: .int, | ||
propertyName: "demo", | ||
className: className, | ||
hint: .enum, | ||
hintStr: tryCase (Demo.self), | ||
usage: .default) | ||
classInfo.registerMethod (name: "_mproxy_get_demo", flags: .default, returnValue: _pdemo, arguments: [], function: SomeNode._mproxy_get_demo) | ||
classInfo.registerMethod (name: "_mproxy_set_demo", flags: .default, returnValue: nil, arguments: [_pdemo], function: SomeNode._mproxy_set_demo) | ||
classInfo.registerProperty (_pdemo, getter: "_mproxy_get_demo", setter: "_mproxy_set_demo") | ||
let _pdemo64 = PropInfo ( | ||
propertyType: .int, | ||
propertyName: "demo64", | ||
className: className, | ||
hint: .enum, | ||
hintStr: tryCase (Demo64.self), | ||
usage: .default) | ||
classInfo.registerMethod (name: "_mproxy_get_demo64", flags: .default, returnValue: _pdemo64, arguments: [], function: SomeNode._mproxy_get_demo64) | ||
classInfo.registerMethod (name: "_mproxy_set_demo64", flags: .default, returnValue: nil, arguments: [_pdemo64], function: SomeNode._mproxy_set_demo64) | ||
classInfo.registerProperty (_pdemo64, getter: "_mproxy_get_demo64", setter: "_mproxy_set_demo64") | ||
func tryCase <T : RawRepresentable & CaseIterable> (_ type: T.Type) -> GString { | ||
GString (type.allCases.map { v in | ||
"\\(v):\\(v.rawValue)" | ||
} .joined(separator: ",")) | ||
} | ||
func tryCase <T : RawRepresentable> (_ type: T.Type) -> String { | ||
"" | ||
} | ||
} () | ||
} | ||
""", | ||
macros: testMacros | ||
) | ||
} | ||
} |
Oops, something went wrong.