aboutsummaryrefslogtreecommitdiffstats
path: root/src/pausescreen.cpp
blob: 399bf544ba23523992e71604cde82ad283e2ffac (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
#include "pausescreen.h"
#include "game.h"

#include "boxii/boxii.h"
#include "boxii/input.h"

static const int selectionCount = 3;
static const std::string selectionText[selectionCount] =
{
    "Resume  ",
    "Settings",
    "Abort   "
};

static void
printSelections(std::shared_ptr<Boxii::TextWindow> menuWindow, int selection)
{
    int optx = 0;
    int opty = 0;

    for(int i = 0; i < selectionCount; i++){
        menuWindow->setfg(0xFFFFFF);
        menuWindow->setbg(0x0);
        if(selection == i){
            menuWindow->setfg(0x0);
            menuWindow->setbg(0xFFFFFF);
        }
        int dw = menuWindow->getBodyWidth() / 2;
        dw -= selectionText[i].length() / 2;

        menuWindow->moveCursor(dw, opty++);
        menuWindow->print("%s", selectionText[i].c_str());
    }
}

PauseScreen::PauseScreen() :
    m_menuWindow(nullptr),
    m_selection(0)
{
    int sw = Boxii::getTerminalWidth();
    int sh = Boxii::getTerminalHeight();

    int menuWidth = 15;
    int menuHeight = 10;
    int menuX = (sw / 2) - (menuWidth / 2);
    int menuY = (sh / 2) - (menuHeight / 2);

    m_menuWindow = Boxii::makeWindow<Boxii::TextWindow>(menuX, menuY / 2, menuWidth, menuHeight);
    m_menuWindow->border('+', '+', '+', '+', '-', '|', 0xFFFFFF, 0x0);
    Boxii::remove(m_menuWindow);

    printSelections(m_menuWindow, m_selection);
}

PauseScreen::~PauseScreen()
{

}

void
PauseScreen::activate()
{
    Boxii::pushToFront(m_menuWindow);
}

void
PauseScreen::deactivate()
{
    Boxii::remove(m_menuWindow);
}

void
PauseScreen::update()
{
    int c = Boxii::Input::getch(true);
    if(c == Boxii::KEY_ESCAPE){
        Game::popScreen();
        return;
    }

    if(c == Boxii::KEY_UP){
        m_selection--;
        if(m_selection < 0) m_selection = 0;
    }

    if(c == Boxii::KEY_DOWN){
        m_selection++;
        if(m_selection >= selectionCount) m_selection = selectionCount - 1;
    }
    
    if(c == Boxii::KEY_RETURN){
        parseSelection();
    }

    printSelections(m_menuWindow, m_selection);
}

void
PauseScreen::parseSelection()
{
    if(m_selection == 0) {
        Game::popScreen();
        return;
    }
    if(m_selection == 2){
        Game::stop();
    }
}