parent
be2734a793
commit
e00807a276
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* \file GameInputProcessor.h
|
||||
* \brief Define a structure, GameInputProcessor and functions to convert SDL_event to envent for Pontu
|
||||
* \author Martin Rouault
|
||||
* \date 06/12/2021
|
||||
*/
|
||||
|
||||
#ifndef GAME_INPUT_PROCESSOR_INCLUDED
|
||||
#define GAME_INPUT_PROCESSOR_INCLUDED
|
||||
|
||||
#include "engine/Button.h"
|
||||
#include "engine/arrayButton.h"
|
||||
#include "engine/InputElement.h"
|
||||
#include "model/Coord.h"
|
||||
|
||||
/**
|
||||
* \struct GameInputProcessor
|
||||
* \brief Contains datas useful to handle input
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
struct array_P_Button tabButton;
|
||||
Coord selectedCase; ///< A case in Board (used to handle move actions) , (-1;-1) si inexistant
|
||||
} GameInputProcessor;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Create a new input processor
|
||||
*
|
||||
* \return A new input processor
|
||||
*/
|
||||
GameInputProcessor createGameInputProcessor();
|
||||
|
||||
/**
|
||||
* \brief Convert a screen coord into a model Coord
|
||||
*
|
||||
* \param [in] point Screen coordinates
|
||||
* \param [in] boardRect The game's board's rect
|
||||
* \return Game coordinate from point
|
||||
*/
|
||||
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect);
|
||||
|
||||
/**
|
||||
* \brief Poll and convert SDL_Events into specific event for Pontu
|
||||
*
|
||||
* \param [in, out] GameinputProcessor The input processor which keeps a state of input in between calls
|
||||
* \param [in] boardRect The game's board's rect
|
||||
* \return InputElement : an event for Pontu
|
||||
*/
|
||||
InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect);
|
||||
|
||||
#endif // GAME_INPUT_PROCESSOR_INCLUDED
|
@ -0,0 +1,9 @@
|
||||
#ifndef ARRAY_BUTTON_INCLUDED
|
||||
#define ARRAY_BUTTON_INCLUDED
|
||||
|
||||
#include "engine/Button.h"
|
||||
#include "engine/ArrayUtils.h"
|
||||
|
||||
GENERATE_DYNAMIC_ARRAY(P_Button)
|
||||
|
||||
#endif //ARRAY_BUTTON_INCLUDED
|
@ -0,0 +1,87 @@
|
||||
#include "engine/GameInputProcessor.h"
|
||||
|
||||
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect){
|
||||
Coord coord = {
|
||||
coord.x = (point->x-boardRect->x)*9/boardRect->w,
|
||||
coord.y = (point->y-boardRect->y)*9/boardRect->h
|
||||
};
|
||||
return coord;
|
||||
}
|
||||
|
||||
GameInputProcessor createGameInputProcessor() {
|
||||
GameInputProcessor gameInputProcessor = {
|
||||
.selectedCase = {.x=-1, .y=-1},
|
||||
.tabButton = array_P_Button_Create()
|
||||
};
|
||||
return gameInputProcessor;
|
||||
}
|
||||
|
||||
InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL_Rect* boardRect)
|
||||
{
|
||||
SDL_Event event;
|
||||
if (!SDL_PollEvent(&event))
|
||||
{
|
||||
return createInputElementNone();
|
||||
}
|
||||
|
||||
switch (event.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
return createInputElementUIQuit();
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
{
|
||||
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y};
|
||||
if (SDL_PointInRect(&mousePoint, boardRect))
|
||||
{
|
||||
if (!coordValid(gameInputProcessor->selectedCase)) {
|
||||
gameInputProcessor->selectedCase = screenCoordToGameCoord(&mousePoint, boardRect);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y};
|
||||
if (SDL_PointInRect(&mousePoint, boardRect))
|
||||
{
|
||||
if (coordValid(gameInputProcessor->selectedCase))
|
||||
{
|
||||
Coord newCoords = screenCoordToGameCoord(&mousePoint, boardRect);
|
||||
if (coordEqual(gameInputProcessor->selectedCase, newCoords)) {
|
||||
gameInputProcessor->selectedCase = newCoords;
|
||||
return createInputElementClickBoard(newCoords);
|
||||
}
|
||||
else {
|
||||
const Coord oldCoord = gameInputProcessor->selectedCase;
|
||||
gameInputProcessor->selectedCase = newCoord(-1,-1);
|
||||
return createInputElementMoveBoard(oldCoord, newCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
|
||||
P_Button* b = &gameInputProcessor->tabButton.elems[i];
|
||||
if (SDL_PointInRect(&mousePoint, &b->rect)) {
|
||||
b->onClick(b);
|
||||
}
|
||||
}
|
||||
return createInputElementNone();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEMOTION:
|
||||
{
|
||||
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
|
||||
P_Button* b = &gameInputProcessor->tabButton.elems[i];
|
||||
isHover(b, event.motion.x, event.motion.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return createInputElementNone();
|
||||
}
|
||||
|
Loading…
Reference in new issue