File: camenu.cpp

package info (click to toggle)
trophy 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 48,568 kB
  • sloc: cpp: 7,285; sh: 1,032; xml: 277; makefile: 94
file content (219 lines) | stat: -rw-r--r-- 5,605 bytes parent folder | download | duplicates (4)
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
#include "camenu.h"

#include "caresources.h"
#include "catrophy.h"
#include "camenulabel.h"
#include "camenuinput.h"
#include "camenuselect.h"

/** Constructor.
*/
CAMenu::CAMenu( const std::string title )
        : CAScreen() {
    this->title = title;

    cursor = 0;
    done = false;
    cancel = false;
    changed = false;

    calcMenuDimensions();
}

/** Destructor.
*/
CAMenu::~CAMenu() {}

/** Adds a simple menu label to the menu.
*/
void
CAMenu::addMenuLabel( const std::string& label )
{
    if( item.size()<CA_MAXMENUITEMS )
        item.push_back( new CAMenuLabel( this, item.size(), label ));
    calcMenuDimensions();
}

/** Adds a menu input to the menu.
    Menu inputs let the user input any value (string).
    \param label The menu item label.
    \param defaultValue Value shown at startup of the menu.
*/
void
CAMenu::addMenuInput( const std::string& label, std::string result, int maxLength )
{
    if( item.size()<CA_MAXMENUITEMS )
        item.push_back(new CAMenuInput( this, item.size(), label, result, maxLength ));
    calcMenuDimensions();
}

/** Adds a menu input to the menu.
    Menu inputs let the user input any value (int).
    \param label The menu item label.
    \param defaultValue Value shown at startup of the menu.
*/
void
CAMenu::addMenuInput( const std::string& label, int* result, int maxLength )
{
    if( item.size()<CA_MAXMENUITEMS )
        item.push_back(new CAMenuInput( this, item.size(), label, result, maxLength ));
    calcMenuDimensions();
}

/** Adds a menu select to the menu.
    Menu selects let the user choose a value from a given list (string).
    \param label The menu item label.
    \param valueList Possible values to choose from.
    \param result Pointer to an int value which stores the selection result.
                  (0=first value selected)
*/
template<typename T>
void CAMenu::addMenuSelect( const std::string& label, const std::string valueList, T* result )
{
    if( item.size()<CA_MAXMENUITEMS )
        item.push_back(new CAMenuSelect<T>( this, item.size(), label, valueList, result ));
    calcMenuDimensions();
}


/** Runs the menu.
*/
int CAMenu::run() {
    //slot = CL_Input::sig_button_press.connect(this, &CAMenu::on_button_press);
    // TODO : Connect the right signal
    slot = CL_Keyboard::sig_key_up().connect(this, &CAMenu::on_key_pressed);

    //CA_APP->fadeScreen( true, this );
    done = false;
    cancel = false;

    // Menu loop:
    //
    while( !done ) {
        CA_APP->measureFrameTime( true );

        buildScreen();

        // Play background sound:
        CASoundEffect::playBackgroundMelody();

        if( configureMenu && changed ) {
            CA_APP->reconfigure();
        }

        changed = false;

        CL_Display::flip();   // Copy framebufer to screen
        CL_System::keep_alive();      // VERY VITAL for the system!

        CA_APP->measureFrameTime( false );
    }

    //CA_APP->fadeScreen( false, this );
    CA_APP->waitForSilence();

    CL_Keyboard::sig_key_up().disconnect(slot);
    return (cancel ? -1 : cursor);
}

/** Builds the menu screen.
*/
void
CAMenu::buildScreen() {
    // Menu Backgroud:
    //
    CA_RES->menu_bg->draw ( CL_Rect (0,0, CA_APP->width,CA_APP->height) );

    // Menu Title:
    //
    //CA_RES->menu_bar->put_screen( left,top );
    //CA_RES->menu_bar->put_screen( left,top+height+12 );
    //CA_RES->menu_itemoff->put_screen( left,top );

    CA_RES->font_normal_14_white->set_alignment(origin_top_center, 0, 0);
    CA_RES->font_normal_14_white->draw ((left + right)/2, top + CA_MENUSPACE/2 -10, title);

    for( int n=0; n<int(item.size()); ++n ) {
        if( item[n]->rtti()!=CA_MI_VIRTUAL ) {
            item[n]->display( n==cursor );
        }
    }
}



/** Calculates the max menu text dimesnions.
*/
void
CAMenu::calcMenuDimensions() {
    //itemHeight = CA_RES->menu_itemon->get_height();
    itemHeight = CA_RES->font_normal_22_white->get_height() + 6;
    //headerHeight = CA_RES->menu_itemon->get_height();
    headerHeight = 22;
    height = itemHeight * item.size() + headerHeight;
    width = CA_MENUWIDTH;

    left = (CA_APP->width - width)/2;
    right = CA_APP->width - left;
    top = (CA_APP->height - height)/2 + 30;
    bottom = CA_APP->height - top;
}

/** Called on key release.
*/
void
CAMenu::on_key_pressed (const CL_InputEvent &key) 
{
    switch( key.id ) {

        // Cancel (ESC):
        //
    case CL_KEY_ESCAPE:
        cancel = true;
        done = true;
        break;

        // Up:
        //
    case CL_KEY_UP:
    case CL_KEY_E:
        if( cursor>0 ) {
            cursor--;
            if( CA_APP->sound ) CA_RES->effectMenu->play( 2 );
        }

        break;

        // Down:
        //
    case CL_KEY_DOWN:
    case CL_KEY_D:
        if( cursor<int(item.size()-1) ) {
            cursor++;
            if( CA_APP->sound ) CA_RES->effectMenu->play( 2 );
        }

        break;

        // Activate:
        //
    case CL_KEY_ENTER:
    case CL_KEY_SPACE:
        if( item[cursor]->rtti()==CA_MI_MENULABEL ) {
            done = true;
        }
        break;

    default:
        break;
    }

    // Maybe the current item is also interested in our event:
    //
    item[cursor]->handleKey( key );
}

template void CAMenu::addMenuSelect<int>( const std::string& label, const std::string valueList, int* result );
template void CAMenu::addMenuSelect<bool>( const std::string& label, const std::string valueList, bool* result );
template void CAMenu::addMenuSelect<std::string>( const std::string& label, const std::string valueList, std::string* result );
// EOF