-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcubelets.js
More file actions
executable file
·1063 lines (734 loc) · 28.1 KB
/
cubelets.js
File metadata and controls
executable file
·1063 lines (734 loc) · 28.1 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
/*
CUBELETS
Faces are mapped in a clockwise spiral from Front to Back:
Back
5
-----------
/ Up /|
/ 1 / |
----------- Right
| | 2
Left | Front | .
4 | 0 | /
| |/
-----------
Down
3
The faces[] Array is mapped to names for convenience:
this.faces[ 0 ] === this.front
this.faces[ 1 ] === this.up
this.faces[ 2 ] === this.right
this.faces[ 3 ] === this.down
this.faces[ 4 ] === this.left
this.faces[ 5 ] === this.back
Each Cubelet has an Index which is assigned during Cube creation
and an Address which changes as the Cubelet changes location.
Additionally an AddressX, AddressY, and AddressZ are calculated
from the Address and represent the Cubelet's location relative
to the Cube's core with integer values ranging from -1 to +1.
For an overview of the Cubelet's data from the browser's console:
this.inspect()
*/
function Cubelet( cube, id, colors ){
// Our Cube can directly address its Cubelet children,
// only fair the Cubelet can address their parent Cube!
this.cube = cube
// Our Cubelet's ID is its unique number on the Cube.
// Each Cube has Cubletes numbered 0 through 26.
// Even if we're debugging (and not attached to an actual Cube)
// we need an ID number for later below
// when we derive positions and rotations for the Cubelet faces.
this.id = id || 0
// Our Cubelet's address is its current location on the Cube.
// When the Cubelet is initialized its ID and address are the same.
// This method will also set the X, Y, and Z components of the
// Cubelet's address on the Cube.
this.setAddress( this.id )
// We're going to build Cubelets that are 140 pixels square.
// Yup. This size is hardwired in Cube.
// It is also hard-wired into the CSS, but we can't simply
// grab the style.getBoundingClientRect() value because
// that's a 2D measurement -- doesn't account for pos and rot.
this.size = cube.cubeletSize || 140
// Now we can find our Cubelet's X, Y, and Z position in space.
// We only need this momentarily to create our Object3D so
// there's no need to attach these properties to our Cubelet object.
var
x = this.addressX * this.size,
y = this.addressY * this.size,
z = this.addressZ * this.size
// For convenience here are maps for rotating and positioning
// the Cubelet face wall into place.
var
half = this.size / 2,
rotations = [
[ 0, 0, 0 ],// Front
[ -90, 0, 0 ],// Up
[ 0, 90, 0 ],// Right
[ 90, 0, 0 ],// Down
[ 0, -90, 0 ],// Left
[ 0, 180, 0 ] // Back
],
positions = [
[ 0, 0, half ],// Front
[ 0, -half, 0 ],// Up
[ half, 0, 0 ],// Right
[ 0, half, 0 ],// Down
[ -half, 0, 0 ],// Left
[ 0, 0, -half ] // Back
]
// Our anchor only achieves rotation during a tween animation.
// It is then immediately reset to rotation( 0, 0, 0 )
// (and its rotation information is applied to the wrapper at that moment)
// and thus can be used as a reliable anchor in space repeatedly.
this.anchor = new THREE.Object3D()
this.anchor.name = 'anchor-' + this.id
if( this.cube ) this.cube.threeObject.add( this.anchor )
else scene.add( this.anchor )
if( erno.renderMode === 'css' ){
var domElement = document.createElement( 'div' )
domElement.classList.add( 'cubelet' )
domElement.classList.add( 'cubeletId-'+ this.id )
this.wrapper = new THREE.CSS3DObject( domElement )
}
else if( erno.renderMode === 'svg' ){
this.wrapper = new THREE.Object3D()
// Create this Cubelet's plastic shell.
this.plastic = new THREE.Mesh(
new THREE.CubeGeometry( cube.cubeletSize, cube.cubeletSize, cube.cubeletSize ),
new THREE.MeshBasicMaterial({ color: 0xFFFFFF, vertexColors: THREE.FaceColors })
)
this.plastic.position.set( x, y, z )
this.wrapper.add( this.plastic )
// Wireframe!
this.wireframe = new THREE.Mesh(
new THREE.CubeGeometry( cube.cubeletSize, cube.cubeletSize, cube.cubeletSize ),
new THREE.MeshBasicMaterial({ color: 0x00CCFF, wireframe: true })
)
this.wireframe.position.set( x, y, z )
this.wrapper.add( this.wireframe )
}
this.wrapper.name = 'wrapper-' + this.id
this.wrapper.position.set( x, y, z )
this.anchor.add( this.wrapper )
//@@@ QUICK HACK TO FORCE A CUBE TO APPEAR FOR HIT TESTING WITH RAYCASTING!!!
/*if( this.plastic === undefined ){
this.plastic = new THREE.Mesh(
new THREE.CubeGeometry( cube.cubeletSize, cube.cubeletSize, cube.cubeletSize ),
new THREE.MeshBasicMaterial({ color: 0xFF00FF, vertexColors: THREE.FaceColors })
)
this.plastic.position.set( x, y, z )
this.wrapper.add( this.plastic )
}*/
// We're about to loop through our colors[] Array
// to build the six faces of our Cubelet.
// Here's our overhead for that:
var extrovertedFaces = 0
if( colors === undefined ) colors = [ W, O, , , G, ]
this.faces = []
// Now let's map one color per side based on colors[].
// Undefined values are allowed (and anticipated).
// We need to loop through the colors[] Array "manually"
// because Array.forEach() would skip the undefined entries.
for( var i = 0; i < 6; i ++ ){
// Before we create our face's THREE object
// we need to know where it should be positioned and rotated.
// (This is based on our above positions and rotations map.)
var
color = colors[ i ] || COLORLESS
// Each face is an object and keeps track of its original ID number
// (which is important because its address will change with each rotation)
// its current color, and so on.
this.faces[ i ] = {}
this.faces[ i ].id = i
this.faces[ i ].color = color
// We're going to keep track of what face was what at the moment of initialization,
// mostly for solving purposes.
// This is particularly useful for Striegel's solver
// which requires an UP normal.
this.faces[ i ].normal = Direction.getNameById( i )
if( erno.renderMode === 'css' ){
// FACE CONTAINER.
// This face of our Cubelet needs a DOM element for all the
// related DOM elements to be attached to.
var faceElement = document.createElement( 'div' )
faceElement.classList.add( 'face' )
faceElement.classList.add( 'face'+ Direction.getNameById( i ).capitalize() )
this.wrapper.element.appendChild( faceElement )
// WIREFRAME.
var wireframeElement = document.createElement( 'div' )
wireframeElement.classList.add( 'wireframe' )
faceElement.appendChild( wireframeElement )
// CUBELET ID.
// For debugging we want the ability to display this Cubelet's ID number
// with an underline (to make numbers like 6 and 9 legible upside-down).
var idElement = document.createElement( 'div' )
idElement.classList.add( 'id' )
faceElement.appendChild( idElement )
var underlineElement = document.createElement( 'span' )
underlineElement.classList.add( 'underline' )
underlineElement.innerText = this.id
idElement.appendChild( underlineElement )
}
// INTROVERTED FACES.
// If this face has no color sticker then it must be interior to the Cube.
// That means in a normal state (no twisting happening) it is entirely hidden.
if( color === COLORLESS ){
if( erno.renderMode === 'css' ) faceElement.classList.add( 'faceIntroverted' )
else {
this.plastic.geometry.faces[ i ].color.setHex( 0x000000 )
this.plastic.geometry.colorsNeedUpdate = true
}
}
// EXTROVERTED FACES.
// But if this face does have a color then we need to
// create a sticker with that color
// and also allow text to be placed on it.
else {
// We're going to use the number of exposed sides
// to determine below what 'type' of Cubelet this is:
// Core, Center, Edge, or Corner.
extrovertedFaces ++
if( erno.renderMode === 'css' ){
faceElement.classList.add( 'faceExtroverted' )
// STICKER.
// You know, the color part that makes the Cube
// the most frustrating toy ever.
var stickerElement = document.createElement( 'div' )
stickerElement.classList.add( 'sticker' )
stickerElement.style.backgroundColor = color.hex
faceElement.appendChild( stickerElement )
// TEXT.
// One character per face, mostly for our branding.
var textElement = document.createElement( 'div' )
textElement.classList.add( 'text' )
textElement.innerText = i
this.faces[ i ].text = textElement
faceElement.appendChild( textElement )
}
else {
/*
var sticker = new THREE.Mesh(
new THREE.PlaneGeometry( cube.cubeletSize, cube.cubeletSize ),
new THREE.MeshBasicMaterial({ color: color })
)
sticker.material.opacity = 0.7
sticker.overdraw = true
sticker.position.set( faceX, faceY, faceZ )
sticker.rotation.set( faceXR, faceYR, faceZR )
this.faces[ i ].sticker = sticker
this.wrapper.add( this.faces[ i ].sticker )
*/
//console.log(colorNameToHex( color ))
//console.log(' ' )
//console.log(this.plastic.geometry.faces[ i ].color)
//console.log(colorNameToDecimal( color ))
//var j = [ 1, 0, 2, 3, 4, 5 ][ i ]
var j = [ 3,3,3,3,3,3 ][ i ]
this.plastic.geometry.faces[ j ].color.setHex( colorNameToDecimal( color ))
//this.plastic.geometry.faces[ i ].color.setHex( 0xFF00FF )
this.plastic.geometry.colorsNeedUpdate = true
//console.log(this.plastic.geometry.faces[ i ].color)
//console.log(' ' )
}
}
}
// Now that we've run through our colors[] Array
// and counted the number of extroverted sides
// we can determine what 'type' of Cubelet this is.
this.type = [
'core',
'center',
'edge',
'corner'
][ extrovertedFaces ]
// Mapping the Cubelet will setup all of our convenience shortcuts
// like "this.front.color" and "this.left.text" for example.
this.map()
// If this happens to be our logo-bearing Cubelet
// we had better attach the logo to it!
if( this.front.color && this.front.color.name === 'white' && this.type === 'center' ){
if( erno.renderMode === 'css' ) stickerElement.classList.add( 'stickerLogo' )
}
// We need to know if we're "engaged" on an axis
// which at first seems indentical to isTweening,
// until you consider partial rotations.
this.isTweening = true
this.isEngagedX = false
this.isEngagedY = false
this.isEngagedZ = false
// Remember our separation of state code and visual code?
// Well here's some slightly (though not entirely!) redundant
// rotation tracking.
// It's actually this that makes partial rotations possible...
this.x = this.xPrevious = 0
this.y = this.yPrevious = 0
this.z = this.zPrevious = 0
// These will perform their actions, of course,
// but also setup their own boolean toggles.
this.show()
this.showPlastics()
this.showIntroverts()
this.showStickers()
this.hideIds()
this.hideTexts()
this.hideWireframes()
// During a rotation animation this Cubelet marks itself as
// this.isTweening = true.
// Very useful. Let's try it out.
this.isTweening = false
// Some fun tweenable properties.
this.opacity = 1
this.radius = 0
}
setupTasks = window.setupTasks || []
setupTasks.push( function(){
// Let's add some functionality to Cubelet's prototype
// via the augment() function from Skip.js.
// By adding to Cubelet's prototype and not the Cubelet constructor
// we're keeping instances of Cubelet super clean and light.
augment( Cubelet, {
// Convience accessors for the Cubelet's faces.
// What color is the left face? this.left() !!
map: function(){
this.front = this.faces[ 0 ]
this.up = this.faces[ 1 ]
this.right = this.faces[ 2 ]
this.down = this.faces[ 3 ]
this.left = this.faces[ 4 ]
this.back = this.faces[ 5 ]
this.colors =
( this.faces[ 0 ].color ? this.faces[ 0 ].color.initial : '-' ) +
( this.faces[ 1 ].color ? this.faces[ 1 ].color.initial : '-' ) +
( this.faces[ 2 ].color ? this.faces[ 2 ].color.initial : '-' ) +
( this.faces[ 3 ].color ? this.faces[ 3 ].color.initial : '-' ) +
( this.faces[ 4 ].color ? this.faces[ 4 ].color.initial : '-' ) +
( this.faces[ 5 ].color ? this.faces[ 5 ].color.initial : '-' )
},
// Aside from initialization this function will be called
// by the Cube during remapping.
// The raw address is an integer from 0 through 26
// mapped to the Cube in the same fashion as this.id.
// The X, Y, and Z components each range from -1 through +1
// where (0, 0, 0) is the Cube's core.
setAddress: function( address ){
this.address = address || 0
this.addressX = address.modulo( 3 ).subtract( 1 )
this.addressY = address.modulo( 9 ).divide( 3 ).roundDown().subtract( 1 ) * -1
this.addressZ = address.divide( 9 ).roundDown().subtract( 1 ) * -1
},
// Full inspection of the Cublet's faces
// using the convenience accessors from above.
inspect: function( face ){
if( face !== undefined ){
// Just a particular face's color -- called by Slice's inspector.
return this[ face ].color || '!'
}
else {
// Full on ASCII-art inspection mode -- with console colors!
var
that = this,
id = this.id,
address = this.address,
type = this.type,
color = this.cube.color,
LEFT = 0,
CENTER = 1,
getColorName = function( face, justification, minimumLength ){
var colorName = that[ face ].color.name.toUpperCase()
if( justification !== undefined && minimumLength !== undefined ){
if( justification === CENTER ) colorName = colorName.justifyCenter( minimumLength )
else if( justification === LEFT ) colorName = colorName.justifyLeft( minimumLength )
}
return colorName
}
if( id < 10 ) id = '0' + id
if( address < 10 ) address = '0' + address
console.log(
'\n ID '+ id +
'\n Type '+ type.toUpperCase() +'\n'+
'\n Address '+ address +
'\n Address X '+ this.addressX.toSignedString() +
'\n Address Y '+ this.addressY.toSignedString() +
'\n Address Z '+ this.addressZ.toSignedString() +'\n'+
'\n Engaged X '+ this.isEngagedX +
'\n Engaged Y '+ this.isEngagedY +
'\n Engaged Z '+ this.isEngagedZ +
'\n Tweening '+ this.isTweening +'\n'+
'\n%c 0 Front '+ getColorName( 'front', LEFT, 7 ) +'%c'+
'\n%c 1 Up '+ getColorName( 'up', LEFT, 7 ) +'%c'+
'\n%c 2 Right '+ getColorName( 'right', LEFT, 7 ) +'%c'+
'\n%c 3 Down '+ getColorName( 'down', LEFT, 7 ) +'%c'+
'\n%c 4 Left '+ getColorName( 'left', LEFT, 7 ) +'%c'+
'\n%c 5 Back '+ getColorName( 'back', LEFT, 7 ) +'%c\n' +
'\n ----------- %cback%c'+
'\n / %cup%c /| %c5%c'+
'\n / %c1%c / | %c'+ getColorName( 'back' ) +'%c'+
'\n /%c'+ getColorName( 'up', CENTER, 11 ) +'%c/ |'+
'\n %cleft%c ----------- %cright%c'+
'\n %c4%c | | %c2%c'+
'\n%c'+ getColorName( 'left', CENTER, 8 ) +'%c | %cfront%c | %c'+ getColorName( 'right' ) +'%c'+
'\n | %c0%c | /'+
'\n |%c'+ getColorName( 'front', CENTER, 11 ) +'%c| /'+
'\n | |/'+
'\n -----------'+
'\n %cdown%c'+
'\n %c3%c'+
'\n %c'+ getColorName( 'down', CENTER, 11 ) +'%c\n',
this.front.color.styleB, '',
this.up.color.styleB, '',
this.right.color.styleB, '',
this.down.color.styleB, '',
this.left.color.styleB, '',
this.back.color.styleB, '',
this.back.color.styleF, '',
this.up.color.styleF, '',
this.back.color.styleF, '',
this.up.color.styleF, '',
this.back.color.styleF, '',
this.up.color.styleF, '',
this.left.color.styleF, '',
this.right.color.styleF, '',
this.left.color.styleF, '',
this.right.color.styleF, '',
this.left.color.styleF, '',
this.front.color.styleF, '',
this.right.color.styleF, '',
this.front.color.styleF, '',
this.front.color.styleF, '',
this.down.color.styleF, '',
this.down.color.styleF, '',
this.down.color.styleF, ''
)
}
},
// Does this Cubelet contain a certain color?
// If so, return a String decribing what face that color is on.
// Otherwise return false.
hasColor: function( color ){
var i, face
for( i = 0; i < 6; i ++ ){
if( this.faces[ i ].color === color ){
face = i
break
}
}
if( face !== undefined ){
return [
'front',
'up',
'right',
'down',
'left',
'back'
][ face ]
}
else return false
},
// Similar to above, but accepts an arbitrary number of colors.
// This function implies AND rather than OR, XOR, etc.
hasColors: function(){
var
cubelet = this,
result = true,
colors = Array.prototype.slice.call( arguments )
colors.forEach( function( color ){
result = result && !!cubelet.hasColor( color )
})
return result
},
// We can rotate this Cublet on the X, Y, and Z axes
// both clockwise and anticlockwise.
rotate: function( rotation, degrees, cubeCallback ){
var
cubelet = this,
cube = this.cube,
xTarget = 0,
yTarget = 0,
zTarget = 0,
rotationUpperCase = rotation.toUpperCase(),
threshold = 0.001
// We need to signal to the world that we cannot accept more rotation() commands.
// This will also cause all Groups (and Slices) containing this Cubelet
// to refuse all twist() commands until further notice.
this.isTweening = true
// Logically rotating our Cubelet is a matter of swapping the order
// of the faces in the this.faces Array. The order is interpreted as:
// Front, Up, Right, Down, Left, Back.
if( rotationUpperCase === 'X' ){
cubelet.isEngagedX = true
if( rotation === 'X' ) xTarget = degrees
else xTarget = -degrees
}
else if( rotationUpperCase === 'Y' ){
cubelet.isEngagedY = true
if( rotation === 'Y' ) yTarget = degrees
else yTarget = -degrees
}
else if( rotationUpperCase === 'Z' ){
cubelet.isEngagedZ = true
if( rotation === 'Z' ) zTarget = degrees
else zTarget = -degrees
}
// At every steps let's try to keep our values tidy.
this.x += xTarget.round()
this.y += yTarget.round()
this.z += zTarget.round()
// Our Cube's twistDuration is the amount of time (in miliseconds)
// that it should take to rotate 90 dgrees.
// We're going to scale that to match whatever number of degrees we're actually rotating:
var
twistDuration = this.cube !== undefined ? this.cube.twistDuration : SECOND,
twistDurationScaled = [ degrees.absolute().scale( 0, 90, 0, twistDuration ), 250 ].maximum()
// And now for the rotation tween itself...
// It feels very wrong to me that we're going to invert the coordinate space here
// but that's how the cookie crumbles. Sorry.
new TWEEN.Tween( this.anchor.rotation )
.to({
x: -xTarget.degreesToRadians(),
y: -yTarget.degreesToRadians(),
z: -zTarget.degreesToRadians()
}, twistDurationScaled )
.easing( TWEEN.Easing.Quartic.Out )
.start()
.onComplete( function(){
// What dark voodoo is this?
// Calling window.render() within a tween onComplete()?
// I noticed that when switching between tabs,
// or after putting the machine to sleep then waking it,
// the tweened bits would be totally f'd
// yet their X, Y, Z rotation values were as expected.
// Calling window.render() is a dirty way to update
// all of the *matrices* and keeps the world tidy!
render()
// We need to reset our anchor's rotation to ( 0, 0, 0 )
// so that we never lose our anchor's orientation relative to the cube itself.
// First thing to do is apply our anchor's matrix
// to the matrix of our visual wrapper:
cubelet.wrapper.applyMatrix( cubelet.anchor.matrix )
// And now that we've retained that rotation information
// we can safely reset the anchor's rotation:
cubelet.anchor.rotation.set( 0, 0, 0 )
// Here's some complexity.
// We need to support partial rotations of arbitrary degrees
// yet ensure our internal model is always in a valid state.
// This means only remapping the Cubelet when it makes sense
// and also remapping the Cube if this Cubelet is allowed to do so.
var
xRemaps = cubelet.x.divide( 90 ).round()
.subtract( cubelet.xPrevious.divide( 90 ).round() )
.absolute(),
yRemaps = cubelet.y.divide( 90 ).round()
.subtract( cubelet.yPrevious.divide( 90 ).round() )
.absolute(),
zRemaps = cubelet.z.divide( 90 ).round()
.subtract( cubelet.zPrevious.divide( 90 ).round() )
.absolute()
if( erno.verbosity >= 0.9 ){
console.log( 'Cublet #'+ ( cubelet.id < 10 ? '0'+ cubelet.id : cubelet.id ),
' | xRemaps:', xRemaps, ' yRemaps:', yRemaps, ' zRemaps:', zRemaps,
' | xPrev:', cubelet.xPrevious, ' x:', cubelet.x,
' | yPrev:', cubelet.yPrevious, ' y:', cubelet.y,
' | zPrev:', cubelet.zPrevious, ' z:', cubelet.z )
}
if( xRemaps ){
while( xRemaps -- ){
if( cubelet.x < cubelet.xPrevious ) cubelet.faces = [ cubelet.up, cubelet.back, cubelet.right, cubelet.front, cubelet.left, cubelet.down ]
else cubelet.faces = [ cubelet.down, cubelet.front, cubelet.right, cubelet.back, cubelet.left, cubelet.up ]
cubelet.map()
if( cubeCallback !== undefined ){
cubeCallback( cubelet.cube.cubelets.slice())
cubelet.cube.map()
}
}
cubelet.xPrevious = cubelet.x
}
if( cubelet.x.modulo( 90 ).absolute() < threshold ){
cubelet.x = 0
cubelet.xPrevious = cubelet.x
cubelet.isEngagedX = false
}
if( yRemaps ){
while( yRemaps -- ){
if( cubelet.y < cubelet.yPrevious ) cubelet.faces = [ cubelet.left, cubelet.up, cubelet.front, cubelet.down, cubelet.back, cubelet.right ]
else cubelet.faces = [ cubelet.right, cubelet.up, cubelet.back, cubelet.down, cubelet.front, cubelet.left ]
cubelet.map()
if( cubeCallback !== undefined ){
cubeCallback( cubelet.cube.cubelets.slice())
cubelet.cube.map()
}
}
cubelet.yPrevious = cubelet.y
}
if( cubelet.y.modulo( 90 ).absolute() < threshold ){
cubelet.y = 0
cubelet.yPrevious = cubelet.y
cubelet.isEngagedY = false
}
if( zRemaps ){
while( zRemaps -- ){
if( cubelet.z < cubelet.zPrevious ) cubelet.faces = [ cubelet.front, cubelet.right, cubelet.down, cubelet.left, cubelet.up, cubelet.back ]
else cubelet.faces = [ cubelet.front, cubelet.left, cubelet.up, cubelet.right, cubelet.down, cubelet.back ]
cubelet.map()
if( cubeCallback !== undefined ){
cubeCallback( cubelet.cube.cubelets.slice())
cubelet.cube.map()
}
}
cubelet.zPrevious = cubelet.z
}
if( cubelet.z.modulo( 90 ).absolute() < threshold ){
cubelet.z = 0
cubelet.zPrevious = cubelet.z
cubelet.isEngagedZ = false
}
// Phew! Now we can turn off the tweening flag.
cubelet.isTweening = false
})
},
// Visual switches.
show: function(){
$( '.cubeletId-'+ this.id ).show()
this.showing = true
},
hide: function(){
$( '.cubeletId-'+ this.id ).hide()
this.showing = false
},
showPlastics: function(){
if( erno.renderMode === 'css' ) $( '.cubeletId-'+ this.id +' .face' ).removeClass( 'faceTransparent' )
else this.plastic.material.opacity = 1
this.showingPlastics = true
},
hidePlastics: function(){
if( erno.renderMode === 'css' ) $( '.cubeletId-'+ this.id +' .face' ).addClass( 'faceTransparent' )
else this.plastic.material.opacity = 0
this.showingPlastics = false
},
showExtroverts: function(){
$( '.cubeletId-'+ this.id + ' .faceExtroverted' ).show()
this.showingExtroverts = true
},
hideExtroverts: function(){
$( '.cubeletId-'+ this.id + ' .faceExtroverted' ).hide()
this.showingExtroverts = false
},
showIntroverts: function(){
$( '.cubeletId-'+ this.id + ' .faceIntroverted' ).show()
this.showingIntroverts = true
},
hideIntroverts: function(){
$( '.cubeletId-'+ this.id + ' .faceIntroverted' ).hide()
this.showingIntroverts = false
},
showStickers: function(){
if( erno.renderMode === 'css' ) $( '.cubeletId-'+ this.id + ' .sticker' ).show()
else this.faces.forEach( function( face ){
if( face.sticker ) face.sticker.material.opacity = 1
})
this.showingStickers = true
},
hideStickers: function(){
if( erno.renderMode === 'css' ) $( '.cubeletId-'+ this.id + ' .sticker' ).hide()
else this.faces.forEach( function( face ){
if( face.sticker ) face.sticker.material.opacity = 0
})
this.showingStickers = false
},
showWireframes: function(){
if( erno.renderMode === 'css' ) $( '.cubeletId-'+ this.id + ' .wireframe' ).show()
else this.wireframe.material.opacity = 1
this.showingWireframes = true
},
hideWireframes: function(){
if( erno.renderMode === 'css' ) $( '.cubeletId-'+ this.id + ' .wireframe' ).hide()
else this.wireframe.material.opacity = 0
this.showingWireframes = false
},
showIds: function(){
$( '.cubeletId-'+ this.id + ' .id' ).show()
this.showingIds = true
},
hideIds: function(){
$( '.cubeletId-'+ this.id + ' .id' ).hide()
this.showingIds = false
},
showTexts: function(){
$( '.cubeletId-'+ this.id + ' .text' ).show()
this.showingTexts = true
},
hideTexts: function(){
$( '.cubeletId-'+ this.id + ' .text' ).hide()
this.showingTexts = false
},
getOpacity: function(){
return this.opacity
},
setOpacity: function( opacityTarget, onComplete ){
if( this.opacityTween ) this.opacityTween.stop()
if( opacityTarget === undefined ) opacityTarget = 1
if( opacityTarget !== this.opacity ){
var
that = this,
tweenDuration = ( opacityTarget - this.opacity ).absolute().scale( 0, 1, 0, SECOND )
this.opacityTween = new TWEEN.Tween({ opacity: this.opacity })
.to({
opacity: opacityTarget
}, tweenDuration )
.easing( TWEEN.Easing.Quadratic.InOut )
.onUpdate( function(){
$( '.cubeletId-'+ that.id ).css( 'opacity', this.opacity )
that.opacity = this.opacity//opacityTarget
})
.onComplete( function(){
if( onComplete instanceof Function ) onComplete()
})
.start()
}
},
getStickersOpacity: function( value ){
return $( '.cubeletId-'+ this.id + ' .sticker' ).css( 'opacity' )
},