File: Menu.cpp

package info (click to toggle)
gav 0.8.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 588 kB
  • ctags: 706
  • sloc: cpp: 3,602; makefile: 152; sh: 10
file content (84 lines) | stat: -rw-r--r-- 2,450 bytes parent folder | download
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
/* -*- C++ -*- */
/*
  GAV - Gpl Arcade Volleyball
  
  Copyright (C) 2002
  GAV team (http://sourceforge.net/projects/gav/)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "Menu.h"
#include <string.h>
#include "SoundMgr.h"

using namespace std;

void Menu::add(MenuItem * mi) {
  if (currentItem < 0)
    currentItem = 0;
  if ((mi->getLabel()).length() > maxLabelLength)
    maxLabelLength = (mi->getLabel()).length();
  items.push_back(mi);
}

int Menu::execute(InputState *is, std::stack<Menu *> &s) {
  SDL_Rect rect;
  const char * label;
  ScreenFont * font;
  static bool spacePressed = false;

  if ( is->getKeyState()[SDLK_UP] && (currentItem > 0) ) {
    currentItem--;
#ifdef AUDIO
    soundMgr->playSound(SND_MENU_SELECT);
#endif // AUDIO
  }
  if ( is->getKeyState()[SDLK_DOWN] &&
       (currentItem < (int) (items.size() - 1)) ) {
    currentItem++;
#ifdef AUDIO
    soundMgr->playSound(SND_MENU_SELECT);
#endif // AUDIO
  }
  /* draw menu items labels */
  rect.y = 30;
  for ( unsigned int it = 0; it < items.size(); it++ ) {
    label = items[it]->getLabel().c_str();
    rect.x = (screen->w / 2) - strlen(label)*(cga->charWidth())/2;
    if (it == (unsigned int) currentItem)
      font = cgaInv;
    else
      font = cga;
    font->printXY(screen, &rect, items[it]->getLabel().c_str());
    rect.y += font->charHeight();
  }

  /* call the execute method of the current item, if an event occurred */
  if ( (is->getKeyState()[SDLK_SPACE]) ||
       (is->getKeyState()[SDLK_RETURN]) ) {
    spacePressed = true;
  }
  if ( spacePressed && !(is->getKeyState()[SDLK_SPACE]) &&
       !(is->getKeyState()[SDLK_RETURN]) ) {
    spacePressed = false;
#ifdef AUDIO
    soundMgr->playSound(SND_MENU_ACTIVATE);
#endif // AUDIO
    return items[currentItem]->execute(s);
  }
  return 0;
}