summary refs log tree commit diff stats
path: root/src/menu.cpp
blob: 7e58de0326b9e5d936f34272bc5e55577f6342db (plain) (blame)
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
#include "menu.h"
#include "game.h"
#include "renderer.h"

Menu::Menu() {
  items.push_back({.text = "New Game", .activationFunction = [this] (Game& game) {
    close();
    game.losing = LoseState::PoppingLamps;
    game.muxer.stopMusic();
  }});
  items.push_back({.text = "Toggle Fullscreen", .activationFunction = [] (Game& game) {
    game.renderer.toggleFullscreen();
  }});
  items.push_back({.text = "Toggle Music", .activationFunction = [] (Game& game) {
    game.muxer.toggleMute();
  }});
  items.push_back({.text = "Quit", .activationFunction = [this] (Game& game) {
    close();
    game.losing = LoseState::PoppingLamps;
    game.quitting = true;
    game.muxer.stopMusic();
  }});
}

void Menu::open(Game& game) {
  menuState = MenuState::Opening;
  menuDisplayProgress.start(200);
  game.muxer.playSound("openmenu");
}

void Menu::close() {
  menuState = MenuState::Closing;
  menuDisplayProgress.start(200);
}

void Menu::update(size_t dt, Game& game) {
  if (menuState != MenuState::Open) {
    SDL_Event e;

    while (SDL_PollEvent(&e)) {
      if (e.type == SDL_QUIT) {
        game.quit = true;
      }
    }
  }

  switch (menuState) {
    case MenuState::Closed: {
      // Shouldn't happen.
      break;
    }
    case MenuState::Opening: {
      menuDisplayProgress.tick(dt);
      if (menuDisplayProgress.isComplete()) {
        menuState = MenuState::Open;
      }

      break;
    }
    case MenuState::Closing: {
      menuDisplayProgress.tick(dt);
      if (menuDisplayProgress.isComplete()) {
        menuState = MenuState::Closed;
      }

      break;
    }
    case MenuState::Open: {
      SDL_Event e;

      while (SDL_PollEvent(&e)) {
        if (e.type == SDL_QUIT) {
          game.quit = true;
        } else if (e.type == SDL_KEYDOWN) {
          switch (e.key.keysym.sym)
          {
            case SDLK_ESCAPE: {
              close();
              game.muxer.playSound("closemenu");
              break;
            }
            case SDLK_SPACE:
            case SDLK_RETURN: {
              items[cursor].activationFunction(game);
              game.muxer.playSound("menuselect");
              break;
            }
            case SDLK_UP:
            case SDLK_w: {
              cursor--;
              if (cursor < 0) {
                cursor = items.size() - 1;
              }
              game.muxer.playSound("menucursor");
              break;
            }
            case SDLK_DOWN:
            case SDLK_s: {
              cursor++;
              if (cursor >= items.size()) {
                cursor = 0;
              }
              game.muxer.playSound("menucursor");
              break;
            }
          }
        }
      }

      break;
    }
  }
}