No destructors

This commit is contained in:
Ruslan Mustakov 2017-07-28 00:28:17 +07:00
commit f5769045fc
34 changed files with 1701 additions and 1216 deletions

View file

@ -104,12 +104,6 @@ proc postfix(left: PNode, op: string): PNode =
result.add(ident(op))
result.add(left)
proc newExprColonExpr(key, value: PNode): PNode =
newNode(nkExprColonExpr).addChain(key, value)
proc pragma(key: PNode, value: PNode): PNode =
newNode(nkPragma).addChain(key, value)
proc newBracketExpr(first, bracket: PNode): PNode =
newNode(nkBracketExpr).addChain(first, bracket)
@ -263,9 +257,9 @@ const standardTypes = toSet(
"uint64",
"int64", "float32", "cfloat", "float64", "GodotString", "Vector2", "Rect2",
"Vector3", "Transform2D", "Plane", "Quat", "Rect3", "Basis", "Transform",
"Color", "NodePath", "RID", "Dictionary", "Array", "PoolByteArray",
"PoolIntArray", "PoolRealArray", "PoolStringArray", "PoolVector2Array",
"PoolVector3Array", "PoolColorArray", "Variant"])
"Color", "RID", "NodePath", "Dictionary", "Variant", "Array",
"PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
"PoolVector2Array", "PoolVector3Array", "PoolColorArray"])
const smallIntTypes = toSet(["uint8", "int8", "uint16", "int16", "uint32", "int32",
"cint", "int", "Error"])
@ -277,6 +271,12 @@ const floatTypes = union(float64Types, float32Types)
const arrayTypes = toSet(["Array", "PoolByteArray",
"PoolIntArray", "PoolRealArray", "PoolStringArray", "PoolVector2Array",
"PoolVector3Array", "PoolColorArray"])
const wrapperTypes = union(arrayTypes,
toSet(["NodePath", "Dictionary", "Variant"]))
proc getInternalPtr(varName: PNode, typ: string): PNode =
assert(typ in wrapperTypes)
result = newDotExpr(varName, ident("godot" & typ))
proc newNilCheck(ident, toAssign: PNode): PNode =
newIfStmt(newCall("isNil", ident),
@ -301,7 +301,7 @@ proc makeDefaultValue(arg: MethodArg): PNode =
result = newFloatLit(parseFloat(arg.defaultVal.str))
elif arg.defaultVal.str == "Null":
if arg.typ == "Variant":
result = newCall("variant")
result = newCall("newVariant")
else:
result = newNilLit()
elif arg.typ == "Color":
@ -376,7 +376,8 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
newNode(nkPtrTy).addChain(
newNode(nkBracketExpr).addChain(
ident("array"), ident("MAX_ARG_COUNT"),
if isVarargs: ident("Variant") else: ident("pointer"))))
if isVarargs: newNode(nkPtrTy).addChain(ident("GodotVariant"))
else: ident("pointer"))))
staticArgsLen = if varargsName.isNil: args.len
else: args.len - 1
if not varargsName.isNil:
@ -407,7 +408,10 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
newInfix(ident("idx"), ident("-"), newIntLit(staticArgsLen)))
let argIdx = if arg.isVarargs: ident("idx") else: newIntLit(idx)
let isStandardType = arg.typ in standardTypes
let convArg = if not varargsName.isNil: newCall("toGodot", argName)
let isWrappedType = arg.typ in wrapperTypes
let convArg = if not varargsName.isNil:
getInternalPtr(newCall("toGodot", argName), "Variant")
elif isWrappedType: getInternalPtr(argName, arg.typ)
elif isStandardType: newCall("unsafeAddr", argName)
elif arg.typ == "string": newCall("unsafeAddr",
ident("argToPassToGodot" & $idx))
@ -418,7 +422,7 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
[arg.typ, godotMethodName, typ.godotName])
if not isStandardType and varargsName.isNil:
if arg.typ == "string":
argConversions.add(newNode(nkLetSection).addChain(
argConversions.add(newNode(nkVarSection).addChain(
newIdentDefs(ident("argToPassToGodot" & $idx), newEmptyNode(),
newCall("toGodotString", argName))))
else:
@ -465,11 +469,6 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
body.add(newNode(nkLetSection).addChain(newIdentDefs(
ident("self"), newNode(nkEmpty), varName
)))
if not varargsName.isNil:
body.add(
pragma(ident("push"), newExprColonExpr(
newBracketExpr(ident("warning"), ident("Destructor")),
ident("off"))))
body.add(nilCheck)
body.add(vars)
body.add(argConversions)
@ -482,9 +481,10 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
var isConversionRet: bool
var isObjRet: bool
var isStringRet: bool
var isWrapperRet: bool
let retValIdent = if not isVarargs: ident("ptrCallVal") else: ident(retName)
if not returnType.isNil and varargsName.isNil:
var addrToAssign = newCall("addr", ident("ptrCallVal"))
if not returnType.isNil and not isVarargs:
var addrToAssign = newCall("addr", retValIdent)
if returnType in smallIntTypes:
isConversionRet = true
body.add(newNode(nkVarSection).addChain(newIdentDefs(
@ -495,6 +495,11 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
body.add(newNode(nkVarSection).addChain(newIdentDefs(
retValIdent, ident("float64"),
)))
elif returnType in wrapperTypes:
isWrapperRet = true
body.add(newNode(nkVarSection).addChain(newIdentDefs(
retValIdent, ident("Godot" & returnType),
)))
elif returnType in standardTypes:
addrToAssign = newCall("addr", ident("result"))
elif returnType == "string":
@ -520,9 +525,12 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
ident(retName), newNode(nkPragma).addChain(ident("used"))),
newNode(nkEmpty), theCall))
body.add(callStmt)
# if not returnType.isNil and not varargsName.isNil:
# body.add(newCall("fromGodot", ident("result"), ident(retName)))
# body.add(newCall("deinit", ident(retName)))
if varargsName.isNil:
for idx, arg in args:
if arg.typ == "string":
body.add(newCall("deinit", ident("argToPassToGodot" & $idx)))
if args.len > 0 and not varargsName.isNil:
body.add(freeCall)
if not varargsName.isNil:
@ -536,7 +544,7 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
if isVariantRet:
let convErrDef = newNode(nkLetSection).addChain(
newIdentDefs(ident("convErr"), newNode(nkEmpty),
newCall("fromGodot", ident("result"), retValIdent)))
newCall("fromGodot", ident("result"), newCall("newVariant", retValIdent))))
let convCheck = newIfStmt(
newInfix(ident("convErr"), ident("!="),
newDotExpr(ident("ConversionResult"), ident("OK"))),
@ -545,12 +553,16 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
)
body.add(convErrDef)
body.add(convCheck)
if not isVarargs and (isVariantRet or isStringRet):
# varargs use call with "let" so destructor is invoked
if isStringRet:
body.add(newCall("deinit", retValIdent))
elif isConversionRet:
body.add(newNode(nkAsgn).addChain(ident("result"),
newCall(returnType, retValIdent)))
body.add(newNode(nkAsgn).addChain(
ident("result"),
newCall(returnType, retValIdent)))
elif isWrapperRet:
body.add(newNode(nkAsgn).addChain(
ident("result"),
newCall("new" & returnType, retValIdent)))
elif isObjRet:
body.add(newNode(nkAsgn).addChain(ident("result"),
newCall(newBracketExpr(ident("asNimGodotObject"),
@ -558,11 +570,6 @@ proc doGenerateMethod(tree: PNode, methodBindRegistry: var HashSet[string],
elif isStringRet:
body.add(newNode(nkAsgn).addChain(ident("result"),
newCall("$", retValIdent)))
if not varargsName.isNil:
body.add(
pragma(ident("push"), newExprColonExpr(
newBracketExpr(ident("warning"), ident("Destructor")),
ident("on"))))
let procType = if isVirtual: nkMethodDef
else: nkProcDef
@ -770,7 +777,7 @@ proc genApi*(targetDir: string, apiJsonFile: string) =
let importStmt = newNode(nkImportStmt)
importStmt.add(ident("godot"))
importStmt.add(ident("godottypes"))
importStmt.add(newDotExpr(ident("core"), ident("godotcore")))
importStmt.add(ident("godotinternal"))
tree.add(importStmt)
let exportStmt = newNode(nkExportStmt)
exportStmt.add(ident("godottypes"))

View file

@ -1,135 +1,127 @@
# Copyright (c) 2017 Xored Software, Inc.
# Copyright 2017 Xored Software, Inc.
import hashes
import godotbase, poolarray, variant, strings
import "../internal/godotinternaltypes.nim", "../internal/godotarrays.nim"
proc initArray(dest: var Array) {.
importc: "godot_array_new".}
proc initArray(dest: var Array; pca: PoolColorArray) {.
importc: "godot_array_new_pool_coloarray".}
proc initArray(dest: var Array; pv3a: PoolVector3Array) {.
importc: "godot_array_new_pool_vector3_array".}
proc initArray(dest: var Array; pv2a: PoolVector2Array) {.
importc: "godot_array_new_pool_vector2_array".}
proc initArray(dest: var Array; psa: PoolStringArray) {.
importc: "godot_array_new_pool_string_array".}
proc initArray(dest: var Array; pra: PoolRealArray) {.
importc: "godot_array_new_pool_real_array".}
proc initArray(dest: var Array; pia: PoolIntArray) {.
importc: "godot_array_new_pool_int_array".}
proc initArray(dest: var Array; pba: PoolByteArray) {.
importc: "godot_array_new_pool_byte_array".}
import godotbase, poolarrays
proc initArray*(): Array {.inline.} =
initArray(result)
proc arrayFinalizer(arr: Array) =
arr.godotArray[].deinit()
proc initArray*(pca: PoolColorArray): Array {.inline.} =
initArray(result, pca)
proc newArray*(): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[])
proc initArray*(pv3a: PoolVector3Array): Array {.inline.} =
initArray(result, pv3a)
proc newArray*(arr: GodotArray): Array {.inline.} =
new(result, arrayFinalizer)
result.godotArray[] = arr
proc initArray*(pv2a: PoolVector2Array): Array {.inline.} =
initArray(result, pv2a)
proc newArray*(pca: PoolColorArray): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], pca.godotPoolColorArray[])
proc initArray*(psa: PoolStringArray): Array {.inline.} =
initArray(result, psa)
proc newArray*(pv3a: PoolVector3Array): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], pv3a.godotPoolVector3Array[])
proc initArray*(pra: PoolRealArray): Array {.inline.} =
initArray(result, pra)
proc newArray*(pv2a: PoolVector2Array): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], pv2a.godotPoolVector2Array[])
proc initArray*(pia: PoolIntArray): Array {.inline.} =
initArray(result, pia)
proc newArray*(psa: PoolStringArray): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], psa.godotPoolStringArray[])
proc initArray*(pba: PoolByteArray): Array {.inline.} =
initArray(result, pba)
proc newArray*(pra: PoolRealArray): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], pra.godotPoolRealArray[])
proc `[]=`*(self: var Array; idx: cint;
value: Variant) {.
noSideEffect,
importc: "godot_array_set".}
proc `[]`*(self: Array; idx: cint): Variant {.
noSideEffect,
importc: "godot_array_get".}
proc mget*(self: var Array; idx: cint): ptr Variant {.
importc: "godot_array_operator_index".}
proc newArray*(pia: PoolIntArray): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], pia.godotPoolIntArray[])
proc add*(self: var Array; value: Variant) {.
noSideEffect,
importc: "godot_array_append".}
proc clear*(self: var Array) {.importc: "godot_array_clear".}
proc count*(self: Array; value: Variant): cint {.
noSideEffect,
importc: "godot_array_count".}
proc isEmpty*(self: Array): bool {.
noSideEffect,
importc: "godot_array_empty".}
proc erase*(self: var Array; value: Variant) {.
importc: "godot_array_erase".}
proc first*(self: Array): Variant {.
noSideEffect,
importc: "godot_array_front".}
proc last*(self: Array): Variant {.
noSideEffect,
importc: "godot_array_back".}
proc find*(self: Array; what: Variant;
f: cint): cint {.
noSideEffect,
importc: "godot_array_find".}
proc findLast*(self: Array; what: Variant): cint {.
noSideEffect,
importc: "godot_array_find_last".}
proc contains*(self: Array; value: Variant): bool {.
noSideEffect,
importc: "godot_array_has".}
proc newArray*(pba: PoolByteArray): Array {.inline.} =
new(result, arrayFinalizer)
initGodotArray(result.godotArray[], pba.godotPoolByteArray[])
import variants
proc `[]=`*(self: var Array; idx: int; value: Variant) {.inline.} =
self.godotArray[][idx.cint] = value.godotVariant[]
proc `[]`*(self: Array; idx: int): Variant {.inline.} =
newVariant(self.godotArray[][idx.cint])
proc add*(self: var Array; value: Variant) {.inline.} =
self.godotArray[].add(value.godotVariant[])
proc clear*(self: var Array) {.inline.} =
self.godotArray[].clear()
proc count*(self: Array; value: Variant): int {.inline.} =
self.godotArray[].count(value.godotVariant[]).int
proc isEmpty*(self: Array): bool {.inline.} =
self.godotArray[].isEmpty()
proc erase*(self: var Array; value: Variant) {.inline.} =
self.godotArray[].erase(value.godotVariant[])
proc first*(self: Array): Variant {.inline.} =
newVariant(self.godotArray[].first())
proc last*(self: Array): Variant {.inline.} =
newVariant(self.godotArray[].last())
proc find*(self: Array; what: Variant; f: int): int {.inline.} =
self.godotArray[].find(what.godotVariant[], f.cint).int
proc findLast*(self: Array; what: Variant): int {.inline.} =
self.godotArray[].findLast(what.godotVariant[]).int
proc contains*(self: Array; value: Variant): bool {.inline.} =
self.godotArray[].contains(value.godotVariant[])
proc godotHash*(self: Array): cint {.
noSideEffect,
importc: "godot_array_hash".}
proc hash*(self: Array): Hash {.inline.} =
hash(godotHash(self))
hash(self.godotArray[].godotHash())
proc insert*(self: var Array; pos: cint;
value: Variant) {.
importc: "godot_array_insert".}
proc reverse*(self: var Array) {.importc: "godot_array_invert".}
proc popLast*(self: var Array): Variant {.
importc: "godot_array_pop_back".}
proc popFirst*(self: var Array): Variant {.
importc: "godot_array_pop_front".}
proc addLast*(self: var Array; value: Variant) {.
importc: "godot_array_push_back".}
proc addFirst*(self: var Array; value: Variant) {.
importc: "godot_array_push_front".}
proc insert*(self: var Array; pos: int; value: Variant) {.inline.} =
self.godotArray[].insert(pos.cint, value.godotVariant[])
proc delete*(self: var Array; idx: cint) {.
importc: "godot_array_remove".}
proc setLen*(self: var Array; size: cint) {.
importc: "godot_array_resize".}
proc rfind*(self: Array; what: Variant;
f: cint): cint {.
noSideEffect,
importc: "godot_array_rfind".}
proc len*(self: Array): cint {.
noSideEffect,
importc: "godot_array_size".}
proc sort*(self: var Array) {.importc: "godot_array_sort".}
proc sortCustom*(self: var Array; obj: var GodotObject;
funcName: GodotString) {.
importc: "godot_array_sort_custom".}
proc reverse*(self: var Array) {.inline.} =
self.godotArray[].reverse()
iterator items*(arr: Array): Variant =
proc popLast*(self: var Array): Variant {.inline.} =
newVariant(self.godotArray[].popLast())
proc popFirst*(self: var Array): Variant {.inline.} =
newVariant(self.godotArray[].popFirst())
proc addLast*(self: var Array; value: Variant) {.inline.} =
self.godotArray[].addLast(value.godotVariant[])
proc addFirst*(self: var Array; value: Variant) {.inline.} =
self.godotArray[].addFirst(value.godotVariant[])
proc delete*(self: var Array; idx: int) {.inline.} =
self.godotArray[].delete(idx.cint)
proc setLen*(self: var Array; size: int) {.inline.} =
self.godotArray[].setLen(size.cint)
proc rfind*(self: Array; what: Variant; f: int): int {.inline.} =
self.godotArray[].rfind(what.godotVariant[], f.cint).int
proc len*(self: Array): int {.inline.} =
self.godotArray[].len.int
proc sort*(self: var Array) {.inline.} =
self.godotArray[].sort()
iterator items*(arr: Array): Variant {.inline.} =
for i in 0..<arr.len:
yield arr[i]
iterator mitems*(arr: var Array): var Variant =
for i in 0..<arr.len:
yield arr.mget(i)[]
iterator pairs*(arr: Array): tuple[key: cint, val: Variant] =
iterator pairs*(arr: Array): tuple[key: int, val: Variant] {.inline.} =
for i in 0..<arr.len:
yield (i, arr[i])
iterator mpairs*(arr: var Array): tuple[key: cint, val: var Variant] =
for i in 0..<arr.len:
yield (i, arr.mget(i)[])

View file

@ -1,7 +1,7 @@
# Copyright (c) 2017 Xored Software, Inc.
import math
import godotbase, vector3, quat, strings
import godotbase, vector3, quats
type
Basis* {.byref.} = object

View file

@ -1,13 +1,13 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, strings
import godotbase
type
Color* {.byref.} = object
r: float32
g: float32
b: float32
a: float32
r*: float32
g*: float32
b*: float32
a*: float32
proc initColor*(r, g, b: float32; a: float32 = 1.0'f32): Color {.inline.} =
Color(
@ -38,10 +38,23 @@ proc `$`*(self: Color): string {.inline.} =
result.add(", ")
result.add($self.a)
proc toHtml*(self: Color; with_alpha: bool): GodotString {.
importc: "godot_color_to_html".}
proc toHex(val: float32, target: var string, targetIdx: int) =
let val = clamp(int(val * 255), 0, 255)
let nums = [(val and 0xF0) shr 4, val and 0xF]
for i, num in nums:
target[targetIdx + i] = if num < 10: chr(ord('0') + num)
else: chr(ord('A') + (num - 10))
proc toARGB32*(self: Color): uint32 =
proc toHtml*(self: Color, withAlpha: bool): string =
let size = if withAlpha: 8 else: 6
result = newString(size)
toHex(self.r, result, 0)
toHex(self.g, result, 2)
toHex(self.b, result, 4)
if withAlpha:
toHex(self.b, result, 6)
proc toARGB32*(self: Color): uint32 {.inline.} =
result = uint8(self.a * 255)
result = (result shl 8) or uint8(self.r * 255)
result = (result shl 8) or uint8(self.g * 255)
@ -58,7 +71,7 @@ proc lerp*(self: Color; b: Color; t: float32): Color {.
proc blend*(self: Color; over: Color): Color {.
importc: "godot_color_blend".}
proc `==`*(a, b: Color): bool =
proc `==`*(a, b: Color): bool {.inline.} =
a.r == b.r and a.g == b.g and a.b == b.b and a.a == b.a
proc `<`*(a, b: Color): bool =

View file

@ -0,0 +1,93 @@
# Copyright 2017 Xored Software, Inc.
import hashes
import godotbase
import "../internal/godotinternaltypes.nim", "../internal/godotstrings.nim",
"../internal/godotdictionaries.nim", "../internal/godotvariants.nim"
type
Dictionary* = ref object
godotDictionary: GodotDictionary
proc dictionaryFinalizer(dict: Dictionary) =
dict.godotDictionary.deinit()
proc newDictionary*(): Dictionary {.inline.} =
new(result, dictionaryFinalizer)
initGodotDictionary(result.godotDictionary)
proc newDictionary*(dict: GodotDictionary): Dictionary {.inline.} =
new(result, dictionaryFinalizer)
result.godotDictionary = dict
proc godotDictionary*(dict: Dictionary): ptr GodotDictionary =
## WARNING: do not keep the returned value for longer than the lifetime of
## ``dict``
addr dict.godotDictionary
proc len*(self: Dictionary): int {.inline.} =
self.godotDictionary.len.int
proc isEmpty*(self: Dictionary): bool {.inline.} =
self.godotDictionary.isEmpty()
proc clear*(self: var Dictionary) {.inline.} =
self.godotDictionary.clear()
proc contains*(self: Dictionary; key: Variant): bool {.inline.}=
self.godotDictionary.contains(key.godotVariant[])
proc contains*(self: Dictionary; keys: Array): bool {.inline.} =
self.godotDictionary.contains(keys.godotArray[])
proc del*(self: var Dictionary; key: Variant) {.inline.} =
self.godotDictionary.del(key.godotVariant[])
proc hash*(self: Dictionary): Hash {.inline.} =
hash(self.godotDictionary.godotHash())
import arrays, variants
proc keys*(self: Dictionary): Array {.inline.} =
newArray(self.godotDictionary.keys())
proc values*(self: Dictionary): Array {.inline.} =
newArray(self.godotDictionary.values())
proc `[]`*(self: Dictionary; key: Variant): Variant {.inline.} =
newVariant(self.godotDictionary[key.godotVariant[]])
proc `[]`*(self: Dictionary; keyStr: string): Variant {.inline.} =
var godotVariant: GodotVariant
var godotStr = keyStr.toGodotString()
initGodotVariant(godotVariant, godotStr)
result = newVariant(self.godotDictionary[godotVariant])
godotStr.deinit()
godotVariant.deinit()
proc `[]=`*(self: var Dictionary; key, value: Variant) {.inline.} =
self.godotDictionary[key.godotVariant[]] = value.godotVariant[]
proc `==`*(self, other: Dictionary): bool {.inline.} =
if self.isNil and other.isNil: return true
if self.isNil != other.isNil: return false
result = self.godotDictionary == other.godotDictionary
proc toJson*(self: Dictionary): string {.inline.} =
var s = self.godotDictionary.toJson()
result = $s
s.deinit()
iterator keys*(dict: Dictionary): Variant {.inline.} =
let keyArr = dict.keys()
for key in keyArr:
yield key
iterator values*(dict: Dictionary): Variant {.inline.} =
let valArr = dict.values()
for val in valArr:
yield val
iterator pairs*(dict: Dictionary): tuple[key, val: Variant] {.inline.} =
for key in keys(dict):
yield (key, dict[key])

View file

@ -1,89 +0,0 @@
# Copyright (c) 2017 Xored Software, Inc.
import hashes
import godotbase, arrays, variant, strings
type
Dictionary* {.byref.} = object
p: pointer
proc initVariant(dest: var Variant; dict: Dictionary) {.
importc: "godot_variant_new_dictionary".}
proc variant*(dict: Dictionary): Variant {.inline.} =
initVariant(result, dict)
proc asDictionary*(self: Variant): Dictionary {.
importc: "godot_variant_as_dictionary".}
proc initDictionary(dest: var Dictionary;
src: Dictionary) {.
importc: "godot_dictionary_new_copy".}
proc deinit(self: var Dictionary) {.
importc: "godot_dictionary_destroy".}
proc `=`(self: var Dictionary, other: Dictionary) {.inline.} =
initDictionary(self, other)
proc `=destroy`(self: Dictionary) {.inline.} =
unsafeAddr(self)[].deinit()
proc initDictionary(dest: var Dictionary) {.
importc: "godot_dictionary_new".}
proc initDictionary*(): Dictionary {.inline.} =
initDictionary(result)
proc len*(self: Dictionary): cint {.
importc: "godot_dictionary_size".}
proc isEmpty*(self: Dictionary): bool {.
importc: "godot_dictionary_empty".}
proc clear*(self: var Dictionary) {.
importc: "godot_dictionary_clear".}
proc contains*(self: Dictionary; key: Variant): bool {.
importc: "godot_dictionary_has".}
proc contains*(self: Dictionary; keys: Array): bool {.
importc: "godot_dictionary_has_all".}
proc del*(self: var Dictionary; key: Variant) {.
importc: "godot_dictionary_erase".}
proc godotHash*(self: Dictionary): cint {.
importc: "godot_dictionary_hash".}
proc hash*(self: Dictionary): Hash {.inline.} =
hash(godotHash(self))
proc keys*(self: Dictionary): Array {.
importc: "godot_dictionary_keys".}
proc values*(self: Dictionary): Array {.
importc: "godot_dictionary_values".}
proc `[]`*(self: Dictionary; key: Variant): Variant {.
importc: "godot_dictionary_get".}
proc `[]`*(self: Dictionary; keyStr: string): Variant =
let key = variant(keyStr)
result = self[key]
proc `[]=`*(self: var Dictionary; key, value: Variant) {.
importc: "godot_dictionary_set".}
proc mget*(self: var Dictionary; key: Variant): ptr Variant {.
importc: "godot_dictionary_operator_index".}
proc `==`*(self, other: Dictionary): bool {.
importc: "godot_dictionary_operator_equal".}
proc toJson*(self: Dictionary): GodotString {.
importc: "godot_dictionary_to_json".}
iterator keys*(dict: Dictionary): Variant =
let keyArr = dict.keys()
for key in keyArr:
yield key
iterator values*(dict: Dictionary): Variant =
let valArr = dict.values()
for val in valArr:
yield val
iterator pairs*(dict: Dictionary): tuple[key, val: Variant] =
for key in keys(dict):
yield (key, dict[key])

View file

@ -57,77 +57,10 @@ type
PrinterOnFire, ## the parallel port printer is engulfed in flames
WTF ## shit happens, has never been used, though
const MAX_ARG_COUNT* = 128
type
GodotObject* = object
Array* {.byref.} = object
p: pointer
Variant* {.byref.} = object
data: array[24, byte]
proc initArray(dest: var Array, src: Array) {.
importc: "godot_array_new_copy".}
proc deinit(self: var Array) {.importc: "godot_array_destroy".}
proc `=`(self: var Array, other: Array) {.inline.} =
initArray(self, other)
proc `=destroy`(self: Array) {.inline.} =
unsafeAddr(self).deinit()
proc initVariant(dest: var Variant; src: Variant) {.
importc: "godot_variant_new_copy".}
proc deinit*(self: var Variant) {.
importc: "godot_variant_destroy".}
proc `=`(self: var Variant, other: Variant) {.inline.} =
initVariant(self, other)
proc `=destroy`(self: Variant) {.inline.} =
unsafeAddr(self).deinit()
# System Functions
proc godotAlloc*(bytes: cint): pointer {.
importc: "godot_alloc".}
## Allocates the specified number of bytes.
## Using this instead of stdlib proc will help Godot track how much memory
## is in use in debug mode.
proc godotRealloc*(p: pointer; bytes: cint): pointer {.
importc: "godot_realloc".}
## Reallocates the pointer for the specified number of bytes.
## Using this instead of stdlib proc will help Godot track how much memory
## is in use in debug mode.
proc godotFree*(p: pointer) {.importc: "godot_free".}
## Frees the memory pointed to by the pointer.
## Using this instead of stdlib proc will help Godot track how much memory
## is in use in debug mode.
# print using Godot's error handler list
proc godotPrintError(description: cstring; function: cstring; file: cstring;
line: cint) {.
importc: "godot_print_error".}
proc godotPrintWarning(description: cstring; function: cstring;
file: cstring; line: cint) {.
importc: "godot_print_warning".}
template printWarning*(warn: typed) =
let (filename, line) = instantiationInfo()
godotPrintWarning(cstring($warn), nil, cstring(filename), line.cint)
template printError*(err: typed) =
let (filename, line) = instantiationInfo()
godotPrintError(cstring($err), nil, cstring(filename), line.cint)
# math helpers
const EPSILON = 0.00001'f32
proc isEqualApprox*(a, b: float32): bool {.inline.} =
proc isEqualApprox*(a, b: float32): bool {.inline.} =
abs(a - b) < EPSILON
proc isEqualApprox*(a, b: float64): bool {.inline.} =
@ -150,3 +83,48 @@ proc stepify*(value, step: float32): float32 =
floor(value / step + 0.5'f32) * step
else:
value
import "../internal/godotinternaltypes.nim"
type
Variant* = ref object
godotVariant: GodotVariant
noDeinit: bool # used to avoid copying when passing the variant to Godot
Array* = ref object
godotArray: GodotArray
proc markNoDeinit*(v: Variant) {.inline.} =
## Makes it so that internal GodotVariant object will not be destroyed
## when the reference is gone. Use only if you know what you are doing.
v.noDeinit = true
proc isNoDeinit*(v: Variant): bool {.inline.} =
v.noDeinit
proc godotArray*(arr: Array): ptr GodotArray {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## ``arr``
addr arr.godotArray
proc godotVariant*(v: Variant): ptr GodotVariant {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## ``v``
addr v.godotVariant
# System Functions
proc godotAlloc*(bytes: cint): pointer {.
importc: "godot_alloc".}
## Allocates the specified number of bytes.
## Using this instead of stdlib proc will help Godot track how much memory
## is in use in debug mode.
proc godotRealloc*(p: pointer; bytes: cint): pointer {.
importc: "godot_realloc".}
## Reallocates the pointer for the specified number of bytes.
## Using this instead of stdlib proc will help Godot track how much memory
## is in use in debug mode.
proc godotFree*(p: pointer) {.importc: "godot_free".}
## Frees the memory pointed to by the pointer.
## Using this instead of stdlib proc will help Godot track how much memory
## is in use in debug mode.

View file

@ -1,51 +0,0 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, strings
type
NodePath* {.byref.} = object
p: pointer
proc initNodePath(dest: var NodePath; s: GodotString) {.
importc: "godot_node_path_new".}
proc initNodePath(dest: var NodePath;
src: NodePath) {.
importc: "godot_node_path_new_copy".}
proc deinit(self: var NodePath) {.
importc: "godot_node_path_destroy".}
proc initNodePath*(s: string): NodePath {.inline.} =
initNodePath(result, s.toGodotString())
proc `=destroy`(self: NodePath) {.inline.} =
unsafeAddr(self).deinit()
proc `=`(self: var NodePath, other: NodePath) {.inline.} =
initNodePath(self, other)
proc toGodotString*(self: NodePath): GodotString {.
importc: "godot_node_path_as_string".}
proc `$`*(self: NodePath): string {.inline.} =
let s = self.toGodotString()
result = $s
proc isAbsolute*(self: NodePath): bool {.
importc: "godot_node_path_is_absolute".}
proc nameCount*(self: NodePath): cint {.
importc: "godot_node_path_get_name_count".}
proc getName*(self: NodePath; idx: cint): GodotString {.
importc: "godot_node_path_get_name".}
proc subnameCount*(self: NodePath): cint {.
importc: "godot_node_path_get_subname_count".}
proc getSubname*(self: NodePath; idx: cint): GodotString {.
importc: "godot_node_path_get_subname".}
proc property*(self: NodePath): GodotString {.
importc: "godot_node_path_get_property".}
proc isEmpty*(self: NodePath): bool {.
importc: "godot_node_path_is_empty".}
proc `==`*(a, b: NodePath): bool {.
importc: "godot_node_path_operator_equal".}
converter fromString*(s: string): NodePath =
initNodePath(result, s.toGodotString())

69
godot/core/nodepaths.nim Normal file
View file

@ -0,0 +1,69 @@
# Copyright 2017 Xored Software, Inc.
import godotbase
import "../internal/godotnodepaths.nim", "../internal/godotstrings.nim"
type
NodePath* = ref object
path: GodotNodePath
proc nodePathFinalizer(path: NodePath) =
path.path.deinit()
proc newNodePath*(path: GodotNodePath): NodePath =
## Moves the GodotNodePath into Nim wrapper. The ``path`` will be destroyed
## automatically when the result is no longer referenced.
new(result, nodePathFinalizer)
result.path = path
proc newNodePath*(s: string): NodePath =
new(result, nodePathFinalizer)
var str = s.toGodotString()
initGodotNodePath(result.path, str)
str.deinit()
proc godotNodePath*(path: NodePath): ptr GodotNodePath {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## ``path``
result = addr path.path
proc `$`*(self: NodePath): string {.inline.} =
var s = self.path.toGodotString()
result = $s
s.deinit()
proc isAbsolute*(self: NodePath): bool {.inline.} =
self.path.isAbsolute()
proc nameCount*(self: NodePath): int {.inline.} =
int(self.path.nameCount())
proc getName*(self: NodePath; idx: int): string {.inline.} =
var s = self.path.getName(idx.cint)
result = $s
s.deinit()
proc subnameCount*(self: NodePath): int {.inline.} =
int(self.path.subnameCount())
proc getSubname*(self: NodePath; idx: int): string {.inline.} =
var s = self.path.getSubname(idx.cint)
result = $s
s.deinit()
proc property*(self: NodePath): string {.inline.} =
var s = self.path.property()
result = $s
s.deinit()
proc isEmpty*(self: NodePath): bool {.inline.} =
self.path.isEmpty()
proc `==`*(a, b: NodePath): bool =
if a.isNil and b.isNil: return true
if a.isNil != b.isNil: return false
result = a.path == b.path
converter fromString*(s: string): NodePath =
newNodePath(s)

View file

@ -1,6 +1,8 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, vector3, strings
import godotbase, vector3
import "../internal/godotstrings.nim"
type
Plane* {.byref.} = object
@ -26,6 +28,7 @@ proc initPlane*(normal: Vector3; d: float32): Plane =
proc toGodotString(self: Plane): GodotString {.
noSideEffect,
importc: "godot_plane_as_string".}
proc `$`*(self: Plane): string {.inline.} =
$self.toGodotString()

View file

@ -1,430 +0,0 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, color, vector2, vector3, strings
type
PoolByteArray* {.byref.} = object
p: pointer
PoolIntArray* {.byref.} = object
p: pointer
PoolRealArray* {.byref.} = object
p: pointer
PoolStringArray* {.byref.} = object
p: pointer
PoolVector2Array* {.byref.} = object
p: pointer
PoolVector3Array* {.byref.} = object
p: pointer
PoolColorArray* {.byref.} = object
p: pointer
# byte
proc initPoolByteArray(dest: var PoolByteArray) {.
importc: "godot_pool_byte_array_new".}
proc initPoolByteArray(dest: var PoolByteArray;
src: PoolByteArray) {.
importc: "godot_pool_byte_array_new_copy".}
proc initPoolByteArray(dest: var PoolByteArray; arr: Array) {.
importc: "godot_pool_byte_array_new_with_array".}
proc deinit(self: var PoolByteArray) {.
importc: "godot_pool_byte_array_destroy".}
proc initPoolByteArray*(): PoolByteArray {.inline.} =
initPoolByteArray(result)
proc initPoolByteArray*(arr: Array): PoolByteArray {.inline.} =
initPoolByteArray(result, arr)
proc `=`(self: var PoolByteArray, other: PoolByteArray) {.inline.} =
initPoolByteArray(self, other)
proc `=destroy`(self: PoolByteArray) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolByteArray;
data: uint8) {.
importc: "godot_pool_byte_array_append".}
proc addFirst*(self: var PoolByteArray, data: uint8) {.
importc: "godot_pool_byte_array_push_back".}
proc add*(self: var PoolByteArray;
arr: PoolByteArray) {.
importc: "godot_pool_byte_array_append_array".}
proc insert*(self: var PoolByteArray; idx: cint;
data: uint8): Error {.
importc: "godot_pool_byte_array_insert".}
proc delete*(self: var PoolByteArray; idx: cint) {.
importc: "godot_pool_byte_array_remove".}
proc reverse*(self: var PoolByteArray) {.
importc: "godot_pool_byte_array_invert".}
proc setLen*(self: var PoolByteArray; size: cint) {.
importc: "godot_pool_byte_array_resize".}
proc `[]=`*(self: var PoolByteArray; idx: cint; data: uint8) {.
importc: "godot_pool_byte_array_set".}
proc `[]`*(self: PoolByteArray; idx: cint): uint8 {.
noSideEffect,
importc: "godot_pool_byte_array_get".}
proc len*(self: PoolByteArray): cint {.
noSideEffect,
importc: "godot_pool_byte_array_size".}
iterator items*(arr: PoolByteArray): byte =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolByteArray): tuple[key: cint, val: byte] =
for i in 0..<arr.len:
yield (i, arr[i])
proc initPoolIntArray(dest: var PoolIntArray) {.
importc: "godot_pool_int_array_new".}
proc initPoolIntArray(dest: var PoolIntArray;
src: PoolIntArray) {.
importc: "godot_pool_int_array_new_copy".}
proc initPoolIntArray(dest: var PoolIntArray;
arr: Array) {.
importc: "godot_pool_int_array_new_with_array".}
proc deinit(self: var PoolIntArray) {.
importc: "godot_pool_int_array_destroy".}
proc initPoolIntArray*(): PoolIntArray {.inline.} =
initPoolIntArray(result)
proc initPoolIntArray*(arr: Array): PoolIntArray {.inline.} =
initPoolIntArray(result, arr)
proc `=`(self: var PoolIntArray, other: PoolIntArray) {.inline.} =
initPoolIntArray(self, other)
proc `=destroy`(self: PoolIntArray) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolIntArray; data: cint) {.
importc: "godot_pool_int_array_append".}
proc addFirst*(self: var PoolIntArray; data: cint) {.
importc: "godot_pool_int_array_push_back".}
proc add*(self: var PoolIntArray; arr: PoolIntArray) {.
importc: "godot_pool_int_array_append_array".}
proc insert*(self: var PoolIntArray; idx: cint;
data: cint): Error {.
importc: "godot_pool_int_array_insert".}
proc delete*(self: var PoolIntArray; idx: cint) {.
importc: "godot_pool_int_array_remove".}
proc reverse*(self: var PoolIntArray) {.
importc: "godot_pool_int_array_invert".}
proc setLen*(self: var PoolIntArray; size: cint) {.
importc: "godot_pool_int_array_resize".}
proc `[]=`*(self: var PoolIntArray; idx: cint; data: cint) {.
importc: "godot_pool_int_array_set".}
proc `[]`*(self: PoolIntArray; idx: cint): cint {.
noSideEffect,
importc: "godot_pool_int_array_get".}
proc len*(self: PoolIntArray): cint {.
noSideEffect,
importc: "godot_pool_int_array_size".}
iterator items*(arr: PoolIntArray): cint =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolIntArray): tuple[key: cint, val: cint] =
for i in 0..<arr.len:
yield (i, arr[i])
# Real
proc initPoolRealArray(dest: var PoolRealArray) {.
importc: "godot_pool_real_array_new".}
proc initPoolRealArray(dest: var PoolRealArray;
src: PoolRealArray) {.
importc: "godot_pool_real_array_new_copy".}
proc initPoolRealArray(dest: var PoolRealArray; arr: Array) {.
importc: "godot_pool_real_array_new_with_array".}
proc deinit(self: var PoolRealArray) {.
importc: "godot_pool_real_array_destroy".}
proc initPoolRealArray*(): PoolRealArray {.inline.} =
initPoolRealArray(result)
proc initPoolRealArray*(arr: Array): PoolRealArray {.inline.} =
initPoolRealArray(result, arr)
proc `=`(self: var PoolRealArray, other: PoolRealArray) {.inline.} =
initPoolRealArray(self, other)
proc `=destroy`(self: PoolRealArray) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolRealArray; data: float32) {.
importc: "godot_pool_real_array_append".}
proc addFirst*(self: var PoolRealArray; data: float32) {.
importc: "godot_pool_real_array_push_back".}
proc add*(self: var PoolRealArray; arr: PoolRealArray) {.
importc: "godot_pool_real_array_append_array".}
proc insert*(self: var PoolRealArray;
idx: cint; data: float32): Error {.
importc: "godot_pool_real_array_insert".}
proc delete*(self: var PoolRealArray; idx: cint) {.
importc: "godot_pool_real_array_remove".}
proc reverse*(self: var PoolRealArray) {.
importc: "godot_pool_real_array_invert".}
proc setLen*(self: var PoolRealArray; size: cint) {.
importc: "godot_pool_real_array_resize".}
proc `[]=`*(self: var PoolRealArray; idx: cint; data: float32) {.
importc: "godot_pool_real_array_set".}
proc `[]`*(self: PoolRealArray; idx: cint): float32 {.
noSideEffect,
importc: "godot_pool_real_array_get".}
proc len*(self: PoolRealArray): cint {.
noSideEffect,
importc: "godot_pool_real_array_size".}
iterator items*(arr: PoolRealArray): float32 =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolRealArray): tuple[key: cint, val: float32] =
for i in 0..<arr.len:
yield (i, arr[i])
# String
proc initPoolStringArray(dest: var PoolStringArray) {.
importc: "godot_pool_string_array_new".}
proc initPoolStringArray(dest: var PoolStringArray;
src: PoolStringArray) {.
importc: "godot_pool_string_array_new_copy".}
proc initPoolStringArray(dest: var PoolStringArray; arr: Array) {.
importc: "godot_pool_string_array_new_with_array".}
proc deinit(self: var PoolStringArray) {.
importc: "godot_pool_string_array_destroy".}
proc initPoolStringArray*(): PoolStringArray {.inline.} =
initPoolStringArray(result)
proc initPoolStringArray*(arr: Array): PoolStringArray {.inline.} =
initPoolStringArray(result, arr)
proc `=`(self: var PoolStringArray, other: PoolStringArray) {.inline.} =
initPoolStringArray(self, other)
proc `=destroy`(self: PoolStringArray) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolStringArray; data: GodotString) {.
importc: "godot_pool_string_array_append".}
proc addFirst*(self: var PoolStringArray; data: GodotString) {.
importc: "godot_pool_string_array_push_back".}
proc add*(self: var PoolStringArray; arr: PoolStringArray) {.
importc: "godot_pool_string_array_append_array".}
proc insert*(self: var PoolStringArray;
idx: cint; data: GodotString): Error {.
importc: "godot_pool_string_array_insert".}
proc delete*(self: var PoolStringArray; idx: cint) {.
importc: "godot_pool_string_array_remove".}
proc reverse*(self: var PoolStringArray) {.
importc: "godot_pool_string_array_invert".}
proc setLen*(self: var PoolStringArray; size: cint) {.
importc: "godot_pool_string_array_resize".}
proc `[]=`*(self: var PoolStringArray; idx: cint;
data: GodotString) {.
importc: "godot_pool_string_array_set".}
proc `[]`*(self: PoolStringArray; idx: cint): GodotString {.
noSideEffect,
importc: "godot_pool_string_array_get".}
proc len*(self: PoolStringArray): cint {.
noSideEffect,
importc: "godot_pool_string_array_size".}
iterator items*(arr: PoolStringArray): GodotString =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolStringArray): tuple[key: cint, val: GodotString] =
for i in 0..<arr.len:
yield (i, arr[i])
# Vector2
proc initPoolVector2Array(dest: var PoolVector2Array) {.
importc: "godot_pool_vector2_array_new".}
proc initPoolVector2Array(dest: var PoolVector2Array;
src: PoolVector2Array) {.
importc: "godot_pool_vector2_array_new_copy".}
proc initPoolVector2Array(dest: var PoolVector2Array; arr: Array) {.
importc: "godot_pool_vector2_array_new_with_array".}
proc deinit(self: var PoolVector2Array) {.
importc: "godot_pool_vector2_array_destroy".}
proc initPoolVector2Array*(): PoolVector2Array {.inline.} =
initPoolVector2Array(result)
proc initPoolVector2Array*(arr: Array): PoolVector2Array {.inline.} =
initPoolVector2Array(result, arr)
proc `=`(self: var PoolVector2Array, other: PoolVector2Array) {.inline.} =
initPoolVector2Array(self, other)
proc `=destroy`(self: PoolVector2Array) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolVector2Array; data: Vector2) {.
importc: "godot_pool_vector2_array_append".}
proc addFirst*(self: var PoolVector2Array; data: Vector2) {.
importc: "godot_pool_vector2_array_push_back".}
proc add*(self: var PoolVector2Array;
arr: PoolVector2Array) {.
importc: "godot_pool_vector2_array_append_array".}
proc insert*(self: var PoolVector2Array;
idx: cint; data: Vector2): Error {.
importc: "godot_pool_vector2_array_insert".}
proc delete*(self: var PoolVector2Array; idx: cint) {.
importc: "godot_pool_vector2_array_remove".}
proc reverse*(self: var PoolVector2Array) {.
importc: "godot_pool_vector2_array_invert".}
proc setLen*(self: var PoolVector2Array; size: cint) {.
importc: "godot_pool_vector2_array_resize".}
proc `[]=`*(self: var PoolVector2Array; idx: cint;
data: ptr Vector2) {.
importc: "godot_pool_vector2_array_set".}
proc `[]`*(self: PoolVector2Array; idx: cint): Vector2 {.
noSideEffect,
importc: "godot_pool_vector2_array_get".}
proc len*(self: PoolVector2Array): cint {.
noSideEffect,
importc: "godot_pool_vector2_array_size".}
iterator items*(arr: PoolVector2Array): Vector2 =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolVector2Array): tuple[key: cint, val: Vector2] =
for i in 0..<arr.len:
yield (i, arr[i])
# Vector3
proc initPoolVector3Array*(dest: var PoolVector3Array) {.
importc: "godot_pool_vector3_array_new".}
proc initPoolVector3Array*(dest: var PoolVector3Array;
src: PoolVector3Array) {.
importc: "godot_pool_vector3_array_new_copy".}
proc initPoolVector3Array*(dest: var PoolVector3Array; arr: Array) {.
importc: "godot_pool_vector3_array_new_with_array".}
proc deinit(self: var PoolVector3Array) {.
importc: "godot_pool_vector3_array_destroy".}
proc initPoolVector3Array*(): PoolVector3Array {.inline.} =
initPoolVector3Array(result)
proc initPoolVector3Array*(arr: Array): PoolVector3Array {.inline.} =
initPoolVector3Array(result, arr)
proc `=`(self: var PoolVector3Array, other: PoolVector3Array) {.inline.} =
initPoolVector3Array(self, other)
proc `=destroy`(self: PoolVector3Array) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolVector3Array;
data: Vector3) {.
importc: "godot_pool_vector3_array_append".}
proc addFirst*(self: var PoolVector3Array;
data: Vector3) {.
importc: "godot_pool_vector3_array_push_back".}
proc add*(self: var PoolVector3Array;
arr: PoolVector3Array) {.
importc: "godot_pool_vector3_array_append_array".}
proc insert*(self: var PoolVector3Array;
idx: cint; data: Vector3): Error {.
importc: "godot_pool_vector3_array_insert".}
proc delete*(self: var PoolVector3Array; idx: cint) {.
importc: "godot_pool_vector3_array_remove".}
proc reverse*(self: var PoolVector3Array) {.
importc: "godot_pool_vector3_array_invert".}
proc setLen*(self: var PoolVector3Array; size: cint) {.
importc: "godot_pool_vector3_array_resize".}
proc `[]=`*(self: var PoolVector3Array;
idx: cint; data: Vector3) {.
importc: "godot_pool_vector3_array_set".}
proc `[]`*(self: PoolVector3Array;
idx: cint): Vector3 {.
noSideEffect,
importc: "godot_pool_vector3_array_get".}
proc len*(self: PoolVector3Array): cint {.
noSideEffect,
importc: "godot_pool_vector3_array_size".}
iterator items*(arr: PoolVector3Array): Vector3 =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolVector3Array): tuple[key: cint, val: Vector3] =
for i in 0..<arr.len:
yield (i, arr[i])
# Color
proc initPoolColorArray(dest: var PoolColorArray) {.
importc: "godot_pool_color_array_new".}
proc initPoolColorArray(dest: var PoolColorArray;
src: PoolColorArray) {.
importc: "godot_pool_color_array_new_copy".}
proc initPoolColorArray(dest: var PoolColorArray; arr: Array) {.
importc: "godot_pool_color_array_new_with_array".}
proc deinit(self: var PoolColorArray) {.
importc: "godot_pool_color_array_destroy".}
proc initPoolColorArray*(): PoolColorArray {.inline.} =
initPoolColorArray(result)
proc initPoolColorArray*(arr: Array): PoolColorArray {.inline.} =
initPoolColorArray(result, arr)
proc `=`(self: var PoolColorArray, other: PoolColorArray) {.inline.} =
initPoolColorArray(self, other)
proc `=destroy`(self: PoolColorArray) {.inline.} =
unsafeAddr(self).deinit()
proc add*(self: var PoolColorArray; data: Color) {.
importc: "godot_pool_color_array_append".}
proc addFirst*(self: var PoolColorArray; data: Color) {.
importc: "godot_pool_color_array_push_back".}
proc add*(self: var PoolColorArray; arr: PoolColorArray) {.
importc: "godot_pool_color_array_append_array".}
proc insert*(self: var PoolColorArray;
idx: cint; data: Color): Error {.
importc: "godot_pool_color_array_insert".}
proc delete*(self: var PoolColorArray; idx: cint) {.
importc: "godot_pool_color_array_remove".}
proc reverse*(self: var PoolColorArray) {.
importc: "godot_pool_color_array_invert".}
proc setLen*(self: var PoolColorArray; size: cint) {.
importc: "godot_pool_color_array_resize".}
proc `[]=`*(self: var PoolColorArray; idx: cint;
data: Color) {.
importc: "godot_pool_color_array_set".}
proc `[]`*(self: PoolColorArray; idx: cint): Color {.
noSideEffect,
importc: "godot_pool_color_array_get".}
proc len*(self: PoolColorArray): cint {.
noSideEffect,
importc: "godot_pool_color_array_size".}
iterator items*(arr: PoolColorArray): Color =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolColorArray): tuple[key: cint, val: Color] =
for i in 0..<arr.len:
yield (i, arr[i])

126
godot/core/poolarrays.nim Normal file
View file

@ -0,0 +1,126 @@
# Copyright 2017 Xored Software, Inc.
import godotbase, vector2, vector3, colors
import "../internal/godotpoolarrays.nim", "../internal/godotstrings.nim"
# byte
template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
noData = false) =
type
T* = ref object
fieldName: GodotT
proc poolArrayFinalizer(arr: T) =
arr.fieldName.deinit()
proc newProcName*(): T {.inline.} =
new(result, poolArrayFinalizer)
initProcName(result.fieldName)
proc newProcName*(arr: Array): T {.inline.} =
new(result, poolArrayFinalizer)
initProcName(result.fieldName, arr.godotArray[])
proc newProcName*(arr: GodotT): T {.inline.} =
new(result, poolArrayFinalizer)
result.fieldName = arr
proc add*(self: var T; arr: T) {.inline.} =
self.fieldName.add(arr.fieldName)
proc delete*(self: var T; idx: int) {.inline.} =
self.fieldName.delete(idx.cint)
proc reverse*(self: var T) {.inline.} =
self.fieldName.reverse()
proc setLen*(self: var T; size: int) {.inline.} =
self.fieldName.setLen(size.cint)
proc len*(self: T): int {.inline.} =
self.fieldName.len.int
proc fieldName*(self: T): ptr GodotT {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.fieldName
when not noData:
proc add*(self: var T; data: DataT) {.inline.} =
self.fieldName.add(data)
proc addFirst*(self: var T, data: DataT) {.inline.} =
self.fieldName.addFirst(data)
proc insert*(self: var T; idx: int; data: DataT): Error {.inline.} =
self.fieldName.insert(idx.cint, data)
proc `[]=`*(self: var T; idx: int; data: DataT) {.inline.} =
self.fieldName[idx.cint] = data
proc `[]`*(self: T; idx: int): DataT {.inline.} =
self.fieldName[idx.cint]
iterator items*(arr: T): DataT =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: T): tuple[key: int, val: DataT] =
for i in 0..<arr.len:
yield (i, arr[i])
definePoolArray(PoolByteArray, GodotPoolByteArray, uint8,
godotPoolByteArray, newPoolByteArray,
initGodotPoolByteArray)
definePoolArray(PoolIntArray, GodotPoolIntArray, cint,
godotPoolIntArray, newPoolIntArray,
initGodotPoolIntArray)
definePoolArray(PoolRealArray, GodotPoolRealArray, float64,
godotPoolRealArray, newPoolRealArray,
initGodotPoolRealArray)
definePoolArray(PoolVector2Array, GodotPoolVector2Array, Vector2,
godotPoolVector2Array, newPoolVector2Array,
initGodotPoolVector2Array)
definePoolArray(PoolVector3Array, GodotPoolVector3Array, Vector3,
godotPoolVector3Array, newPoolVector3Array,
initGodotPoolVector3Array)
definePoolArray(PoolColorArray, GodotPoolColorArray, Color,
godotPoolColorArray, newPoolColorArray,
initGodotPoolColorArray)
definePoolArray(PoolStringArray, GodotPoolStringArray, string,
godotPoolStringArray, newPoolStringArray,
initGodotPoolStringArray, true)
proc add*(self: var PoolStringArray; data: string) =
var s = data.toGodotString()
self.godotPoolStringArray.add(s)
s.deinit()
proc addFirst*(self: var PoolStringArray, data: string) =
var s = data.toGodotString()
self.godotPoolStringArray.addFirst(s)
s.deinit()
proc insert*(self: var PoolStringArray; idx: int; data: string): Error =
var s = data.toGodotString()
result = self.godotPoolStringArray.insert(idx.cint, s)
s.deinit()
proc `[]=`*(self: var PoolStringArray; idx: int; data: string) =
var s = data.toGodotString()
self.godotPoolStringArray[idx.cint] = s
s.deinit()
proc `[]`*(self: PoolStringArray; idx: int): string =
var s = self.godotPoolStringArray[idx.cint]
result = $s
s.deinit()
iterator items*(arr: PoolStringArray): string =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: PoolStringArray): tuple[key: int, val: string] =
for i in 0..<arr.len:
yield (i, arr[i])

View file

@ -1,6 +1,7 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, vector3, strings
import godotbase, vector3
import "../internal/godotstrings.nim"
type
Quat* {.byref.} = object
@ -20,34 +21,10 @@ proc initQuat*(x, y, z, w: float32): Quat {.inline.} =
proc initQuat*(axis: Vector3; angle: float32): Quat {.inline.} =
initQuat(result, axis, angle)
proc x*(self: Quat): float32 {.
noSideEffect,
importc: "godot_quat_get_x".}
proc `x=`*(self: var Quat; val: float32) {.
noSideEffect,
importc: "godot_quat_set_x".}
proc y*(self: Quat): float32 {.
noSideEffect,
importc: "godot_quat_get_y".}
proc `y=`*(self: var Quat; val: float32) {.
noSideEffect,
importc: "godot_quat_set_y".}
proc z*(self: Quat): float32 {.
noSideEffect,
importc: "godot_quat_get_z".}
proc `z=`*(self: var Quat; val: float32) {.
noSideEffect,
importc: "godot_quat_set_z".}
proc w*(self: Quat): float32 {.
noSideEffect,
importc: "godot_quat_get_w".}
proc `w=`*(self: var Quat; val: float32) {.
noSideEffect,
importc: "godot_quat_set_w".}
proc toGodotString*(self: Quat): GodotString {.
proc toGodotString(self: Quat): GodotString {.
noSideEffect,
importc: "godot_quat_as_string".}
proc `$`*(self: Quat): string {.inline.} =
$self.toGodotString()

View file

@ -1,6 +1,8 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, vector2, strings
import godotbase, vector2
import "../internal/godotstrings.nim"
type
Rect2* {.byref.} = object
@ -13,9 +15,10 @@ proc initRect2*(pos, size: Vector2): Rect2 {.inline.} =
proc initRect2*(x, y, sizeX, sizeY: float32): Rect2 {.inline.} =
Rect2(position: vec2(x, y), size: vec2(sizeX, sizeY))
proc toGodotString*(self: Rect2): GodotString {.
proc toGodotString(self: Rect2): GodotString {.
noSideEffect,
importc: "godot_rect2_as_string".}
proc `$`*(self: Rect2): string {.inline.} =
$self.toGodotString()

View file

@ -1,6 +1,8 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, plane, vector3, strings
import godotbase, planes, vector3
import "../internal/godotstrings.nim"
type
Rect3* {.byref.} = object
@ -10,11 +12,13 @@ type
proc initRect3*(pos, size: Vector3): Rect3 {.inline.} =
Rect3(position: pos, size: size)
proc toGodotString*(self: Rect3): GodotString {.
proc toGodotString(self: Rect3): GodotString {.
noSideEffect,
importc: "godot_rect3_as_string".}
proc `$`*(self: Rect3): string {.inline.} =
$self.toGodotString()
proc area*(self: Rect3): float32 {.
noSideEffect,
importc: "godot_rect3_get_area".}

View file

@ -1,5 +1,7 @@
import godotbase
import "../internal/godotinternaltypes.nim"
type
RID* {.byref.} = object
p: pointer

View file

@ -1,10 +1,12 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, strings, vector2, rect2
import godotbase, vector2, rect2
import "../internal/godotstrings.nim"
type
Transform2D* {.byref.} = object
elements: array[3, Vector2]
elements*: array[3, Vector2]
proc initTransform2D(dest: var Transform2D) {.
importc: "godot_transform2d_new_identity".}
@ -22,8 +24,9 @@ proc initTransform2D*(rot: float32; pos: Vector2): Transform2D {.inline.} =
proc initTransform2D*(xAxis, yAxis, origin: Vector2): Transform2D {.inline.} =
initTransform2D(result, xAxis, yAxis, origin)
proc toGodotString*(self: Transform2D): GodotString {.
proc toGodotString(self: Transform2D): GodotString {.
importc: "godot_transform2d_as_string".}
proc `$`*(self: Transform2D): string {.inline.} =
$self.toGodotString()
@ -71,4 +74,3 @@ proc `*`*(a, b: Transform2D): Transform2D {.
proc `*=`*(a: var Transform2D, b: Transform2D) {.inline.} =
a = a * b

View file

@ -1,6 +1,8 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, basis, vector3, strings, plane, rect3
import godotbase, basis, vector3, planes, rect3
import "../internal/godotstrings.nim"
type
Transform* {.byref.} = object
@ -21,8 +23,9 @@ proc initTransform*(xAxis, yAxis, zAxis,
proc initTransform*(basis: Basis, origin: Vector3): Transform {.inline.} =
Transform(basis: basis, origin: origin)
proc toGodotString*(self: Transform): GodotString {.
proc toGodotString(self: Transform): GodotString {.
importc: "godot_transform_as_string".}
proc `$`*(self: Transform): string {.inline.} =
$self.toGodotString()

View file

@ -1,269 +0,0 @@
import godotbase
import arrays, basis, nodepath
import plane, poolarray, quat, rect2, rect3, rid
import strings, transform, transform2d, vector2
import vector3, color
type
VariantType* {.size: sizeof(cint), pure.} = enum
Nil, ## atomic types
Bool,
Int,
Real,
String,
# math types
Vector2, ## 5
Rect2,
Vector3,
Transform2D,
Plane,
Quat, ## 10
Rect3,
Basis,
Transform, ## misc types
Color,
NodePath, ## 15
RID,
Object,
Dictionary,
Array, ## 20
# arrays
PoolByteArray,
PoolIntArray,
PoolRealArray,
PoolStringArray,
PoolVector2Array, ## 25
PoolVector3Array,
PoolColorArray
VariantCallErrorType* {.size: sizeof(cint), pure.} = enum
OK,
InvalidMethod,
InvalidArgument,
TooManyArguments,
TooFewArguments,
InstanceIsNull
VariantCallError* = object
error*: VariantCallErrorType
argument*: cint
expected*: VariantType
proc getType*(p: Variant): VariantType {.
importc: "godot_variant_get_type".}
proc initNilVariant(dest: var Variant) {.
importc: "godot_variant_new_nil".}
proc initVariant(dest: var Variant; b: bool) {.
importc: "godot_variant_new_bool".}
proc initVariant(dest: var Variant; i: uint64) {.
importc: "godot_variant_new_uint".}
proc initVariant(dest: var Variant; i: int64) {.
importc: "godot_variant_new_int".}
proc initVariant(dest: var Variant; r: cdouble) {.
importc: "godot_variant_new_real".}
proc initVariant(dest: var Variant; s: GodotString) {.
importc: "godot_variant_new_string".}
proc initVariant(dest: var Variant; v2: Vector2) {.
importc: "godot_variant_new_vector2".}
proc initVariant(dest: var Variant; rect2: Rect2) {.
importc: "godot_variant_new_rect2".}
proc initVariant(dest: var Variant; v3: Vector3) {.
importc: "godot_variant_new_vector3".}
proc initVariant(dest: var Variant; t2d: Transform2D) {.
importc: "godot_variant_new_transform2d".}
proc initVariant(dest: var Variant; plane: Plane) {.
importc: "godot_variant_new_plane".}
proc initVariant(dest: var Variant; quat: Quat) {.
importc: "godot_variant_new_quat".}
proc initVariant(dest: var Variant; rect3: Rect3) {.
importc: "godot_variant_new_rect3".}
proc initVariant(dest: var Variant; basis: Basis) {.
importc: "godot_variant_new_basis".}
proc initVariant(dest: var Variant;
trans: Transform) {.
importc: "godot_variant_new_transform".}
proc initVariant(dest: var Variant; color: Color) {.
importc: "godot_variant_new_color".}
proc initVariant(dest: var Variant; nodePath: NodePath) {.
importc: "godot_variant_new_node_path".}
proc initVariant(dest: var Variant; rid: RID) {.
importc: "godot_variant_new_rid".}
proc initVariant(dest: var Variant; obj: ptr GodotObject) {.
importc: "godot_variant_new_object".}
proc initVariant(dest: var Variant; arr: Array) {.
importc: "godot_variant_new_array".}
proc initVariant(dest: var Variant; pba: PoolByteArray) {.
importc: "godot_variant_new_pool_byte_array".}
proc initVariant(dest: var Variant; pia: PoolIntArray) {.
importc: "godot_variant_new_pool_int_array".}
proc initVariant(dest: var Variant; pra: PoolRealArray) {.
importc: "godot_variant_new_pool_real_array".}
proc initVariant(dest: var Variant; psa: PoolStringArray) {.
importc: "godot_variant_new_pool_string_array".}
proc initVariant(dest: var Variant; pv2a: PoolVector2Array) {.
importc: "godot_variant_new_pool_vector2_array".}
proc initVariant(dest: var Variant; pv3a: PoolVector3Array) {.
importc: "godot_variant_new_pool_vector3_array".}
proc initVariant(dest: var Variant; pca: PoolColorArray) {.
importc: "godot_variant_new_pool_color_array".}
proc variant*(): Variant {.inline.} =
initNilVariant(result)
proc variant*(b: bool): Variant {.inline.} =
initVariant(result, b)
proc variant*(i: uint64): Variant {.inline.} =
initVariant(result, i)
proc variant*(i: int64): Variant {.inline.} =
initVariant(result, i)
proc variant*(r: cdouble): Variant {.inline.} =
initVariant(result, r)
proc variant*(s: GodotString): Variant {.inline.} =
initVariant(result, s)
proc variant*(s: string): Variant {.inline.} =
let godotStr = s.toGodotString()
initVariant(result, godotStr)
proc variant*(v2: Vector2): Variant {.inline.} =
initVariant(result, v2)
proc variant*(rect2: Rect2): Variant {.inline.} =
initVariant(result, rect2)
proc variant*(v3: Vector3): Variant {.inline.} =
initVariant(result, v3)
proc variant*(t2d: Transform2D): Variant {.inline.} =
initVariant(result, t2d)
proc variant*(plane: Plane): Variant {.inline.} =
initVariant(result, plane)
proc variant*(quat: Quat): Variant {.inline.} =
initVariant(result, quat)
proc variant*(rect3: Rect3): Variant {.inline.} =
initVariant(result, rect3)
proc variant*(basis: Basis): Variant {.inline.} =
initVariant(result, basis)
proc variant*(trans: Transform): Variant {.inline.} =
initVariant(result, trans)
proc variant*(color: Color): Variant {.inline.} =
initVariant(result, color)
proc variant*(nodePath: NodePath): Variant {.inline.} =
initVariant(result, nodePath)
proc variant*(rid: RID): Variant {.inline.} =
initVariant(result, rid)
proc variant*(obj: ptr GodotObject): Variant {.inline.} =
initVariant(result, obj)
proc variant*(arr: Array): Variant {.inline.} =
initVariant(result, arr)
proc variant*(pba: PoolByteArray): Variant {.inline.} =
initVariant(result, pba)
proc variant*(pia: PoolIntArray): Variant {.inline.} =
initVariant(result, pia)
proc variant*(pra: PoolRealArray): Variant {.inline.} =
initVariant(result, pra)
proc variant*(psa: PoolStringArray): Variant {.inline.} =
initVariant(result, psa)
proc variant*(pv2a: PoolVector2Array): Variant {.inline.} =
initVariant(result, pv2a)
proc variant*(pv3a: PoolVector3Array): Variant {.inline.} =
initVariant(result, pv3a)
proc variant*(pca: PoolColorArray): Variant {.inline.} =
initVariant(result, pca)
proc asBool*(self: Variant): bool {.
importc: "godot_variant_as_bool".}
proc asUint*(self: Variant): uint64 {.
importc: "godot_variant_as_uint".}
proc asInt*(self: Variant): int64 {.
importc: "godot_variant_as_int".}
proc asReal*(self: Variant): cdouble {.
importc: "godot_variant_as_real".}
proc asGodotString*(self: Variant): GodotString {.
importc: "godot_variant_as_string".}
proc asString*(self: Variant): string =
let s = self.asGodotString()
result = $s
proc asVector2*(self: Variant): Vector2 {.
importc: "godot_variant_as_vector2".}
proc asRect2*(self: Variant): Rect2 {.
importc: "godot_variant_as_rect2".}
proc asVector3*(self: Variant): Vector3 {.
importc: "godot_variant_as_vector3".}
proc asTransform2D*(self: Variant): Transform2D {.
importc: "godot_variant_as_transform2d".}
proc asPlane*(self: Variant): Plane {.
importc: "godot_variant_as_plane".}
proc asQuat*(self: Variant): Quat {.
importc: "godot_variant_as_quat".}
proc asRect3*(self: Variant): Rect3 {.
importc: "godot_variant_as_rect3".}
proc asBasis*(self: Variant): Basis {.
importc: "godot_variant_as_basis".}
proc asTransform*(self: Variant): Transform {.
importc: "godot_variant_as_transform".}
proc asColor*(self: Variant): Color {.
importc: "godot_variant_as_color".}
proc asNodePath*(self: Variant): NodePath {.
importc: "godot_variant_as_node_path".}
proc asRid*(self: Variant): RID {.
importc: "godot_variant_as_rid".}
proc asObject*(self: Variant): ptr GodotObject {.
importc: "godot_variant_as_object".}
proc asArray*(self: Variant): Array {.
importc: "godot_variant_as_array".}
proc asPoolByteArray*(self: Variant): PoolByteArray {.
importc: "godot_variant_as_pool_byte_array".}
proc asPoolIntArray*(self: Variant): PoolIntArray {.
importc: "godot_variant_as_pool_int_array".}
proc asPoolRealArray*(self: Variant): PoolRealArray {.
importc: "godot_variant_as_pool_real_array".}
proc asPoolStringArray*(self: Variant): PoolStringArray {.
importc: "godot_variant_as_pool_string_array".}
proc asPoolVector2Array*(self: Variant): PoolVector2Array {.
importc: "godot_variant_as_pool_vector2_array".}
proc asPoolVector3Array*(self: Variant): PoolVector3Array {.
importc: "godot_variant_as_pool_vector3_array".}
proc asPoolColorArray*(self: Variant): PoolColorArray {.
importc: "godot_variant_as_pool_color_array".}
proc call*(self: Variant; meth: GodotString;
args: var array[MAX_ARG_COUNT, Variant]; argCount: cint;
error: var VariantCallError): Variant {.
importc: "godot_variant_call".}
proc hasMethod*(self: Variant; meth: GodotString): bool {.
importc: "godot_variant_has_method".}
proc `==`*(self, other: Variant): bool {.
importc: "godot_variant_operator_equal".}
proc `<`*(self, other: Variant): bool {.
importc: "godot_variant_operator_less".}
proc hashCompare*(self, other: Variant): bool {.
importc: "godot_variant_hash_compare".}
proc booleanize*(self: Variant; isValid: var bool): bool {.
importc: "godot_variant_booleanize".}

251
godot/core/variants.nim Normal file
View file

@ -0,0 +1,251 @@
import tables
import godotbase
import basis, nodepaths
import planes, poolarrays, quats, rect2, rect3, rids
import transforms, transform2d, vector2
import vector3, colors
import "../internal/godotinternaltypes.nim"
import "../internal/godotvariants.nim", "../internal/godotpoolarrays.nim",
"../internal/godotstrings.nim", "../internal/godotnodepaths.nim"
export VariantType, VariantCallErrorType, VariantCallError
proc getType*(v: Variant): VariantType =
v.godotVariant[].getType()
proc variantFinalizer(v: Variant) =
if not v.isNoDeinit:
v.godotVariant[].deinit()
proc newVariant*(): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[])
proc newVariant*(v: Variant): Variant {.inline.} =
## Makes a copy
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], v.godotVariant[])
proc newVariant*(v: GodotVariant): Variant {.inline.} =
new(result, variantFinalizer)
result.godotVariant[] = v
proc newVariant*(b: bool): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], b)
proc newVariant*(i: uint64): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], i)
proc newVariant*(i: int64): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], i)
proc newVariant*(r: cdouble): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], r)
proc newVariant*(s: GodotString): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], s)
proc newVariant*(s: string): Variant {.inline.} =
new(result, variantFinalizer)
var godotStr = s.toGodotString()
initGodotVariant(result.godotVariant[], godotStr)
godotStr.deinit()
proc newVariant*(v2: Vector2): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], v2)
proc newVariant*(rect2: Rect2): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], rect2)
proc newVariant*(v3: Vector3): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], v3)
proc newVariant*(t2d: Transform2D): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], t2d)
proc newVariant*(plane: Plane): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], plane)
proc newVariant*(quat: Quat): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], quat)
proc newVariant*(rect3: Rect3): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], rect3)
proc newVariant*(basis: Basis): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], basis)
proc newVariant*(trans: Transform): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], trans)
proc newVariant*(color: Color): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], color)
proc newVariant*(nodePath: NodePath): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], nodePath.godotNodePath[])
proc newVariant*(godotNodePath: GodotNodePath): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], godotNodePath)
proc newVariant*(rid: RID): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], rid)
proc newVariant*(obj: ptr GodotObject): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], obj)
import arrays
proc newVariant*(arr: Array): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], arr.godotArray[])
proc newVariant*(pba: PoolByteArray): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], pba.godotPoolByteArray[])
proc newVariant*(pia: PoolIntArray): Variant {.inline.} =
initGodotVariant(result.godotVariant[], pia.godotPoolIntArray[])
proc newVariant*(pra: PoolRealArray): Variant {.inline.} =
initGodotVariant(result.godotVariant[], pra.godotPoolRealArray[])
proc newVariant*(psa: PoolStringArray): Variant {.inline.} =
initGodotVariant(result.godotVariant[], psa.godotPoolStringArray[])
proc newVariant*(pv2a: PoolVector2Array): Variant {.inline.} =
initGodotVariant(result.godotVariant[], pv2a.godotPoolVector2Array[])
proc newVariant*(pv3a: PoolVector3Array): Variant {.inline.} =
initGodotVariant(result.godotVariant[], pv3a.godotPoolVector3Array[])
proc newVariant*(pca: PoolColorArray): Variant {.inline.} =
initGodotVariant(result.godotVariant[], pca.godotPoolColorArray[])
import dictionaries
proc newVariant*(dict: Dictionary): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], dict.godotDictionary[])
proc asBool*(self: Variant): bool {.inline.} =
self.godotVariant[].asBool()
proc asUInt*(self: Variant): uint64 =
self.godotVariant[].asUInt()
proc asInt*(self: Variant): int64 =
self.godotVariant[].asInt()
proc asReal*(self: Variant): cdouble =
self.godotVariant[].asReal()
proc asString*(self: Variant): string =
var s = self.godotVariant[].asGodotString()
result = $s
s.deinit()
proc asVector2*(self: Variant): Vector2 =
self.godotVariant[].asVector2()
proc asRect2*(self: Variant): Rect2 =
self.godotVariant[].asRect2()
proc asVector3*(self: Variant): Vector3 =
self.godotVariant[].asVector3()
proc asTransform2D*(self: Variant): Transform2D =
self.godotVariant[].asTransform2D()
proc asPlane*(self: Variant): Plane =
self.godotVariant[].asPlane()
proc asQuat*(self: Variant): Quat =
self.godotVariant[].asQuat()
proc asRect3*(self: Variant): Rect3 =
self.godotVariant[].asRect3()
proc asBasis*(self: Variant): Basis =
self.godotVariant[].asBasis()
proc asTransform*(self: Variant): Transform =
self.godotVariant[].asTransform()
proc asColor*(self: Variant): Color =
self.godotVariant[].asColor()
proc asNodePath*(self: Variant): NodePath =
result = newNodePath(self.godotVariant[].asNodePath())
proc asRID*(self: Variant): RID =
self.godotVariant[].asRID()
proc asGodotObject*(self: Variant): ptr GodotObject =
self.godotVariant[].asGodotObject()
proc asArray*(self: Variant): Array =
newArray(self.godotVariant[].asGodotArray())
proc asPoolByteArray*(self: Variant): PoolByteArray =
newPoolByteArray(self.godotVariant[].asGodotPoolByteArray())
proc asPoolIntArray*(self: Variant): PoolIntArray =
newPoolIntArray(self.godotVariant[].asGodotPoolIntArray())
proc asPoolRealArray*(self: Variant): PoolRealArray =
newPoolRealArray(self.godotVariant[].asGodotPoolRealArray())
proc asPoolStringArray*(self: Variant): PoolStringArray =
newPoolStringArray(self.godotVariant[].asGodotPoolStringArray())
proc asPoolVector2Array*(self: Variant): PoolVector2Array =
newPoolVector2Array(self.godotVariant[].asGodotPoolVector2Array())
proc asPoolVector3Array*(self: Variant): PoolVector3Array =
newPoolVector3Array(self.godotVariant[].asGodotPoolVector3Array())
proc asPoolColorArray*(self: Variant): PoolColorArray =
newPoolColorArray(self.godotVariant[].asGodotPoolColorArray())
proc asDictionary*(self: Variant): Dictionary {.inline.} =
newDictionary(self.godotVariant[].asGodotDictionary)
proc hasMethod*(self: Variant; meth: string): bool =
var s = meth.toGodotString()
result = self.godotVariant[].hasMethod(s)
s.deinit()
proc `==`*(self, other: Variant): bool =
if self.isNil and other.isNil: return true
if self.isNil != other.isNil: return false
result = self.godotVariant[] == other.godotVariant[]
proc `<`*(self, other: Variant): bool =
result = self.godotVariant[] < other.godotVariant[]
proc hashCompare*(self, other: Variant): bool =
self.godotVariant[].hashCompare(other.godotVariant[])
proc booleanize*(self: Variant; isValid: var bool): bool =
self.godotVariant[].booleanize(isValid)

View file

@ -1,6 +1,8 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase, strings
import godotbase
import "../internal/godotstrings.nim"
type
Vector2* {.byref.} = object
@ -10,8 +12,9 @@ type
proc vec2*(x, y: float32): Vector2 {.inline.} =
Vector2(x: x, y: y)
proc toGodotString*(self: Vector2): GodotString {.
proc toGodotString(self: Vector2): GodotString {.
importc: "godot_vector2_as_string".}
proc `$`*(self: Vector2): string {.inline.} =
$self.toGodotString()

View file

@ -1,7 +1,7 @@
# Copyright (c) 2017 Xored Software, Inc.
import math
import godotbase, strings
import godotbase
type
Vector3* {.byref.} = object

View file

@ -1,16 +1,13 @@
# Copyright 2017 Xored Software, Inc.
import core.godotbase, core.godotcore
import core / [strings, vector2, rect2, vector3, transform2d, plane, quat]
import core / [rect3, basis, transform, color, nodepath, rid]
import core / [dictionary, arrays, poolarray, variant]
import core.godotbase, godotinternal
import core / [vector2, rect2, vector3, transform2d, planes, quats]
import core / [rect3, basis, transforms, colors, nodepaths, rids]
import core / [dictionaries, arrays, poolarrays, variants]
import nim / [godotmacros, godotnim]
export godotbase, strings, vector2, rect2, vector3, transform2d, plane, quat,
rect3, basis, transform, color, nodepath, rid, dictionary, arrays,
poolarray, variant
export godotbase, vector2, rect2, vector3, transform2d, planes, quats,
rect3, basis, transforms, colors, nodepaths, rids, dictionaries, arrays,
poolarrays, variants
export godotmacros, godotnim
# from godotcore
export print, printWarning, printError

View file

@ -1,13 +1,13 @@
# Copyright (c) 2017 Xored Software, Inc.
# Copyright 2017 Xored Software, Inc.
import godotbase
import strings, vector2, rect2, vector3, transform2d, plane, quat
import rect3, basis, transform, color, nodepath, rid
import dictionary, arrays, poolarray, variant
import internal / [
godotinternaltypes, godotarrays, godotnodepaths, godotpoolarrays,
godotstrings, godotvariants, godotdictionaries]
export godotbase, strings, vector2, rect2, vector3, transform2d, plane, quat,
rect3, basis, transform, color, nodepath, rid, dictionary, arrays,
poolarray, variant
export godotinternaltypes, godotarrays, godotnodepaths, godotpoolarrays,
godotstrings, godotvariants, godotdictionaries
const MAX_ARG_COUNT* = 128
# MethodBind API
@ -22,8 +22,8 @@ proc ptrCall*(methodBind: ptr GodotMethodBind;
importc: "godot_method_bind_ptrcall".}
proc call*(methodBind: ptr GodotMethodBind;
instance: ptr GodotObject;
args: var ptr array[MAX_ARG_COUNT, Variant]; argCount: cint;
callError: var VariantCallError): Variant {.
args: ptr array[MAX_ARG_COUNT, ptr GodotVariant]; argCount: cint;
callError: var VariantCallError): GodotVariant {.
importc: "godot_method_bind_call".}
# Script API
@ -151,7 +151,7 @@ type
hint*: GodotPropertyHint
hintString*: GodotString
usage*: GodotPropertyUsageFlags
defaultValue*: Variant
defaultValue*: GodotVariant
GodotInstanceCreateFunc* {.bycopy.} = object
createFunc*:
@ -180,7 +180,8 @@ type
meth*:
proc (obj: ptr GodotObject; methodData: pointer;
userData: pointer; numArgs: cint;
args: var ptr array[MAX_ARG_COUNT, Variant]): Variant {.noconv.}
args: var ptr array[MAX_ARG_COUNT, GodotVariant]):
GodotVariant {.noconv.}
methodData*: pointer
freeFunc*: proc (a2: pointer) {.noconv.}
@ -192,13 +193,13 @@ proc godotScriptRegisterMethod*(libHandle: pointer;
type
GodotPropertySetFunc* {.bycopy.} = object
setFunc*: proc (obj: ptr GodotObject; methodData: pointer;
userData: pointer; value: Variant) {.noconv.}
userData: pointer; value: GodotVariant) {.noconv.}
methodData*: pointer
freeFunc*: proc (a2: pointer) {.noconv.}
GodotPropertyGetFunc* {.bycopy.} = object
getFunc*: proc (obj: ptr GodotObject; methodData: pointer;
userData: pointer): Variant {.noconv.}
userData: pointer): GodotVariant {.noconv.}
methodData*: pointer
freeFunc*: proc (a2: pointer) {.noconv.}
@ -216,14 +217,15 @@ type
hint*: GodotPropertyHint
hintString*: GodotString
usage*: GodotPropertyUsageFlags
defaultValue*: Variant
defaultValue*: GodotVariant
GodotSignal* = object
name*: GodotString
numArgs*: cint
args*: ptr GodotSignalArgument
numDefaultArgs*: cint
defaultArgs*: ptr Variant
defaultArgs*: ptr GodotVariant
proc godotScriptRegisterSignal*(libHandle: pointer; name: cstring;
signal: GodotSignal) {.
@ -240,3 +242,27 @@ proc godotStackBottom*(): pointer {.
proc deinit*(o: ptr GodotObject) {.
importc: "godot_object_destroy".}
# print using Godot's error handler list
proc godotPrintError*(description: cstring; function: cstring; file: cstring;
line: cint) {.
importc: "godot_print_error".}
proc godotPrintWarning*(description: cstring; function: cstring;
file: cstring; line: cint) {.
importc: "godot_print_warning".}
proc godotPrint*(message: GodotString) {.
importc: "godot_print".}
var getClassMethodBind {.threadvar.}: ptr GodotMethodBind
proc getClassName*(o: ptr GodotObject): string =
if getClassMethodBind.isNil:
getClassMethodBind = getMethod(cstring"Object", cstring"get_class")
var ret: GodotString
getClassMethodBind.ptrCall(o, nil, addr ret)
result = $ret
if result.len > 2 and result[^2] == 'S' and result[^1] == 'W':
# There are physics type not known by ClassDB
result = result[0..result.len-3]

View file

@ -0,0 +1,115 @@
# Copyright (c) 2017 Xored Software, Inc.
import hashes
import godotinternaltypes, godotpoolarrays, godotvariants, godotstrings
proc initGodotArray*(dest: var GodotArray) {.
importc: "godot_array_new".}
proc initGodotArray*(dest: var GodotArray; pca: GodotPoolColorArray) {.
importc: "godot_array_new_pool_coloarray".}
proc initGodotArray*(dest: var GodotArray; pv3a: GodotPoolVector3Array) {.
importc: "godot_array_new_pool_vector3_array".}
proc initGodotArray*(dest: var GodotArray; pv2a: GodotPoolVector2Array) {.
importc: "godot_array_new_pool_vector2_array".}
proc initGodotArray*(dest: var GodotArray; psa: GodotPoolStringArray) {.
importc: "godot_array_new_pool_string_array".}
proc initGodotArray*(dest: var GodotArray; pra: GodotPoolRealArray) {.
importc: "godot_array_new_pool_real_array".}
proc initGodotArray*(dest: var GodotArray; pia: GodotPoolIntArray) {.
importc: "godot_array_new_pool_int_array".}
proc initGodotArray*(dest: var GodotArray; pba: GodotPoolByteArray) {.
importc: "godot_array_new_pool_byte_array".}
proc iniGodotArray*(dest: var GodotArray; src: GodotArray) {.
importc: "godot_array_new_copy".}
proc deinit*(self: var GodotArray) {.importc: "godot_array_destroy".}
proc `[]=`*(self: var GodotArray; idx: cint;
value: GodotVariant) {.
noSideEffect,
importc: "godot_array_set".}
proc `[]`*(self: GodotArray; idx: cint): GodotVariant {.
noSideEffect,
importc: "godot_array_get".}
proc mget*(self: var GodotArray; idx: cint): ptr GodotVariant {.
importc: "godot_array_operator_index".}
proc add*(self: var GodotArray; value: GodotVariant) {.
noSideEffect,
importc: "godot_array_append".}
proc clear*(self: var GodotArray) {.importc: "godot_array_clear".}
proc count*(self: GodotArray; value: GodotVariant): cint {.
noSideEffect,
importc: "godot_array_count".}
proc isEmpty*(self: GodotArray): bool {.
noSideEffect,
importc: "godot_array_empty".}
proc erase*(self: var GodotArray; value: GodotVariant) {.
importc: "godot_array_erase".}
proc first*(self: GodotArray): GodotVariant {.
noSideEffect,
importc: "godot_array_front".}
proc last*(self: GodotArray): GodotVariant {.
noSideEffect,
importc: "godot_array_back".}
proc find*(self: GodotArray; what: GodotVariant;
f: cint): cint {.
noSideEffect,
importc: "godot_array_find".}
proc findLast*(self: GodotArray; what: GodotVariant): cint {.
noSideEffect,
importc: "godot_array_find_last".}
proc contains*(self: GodotArray; value: GodotVariant): bool {.
noSideEffect,
importc: "godot_array_has".}
proc godotHash*(self: GodotArray): cint {.
noSideEffect,
importc: "godot_array_hash".}
proc hash*(self: GodotArray): Hash {.inline.} =
hash(godotHash(self))
proc insert*(self: var GodotArray; pos: cint;
value: GodotVariant) {.
importc: "godot_array_insert".}
proc reverse*(self: var GodotArray) {.importc: "godot_array_invert".}
proc popLast*(self: var GodotArray): GodotVariant {.
importc: "godot_array_pop_back".}
proc popFirst*(self: var GodotArray): GodotVariant {.
importc: "godot_array_pop_front".}
proc addLast*(self: var GodotArray; value: GodotVariant) {.
importc: "godot_array_push_back".}
proc addFirst*(self: var GodotArray; value: GodotVariant) {.
importc: "godot_array_push_front".}
proc delete*(self: var GodotArray; idx: cint) {.
importc: "godot_array_remove".}
proc setLen*(self: var GodotArray; size: cint) {.
importc: "godot_array_resize".}
proc rfind*(self: GodotArray; what: GodotVariant;
f: cint): cint {.
noSideEffect,
importc: "godot_array_rfind".}
proc len*(self: GodotArray): cint {.
noSideEffect,
importc: "godot_array_size".}
proc sort*(self: var GodotArray) {.importc: "godot_array_sort".}
proc sortCustom*(self: var GodotArray; obj: var GodotObject;
funcName: GodotString) {.
importc: "godot_array_sort_custom".}
iterator items*(arr: GodotArray): GodotVariant =
for i in 0..<arr.len:
yield arr[i]
iterator mitems*(arr: var GodotArray): var GodotVariant =
for i in 0..<arr.len:
yield arr.mget(i)[]
iterator pairs*(arr: GodotArray): tuple[key: cint, val: GodotVariant] =
for i in 0..<arr.len:
yield (i, arr[i])
iterator mpairs*(arr: var GodotArray): tuple[key: cint, val: var GodotVariant] =
for i in 0..<arr.len:
yield (i, arr.mget(i)[])

View file

@ -0,0 +1,48 @@
# Copyright (c) 2017 Xored Software, Inc.
type
GodotDictionary* {.byref.} = object
p: pointer
import godotinternaltypes, godotarrays, godotvariants, godotstrings
proc initGodotDictionary*(dest: var GodotDictionary; src: GodotDictionary) {.
importc: "godot_dictionary_new_copy".}
proc deinit*(self: var GodotDictionary) {.
importc: "godot_dictionary_destroy".}
proc initGodotDictionary*(dest: var GodotDictionary) {.
importc: "godot_dictionary_new".}
proc len*(self: GodotDictionary): cint {.
importc: "godot_dictionary_size".}
proc isEmpty*(self: GodotDictionary): bool {.
importc: "godot_dictionary_empty".}
proc clear*(self: var GodotDictionary) {.
importc: "godot_dictionary_clear".}
proc contains*(self: GodotDictionary; key: GodotVariant): bool {.
importc: "godot_dictionary_has".}
proc contains*(self: GodotDictionary; keys: GodotArray): bool {.
importc: "godot_dictionary_has_all".}
proc del*(self: var GodotDictionary; key: GodotVariant) {.
importc: "godot_dictionary_erase".}
proc godotHash*(self: GodotDictionary): cint {.
importc: "godot_dictionary_hash".}
proc keys*(self: GodotDictionary): GodotArray {.
importc: "godot_dictionary_keys".}
proc values*(self: GodotDictionary): GodotArray {.
importc: "godot_dictionary_values".}
proc `[]`*(self: GodotDictionary; key: GodotVariant): GodotVariant {.
importc: "godot_dictionary_get".}
proc `[]=`*(self: var GodotDictionary; key, value: GodotVariant) {.
importc: "godot_dictionary_set".}
proc mget*(self: var GodotDictionary; key: GodotVariant): ptr GodotVariant {.
importc: "godot_dictionary_operator_index".}
proc `==`*(self, other: GodotDictionary): bool {.
importc: "godot_dictionary_operator_equal".}
proc toJson*(self: GodotDictionary): GodotString {.
importc: "godot_dictionary_to_json".}

View file

@ -0,0 +1,8 @@
# Copyright 2017 Xored Software, Inc.
type
GodotObject* = object
GodotArray* {.byref.} = object
p: pointer
GodotVariant* {.byref.} = object
data: array[24, byte]

View file

@ -0,0 +1,32 @@
# Copyright 2017 Xored Software, Inc.
import godotstrings
type
GodotNodePath* {.byref.} = object
p: pointer
proc initGodotNodePath*(dest: var GodotNodePath; s: GodotString) {.
importc: "godot_node_path_new".}
proc deinit*(self: var GodotNodePath) {.
importc: "godot_node_path_destroy".}
proc toGodotString*(self: GodotNodePath): GodotString {.
importc: "godot_node_path_as_string".}
proc isAbsolute*(self: GodotNodePath): bool {.
noSideEffect, importc: "godot_node_path_is_absolute".}
proc nameCount*(self: GodotNodePath): cint {.
noSideEffect, importc: "godot_node_path_get_name_count".}
proc getName*(self: GodotNodePath; idx: cint): GodotString {.
noSideEffect, importc: "godot_node_path_get_name".}
proc subnameCount*(self: GodotNodePath): cint {.
noSideEffect, importc: "godot_node_path_get_subname_count".}
proc getSubname*(self: GodotNodePath; idx: cint): GodotString {.
noSideEffect, importc: "godot_node_path_get_subname".}
proc property*(self: GodotNodePath): GodotString {.
noSideEffect, importc: "godot_node_path_get_property".}
proc isEmpty*(self: GodotNodePath): bool {.
noSideEffect, importc: "godot_node_path_is_empty".}
proc `==`*(a, b: GodotNodePath): bool {.
noSideEffect, importc: "godot_node_path_operator_equal".}

View file

@ -0,0 +1,376 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotinternaltypes, godotstrings
import "../core/godotbase.nim", "../core/colors.nim",
"../core/vector2.nim", "../core/vector3.nim"
type
GodotPoolByteArray* {.byref.} = object
p: pointer
GodotPoolIntArray* {.byref.} = object
p: pointer
GodotPoolRealArray* {.byref.} = object
p: pointer
GodotPoolStringArray* {.byref.} = object
p: pointer
GodotPoolVector2Array* {.byref.} = object
p: pointer
GodotPoolVector3Array* {.byref.} = object
p: pointer
GodotPoolColorArray* {.byref.} = object
p: pointer
# byte
proc initGodotPoolByteArray*(dest: var GodotPoolByteArray) {.
importc: "godot_pool_byte_array_new".}
proc initGodotPoolByteArray*(dest: var GodotPoolByteArray;
src: GodotPoolByteArray) {.
importc: "godot_pool_byte_array_new_copy".}
proc initGodotPoolByteArray*(dest: var GodotPoolByteArray; arr: GodotArray) {.
importc: "godot_pool_byte_array_new_with_array".}
proc deinit*(self: var GodotPoolByteArray) {.
importc: "godot_pool_byte_array_destroy".}
proc add*(self: var GodotPoolByteArray;
data: uint8) {.
importc: "godot_pool_byte_array_append".}
proc addFirst*(self: var GodotPoolByteArray, data: uint8) {.
importc: "godot_pool_byte_array_push_back".}
proc add*(self: var GodotPoolByteArray;
arr: GodotPoolByteArray) {.
importc: "godot_pool_byte_array_append_array".}
proc insert*(self: var GodotPoolByteArray; idx: cint;
data: uint8): Error {.
importc: "godot_pool_byte_array_insert".}
proc delete*(self: var GodotPoolByteArray; idx: cint) {.
importc: "godot_pool_byte_array_remove".}
proc reverse*(self: var GodotPoolByteArray) {.
importc: "godot_pool_byte_array_invert".}
proc setLen*(self: var GodotPoolByteArray; size: cint) {.
importc: "godot_pool_byte_array_resize".}
proc `[]=`*(self: var GodotPoolByteArray; idx: cint; data: uint8) {.
importc: "godot_pool_byte_array_set".}
proc `[]`*(self: GodotPoolByteArray; idx: cint): uint8 {.
noSideEffect,
importc: "godot_pool_byte_array_get".}
proc len*(self: GodotPoolByteArray): cint {.
noSideEffect,
importc: "godot_pool_byte_array_size".}
iterator items*(arr: GodotPoolByteArray): byte =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolByteArray): tuple[key: cint, val: byte] =
for i in 0..<arr.len:
yield (i, arr[i])
proc initGodotPoolIntArray*(dest: var GodotPoolIntArray) {.
importc: "godot_pool_int_array_new".}
proc initGodotPoolIntArray*(dest: var GodotPoolIntArray;
src: GodotPoolIntArray) {.
importc: "godot_pool_int_array_new_copy".}
proc initGodotPoolIntArray*(dest: var GodotPoolIntArray;
arr: GodotArray) {.
importc: "godot_pool_int_array_new_with_array".}
proc deinit*(self: var GodotPoolIntArray) {.
importc: "godot_pool_int_array_destroy".}
proc add*(self: var GodotPoolIntArray; data: cint) {.
importc: "godot_pool_int_array_append".}
proc addFirst*(self: var GodotPoolIntArray; data: cint) {.
importc: "godot_pool_int_array_push_back".}
proc add*(self: var GodotPoolIntArray; arr: GodotPoolIntArray) {.
importc: "godot_pool_int_array_append_array".}
proc insert*(self: var GodotPoolIntArray; idx: cint;
data: cint): Error {.
importc: "godot_pool_int_array_insert".}
proc delete*(self: var GodotPoolIntArray; idx: cint) {.
importc: "godot_pool_int_array_remove".}
proc reverse*(self: var GodotPoolIntArray) {.
importc: "godot_pool_int_array_invert".}
proc setLen*(self: var GodotPoolIntArray; size: cint) {.
importc: "godot_pool_int_array_resize".}
proc `[]=`*(self: var GodotPoolIntArray; idx: cint; data: cint) {.
importc: "godot_pool_int_array_set".}
proc `[]`*(self: GodotPoolIntArray; idx: cint): cint {.
noSideEffect,
importc: "godot_pool_int_array_get".}
proc len*(self: GodotPoolIntArray): cint {.
noSideEffect,
importc: "godot_pool_int_array_size".}
iterator items*(arr: GodotPoolIntArray): cint =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolIntArray): tuple[key: cint, val: cint] =
for i in 0..<arr.len:
yield (i, arr[i])
# Real
proc initGodotPoolRealArray*(dest: var GodotPoolRealArray) {.
importc: "godot_pool_real_array_new".}
proc initGodotPoolRealArray*(dest: var GodotPoolRealArray;
src: GodotPoolRealArray) {.
importc: "godot_pool_real_array_new_copy".}
proc initGodotPoolRealArray*(dest: var GodotPoolRealArray; arr: GodotArray) {.
importc: "godot_pool_real_array_new_with_array".}
proc deinit*(self: var GodotPoolRealArray) {.
importc: "godot_pool_real_array_destroy".}
proc add*(self: var GodotPoolRealArray; data: float32) {.
importc: "godot_pool_real_array_append".}
proc addFirst*(self: var GodotPoolRealArray; data: float32) {.
importc: "godot_pool_real_array_push_back".}
proc add*(self: var GodotPoolRealArray; arr: GodotPoolRealArray) {.
importc: "godot_pool_real_array_append_array".}
proc insert*(self: var GodotPoolRealArray;
idx: cint; data: float32): Error {.
importc: "godot_pool_real_array_insert".}
proc delete*(self: var GodotPoolRealArray; idx: cint) {.
importc: "godot_pool_real_array_remove".}
proc reverse*(self: var GodotPoolRealArray) {.
importc: "godot_pool_real_array_invert".}
proc setLen*(self: var GodotPoolRealArray; size: cint) {.
importc: "godot_pool_real_array_resize".}
proc `[]=`*(self: var GodotPoolRealArray; idx: cint; data: float32) {.
importc: "godot_pool_real_array_set".}
proc `[]`*(self: GodotPoolRealArray; idx: cint): float32 {.
noSideEffect,
importc: "godot_pool_real_array_get".}
proc len*(self: GodotPoolRealArray): cint {.
noSideEffect,
importc: "godot_pool_real_array_size".}
iterator items*(arr: GodotPoolRealArray): float32 =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolRealArray): tuple[key: cint, val: float32] =
for i in 0..<arr.len:
yield (i, arr[i])
# String
proc initGodotPoolStringArray*(dest: var GodotPoolStringArray) {.
importc: "godot_pool_string_array_new".}
proc initGodotPoolStringArray*(dest: var GodotPoolStringArray;
src: GodotPoolStringArray) {.
importc: "godot_pool_string_array_new_copy".}
proc initGodotPoolStringArray*(dest: var GodotPoolStringArray; arr: GodotArray) {.
importc: "godot_pool_string_array_new_with_array".}
proc deinit*(self: var GodotPoolStringArray) {.
importc: "godot_pool_string_array_destroy".}
proc initGodotPoolStringArray*(): GodotPoolStringArray {.inline.} =
initGodotPoolStringArray(result)
proc initGodotPoolStringArray*(arr: GodotArray): GodotPoolStringArray {.inline.} =
initGodotPoolStringArray(result, arr)
proc add*(self: var GodotPoolStringArray; data: GodotString) {.
importc: "godot_pool_string_array_append".}
proc addFirst*(self: var GodotPoolStringArray; data: GodotString) {.
importc: "godot_pool_string_array_push_back".}
proc add*(self: var GodotPoolStringArray; arr: GodotPoolStringArray) {.
importc: "godot_pool_string_array_append_array".}
proc insert*(self: var GodotPoolStringArray;
idx: cint; data: GodotString): Error {.
importc: "godot_pool_string_array_insert".}
proc delete*(self: var GodotPoolStringArray; idx: cint) {.
importc: "godot_pool_string_array_remove".}
proc reverse*(self: var GodotPoolStringArray) {.
importc: "godot_pool_string_array_invert".}
proc setLen*(self: var GodotPoolStringArray; size: cint) {.
importc: "godot_pool_string_array_resize".}
proc `[]=`*(self: var GodotPoolStringArray; idx: cint;
data: GodotString) {.
importc: "godot_pool_string_array_set".}
proc `[]`*(self: GodotPoolStringArray; idx: cint): GodotString {.
noSideEffect,
importc: "godot_pool_string_array_get".}
proc len*(self: GodotPoolStringArray): cint {.
noSideEffect,
importc: "godot_pool_string_array_size".}
iterator items*(arr: GodotPoolStringArray): GodotString =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolStringArray): tuple[key: cint, val: GodotString] =
for i in 0..<arr.len:
yield (i, arr[i])
# Vector2
proc initGodotPoolVector2Array*(dest: var GodotPoolVector2Array) {.
importc: "godot_pool_vector2_array_new".}
proc initGodotPoolVector2Array*(dest: var GodotPoolVector2Array;
src: GodotPoolVector2Array) {.
importc: "godot_pool_vector2_array_new_copy".}
proc initGodotPoolVector2Array*(dest: var GodotPoolVector2Array;
arr: GodotArray) {.
importc: "godot_pool_vector2_array_new_with_array".}
proc deinit*(self: var GodotPoolVector2Array) {.
importc: "godot_pool_vector2_array_destroy".}
proc initGodotPoolVector2Array*(): GodotPoolVector2Array {.inline.} =
initGodotPoolVector2Array(result)
proc initGodotPoolVector2Array*(arr: GodotArray): GodotPoolVector2Array {.inline.} =
initGodotPoolVector2Array(result, arr)
proc add*(self: var GodotPoolVector2Array; data: Vector2) {.
importc: "godot_pool_vector2_array_append".}
proc addFirst*(self: var GodotPoolVector2Array; data: Vector2) {.
importc: "godot_pool_vector2_array_push_back".}
proc add*(self: var GodotPoolVector2Array;
arr: GodotPoolVector2Array) {.
importc: "godot_pool_vector2_array_append_array".}
proc insert*(self: var GodotPoolVector2Array;
idx: cint; data: Vector2): Error {.
importc: "godot_pool_vector2_array_insert".}
proc delete*(self: var GodotPoolVector2Array; idx: cint) {.
importc: "godot_pool_vector2_array_remove".}
proc reverse*(self: var GodotPoolVector2Array) {.
importc: "godot_pool_vector2_array_invert".}
proc setLen*(self: var GodotPoolVector2Array; size: cint) {.
importc: "godot_pool_vector2_array_resize".}
proc `[]=`*(self: var GodotPoolVector2Array; idx: cint;
data: Vector2) {.
importc: "godot_pool_vector2_array_set".}
proc `[]`*(self: GodotPoolVector2Array; idx: cint): Vector2 {.
noSideEffect,
importc: "godot_pool_vector2_array_get".}
proc len*(self: GodotPoolVector2Array): cint {.
noSideEffect,
importc: "godot_pool_vector2_array_size".}
iterator items*(arr: GodotPoolVector2Array): Vector2 =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolVector2Array): tuple[key: cint, val: Vector2] =
for i in 0..<arr.len:
yield (i, arr[i])
# Vector3
proc initGodotPoolVector3Array*(dest: var GodotPoolVector3Array) {.
importc: "godot_pool_vector3_array_new".}
proc initGodotPoolVector3Array*(dest: var GodotPoolVector3Array;
src: GodotPoolVector3Array) {.
importc: "godot_pool_vector3_array_new_copy".}
proc initGodotPoolVector3Array*(dest: var GodotPoolVector3Array;
arr: GodotArray) {.
importc: "godot_pool_vector3_array_new_with_array".}
proc deinit*(self: var GodotPoolVector3Array) {.
importc: "godot_pool_vector3_array_destroy".}
proc initGodotPoolVector3Array*(): GodotPoolVector3Array {.inline.} =
initGodotPoolVector3Array(result)
proc initGodotPoolVector3Array*(arr: GodotArray):
GodotPoolVector3Array {.inline.} =
initGodotPoolVector3Array(result, arr)
proc add*(self: var GodotPoolVector3Array;
data: Vector3) {.
importc: "godot_pool_vector3_array_append".}
proc addFirst*(self: var GodotPoolVector3Array;
data: Vector3) {.
importc: "godot_pool_vector3_array_push_back".}
proc add*(self: var GodotPoolVector3Array;
arr: GodotPoolVector3Array) {.
importc: "godot_pool_vector3_array_append_array".}
proc insert*(self: var GodotPoolVector3Array;
idx: cint; data: Vector3): Error {.
importc: "godot_pool_vector3_array_insert".}
proc delete*(self: var GodotPoolVector3Array; idx: cint) {.
importc: "godot_pool_vector3_array_remove".}
proc reverse*(self: var GodotPoolVector3Array) {.
importc: "godot_pool_vector3_array_invert".}
proc setLen*(self: var GodotPoolVector3Array; size: cint) {.
importc: "godot_pool_vector3_array_resize".}
proc `[]=`*(self: var GodotPoolVector3Array;
idx: cint; data: Vector3) {.
importc: "godot_pool_vector3_array_set".}
proc `[]`*(self: GodotPoolVector3Array;
idx: cint): Vector3 {.
noSideEffect,
importc: "godot_pool_vector3_array_get".}
proc len*(self: GodotPoolVector3Array): cint {.
noSideEffect,
importc: "godot_pool_vector3_array_size".}
iterator items*(arr: GodotPoolVector3Array): Vector3 =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolVector3Array): tuple[key: cint, val: Vector3] =
for i in 0..<arr.len:
yield (i, arr[i])
# Color
proc initGodotPoolColorArray*(dest: var GodotPoolColorArray) {.
importc: "godot_pool_color_array_new".}
proc initGodotPoolColorArray*(dest: var GodotPoolColorArray;
src: GodotPoolColorArray) {.
importc: "godot_pool_color_array_new_copy".}
proc initGodotPoolColorArray*(dest: var GodotPoolColorArray; arr: GodotArray) {.
importc: "godot_pool_color_array_new_with_array".}
proc deinit*(self: var GodotPoolColorArray) {.
importc: "godot_pool_color_array_destroy".}
proc initGodotPoolColorArray*(): GodotPoolColorArray {.inline.} =
initGodotPoolColorArray(result)
proc initGodotPoolColorArray*(arr: GodotArray): GodotPoolColorArray {.inline.} =
initGodotPoolColorArray(result, arr)
proc add*(self: var GodotPoolColorArray; data: Color) {.
importc: "godot_pool_color_array_append".}
proc addFirst*(self: var GodotPoolColorArray; data: Color) {.
importc: "godot_pool_color_array_push_back".}
proc add*(self: var GodotPoolColorArray; arr: GodotPoolColorArray) {.
importc: "godot_pool_color_array_append_array".}
proc insert*(self: var GodotPoolColorArray;
idx: cint; data: Color): Error {.
importc: "godot_pool_color_array_insert".}
proc delete*(self: var GodotPoolColorArray; idx: cint) {.
importc: "godot_pool_color_array_remove".}
proc reverse*(self: var GodotPoolColorArray) {.
importc: "godot_pool_color_array_invert".}
proc setLen*(self: var GodotPoolColorArray; size: cint) {.
importc: "godot_pool_color_array_resize".}
proc `[]=`*(self: var GodotPoolColorArray; idx: cint;
data: Color) {.
importc: "godot_pool_color_array_set".}
proc `[]`*(self: GodotPoolColorArray; idx: cint): Color {.
noSideEffect,
importc: "godot_pool_color_array_get".}
proc len*(self: GodotPoolColorArray): cint {.
noSideEffect,
importc: "godot_pool_color_array_size".}
iterator items*(arr: GodotPoolColorArray): Color =
for i in 0..<arr.len:
yield arr[i]
iterator pairs*(arr: GodotPoolColorArray): tuple[key: cint, val: Color] =
for i in 0..<arr.len:
yield (i, arr[i])

View file

@ -1,15 +1,11 @@
# Copyright (c) 2017 Xored Software, Inc.
import godotbase
type
GodotString* {.byref.} = object
p: pointer
proc initGodotString(dest: var GodotString) {.
importc: "godot_string_new".}
proc initGodotString(dest: var GodotString; src: GodotString) {.
importc: "godot_string_new_copy".}
proc initGodotString(dest: var GodotString; contents: cstring;
size: cint) {.
importc: "godot_string_new_data".}
@ -34,11 +30,6 @@ proc `&`*(self, b: GodotString): GodotString {.
importc: "godot_string_operator_plus".}
proc deinit*(self: var GodotString) {.importc: "godot_string_destroy".}
proc `=destroy`(self: GodotString) {.inline.} =
unsafeAddr(self).deinit()
proc `=`(self: var GodotString, other: GodotString) {.inline.} =
initGodotString(self, other)
proc `$`*(self: GodotString): string =
## Converts the ``GodotString`` into Nim string
@ -61,14 +52,3 @@ proc toGodotString*(s: cstring): GodotString {.inline.} =
initGodotString(result)
else:
initGodotString(result, s, cint(s.len + 1))
proc godotPrint(message: GodotString) {.
importc: "godot_print".}
proc print*(message: string) {.inline.} =
let s = message.toGodotString()
godotPrint(s)
proc print*(message: cstring) {.inline.} =
let s = message.toGodotString()
godotPrint(s)

View file

@ -0,0 +1,190 @@
# Copyright 2017 Xored Software, Inc.
type
VariantType* {.size: sizeof(cint), pure.} = enum
Nil, ## atomic types
Bool,
Int,
Real,
String,
# math types
Vector2, ## 5
Rect2,
Vector3,
Transform2D,
Plane,
Quat, ## 10
Rect3,
Basis,
Transform, ## misc types
Color,
NodePath, ## 15
RID,
Object,
Dictionary,
Array, ## 20
# arrays
PoolByteArray,
PoolIntArray,
PoolRealArray,
PoolStringArray,
PoolVector2Array, ## 25
PoolVector3Array,
PoolColorArray
VariantCallErrorType* {.size: sizeof(cint), pure.} = enum
OK,
InvalidMethod,
InvalidArgument,
TooManyArguments,
TooFewArguments,
InstanceIsNull
VariantCallError* = object
error*: VariantCallErrorType
argument*: cint
expected*: VariantType
import godotinternaltypes, godotpoolarrays, godotstrings, godotnodepaths,
godotdictionaries
import "../core/vector2.nim", "../core/rect2.nim",
"../core/vector3.nim", "../core/transform2d.nim", "../core/planes.nim",
"../core/quats.nim", "../core/rect3.nim", "../core/basis.nim",
"../core/transforms.nim", "../core/colors.nim", "../core/rids.nim",
"../core/rect2.nim"
proc getType*(p: GodotVariant): VariantType {.
importc: "godot_variant_get_type".}
proc initGodotVariant*(dest: var GodotVariant) {.
importc: "godot_variant_new_nil".}
proc initGodotVariant*(dest: var GodotVariant, src: GodotVariant) {.
importc: "godot_variant_new_copy".}
proc initGodotVariant*(dest: var GodotVariant; b: bool) {.
importc: "godot_variant_new_bool".}
proc initGodotVariant*(dest: var GodotVariant; i: uint64) {.
importc: "godot_variant_new_uint".}
proc initGodotVariant*(dest: var GodotVariant; i: int64) {.
importc: "godot_variant_new_int".}
proc initGodotVariant*(dest: var GodotVariant; r: cdouble) {.
importc: "godot_variant_new_real".}
proc initGodotVariant*(dest: var GodotVariant; s: GodotString) {.
importc: "godot_variant_new_string".}
proc initGodotVariant*(dest: var GodotVariant; v2: Vector2) {.
importc: "godot_variant_new_vector2".}
proc initGodotVariant*(dest: var GodotVariant; rect2: Rect2) {.
importc: "godot_variant_new_rect2".}
proc initGodotVariant*(dest: var GodotVariant; v3: Vector3) {.
importc: "godot_variant_new_vector3".}
proc initGodotVariant*(dest: var GodotVariant; t2d: Transform2D) {.
importc: "godot_variant_new_transform2d".}
proc initGodotVariant*(dest: var GodotVariant; plane: Plane) {.
importc: "godot_variant_new_plane".}
proc initGodotVariant*(dest: var GodotVariant; quat: Quat) {.
importc: "godot_variant_new_quat".}
proc initGodotVariant*(dest: var GodotVariant; rect3: Rect3) {.
importc: "godot_variant_new_rect3".}
proc initGodotVariant*(dest: var GodotVariant; basis: Basis) {.
importc: "godot_variant_new_basis".}
proc initGodotVariant*(dest: var GodotVariant; trans: Transform) {.
importc: "godot_variant_new_transform".}
proc initGodotVariant*(dest: var GodotVariant; color: Color) {.
importc: "godot_variant_new_color".}
proc initGodotVariant*(dest: var GodotVariant; nodePath: GodotNodePath) {.
importc: "godot_variant_new_node_path".}
proc initGodotVariant*(dest: var GodotVariant; rid: RID) {.
importc: "godot_variant_new_rid".}
proc initGodotVariant*(dest: var GodotVariant; obj: ptr GodotObject) {.
importc: "godot_variant_new_object".}
proc initGodotVariant*(dest: var GodotVariant; arr: GodotArray) {.
importc: "godot_variant_new_array".}
proc initGodotVariant*(dest: var GodotVariant; pba: GodotPoolByteArray) {.
importc: "godot_variant_new_pool_byte_array".}
proc initGodotVariant*(dest: var GodotVariant; pia: GodotPoolIntArray) {.
importc: "godot_variant_new_pool_int_array".}
proc initGodotVariant*(dest: var GodotVariant; pra: GodotPoolRealArray) {.
importc: "godot_variant_new_pool_real_array".}
proc initGodotVariant*(dest: var GodotVariant; psa: GodotPoolStringArray) {.
importc: "godot_variant_new_pool_string_array".}
proc initGodotVariant*(dest: var GodotVariant; pv2a: GodotPoolVector2Array) {.
importc: "godot_variant_new_pool_vector2_array".}
proc initGodotVariant*(dest: var GodotVariant; pv3a: GodotPoolVector3Array) {.
importc: "godot_variant_new_pool_vector3_array".}
proc initGodotVariant*(dest: var GodotVariant; pca: GodotPoolColorArray) {.
importc: "godot_variant_new_pool_color_array".}
proc initGodotVariant*(dest: var GodotVariant; dict: GodotDictionary) {.
importc: "godot_variant_new_dictionary".}
proc deinit*(v: var GodotVariant) {.importc: "godot_variant_destroy".}
proc asBool*(self: GodotVariant): bool {.
importc: "godot_variant_as_bool".}
proc asUInt*(self: GodotVariant): uint64 {.
importc: "godot_variant_as_uint".}
proc asInt*(self: GodotVariant): int64 {.
importc: "godot_variant_as_int".}
proc asReal*(self: GodotVariant): cdouble {.
importc: "godot_variant_as_real".}
proc asGodotString*(self: GodotVariant): GodotString {.
importc: "godot_variant_as_string".}
proc asVector2*(self: GodotVariant): Vector2 {.
importc: "godot_variant_as_vector2".}
proc asRect2*(self: GodotVariant): Rect2 {.
importc: "godot_variant_as_rect2".}
proc asVector3*(self: GodotVariant): Vector3 {.
importc: "godot_variant_as_vector3".}
proc asTransform2D*(self: GodotVariant): Transform2D {.
importc: "godot_variant_as_transform2d".}
proc asPlane*(self: GodotVariant): Plane {.
importc: "godot_variant_as_plane".}
proc asQuat*(self: GodotVariant): Quat {.
importc: "godot_variant_as_quat".}
proc asRect3*(self: GodotVariant): Rect3 {.
importc: "godot_variant_as_rect3".}
proc asBasis*(self: GodotVariant): Basis {.
importc: "godot_variant_as_basis".}
proc asTransform*(self: GodotVariant): Transform {.
importc: "godot_variant_as_transform".}
proc asColor*(self: GodotVariant): Color {.
importc: "godot_variant_as_color".}
proc asNodePath*(self: GodotVariant): GodotNodePath {.
importc: "godot_variant_as_node_path".}
proc asRID*(self: GodotVariant): RID {.
importc: "godot_variant_as_rid".}
proc asGodotObject*(self: GodotVariant): ptr GodotObject {.
importc: "godot_variant_as_object".}
proc asGodotArray*(self: GodotVariant): GodotArray {.
importc: "godot_variant_as_array".}
proc asGodotPoolByteArray*(self: GodotVariant): GodotPoolByteArray {.
importc: "godot_variant_as_pool_byte_array".}
proc asGodotPoolIntArray*(self: GodotVariant): GodotPoolIntArray {.
importc: "godot_variant_as_pool_int_array".}
proc asGodotPoolRealArray*(self: GodotVariant): GodotPoolRealArray {.
importc: "godot_variant_as_pool_real_array".}
proc asGodotPoolStringArray*(self: GodotVariant): GodotPoolStringArray {.
importc: "godot_variant_as_pool_string_array".}
proc asGodotPoolVector2Array*(self: GodotVariant): GodotPoolVector2Array {.
importc: "godot_variant_as_pool_vector2_array".}
proc asGodotPoolVector3Array*(self: GodotVariant): GodotPoolVector3Array {.
importc: "godot_variant_as_pool_vector3_array".}
proc asGodotPoolColorArray*(self: GodotVariant): GodotPoolColorArray {.
importc: "godot_variant_as_pool_color_array".}
proc asGodotDictionary*(self: GodotVariant): GodotDictionary {.
importc: "godot_variant_as_dictionary".}
proc call*(self: GodotVariant; meth: GodotString;
args: var array[128, GodotVariant]; argCount: cint;
error: var VariantCallError): GodotVariant {.
importc: "godot_variant_call".}
proc hasMethod*(self: GodotVariant; meth: GodotString): bool {.
importc: "godot_variant_has_method".}
proc `==`*(self, other: GodotVariant): bool {.
importc: "godot_variant_operator_equal".}
proc `<`*(self, other: GodotVariant): bool {.
importc: "godot_variant_operator_less".}
proc hashCompare*(self, other: GodotVariant): bool {.
importc: "godot_variant_hash_compare".}
proc booleanize*(self: GodotVariant; isValid: var bool): bool {.
importc: "godot_variant_booleanize".}

View file

@ -1,8 +1,8 @@
# Copyright 2017 Xored Software, Inc.
import macros, tables, typetraits
import "../core/godotcore.nim"
import godotnim
import "../godotinternal.nim", "../internal/godotvariants.nim"
import godotnim, "../core/variants.nim"
type
VarDecl = ref object
@ -64,18 +64,6 @@ iterator pragmas(node: NimNode):
elif node[index].kind == nnkIdent:
yield (node[index].ident, nil, index)
proc hasPragma(statement: NimNode, pname: string): bool =
## Checks if the pragma is present in the `statement`
if not (RoutineNodes.contains(statement.kind) or
statement.kind == nnkPragmaExpr):
return false
var pragmas = if RoutineNodes.contains(statement.kind): statement.pragma()
else: statement[1]
let pnameIdent = !pname
for ident, val, i in pragmas(pragmas):
if ident == pnameIdent:
return true
proc removePragma(statement: NimNode, pname: string): bool =
## Removes the pragma from the node and returns whether pragma was removed
if not (RoutineNodes.contains(statement.kind) or
@ -205,7 +193,9 @@ macro invokeVarArgs(procIdent, objIdent;
## error(...)
template conv(procLit, argT, argSeq, idx, argIdent, errIdent) =
let (argIdent, errIdent) = godotToNim[argT](argSeq[idx])
let v = newVariant(argSeq[idx])
v.markNoDeinit() # args should be destroyed externally
let (argIdent, errIdent) = godotToNim[argT](v)
if errIdent != ConversionResult.OK:
let errorKind = if errIdent == ConversionResult.TypeError: "a type error"
else: "a range error"
@ -215,6 +205,14 @@ macro invokeVarArgs(procIdent, objIdent;
" of Godot type " & $argSeq[idx].getType())
return
# these help to avoid exporting internal modules
# thanks to closed symbol binding
template initGodotVariantCall(result) =
initGodotVariant(result)
template initGodotVariantCall(result, src) =
initGodotVariant(result, src)
result = newNimNode(nnkCaseStmt)
result.add(numArgsIdent)
for i in minArgs..maxArgs:
@ -231,12 +229,13 @@ macro invokeVarArgs(procIdent, objIdent;
argSeqIdent, idx, argIdent, errIdent)))
invocation.add(argIdent)
if hasReturnValue:
branchBody.add(newNimNode(nnkAsgn).add(ident("result"),
newCall("toGodot", invocation)))
let theCall = newNimNode(nnkBracketExpr).add(newNimNode(nnkDotExpr).add(
newCall("toGodot", newCall("newVariant", invocation)),
ident("godotVariant")))
branchBody.add(getAst(initGodotVariantCall(ident("result"), theCall)))
else:
branchBody.add(invocation)
branchBody.add(newNimNode(nnkAsgn).add(ident("result"),
newCall("variant")))
branchBody.add(getAst(initGodotVariantCall(ident("result"))))
branch.add(branchBody)
result.add(branch)
@ -306,8 +305,10 @@ template registerGodotField(classNameLit, classNameIdent, propNameLit,
hintIdent, usageIdent, hasDefaultValue,
defaultValueNode) =
proc setFuncIdent(obj: ptr GodotObject, methData: pointer,
nimPtr: pointer, val: Variant) {.noconv.} =
let (nimVal, err) = godotToNim[propTypeIdent](val)
nimPtr: pointer, val: GodotVariant) {.noconv.} =
let variant = newVariant(val)
variant.markNoDeinit()
let (nimVal, err) = godotToNim[propTypeIdent](variant)
case err:
of ConversionResult.OK:
cast[classNameIdent](nimPtr).propNameIdent = nimVal
@ -321,8 +322,10 @@ template registerGodotField(classNameLit, classNameIdent, propNameLit,
printError(errStr)
proc getFuncIdent(obj: ptr GodotObject, methData: pointer,
nimPtr: pointer): Variant {.noconv.} =
result = nimToGodot(cast[classNameIdent](nimPtr).propNameIdent)
nimPtr: pointer): GodotVariant {.noconv.} =
let variant = nimToGodot(cast[classNameIdent](nimPtr).propNameIdent)
variant.markNoDeinit()
result = variant.godotVariant[]
let setFunc = GodotPropertySetFunc(
setFunc: setFuncIdent
@ -436,8 +439,8 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
argTypes, methFuncIdent, hasReturnValue) =
proc methFuncIdent(obj: ptr GodotObject, methodData: pointer,
userData: pointer, numArgs: cint,
args: var ptr array[MAX_ARG_COUNT, Variant]):
Variant {.noconv.} =
args: var ptr array[MAX_ARG_COUNT, GodotVariant]):
GodotVariant {.noconv.} =
let self = cast[classNameIdent](userData)
when defined(release):
invokeVarArgs(methodNameIdent, self, minArgs, maxArgs, numArgs,
@ -448,8 +451,7 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
args, argTypes, hasReturnValue)
except:
let ex = getCurrentException()
printError("Unhandled Nim exception (" &
$ex.name & "): " &
printError("Unhandled Nim exception (" & $ex.name & "): " &
ex.msg & "\n" & ex.getStackTrace())
let meth = GodotInstanceMethod(

View file

@ -1,13 +1,14 @@
# Copyright 2017 Xored Software, Inc.
import tables, typetraits, macros
import "../core/godotbase.nim", "../core/godotcore.nim"
import "../core/strings.nim", "../core/vector2.nim", "../core/rect2.nim",
import "../core/godotbase.nim"
import "../core/vector2.nim", "../core/rect2.nim",
"../core/vector3.nim", "../core/transform2d.nim",
"../core/plane.nim", "../core/quat.nim", "../core/rect3.nim",
"../core/basis.nim", "../core/transform.nim", "../core/color.nim",
"../core/nodepath.nim", "../core/rid.nim", "../core/dictionary.nim",
"../core/arrays.nim", "../core/poolarray.nim", "../core/variant.nim"
"../core/planes.nim", "../core/quats.nim", "../core/rect3.nim",
"../core/basis.nim", "../core/transforms.nim", "../core/colors.nim",
"../core/nodepaths.nim", "../core/rids.nim", "../core/dictionaries.nim",
"../core/arrays.nim", "../core/poolarrays.nim", "../core/variants.nim"
import "../godotinternal.nim"
## Definition of NimGodotObject and converters for built-in types.
@ -48,6 +49,24 @@ type
isNative: bool
isRef: bool
template printWarning*(warn: typed) =
let (filename, line) = instantiationInfo()
godotPrintWarning(cstring($warn), nil, cstring(filename), line.cint)
template printError*(err: typed) =
let (filename, line) = instantiationInfo()
godotPrintError(cstring($err), nil, cstring(filename), line.cint)
proc print*(message: string) {.inline.} =
var s = message.toGodotString()
godotPrint(s)
s.deinit()
proc print*(message: cstring) {.inline.} =
var s = message.toGodotString()
godotPrint(s)
s.deinit()
var classRegistry {.threadvar.}: TableRef[cstring, ObjectInfo]
static:
@ -55,16 +74,8 @@ static:
var nativeClasses {.compileTime.} = newSeq[string]()
var refClasses* {.compileTime.} = newSeq[string]()
var getClassMethodBind {.threadvar.}: ptr GodotMethodBind
proc getClassName*(o: ptr GodotObject): string =
if getClassMethodBind.isNil:
getClassMethodBind = getMethod(cstring"Object", cstring"get_class")
var ret: GodotString
getClassMethodBind.ptrCall(o, nil, addr ret)
result = $ret
if result.endsWith("SW"):
# There are physics type not known by ClassDB
result = result[0..result.len-3]
proc getClassName*(o: NimGodotObject): string =
o.godotObject.getClassName()
var unreferenceMethodBind {.threadvar.}: ptr GodotMethodBind
proc unreference(o: ptr GodotObject): bool =
@ -176,6 +187,15 @@ proc asNimGodotObject*[T: NimGodotObject](godotObject: ptr GodotObject): T =
result = newNimGodotObject[T](
godotObject, cstring(godotObject.getClassName()))
proc newVariant*(obj: NimGodotObject): Variant {.inline.} =
newVariant(obj.godotObject)
proc asObject*[T: NimGodotObject](v: Variant): T {.inline.} =
asNimGodotObject[T](v.asGodotObject())
proc asObject*(v: Variant, T: typedesc[NimGodotObject]): T {.inline.} =
asNimGodotObject[T](v.asGodotObject())
proc `as`*[T: NimGodotObject](obj: NimGodotObject, t: typedesc[T]): T =
## Converts the ``obj`` into the specified type.
## Returns ``nil`` if the conversion cannot be performed
@ -250,7 +270,7 @@ proc newConversionError*(err: ConversionResult): ref ValueError =
result = newException(ValueError, msg)
proc removeGodotObject*(nimObj: NimGodotObject) =
proc removeGodotObject*(nimObj: NimGodotObject) {.inline.} =
## Used from destructor
nimObj.godotObject = nil
@ -269,12 +289,12 @@ proc godotVariantType*(T: typedesc[NimGodotObject]): VariantType {.inline.} =
VariantType.Object
proc toGodot*(self: NimGodotObject): Variant {.inline.} =
variant(self.godotObject)
newVariant(self.godotObject)
proc fromGodot*[T: NimGodotObject](self: var T,
val: Variant): ConversionResult =
if val.getType() == VariantType.Object:
let objPtr = val.asObject()
let objPtr = val.asGodotObject()
self = asNimGodotObject[T](objPtr)
if self.isNil:
result = ConversionResult.TypeError
@ -287,19 +307,21 @@ proc godotVariantType*(T: typedesc[enum]): VariantType {.inline.} =
VariantType.Int
proc toGodot*[T: enum](self: T): Variant {.inline.} =
variant(int64(ord(self)))
newVariant(int64(ord(self)))
proc fromGodot*[T: enum](self: var T, val: Variant): ConversionResult =
proc fromGodot*[T: enum](self: var T,
val: Variant): ConversionResult {.inline.} =
if val.getType() == VariantType.Int:
self = T(val.asInt())
else:
result = ConversionResult.TypeError
proc toGodot*(self: Variant): Variant {.inline.} =
result = self
self
proc fromGodot*(self: var Variant, val: Variant): ConversionResult {.inline.} =
self = val
proc fromGodot*(self: var Variant,
val: Variant): ConversionResult {.inline.} =
self = newVariant(val)
proc godotVariantType*(T: typedesc[SomeGodotOrNum]): VariantType {.inline.} =
when T is SomeSignedInt or T is SomeUnsignedInt:
@ -330,12 +352,12 @@ proc godotVariantType*(T: typedesc[SomeGodotOrNum]): VariantType {.inline.} =
VariantType.Transform
elif T is Color:
VariantType.Color
elif T is NodePath:
VariantType.NodePath
elif T is RID:
VariantType.RID
elif T is GodotObject:
elif T is ptr GodotObject:
VariantType.Object
elif T is NodePath:
VariantType.NodePath
elif T is Dictionary:
VariantType.Dictionary
elif T is Array:
@ -358,7 +380,7 @@ proc godotVariantType*(T: typedesc[SomeGodotOrNum]): VariantType {.inline.} =
VariantType.Nil
proc toGodot*[T: SomeGodotOrNum](val: T): Variant {.inline.} =
result = variant(val)
newVariant(val)
proc fromGodot*[T: SomeSignedInt or SomeUnsignedInt](
self: var T, val: Variant): ConversionResult =
@ -376,7 +398,7 @@ proc fromGodot*[T: SomeSignedInt or SomeUnsignedInt](
proc fromGodot*[T: SomeFloat](self: var T, val: Variant): ConversionResult =
if val.getType() == VariantType.Real:
self = val.asReal()
self = T(val.asReal())
elif val.getType() == VariantType.Int:
self = T(val.asInt())
else:
@ -441,13 +463,15 @@ proc godotVariantType*(T: typedesc[string]): VariantType {.inline.} =
VariantType.String
proc toGodot*(s: string): Variant {.inline.} =
variant(s.toGodotString())
newVariant(s)
proc fromGodot*(s: var string, val: Variant): ConversionResult =
if val.getType() != VariantType.String:
result = ConversionResult.TypeError
if val.getType() == VariantType.String:
s = val.asString()
elif val.getType() == VariantType.Nil:
s = nil
else:
s = $val.asGodotString()
result = ConversionResult.TypeError
proc godotVariantType*(T: typedesc[seq]): VariantType =
VariantType.Array
@ -456,11 +480,11 @@ proc godotVariantType*(T: typedesc[array]): VariantType =
VariantType.Array
proc toGodot*[T](s: openarray[T]): Variant =
var arr = initArray()
var arr = newArray()
mixin toGodot
for item in s:
arr.add(toGodot(item))
result = variant(arr)
result = newVariant(arr)
proc fromGodot*[T](s: var seq[T], val: Variant): ConversionResult =
if val.getType() != VariantType.Array:
@ -492,11 +516,11 @@ proc godotVariantType*(T: typedesc[Table|TableRef]): VariantType {.inline.} =
VariantType.Dictionary
proc toGodot*[T: Table or TableRef](t: T): Variant =
var dict = initDictionary()
var dict = newDictionary()
mixin toGodot
for k, v in t.pairs():
dict[toGodot(k)] = toGodot(v)
result = variant(dict)
result = newVariant(dict)
proc fromGodot*[T: Table or TableRef](t: var T, val: Variant): ConversionResult =
if val.getType() != VariantType.Dictionary:
@ -563,7 +587,7 @@ proc godot_gdnative_init(options: ptr GodotNativeInitOptions) {.
proc godot_gdnative_terminate(options: ptr GodotNativeTerminateOptions) {.
cdecl, exportc, dynlib.} =
deallocHeap(runFinalizers = false, allowGcAfterwards = false)
deallocHeap(runFinalizers = not options[].inEditor, allowGcAfterwards = false)
proc godot_nativescript_thread_enter() {.
cdecl, exportc, dynlib.} =

View file

@ -4,7 +4,7 @@ import asyncdispatch
import godot, node
gdobj NimRuntime of Node:
method process*(delta: float32) =
method process*(delta: float64) =
if asyncdispatch.hasPendingOperations():
poll(0)
GC_step(2000, false, 0)