empezando a trabajar en la clase DefineButtons
This commit is contained in:
172
source/define_buttons.cpp
Normal file
172
source/define_buttons.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#include "define_buttons.h"
|
||||
|
||||
// Constructor
|
||||
DefineButtons::DefineButtons(SDL_Renderer *renderer, Input *input, Text *text, param_t *param, options_t *options)
|
||||
{
|
||||
// Copia punteros a los objetos
|
||||
this->renderer = renderer;
|
||||
this->input = input;
|
||||
this->text = text;
|
||||
this->param = param;
|
||||
this->options = options;
|
||||
|
||||
// Inicializa variables
|
||||
enabled = false;
|
||||
x = param->gameWidth / 2;
|
||||
y = param->pressStart;
|
||||
indexController = 0;
|
||||
indexButton = 0;
|
||||
|
||||
buttons.clear();
|
||||
db_button_t button;
|
||||
|
||||
button.label = "DISPARO IZQUIERDA";
|
||||
button.input = input_fire_left;
|
||||
button.button = SDL_CONTROLLER_BUTTON_X;
|
||||
buttons.push_back(button);
|
||||
|
||||
button.label = "DISPARO CENTRO";
|
||||
button.input = input_fire_center;
|
||||
button.button = SDL_CONTROLLER_BUTTON_Y;
|
||||
buttons.push_back(button);
|
||||
|
||||
button.label = "DISPARO DERECHA";
|
||||
button.input = input_fire_right;
|
||||
button.button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
|
||||
buttons.push_back(button);
|
||||
|
||||
button.label = "START";
|
||||
button.input = input_start;
|
||||
button.button = SDL_CONTROLLER_BUTTON_START;
|
||||
buttons.push_back(button);
|
||||
|
||||
button.label = "QUIT";
|
||||
button.input = input_exit;
|
||||
button.button = SDL_CONTROLLER_BUTTON_BACK;
|
||||
buttons.push_back(button);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
DefineButtons::~DefineButtons()
|
||||
{
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void DefineButtons::update()
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el objeto en pantalla
|
||||
void DefineButtons::render()
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
text->writeCentered(x, y - 10, "PLAYER " + std::to_string(indexController + 1));
|
||||
text->writeCentered(x, y, buttons[indexButton].label);
|
||||
}
|
||||
}
|
||||
|
||||
int getJoyIndex(int id);
|
||||
|
||||
void DefineButtons::doJoystickButtonDown(SDL_JoyButtonEvent *event)
|
||||
{
|
||||
//int i = getJoyIndex(event->which);
|
||||
|
||||
buttons[indexButton].button = (SDL_GameControllerButton)event->button;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void DefineButtons::checkInput()
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
SDL_Event event;
|
||||
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_JOYBUTTONDOWN)
|
||||
doJoystickButtonDown(&event.jbutton);
|
||||
|
||||
/*if (eventHandler->type == SDL_JOYBUTTONDOWN)
|
||||
{
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_A)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_A;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_B)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_B;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_X)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_X;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_Y)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_Y;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_BACK)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_BACK;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_START)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_START;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
|
||||
incIndexButton();
|
||||
}
|
||||
|
||||
if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)
|
||||
{
|
||||
buttons[indexButton].button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
|
||||
incIndexButton();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Habilita el objeto
|
||||
void DefineButtons::enable(int index)
|
||||
{
|
||||
enabled = true;
|
||||
indexController = index - 1;
|
||||
indexButton = 0;
|
||||
}
|
||||
|
||||
// Comprueba si está habilitado
|
||||
bool DefineButtons::isEnabled()
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// Incrementa el indice de los botones
|
||||
void DefineButtons::incIndexButton()
|
||||
{
|
||||
indexButton++;
|
||||
|
||||
if (indexButton == (int)buttons.size())
|
||||
{
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user