ELEC2645 (2017/18) / Mbed OS el16ajm

Menu/Menu.cpp

Committer:
Andrew_M
Date:
2018-05-07
Revision:
13:81573be8fac6
Parent:
12:d3eef5ea3f43
Child:
14:a57a40ff9430

File content as of revision 13:81573be8fac6:

#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 < 4) {
            _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 (_difSelection == 4) {
                    _difficulty = 4;
                }
            } 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) || pad.check_event(Gamepad::A_PRESSED) || pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::X_PRESSED) || pad.check_event(Gamepad::Y_PRESSED) ||pad.check_event(Gamepad::BACK_PRESSED)) {
        _buttonPressed = true;
        pad.tone(750.0,0.1);
        wait(0.1);
    }
    _d = pad.get_direction();

}

void Menu::draw(N5110 &lcd)
{
    lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);
    if (_menuScreen == "main" ) {

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

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

        int _snake[8][40] =   {
            { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,1,0,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            { 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0},
            { 0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
            { 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0},
            { 0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}     

        };
        
        lcd.drawSprite(10,35,8,40,(int *)_snake);

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

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

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

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

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

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

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

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

int Menu::getLvl()
{
    return _level;
}