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.
202 lines
8.4 KiB
202 lines
8.4 KiB
#include "uiContainer.h"
|
|
#include "../game.h"
|
|
#include "../Textures/textureatlas.h"
|
|
#include "button.h"
|
|
#include "raylib.h"
|
|
#include "stdlib.h"
|
|
#include "stdio.h"
|
|
#include "../IsometricMap/isometricMap.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(Game *game){
|
|
UiContainer *uiContainer = UiContainerInitEmptyUiContainer();
|
|
|
|
// predefined stuff
|
|
Texture2D **textures = &(game->textures->textures[TE_BUTTON]);
|
|
Vector2 position = (Vector2){GetScreenWidth()/2 - (*textures)[0].width/2, GetScreenHeight()/2 - 50};
|
|
int buttonFontSize = 36;
|
|
|
|
// creating the ui elements
|
|
Button *continuebutton = ButtonInitButton(textures, &position, "Continue", 9/*String len*/, buttonFontSize, BUTTON_ID_CONTINUE);
|
|
position.y += 250;
|
|
Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5/*String len*/, buttonFontSize, BUTTON_ID_EXIT);
|
|
|
|
// adding the ui elements
|
|
UiContainerAddButton(uiContainer, continuebutton);
|
|
UiContainerAddButton(uiContainer, exitButton);
|
|
|
|
return uiContainer;
|
|
}
|
|
|
|
// Only for use in UiContainer
|
|
static UiContainer * UiContainerInitMainMenuUiContainer(Game *game){
|
|
UiContainer *uiContainer = UiContainerInitEmptyUiContainer();
|
|
|
|
// predefined stuff
|
|
Texture2D **textures = &(game->textures->textures[TE_BUTTON]);
|
|
Vector2 position = (Vector2){GetScreenWidth()/2 -( *textures)[0].width/2, GetScreenHeight()/2 - 50};
|
|
int buttonFontSize = 36;
|
|
|
|
// creating the ui elements
|
|
Button *continuebutton = ButtonInitButton(textures, &position, "Start Game", 11/*String len*/, buttonFontSize, BUTTON_ID_START_GAME);
|
|
position.y += 250;
|
|
Button *exitbutton = ButtonInitButton(textures, &position, "EXIT", 5/*String len*/, buttonFontSize, BUTTON_ID_EXIT);
|
|
|
|
// adding the ui elements
|
|
UiContainerAddButton(uiContainer, continuebutton);
|
|
UiContainerAddButton(uiContainer, exitbutton);
|
|
|
|
return uiContainer;
|
|
}
|
|
|
|
// Only for use in UiContainer
|
|
static UiContainer * UiContainerInitGameUiContainer(Game *game){
|
|
UiContainer *uiContainer = UiContainerInitEmptyUiContainer();
|
|
|
|
// predefined stuff
|
|
Texture2D *texture1 = game->textures->textures[TE_BUILDING];
|
|
Texture2D *texture2 = game->textures->textures[TE_WORKER] ;
|
|
Texture2D *texture3 = game->textures->textures[TE_LUMBERJACK] ;
|
|
Texture2D **backgroundTextures = &(game->textures->textures[TE_SELECTABLE_BACKGROUND]);
|
|
Vector2 position = (Vector2){4, GetScreenHeight() - 100};
|
|
int showDescription = 1;
|
|
int hasBackground = 1;
|
|
int fontSize = 16;
|
|
int unselectAfterExecute = 1;
|
|
|
|
// creating the ui elements
|
|
Selectable *selectable1 = SelectableInit(texture1, backgroundTextures, hasBackground , &position, "Building",
|
|
showDescription, 9/*String len*/, fontSize, SELECTABLE_ID_SPAWN_BUILDING,
|
|
1/*Group*/, unselectAfterExecute, 1);
|
|
position.x += 100;
|
|
Selectable *selectable2 = SelectableInit(texture2, backgroundTextures, hasBackground,&position, "Worker",
|
|
showDescription, 7/*String len*/, fontSize, SELECTABLE_ID_SPAWN_WORKER, 1/*Group*/,
|
|
unselectAfterExecute, 1);
|
|
position.x += 100;
|
|
Selectable *selectable3 = SelectableInit(texture3, backgroundTextures, hasBackground,&position, "Lumberjack",
|
|
showDescription, 11/*String len*/, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, 1/*Group*/,
|
|
unselectAfterExecute, 1);
|
|
|
|
// adding the ui elements
|
|
UiContainerAddSelectable(uiContainer, selectable1);
|
|
UiContainerAddSelectable(uiContainer, selectable2);
|
|
UiContainerAddSelectable(uiContainer, selectable3);
|
|
|
|
showDescription = 0;
|
|
unselectAfterExecute = 0;
|
|
int i = 0;
|
|
position = (Vector2){GetScreenWidth()-50, 2};
|
|
for(i; i < ISOMETRICMAP_TILE_TEXTURE_AMOUNT; i++){
|
|
UiContainerAddSelectable(uiContainer,
|
|
SelectableInit(game->map->tileTextures[i], backgroundTextures, hasBackground , &position, "",
|
|
showDescription, 0/*String len*/, fontSize, SELECTABLE_ID_DRAWING_TILE+i,
|
|
1/*Group*/, 1, 0.5f)
|
|
);
|
|
position.y += 50;
|
|
}
|
|
|
|
return uiContainer;
|
|
}
|
|
|
|
UiContainer * UiContainerInitUiContainerForScreenID(Game *game, int id){
|
|
switch(id){
|
|
case SCREEN_MAINMENU:
|
|
return UiContainerInitMainMenuUiContainer(game);
|
|
case SCREEN_GAME:
|
|
return UiContainerInitGameUiContainer(game);
|
|
case SCREEN_PAUSE:
|
|
return UiContainerInitPauseUiContainer(game);
|
|
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]);
|
|
}
|
|
}
|
|
|
|
void UiContainerAddButton(UiContainer *uiContainer, Button *button){
|
|
if(uiContainer->buttonCounter == UI_CONTAINER_MAX_BUTTONS){
|
|
printf("UI CONTAINER VOLL [BUTTONS] \n");
|
|
return;
|
|
}
|
|
uiContainer->buttons[uiContainer->buttonCounter++] = button;
|
|
}
|
|
void UiContainerAddSelectable(UiContainer *uiContainer, Selectable *selectable){
|
|
if(uiContainer->selectableCounter == UI_CONTAINER_MAX_SELECTABLES){
|
|
printf("UI CONTAINER VOLL [SElECTABLES] \n");
|
|
return;
|
|
}
|
|
uiContainer->selectables[uiContainer->selectableCounter++] = selectable;
|
|
} |