From 1752eaa07cde4ad91638d7eae2bd6bde90a014d7 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 22 Jan 2023 23:48:52 +0100 Subject: [PATCH] MouseLock added --- Input/inputHandler.c | 114 ++++++++++++++++++++++--------------------- Ui/selectable.c | 25 ++++++++-- Ui/uiContainer.c | 41 ++++++++-------- definitions.h | 3 +- game.c | 3 ++ game.h | 3 ++ main.c | 20 ++++++-- 7 files changed, 122 insertions(+), 87 deletions(-) diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 775c1e4..7a272b3 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -103,71 +103,73 @@ void mouseInput(Game *game){ IsometricMapChangeTextureIdOfTile(map, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, 1); } - - if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ - if(inputHandler->pressed == 0){ - inputHandler->rectStart.x = GetMousePosition().x; - inputHandler->rectStart.y = GetMousePosition().y; - inputHandler->pressed = 1; - - // Cursorsprite is changed to "down" - game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1]; + if(game->mouseOnUI == 0){ + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ + if(inputHandler->pressed == 0){ + inputHandler->rectStart.x = GetMousePosition().x; + inputHandler->rectStart.y = GetMousePosition().y; + inputHandler->pressed = 1; + + // Cursorsprite is changed to "down" + game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1]; + } } - } if(inputHandler->pressed){ DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos)); } - if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ - inputHandler->pressed = 0; - // Cursorsprite is changed back to normal - game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0]; - float width = GetMousePosition().x - inputHandler->rectStart.x; - float height = GetMousePosition().y - inputHandler->rectStart.y; - - - // Add Sprite - if(abs(width) + abs(height) < 20){ - int maxWidth = (game->map->width) * game->map->textureWidth; - int maxHeight = (game->map->height) * game->map->textureHeight; - if(inputHandler->cursorWorldPos.x < 0){ printf("OutOfBoundsDestination Spawn\n");} - else if(inputHandler->cursorWorldPos.y < 0){ printf("OutOfBoundsDestination Spawn\n");} - else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} - else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} - else { - Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); - Entity *entity = EntityInit(newSprite, TE_WORKER, game->textures); - EntityListInsert(game->entities, entity); - SpriteListInsert(game->sprites, newSprite); - //ListPrintForward(sprites); - //ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); - } - } else{ - // Berechnung, welche Sprites ausgewählt wurden - Vector2 rect = GetRectangle(inputHandler->rectStart); - width = abs(width); - height = abs(height); - - float deltaX; - float deltaY; - Entity *current = game->entities->head; - while (current != 0){ - Vector2 currPos = {current->sprite->x + current->sprite->texture->width, current->sprite->y + current->sprite->texture->height/2}; - IsometricMapUnproject(map, camera, currPos.x, currPos.y, current->sprite->z, &currPos); - - deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x); - deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y); - - if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ - current->selected = 1; + if(game->mouseOnUI == 0){ + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + inputHandler->pressed = 0; + // Cursorsprite is changed back to normal + game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0]; + float width = GetMousePosition().x - inputHandler->rectStart.x; + float height = GetMousePosition().y - inputHandler->rectStart.y; + + + // Add Sprite + if(abs(width) + abs(height) < 20){ + int maxWidth = (game->map->width) * game->map->textureWidth; + int maxHeight = (game->map->height) * game->map->textureHeight; + if(inputHandler->cursorWorldPos.x < 0){ printf("OutOfBoundsDestination Spawn\n");} + else if(inputHandler->cursorWorldPos.y < 0){ printf("OutOfBoundsDestination Spawn\n");} + else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} + else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} + else { + //Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); + //Entity *entity = EntityInit(newSprite, TE_WORKER, game->textures); + //EntityListInsert(game->entities, entity); + //SpriteListInsert(game->sprites, newSprite); + //ListPrintForward(sprites); + //ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); } - else{ - current->selected = 0; + } else{ + // Berechnung, welche Sprites ausgewählt wurden + Vector2 rect = GetRectangle(inputHandler->rectStart); + width = abs(width); + height = abs(height); + + float deltaX; + float deltaY; + Entity *current = game->entities->head; + while (current != 0){ + Vector2 currPos = {current->sprite->x + current->sprite->texture->width, current->sprite->y + current->sprite->texture->height/2}; + IsometricMapUnproject(map, camera, currPos.x, currPos.y, current->sprite->z, &currPos); + + deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x); + deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y); + + if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ + current->selected = 1; + } + else{ + current->selected = 0; + } + + current = current->next; } - - current = current->next; } } } diff --git a/Ui/selectable.c b/Ui/selectable.c index 7d2d2e8..29c094b 100644 --- a/Ui/selectable.c +++ b/Ui/selectable.c @@ -6,6 +6,7 @@ #include "../MapObject/mapobject.h" #include "../Sprite/sprite.h" #include "../Input/inputHandler.h" +#include "../Entity/entity.h" Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id, int groupID){ @@ -38,12 +39,12 @@ Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[ } void SelectableExecuteSelectable(Selectable *selectable, Game * game){ + // Hier sollte eine Bedingung stehen die nur in einem Frame true sein kann + // Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird + // Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt switch(selectable->id){ - case SELECTABLE_ID_TEST: // Test Case Platzhalter - // Hier sollte eine Bedingung stehen die nur in einem Frame true sein kann - // Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird - // Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt - if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){ + case SELECTABLE_ID_SPAWN_BUILDING: + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ Sprite *new = SpriteCreate(game->textures, TE_BAUSTELLE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); MapObject *newObject = MapObjectInit(new, MO_BAUSTELLE); SpriteListInsert(game->sprites, new); @@ -52,6 +53,16 @@ void SelectableExecuteSelectable(Selectable *selectable, Game * game){ selectable->state = SELECTABLE_STATE_DEFAULT; } break; + case SELECTABLE_ID_SPAWN_WORKER: + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ + Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); + Entity *entity = EntityInit(newSprite, TE_WORKER, game->textures); + EntityListInsert(game->entities, entity); + SpriteListInsert(game->sprites, newSprite); + + selectable->state = SELECTABLE_STATE_DEFAULT; + } + break; default: printf("\n\n\n\n\n\n WARNING: Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id); break; @@ -86,9 +97,13 @@ int SelectableUnselectSelectable(Selectable * selectable){ } void SelectableDrawSelectable(Selectable * selectable){ + int targetWidth = 0; if(selectable->hasBackground == 1) { + targetWidth = selectable->backgroundTexture[selectable->state].width; DrawTexture(selectable->backgroundTexture[selectable->state], selectable->position.x, selectable->position.y, WHITE); } + + //DrawTextureEx(selectable->texture[selectable->state], selectable->position, targetWidth / selectable->texture[selectable->state].width, 2, WHITE); DrawTexture(selectable->texture[selectable->state], selectable->position.x, selectable->position.y, WHITE); if(selectable->showDescription == 1){ DrawText(selectable->description, selectable->position.x, selectable->position.y, selectable->fontSize, WHITE); diff --git a/Ui/uiContainer.c b/Ui/uiContainer.c index a48d149..8458999 100644 --- a/Ui/uiContainer.c +++ b/Ui/uiContainer.c @@ -76,10 +76,14 @@ static UiContainer * UiContainerInitGameUiContainer(){ uiContainer->buttonCounter = 0; - Texture2D textures[3] = { LoadTexture("assets/selectable.png"), //DEFAULT + Texture2D textures[3] = { LoadTexture("assets/selectable.png"), //DEFAULT LoadTexture("assets/selectable.png"), //HOVERED LoadTexture("assets/selectable.png") //SELECTED }; + Texture2D textures2[3] = { LoadTexture("assets/entities/worker/arbeiten-4.png"), //DEFAULT + LoadTexture("assets/entities/worker/arbeiten-4.png"), //HOVERED + LoadTexture("assets/entities/worker/arbeiten-4.png") //SELECTED + }; Texture2D backgroundTextures[3] = { LoadTexture("assets/selectable_background.png"), //DEFAULT LoadTexture("assets/selectable_background_hovered.png"), //HOVERED @@ -92,31 +96,16 @@ static UiContainer * UiContainerInitGameUiContainer(){ int groupTwo = 1; int fontSize = 16; Selectable *selectable1 = SelectableInit(textures, backgroundTextures, hasBackground , &position, "Building", - showDescription, 9, fontSize, SELECTABLE_ID_TEST, groupOne); + showDescription, 9, fontSize, SELECTABLE_ID_SPAWN_BUILDING, groupOne); position.y += 100; - Selectable *selectable2 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building2", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupOne); + Selectable *selectable2 = SelectableInit(textures2, backgroundTextures, hasBackground,&position, "Worker", + showDescription, 10, fontSize, SELECTABLE_ID_SPAWN_WORKER, groupOne); position.y += 100; - Selectable *selectable3 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building3", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupOne); - position.y += 100; - Selectable *selectable4 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building4", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupTwo); - position.x += 100; - Selectable *selectable5 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building5", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupTwo); - position.x += 100; - Selectable *selectable6 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building6", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupTwo); int selectableCounter = 0; uiContainer->selectables[selectableCounter++] = selectable1; uiContainer->selectables[selectableCounter++] = selectable2; - uiContainer->selectables[selectableCounter++] = selectable3; - uiContainer->selectables[selectableCounter++] = selectable4; - uiContainer->selectables[selectableCounter++] = selectable5; - uiContainer->selectables[selectableCounter++] = selectable6; uiContainer->selectableCounter = selectableCounter; @@ -141,7 +130,10 @@ UiContainer * UiContainerInitUiContainerForScreenID(int id){ static void UpdateButtons(Game *game, Button **buttons, int buttonCounter){ int i = 0; for(i=0 ; i < buttonCounter; i++){ - ButtonUpdateButtonState(buttons[i]); + int state = ButtonUpdateButtonState(buttons[i]); + if(state == BUTTON_STATE_HOVERED || state == BUTTON_STATE_PRESSED){ + game->mouseOnUI = 1; + } if(buttons[i]->state == BUTTON_STATE_RELEASED){ ButtonExecuteButton(buttons[i], game); } @@ -152,7 +144,11 @@ static void UpdateButtons(Game *game, Button **buttons, int buttonCounter){ static void UpdateSelectables(Game *game, Selectable **selectables, int selectableCounter){ int i = 0; for(i=0 ; i < selectableCounter; i++){ + int oldState = selectables[i]->state; int setState = SelectableUpdateSelectableState(selectables[i]); + if(setState == SELECTABLE_STATE_HOVERED || (setState == SELECTABLE_STATE_SELECTED && oldState != SELECTABLE_STATE_SELECTED)){ + game->mouseOnUI = 1; + } // Unselecting every selectable if one is selected if(setState == SELECTABLE_STATE_SELECTED){ int j; @@ -176,7 +172,10 @@ void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){ // Drawed alle Buttons und Selectables void UiContainerDrawUiContainer(UiContainer *uiContainer){ - if(uiContainer == 0) return; + if(uiContainer == 0){ + printf("WARNING: Trying to draw not existing UiContainer!!\n"); + return; + } int i = 0; // Funktion kann genauso wie update auch noch aufgeteilt werden, aktuell aber noch nicht notwendig for(i=0 ; i < uiContainer->buttonCounter; i++){ diff --git a/definitions.h b/definitions.h index 1ba8c32..db8e744 100644 --- a/definitions.h +++ b/definitions.h @@ -61,7 +61,8 @@ #define SELECTABLE_STATE_HOVERED 1 #define SELECTABLE_STATE_SELECTED 2 -#define SELECTABLE_ID_TEST 0 +#define SELECTABLE_ID_SPAWN_BUILDING 0 +#define SELECTABLE_ID_SPAWN_WORKER 1 diff --git a/game.c b/game.c index 9974b8b..00c372d 100644 --- a/game.c +++ b/game.c @@ -31,6 +31,9 @@ Game *GameInit() game->inputHandler->selectedLayer = -1; game->screen = SCREEN_MAINMENU; + game->mouseOnUI = 0; + game->mouseFrameLock = 0; + game->camera = malloc(sizeof(Camera2D)); game->camera->target.x = 0; game->camera->target.y = 0; diff --git a/game.h b/game.h index 5bad9bd..b5cb7cb 100644 --- a/game.h +++ b/game.h @@ -17,6 +17,9 @@ typedef struct Game{ struct EntityList *entities; struct UiContainer *UiContainers[SCREEN_AMOUNT]; int screen; + + int mouseOnUI ; // 0:not on UI 1:on UI + int mouseFrameLock ; // Sollten wir nicht brauchen wenn wir unsere Inputs gut definieren } Game; // returns pointer to new Game instance diff --git a/main.c b/main.c index d601301..6425883 100644 --- a/main.c +++ b/main.c @@ -28,13 +28,28 @@ int main(){ //SetTargetFPS(60); - + double frame = 0; while(!WindowShouldClose()){ + // Drawen wir einfach immer den UI Container vom aktuellen Screen? Aktuell hat ja jeder Screen einen, es sollte also keine Probleme geben + // Kann man natürlich auch noch oben in die einzelnen Cases schieben falls es mal Probleme machen sollte + UiContainerUpdateUiContainer(game->UiContainers[game->screen], game); + + if(game->mouseOnUI == 1 && frame == 0){ + frame = 1; + } + if(frame > 0){ + frame += GetFrameTime(); + } + if(frame >= 1.25){ + game->mouseOnUI = 0; + frame = 0; + } // Moving cursor Sprite to Mouse Pos game->cursorSprite->x = GetMousePosition().x; game->cursorSprite->y = GetMousePosition().y; + BeginDrawing(); // Drawing ist grundsätzlich immer aktiviert ClearBackground(RAYWHITE); // Screen wird in jedem Frame gecleared switch(game->screen){ // Screenspecific Code @@ -100,7 +115,6 @@ int main(){ // Drawen wir einfach immer den UI Container vom aktuellen Screen? Aktuell hat ja jeder Screen einen, es sollte also keine Probleme geben // Kann man natürlich auch noch oben in die einzelnen Cases schieben falls es mal Probleme machen sollte - UiContainerUpdateUiContainer(game->UiContainers[game->screen], game); UiContainerDrawUiContainer(game->UiContainers[game->screen]); @@ -111,8 +125,6 @@ int main(){ DrawSpriteToScreen(game->cursorSprite); EndDrawing(); - - } CloseWindow();