diff options
Diffstat (limited to 'Samples/CommonSrc/Util')
-rw-r--r-- | Samples/CommonSrc/Util/OptionMenu.cpp | 103 | ||||
-rw-r--r-- | Samples/CommonSrc/Util/OptionMenu.h | 66 | ||||
-rw-r--r-- | Samples/CommonSrc/Util/RenderProfiler.h | 2 |
3 files changed, 109 insertions, 62 deletions
diff --git a/Samples/CommonSrc/Util/OptionMenu.cpp b/Samples/CommonSrc/Util/OptionMenu.cpp index d9acc44..2d3c3d0 100644 --- a/Samples/CommonSrc/Util/OptionMenu.cpp +++ b/Samples/CommonSrc/Util/OptionMenu.cpp @@ -30,7 +30,7 @@ limitations under the License. //------------------------------------------------------------------------------------- bool OptionShortcut::MatchKey(OVR::KeyCode key, bool shift) const { - for (UInt32 i = 0; i < Keys.GetSize(); i++) + for (uint32_t i = 0; i < Keys.GetSize(); i++) { if (Keys[i].Key != key) continue; @@ -54,9 +54,9 @@ bool OptionShortcut::MatchKey(OVR::KeyCode key, bool shift) const return false; } -bool OptionShortcut::MatchGamepadButton(UInt32 gamepadButtonMask) const +bool OptionShortcut::MatchGamepadButton(uint32_t gamepadButtonMask) const { - for (UInt32 i = 0; i < GamepadButtons.GetSize(); i++) + for (uint32_t i = 0; i < GamepadButtons.GetSize(); i++) { if (GamepadButtons[i] & gamepadButtonMask) { @@ -75,7 +75,7 @@ bool OptionShortcut::MatchGamepadButton(UInt32 gamepadButtonMask) const String OptionMenuItem::PopNamespaceFrom(OptionMenuItem* menuItem) { String label = menuItem->Label; - for (UInt32 i = 0; i < label.GetLength(); i++) + for (uint32_t i = 0; i < label.GetLength(); i++) { if (label.GetCharAt(i) == '.') { @@ -91,7 +91,7 @@ String OptionMenuItem::PopNamespaceFrom(OptionMenuItem* menuItem) String OptionVar::FormatEnum(OptionVar* var) { - UInt32 index = var->GetEnumIndex(); + uint32_t index = var->GetEnumIndex(); if (index < var->EnumValues.GetSize()) return var->EnumValues[index].Name; return String("<Bad enum index>"); @@ -116,6 +116,12 @@ String OptionVar::FormatBool(OptionVar* var) return *var->AsBool() ? "On" : "Off"; } +String OptionVar::FormatTrigger(OptionVar* var) +{ + OVR_UNUSED(var); + return "[Trigger]"; +} + OptionVar::OptionVar(const char* name, void* pvar, VarType type, FormatFunction formatFunction, @@ -124,13 +130,13 @@ OptionVar::OptionVar(const char* name, void* pvar, VarType type, Label = name; Type = type; this->pVar = pvar; - fFormat = formatFunction; + fFormat = ((Type == Type_Trigger) && !formatFunction) ? FormatTrigger : formatFunction; fUpdate = updateFunction; pNotify = 0; FormatString= 0; - MaxFloat = Math<float>::MaxValue; - MinFloat = -Math<float>::MaxValue; + MaxFloat = MATH_FLOAT_MAXVALUE; + MinFloat = -MATH_FLOAT_MAXVALUE; StepFloat = 1.0f; FormatScale = 1.0f; @@ -138,12 +144,14 @@ OptionVar::OptionVar(const char* name, void* pvar, VarType type, MinInt = -(MaxInt) - 1; StepInt = 1; + SelectedIndex = 0; + ShortcutUp.pNotify = new FunctionNotifyContext<OptionVar, bool>(this, &OptionVar::NextValue); ShortcutDown.pNotify = new FunctionNotifyContext<OptionVar, bool>(this, &OptionVar::PrevValue); } -OptionVar::OptionVar(const char* name, SInt32* pvar, - SInt32 min, SInt32 max, SInt32 stepSize, +OptionVar::OptionVar(const char* name, int32_t* pvar, + int32_t min, int32_t max, int32_t stepSize, const char* formatString, FormatFunction formatFunction, UpdateFunction updateFunction) @@ -160,6 +168,8 @@ OptionVar::OptionVar(const char* name, SInt32* pvar, MaxInt = max; StepInt = stepSize; + SelectedIndex = 0; + ShortcutUp.pNotify = new FunctionNotifyContext<OptionVar, bool>(this, &OptionVar::NextValue); ShortcutDown.pNotify = new FunctionNotifyContext<OptionVar, bool>(this, &OptionVar::PrevValue); } @@ -184,6 +194,8 @@ OptionVar::OptionVar(const char* name, float* pvar, StepFloat = stepSize; FormatScale = formatScale; + SelectedIndex = 0; + ShortcutUp.pNotify = new FunctionNotifyContext<OptionVar, bool>(this, &OptionVar::NextValue); ShortcutDown.pNotify = new FunctionNotifyContext<OptionVar, bool>(this, &OptionVar::PrevValue); } @@ -204,7 +216,7 @@ void OptionVar::NextValue(bool* pFastStep) break; case Type_Int: - *AsInt() = Alg::Min<SInt32>(*AsInt() + StepInt * (fastStep ? 5 : 1), MaxInt); + *AsInt() = Alg::Min<int32_t>(*AsInt() + StepInt * (fastStep ? 5 : 1), MaxInt); break; case Type_Float: @@ -215,6 +227,11 @@ void OptionVar::NextValue(bool* pFastStep) case Type_Bool: *AsBool() = !*AsBool(); break; + + case Type_Trigger: + break; // nothing to do + + default: OVR_ASSERT(false); break; // unhandled } SignalUpdate(); @@ -226,11 +243,11 @@ void OptionVar::PrevValue(bool* pFastStep) switch (Type) { case Type_Enum: - *AsInt() = ((GetEnumIndex() + (UInt32)EnumValues.GetSize() - 1) % EnumValues.GetSize()); + *AsInt() = ((GetEnumIndex() + (uint32_t)EnumValues.GetSize() - 1) % EnumValues.GetSize()); break; case Type_Int: - *AsInt() = Alg::Max<SInt32>(*AsInt() - StepInt * (fastStep ? 5 : 1), MinInt); + *AsInt() = Alg::Max<int32_t>(*AsInt() - StepInt * (fastStep ? 5 : 1), MinInt); break; case Type_Float: @@ -241,6 +258,11 @@ void OptionVar::PrevValue(bool* pFastStep) case Type_Bool: *AsBool() = !*AsBool(); break; + + case Type_Trigger: + break; // nothing to do + + default: OVR_ASSERT(false); break; // unhandled } SignalUpdate(); @@ -248,8 +270,16 @@ void OptionVar::PrevValue(bool* pFastStep) String OptionVar::HandleShortcutUpdate() { - SignalUpdate(); - return Label + " - " + GetValue(); + if(Type != Type_Trigger) + { + SignalUpdate(); + return Label + " - " + GetValue(); + } + else + { + // Avoid double trigger (shortcut key already triggers NextValue()) + return String("Triggered: ") + Label; + } } String OptionVar::ProcessShortcutKey(OVR::KeyCode key, bool shift) @@ -262,7 +292,7 @@ String OptionVar::ProcessShortcutKey(OVR::KeyCode key, bool shift) return String(); } -String OptionVar::ProcessShortcutButton(UInt32 buttonMask) +String OptionVar::ProcessShortcutButton(uint32_t buttonMask) { if (ShortcutUp.MatchGamepadButton(buttonMask) || ShortcutDown.MatchGamepadButton(buttonMask)) { @@ -272,7 +302,7 @@ String OptionVar::ProcessShortcutButton(UInt32 buttonMask) } -OptionVar& OptionVar::AddEnumValue(const char* displayName, SInt32 value) +OptionVar& OptionVar::AddEnumValue(const char* displayName, int32_t value) { EnumEntry entry; entry.Name = displayName; @@ -283,16 +313,19 @@ OptionVar& OptionVar::AddEnumValue(const char* displayName, SInt32 value) String OptionVar::GetValue() { - return fFormat(this); + if(fFormat == NULL) + return String(); + else + return fFormat(this); } -UInt32 OptionVar::GetEnumIndex() +uint32_t OptionVar::GetEnumIndex() { OVR_ASSERT(Type == Type_Enum); OVR_ASSERT(EnumValues.GetSize() > 0); // TODO: Change this from a linear search to binary or a hash. - for (UInt32 i = 0; i < EnumValues.GetSize(); i++) + for (uint32_t i = 0; i < EnumValues.GetSize(); i++) { if (EnumValues[i].Value == *AsInt()) return i; @@ -353,7 +386,7 @@ OptionSelectionMenu::OptionSelectionMenu(OptionSelectionMenu* parentMenu) OptionSelectionMenu::~OptionSelectionMenu() { - for (UInt32 i = 0; i < Items.GetSize(); i++) + for (uint32_t i = 0; i < Items.GetSize(); i++) delete Items[i]; } @@ -400,7 +433,7 @@ bool OptionSelectionMenu::OnKey(OVR::KeyCode key, int chr, bool down, int modifi return false; } -bool OptionSelectionMenu::OnGamepad(UInt32 buttonMask) +bool OptionSelectionMenu::OnGamepad(uint32_t buttonMask) { // Check global shortcuts first. String s = ProcessShortcutButton(buttonMask); @@ -436,7 +469,7 @@ String OptionSelectionMenu::ProcessShortcutKey(OVR::KeyCode key, bool shift) { String s; - for (UPInt i = 0; (i < Items.GetSize()) && s.IsEmpty(); i++) + for (size_t i = 0; (i < Items.GetSize()) && s.IsEmpty(); i++) { s = Items[i]->ProcessShortcutKey(key, shift); } @@ -444,11 +477,11 @@ String OptionSelectionMenu::ProcessShortcutKey(OVR::KeyCode key, bool shift) return s; } -String OptionSelectionMenu::ProcessShortcutButton(UInt32 buttonMask) +String OptionSelectionMenu::ProcessShortcutButton(uint32_t buttonMask) { String s; - for (UPInt i = 0; (i < Items.GetSize()) && s.IsEmpty(); i++) + for (size_t i = 0; (i < Items.GetSize()) && s.IsEmpty(); i++) { s = Items[i]->ProcessShortcutButton(buttonMask); } @@ -457,9 +490,9 @@ String OptionSelectionMenu::ProcessShortcutButton(UInt32 buttonMask) } // Fills in inclusive character range; returns false if line not found. -bool FindLineCharRange(const char* text, int searchLine, UPInt charRange[2]) +bool FindLineCharRange(const char* text, int searchLine, size_t charRange[2]) { - UPInt i = 0; + size_t i = 0; for (int line = 0; line <= searchLine; line ++) { @@ -522,11 +555,9 @@ void OptionSelectionMenu::Render(RenderDevice* prender, String title) float valuesSize[2] = {0.0f, 0.0f}; float maxValueWidth = 0.0f; - UPInt selection[2] = { 0, 0 }; + size_t selection[2] = { 0, 0 }; Vector2f labelSelectionRect[2]; Vector2f valueSelectionRect[2]; - bool havelLabelSelection = false; - bool haveValueSelection = false; float textSize = 22.0f; prender->MeasureText(&DejaVu, " ", textSize, bufferSize); @@ -538,14 +569,14 @@ void OptionSelectionMenu::Render(RenderDevice* prender, String title) if (DisplayState == Display_Menu) { highlightIndex = SelectedIndex; - for (UInt32 i = 0; i < Items.GetSize(); i++) + for (uint32_t i = 0; i < Items.GetSize(); i++) { if (i > 0) values += "\n"; values += Items[i]->GetValue(); } - for (UInt32 i = 0; i < Items.GetSize(); i++) + for (uint32_t i = 0; i < Items.GetSize(); i++) { if (i > 0) menuItems += "\n"; @@ -560,13 +591,15 @@ void OptionSelectionMenu::Render(RenderDevice* prender, String title) // Measure labels const char* menuItemsCStr = menuItems.ToCStr(); - havelLabelSelection = FindLineCharRange(menuItemsCStr, highlightIndex, selection); + bool havelLabelSelection = FindLineCharRange(menuItemsCStr, highlightIndex, selection); + OVR_UNUSED(havelLabelSelection); prender->MeasureText(&DejaVu, menuItemsCStr, textSize, labelsSize, selection, labelSelectionRect); // Measure label-to-value gap const char* valuesCStr = values.ToCStr(); - haveValueSelection = FindLineCharRange(valuesCStr, highlightIndex, selection); + bool haveValueSelection = FindLineCharRange(valuesCStr, highlightIndex, selection); + OVR_UNUSED(haveValueSelection); prender->MeasureText(&DejaVu, valuesCStr, textSize, valuesSize, selection, valueSelectionRect); // Measure max value size (absolute size varies, so just use a reasonable max) @@ -712,7 +745,7 @@ OptionSelectionMenu* OptionSelectionMenu::GetSubmenu() OptionSelectionMenu* OptionSelectionMenu::GetOrCreateSubmenu(String submenuName) { - for (UInt32 i = 0; i < Items.GetSize(); i++) + for (uint32_t i = 0; i < Items.GetSize(); i++) { if (!Items[i]->IsMenu()) continue; diff --git a/Samples/CommonSrc/Util/OptionMenu.h b/Samples/CommonSrc/Util/OptionMenu.h index d52ba5c..5eea9bd 100644 --- a/Samples/CommonSrc/Util/OptionMenu.h +++ b/Samples/CommonSrc/Util/OptionMenu.h @@ -24,7 +24,7 @@ limitations under the License. #ifndef INC_OptionMenu_h #define INC_OptionMenu_h -#include "OVR.h" +#include "OVR_Kernel.h" #include "../Platform/Platform_Default.h" #include "../Render/Render_Device.h" @@ -37,11 +37,11 @@ using namespace OVR::Util::Render; #include <Kernel/OVR_Log.h> #include <Kernel/OVR_Timer.h> -#include "OVR_DeviceConstants.h" +#include "Sensors/OVR_DeviceConstants.h" using namespace OVR; -using namespace OVR::Platform; +using namespace OVR::OvrPlatform; using namespace OVR::Render; @@ -108,7 +108,7 @@ struct ShortcutKey struct OptionShortcut { Array<ShortcutKey> Keys; - Array<UInt32> GamepadButtons; + Array<uint32_t> GamepadButtons; FunctionNotifyBase* pNotify; OptionShortcut() : pNotify(NULL) {} @@ -116,10 +116,10 @@ struct OptionShortcut ~OptionShortcut() { if (pNotify) delete pNotify; } void AddShortcut(ShortcutKey key) { Keys.PushBack(key); } - void AddShortcut(UInt32 gamepadButton) { GamepadButtons.PushBack(gamepadButton); } + void AddShortcut(uint32_t gamepadButton) { GamepadButtons.PushBack(gamepadButton); } bool MatchKey(OVR::KeyCode key, bool shift) const; - bool MatchGamepadButton(UInt32 gamepadButtonMask) const; + bool MatchGamepadButton(uint32_t gamepadButtonMask) const; }; @@ -142,7 +142,7 @@ public: // Returns empty string if shortcut not handled virtual String ProcessShortcutKey(OVR::KeyCode key, bool shift) = 0; - virtual String ProcessShortcutButton(UInt32 buttonMask) = 0; + virtual String ProcessShortcutButton(uint32_t buttonMask) = 0; virtual bool IsMenu() const { return false; } @@ -165,7 +165,8 @@ public: Type_Enum, Type_Int, Type_Float, - Type_Bool + Type_Bool, + Type_Trigger, }; typedef String (*FormatFunction)(OptionVar*); @@ -176,14 +177,15 @@ public: static String FormatFloat(OptionVar* var); static String FormatTan(OptionVar* var); static String FormatBool(OptionVar* var); + static String FormatTrigger(OptionVar* var); OptionVar(const char* name, void* pVar, VarType type, FormatFunction formatFunction, UpdateFunction updateFunction = NULL); // Integer with range and step size. - OptionVar(const char* name, SInt32* pVar, - SInt32 min, SInt32 max, SInt32 stepSize=1, + OptionVar(const char* name, int32_t* pVar, + int32_t min, int32_t max, int32_t stepSize=1, const char* formatString = "%d", FormatFunction formatFunction = 0, // Default int formatting. UpdateFunction updateFunction = NULL); @@ -197,10 +199,12 @@ public: virtual ~OptionVar(); - SInt32* AsInt() { return reinterpret_cast<SInt32*>(pVar); } - bool* AsBool() { return reinterpret_cast<bool*>(pVar); } - float* AsFloat() { return reinterpret_cast<float*>(pVar); } - VarType GetType() { return Type; } + int32_t* AsInt() { return reinterpret_cast<int32_t*>(pVar); } + bool* AsBool() { return reinterpret_cast<bool*>(pVar); } + float* AsFloat() { return reinterpret_cast<float*>(pVar); } + VarType GetType() { return Type; } + + //virtual void Select() { if(Type == Type_Trigger) SignalUpdate(); } // Step through values (wrap for enums). virtual void NextValue(bool* pFastStep); @@ -210,9 +214,9 @@ public: // Returns empty string for no action. String HandleShortcutUpdate(); virtual String ProcessShortcutKey(OVR::KeyCode key, bool shift); - virtual String ProcessShortcutButton(UInt32 buttonMask); + virtual String ProcessShortcutButton(uint32_t buttonMask); - OptionVar& AddEnumValue(const char* displayName, SInt32 value); + OptionVar& AddEnumValue(const char* displayName, int32_t value); template<class C> OptionVar& SetNotify(C* p, void (C::*fn)(OptionVar*)) @@ -231,7 +235,7 @@ public: OptionVar& AddShortcutUpKey(OVR::KeyCode key, ShortcutKey::ShiftUsageType shiftUsage = ShortcutKey::Shift_Modify) { ShortcutUp.AddShortcut(ShortcutKey(key, shiftUsage)); return *this; } - OptionVar& AddShortcutUpButton(UInt32 gamepadButton) + OptionVar& AddShortcutUpButton(uint32_t gamepadButton) { ShortcutUp.AddShortcut(gamepadButton); return *this; } OptionVar& AddShortcutDownKey(const ShortcutKey& shortcut) @@ -239,7 +243,7 @@ public: OptionVar& AddShortcutDownKey(OVR::KeyCode key, ShortcutKey::ShiftUsageType shiftUsage = ShortcutKey::Shift_Modify) { ShortcutDown.AddShortcut(ShortcutKey(key, shiftUsage)); return *this; } - OptionVar& AddShortcutDownButton(UInt32 gamepadButton) + OptionVar& AddShortcutDownButton(uint32_t gamepadButton) { ShortcutDown.AddShortcut(gamepadButton); return *this; } OptionVar& AddShortcutKey(const ShortcutKey& shortcut) @@ -247,7 +251,7 @@ public: OptionVar& AddShortcutKey(OVR::KeyCode key, ShortcutKey::ShiftUsageType shiftUsage = ShortcutKey::Shift_RequireOff) { return AddShortcutUpKey(key, shiftUsage); } - OptionVar& AddShortcutButton(UInt32 gamepadButton) + OptionVar& AddShortcutButton(uint32_t gamepadButton) { return AddShortcutUpButton(gamepadButton); } private: @@ -262,13 +266,13 @@ private: { // Human readable name for enum. String Name; - SInt32 Value; + int32_t Value; }; // Array of possible enum values. Array<EnumEntry> EnumValues; // Gets the index of the current enum value. - UInt32 GetEnumIndex(); + uint32_t GetEnumIndex(); FormatFunction fFormat; UpdateFunction fUpdate; @@ -286,9 +290,11 @@ private: float StepFloat; float FormatScale; // Multiply float by this before rendering - SInt32 MinInt; - SInt32 MaxInt; - SInt32 StepInt; + int32_t MinInt; + int32_t MaxInt; + int32_t StepInt; + + int SelectedIndex; }; @@ -312,7 +318,7 @@ public: bool OnKey(OVR::KeyCode key, int chr, bool down, int modifiers); - bool OnGamepad(UInt32 buttonMask); + bool OnGamepad(uint32_t buttonMask); void Render(RenderDevice* prender, String title = ""); @@ -353,12 +359,20 @@ public: AddItem(p); return *p; } + + OptionVar& AddTrigger( const char* name, OptionVar::UpdateFunction updateFunction = 0 ) + { + OptionVar* p = new OptionVar(name, NULL, OptionVar::Type_Trigger, + NULL, updateFunction); + AddItem(p); + return *p; + } virtual void Select(); virtual String GetLabel() { return Label + " >"; } virtual String ProcessShortcutKey(OVR::KeyCode key, bool shift); - virtual String ProcessShortcutButton(UInt32 buttonMask); + virtual String ProcessShortcutButton(uint32_t buttonMask); // Sets a message to display with a time-out. Default time-out is 4 seconds. // This uses the same overlay approach as used for shortcut notifications. diff --git a/Samples/CommonSrc/Util/RenderProfiler.h b/Samples/CommonSrc/Util/RenderProfiler.h index 96ec50a..7494034 100644 --- a/Samples/CommonSrc/Util/RenderProfiler.h +++ b/Samples/CommonSrc/Util/RenderProfiler.h @@ -24,7 +24,7 @@ limitations under the License. #ifndef INC_RenderProfiler_h #define INC_RenderProfiler_h -#include "OVR.h" +#include "OVR_Kernel.h" // TODO: Refactor option menu so dependencies are in a separate file. #include "OptionMenu.h" |