ELEC2645 (2017/18) / Mbed OS el16ajm

Menu/Menu.cpp

Committer:
Andrew_M
Date:
2018-05-06
Revision:
10:279d3775d52c
Parent:
9:fe86ddbf7799
Child:
11:b25874e7efe4

File content as of revision 10:279d3775d52c:

#include "Menu.h"

// nothing doing in the constructor and destructor
Menu::Menu()
{

}

Menu::~Menu()
{

}

void Menu::init()
{
    _start = false;

    _mainSelection = 1;

    _difficulty = 2;

    _difSelection = _difficulty;

    _level = 1;

    _lvlSelection = _level;

    _buttonPressed = false;

    _menuScreen = "main";
}

void Menu::update()
{
    //moves arrow around menus
    if (_menuScreen == "main" ) {
        if (_d == N && _mainSelection > 1) { //checks the menu arrow location
            _mainSelection -= 1;
        } else if (_d == S && _mainSelection < 3) {
            _mainSelection += 1;
        }
    } else if (_menuScreen == "dif" ) {
        if (_d == N && _difSelection > 1) {
            _difSelection -= 1;
        } else if (_d == S && _difSelection < 3) {
            _difSelection += 1;
        }
    } else if (_menuScreen == "lvl" ) {
        if (_d == N && _lvlSelection > 1) {
            _lvlSelection -= 1;
        } else if (_d == S && _lvlSelection < 3) {
            _lvlSelection += 1;
        }
    }

    //selects item in menu
    if (_buttonPressed) {
        if (_menuScreen == "main" ) {
            if (_mainSelection == 1) {
                _start = true;
            } else if (_mainSelection == 2) {
                _menuScreen = "lvl";
            } else if (_mainSelection == 3) {
                _menuScreen = "dif";
            }
        } else {
            if (_menuScreen == "dif" ) {
                if (_difSelection == 1) {
                    _difficulty = 1;
                } else if (_difSelection == 2) {
                    _difficulty = 2;
                } else if (_difSelection == 3) {
                    _difficulty = 3;
                }
            } else if (_menuScreen == "lvl" ) {
                if (_lvlSelection == 1) {
                    _level = 1;
                } else if (_lvlSelection == 2) {
                    _level = 2;
                } else if (_lvlSelection == 3) {
                    _level = 3;
                }
            }

            _menuScreen = "main";

        }

    }
}

void Menu::read_input(Gamepad &pad)
{
    _buttonPressed = false;

    if ( pad.check_event(Gamepad::START_PRESSED)) {
        _buttonPressed = true;
        wait(0.1);
    }
    _d = pad.get_direction();   

}

void Menu::draw(N5110 &lcd)
{
    if (_menuScreen == "main" ) {

        lcd.printString("  Start",0,1);
        lcd.printString("  Level Select",0,2);
        lcd.printString("  Difficulty",0,3);

        lcd.printString(" >",0,_mainSelection);

    } else if ( _menuScreen == "dif") {

        lcd.printString("  Easy",0,1);
        lcd.printString("  Normal",0,2);
        lcd.printString("  Hard",0,3);

        lcd.printString(" >",0,_difSelection);

    }   else if ( _menuScreen == "lvl") {

        lcd.printString("  1",0,1);
        lcd.printString("  2",0,2);
        lcd.printString("  3",0,3);

        lcd.printString(" >",0,_lvlSelection);
    }
}

bool Menu::started()
{
    return _start;
}

int Menu::getDif()
{
    return _difficulty;
}