-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathQuickTiGame2dSprite.java
More file actions
1292 lines (1042 loc) · 37.5 KB
/
QuickTiGame2dSprite.java
File metadata and controls
1292 lines (1042 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 quicktigame2d project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the project nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
package com.googlecode.quicktigame2d;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import com.googlecode.quicktigame2d.opengl.GLHelper;
import android.graphics.BitmapFactory;
import android.util.Log;
public class QuickTiGame2dSprite {
protected boolean loaded = false;
protected boolean debug = false;
protected boolean hasTexture = false;
protected boolean hasSheet = false;
protected boolean isPackedAtlas = false;
protected boolean animating = false;
protected String animationName;
protected QuickTiGame2dAnimationFrame currentAnimation;
protected HashMap<String, QuickTiGame2dAnimationFrame> animations = new HashMap<String, QuickTiGame2dAnimationFrame>();
protected HashMap<String, QuickTiGame2dImagePackInfo> imagepacks = new HashMap<String, QuickTiGame2dImagePackInfo>();
protected List<String> imagepacks_names = new ArrayList<String>();
protected String selectedFrameName;
protected float[] vertex_tex_coords = new float[8];
protected int[] frames_vbos = new int[1];
protected float[] param_color = new float[4];
protected float[] param_rotate = new float[5];
protected float[] param_scale = new float[6];
protected QuickTiGame2dGameView view;
protected String image;
protected float x = 0;
protected float y = 0;
protected float z = 0;
protected int width = 0;
protected int height = 0;
protected int border = 0;
protected int margin = 0;
protected float orthFactorX = 1.0f;
protected float orthFactorY = 1.0f;
protected int frameCount = 1;
protected int frameIndex = 0;
protected int nextFrameIndex = 0;
protected boolean frameIndexChanged = false;
protected int srcBlendFactor = GL11.GL_ONE;
protected int dstBlendFactor = GL11.GL_ONE_MINUS_SRC_ALPHA;
protected List<QuickTiGame2dTransform> transforms = new ArrayList<QuickTiGame2dTransform>();
protected List<QuickTiGame2dTransform> transformsToBeRemoved = new ArrayList<QuickTiGame2dTransform>();
protected List<QuickTiGame2dSprite> children = new ArrayList<QuickTiGame2dSprite>();
protected boolean relativeToTransformParent;
protected float relativeToTransformParentX;
protected float relativeToTransformParentY;
protected boolean followParentTransformPosition = true;
protected boolean followParentTransformRotation = true;
protected boolean followParentTransformRotationCenter = true;
protected boolean followParentTransformScale = true;
protected boolean followParentTransformSize = true;
protected boolean followParentTransformColor = true;
protected boolean followParentTransformFrameIndex = false;
public QuickTiGame2dSprite() {
// color param RGBA
param_color[0] = 1.0f;
param_color[1] = 1.0f;
param_color[2] = 1.0f;
param_color[3] = 1.0f;
// rotate angle, center x, center y, center z, axis
param_rotate[0] = 0;
param_rotate[1] = 0;
param_rotate[2] = 0;
param_rotate[3] = 0;
param_rotate[4] = QuickTiGame2dConstant.AXIS_Z;
// scale param x, y, z, center x, center y, center z
param_scale[0] = 1;
param_scale[1] = 1;
param_scale[2] = 1;
param_scale[3] = 0;
param_scale[4] = 0;
param_scale[5] = 0;
relativeToTransformParent = false;
relativeToTransformParentX = 0;
relativeToTransformParentY = 0;
}
public QuickTiGame2dTexture getTexture() {
return view.getTextureFromCache(image);
}
public void onLoad(GL10 gl, QuickTiGame2dGameView view) {
if (loaded) return;
this.view = view;
QuickTiGame2dTexture aTexture = view.getTextureFromCache(image);
// if texture is not yet cached, try to load texture here
if (aTexture == null && image != null) {
view.loadTexture(gl, image);
aTexture = view.getTextureFromCache(image);
}
if (aTexture != null) {
hasTexture = true;
if (width == 0) width = aTexture.getWidth();
if (height == 0) height = aTexture.getHeight();
if (hasSheet && !isPackedAtlas) {
setFrameCount((int) ((int)Math.floor(aTexture.getWidth() / (float)(width + border))
* Math.floor(aTexture.getHeight() /(float)(height + border))));
}
} else {
hasTexture = false;
hasSheet = false;
isPackedAtlas = false;
}
createTextureBuffer(gl);
bindVertex(gl);
if (animating && currentAnimation != null) {
setFrameIndex(currentAnimation.current());
}
if (isPackedAtlas && selectedFrameName != null) {
selectFrame(selectedFrameName);
}
if (debug && hasTexture && !aTexture.isSnapshot()) Log.d(Quicktigame2dModule.LOG_TAG, String.format("load Sprite: %s", image));
if (hasTexture && !aTexture.isSnapshot()) view.onLoadSprite(image);
if (debug) GLHelper.checkError(gl);
loaded = true;
}
public void onDrawFrame(GL10 gl10, boolean fpsTimeElapsed) {
GL11 gl = (GL11)gl10;
// sprite had texture but it is unloaded
if (hasTexture && getTexture() == null) {
return;
}
if (frameIndexChanged) {
frameIndex = nextFrameIndex;
frameIndexChanged = false;
}
if (animating && currentAnimation != null) {
QuickTiGame2dAnimationFrame animation = currentAnimation;
if (animation.getLastOnAnimationDelta(QuickTiGame2dGameView.uptime()) >= animation.getInterval()) {
setFrameIndex(animation.getNextIndex(frameCount, frameIndex));
animation.setLastOnAnimationInterval(QuickTiGame2dGameView.uptime());
}
}
if (frames_vbos[frameIndex] <= 0) {
bindVertex(gl10);
}
synchronized (transforms) {
if (fpsTimeElapsed) {
onTransform();
}
}
gl.glMatrixMode (GL11.GL_MODELVIEW);
gl.glLoadIdentity ();
// update colors
if (srcBlendFactor == GL11.GL_ONE && dstBlendFactor == GL11.GL_ONE_MINUS_SRC_ALPHA) {
gl.glColor4f(param_color[0] * param_color[3],
param_color[1] * param_color[3],
param_color[2] * param_color[3], param_color[3]);
} else {
gl.glColor4f(param_color[0], param_color[1], param_color[2], param_color[3]);
}
// update position
gl.glTranslatef(x * orthFactorX, y * orthFactorY, 0);
// rotate angle, center x, center y, center z, axis
gl.glTranslatef(param_rotate[1], param_rotate[2], param_rotate[3]);
if (param_rotate[4] == QuickTiGame2dConstant.AXIS_X) {
gl.glRotatef(param_rotate[0], 1, 0, 0);
} else if (param_rotate[4] == QuickTiGame2dConstant.AXIS_Y) {
gl.glRotatef(param_rotate[0], 0, 1, 0);
} else {
gl.glRotatef(param_rotate[0], 0, 0, 1);
}
gl.glTranslatef(-param_rotate[1], -param_rotate[2], -param_rotate[3]);
// scale param x, y, z, center x, center y, center z
gl.glTranslatef(param_scale[3], param_scale[4], param_scale[5]);
gl.glScalef(param_scale[0], param_scale[1], param_scale[2]);
gl.glTranslatef(-param_scale[3], -param_scale[4], -param_scale[5]);
// update width and height
gl.glScalef(width, height, 1);
// bind vertex positions
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, QuickTiGame2dGameView.sharedPositionPointer());
gl.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
// bind a texture
if (hasTexture && getTexture() != null) {
gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glBindTexture(GL11.GL_TEXTURE_2D, getTexture().getTextureId());
// bind texture coords
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, frames_vbos[frameIndex]);
gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);
} else {
gl.glDisable(GL11.GL_TEXTURE_2D);
}
// bind indices
gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, QuickTiGame2dGameView.sharedIndexPointer());
// draw sprite
gl.glDrawElements(GL11.GL_TRIANGLE_FAN, 4, GL11.GL_UNSIGNED_SHORT, 0);
gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}
public void onDispose() {
if (!loaded) return;
if (debug && hasTexture && !getTexture().isSnapshot()) Log.d(Quicktigame2dModule.LOG_TAG, String.format("unload Sprite: %s", image));
if (hasTexture && !getTexture().isSnapshot()) view.onUnloadSprite(image);
QuickTiGame2dGameView.deleteGLBuffer(frames_vbos);
loaded = false;
}
public boolean pauseAt(int index) {
if (!setFrameIndex(index)) {
return false;
}
animating = false;
return true;
}
public void pause() {
animating = false;
}
public void stop() {
animating = false;
setFrameIndex(0);
}
protected void addAnimation(QuickTiGame2dAnimationFrame animation) {
deleteAnimation(animation.getName());
animations.put(animation.getName(), animation);
}
private boolean setAnimation(String name) {
if (getAnimation(name) == null) {
animationName = null;
return false;
} else {
animationName = name;
}
return true;
}
public QuickTiGame2dAnimationFrame getAnimation(String name) {
return animations.get(name);
}
public boolean deleteAnimation(String name) {
QuickTiGame2dAnimationFrame animation = getAnimation(name);
if (animation == null) return false;
animations.remove(name);
return true;
}
public void deleteAnimations() {
animations.clear();
}
private boolean enableAnimation(boolean enable) {
animating = enable;
currentAnimation = null;
if (enable) {
if (animationName == null) return false;
QuickTiGame2dAnimationFrame animation = getAnimation(animationName);
if (animation == null) {
return false;
} else {
currentAnimation = animation;
animation.setLastOnAnimationInterval(QuickTiGame2dGameView.uptime());
setFrameIndex(animation.getStart());
}
}
return true;
}
public boolean isAnimationFinished() {
if (animating && currentAnimation != null) {
return currentAnimation.isFinished();
}
return true;
}
public void animate(int start, int count, int interval) {
animate(start, count, interval, 0);
}
public void animate(int start, int count, int interval, int loop) {
QuickTiGame2dAnimationFrame animation = new QuickTiGame2dAnimationFrame();
animation.setName(QuickTiGame2dConstant.DEFAULT_ANIMATION_NAME);
animation.setStart(start);
animation.setCount(count);
animation.setInterval(interval);
animation.setLoop(loop);
addAnimation(animation);
setAnimation(animation.getName());
enableAnimation(true);
}
public void animate(int[] frames, int interval) {
animate(frames, interval, 0);
}
public void animate(int[] frames, int interval, int loop) {
QuickTiGame2dAnimationFrame animation = new QuickTiGame2dAnimationFrame();
animation.setName(QuickTiGame2dConstant.DEFAULT_ANIMATION_NAME);
animation.setCount(frames.length);
animation.setInterval(interval);
animation.setLoop(loop);
animation.initializeIndividualFrames();
for (int i = 0; i < frames.length; i++) {
animation.setFrame(i, frames[i]);
}
addAnimation(animation);
setAnimation(animation.getName());
enableAnimation(true);
}
public boolean setFrameIndex(int index, boolean force) {
if (loaded && frameCount <= index) {
return false;
}
if (force) {
frameIndex = index;
} else {
nextFrameIndex = index;
frameIndexChanged = true;
}
if (isPackedAtlas) {
QuickTiGame2dImagePackInfo info = getImagePack(imagepacks_names.get(index));
this.width = info.getWidth();
this.height = info.getHeight();
}
return true;
}
public boolean setFrameIndex(int index) {
return setFrameIndex(index, false);
}
protected void bindVertex(GL10 gl10) {
GL11 gl = (GL11)gl10;
vertex_tex_coords[0] = getTexCoordStartX();
vertex_tex_coords[1] = flipY() ? getTexCoordStartY() : getTexCoordEndY();
vertex_tex_coords[2] = getTexCoordStartX();
vertex_tex_coords[3] = flipY() ? getTexCoordEndY() : getTexCoordStartY();
vertex_tex_coords[4] = getTexCoordEndX();
vertex_tex_coords[5] = flipY() ? getTexCoordEndY() : getTexCoordStartY();
vertex_tex_coords[6] = getTexCoordEndX();
vertex_tex_coords[7] = flipY() ? getTexCoordStartY() : getTexCoordEndY();
if (frames_vbos[frameIndex] == 0) {
gl.glGenBuffers(1, frames_vbos, frameIndex);
}
FloatBuffer vertexBuffer = GLHelper.createFloatBuffer(vertex_tex_coords);
gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, frames_vbos[frameIndex]);
gl.glBufferData(GL11.GL_ARRAY_BUFFER, 4 * vertex_tex_coords.length, vertexBuffer, GL11.GL_STATIC_DRAW);
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
if (debug) GLHelper.checkError(gl);
}
protected void createTextureBuffer(GL10 gl10) {
GL11 gl = (GL11)gl10;
frames_vbos = new int[frameCount];
for (int i = 0; i < frameCount; i++) {
frames_vbos[i] = 0;
}
gl.glGenBuffers(1, frames_vbos, frameIndex);
}
protected boolean flipY() {
return hasTexture && getTexture().isSnapshot();
}
private int tex_coord_frame_startX() {
if (isPackedAtlas && !flipY()) {
return getImagePack(imagepacks_names.get(frameIndex)).getX();
} else if (isPackedAtlas) {
return getTexture().getHeight() - height - getImagePack(imagepacks_names.get(frameIndex)).getY();
}
int xcount = (int)Math.round((getTexture().getWidth() - (margin * 2) + border) / (float)(width + border));
int xindex = frameIndex % xcount;
return ((border + width) * xindex) + margin;
}
private int tex_coord_frame_startY() {
if (isPackedAtlas) {
return getImagePack(imagepacks_names.get(frameIndex)).getY();
}
int xcount = (int)Math.round((getTexture().getWidth() - (margin * 2) + border) / (float)(width + border));
int ycount = (int)Math.round((getTexture().getHeight() - (margin * 2) + border) / (float)(height + border));
int yindex = flipY() ? ycount - (frameIndex / xcount) - 1 : (frameIndex / xcount);
return ((border + height) * yindex) + margin;
}
protected float getTexelHalfX() {
if (hasTexture) {
return (float)((1.0 / getTexture().getGlWidth()) * 0.5);
} else {
return 0;
}
}
protected float getTexelHalfY (){
if (hasTexture) {
return (float)((1.0 / getTexture().getGlHeight()) * 0.5);
} else {
return 0;
}
}
private float getTexCoordStartX(){
if (hasSheet) {
return tex_coord_frame_startX() / (float)getTexture().getGlWidth() + getTexelHalfX();
} else {
return getTexelHalfX();
}
}
private float getTexCoordEndX() {
if (!hasTexture) {
return 1 - getTexelHalfX();
} else if (hasSheet) {
return (float)(tex_coord_frame_startX() + width) / (float)getTexture().getGlWidth() - getTexelHalfX();
} else {
return (float)getTexture().getWidth() / (float)getTexture().getGlWidth() - getTexelHalfX();
}
}
private float getTexCoordStartY() {
if (!hasTexture) {
return 1 - getTexelHalfY();
} else if (hasSheet) {
return (float)(tex_coord_frame_startY() + height) / (float)getTexture().getGlHeight() - getTexelHalfY();
} else {
return (float)getTexture().getHeight() / (float)getTexture().getGlHeight() - getTexelHalfY();
}
}
private float getTexCoordEndY() {
if (hasSheet) {
return tex_coord_frame_startY() / (float)getTexture().getGlHeight() + getTexelHalfY();
} else {
return getTexelHalfY();
}
}
public void setX(float x) {
this.x = x;
}
public float getX() {
return x;
}
public void setY(float y) {
this.y = y;
}
public float getY() {
return y;
}
public void setZ(float z) {
this.z = z;
QuickTiGame2dScene.setSortOrderDirty();
}
public float getZ() {
return z;
}
public void setImage(String image) {
this.image = image;
// update image size
if (image.endsWith(".xml")) {
isPackedAtlas = true;
hasSheet = true;
if (!loadPackedAtlasXml(frameIndex)) {
if (debug) Log.w(Quicktigame2dModule.LOG_TAG, String.format("packed atlas loading failed: %s", image));
}
} else {
BitmapFactory.Options options = QuickTiGame2dUtil.getBitmapOptions(image, debug);
if (width == 0) width = options.outWidth;
if (height == 0) height = options.outHeight;
}
}
private boolean loadPackedAtlasXml(int frameIndex) {
InputStream is = null;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
QuickTiGame2dImagePackParser handler = new QuickTiGame2dImagePackParser(frameIndex, this);
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(handler);
is = QuickTiGame2dUtil.getFileInputStream(image);
xr.parse(new InputSource(new BufferedInputStream(is)));
setFrameCount(imagepacks_names.size());
} catch (Exception e) {
if (debug) Log.w(Quicktigame2dModule.LOG_TAG, String.format("failed to load packed atlas: %s", image), e);
return false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// nothing to do
}
}
}
return true;
}
public void color(float red, float green, float blue) {
param_color[0] = red;
param_color[1] = green;
param_color[2] = blue;
}
public void hide() {
setAlpha(0);
}
public void show() {
setAlpha(1);
}
public void move(float x, float y) {
this.x = x;
this.y = y;
}
public void setAlpha(float alpha) {
param_color[3] = alpha;
}
public float getAlpha() {
return param_color[3];
}
public void setAngle(float angle) {
param_rotate[0] = angle;
}
public float getAngle() {
return param_rotate[0];
}
public void rotate(float angle) {
rotate(angle, (width * 0.5f), (height * 0.5f));
}
public void rotateZ(float angle) {
rotate(angle);
param_rotate[4] = QuickTiGame2dConstant.AXIS_Z;
}
public void rotateY(float angle) {
rotate(angle);
param_rotate[4] = QuickTiGame2dConstant.AXIS_Y;
}
public void rotateX(float angle) {
rotate(angle);
param_rotate[4] = QuickTiGame2dConstant.AXIS_X;
}
public void rotate(float angle, float centerX, float centerY) {
rotate(angle, centerX, centerY, QuickTiGame2dConstant.AXIS_Z);
}
public void rotate(float angle, float centerX, float centerY, float axis) {
param_rotate[0] = angle;
param_rotate[1] = centerX;
param_rotate[2] = centerY;
param_rotate[3] = 0;
param_rotate[4] = axis;
}
public float getRotationCenterX() {
return param_rotate[1];
}
public void setRotationCenterX(float value) {
param_rotate[1] = value;
}
public float getRotationCenterY() {
return param_rotate[2];
}
public void setRotationCenterY(float value) {
param_rotate[2] = value;
}
public void scale(float scaleXY) {
scale(scaleXY, scaleXY);
}
public void scale(float scaleX, float scaleY) {
param_scale[0] = scaleX;
param_scale[1] = scaleY;
param_scale[2] = 1;
param_scale[3] = width * 0.5f;
param_scale[4] = height * 0.5f;
param_scale[5] = 0;
}
public void scale(float scaleX, float scaleY, float centerX, float centerY) {
param_scale[0] = scaleX;
param_scale[1] = scaleY;
param_scale[2] = 1;
param_scale[3] = centerX;
param_scale[4] = centerY;
param_scale[5] = 0;
}
public float getScaleX() {
return param_scale[0];
}
public void setScaleX(float value) {
param_scale[0] = value;
}
public float getScaleY() {
return param_scale[1];
}
public void setScaleY(float value) {
param_scale[1] = value;
}
public float getScaleCenterX() {
return param_scale[3];
}
public void setScaleCenterX(float value) {
param_scale[3] = value;
}
public float getScaleCenterY() {
return param_scale[4];
}
public void setScaleCenterY(float value) {
param_scale[4] = value;
}
public String getImage() {
return image;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getBorder() {
return border;
}
public void setBorder(int border) {
this.border = border;
}
public int getMargin() {
return margin;
}
public void setMargin(int margin) {
this.margin = margin;
}
public void setDebug(boolean enabled) {
this.debug = enabled;
}
public boolean getDebug() {
return debug;
}
public boolean hasTexture() {
return hasTexture;
}
public int getFrameCount() {
return frameCount;
}
public void setFrameCount(int frameCount) {
this.frameCount = frameCount;
}
public int getFrameIndex() {
return frameIndex;
}
public boolean isLoaded() {
return loaded;
}
public boolean hasSheet() {
return hasSheet;
}
public void enableSheet(boolean enable) {
this.hasSheet = enable;
}
public void blendFunc(int src, int dst) {
this.srcBlendFactor = src;
this.dstBlendFactor = dst;
}
public int getSrcBlendFactor() {
return srcBlendFactor;
}
public void setSrcBlendFactor(int value) {
this.srcBlendFactor = value;
}
public int getDstBlendFactor() {
return dstBlendFactor;
}
public void setDstBlendFactor(int value) {
this.dstBlendFactor = value;
}
public boolean selectFrame(String name) {
if (!isPackedAtlas) return false;
this.selectedFrameName = name;
QuickTiGame2dImagePackInfo info = getImagePack(name);
if (info == null) return false;
return setFrameIndex(info.getIndex());
}
public void addImagePack(QuickTiGame2dImagePackInfo info) {
deleteImagePack(info.getName());
imagepacks.put(info.getName(), info);
imagepacks_names.add(info.getName());
}
private QuickTiGame2dImagePackInfo getImagePack(String name) {
return imagepacks.get(name);
}
private boolean deleteImagePack(String name) {
QuickTiGame2dImagePackInfo info = getImagePack(name);
if (info == null) return false;
return imagepacks.remove(name) != null;
}
protected void onTransform() {
if (transforms.size() == 0) return;
for (QuickTiGame2dTransform transform : transforms) {
if (transform.isCompleted()) {
transformsToBeRemoved.add(transform);
continue;
}
// waiting for delay
if (!transform.hasStarted()) continue;
// fire onStartTransform event
if (!transform.isStartEventFired()) {
transform.setStartEventFired(true);
view.onStartTransform(transform);
}
if (transform.hasExpired()) {
// if transform has been completed, finish the transformation
if (transform.getRepeat() >= 0 && transform.getRepeatCount() >= transform.getRepeat()) {
if (transform.isAutoreverse() && !transform.isReversing()) {
// no nothing
} else {
applyTransform(transform);
completeTransform(transform);
continue;
}
}
if (transform.isAutoreverse()) {
applyTransform(transform);
transform.reverse();
} else if (transform.getRepeat() < 0) {
// transform.repeat < 0 means infinite loop
applyTransform(transform);
transform.start();
} else {
applyTransform(transform);
transform.restart();
}
continue;
}
applyTransform(transform);
}
for (QuickTiGame2dTransform transform : transformsToBeRemoved) {
transforms.remove(transform);
}
transformsToBeRemoved.clear();
}
public void transform(QuickTiGame2dTransform transform) {
synchronized(children) {
for (QuickTiGame2dSprite child : children) {
if (child.isRelativeToTransformParent()) {
child.setRelativeToTransformParentX(child.x - this.x);
child.setRelativeToTransformParentY(child.y - this.y);
}
}
}
synchronized (transforms) {
try {
transforms.remove(transform);
transforms.add(transform);
// save initial state
transform.setStart_x(x);
transform.setStart_y(y);
transform.setStart_z(z);
transform.setStart_width(width);
transform.setStart_height(height);
transform.setStart_frameIndex(frameIndex);
transform.setStart_angle(param_rotate[0]);
transform.setStart_rotate_axis(param_rotate[4]);
transform.setStart_rotate_centerX(param_rotate[1]);
transform.setStart_rotate_centerY(param_rotate[2]);
transform.setStart_scaleX(param_scale[0]);
transform.setStart_scaleY(param_scale[1]);
transform.setStart_red(param_color[0]);
transform.setStart_green(param_color[1]);
transform.setStart_blue(param_color[2]);
transform.setStart_alpha(param_color[3]);
transform.start();
} catch (Exception e) {
Log.e(Quicktigame2dModule.LOG_TAG, "Error at sprite.transform", e);
}
}
}
protected void applyTransform(QuickTiGame2dTransform transform) {
applyTransform(transform, false);
}
protected void applyTransform(QuickTiGame2dTransform transform, boolean isChild) {
if (transform.isCompleted()) return;
transform.apply();
if (isChild && relativeToTransformParent) {
if (transform.getX() != null && (!isChild || followParentTransformPosition)) x = transform.getCurrent_x() + relativeToTransformParentX;
if (transform.getY() != null && (!isChild || followParentTransformPosition)) y = transform.getCurrent_y() + relativeToTransformParentY;
} else {
if (transform.getX() != null && (!isChild || followParentTransformPosition)) x = transform.getCurrent_x();