You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
7.8 KiB
177 lines
7.8 KiB
#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;
|
|
Selectable *selectable1 = SelectableInit(textures, backgroundTextures, 1, &position, "Building", showDescription, 9, 16, SELECTABLE_ID_TEST);
|
|
position.y += 100;
|
|
Selectable *selectable2 = SelectableInit(textures, backgroundTextures, 1,&position, "Building2", showDescription, 10, 16, SELECTABLE_ID_TEST);
|
|
position.y += 100;
|
|
Selectable *selectable3 = SelectableInit(textures, backgroundTextures, 1,&position, "Building3", showDescription, 10, 16, SELECTABLE_ID_TEST);
|
|
position.y += 100;
|
|
Selectable *selectable4 = SelectableInit(textures, backgroundTextures, 1,&position, "Building4", showDescription, 10, 16, SELECTABLE_ID_TEST);
|
|
position.x += 100;
|
|
Selectable *selectable5 = SelectableInit(textures, backgroundTextures, 1,&position, "Building5", showDescription, 10, 16, SELECTABLE_ID_TEST);
|
|
position.x += 100;
|
|
Selectable *selectable6 = SelectableInit(textures, backgroundTextures, 1,&position, "Building6", showDescription, 10, 16, SELECTABLE_ID_TEST);
|
|
|
|
uiContainer->selectables[0] = selectable1;
|
|
uiContainer->selectables[1] = selectable2;
|
|
uiContainer->selectables[2] = selectable3;
|
|
uiContainer->selectables[3] = selectable4;
|
|
uiContainer->selectables[4] = selectable5;
|
|
uiContainer->selectables[5] = selectable6;
|
|
|
|
uiContainer->selectableCounter = 6;
|
|
|
|
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){
|
|
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]);
|
|
}
|
|
} |