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
|
/*
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
/**
* Implements Text3D class.
*/
class Text3DRetained extends GeometryRetained {
/**
* Packaged scope variables needed for implementation
*/
Font3D font3D = null;
String string = null;
Point3f position = new Point3f(0.0f, 0.0f, 0.0f);
int alignment = Text3D.ALIGN_FIRST, path = Text3D.PATH_RIGHT;
float charSpacing = 0.0f;
int numChars = 0;
static final int targetThreads = (J3dThread.UPDATE_TRANSFORM |
J3dThread.UPDATE_GEOMETRY |
J3dThread.UPDATE_RENDER);
/**
* The temporary transforms for this Text3D
*/
Transform3D[] charTransforms = new Transform3D[0];
/**
* A cached list of geometry arrays for the current settings
*/
GeometryArrayRetained[] geometryList = new GeometryArrayRetained[0];
GlyphVector[] glyphVecs = new GlyphVector[0];
/**
* Bounding box data for this text string.
*/
Point3d lower = new Point3d();
Point3d upper = new Point3d();
/**
* An Array list used for messages
*/
ArrayList newGeometryAtomList = new ArrayList();
ArrayList oldGeometryAtomList = new ArrayList();
/**
* temporary model view matrix for immediate mode only
*/
Transform3D vpcToEc;
Transform3D drawTransform;
Text3DRetained(){
this.geoType = GEO_TYPE_TEXT3D;
}
@Override
synchronized void computeBoundingBox() {
Point3d l = new Point3d();
Point3d u = new Point3d();
Vector3f location = new Vector3f(this.position);
int i, k=0, numTotal=0;
double width = 0, height = 0;
Rectangle2D bounds;
//Reset bounds data
l.set(location);
u.set(location);
if (numChars != 0) {
// Set loop counters based on path type
if (path == Text3D.PATH_RIGHT || path == Text3D.PATH_UP) {
k = 0;
numTotal = numChars + 1;
} else if (path == Text3D.PATH_LEFT || path == Text3D.PATH_DOWN) {
k = 1;
numTotal = numChars;
// Reset bounds to bounding box if first character
bounds = glyphVecs[0].getVisualBounds();
u.x += bounds.getWidth();
u.y += bounds.getHeight();
}
for (i=1; i<numTotal; i++, k++) {
width = glyphVecs[k].getLogicalBounds().getWidth();
bounds = glyphVecs[k].getVisualBounds();
// 'switch' could be outside loop with little hacking,
width += charSpacing;
height = bounds.getHeight();
switch (this.path) {
case Text3D.PATH_RIGHT:
u.x += (width);
if (u.y < (height + location.y)) {
u.y = location.y + height;
}
break;
case Text3D.PATH_LEFT:
l.x -= (width);
if (u.y < ( height + location.y)) {
u.y = location.y + height;
}
break;
case Text3D.PATH_UP:
u.y += height;
if (u.x < (bounds.getWidth() + location.x)) {
u.x = location.x + bounds.getWidth();
}
break;
case Text3D.PATH_DOWN:
l.y -= height;
if (u.x < (bounds.getWidth() + location.x)) {
u.x = location.x + bounds.getWidth();
}
break;
}
}
// Handle string alignment. ALIGN_FIRST is handled by default
if (alignment != Text3D.ALIGN_FIRST) {
double cx = (u.x - l.x);
double cy = (u.y - l.y);
if (alignment == Text3D.ALIGN_CENTER) {
cx *= .5;
cy *= .5;
}
switch (path) {
case Text3D.PATH_RIGHT:
l.x -= cx;
u.x -= cx;
break;
case Text3D.PATH_LEFT:
l.x += cx;
u.x += cx;
break;
case Text3D.PATH_UP:
l.y -= cy;
u.y -= cy;
break;
case Text3D.PATH_DOWN:
l.y += cy;
u.y += cy;
break;
}
}
}
l.z = 0.0f;
if ((font3D == null) || (font3D.fontExtrusion == null)) {
u.z = l.z;
} else {
u.z = l.z + font3D.fontExtrusion.length;
}
}
@Override
void update() {}
/**
* Returns the Font3D objects used by this Text3D NodeComponent object.
*
* @return the Font3D object of this Text3D node - null if no Font3D
* has been associated with this node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final Font3D getFont3D() {
return this.font3D;
}
/**
* Sets the Font3D object used by this Text3D NodeComponent object.
*
* @param font3d the Font3D object to associate with this Text3D node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final void setFont3D(Font3D font3d) {
geomLock.getLock();
this.font3D = font3d;
updateCharacterData();
geomLock.unLock();
sendDataChangedMessage();
}
/**
* Copies the character string used in the construction of the
* Text3D node into the supplied parameter.
*
* @return a copy of the String object in this Text3D node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final String getString() {
return this.string;
}
/**
* Copies the character string from the supplied parameter into Tex3D
* node.
*
* @param string the String object to recieve the Text3D node's string.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final void setString(String string) {
geomLock.getLock();
this.string = string;
if (string == null) {
numChars = 0;
} else {
numChars = string.length();
}
updateCharacterData();
geomLock.unLock();
sendDataChangedMessage();
}
/**
* Copies the node's <code>position</code> field into the supplied
* parameter. The <code>position</code> is used to determine the
* initial placement of the Text3D string. The position, combined with
* the path and alignment control how the text is displayed.
*
* @param position the point to position the text.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @see #getAlignment
* @see #getPath
*/
final void getPosition(Point3f position) {
position.set(this.position);
}
/**
* Sets the node's <code>position</code> field to the supplied
* parameter. The <code>position</code> is used to determine the
* initial placement of the Text3D string. The position, combined with
* the path and alignment control how the text is displayed.
*
* @param position the point to position the text.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @see #getAlignment
* @see #getPath
*/
final void setPosition(Point3f position) {
geomLock.getLock();
this.position.set(position);
updateTransformData();
geomLock.unLock();
sendTransformChangedMessage();
}
/**
* Retrieves the text alignment policy for this Text3D NodeComponent
* object. The <code>alignment</code> is used to specify how
* glyphs in the string are placed in relation to the
* <code>position</code> field. Valid values for this field
* are:
* <UL>
* <LI> ALIGN_CENTER - the center of the string is placed on the
* <code>position</code> point.
* <LI> ALIGN_FIRST - the first character of the string is placed on
* the <code>position</code> point.
* <LI> ALIGN_LAST - the last character of the string is placed on the
* <code>position</code> point.
* </UL>
* The default value of this field is <code>ALIGN_FIRST</code>.
*
* @return the current alingment policy for this node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @see #getPosition
*/
final int getAlignment() {
return alignment;
}
/**
* Sets the text alignment policy for this Text3D NodeComponent
* object. The <code>alignment</code> is used to specify how
* glyphs in the string are placed in relation to the
* <code>position</code> field. Valid values for this field
* are:
* <UL>
* <LI> ALIGN_CENTER - the center of the string is placed on the
* <code>position</code> point.
* <LI> ALIGN_FIRST - the first character of the string is placed on
* the <code>position</code> point.
* <LI> ALIGN_LAST - the last character of the string is placed on the
* <code>position</code> point.
* </UL>
* The default value of this field is <code>ALIGN_FIRST</code>.
*
* @return the current alingment policy for this node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @see #getPosition
*/
final void setAlignment(int alignment) {
geomLock.getLock();
this.alignment = alignment;
updateTransformData();
geomLock.unLock();
sendTransformChangedMessage();
}
/**
* Retrieves the node's <code>path</code> field. This field
* is used to specify how succeeding
* glyphs in the string are placed in relation to the previous glyph.
* Valid values for this field are:
* <UL>
* <LI> PATH_LEFT: - succeeding glyphs are placed to the left of the
* current glyph.
* <LI> PATH_RIGHT: - succeeding glyphs are placed to the right of the
* current glyph.
* <LI> PATH_UP: - succeeding glyphs are placed above the current glyph.
* <LI> PATH_DOWN: - succeeding glyphs are placed below the current glyph.
* </UL>
* The default value of this field is <code>PATH_RIGHT</code>.
*
* @return the current alingment policy for this node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final int getPath() {
return this.path;
}
/**
* Sets the node's <code>path</code> field. This field
* is used to specify how succeeding
* glyphs in the string are placed in relation to the previous glyph.
* Valid values for this field are:
* <UL>
* <LI> PATH_LEFT - succeeding glyphs are placed to the left of the
* current glyph.
* <LI> PATH_RIGHT - succeeding glyphs are placed to the right of the
* current glyph.
* <LI> PATH_UP - succeeding glyphs are placed above the current glyph.
* <LI> PATH_DOWN - succeeding glyphs are placed below the current glyph.
* </UL>
* The default value of this field is <code>PATH_RIGHT</code>.
*
* @param path the value to set the path to.
*
* @return the current alingment policy for this node.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final void setPath(int path) {
this.path = path;
updateTransformData();
sendTransformChangedMessage();
}
/**
* Retrieves the 3D bounding box that encloses this Text3D object.
*
* @param bounds the object to copy the bounding information to.
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @see BoundingBox
*/
final void getBoundingBox(BoundingBox bounds) {
synchronized (this) {
bounds.setLower(lower);
bounds.setUpper(upper);
}
}
/**
* Retrieves the character spacing used to construct the Text3D string.
* This spacing is in addition to the regular spacing between glyphs as
* defined in the Font object. 1.0 in this space is measured as the
* width of the largest glyph in the 2D Font. The default value is
* 0.0.
*
* @return the current character spacing value
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final float getCharacterSpacing() {
return charSpacing;
}
/**
* Sets the character spacing used hwne constructing the Text3D string.
* This spacing is in addition to the regular spacing between glyphs as
* defined in the Font object. 1.0 in this space is measured as the
* width of the largest glyph in the 2D Font. The default value is
* 0.0.
*
* @param characterSpacing the new character spacing value
*
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
final void setCharacterSpacing(float characterSpacing) {
geomLock.getLock();
this.charSpacing = characterSpacing;
updateTransformData();
geomLock.unLock();
sendTransformChangedMessage();
}
final void sendDataChangedMessage() {
J3dMessage[] m;
int i, j, k, kk, numMessages;
int gSize;
ArrayList gaList;
GeometryAtom[] newGeometryAtoms;
ArrayList tiArrList = new ArrayList();
ArrayList newCtArrArrList = new ArrayList();
synchronized(liveStateLock) {
if (source.isLive()) {
synchronized (universeList) {
numMessages = universeList.size();
m = new J3dMessage[numMessages];
for (i=0; i<numMessages; i++) {
m[i] = new J3dMessage();
m[i].type = J3dMessage.TEXT3D_DATA_CHANGED;
m[i].threads = targetThreads;
ArrayList<Shape3DRetained> shapeList = userLists.get(i);
newGeometryAtomList.clear();
oldGeometryAtomList.clear();
for (j=0; j<shapeList.size(); j++) {
Shape3DRetained s = shapeList.get(j);
if (s.boundsAutoCompute) {
// update combine bounds of mirrorShape3Ds. So we need to
// use its bounds and not localBounds.
// bounds is actually a reference to
// mirrorShape3D.source.localBounds.
// XXXX : Should only need to update distinct localBounds.
s.getCombineBounds((BoundingBox)s.bounds);
}
gSize = s.geometryList.size();
GeometryAtom oldGA = Shape3DRetained.getGeomAtom(s);
GeometryAtom newGA = new GeometryAtom();
int geometryCnt = 0;
for(k = 0; k<gSize; k++) {
GeometryRetained geomRetained = s.geometryList.get(k);
if(geomRetained != null) {
Text3DRetained tempT3d = (Text3DRetained)geomRetained;
geometryCnt += tempT3d.numChars;
}
else {
// Slightly wasteful, but not quite worth to optimize yet.
geometryCnt++;
}
}
newGA.geometryArray = new GeometryRetained[geometryCnt];
newGA.lastLocalTransformArray = new Transform3D[geometryCnt];
// Reset geometryCnt;
geometryCnt = 0;
newGA.locale = s.locale;
newGA.visible = s.visible;
newGA.source = s;
int gaCnt=0;
GeometryRetained geometry = null;
for(; gaCnt<gSize; gaCnt++) {
geometry = s.geometryList.get(gaCnt);
if(geometry != null) {
newGA.geoType = geometry.geoType;
newGA.alphaEditable = s.isAlphaEditable(geometry);
break;
}
}
for(; gaCnt<gSize; gaCnt++) {
geometry = s.geometryList.get(gaCnt);
if(geometry == null) {
newGA.geometryArray[gaCnt] = null;
}
else {
Text3DRetained t = (Text3DRetained)geometry;
GeometryRetained geo;
for (k=0; k<t.numChars; k++, geometryCnt++) {
geo = t.geometryList[k];
if (geo != null) {
newGA.geometryArray[geometryCnt] = geo;
newGA.lastLocalTransformArray[geometryCnt] =
t.charTransforms[k];
} else {
newGA.geometryArray[geometryCnt] = null;
newGA.lastLocalTransformArray[geometryCnt] = null;
}
}
}
}
oldGeometryAtomList.add(oldGA);
newGeometryAtomList.add(newGA);
Shape3DRetained.setGeomAtom(s, newGA);
}
Object[] oldGAArray = oldGeometryAtomList.toArray();
Object[] newGAArray = newGeometryAtomList.toArray();
ArrayList uniqueList = getUniqueSource(shapeList);
int numSrc = uniqueList.size();
int numMS3D;
Shape3DRetained ms, src;
for (j=0; j<numSrc; j++) {
CachedTargets[] newCtArr = null;
src = (Shape3DRetained)uniqueList.get(j);
numMS3D = src.mirrorShape3D.size();
TargetsInterface ti = ((GroupRetained)src.
parent).getClosestTargetsInterface(
TargetsInterface.TRANSFORM_TARGETS);
if (ti != null) {
CachedTargets ct;
newCtArr = new CachedTargets[numMS3D];
for (k=0; k<numMS3D; k++) {
ms = src.mirrorShape3D.get(k);
GeometryAtom ga =
Shape3DRetained.getGeomAtom(ms);
for(kk=0; kk<newGAArray.length; kk++) {
if(ga == newGAArray[kk]) {
break;
}
}
if(kk==newGAArray.length) {
System.err.println("Text3DRetained : Problem !!! Can't find matching geomAtom");
}
ct = ti.getCachedTargets(TargetsInterface.
TRANSFORM_TARGETS, k, -1);
if (ct != null) {
newCtArr[k] = new CachedTargets();
newCtArr[k].copy(ct);
newCtArr[k].replace((NnuId)oldGAArray[kk],
(NnuId)newGAArray[kk],
Targets.GEO_TARGETS);
} else {
newCtArr[k] = null;
}
}
ti.resetCachedTargets(
TargetsInterface.TRANSFORM_TARGETS, newCtArr, -1);
tiArrList.add(ti);
newCtArrArrList.add(newCtArr);
}
}
m[i].args[0] = oldGAArray;
m[i].args[1] = newGAArray;
m[i].universe = universeList.get(i);
if(tiArrList.size() > 0) {
m[i].args[2] = tiArrList.toArray();
m[i].args[3] = newCtArrArrList.toArray();
}
tiArrList.clear();
newCtArrArrList.clear();
}
VirtualUniverse.mc.processMessage(m);
}
}
}
}
final void sendTransformChangedMessage() {
J3dMessage[] m;
int i, j, numMessages, sCnt;
ArrayList gaList = new ArrayList();
GeometryRetained geomR;
synchronized(liveStateLock) {
if (source.isLive()) {
synchronized (universeList) {
numMessages = universeList.size();
m = new J3dMessage[numMessages];
for (i=0; i<numMessages; i++) {
m[i] = new J3dMessage();
m[i].type = J3dMessage.TEXT3D_TRANSFORM_CHANGED;
m[i].threads = targetThreads;
ArrayList<Shape3DRetained> shapeList = userLists.get(i);
// gaList = new GeometryAtom[shapeList.size() * numChars];
for (j = 0; j < shapeList.size(); j++) {
Shape3DRetained s = shapeList.get(j);
// Find the right geometry.
for(sCnt=0; sCnt<s.geometryList.size(); sCnt++) {
geomR = s.geometryList.get(sCnt);
if(geomR == this) {
break;
}
}
if(sCnt < s.geometryList.size())
gaList.add(Shape3DRetained.getGeomAtom(s));
}
m[i].args[0] = gaList.toArray();
m[i].args[1] = charTransforms;
m[i].universe = universeList.get(i);
}
VirtualUniverse.mc.processMessage(m);
}
}
}
}
/**
* Update internal reprsentation of tranform matrices and geometry.
* This method will be called whenever string or font3D change.
*/
final void updateCharacterData() {
char c[] = new char[1];
if (geometryList.length != numChars) {
geometryList = new GeometryArrayRetained[numChars];
glyphVecs = new GlyphVector[numChars];
}
if (font3D != null) {
for (int i=0; i<numChars; i++) {
c[0] = string.charAt(i);
glyphVecs[i] = font3D.font.createGlyphVector(font3D.frc, c);
geometryList[i] = font3D.triangulateGlyphs(glyphVecs[i], c[0]);
}
}
updateTransformData();
}
/**
* Update per character transform based on Text3D location,
* per character size and path.
*
* WARNING: Caller of this method must make sure SceneGraph is live,
* else exceptions may be thrown.
*/
final void updateTransformData(){
int i, k=0, numTotal=0;
double width = 0, height = 0;
Vector3f location = new Vector3f(this.position);
Rectangle2D bounds;
//Reset bounds data
lower.set(location);
upper.set(location);
charTransforms = new Transform3D[numChars];
for (i=0; i<numChars; i++) {
charTransforms[i] = new Transform3D();
}
if (numChars != 0) {
charTransforms[0].set(location);
// Set loop counters based on path type
if (path == Text3D.PATH_RIGHT || path == Text3D.PATH_UP) {
k = 0;
numTotal = numChars + 1;
} else if (path == Text3D.PATH_LEFT || path == Text3D.PATH_DOWN) {
k = 1;
numTotal = numChars;
// Reset bounds to bounding box if first character
bounds = glyphVecs[0].getVisualBounds();
upper.x += bounds.getWidth();
upper.y += bounds.getHeight();
}
for (i=1; i<numTotal; i++, k++) {
width = glyphVecs[k].getLogicalBounds().getWidth();
bounds = glyphVecs[k].getVisualBounds();
// 'switch' could be outside loop with little hacking,
width += charSpacing;
height = bounds.getHeight();
switch (this.path) {
case Text3D.PATH_RIGHT:
location.x += width;
upper.x += (width);
if (upper.y < (height + location.y)) {
upper.y = location.y + height;
}
break;
case Text3D.PATH_LEFT:
location.x -= width;
lower.x -= (width);
if (upper.y < ( height + location.y)) {
upper.y = location.y + height;
}
break;
case Text3D.PATH_UP:
location.y += height;
upper.y += height;
if (upper.x < (bounds.getWidth() + location.x)) {
upper.x = location.x + bounds.getWidth();
}
break;
case Text3D.PATH_DOWN:
location.y -= height;
lower.y -= height;
if (upper.x < (bounds.getWidth() + location.x)) {
upper.x = location.x + bounds.getWidth();
}
break;
}
if (i < numChars) {
charTransforms[i].set(location);
}
}
// Handle string alignment. ALIGN_FIRST is handled by default
if (alignment != Text3D.ALIGN_FIRST) {
double cx = (upper.x - lower.x);
double cy = (upper.y - lower.y);
if (alignment == Text3D.ALIGN_CENTER) {
cx *= .5;
cy *= .5;
}
switch (path) {
case Text3D.PATH_RIGHT:
for (i=0;i < numChars;i++) {
charTransforms[i].mat[3] -= cx;
}
lower.x -= cx;
upper.x -= cx;
break;
case Text3D.PATH_LEFT:
for (i=0;i < numChars;i++) {
charTransforms[i].mat[3] += cx;
}
lower.x += cx;
upper.x += cx;
break;
case Text3D.PATH_UP:
for (i=0;i < numChars;i++) {
charTransforms[i].mat[7] -=cy;
}
lower.y -= cy;
upper.y -= cy;
break;
case Text3D.PATH_DOWN:
for (i=0;i < numChars;i++) {
charTransforms[i].mat[7] +=cy;
}
lower.y += cy;
upper.y += cy;
break;
}
}
}
lower.z = 0.0f;
if ((font3D == null) || (font3D.fontExtrusion == null)) {
upper.z = lower.z;
} else {
upper.z = lower.z + font3D.fontExtrusion.length;
}
// update geoBounds
getBoundingBox(geoBounds);
}
/**
* This method is called when the SceneGraph becomes live. All characters
* used by this.string are tesselated in this method, to avoid wait during
* traversal and rendering.
*/
@Override
void setLive(boolean inBackgroundGroup, int refCount) {
// Tesselate all character data and update character transforms
updateCharacterData();
super.doSetLive(inBackgroundGroup, refCount);
super.markAsLive();
}
// TODO -- Need to rethink. Might have to consider charTransform[] in returns pickInfo.
@Override
boolean intersect(PickShape pickShape, PickInfo pickInfo, int flags, Point3d iPnt,
GeometryRetained geom, int geomIndex) {
Transform3D tempT3D = new Transform3D();
GeometryArrayRetained geo = null;
int sIndex = -1;
PickShape newPS;
double minDist = Double.MAX_VALUE;
double distance =0.0;
Point3d closestIPnt = new Point3d();
for (int i=0; i < numChars; i++) {
geo= geometryList[i];
if (geo != null) {
tempT3D.invert(charTransforms[i]);
newPS = pickShape.transform(tempT3D);
if (geo.intersect(newPS, pickInfo, flags, iPnt, geom, geomIndex)) {
if (flags == 0) {
return true;
}
distance = newPS.distance(iPnt);
if (distance < minDist) {
sIndex = i;
minDist = distance;
closestIPnt.set(iPnt);
}
}
}
}
if (sIndex >= 0) {
// We need to transform iPnt to the vworld to compute the actual distance.
// In this method we'll transform iPnt by its char. offset. Shape3D will
// do the localToVworld transform.
iPnt.set(closestIPnt);
charTransforms[sIndex].transform(iPnt);
return true;
}
return false;
}
@Override
boolean intersect(Point3d[] pnts) {
Transform3D tempT3D = new Transform3D();
GeometryArrayRetained ga;
boolean isIntersect = false;
Point3d transPnts[] = new Point3d[pnts.length];
for (int j=pnts.length-1; j >= 0; j--) {
transPnts[j] = new Point3d();
}
for (int i=numChars-1; i >= 0; i--) {
ga = geometryList[i];
if ( ga != null) {
tempT3D.invert(charTransforms[i]);
for (int j=pnts.length-1; j >= 0; j--) {
tempT3D.transform(pnts[j], transPnts[j]);
}
if (ga.intersect(transPnts)) {
isIntersect = true;
break;
}
}
}
return isIntersect;
}
@Override
boolean intersect(Transform3D thisToOtherVworld, GeometryRetained geom) {
GeometryArrayRetained ga;
for (int i=numChars-1; i >=0; i--) {
ga = geometryList[i];
if ((ga != null) && ga.intersect(thisToOtherVworld, geom)) {
return true;
}
}
return false;
}
@Override
boolean intersect(Bounds targetBound) {
GeometryArrayRetained ga;
for (int i=numChars-1; i >=0; i--) {
ga = geometryList[i];
if ((ga != null) && ga.intersect(targetBound)) {
return true;
}
}
return false;
}
void setModelViewMatrix(Transform3D vpcToEc, Transform3D drawTransform) {
this.vpcToEc = vpcToEc;
this.drawTransform = drawTransform;
}
@Override
void execute(Canvas3D cv, RenderAtom ra, boolean isNonUniformScale,
boolean updateAlpha, float alpha,
int screen,
boolean ignoreVertexColors) {
Transform3D trans = new Transform3D();
for (int i = 0; i < geometryList.length; i++) {
trans.set(drawTransform);
trans.mul(charTransforms[i]);
cv.setModelViewMatrix(cv.ctx, vpcToEc.mat, trans);
geometryList[i].execute(cv, ra, isNonUniformScale, updateAlpha, alpha,
screen, ignoreVertexColors);
}
}
@Override
int getClassType() {
return TEXT3D_TYPE;
}
ArrayList getUniqueSource(ArrayList shapeList) {
ArrayList uniqueList = new ArrayList();
int size = shapeList.size();
Object src;
int i, index;
for (i=0; i<size; i++) {
src = ((Shape3DRetained)shapeList.get(i)).sourceNode;
index = uniqueList.indexOf(src);
if (index == -1) {
uniqueList.add(src);
}
}
return uniqueList;
}
}
|