aboutsummaryrefslogtreecommitdiffstats
path: root/Samples/CommonSrc/Util/OptionMenu.h
blob: 0c3c289c995d36f1318aa5e4ce43fa89be0535b7 (plain)
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
/************************************************************************************

Filename    :   OptionMenu.h
Content     :   Option selection and editing for OculusWorldDemo
Created     :   March 7, 2014
Authors     :   Michael Antonov, Caleb Leak

Copyright   :   Copyright 2012 Oculus VR, LLC All Rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*************************************************************************************/

#ifndef INC_OptionMenu_h
#define INC_OptionMenu_h

#include "OVR_Kernel.h"

#include "../Platform/Platform_Default.h"
#include "../Render/Render_Device.h"
#include "../Platform/Gamepad.h"

#include "Util/Util_Render_Stereo.h"
using namespace OVR::Util::Render;

#include <Kernel/OVR_SysFile.h>
#include <Kernel/OVR_Log.h>
#include <Kernel/OVR_Timer.h>

#include "Sensors/OVR_DeviceConstants.h"


using namespace OVR;
using namespace OVR::OvrPlatform;
using namespace OVR::Render;


//-------------------------------------------------------------------------------------
struct FunctionNotifyBase : public NewOverrideBase
{
    virtual void CallNotify(void*) { }
    virtual void CallNotify() { }
};

// Simple member pointer wrapper to support calling class members
template<class C, class X>
struct FunctionNotifyContext : public FunctionNotifyBase
{
    typedef void (C::*FnPtr)(X*);

    FunctionNotifyContext(C* p, FnPtr fn) : pClass(p), pFn(fn), pContext(NULL) { }
    FunctionNotifyContext(C* p, FnPtr fn, X* pContext) : pClass(p), pFn(fn), pContext(pContext) { }
    virtual void CallNotify(void* var)  { (void)(pClass->*pFn)(static_cast<X*>(var)); }
    virtual void CallNotify()  { (void)(pClass->*pFn)(pContext); }
private:

    X*      pContext;
    C*      pClass;
    FnPtr   pFn;
};

template<class C>
struct FunctionNotifySimple : public FunctionNotifyBase
{
    typedef void (C::*FnPtr)(void);

    FunctionNotifySimple(C* p, FnPtr fn) : pClass(p), pFn(fn) { }
    virtual void CallNotify(void*)  { CallNotify(); }
    virtual void CallNotify()  { (void)(pClass->*pFn)(); }
private:

    C*      pClass;
    FnPtr   pFn;
};


//-------------------------------------------------------------------------------------
// Describes a shortcut key 
struct ShortcutKey
{
    enum ShiftUsageType
    {
        Shift_Ignore,
        Shift_Modify,
        Shift_RequireOn,
        Shift_RequireOff
    };

    ShortcutKey(OVR::KeyCode key = Key_None, ShiftUsageType shiftUsage = Shift_RequireOff)
        : Key(key), ShiftUsage(shiftUsage) { }

    OVR::KeyCode        Key;
    ShiftUsageType ShiftUsage;
};


//-------------------------------------------------------------------------------------
struct OptionShortcut
{
    Array<ShortcutKey>  Keys;
    Array<uint32_t>       GamepadButtons;
    FunctionNotifyBase* pNotify;

    OptionShortcut() : pNotify(NULL) {}
    OptionShortcut(FunctionNotifyBase* pNotify) : pNotify(pNotify) {}
    ~OptionShortcut()  { if (pNotify) delete pNotify; }

    void AddShortcut(ShortcutKey key) { Keys.PushBack(key); }
    void AddShortcut(uint32_t gamepadButton) { GamepadButtons.PushBack(gamepadButton); }

    bool MatchKey(OVR::KeyCode key, bool shift) const;
    bool MatchGamepadButton(uint32_t gamepadButtonMask) const;
};


//-------------------------------------------------------------------------------------

// Base class for a menu item. Internally, this can also be OptionSelectionMenu itself
// to support nested menus. Users shouldn't need to use this class.
class OptionMenuItem : public NewOverrideBase
{
public:
    virtual ~OptionMenuItem() { }

    virtual void    Select() { }
        
    virtual void    NextValue(bool* pFastStep = NULL) { OVR_UNUSED1(pFastStep); }
    virtual void    PrevValue(bool* pFastStep = NULL) { OVR_UNUSED1(pFastStep); }

    virtual String  GetLabel() { return Label; }
    virtual String  GetValue() { return ""; }

    // Returns empty string if shortcut not handled
    virtual String  ProcessShortcutKey(OVR::KeyCode key, bool shift) = 0;
    virtual String  ProcessShortcutButton(uint32_t buttonMask) = 0;

    virtual bool    IsMenu() const { return false; }

protected:
    String          Label;
    String          PopNamespaceFrom(OptionMenuItem* menuItem);
};


//-------------------------------------------------------------------------------------

// OptionVar implements a basic menu item, which binds to an external variable,
// displaying and editing its state.
class OptionVar : public OptionMenuItem
{
public:

    enum VarType
    {
        Type_Enum,
        Type_Int,
        Type_Float,
        Type_Bool,
        Type_Trigger,
    };

    typedef String (*FormatFunction)(OptionVar*);
    typedef void (*UpdateFunction)(OptionVar*);
    
    static String FormatEnum(OptionVar* var);
    static String FormatInt(OptionVar* var);
    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, 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);

    // Float with range and step size.
    OptionVar(const char* name, float* pvar, 
              float minf, float maxf, float stepSize = 1.0f,
              const char* formatString = "%.3f", float formatScale = 1.0f,
              FormatFunction formatFunction = 0, // Default float formatting.
              UpdateFunction updateFunction = 0 );

    virtual ~OptionVar();

    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);
    virtual void PrevValue(bool* pFastStep);

    // Executes shortcut message and returns notification string.
    // Returns empty string for no action.
    String         HandleShortcutUpdate();
    virtual String ProcessShortcutKey(OVR::KeyCode key, bool shift);
    virtual String ProcessShortcutButton(uint32_t buttonMask);

    OptionVar& AddEnumValue(const char* displayName, int32_t value);

    template<class C>
    OptionVar& SetNotify(C* p, void (C::*fn)(OptionVar*))
    {
        OVR_ASSERT(pNotify == 0); // Can't set notifier twice.
        pNotify = new FunctionNotifyContext<C, OptionVar>(p, fn, this);
        return *this;
    }   


    //String Format();
    virtual String GetValue();

    OptionVar& AddShortcutUpKey(const ShortcutKey& shortcut)
    { ShortcutUp.AddShortcut(shortcut); return *this; }
    OptionVar& AddShortcutUpKey(OVR::KeyCode key,
                                ShortcutKey::ShiftUsageType shiftUsage = ShortcutKey::Shift_Modify)
    { ShortcutUp.AddShortcut(ShortcutKey(key, shiftUsage)); return *this; }
    OptionVar& AddShortcutUpButton(uint32_t gamepadButton)
    { ShortcutUp.AddShortcut(gamepadButton); return *this; }

    OptionVar& AddShortcutDownKey(const ShortcutKey& shortcut)
    { ShortcutDown.AddShortcut(shortcut); return *this; }
    OptionVar& AddShortcutDownKey(OVR::KeyCode key,
                                  ShortcutKey::ShiftUsageType shiftUsage = ShortcutKey::Shift_Modify)
    { ShortcutDown.AddShortcut(ShortcutKey(key, shiftUsage)); return *this; }
    OptionVar& AddShortcutDownButton(uint32_t gamepadButton)
    { ShortcutDown.AddShortcut(gamepadButton); return *this; }
    
    OptionVar& AddShortcutKey(const ShortcutKey& shortcut)
    { return AddShortcutUpKey(shortcut); }
    OptionVar& AddShortcutKey(OVR::KeyCode key,
                               ShortcutKey::ShiftUsageType shiftUsage = ShortcutKey::Shift_RequireOff)
    { return AddShortcutUpKey(key, shiftUsage); }
    OptionVar& AddShortcutButton(uint32_t gamepadButton)
    { return AddShortcutUpButton(gamepadButton); }

private:

    void SignalUpdate()
    {
        if (fUpdate) fUpdate(this);
        if (pNotify) pNotify->CallNotify(this);
    }

    struct EnumEntry
    {
        // Human readable name for enum.
        String Name;
        int32_t Value;
    };    

    // Array of possible enum values.
    Array<EnumEntry>    EnumValues;
    // Gets the index of the current enum value.
    uint32_t            GetEnumIndex();

    FormatFunction      fFormat;
    UpdateFunction      fUpdate;
    FunctionNotifyBase* pNotify; 

    VarType         Type;
    void*           pVar;
    const char*     FormatString;
    
    OptionShortcut  ShortcutUp;
    OptionShortcut  ShortcutDown;

    float       MinFloat;
    float       MaxFloat;
    float       StepFloat;
    float       FormatScale; // Multiply float by this before rendering

    int32_t     MinInt;
    int32_t     MaxInt;
    int32_t     StepInt;

    int         SelectedIndex;
};


//-------------------------------------------------------------------------------------
// ***** OptionSelectionMenu

// Implements an overlay option menu, brought up by the 'Tab' key.
// Items are added to the menu with AddBool, AddEnum, AddFloat on startup,
// and are editable by using arrow keys (underlying variable is modified).
// 
// Call Render() to render the menu every frame.
//
// Menu also support displaying popup messages with a timeout, displayed
// when menu body isn't up.

class OptionSelectionMenu : public OptionMenuItem
{
public:
    OptionSelectionMenu(OptionSelectionMenu* parentMenu = NULL);
    ~OptionSelectionMenu();


    bool OnKey(OVR::KeyCode key, int chr, bool down, int modifiers);
    bool OnGamepad(uint32_t buttonMask);

    void Render(RenderDevice* prender, String title = "");
    
    void AddItem(OptionMenuItem* menuItem);

    // Adds a boolean toggle. Returns added item to allow customization.
    OptionVar& AddBool(const char* name, bool* pvar,
                       OptionVar::UpdateFunction updateFunction = 0,
                       OptionVar::FormatFunction formatFunction = OptionVar::FormatBool)
    {
        OptionVar* p = new OptionVar(name, pvar, OptionVar::Type_Bool,
                                     formatFunction, updateFunction);
        AddItem(p);
        return *p;
    }

    // Adds a boolean toggle. Returns added item to allow customization.
    OptionVar& AddEnum(const char* name, void* pvar,
                       OptionVar::UpdateFunction updateFunction = 0)
    {
        OptionVar* p = new OptionVar(name, pvar, OptionVar::Type_Enum,
                                     OptionVar::FormatEnum, updateFunction);
        AddItem(p);
        return *p;
    }


    // Adds a Float variable. Returns added item to allow customization.
    OptionVar& AddFloat( const char* name, float* pvar, 
                         float minf, float maxf, float stepSize = 1.0f,
                         const char* formatString = "%.3f", float formatScale = 1.0f,
                         OptionVar::FormatFunction formatFunction = 0, // Default float formatting.
                         OptionVar::UpdateFunction updateFunction = 0 )
    {
        OptionVar* p = new OptionVar(name, pvar, minf, maxf, stepSize,
                                     formatString, formatScale,
                                     formatFunction, updateFunction);
        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_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.
    void            SetPopupMessage(const char* format, ...);
    // Overrides current timeout, in seconds (not the future default value);
    // intended to be called right after SetPopupMessage.
    void            SetPopupTimeout(double timeoutSeconds, bool border = false);

    virtual bool    IsMenu() const { return true; }

protected:

    void renderShortcutChangeMessage(RenderDevice* prender);

public:
    OptionSelectionMenu* GetSubmenu();
    OptionSelectionMenu* GetOrCreateSubmenu(String submenuName);

    enum DisplayStateType
    {
        Display_None,
        Display_Menu,
        Display_SingleItem
    };

    DisplayStateType          DisplayState;
    OptionSelectionMenu*      ParentMenu;
   
    ArrayPOD<OptionMenuItem*> Items;
    int                       SelectedIndex;
    bool                      SelectionActive;

    String                    PopupMessage;
    double                    PopupMessageTimeout;
    bool                      PopupMessageBorder;

    // Possible menu navigation actions.
    enum NavigationActions
    {
        Nav_Up,
        Nav_Down,
        Nav_Left,
        Nav_Right,
        Nav_Select,
        Nav_Back,
        Nav_LAST
    };

    // Handlers for navigation actions.
    void HandleUp(bool* pFast);
    void HandleDown(bool* pFast);
    void HandleLeft();
    void HandleRight();
    void HandleSelect();
    void HandleBack();

    void HandleMenuToggle();
    void HandleSingleItemToggle();

    OptionShortcut NavShortcuts[Nav_LAST];
    OptionShortcut ToggleShortcut;
    OptionShortcut ToggleSingleItemShortcut;
};


//-------------------------------------------------------------------------------------
// Text Rendering Utility
enum DrawTextCenterType
{
    DrawText_NoCenter= 0,
    DrawText_VCenter = 0x01,
    DrawText_HCenter = 0x02,
    DrawText_Center  = DrawText_VCenter | DrawText_HCenter,
    DrawText_Border  = 0x10,
};

void    DrawTextBox(RenderDevice* prender, float x, float y,
                    float textSize, const char* text,
                    unsigned centerType = DrawText_NoCenter);

void    CleanupDrawTextFont();


#endif // INC_OptionMenu_h