#include "uiContainer.h" #include "../game.h" #include "button.h" #include "raylib.h" #include "stdlib.h" #include "stdio.h" #include "screenIDs.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 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_TEST, groupOne); position.y += 100; Selectable *selectable2 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building2", showDescription, 10, fontSize, SELECTABLE_ID_TEST, 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; 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++){ ButtonUpdateButtonState(buttons[i]); if(buttons[i]->state == BUTTON_STATE_RELEASED){ ButtonExecuteButton(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]); // 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]); } } } 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); } // Drawed alle Buttons und Selectables void UiContainerDrawUiContainer(UiContainer *uiContainer){ if(uiContainer == 0) 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]); } }