diff --git a/Pontu/include/engine/GameInputProcessor.h b/Pontu/include/engine/GameInputProcessor.h index d94fc8e..8858507 100644 --- a/Pontu/include/engine/GameInputProcessor.h +++ b/Pontu/include/engine/GameInputProcessor.h @@ -11,6 +11,7 @@ #include "engine/Button.h" #include "engine/arrayButton.h" #include "engine/InputElement.h" +#include "engine/InputProcessor.h" #include "model/Coord.h" /** @@ -19,7 +20,7 @@ */ typedef struct { - struct array_P_Button tabButton; + InputProcessor inputProcessor; Coord selectedCase; ///< A case in Board (used to handle move actions) , (-1;-1) si inexistant } GameInputProcessor; @@ -56,4 +57,7 @@ Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect); */ InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect); + +InputElement interpretSDL_EventGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect, const SDL_Event* event); + #endif // GAME_INPUT_PROCESSOR_INCLUDED diff --git a/Pontu/include/engine/InputProcessor.h b/Pontu/include/engine/InputProcessor.h index 53e665c..1056a78 100644 --- a/Pontu/include/engine/InputProcessor.h +++ b/Pontu/include/engine/InputProcessor.h @@ -49,4 +49,6 @@ void freeInputProcessor(InputProcessor* inputProcessor); */ InputElement proccessInput(InputProcessor* inputProcessor); +InputElement interpretSDL_EventInput(InputProcessor* inputProcessor, const SDL_Event* event); + #endif // INPUT_PROCESSOR_INCLUDED diff --git a/Pontu/src/engine/GameInputProcessor.c b/Pontu/src/engine/GameInputProcessor.c index 01558fa..3b7bd89 100644 --- a/Pontu/src/engine/GameInputProcessor.c +++ b/Pontu/src/engine/GameInputProcessor.c @@ -1,43 +1,37 @@ #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 - }; +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() - }; +GameInputProcessor createGameInputProcessor() +{ + GameInputProcessor gameInputProcessor = { .selectedCase = { .x = -1, .y = -1 }, + .inputProcessor = createInputProcessor() }; return gameInputProcessor; } -void freeGameInputProcessor(GameInputProcessor* gameInputProcessor) { - array_P_Button_Free(&gameInputProcessor->tabButton); +void freeGameInputProcessor(GameInputProcessor* gameInputProcessor) +{ + freeInputProcessor(&gameInputProcessor->inputProcessor); } -InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL_Rect* boardRect) +InputElement interpretSDL_EventGameInput(GameInputProcessor* gameInputProcessor, + const SDL_Rect* boardRect, + const SDL_Event* event) { - SDL_Event event; - if (!SDL_PollEvent(&event)) - { - return createInputElementNone(); - } - - switch (event.type) + switch (event->type) { - case SDL_QUIT: - return createInputElementUIQuit(); case SDL_MOUSEBUTTONDOWN: { - const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; + const SDL_Point mousePoint = { .x = event->button.x, .y = event->button.y }; if (SDL_PointInRect(&mousePoint, boardRect)) { - if (!coordValid(gameInputProcessor->selectedCase)) { + if (!coordValid(gameInputProcessor->selectedCase)) + { gameInputProcessor->selectedCase = screenCoordToGameCoord(&mousePoint, boardRect); } } @@ -45,49 +39,44 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL } case SDL_MOUSEBUTTONUP: { - const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; + 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)) { + if (coordEqual(gameInputProcessor->selectedCase, newCoords)) + { gameInputProcessor->selectedCase = newCoords; return createInputElementClickBoard(newCoords); } - else { - const Coord oldCoord = gameInputProcessor->selectedCase; - gameInputProcessor->selectedCase = newCoord(-1,-1); + else + { + const Coord oldCoord = gameInputProcessor->selectedCase; + gameInputProcessor->selectedCase = newCoord(-1, -1); return createInputElementMoveBoard(oldCoord, newCoords); } } } - else - { - for (size_t i = 0; itabButton.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; itabButton.size; ++i) { - P_Button* b = &gameInputProcessor->tabButton.elems[i]; - isButtonInteractWithCursor(b, event.motion.x, event.motion.y); - } - break; - } - case SDL_WINDOWEVENT: - if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - return createInputElementResizeWindow(event.window.data1, event.window.data2); - } - break; } return createInputElementNone(); } + +InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect) +{ + SDL_Event event; + if (!SDL_PollEvent(&event)) + { + return createInputElementNone(); + } + + InputElement ret = interpretSDL_EventGameInput(gameInputProcessor, boardRect, &event); + if (ret.type == InputType_None) + { + return interpretSDL_EventInput(&gameInputProcessor->inputProcessor, &event); + } + return ret; +} diff --git a/Pontu/src/engine/InputProcessor.c b/Pontu/src/engine/InputProcessor.c index 2eef572..b4b9924 100644 --- a/Pontu/src/engine/InputProcessor.c +++ b/Pontu/src/engine/InputProcessor.c @@ -1,62 +1,64 @@ #include "engine/InputProcessor.h" -InputProcessor createInputProcessor() { - InputProcessor inputProcessor = { - .tabButton = array_P_Button_Create(), - .tabTextInput = array_TextInput_Create(), - .selectedTextInput = NULL - }; +InputProcessor createInputProcessor() +{ + InputProcessor inputProcessor = { .tabButton = array_P_Button_Create(), + .tabTextInput = array_TextInput_Create(), + .selectedTextInput = NULL }; SDL_StopTextInput(); return inputProcessor; } -void freeInputProcessor(InputProcessor* inputProcessor) { +void freeInputProcessor(InputProcessor* inputProcessor) +{ array_P_Button_Free(&inputProcessor->tabButton); } -InputElement proccessInput(InputProcessor *inputProcessor) -{ - SDL_Event event; - if (!SDL_PollEvent(&event)) - { - return createInputElementNone(); - } - switch (event.type) +InputElement interpretSDL_EventInput(InputProcessor* inputProcessor, const SDL_Event* event) +{ + switch (event->type) { - case SDL_QUIT: - return createInputElementUIQuit(); + case SDL_QUIT: return createInputElementUIQuit(); case SDL_MOUSEBUTTONUP: { - const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; + const SDL_Point mousePoint = { .x = event->button.x, .y = event->button.y }; - for (size_t i = 0; itabButton.size; ++i) { + for (size_t i = 0; i < inputProcessor->tabButton.size; ++i) + { P_Button* b = &inputProcessor->tabButton.elems[i]; - if (isHover(b)) { + if (isHover(b)) + { b->onClick(b); - break; + break; } } bool textInputClicked = false; - for (size_t i = 0; itabTextInput.size; ++i) { + for (size_t i = 0; i < inputProcessor->tabTextInput.size; ++i) + { TextInput* ti = &inputProcessor->tabTextInput.elems[i]; - if (SDL_PointInRect(&mousePoint, &ti->size)) { - if (inputProcessor->selectedTextInput == NULL) { + if (SDL_PointInRect(&mousePoint, &ti->size)) + { + if (inputProcessor->selectedTextInput == NULL) + { SDL_StartTextInput(); } - else { + else + { inputProcessor->selectedTextInput->isActive = false; } - inputProcessor->selectedTextInput = ti; - textInputClicked = true; + inputProcessor->selectedTextInput = ti; + textInputClicked = true; inputProcessor->selectedTextInput->isActive = true; - inputProcessor->selectedTextInput->cursorPosition = strlen(inputProcessor->selectedTextInput->value); + inputProcessor->selectedTextInput->cursorPosition = + strlen(inputProcessor->selectedTextInput->value); break; } } - if (!textInputClicked && inputProcessor->selectedTextInput != NULL) { + if (!textInputClicked && inputProcessor->selectedTextInput != NULL) + { inputProcessor->selectedTextInput->isActive = false; - inputProcessor->selectedTextInput = NULL; + inputProcessor->selectedTextInput = NULL; SDL_StopTextInput(); } @@ -64,33 +66,38 @@ InputElement proccessInput(InputProcessor *inputProcessor) } case SDL_MOUSEMOTION: { - for (size_t i = 0; itabButton.size; ++i) { + for (size_t i = 0; i < inputProcessor->tabButton.size; ++i) + { P_Button* b = &inputProcessor->tabButton.elems[i]; - int res = isButtonInteractWithCursor(b, event.motion.x, event.motion.y); - if (res != BUTTON_NOTHING) { + int res = isButtonInteractWithCursor(b, event->motion.x, event->motion.y); + if (res != BUTTON_NOTHING) + { return createInputElementButtonChanged(b, res); } } break; } case SDL_WINDOWEVENT: - if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - return createInputElementResizeWindow(event.window.data1, event.window.data2); + if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) + { + return createInputElementResizeWindow(event->window.data1, event->window.data2); } - if (event.window.event == SDL_WINDOWEVENT_CLOSE) { + if (event->window.event == SDL_WINDOWEVENT_CLOSE) + { return createInputElementCloseWindow(); } break; case SDL_TEXTINPUT: - addStringToInputTextValue(inputProcessor->selectedTextInput, event.text.text); + addStringToInputTextValue(inputProcessor->selectedTextInput, event->text.text); return createInputElementTextInput(inputProcessor->selectedTextInput); break; case SDL_TEXTEDITING: - inputProcessor->selectedTextInput->cursorPosition = event.edit.start; + inputProcessor->selectedTextInput->cursorPosition = event->edit.start; return createInputElementTextInput(inputProcessor->selectedTextInput); break; case SDL_KEYDOWN: - if (inputProcessor->selectedTextInput != NULL && event.key.keysym.sym == SDLK_BACKSPACE) { + if (inputProcessor->selectedTextInput != NULL && event->key.keysym.sym == SDLK_BACKSPACE) + { removeCharacterToInputTextValueAtCursor(inputProcessor->selectedTextInput); return createInputElementTextInput(inputProcessor->selectedTextInput); } @@ -99,3 +106,14 @@ InputElement proccessInput(InputProcessor *inputProcessor) return createInputElementNone(); } + +InputElement proccessInput(InputProcessor* inputProcessor) +{ + SDL_Event event; + if (!SDL_PollEvent(&event)) + { + return createInputElementNone(); + } + + return interpretSDL_EventInput(inputProcessor, &event); +} diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index a48e731..30c5449 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -39,13 +39,13 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend if (*generalState != GS_Game) { return; } - GameInputProcessor inputProcessor = createGameInputProcessor(); + GameInputProcessor gameInputProcessor = createGameInputProcessor(); struct array_Coord interactiveCases = array_Coord_Create(); Game game = newGame(nbPlayers, players); TextureHandler textureHandler = newTextureHandler(renderer); - inputProcessor.tabButton = createGameInterfaceButtons(renderer, fontHandler, generalState,audioHandler); + gameInputProcessor.inputProcessor.tabButton = createGameInterfaceButtons(renderer, fontHandler, generalState,audioHandler); struct array_TextLabel tabLabel = createGameInterfaceLabels(renderer,fontHandler); @@ -59,8 +59,8 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend SDL_SetRenderDrawColor(renderer, 50,10,10, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board); - for (size_t i=0; i