#include "uiContainer.h" #include "../game.h" #include "button.h" #include "raylib.h" #include "stdlib.h" #include "stdio.h" static UiContainer * UiContainerInitEmptyUiContainer(){ UiContainer *uiContainer = malloc(sizeof(UiContainer)); uiContainer->buttonCounter = 0; uiContainer->selectableCounter = 0; return uiContainer; } // Only for use in UiContainer static UiContainer * UiContainerInitPauseUiContainer(){ UiContainer *uiContainer = malloc(sizeof(UiContainer)); Texture2D textures[4] = { LoadTexture("assets/button.png"), //DEFAULT LoadTexture("assets/button_hovered.png"), //HOVERED LoadTexture("assets/button_pressed.png"), //PRESSED LoadTexture("assets/button_pressed.png")}; //RELEASED Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50}; int buttonFontSize = 36; Button *continuebutton = ButtonInitButton(textures, &position, "Continue", 9, buttonFontSize, BUTTON_ID_CONTINUE); uiContainer->buttons[0] = continuebutton; uiContainer->buttonCounter = 1; position.y += 250; Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5, buttonFontSize, BUTTON_ID_EXIT); uiContainer->buttons[1] = exitButton; uiContainer->buttonCounter = 2; // WICHTIG, auch wenn der UiContainer gar keine selectables hat! uiContainer->selectableCounter = 0; // Methode funktioniert wieso auch immer auch ohne dieses return. C returned Implizit odder was O_o return uiContainer; } // Only for use in UiContainer static UiContainer * UiContainerInitMainMenuUiContainer(){ UiContainer *uiContainer = malloc(sizeof(UiContainer)); Texture2D textures[4] = { LoadTexture("assets/button.png"), //DEFAULT LoadTexture("assets/button_hovered.png"), //HOVERED LoadTexture("assets/button_pressed.png"), //PRESSED LoadTexture("assets/button_pressed.png")}; //RELEASED Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50}; int buttonFontSize = 36; Button *continuebutton = ButtonInitButton(textures, &position, "Start Game", 11, buttonFontSize, BUTTON_ID_START_GAME); uiContainer->buttons[0] = continuebutton; uiContainer->buttonCounter = 1; // WICHTIG, auch wenn der UiContainer gar keine selectables hat! uiContainer->selectableCounter = 0; position.y += 250; Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5, buttonFontSize, BUTTON_ID_EXIT); uiContainer->buttons[1] = exitButton; uiContainer->buttonCounter = 2; // Methode funktioniert wieso auch immer auch ohne dieses return. GCC returned Implizit odder was O_o return uiContainer; } // Only for use in UiContainer static UiContainer * UiContainerInitGameUiContainer(){ UiContainer *uiContainer = malloc(sizeof(UiContainer)); uiContainer->buttonCounter = 0; 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 LoadTexture("assets/selectable_background_selected.png") //SELECTED }; Vector2 position = (Vector2){20, 300}; int showDescription = 1; int hasBackground = 1; int groupOne = 0; int groupTwo = 1; int fontSize = 16; Selectable *selectable1 = SelectableInit(textures, backgroundTextures, hasBackground , &position, "Building", showDescription, 9, fontSize, SELECTABLE_ID_SPAWN_BUILDING, groupOne, 1); position.y += 100; Selectable *selectable2 = SelectableInit(textures2, backgroundTextures, hasBackground,&position, "Worker", showDescription, 7, fontSize, SELECTABLE_ID_SPAWN_WORKER, groupOne, 1); position.y += 100; Selectable *selectable3 = SelectableInit(textures2, backgroundTextures, hasBackground,&position, "Lumberjack", showDescription, 11, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, groupOne, 1); position.y += 100; int selectableCounter = 0; uiContainer->selectables[selectableCounter++] = selectable1; uiContainer->selectables[selectableCounter++] = selectable2; uiContainer->selectables[selectableCounter++] = selectable3; uiContainer->selectableCounter = selectableCounter; return uiContainer; } UiContainer * UiContainerInitUiContainerForScreenID(int id){ switch(id){ case SCREEN_MAINMENU: return UiContainerInitMainMenuUiContainer(); case SCREEN_GAME: return UiContainerInitGameUiContainer(); case SCREEN_PAUSE: return UiContainerInitPauseUiContainer(); default: printf("WARNING: Creating UiContainer for Screen ID %d (not implemented)\n", id); return UiContainerInitEmptyUiContainer(); } } // Only for use in UiContainer, should not be used on their own static void UpdateButtons(Game *game, Button **buttons, int buttonCounter){ int i = 0; for(i=0 ; i < buttonCounter; i++){ int state = ButtonUpdateButtonState(buttons[i], game); } } // Only for use in UiContainer, should not be used on their own static void UpdateSelectables(Game *game, Selectable **selectables, int selectableCounter){ int i = 0; for(i=0 ; i < selectableCounter; i++){ int setState = SelectableUpdateSelectableState(selectables[i], game); // Unselecting every selectable if one is selected if(setState == SELECTABLE_STATE_SELECTED){ int j; for(j=0 ; j < selectableCounter; j++){ if(i != j && selectables[j]->groupID == selectables[i]->groupID){ SelectableUnselectSelectable(selectables[j]); } } } } } static void ExecuteButtons(Game *game, Button **buttons, int buttonCounter){ int i = 0; for(i=0 ; i < buttonCounter; i++){ if(buttons[i]->state == BUTTON_STATE_RELEASED){ ButtonExecuteButton(buttons[i], game); } } } static void ExecuteSelectables(Game *game, Selectable **selectables, int selectableCounter){ int i = 0; for(i=0 ; i < selectableCounter; i++){ if(selectables[i]->state == SELECTABLE_STATE_SELECTED){ SelectableExecuteSelectable(selectables[i], game); } } } // Updated alle Buttons und Selectables und führt gegebenenfalls deren Code aus void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){ UpdateButtons(game, uiContainer->buttons, uiContainer->buttonCounter); UpdateSelectables(game, uiContainer->selectables, uiContainer->selectableCounter); ExecuteButtons(game, uiContainer->buttons, uiContainer->buttonCounter); ExecuteSelectables(game, uiContainer->selectables, uiContainer->selectableCounter); } // Drawed alle Buttons und Selectables void UiContainerDrawUiContainer(UiContainer *uiContainer){ 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++){ ButtonDrawButton(uiContainer->buttons[i]); } for(i=0 ; i < uiContainer->selectableCounter; i++){ SelectableDrawSelectable(uiContainer->selectables[i]); } }