1 module spine.skeleton_attach;
2 
3 import spine.skeleton;
4 import std.string: toStringz;
5 import std.exception: enforce;
6 
7 package static Skeleton[size_t] attachedSkeletons;
8 private static size_t skeletonsCount = 0;
9 
10 alias SkAtt = spSkeletonAttachment_unofficial;
11 
12 void setAttachment(Skeleton si, string creatingAttachmentName, spSlot* slot, Skeleton addingSkeleton)
13 {
14     // It is need to remove old skeleton attachment from array?
15     if(slot.attachment !is null && slot.attachment.type == spAttachmentType.SKELETON)
16     {
17         SkAtt* a = cast(SkAtt*) slot.attachment;
18 
19         attachedSkeletons.remove(a.attachedSkeletonIdx);
20     }
21 
22     if(addingSkeleton is null)
23     {
24         spSlot_setAttachment(slot, null);
25     }
26     else
27     {
28         attachedSkeletons[skeletonsCount] = addingSkeleton;
29 
30         SkAtt* attachment = createSkeletonAttachment(creatingAttachmentName, skeletonsCount);
31 
32         spSlot_setAttachment(slot, &attachment._super);
33 
34         skeletonsCount++;
35     }
36 }
37 
38 // TODO: it is need to add ability removing of attach
39 private SkAtt* createSkeletonAttachment(string creatingAttachmentName, size_t attachedSkeletonIdx)
40 {
41     SkAtt* sa = cast(SkAtt*) spineCalloc(SkAtt.sizeof, 1, __FILE__, __LINE__);
42 
43     _spAttachment_init(&sa._super, creatingAttachmentName.toStringz, spAttachmentType.SKELETON, &disposeSkeletonAttachment);
44     sa.attachedSkeletonIdx = attachedSkeletonIdx;
45 
46     return sa;
47 }
48 
49 private extern (C) void disposeSkeletonAttachment(spAttachment* attachment)
50 {
51 	SkAtt* self = cast(SkAtt*) attachment;
52 
53 	_spAttachment_deinit(attachment);
54     _spFree(self);
55 }
56 
57 private void* spineCalloc(size_t num, size_t size, string fileName, int line)
58 {
59     assert(size > 0);
60 
61     auto ret = _spCalloc(num, size, fileName.toStringz, line);
62 
63     enforce(ret !is null);
64 
65     return ret;
66 }
67 
68 package extern (C):
69 
70 struct spSkeletonAttachment_unofficial
71 {
72     spAttachment _super;
73     alias _super this;
74 
75     size_t attachedSkeletonIdx;
76 }
77 
78 private:
79 
80 void* _spCalloc (size_t num, size_t size, const(char)* file, int line);
81 void _spFree (void* ptr);
82 
83 void _spAttachment_init (spAttachment* self, const(char)* name, spAttachmentType type, void function(spAttachment* self) dispose);
84 void _spAttachment_deinit (spAttachment* self);
85 
86 void spSlot_setAttachment (spSlot* self, spAttachment* attachment);