/
SampleBrowser.h
1195 lines (1026 loc) · 50.8 KB
/
SampleBrowser.h
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
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2014 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __SampleBrowser_H__
#define __SampleBrowser_H__
#include "SampleContext.h"
#include "SamplePlugin.h"
#include "OgreTrays.h"
#include "OgreConfigFile.h"
#include "OgreTechnique.h"
#include "OgreArchiveManager.h"
#define ENABLE_SHADERS_CACHE 1
#if OGRE_PLATFORM == OGRE_PLATFORM_WINRT
# include <sdkddkver.h>
# if defined(_WIN32_WINNT) && _WIN32_WINNT == _WIN32_WINNT_WIN8
// For WinRT 8.0 we only support running from the cache file.
# undef ENABLE_SHADERS_CACHE
# define ENABLE_SHADERS_CACHE 1
# endif
#endif
#ifndef __OGRE_WINRT_PHONE
#define __OGRE_WINRT_PHONE 0
#endif
#ifdef OGRE_STATIC_LIB
#include "DefaultSamplesPlugin.h"
# ifdef SAMPLES_INCLUDE_PLAYPEN
# include "PlayPenTestPlugin.h"
# endif
#endif
#define CAROUSEL_REDRAW_EPS 0.001
namespace OgreBites
{
/*=============================================================================
| The OGRE Sample Browser. Features a menu accessible from all samples,
| dynamic configuration, resource reloading, node labeling, and more.
=============================================================================*/
class SampleBrowser : public SampleContext, public TrayListener
{
#ifdef OGRE_STATIC_LIB
typedef std::map<Ogre::String, Ogre::Plugin*> PluginMap;
PluginMap mPluginNameMap; // A structure to map plugin names to class types
#endif
public:
SampleBrowser(bool nograb = false, int startSampleIndex = -1)
: SampleContext("OGRE Sample Browser"), mGrabInput(!nograb)
{
mIsShuttingDown = false;
mTrayMgr = 0;
mLastViewCategory = 0;
mLastViewTitle = 0;
mLastSampleIndex = -1;
mStartSampleIndex = startSampleIndex;
mCategoryMenu = 0;
mSampleMenu = 0;
mSampleSlider = 0;
mTitleLabel = 0;
mDescBox = 0;
mRendererMenu = 0;
mCarouselPlace = 0.0f;
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
mGestureView = 0;
#endif
}
virtual void loadStartUpSample()
{
if (mStartSampleIndex != -1)
{
runSampleByIndex(mStartSampleIndex);
mStartSampleIndex = -1;
}
}
virtual void runSampleByIndex(int idx)
{
runSample(Ogre::any_cast<Sample*>(mThumbs[idx]->getUserObjectBindings().getUserAny()));
}
/*-----------------------------------------------------------------------------
| Extends runSample to handle creation and destruction of dummy scene.
-----------------------------------------------------------------------------*/
virtual void runSample(Sample* s)
{
if (mCurrentSample) // sample quitting
{
mCurrentSample->_shutdown();
mCurrentSample = 0;
mSamplePaused = false; // don't pause next sample
// create dummy scene and modify controls
createDummyScene();
mTrayMgr->showBackdrop("SdkTrays/Bands");
mTrayMgr->showAll();
((Button*)mTrayMgr->getWidget("StartStop"))->setCaption("Start Sample");
}
if (s) // sample starting
{
// destroy dummy scene and modify controls
((Button*)mTrayMgr->getWidget("StartStop"))->setCaption("Stop Sample");
mTrayMgr->showBackdrop("SdkTrays/Shade");
mTrayMgr->hideAll();
destroyDummyScene();
try
{
s->_setupTrays(mWindow);
SampleContext::runSample(s);
}
catch (Ogre::Exception& e) // if failed to start, show error and fall back to menu
{
destroyDummyScene();
s->_shutdown();
createDummyScene();
mTrayMgr->showBackdrop("SdkTrays/Bands");
mTrayMgr->showAll();
((Button*)mTrayMgr->getWidget("StartStop"))->setCaption("Start Sample");
mTrayMgr->showOkDialog("Error!", e.getDescription() + "\nSource: " + e.getSource());
}
}
}
/// catch any exceptions that might drop out of event handlers implemented by Samples
bool frameStarted(const Ogre::FrameEvent& evt)
{
try
{
return SampleContext::frameStarted(evt);
}
catch (Ogre::Exception& e) // show error and fall back to menu
{
runSample(0);
mTrayMgr->showOkDialog("Error!", e.getDescription() + "\nSource: " + e.getSource());
}
return true;
}
/*-----------------------------------------------------------------------------
| Extends frameRenderingQueued to update tray manager and carousel.
-----------------------------------------------------------------------------*/
bool frameRenderingQueued(const Ogre::FrameEvent& evt)
{
// don't do all these calculations when sample's running or when in configuration screen or when no samples loaded
if (!mLoadedSamples.empty() && mTitleLabel->getTrayLocation() != TL_NONE && (!mCurrentSample || mSamplePaused))
{
// makes the carousel spin smoothly toward its right position
float carouselOffset = mSampleMenu->getSelectionIndex() - mCarouselPlace;
if (std::abs(carouselOffset) <= CAROUSEL_REDRAW_EPS) mCarouselPlace = mSampleMenu->getSelectionIndex();
else mCarouselPlace += carouselOffset * Ogre::Math::Clamp<float>(evt.timeSinceLastFrame * 15.0, -1.0, 1.0);
// update the thumbnail positions based on carousel state
for (int i = 0; i < (int)mThumbs.size(); i++)
{
if(carouselOffset == 0) break;
Ogre::Real thumbOffset = mCarouselPlace - i;
Ogre::Real phase = (thumbOffset / 2.0) - 2.8;
if (thumbOffset < -5 || thumbOffset > 4) // prevent thumbnails from wrapping around in a circle
{
mThumbs[i]->hide();
continue;
}
else mThumbs[i]->show();
Ogre::Real left = Ogre::Math::Cos(phase) * 200.0;
Ogre::Real top = Ogre::Math::Sin(phase) * 200.0;
Ogre::Real scale = 1.0 / Ogre::Math::Pow((Ogre::Math::Abs(thumbOffset) + 1.0), 0.75);
Ogre::BorderPanelOverlayElement* frame =
(Ogre::BorderPanelOverlayElement*)mThumbs[i]->getChildren().begin()->second;
mThumbs[i]->setDimensions(128.0 * scale, 96.0 * scale);
frame->setDimensions(mThumbs[i]->getWidth() + 16.0, mThumbs[i]->getHeight() + 16.0);
mThumbs[i]->setPosition((int)(left - 80.0 - (mThumbs[i]->getWidth() / 2.0)),
(int)(top - 5.0 - (mThumbs[i]->getHeight() / 2.0)));
if (i == mSampleMenu->getSelectionIndex()) frame->setBorderMaterialName("SdkTrays/Frame/Over");
else frame->setBorderMaterialName("SdkTrays/Frame");
}
}
mTrayMgr->frameRendered(evt);
return SampleContext::frameRenderingQueued(evt);
}
/*-----------------------------------------------------------------------------
| Handles confirmation dialog responses.
-----------------------------------------------------------------------------*/
virtual void yesNoDialogClosed(const Ogre::DisplayString& question, bool yesHit)
{
if (question.substr(0, 14) == "This will stop" && yesHit) // confirm unloading of samples
{
runSample(0);
buttonHit((Button*)mTrayMgr->getWidget("UnloadReload"));
}
}
/*-----------------------------------------------------------------------------
| Handles button widget events.
-----------------------------------------------------------------------------*/
virtual void buttonHit(Button* b)
{
if (b->getName() == "StartStop") // start or stop sample
{
if (b->getCaption() == "Start Sample")
{
if (mLoadedSamples.empty()) {
mTrayMgr->showOkDialog("Error!", "No sample selected!");
} else {
// use the sample pointer we stored inside the thumbnail
Ogre::Renderable* r = mThumbs[mSampleMenu->getSelectionIndex()];
runSample(Ogre::any_cast<Sample*>(r->getUserObjectBindings().getUserAny()));
}
} else
runSample(0);
}
else if (b->getName() == "UnloadReload") // unload or reload sample plugins and update controls
{
if (b->getCaption() == "Unload Samples")
{
if (mCurrentSample) mTrayMgr->showYesNoDialog("Warning!", "This will stop the current sample. Unload anyway?");
else
{
// save off current view and try to restore it on the next reload
mLastViewTitle = mSampleMenu->getSelectionIndex();
mLastViewCategory = mCategoryMenu->getSelectionIndex();
unloadSamples();
populateSampleMenus();
b->setCaption("Reload Samples");
}
}
else
{
loadSamples();
populateSampleMenus();
if (!mLoadedSamples.empty()) b->setCaption("Unload Samples");
try // attempt to restore the last view before unloading samples
{
mCategoryMenu->selectItem(mLastViewCategory);
mSampleMenu->selectItem(mLastViewTitle);
}
catch (Ogre::Exception&) {}
}
}
else if (b->getName() == "Configure") // enter configuration screen
{
mTrayMgr->removeWidgetFromTray("StartStop");
mTrayMgr->removeWidgetFromTray("Configure");
#if OGRE_PLATFORM != OGRE_PLATFORM_ANDROID
mTrayMgr->removeWidgetFromTray("UnloadReload");
mTrayMgr->removeWidgetFromTray("Quit");
mTrayMgr->moveWidgetToTray("Apply", TL_RIGHT);
#endif
mTrayMgr->moveWidgetToTray("Back", TL_RIGHT);
for (unsigned int i = 0; i < mThumbs.size(); i++)
{
mThumbs[i]->hide();
}
while (mTrayMgr->getTrayContainer(TL_CENTER)->isVisible())
{
mTrayMgr->removeWidgetFromTray(TL_CENTER, 0);
}
while (mTrayMgr->getTrayContainer(TL_LEFT)->isVisible())
{
mTrayMgr->removeWidgetFromTray(TL_LEFT, 0);
}
mTrayMgr->moveWidgetToTray("ConfigLabel", TL_LEFT);
mTrayMgr->moveWidgetToTray(mRendererMenu, TL_LEFT);
mTrayMgr->moveWidgetToTray("ConfigSeparator", TL_LEFT);
mRendererMenu->selectItem(mRoot->getRenderSystem()->getName());
windowResized(mWindow);
}
else if (b->getName() == "Back") // leave configuration screen
{
while (mTrayMgr->getWidgets(mRendererMenu->getTrayLocation()).size() > 3)
{
mTrayMgr->destroyWidget(mRendererMenu->getTrayLocation(), 3);
}
while (!mTrayMgr->getWidgets(TL_NONE).empty())
{
mTrayMgr->moveWidgetToTray(TL_NONE, 0, TL_LEFT);
}
mTrayMgr->removeWidgetFromTray("Apply");
mTrayMgr->removeWidgetFromTray("Back");
mTrayMgr->removeWidgetFromTray("ConfigLabel");
mTrayMgr->removeWidgetFromTray(mRendererMenu);
mTrayMgr->removeWidgetFromTray("ConfigSeparator");
mTrayMgr->moveWidgetToTray("StartStop", TL_RIGHT);
#if OGRE_PLATFORM != OGRE_PLATFORM_ANDROID
mTrayMgr->moveWidgetToTray("UnloadReload", TL_RIGHT);
#endif
mTrayMgr->moveWidgetToTray("Configure", TL_RIGHT);
#if OGRE_PLATFORM != OGRE_PLATFORM_ANDROID
mTrayMgr->moveWidgetToTray("Quit", TL_RIGHT);
#endif
mCarouselPlace += CAROUSEL_REDRAW_EPS; // force redraw
windowResized(mWindow);
}
else if (b->getName() == "Apply") // apply any changes made in the configuration screen
{
bool reset = false;
auto options =
mRoot->getRenderSystemByName(mRendererMenu->getSelectedItem())->getConfigOptions();
Ogre::NameValuePairList newOptions;
// collect new settings and decide if a reset is needed
if (mRendererMenu->getSelectedItem() != mRoot->getRenderSystem()->getName()) {
reset = true;
}
for (unsigned int i = 3; i < mTrayMgr->getWidgets(mRendererMenu->getTrayLocation()).size(); i++)
{
SelectMenu* menu = (SelectMenu*)mTrayMgr->getWidgets(mRendererMenu->getTrayLocation())[i];
if (menu->getSelectedItem() != options[menu->getCaption()].currentValue) reset = true;
newOptions[menu->getCaption()] = menu->getSelectedItem();
}
// reset with new settings if necessary
if (reset) reconfigure(mRendererMenu->getSelectedItem(), newOptions);
}
else
{
mRoot->queueEndRendering(); // exit browser
}
}
/*-----------------------------------------------------------------------------
| Handles menu item selection changes.
-----------------------------------------------------------------------------*/
virtual void itemSelected(SelectMenu* menu)
{
if (menu == mCategoryMenu) // category changed, so update the sample menu, carousel, and slider
{
for (unsigned int i = 0; i < mThumbs.size(); i++) // destroy all thumbnails in carousel
{
Ogre::MaterialManager::getSingleton().remove(mThumbs[i]->getName(), "Essential");
Widget::nukeOverlayElement(mThumbs[i]);
}
mThumbs.clear();
Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();
Ogre::String selectedCategory;
if (menu->getSelectionIndex() != -1) selectedCategory = menu->getSelectedItem();
else
{
mTitleLabel->setCaption("");
mDescBox->setText("");
}
bool all = selectedCategory == "All";
Ogre::StringVector sampleTitles;
Ogre::MaterialPtr templateMat = Ogre::MaterialManager::getSingleton().getByName("SdkTrays/SampleThumbnail");
// populate the sample menu and carousel with filtered samples
for (SampleSet::iterator i = mLoadedSamples.begin(); i != mLoadedSamples.end(); i++)
{
Ogre::NameValuePairList& info = (*i)->getInfo();
if (all || info["Category"] == selectedCategory)
{
Ogre::String name = "SdkTrays/SampleThumb" + Ogre::StringConverter::toString(sampleTitles.size() + 1);
// clone a new material for sample thumbnail
Ogre::MaterialPtr newMat = templateMat->clone(name);
Ogre::TextureUnitState* tus = newMat->getTechnique(0)->getPass(0)->getTextureUnitState(0);
tus->setTextureName(info["Thumbnail"]);
// create sample thumbnail overlay
Ogre::BorderPanelOverlayElement* bp = (Ogre::BorderPanelOverlayElement*)
om.createOverlayElementFromTemplate("SdkTrays/Picture", "BorderPanel", name);
bp->setHorizontalAlignment(Ogre::GHA_RIGHT);
bp->setVerticalAlignment(Ogre::GVA_CENTER);
bp->setMaterialName(name);
bp->getUserObjectBindings().setUserAny(Ogre::Any(*i));
mTrayMgr->getTraysLayer()->add2D(bp);
// add sample thumbnail and title
mThumbs.push_back(bp);
sampleTitles.push_back((*i)->getInfo()["Title"]);
}
}
mCarouselPlace = CAROUSEL_REDRAW_EPS; // reset carousel
mSampleMenu->setItems(sampleTitles);
if (mSampleMenu->getNumItems() != 0) itemSelected(mSampleMenu);
mSampleSlider->setRange(1, static_cast<Ogre::Real>(sampleTitles.size()), static_cast<Ogre::Real>(sampleTitles.size()));
}
else if (menu == mSampleMenu) // sample changed, so update slider, label and description
{
if (mSampleSlider->getValue() != menu->getSelectionIndex() + 1)
mSampleSlider->setValue(menu->getSelectionIndex() + 1);
Ogre::Renderable* r = mThumbs[mSampleMenu->getSelectionIndex()];
Sample* s = Ogre::any_cast<Sample*>(r->getUserObjectBindings().getUserAny());
mTitleLabel->setCaption(menu->getSelectedItem());
mDescBox->setText("Category: " + s->getInfo()["Category"] + "\nDescription: " + s->getInfo()["Description"]);
if (mCurrentSample != s) ((Button*)mTrayMgr->getWidget("StartStop"))->setCaption("Start Sample");
else ((Button*)mTrayMgr->getWidget("StartStop"))->setCaption("Stop Sample");
}
else if (menu == mRendererMenu) // renderer selected, so update all settings
{
while (mTrayMgr->getWidgets(mRendererMenu->getTrayLocation()).size() > 3)
{
mTrayMgr->destroyWidget(mRendererMenu->getTrayLocation(), 3);
}
auto options = mRoot->getRenderSystemByName(menu->getSelectedItem())->getConfigOptions();
unsigned int i = 0;
// create all the config option select menus
for (Ogre::ConfigOptionMap::iterator it = options.begin(); it != options.end(); it++)
{
i++;
SelectMenu* optionMenu = mTrayMgr->createLongSelectMenu
(TL_LEFT, "ConfigOption" + Ogre::StringConverter::toString(i), it->first, 450, 240, 10);
optionMenu->setItems(it->second.possibleValues);
// if the current config value is not in the menu, add it
if(optionMenu->containsItem(it->second.currentValue) == false)
{
optionMenu->addItem(it->second.currentValue);
}
optionMenu->selectItem(it->second.currentValue);
}
windowResized(mWindow);
}
}
/*-----------------------------------------------------------------------------
| Handles sample slider changes.
-----------------------------------------------------------------------------*/
virtual void sliderMoved(Slider* slider)
{
// format the caption to be fraction style
Ogre::String denom = "/" + Ogre::StringConverter::toString(mSampleMenu->getNumItems());
slider->setValueCaption(slider->getValueCaption() + denom);
// tell the sample menu to change if it hasn't already
if (mSampleMenu->getSelectionIndex() != -1 && mSampleMenu->getSelectionIndex() != slider->getValue() - 1)
mSampleMenu->selectItem(slider->getValue() - 1);
}
/*-----------------------------------------------------------------------------
| Handles keypresses.
-----------------------------------------------------------------------------*/
virtual bool keyPressed(const KeyboardEvent& evt)
{
if (mTrayMgr->isDialogVisible()) return true; // ignore keypresses when dialog is showing
Keycode key = evt.keysym.sym;
if (key == SDLK_ESCAPE)
{
#if __OGRE_WINRT_PHONE
// If there is a quit button, assume that we intended to press it via 'ESC'.
if (mTrayMgr->areTraysVisible())
{
Widget *pWidget = mTrayMgr->getWidget("Quit");
if (pWidget)
{
buttonHit((Button*)pWidget); // on phone, quit entirely.
return false; // now act as if we didn't handle the button to get AppModel to exit.
}
}
#endif // __OGRE_WINRT_PHONE
if (mTitleLabel->getTrayLocation() != TL_NONE)
{
// if we're in the main screen and a sample's running, toggle sample pause state
if (mCurrentSample)
{
if (mSamplePaused)
{
mTrayMgr->hideAll();
unpauseCurrentSample();
}
else
{
pauseCurrentSample();
mTrayMgr->showAll();
}
}
}
else buttonHit((Button*)mTrayMgr->getWidget("Back")); // if we're in config, just go back
}
else if ((key == SDLK_UP || key == SDLK_DOWN) && mTitleLabel->getTrayLocation() != TL_NONE)
{
// if we're in the main screen, use the up and down arrow keys to cycle through samples
int newIndex = mSampleMenu->getSelectionIndex() + (key == SDLK_UP ? -1 : 1);
mSampleMenu->selectItem(Ogre::Math::Clamp<size_t>(newIndex, 0, mSampleMenu->getNumItems() - 1));
}
else if (key == SDLK_RETURN) // start or stop sample
{
if (!mLoadedSamples.empty() && (mSamplePaused || mCurrentSample == 0))
{
Ogre::Renderable* r = mThumbs[mSampleMenu->getSelectionIndex()];
Sample* newSample = Ogre::any_cast<Sample*>(r->getUserObjectBindings().getUserAny());
runSample(newSample == mCurrentSample ? 0 : newSample);
}
}
#if OGRE_NO_VIEWPORT_ORIENTATIONMODE == 0
else if (key == SDLK_M) // change orientation mode
{
unsigned int orientationMode = (unsigned int)mWindow->getViewport(0)->getOrientationMode();
orientationMode++;
if (orientationMode >= 4)
orientationMode = 0;
mWindow->getViewport(0)->setOrientationMode((Ogre::OrientationMode)orientationMode);
}
#endif
else if(key == SDLK_F9) // toggle full screen
{
// Make sure we use the window size as originally requested, NOT the
// current window size (which may have altered to fit desktop)
auto opti = mRoot->getRenderSystem()->getConfigOptions().find(
"Video Mode");
Ogre::StringVector vmopts = Ogre::StringUtil::split(opti->second.currentValue, " x");
unsigned int w = Ogre::StringConverter::parseUnsignedInt(vmopts[0]);
unsigned int h = Ogre::StringConverter::parseUnsignedInt(vmopts[1]);
mWindow->setFullscreen(!mWindow->isFullScreen(), w, h);
}
else if(key == SDLK_F11 || key == SDLK_F12) // Decrease and increase FSAA level on the fly
{
// current FSAA 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
unsigned decreasedFSAA[17] = { 0, 0, 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8 };
unsigned increasedFSAA[17] = { 2, 2, 4, 4, 8, 8, 8, 8,16,16,16,16,16,16,16,16, 0, };
unsigned FSAA = std::min(mWindow->getFSAA(), 16U);
unsigned newFSAA = (key == SDLK_F12) ? increasedFSAA[FSAA] : decreasedFSAA[FSAA];
if(newFSAA != 0)
mWindow->setFSAA(newFSAA, mWindow->getFSAAHint());
}
return SampleContext::keyPressed(evt);
}
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
void motionBegan( void )
{
}
void motionEnded( void )
{
if (mTrayMgr->isDialogVisible()) return; // ignore keypresses when dialog is showing
if (mTitleLabel->getTrayLocation() != TL_NONE)
{
// if we're in the main screen and a sample's running, toggle sample pause state
if (mCurrentSample)
{
if (mSamplePaused)
{
mTrayMgr->hideAll();
unpauseCurrentSample();
}
else
{
pauseCurrentSample();
mTrayMgr->showAll();
}
}
}
else buttonHit((Button*)mTrayMgr->getWidget("Back")); // if we're in config, just go back
}
void motionCancelled( void )
{
}
#endif
/*-----------------------------------------------------------------------------
| Extends pointerPressed to inject mouse press into tray manager, and to check
| for thumbnail clicks, just because we can.
-----------------------------------------------------------------------------*/
virtual bool mousePressed(const MouseButtonEvent& evt)
{
if (mTitleLabel->getTrayLocation() != TL_NONE)
{
for (unsigned int i = 0; i < mThumbs.size(); i++)
{
if (mThumbs[i]->isVisible() && Widget::isCursorOver(mThumbs[i],
Ogre::Vector2(mTrayMgr->getCursorContainer()->getLeft(), mTrayMgr->getCursorContainer()->getTop()), 0))
{
mSampleMenu->selectItem(i);
return true;
}
}
}
if (isCurrentSamplePaused()) return mTrayMgr->mousePressed(evt);
return SampleContext::mousePressed(evt);
}
// convert and redirect
virtual bool touchPressed(const TouchFingerEvent& evt) {
MouseButtonEvent e;
e.button = BUTTON_LEFT;
return mousePressed(e);
}
/*-----------------------------------------------------------------------------
| Extends pointerReleased to inject mouse release into tray manager.
-----------------------------------------------------------------------------*/
virtual bool mouseReleased(const MouseButtonEvent& evt)
{
if (isCurrentSamplePaused()) return mTrayMgr->mouseReleased(evt);
return SampleContext::mouseReleased(evt);
}
// convert and redirect
virtual bool touchReleased(const TouchFingerEvent& evt) {
MouseButtonEvent e;
e.button = BUTTON_LEFT;
return mouseReleased(e);
}
/*-----------------------------------------------------------------------------
| Extends pointerMoved to inject mouse position into tray manager, and checks
| for mouse wheel movements to slide the carousel, because we can.
-----------------------------------------------------------------------------*/
virtual bool mouseMoved(const MouseMotionEvent& evt)
{
if (isCurrentSamplePaused()) return mTrayMgr->mouseMoved(evt);
return SampleContext::mouseMoved(evt);
}
// convert and redirect
virtual bool touchMoved(const TouchFingerEvent& evt) {
MouseMotionEvent e;
e.x = evt.x * mWindow->getWidth();
e.y = evt.y * mWindow->getHeight();
e.xrel = evt.dx * mWindow->getWidth();
e.yrel = evt.dy * mWindow->getHeight();
return mouseMoved(e);
}
//TODO: Handle iOS and Android.
/** Mouse wheel scrolls the sample list.
*/
virtual bool mouseWheelRolled(const MouseWheelEvent& evt)
{
if (isCurrentSamplePaused() && mTitleLabel->getTrayLocation() != TL_NONE &&
mSampleMenu->getNumItems() != 0)
{
int newIndex = mSampleMenu->getSelectionIndex() - evt.y / Ogre::Math::Abs(evt.y);
mSampleMenu->selectItem(Ogre::Math::Clamp<size_t>(newIndex, 0, mSampleMenu->getNumItems() - 1));
}
return SampleContext::mouseWheelRolled(evt);
}
/*-----------------------------------------------------------------------------
| Extends windowResized to best fit menus on screen. We basically move the
| menu tray to the left for higher resolutions and move it to the center
| for lower resolutions.
-----------------------------------------------------------------------------*/
virtual void windowResized(Ogre::RenderWindow* rw)
{
if (!mTrayMgr) return;
Ogre::OverlayContainer* center = mTrayMgr->getTrayContainer(TL_CENTER);
Ogre::OverlayContainer* left = mTrayMgr->getTrayContainer(TL_LEFT);
if (center->isVisible() && rw->getWidth() < 1280 - center->getWidth())
{
while (center->isVisible())
{
mTrayMgr->moveWidgetToTray(mTrayMgr->getWidgets(TL_CENTER)[0], TL_LEFT);
}
}
else if (left->isVisible() && rw->getWidth() >= 1280 - left->getWidth())
{
while (left->isVisible())
{
mTrayMgr->moveWidgetToTray(mTrayMgr->getWidgets(TL_LEFT)[0], TL_CENTER);
}
}
SampleContext::windowResized(rw);
}
/*-----------------------------------------------------------------------------
| Extends setup to create dummy scene and tray interface.
-----------------------------------------------------------------------------*/
virtual void setup()
{
ApplicationContext::setup();
mWindow = getRenderWindow();
addInputListener(this);
if(mGrabInput) setWindowGrab();
else mTrayMgr->hideCursor();
#ifdef OGRE_STATIC_LIB
mPluginNameMap["DefaultSamples"] = new DefaultSamplesPlugin();
# ifdef SAMPLES_INCLUDE_PLAYPEN
mPluginNameMap["PlaypenTests"] = new PlaypenTestPlugin();
# endif
#endif
Sample* startupSample = loadSamples();
// create template material for sample thumbnails
Ogre::MaterialPtr thumbMat = Ogre::MaterialManager::getSingleton().create("SdkTrays/SampleThumbnail", "Essential");
thumbMat->getTechnique(0)->getPass(0)->createTextureUnitState();
setupWidgets();
windowResized(mWindow); // adjust menus for resolution
// if this is our first time running, and there's a startup sample, run it
if (startupSample && mFirstRun){
runSample(startupSample);
}
}
protected:
/*-----------------------------------------------------------------------------
| Overrides the default window title.
-----------------------------------------------------------------------------*/
virtual NativeWindowPair createWindow(const Ogre::String& name, uint32_t w, uint32_t h, Ogre::NameValuePairList miscParams)
{
#if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID
// Make sure Trays are not tiny - we cannot easily scale the UI, therefore just reduce resolution
float contentScaling = AConfiguration_getDensity(mAConfig)/float(ACONFIGURATION_DENSITY_HIGH);
if(contentScaling > 1.0)
{
miscParams["contentScalingFactor"] = std::to_string(contentScaling);
miscParams["FSAA"] = "2";
}
#endif
NativeWindowPair res = ApplicationContext::createWindow(name, w, h, miscParams);
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
mGestureView = [[SampleBrowserGestureView alloc] init];
mGestureView.mBrowser = this;
[[[UIApplication sharedApplication] keyWindow] addSubview:mGestureView];
#endif
return res;
}
/*-----------------------------------------------------------------------------
| Initialises only the browser's resources and those most commonly used
| by samples. This way, additional special content can be initialised by
| the samples that use them, so startup time is unaffected.
-----------------------------------------------------------------------------*/
virtual void loadResources()
{
Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Essential");
mTrayMgr = new TrayManager("BrowserControls", getRenderWindow(), this);
mTrayMgr->showBackdrop("SdkTrays/Bands");
mTrayMgr->getTrayContainer(TL_NONE)->hide();
#if ENABLE_SHADERS_CACHE == 1
enableShaderCache();
#endif
// Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
createDummyScene();
mTrayMgr->showLoadingBar(1, 0);
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
mTrayMgr->hideLoadingBar();
}
/*-----------------------------------------------------------------------------
| Loads sample plugins from a configuration file.
-----------------------------------------------------------------------------*/
virtual Sample* loadSamples()
{
Sample* startupSample = 0;
Ogre::StringVector unloadedSamplePlugins;
Ogre::ConfigFile cfg;
#if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID
Ogre::Archive* apk = Ogre::ArchiveManager::getSingleton().load("", "APKFileSystem", true);
cfg.load(apk->open(mFSLayer->getConfigFilePath("samples.cfg")));
#else
cfg.load(mFSLayer->getConfigFilePath("samples.cfg"));
#endif
Ogre::String sampleDir = cfg.getSetting("SampleFolder"); // Mac OS X just uses Resources/ directory
Ogre::StringVector sampleList = cfg.getMultiSetting("SamplePlugin");
Ogre::String startupSampleTitle = cfg.getSetting("StartupSample");
sampleDir = Ogre::FileSystemLayer::resolveBundlePath(sampleDir);
#if OGRE_PLATFORM != OGRE_PLATFORM_APPLE && OGRE_PLATFORM != OGRE_PLATFORM_APPLE_IOS
if (sampleDir.empty()) sampleDir = "."; // user didn't specify plugins folder, try current one
#endif
// add slash or backslash based on platform
char lastChar = sampleDir[sampleDir.length() - 1];
if (lastChar != '/' && lastChar != '\\')
{
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || (OGRE_PLATFORM == OGRE_PLATFORM_WINRT)
sampleDir += "\\";
#elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
sampleDir += "/";
#endif
}
SampleSet newSamples;
// loop through all sample plugins...
for (Ogre::StringVector::iterator i = sampleList.begin(); i != sampleList.end(); i++)
{
try // try to load the plugin
{
#ifdef OGRE_STATIC_LIB
Ogre::String sampleName = *i;
// in debug, remove any suffix
if(Ogre::StringUtil::endsWith(sampleName, "_d"))
sampleName = sampleName.substr(0, sampleName.length()-2);
Ogre::Plugin* pluginInstance = mPluginNameMap[sampleName];
if(pluginInstance)
{
mRoot->installPlugin(pluginInstance);
}
#else
mRoot->loadPlugin(sampleDir + *i);
#endif
}
catch (Ogre::Exception& e) // plugin couldn't be loaded
{
Ogre::LogManager::getSingleton().logError(e.what());
unloadedSamplePlugins.push_back(sampleDir + *i);
continue;
}
Ogre::Plugin* p = mRoot->getInstalledPlugins().back(); // acquire plugin instance
SamplePlugin* sp = dynamic_cast<SamplePlugin*>(p);
if (!sp) // this is not a SamplePlugin, so unload it
{
//unloadedSamplePlugins.push_back(sampleDir + *i);
#ifdef OGRE_STATIC_LIB
//mRoot->uninstallPlugin(p);
#else
//mRoot->unloadPlugin(sampleDir + *i);
#endif
continue;
}
mLoadedSamplePlugins.push_back(sampleDir + *i); // add to records
// go through every sample in the plugin...
newSamples = sp->getSamples();
for (SampleSet::iterator j = newSamples.begin(); j != newSamples.end(); j++)
{
Ogre::NameValuePairList& info = (*j)->getInfo(); // acquire custom sample info
mLoadedSamples.insert(*j); // add sample only after ensuring title for sorting
mSampleCategories.insert(info["Category"]); // add sample category
if (info["Title"] == startupSampleTitle) startupSample = *j; // we found the startup sample
}
}
if (!mLoadedSamples.empty()) mSampleCategories.insert("All"); // insert a category for all samples
if (!unloadedSamplePlugins.empty()) // show error message summarising missing or invalid plugins
{
Ogre::String message = "These requested sample plugins were either missing, corrupt or invalid:";
for (unsigned int i = 0; i < unloadedSamplePlugins.size(); i++)
{
message += "\n- " + unloadedSamplePlugins[i];
}
mTrayMgr->showOkDialog("Error!", message);
}
return startupSample;
}
/*-----------------------------------------------------------------------------
| Unloads any loaded sample plugins.
-----------------------------------------------------------------------------*/
virtual void unloadSamples()
{
#ifdef OGRE_STATIC_LIB
const Ogre::Root::PluginInstanceList pluginList = mRoot->getInstalledPlugins();
for(unsigned int i = 0; i < pluginList.size(); i++)
{
SamplePlugin* sp = dynamic_cast<SamplePlugin*>(pluginList[i]);
// This is a sample plugin so we can unload it
if(!sp) continue;
mRoot->uninstallPlugin(sp);
delete sp;
}
#else
for (unsigned int i = 0; i < mLoadedSamplePlugins.size(); i++)
{
mRoot->unloadPlugin(mLoadedSamplePlugins[i]);
}
#endif
mLoadedSamples.clear();
mLoadedSamplePlugins.clear();
mSampleCategories.clear();
}
/*-----------------------------------------------------------------------------
| Sets up main page for browsing samples.
-----------------------------------------------------------------------------*/
virtual void setupWidgets()
{
mTrayMgr->destroyAllWidgets();
// create main navigation tray
mTrayMgr->showLogo(TL_RIGHT);
mTrayMgr->createSeparator(TL_RIGHT, "LogoSep");
mTrayMgr->createButton(TL_RIGHT, "StartStop", "Start Sample", 120);
#if (OGRE_PLATFORM != OGRE_PLATFORM_WINRT) && (OGRE_PLATFORM != OGRE_PLATFORM_ANDROID)
mTrayMgr->createButton(TL_RIGHT, "UnloadReload", mLoadedSamples.empty() ? "Reload Samples" : "Unload Samples");
#endif
#if (OGRE_PLATFORM != OGRE_PLATFORM_WINRT)
mTrayMgr->createButton(TL_RIGHT, "Configure", "Configure");
#endif
#if (OGRE_PLATFORM != OGRE_PLATFORM_ANDROID)
mTrayMgr->createButton(TL_RIGHT, "Quit", "Quit");
#endif
// create sample viewing controls
float infoWidth = 250;
#if (OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS) || (OGRE_PLATFORM == OGRE_PLATFORM_ANDROID)
infoWidth *= 0.9;
#endif
mTitleLabel = mTrayMgr->createLabel(TL_LEFT, "SampleTitle", "");
mDescBox = mTrayMgr->createTextBox(TL_LEFT, "SampleInfo", "Sample Info", infoWidth, 208);
mCategoryMenu = mTrayMgr->createThickSelectMenu(TL_LEFT, "CategoryMenu", "Select Category", infoWidth, 10);
mSampleMenu = mTrayMgr->createThickSelectMenu(TL_LEFT, "SampleMenu", "Select Sample", infoWidth, 10);