-
Notifications
You must be signed in to change notification settings - Fork 47
/
Shader.cpp
859 lines (715 loc) · 21.2 KB
/
Shader.cpp
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
#include "Shader.h"
#include "i18n.h"
#include "iselection.h"
#include "iscenegraph.h"
#include "itextstream.h"
#include "iselectiontest.h"
#include "igroupnode.h"
#include "selectionlib.h"
#include "registry/registry.h"
#include "messages/TextureChanged.h"
#include "command/ExecutionFailure.h"
#include "command/ExecutionNotPossible.h"
#include "string/string.h"
#include "brush/FaceInstance.h"
#include "brush/BrushVisit.h"
#include "brush/TextureProjection.h"
#include "patch/PatchNode.h"
#include "selection/algorithm/Primitives.h"
#include "selection/shaderclipboard/ShaderClipboard.h"
#include "selection/shaderclipboard/ClosestTexturableFinder.h"
#include "scene/Traverse.h"
#include "string/case_conv.h"
namespace selection
{
namespace algorithm
{
void applyShaderToSelection(const std::string& shaderName)
{
UndoableCommand undo("setShader");
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.setShader(shaderName); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.setShader(shaderName); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void applyShaderToSelectionCmd(const cmd::ArgumentList& args)
{
if (args.size() != 1 || args[0].getString().empty())
{
rMessage() << "SetShaderOnSelection <shadername>" << std::endl;
return;
}
applyShaderToSelection(args[0].getString());
}
/** greebo: Applies the shader from the clipboard's face to the given <target> face
*/
void applyClipboardFaceToFace(Face& target)
{
// Get a reference to the source Texturable in the clipboard
Texturable& source = ShaderClipboard::Instance().getSource();
target.applyShaderFromFace(*(source.face));
}
/** greebo: Applies the shader from the clipboard's patch to the given <target> face
*/
void applyClipboardPatchToFace(Face& target)
{
// Get a reference to the source Texturable in the clipboard
Texturable& source = ShaderClipboard::Instance().getSource();
// Apply a default projection to the face
TextureProjection projection;
// Copy just the shader name, the rest is default value
target.setShader(source.patch->getShader());
target.SetTexdef(projection);
// To fix the extremely small scale we get when applying a default TextureProjection
target.applyDefaultTextureScale();
}
// Function may leak a cmd::ExecutionFailure if the source/target combination doesn't work out
void applyClipboardToTexturable(Texturable& target, bool projected, bool entireBrush)
{
// Get a reference to the source Texturable in the clipboard
Texturable& source = ShaderClipboard::Instance().getSource();
// Check the basic conditions
if (!target.empty() && !source.empty())
{
// Do we have a Face to copy from?
if (source.isFace())
{
if (target.isFace() && entireBrush)
{
// Copy Face >> Whole Brush
for (Brush::const_iterator i = target.brush->begin();
i != target.brush->end();
i++)
{
applyClipboardFaceToFace(*(*i));
}
}
else if (target.isFace() && !entireBrush)
{
// Copy Face >> Face
applyClipboardFaceToFace(*target.face);
}
else if (target.isPatch() && !entireBrush)
{
// Copy Face >> Patch
// Set the shader name first
target.patch->setShader(source.face->getShader());
// Either paste the texture projected or naturally
if (projected)
{
target.patch->pasteTextureProjected(source.face);
}
else
{
target.patch->pasteTextureNatural(source.face);
}
}
}
else if (source.isPatch())
{
// Source holds a patch
if (target.isFace() && entireBrush)
{
// Copy patch >> whole brush
for (Brush::const_iterator i = target.brush->begin();
i != target.brush->end();
i++)
{
applyClipboardPatchToFace(*(*i));
}
}
else if (target.isFace() && !entireBrush)
{
// Copy patch >> face
applyClipboardPatchToFace(*target.face);
}
else if (target.isPatch() && !entireBrush)
{
// Copy patch >> patch
target.patch->setShader(source.patch->getShader());
target.patch->pasteTextureNatural(*source.patch);
}
}
else if (source.isShader())
{
if (target.isFace() && entireBrush)
{
// Copy patch >> whole brush
for (Brush::const_iterator i = target.brush->begin();
i != target.brush->end();
i++)
{
(*i)->setShader(source.getShader());
}
}
else if (target.isFace() && !entireBrush)
{
target.face->setShader(source.getShader());
}
else if (target.isPatch() && !entireBrush)
{
target.patch->setShader(source.getShader());
}
}
}
}
void pasteShader(SelectionTest& test, bool projected, bool entireBrush)
{
// Construct the command string
std::string command("pasteShader");
command += (projected ? "Projected" : "Natural");
command += (entireBrush ? "ToBrush" : "");
UndoableCommand undo(command);
// Initialise an empty Texturable structure
Texturable target;
// Find a suitable target Texturable
ClosestTexturableFinder finder(test, target);
GlobalSceneGraph().root()->traverseChildren(finder);
if (target.isPatch() && entireBrush)
{
throw cmd::ExecutionFailure(
_("Can't paste shader to entire brush.\nTarget is not a brush."));
}
else
{
// Pass the call to the algorithm function taking care of all the IFs
applyClipboardToTexturable(target, projected, entireBrush);
}
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void pasteTextureCoords(SelectionTest& test)
{
UndoableCommand undo("pasteTextureCoordinates");
// Initialise an empty Texturable structure
Texturable target;
// Find a suitable target Texturable
ClosestTexturableFinder finder(test, target);
GlobalSceneGraph().root()->traverseChildren(finder);
// Get a reference to the source Texturable in the clipboard
Texturable& source = ShaderClipboard::Instance().getSource();
// Check the basic conditions
if (target.isPatch() && source.isPatch())
{
// Check if the dimensions match, emit an error otherwise
if (target.patch->getWidth() == source.patch->getWidth() &&
target.patch->getHeight() == source.patch->getHeight())
{
target.patch->pasteTextureCoordinates(source.patch);
}
else
{
throw cmd::ExecutionFailure(
_("Can't paste Texture Coordinates.\nTarget patch dimensions must match."));
}
}
else
{
if (source.isPatch())
{
// Nothing to do, this works for patches only
throw cmd::ExecutionFailure(_("Can't paste Texture Coordinates from patches to faces."));
}
else
{
// Nothing to do, this works for patches only
throw cmd::ExecutionFailure(_("Can't paste Texture Coordinates from faces."));
}
}
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void pasteShaderName(SelectionTest& test)
{
// Initialise an empty Texturable structure
Texturable target;
// Find a suitable target Texturable
ClosestTexturableFinder finder(test, target);
GlobalSceneGraph().root()->traverseChildren(finder);
if (target.empty())
{
return;
}
UndoableCommand undo("pasteTextureName");
// Get a reference to the source Texturable in the clipboard
Texturable& source = ShaderClipboard::Instance().getSource();
if (target.isPatch())
{
target.patch->setShader(source.getShader());
}
else if (target.isFace())
{
target.face->setShader(source.getShader());
}
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void pickShaderFromSelection(const cmd::ArgumentList& args)
{
GlobalShaderClipboard().clear();
const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
// Check for a single patch
if (info.totalCount == 1 && info.patchCount == 1)
{
try
{
Patch& sourcePatch = getLastSelectedPatch();
ShaderClipboard::Instance().setSource(sourcePatch);
}
catch (const InvalidSelectionException&)
{
throw cmd::ExecutionNotPossible(_("Can't copy Shader. Couldn't retrieve patch."));
}
}
else if (FaceInstance::Selection().size() == 1)
{
try
{
auto sourceFace = FaceInstance::Selection().back();
ShaderClipboard::Instance().setSource(sourceFace->getFace());
}
catch (const InvalidSelectionException&)
{
throw cmd::ExecutionNotPossible(_("Can't copy Shader. Couldn't retrieve face."));
}
}
else
{
// Nothing to do, this works for patches only
throw cmd::ExecutionNotPossible(_("Can't copy Shader. Please select a single face or patch."));
}
}
/** greebo: This applies the clipboard to the visited faces/patches.
*/
class ClipboardShaderApplicator
{
bool _natural;
public:
ClipboardShaderApplicator(bool natural = false) :
_natural(natural)
{}
void operator()(IPatch& patch)
{
try
{
Texturable target;
Patch& targetPatch = dynamic_cast<Patch&>(patch);
target.patch = &targetPatch;
target.node = targetPatch.getPatchNode().shared_from_this();
// Apply the shader (projected, not to the entire brush)
applyClipboardToTexturable(target, !_natural, false);
}
catch (const std::bad_cast&)
{}
}
void operator()(IFace& face)
{
try
{
Texturable target;
// Downcast the IFace reference
Face& targetFace = dynamic_cast<Face&>(face);
target.face = &targetFace;
target.node = targetFace.getBrush().getBrushNode().shared_from_this();
// Apply the shader (projected, not to the entire brush)
applyClipboardToTexturable(target, !_natural, false);
}
catch (const std::bad_cast&)
{}
}
};
void pasteShaderToSelection(const cmd::ArgumentList& args)
{
if (ShaderClipboard::Instance().getSource().empty())
{
return;
}
// Start a new command
UndoableCommand command("pasteShaderToSelection");
ClipboardShaderApplicator applicator;
GlobalSelectionSystem().foreachFace(applicator);
GlobalSelectionSystem().foreachPatch(applicator);
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void pasteShaderNaturalToSelection(const cmd::ArgumentList& args)
{
if (ShaderClipboard::Instance().getSource().empty())
{
return;
}
// Start a new command
UndoableCommand command("pasteShaderNaturalToSelection");
// greebo: Construct a visitor and traverse the selection
ClipboardShaderApplicator applicator(true); // true == paste natural
GlobalSelectionSystem().foreachFace(applicator);
GlobalSelectionSystem().foreachPatch(applicator);
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
TextureProjection getSelectedTextureProjection()
{
TextureProjection returnValue;
if (FaceInstance::Selection().size() == 1)
{
// Get the last selected face instance from the global
FaceInstance& faceInstance = *FaceInstance::Selection().back();
faceInstance.getFace().GetTexdef(returnValue);
}
return returnValue;
}
Vector2 getSelectedFaceShaderSize()
{
Vector2 returnValue(0,0);
if (FaceInstance::Selection().size() == 1)
{
// Get the last selected face instance from the global
FaceInstance& faceInstance = *FaceInstance::Selection().back();
returnValue[0] = faceInstance.getFace().getFaceShader().getWidth();
returnValue[1] = faceInstance.getFace().getFaceShader().getHeight();
}
return returnValue;
}
void fitTexture(const float repeatS, const float repeatT)
{
UndoableCommand command("fitTexture");
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.fitTexture(repeatS, repeatT); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.fitTexture(repeatS, repeatT); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void fitTextureCmd(const cmd::ArgumentList& args)
{
if (args.size() != 2)
{
rWarning() << "Usage: FitTexture <repeatU> <repeatV>" << std::endl;
return;
}
fitTexture(args[0].getDouble(), args[1].getDouble());
}
void flipTexture(unsigned int flipAxis)
{
UndoableCommand undo("flipTexture");
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.flipTexture(flipAxis); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.flipTexture(flipAxis); });
SceneChangeNotify();
}
void flipTextureS(const cmd::ArgumentList& args)
{
flipTexture(0);
}
void flipTextureT(const cmd::ArgumentList& args)
{
flipTexture(1);
}
void naturalTexture(const cmd::ArgumentList& args)
{
UndoableCommand undo("naturalTexture");
// Construct the "naturally" scaled Texdef structure
ShiftScaleRotation shiftScaleRotation;
float naturalScale = registry::getValue<float>("user/ui/textures/defaultTextureScale");
shiftScaleRotation.scale[0] = naturalScale;
shiftScaleRotation.scale[1] = naturalScale;
// Patches
GlobalSelectionSystem().foreachPatch(
[] (IPatch& patch) { patch.scaleTextureNaturally(); }
);
GlobalSelectionSystem().foreachFace(
[&] (IFace& face) { face.setShiftScaleRotation(shiftScaleRotation); }
);
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void shiftTexture(const Vector2& shift)
{
std::string command("shiftTexture: ");
command += "s=" + string::to_string(shift[0]) + ", t=" + string::to_string(shift[1]);
UndoableCommand undo(command);
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.shiftTexdefByPixels(shift[0], shift[1]); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.translateTexture(shift[0], shift[1]); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void scaleTexture(const Vector2& scale)
{
std::string command("scaleTexture: ");
command += "sScale=" + string::to_string(scale[0]) + ", tScale=" + string::to_string(scale[1]);
UndoableCommand undo(command);
// Prepare the according patch scale value (they are relatively scaled)
Vector2 patchScale;
// We need to have 1.05 for a +0.05 scale
// and a 1/1.05 for a -0.05 scale
for (int i = 0; i < 2; i++)
{
if (scale[i] >= 0.0f)
{
patchScale[i] = 1.0f + scale[i];
}
else
{
patchScale[i] = 1/(1.0f + fabs(scale[i]));
}
}
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.scaleTexdef(scale[0], scale[1]); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.scaleTexture(patchScale[0], patchScale[1]); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void rotateTexture(const float angle)
{
std::string command("rotateTexture: ");
command += "angle=" + string::to_string(angle);
UndoableCommand undo(command);
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.rotateTexdef(angle); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.rotateTexture(angle); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void shiftTextureLeft() {
shiftTexture(Vector2(-registry::getValue<float>("user/ui/textures/surfaceInspector/hShiftStep"), 0.0f));
}
void shiftTextureRight() {
shiftTexture(Vector2(registry::getValue<float>("user/ui/textures/surfaceInspector/hShiftStep"), 0.0f));
}
void shiftTextureUp() {
shiftTexture(Vector2(0.0f, registry::getValue<float>("user/ui/textures/surfaceInspector/vShiftStep")));
}
void shiftTextureDown() {
shiftTexture(Vector2(0.0f, -registry::getValue<float>("user/ui/textures/surfaceInspector/vShiftStep")));
}
void scaleTextureLeft() {
scaleTexture(Vector2(-registry::getValue<float>("user/ui/textures/surfaceInspector/hScaleStep"), 0.0f));
}
void scaleTextureRight() {
scaleTexture(Vector2(registry::getValue<float>("user/ui/textures/surfaceInspector/hScaleStep"), 0.0f));
}
void scaleTextureUp() {
scaleTexture(Vector2(0.0f, registry::getValue<float>("user/ui/textures/surfaceInspector/vScaleStep")));
}
void scaleTextureDown() {
scaleTexture(Vector2(0.0f, -registry::getValue<float>("user/ui/textures/surfaceInspector/vScaleStep")));
}
void rotateTextureClock() {
rotateTexture(fabs(registry::getValue<float>("user/ui/textures/surfaceInspector/rotStep")));
}
void rotateTextureCounter() {
rotateTexture(-fabs(registry::getValue<float>("user/ui/textures/surfaceInspector/rotStep")));
}
void rotateTexture(const cmd::ArgumentList& args) {
if (args.size() != 1) {
rMessage() << "Usage: TexRotate [+1|-1]" << std::endl;
return;
}
if (args[0].getInt() > 0) {
// Clockwise
rotateTextureClock();
}
else {
// Counter-Clockwise
rotateTextureCounter();
}
}
void scaleTexture(const cmd::ArgumentList& args) {
if (args.size() != 1) {
rMessage() << "Usage: TexScale 's t'" << std::endl;
rMessage() << " TexScale [up|down|left|right]" << std::endl;
rMessage() << "Example: TexScale '0.05 0' performs"
<< " a 105% scale in the s direction." << std::endl;
rMessage() << "Example: TexScale up performs"
<< " a vertical scale using the step value defined in the Surface Inspector."
<< std::endl;
return;
}
std::string arg = string::to_lower_copy(args[0].getString());
if (arg == "up") {
scaleTextureUp();
}
else if (arg == "down") {
scaleTextureDown();
}
if (arg == "left") {
scaleTextureLeft();
}
if (arg == "right") {
scaleTextureRight();
}
else {
// No special argument, retrieve the Vector2 argument and pass the call
scaleTexture(args[0].getVector2());
}
}
void shiftTextureCmd(const cmd::ArgumentList& args) {
if (args.size() != 1) {
rMessage() << "Usage: TexShift 's t'" << std::endl
<< " TexShift [up|down|left|right]" << std::endl
<< "[up|down|left|right| takes the step values "
<< "from the Surface Inspector." << std::endl;
return;
}
std::string arg = string::to_lower_copy(args[0].getString());
if (arg == "up") {
shiftTextureUp();
}
else if (arg == "down") {
shiftTextureDown();
}
if (arg == "left") {
shiftTextureLeft();
}
if (arg == "right") {
shiftTextureRight();
}
else {
// No special argument, retrieve the Vector2 argument and pass the call
shiftTexture(args[0].getVector2());
}
}
void alignTexture(EAlignType align)
{
std::string command("alignTexture: ");
command += "edge=";
IFace::AlignEdge faceAlignEdge = IFace::AlignEdge::Top;
IPatch::AlignEdge patchAlignEdge = IPatch::AlignEdge::Top;
switch (align)
{
case ALIGN_TOP:
command += "top";
patchAlignEdge = IPatch::AlignEdge::Top;
faceAlignEdge = IFace::AlignEdge::Top;
break;
case ALIGN_BOTTOM:
command += "bottom";
patchAlignEdge = IPatch::AlignEdge::Bottom;
faceAlignEdge = IFace::AlignEdge::Bottom;
break;
case ALIGN_LEFT:
command += "left";
patchAlignEdge = IPatch::AlignEdge::Left;
faceAlignEdge = IFace::AlignEdge::Left;
break;
case ALIGN_RIGHT:
command += "right";
patchAlignEdge = IPatch::AlignEdge::Right;
faceAlignEdge = IFace::AlignEdge::Right;
break;
};
UndoableCommand undo(command);
GlobalSelectionSystem().foreachFace([&] (IFace& face) { face.alignTexture(faceAlignEdge); });
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch) { patch.alignTexture(patchAlignEdge); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
void alignTextureCmd(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rMessage() << "Usage: TexAlign [top|bottom|left|right]" << std::endl;
return;
}
std::string arg = string::to_lower_copy(args[0].getString());
if (arg == "top")
{
alignTexture(ALIGN_TOP);
}
else if (arg == "bottom")
{
alignTexture(ALIGN_BOTTOM);
}
if (arg == "left")
{
alignTexture(ALIGN_LEFT);
}
if (arg == "right")
{
alignTexture(ALIGN_RIGHT);
}
else
{
rMessage() << "Usage: TexAlign [top|bottom|left|right]" << std::endl;
}
}
void normaliseTexture(const cmd::ArgumentList& args)
{
UndoableCommand undo("normaliseTexture");
GlobalSelectionSystem().foreachFace([] (IFace& face) { face.normaliseTexture(); });
GlobalSelectionSystem().foreachPatch([] (IPatch& patch) { patch.normaliseTexture(); });
SceneChangeNotify();
// Update the Texture Tools
radiant::TextureChangedMessage::Send();
}
class ByShaderSelector :
public scene::NodeVisitor
{
private:
std::string _shaderName;
bool _select;
public:
ByShaderSelector(const std::string& shaderName, bool select = true) :
_shaderName(shaderName),
_select(select)
{}
bool pre(const scene::INodePtr& node)
{
Brush* brush = Node_getBrush(node);
if (brush != NULL)
{
if (brush->hasShader(_shaderName))
{
Node_setSelected(node, _select);
}
return false; // don't traverse primitives
}
Patch* patch = Node_getPatch(node);
if (patch != NULL)
{
if (patch->getShader() == _shaderName)
{
Node_setSelected(node, _select);
}
return false; // don't traverse primitives
}
return true;
}
};
void selectItemsByShader(const std::string& shaderName)
{
ByShaderSelector selector(shaderName, true);
GlobalSceneGraph().root()->traverseChildren(selector);
}
void deselectItemsByShader(const std::string& shaderName)
{
ByShaderSelector selector(shaderName, false);
GlobalSceneGraph().root()->traverseChildren(selector);
}
void selectItemsByShaderCmd(const cmd::ArgumentList& args)
{
if (args.size() < 1)
{
rMessage() << "Usage: SelectItemsByShader <SHADERNAME>" << std::endl;
return;
}
selectItemsByShader(args[0].getString());
}
void deselectItemsByShaderCmd(const cmd::ArgumentList& args)
{
if (args.size() < 1)
{
rMessage() << "Usage: DeselectItemsByShader <SHADERNAME>" << std::endl;
return;
}
deselectItemsByShader(args[0].getString());
}
} // namespace algorithm
} // namespace selection