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.

78 lines
2.7 KiB

#include "button.h"
#include "../game.h"
#include "stdio.h"
#include "stdlib.h"
#include "raylib.h"
#include "string.h"
#include "onClickFunctions.h"
Button * ButtonInitButton(Texture2D **textures, Vector2 *position, char *text, int textLEN, int fontSize, int id){
Button *button = malloc(sizeof(Button));
button->textures = textures;
button->position = (Vector2){position->x, position->y};
button->centerPosition = (Vector2){position->x + (*textures)[0].width/2, position->y + (*textures)[0].height/2};
strncpy(button->text, text, textLEN);
button->state = BUTTON_STATE_DEFAULT;
button->id = id;
button->fontSize = fontSize;
switch(button->id){
case BUTTON_ID_CONTINUE:
button->onClick = &OnClickContinueButton;
break;
case BUTTON_ID_EXIT:
button->onClick = &OnClickExitButton;
break;
case BUTTON_ID_START_GAME:
button->onClick = &OnClickStartButton;
break;
default:
printf("\n\n\n\n\n\n Unsupported Button ID %d \n\n\n\n\n\n", button->id);
button->onClick = &OnClickButton;
break;
}
return button;
}
void ButtonExecuteButton(Button *button, Game *game){
button->state = BUTTON_STATE_DEFAULT;
button->onClick(game, button);
}
void ButtonDrawButton(Button * button){
// erst Button Texture, dann Text zentriert drauf
int state = button->state;
if(state > 3){state = 3;}
DrawTexture((*button->textures)[state], button->position.x, button->position.y, WHITE);
DrawText(button->text, button->centerPosition.x - MeasureText(button->text, button->fontSize)/2, button->centerPosition.y - button->fontSize/2, button->fontSize, BLACK);
}
int ButtonUpdateButtonState(Button * button, Game *game){
if(button->state == BUTTON_STATE_RELEASED){
// Wir verlassen den RELEASED State automatisch wenn wir den Code des Buttons ausführen, siehe ButtonExecuteButton
// So lange der Code nicht ausgeführt wurde bleiben wir im state damit er definitiv im nächsten Frame ausgeführt wird
return button->state;
}
else if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + (*button->textures)[button->state].width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + (*button->textures)[button->state].height
){
game->mouseOnUI = 1;
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){
return button->state = BUTTON_STATE_PRESSED;
}
else if(button->state == BUTTON_STATE_PRESSED){
return button->state = BUTTON_STATE_RELEASED;
}
return button->state = BUTTON_STATE_HOVERED;
}
return button->state = BUTTON_STATE_DEFAULT;
}