-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWorldPalette.mel
1756 lines (1557 loc) · 62.8 KB
/
WorldPalette.mel
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
global proc reorderPriority()
{
global string $scrollPriority;
global int $priorityOrder[]; // this keeps track of the updates
// Total number of items
int $numItems = `textScrollList -q -ni $scrollPriority`;
// Get selected item index
int $clickedItem[] = `textScrollList -q -sii $scrollPriority`;
// Get all items
string $allItems[] = `textScrollList -q -ai $scrollPriority`;
// If the first item is already the clicked item, do nothing
if ($allItems[0] == $allItems[$clickedItem[0] - 1]) {
return;
} else {
// Bring this item to the top
textScrollList -e -ap 1 ($allItems[$clickedItem[0] - 1] + "") $scrollPriority;
// Remove the item at the old position
textScrollList -e -rii ($clickedItem[0] + 1) $scrollPriority;
// Update the priority order array
// Shift elements until you get to the moved item's index
int $replaced = $priorityOrder[0];
$priorityOrder[0] = $priorityOrder[$clickedItem[0] - 1];
for ($i = 1; $i < $clickedItem[0]; $i++) {
int $replaced2 = $priorityOrder[$i];
$priorityOrder[$i] = $replaced;
$replaced = $replaced2;
}
}
}
global proc updatePriority()
{
// IF WE ADD MORE CATEGORIES, WE MUST UPDATE THIS FUNCTION!!!
global int $priorityOrder[];
// Send updated order to C++
WorldPalette -po $priorityOrder[0] $priorityOrder[1] $priorityOrder[2] $priorityOrder[3];
}
global proc fillPriority()
{
// IF WE ADD MORE CATEGORIES, WE MUST UPDATE THIS FUNCTION!!!
global string $scrollPriority;
global int $priorityOrder[];
for ($i = 0; $i < 4; $i++) {
switch($priorityOrder[$i]) {
case 0:
textScrollList -e -a "GRASS" $scrollPriority;
break;
case 1:
textScrollList -e -a "ROCK" $scrollPriority;
break;
case 2:
textScrollList -e -a "SHRUB" $scrollPriority;
break;
case 3:
textScrollList -e -a "TREE" $scrollPriority;
break;
default:
break;
}
}
}
global proc deleteCurrentSelectRegion()
{
global int $selectionType;
global string $selectionRegion[];
// Delete current selection region (if there is one)
if ($selectionType) {
select -r $selectionRegion[0];
delete;
$selectionType = 0;
// We must also reset the move GUI tools related to this
// Selection region sliders
global string $moveXSlider;
global string $moveZSlider;
global string $widthSlider;
// Reset slider values
floatSliderGrp -e -v 0 $moveXSlider;
floatSliderGrp -e -v 0 $moveZSlider;
floatSliderGrp -e -v 1 $widthSlider;
// Disable move sliders
floatSliderGrp -e -en false $moveXSlider;
floatSliderGrp -e -en false $moveZSlider;
// Disable width slider
floatSliderGrp -e -en false $widthSlider;
// Editing tools
global int $isMoveDistributionActive;
global int $isResizeDistributionActive;
global string $moveButton;
global string $resizeButton;
$isMoveDistributionActive = false;
$isResizeDistributionActive = false;
iconTextButton -e -l "Start Moving Distribution" $moveButton;
iconTextButton -e -l "Start Resizing Distribution" $resizeButton;
// Undo tools
global int $canUndoMove;
global string $undoMoveButton;
global int $canUndoResize;
global string $undoResizeButton;
$canUndoMove = false;
$canUndoResize = false;
iconTextButton -e -en false $undoMoveButton;
iconTextButton -e -en false $undoResizeButton;
}
}
/*global proc makePlanarSelectRegion()
{
global string $selectionMat;
global string $selectionRegion[];
global int $selectionType;
global string $selectionLayer;
global int $isTerrainLoaded;
// Selection region sliders
global string $moveXSlider;
global string $moveZSlider;
global string $widthSlider;
global string $heightSlider;
int $res = 10; // plane resolution
if ($selectionType != 1) {
// Create a 2-D selection bound from a plane
$selectionRegion = `polyPlane -name "selectionRegion" -sw $res -sh $res`;
scale -r 2 2 2;
// Assign selection material to plane
hyperShade -assign $selectionMat;
$selectionType = 1;
// Add the plane to selectionLayer
layerEditorAddObjects $selectionLayer;
// Map region vertices to terrain height (if there is terrain)
if ($isTerrainLoaded) {
WorldPalette -msr true;
}
select -cl ; // deselect
// Enable move sliders
floatSliderGrp -e -en true $moveXSlider;
floatSliderGrp -e -en true $moveZSlider;
// Enable width & height sliders
floatSliderGrp -e -en true $widthSlider;
floatSliderGrp -e -en true $heightSlider;
}
}*/
global proc makeRadialSelectRegion()
{
global string $selectionMat;
global string $selectionRegion[];
global int $selectionType;
global string $selectionLayer;
global int $isTerrainLoaded;
// Selection region sliders
global string $moveXSlider;
global string $moveZSlider;
global string $widthSlider;
int $res = 20; // disk resolution
if ($selectionType != 2) {
// Create a 2-D selection bound from a disc
$selectionRegion = `polyCylinder -name "selectionRegion" -height 0.010 -sx $res -sz $res`;
// Assign selection material to disc
hyperShade -assign $selectionMat;
$selectionType = 2;
// Add the disc to selectionLayer
layerEditorAddObjects $selectionLayer;
// Map region vertices to terrain height (if there is terrain)
if ($isTerrainLoaded) {
WorldPalette -msr true;
}
select -cl ; // deselect
// Enable move sliders
floatSliderGrp -e -en true $moveXSlider;
floatSliderGrp -e -en true $moveZSlider;
// Enable width slider
floatSliderGrp -e -en true $widthSlider;
}
}
global proc focusOnCurrentSelection()
{
global string $selectionRegion[];
global int $selectionType;
if ($selectionType) {
select -r $selectionRegion[0];
viewFit -f 0.;
select -cl ; // deselect
} else {
confirmDialog -t "No Region Selected" -m "You must first create a selection region on canvas." -b "OK" -icn "warning";
}
}
global proc changeMoveStatus()
{
global int $selectionType;
global int $isMoveDistributionActive;
global string $moveButton;
// Undo state
global int $canUndoMove;
global string $undoMoveButton;
// Can't change status if there isn't an active selection region
if (!$selectionType) {
confirmDialog -t "No Region Selected" -m "You must first create a selection region on canvas." -b "OK" -icn "warning";
return;
}
$isMoveDistributionActive = ! $isMoveDistributionActive; // revert status
if ($isMoveDistributionActive) {
iconTextButton -e -l "Stop Moving Distribution" $moveButton;
global string $selectionRegion[];
global int $selectionType;
// Get region properties
float $width = 0;
float $height = 0;
float $bb[]; // bound box
float $wp[]; // world position
// RADIAL
$width = `polyCylinder -q -r $selectionRegion[0]`;
$bb = `exactWorldBoundingBox selectionRegion`;
$wp = `xform -q -ws -t $selectionRegion`;
// Set current distribution on the C++ side (aka get the list of geoms inside the selection region)
WorldPalette -sm true -st $selectionType -w $width -h $height -wp $wp[0] $wp[1] $wp[2] -mib $bb[0] $bb[1] $bb[2] -mab $bb[3] $bb[4] $bb[5];
} else {
iconTextButton -e -l "Start Moving Distribution" $moveButton;
// Disable undo
$canUndoMove = false;
iconTextButton -e -en false $undoMoveButton;
}
}
global proc changeResizeStatus()
{
global int $selectionType;
global int $isResizeDistributionActive;
global string $resizeButton;
// Undo state
global int $canUndoResize;
global string $undoResizeButton;
// Can't change status if there isn't an active selection region
if (!$selectionType) {
confirmDialog -t "No Region Selected" -m "You must first create a selection region on canvas." -b "OK" -icn "warning";
return;
}
$isResizeDistributionActive = ! $isResizeDistributionActive; // revert status
if ($isResizeDistributionActive) {
iconTextButton -e -l "Stop Resizing Distribution" $resizeButton;
global string $selectionRegion[];
global int $selectionType;
// Get region properties
float $width = 0;
float $height = 0;
float $bb[]; // bound box
float $wp[]; // world position
// RADIAL
$width = `polyCylinder -q -r $selectionRegion[0]`;
$bb = `exactWorldBoundingBox selectionRegion`;
$wp = `xform -q -ws -t $selectionRegion`;
// Set current distribution on the C++ side (aka get the list of geoms inside the selection region)
WorldPalette -sr true -st $selectionType -w $width -h $height -wp $wp[0] $wp[1] $wp[2] -mib $bb[0] $bb[1] $bb[2] -mab $bb[3] $bb[4] $bb[5];
} else {
iconTextButton -e -l "Start Resizing Distribution" $resizeButton;
// Disable undo
$canUndoResize = false;
iconTextButton -e -en false $undoResizeButton;
}
}
global proc moveAlongAxis(int $axis)
{
global string $selectionRegion[];
global int $selectionType;
global int $isTerrainLoaded;
// Selection region move sliders
global string $moveXSlider;
global string $moveZSlider;
global int $isMoveDistributionActive;
// Undo state
global int $canUndoMove;
global string $undoMoveButton;
float $val = 0; // coordinate value
float $dx = 0; // change in x-position
float $dz = 0; // change in z-position
if ($selectionType) {
select -r $selectionRegion[0];
if ($axis == 0) {
// Add onto current X position
$val = `floatSliderGrp -q -v $moveXSlider`;
$dx = $val - (getAttr ($selectionRegion[0] + ".translateX"));
move -x $val;
} else if ($axis == 1) {
// Add onto current Z position
$val = `floatSliderGrp -q -v $moveZSlider`;
$dz = $val - (getAttr ($selectionRegion[0] + ".translateZ"));
move -z $val;
}
select -cl ; // deselect
// False: Move selection region
// True: Move distribution within selection region
if ($isMoveDistributionActive) {
// Map region vertices to terrain height (if there is terrain)
if ($isTerrainLoaded) {
if ($axis == 0) {
WorldPalette -msr true -dx $dx;
} else if ($axis == 1) {
WorldPalette -msr true -dz $dz;
}
} else {
if ($axis == 0) {
WorldPalette -dx $dx;
} else if ($axis == 1) {
WorldPalette -dz $dz;
}
}
// Update undo state
$canUndoMove = true;
iconTextButton -e -en true $undoMoveButton;
} else {
// Map region vertices to terrain height (if there is terrain)
if ($isTerrainLoaded) {
WorldPalette -msr true;
}
}
}
}
// Same function as moveAlongAxis except the region is moved with hotkeys instead
global proc moveAlongAxisHotkey(int $axis, int $value)
{
global string $selectionRegion[];
global int $selectionType;
global int $isTerrainLoaded;
// Selection region move sliders
global string $moveXSlider;
global string $moveZSlider;
global int $isMoveDistributionActive;
// Undo state
global int $canUndoMove;
global string $undoMoveButton;
float $val = 0; // coordinate value
float $dx = 0; // change in x-position
float $dz = 0; // change in z-position
if ($selectionType) {
select -r $selectionRegion[0];
if ($axis == 0) {
// Add onto current X position
$val = (getAttr ($selectionRegion[0] + ".translateX"));
$dx = $value;
move -x ($val + $dx);
// Update the slider value as well
floatSliderGrp -e -v ($val + $dx) $moveXSlider;
} else if ($axis == 1) {
// Add onto current Z position
$val = (getAttr ($selectionRegion[0] + ".translateZ"));
$dz = $value;
move -z ($val + $dz);
// Update the slider value as well
floatSliderGrp -e -v ($val + $dz) $moveZSlider;
}
select -cl ; // deselect
// False: Move selection region
// True: Move distribution within selection region
if ($isMoveDistributionActive) {
// Map region vertices to terrain height (if there is terrain)
if ($isTerrainLoaded) {
if ($axis == 0) {
WorldPalette -msr true -dx $dx;
} else if ($axis == 1) {
WorldPalette -msr true -dz $dz;
}
} else {
if ($axis == 0) {
WorldPalette -dx $dx;
} else if ($axis == 1) {
WorldPalette -dz $dz;
}
}
// Update undo state
$canUndoMove = true;
iconTextButton -e -en true $undoMoveButton;
} else {
// Map region vertices to terrain height (if there is terrain)
if ($isTerrainLoaded) {
WorldPalette -msr true;
}
}
}
}
global proc scaleRegion(int $axis)
{
// Selection region state
global string $selectionRegion[];
global int $selectionType;
global int $isMoveDistributionActive;
// Selection region sliders
global string $widthSlider;
global int $isResizeDistributionActive;
// Undo state
global int $canUndoResize;
global string $undoResizeButton;
// Terrain state
global int $isTerrainLoaded;
float $width = 0; // width of region
float $height = 0; // height of region
float $dw = 0; // change in width
float $dh = 0; // change in height
if ($selectionType) {
select -r $selectionRegion[0];
if ($axis == 0) {
$width = `floatSliderGrp -q -v $widthSlider`;
$dw = $width - (getAttr ($selectionRegion[1] + ".radius"));
// increase/decrease radius
setAttr ($selectionRegion[1] + ".radius") ($width);
}
select -cl ; // deselect
// False: Resize selection region
// True: Resize distribution within selection region
if ($isResizeDistributionActive) {
// Map region vertices to terrain height (if there is terrain)
WorldPalette -msr $isTerrainLoaded -dw $dw -dh $dh;
// Update undo state
$canUndoResize = true;
iconTextButton -e -en true $undoResizeButton;
} else {
// Map region vertices to terrain height (if there is terrain)
WorldPalette -msr $isTerrainLoaded;
}
// Also update the selection region for moving the distribution
// Get other region properties
float $bb[] = `exactWorldBoundingBox $selectionRegion`;
float $wp[] = `xform -q -ws -t $selectionRegion`;
if ($isMoveDistributionActive) {
WorldPalette -sm true -st $selectionType -w $width -h $height -wp $wp[0] $wp[1] $wp[2] -mib $bb[0] $bb[1] $bb[2] -mab $bb[3] $bb[4] $bb[5];
}
}
}
global proc clearSelection()
{
global string $clearButton;
global string $selectionRegion[];
global int $selectionType;
// Undo state
global int $canUndoClear;
global string $undoClearButton;
// We must have an active selection region to be able to clear a distribution
if ($selectionType) {
// Get the current region properties
float $width = 0;
float $height = 0;
float $boundBox[];
float $worldPos[];
// RADIAL
$width = `polyCylinder -q -r $selectionRegion[0]`;
$boundBox = `exactWorldBoundingBox selectionRegion`;
$worldPos = `xform -q -ws -t $selectionRegion`;
// Clear the selection
WorldPalette -csl true -st $selectionType -w $width -h $height -wp $worldPos[0] $worldPos[1] $worldPos[2] -mib $boundBox[0] $boundBox[1] $boundBox[2] -mab $boundBox[3] $boundBox[4] $boundBox[5];
// Update undo state
$canUndoClear = true;
iconTextButton -e -en true $undoClearButton;
} else {
confirmDialog -t "No Region Selected" -m "You must first create a selection region on canvas." -b "OK" -icn "warning";
}
}
global proc saveSelection()
{
global string $selectionRegion[];
global int $selectionType;
if ($selectionType) {
global string $palette[];
global int $paletteOccupancy[];
global int $numSlots;
global int $freeIdx;
// Create the image for this button
string $ws=`workspace -q -fullName`;
string $wsp=$ws + "/WorldPalette/images";
sysFile -makeDir $wsp;
string $imgFile = $wsp + "/palette" + $freeIdx + ".png";
lookThru top; // look through the top camera
focusOnCurrentSelection;
setAttr ("SelectionLayer" + ".visibility") 0; // Don't show the GUI geometry in the screenshot
refresh -cv -fe "png" -fn $imgFile; // take the screenshot
setAttr ("SelectionLayer" + ".visibility") 1; // Put the GUI geometry back in
lookThru persp; // go back to perspective view
iconTextButton -e -i $imgFile $palette[$freeIdx]; // set slot image to screenshot
iconTextButton -e -en true $palette[$freeIdx]; // make the button selectable
if (!$paletteOccupancy[$freeIdx]) {
// This slot is free - fill it
$paletteOccupancy[$freeIdx] = 1;
}
// Send data to save the distribution
float $width = 0;
float $height = 0;
float $boundBox[];
float $worldPos[];
// RADIAL
$width = `polyCylinder -q -r $selectionRegion[0]`;
$boundBox = `exactWorldBoundingBox selectionRegion`;
$worldPos = `xform -q -ws -t $selectionRegion`;
WorldPalette -st $selectionType -w $width -h $height -wp $worldPos[0] $worldPos[1] $worldPos[2] -mib $boundBox[0] $boundBox[1] $boundBox[2] -mab $boundBox[3] $boundBox[4] $boundBox[5] -pi $freeIdx -ge true;
// Update the free slot index
$freeIdx = $freeIdx + 1;
if ($freeIdx == $numSlots) {
$freeIdx = 0; // set back to idx 0
}
} else {
confirmDialog -t "No Region Selected" -m "You must first create a selection region on canvas." -b "OK" -icn "warning";
}
}
global proc getDistributionFromPalette(int $index)
{
global int $currentSlot;
global string $palette[];
// Update currently selected slot
if ($currentSlot != -1) {
iconTextButton -e -nbg true $palette[$currentSlot];
}
$currentSlot = $index;
print ("Current selection: " + $currentSlot);
iconTextButton -e -ebg true -bgc 0 0.75 1 $palette[$currentSlot];
// This is where we will set the brush stroke to use the given distribution!
WorldPalette -ge true -pi $currentSlot;
}
global proc updatePalette()
{
global string $palette[];
global int $paletteOccupancy[];
global int $numSlots;
global int $currentSlot;
// Set the palette images accordingly
// Free slot = default image (sphere.png) for now; Occupied = top view img
// Get the image directory
string $ws = `workspace -q -fullName`;
string $wsp = $ws + "/WorldPalette/images";
sysFile -makeDir $wsp; // creates the image directory (if one doesn't already exist)
for ($i = 0; $i < $numSlots; $i++) {
if ($paletteOccupancy[$i]) {
// Make the button clickable
iconTextButton -e -en true $palette[$i];
// Set button image to top view image if slot is occupied
string $imgFile = $wsp + "/palette" + $i + ".png";
if ($currentSlot == $i) {
// Mark this button as currently selected
iconTextButton -e -i $imgFile -ebg true -bgc 0 0.75 1 $palette[$i];
} else {
iconTextButton -e -i $imgFile $palette[$i];
}
}
}
}
global proc updatePaletteFromFile(int $index)
{
global string $palette[];
global int $paletteOccupancy[];
global int $numSlots;
global int $currentSlot;
global int $freeIdx;
// Set the palette images accordingly
// Free slot = default image (sphere.png) for now; Occupied = top view img
// updating occupancy and next free slot
for ($x = 0; $x < $numSlots; $x++) {
if ($x < $index) {
$paletteOccupancy[$x] = 1;
}
else {
$paletteOccupancy[$x] = 0;
}
}
$freeIdx = $index;
updatePalette();
}
global proc loadPalette()
{
global string $palette[];
global int $paletteOccupancy[];
global int $numSlots;
global int $currentSlot;
global int $freeIdx;
// TO DO: Import palette
// We first want to clear our palette - however this should happen ONLY IF the user was able to import a palette successfully
// You can call the clearPalette() function implemented below to clear the palette
clearPalette();
WorldPalette -pl true;
// Once the palette is cleared, you can check out the updatePalette() function above to see how to set palette slot icons to corresponding screenshots
// Remember to update the paletteOccupancy and freeIdx variables as well
// For palette occupancy, we set entries to either 0 (free slot) or 1 (occupied slot)
// freeIdx is used to track the next available slot index (we use it for saving distributions)
// this is updated on the c++ side by calling the function updatePalettefromfile
}
global proc savePalette()
{
global string $palette[];
global int $paletteOccupancy[];
global int $numSlots;
// TO DO: Export palette
// Nothing to do on the MEL side - handle it on C++
WorldPalette -ps true;
// We can show this popup if saving is successful
confirmDialog -t "Save Palette" -m "Palette is saved successfully." -b "OK" -icn "information";
}
global proc clearPalette()
{
global string $palette[];
global int $paletteOccupancy[];
global int $numSlots;
global int $currentSlot;
global int $freeIdx;
// Screenshot directory
string $ws = `workspace -q -fullName`;
string $wsp = $ws + "/WorldPalette/images";
// First reset palette buttons and their occupancies
for ($i = 0; $i < $numSlots; $i++) {
// Reset the button
iconTextButton -e -i "sphere.png" $palette[$i];
iconTextButton -e -en false $palette[$i];
iconTextButton -e -ebg false $palette[$i];
if ($paletteOccupancy[$i]) {
// Delete the corresponding screenshot
string $imgFile = $wsp + "/palette" + $i + ".png";
sysFile -delete $imgFile;
}
$paletteOccupancy[$i] = 0; // reset occupancy
}
// Reset current slot and slot index where we can save the next distribution
$freeIdx = 0;
$currentSlot = -1;
// TO DO: Call C++ code to reset things on C++ side
WorldPalette -pc true;
}
global proc loadDefaultScene()
{
// We want to stop the distribution move tool because we're basically changing all scene geometry information
global int $isMoveDistributionActive;
global string $moveButton;
$isMoveDistributionActive = false;
iconTextButton -e -l "Start Moving Distribution" $moveButton;
global int $numTerrainLoads;
global string $terrainName;
global string $treeName;
global string $rockName;
global string $shrubName;
global string $grassName;
// Delete all geometry in the scene first
string $objects[] = `listTransforms -geometry`;
for ($geom in $objects) {
if ($geom == "selectionRegion") {
continue;
}
if ($geom == $treeName || $geom == $rockName || $geom == $shrubName || $geom == $grassName) {
continue;
}
if ($geom == $terrainName) {
continue;
}
delete $geom;
}
// Create a bunch of scene objects
int $numPrims = 6;
for ($i = 0; $i < $numPrims; $i++) {
int $t = 3; // TREE
float $rx = `rand -2 2`;
float $rz = `rand -2 2`;
addSceneGeometryAtLoc($t, $rx, 0, $rz);
}
int $numPrims2 = 6;
for ($i = 0; $i < $numPrims2; $i++) {
int $t = 2; // SHRUB
float $rx = `rand -3 3`;
float $rz = `rand -3 3`;
addSceneGeometryAtLoc($t, $rx, 0, $rz);
}
int $numPrims3 = 20;
for ($i = 0; $i < $numPrims3; $i++) {
int $t = 1; // ROCK
float $rx = `rand -4 4`;
float $rz = `rand -4 4`;
addSceneGeometryAtLoc($t, $rx, 0, $rz);
}
int $numPrims4 = 40;
for ($i = 0; $i < $numPrims4; $i++) {
int $t = 0; // GRASS
float $rx = `rand -5 5`;
float $rz = `rand -5 5`;
addSceneGeometryAtLoc($t, $rx, 0, $rz);
}
select -cl ; // deselect
}
global proc pasteDistribution()
{
global int $currentSlot; // A slot must be selected first for pasting distributions
global string $selectionRegion[];
global int $selectionType; // There must be a selection region
// Undo state
global int $canUndoPaste;
global string $undoPasteButton;
if ($currentSlot != -1) {
if ($selectionType) {
float $width = 0;
float $height = 0;
float $boundBox[];
float $worldPos[];
// RADIAL
$width = `polyCylinder -q -r $selectionRegion[0]`;
$boundBox = `exactWorldBoundingBox selectionRegion`;
$worldPos = `xform -q -ws -t $selectionRegion`;
WorldPalette -st 2 -w $width -wp $worldPos[0] $worldPos[1] $worldPos[2] -mib $boundBox[0] $boundBox[1] $boundBox[2] -mab $boundBox[3] $boundBox[4] $boundBox[5] -pi $currentSlot -ge false;
// Update undo state
$canUndoPaste = true;
iconTextButton -e -en true $undoPasteButton;
} else {
confirmDialog -t "No Region Selected" -m "You must first create a selection region on canvas." -b "OK" -icn "warning";
}
} else {
confirmDialog -t "No Slot Selected" -m "You must first select a distribution from the palette." -b "OK" -icn "warning";
}
}
global proc changeBrushStatus()
{
global int $currentSlot; // A slot must be selected first for using brush
global string $brushDragContext;
global int $isBrushActive;
global string $brushButton;
global string $brushWidthSlider;
// Undo state
global int $canUndoBrush;
global int $canUndoErase;
global string $undoBrushButton;
global string $undoEraseButton;
if ($currentSlot == -1) {
confirmDialog -t "No Slot Selected" -m "You must first select a distribution from the palette." -b "OK" -icn "warning";
return;
}
$isBrushActive = ! $isBrushActive; // revert status
if ($isBrushActive) {
iconTextButton -e -l "Stop Brush" $brushButton;
// Set tool to brush
setToolTo $brushDragContext;
// Enable width slider
floatSliderGrp -e -en true $brushWidthSlider;
} else {
iconTextButton -e -l "Use Brush" $brushButton;
// Set tool back to selection
setToolTo selectSuperContext;
// Disable width slider
floatSliderGrp -e -en false $brushWidthSlider;
// Disable undo
$canUndoBrush = false;
iconTextButton -e -en false $undoBrushButton;
}
// Terminate eraser tool
global int $isEraserActive;
global string $eraserButton;
$isEraserActive = false;
iconTextButton -e -l "Use Eraser" $eraserButton;
$canUndoErase = false;
iconTextButton -e -en false $undoEraseButton;
}
global proc changeEraserStatus()
{
global string $eraserDragContext;
global int $isEraserActive;
global string $eraserButton;
global string $brushWidthSlider;
// Undo state
global int $canUndoBrush;
global int $canUndoErase;
global string $undoBrushButton;
global string $undoEraseButton;
$isEraserActive = ! $isEraserActive; // revert status
if ($isEraserActive) {
iconTextButton -e -l "Stop Eraser" $eraserButton;
// Set tool to eraser
setToolTo $eraserDragContext;
// Enable width slider
floatSliderGrp -e -en true $brushWidthSlider;
} else {
iconTextButton -e -l "Use Eraser" $eraserButton;
// Set tool back to selection
setToolTo selectSuperContext;
// Disable width slider
floatSliderGrp -e -en false $brushWidthSlider;
// Disable undo
$canUndoErase = false;
iconTextButton -e -en false $undoEraseButton;
}
// Terminate brush tool
global int $isBrushActive;
global string $brushButton;
$isBrushActive = false;
iconTextButton -e -l "Use Brush" $brushButton;
$canUndoBrush = false;
iconTextButton -e -en false $undoBrushButton;
}
global proc changeBrushWidth()
{
global float $brushWidth;
global string $brushWidthSlider;
// Get the slider value
$brushWidth = `floatSliderGrp -q -v $brushWidthSlider`;
}
global proc brushInitialize()
{
// This function handles brush usage for the case where the user activates brush by clicking on the Maya side menu
global int $isBrushActive;
global string $brushButton;
global string $brushWidthSlider;
// Undo state
global int $canUndoErase;
global string $undoEraseButton;
$isBrushActive = true;
iconTextButton -e -l "Stop Brush" $brushButton;
// Terminate eraser tool
global int $isEraserActive;
global string $eraserButton;
$isEraserActive = false;
iconTextButton -e -l "Use Eraser" $eraserButton;
$canUndoErase = false;
iconTextButton -e -en false $undoEraseButton;
// Enable brush width slider
floatSliderGrp -e -en true $brushWidthSlider;
}
global proc eraserInitialize()
{
global int $isEraserActive;
global string $eraserButton;
global string $brushWidthSlider;
// Undo state
global int $canUndoBrush;
global string $undoBrushButton;
$isEraserActive = true;
iconTextButton -e -l "Stop Eraser" $eraserButton;
// Terminate brush tool
global int $isBrushActive;
global string $brushButton;
$isBrushActive = false;
iconTextButton -e -l "Use Brush" $brushButton;
$canUndoBrush = false;
iconTextButton -e -en false $undoBrushButton;
// Enable brush width slider
floatSliderGrp -e -en true $brushWidthSlider;
}
global proc brushFinalize()
{
// This function handles brush usage for the case where the user deactivates brush by clicking on the Maya side menu
global int $isBrushActive;
global string $brushButton;
global string $brushWidthSlider;
// Undo state
global int $canUndoBrush;
global string $undoBrushButton;
$isBrushActive = false;
iconTextButton -e -l "Use Brush" $brushButton;
// Disable brush width slider
floatSliderGrp -e -en false $brushWidthSlider;
// Disable undo
$canUndoBrush = false;
iconTextButton -e -en false $undoBrushButton;
}
global proc eraserFinalize()
{
// This function handles eraser usage for the case where the user deactivates eraser by clicking on the Maya side menu
global int $isEraserActive;
global string $eraserButton;
global string $brushWidthSlider;
// Undo state
global int $canUndoErase;
global string $undoEraseButton;
$isEraserActive = false;
iconTextButton -e -l "Use Eraser" $eraserButton;
// Disable brush width slider
floatSliderGrp -e -en false $brushWidthSlider;
// Disable undo
$canUndoErase = false;
iconTextButton -e -en false $undoEraseButton;
}
global proc brushPress()
{
float $pressPosition[] = `autoPlace -um`;
WorldPalette -sb true -sbp $pressPosition[0] $pressPosition[2]; // send stroke position to C++
}
global proc brushDrag()
{
global float $lastDragPosition[]; // last brush drag position
global int $first; // is this the first brush stroke during drag?
float $dragPosition[] = `autoPlace -um`;
if ($first) {
$first = false;
WorldPalette -sb true -sbp $dragPosition[0] $dragPosition[2]; // send stroke position to C++
} else {