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.
76 lines
2.8 KiB
76 lines
2.8 KiB
#include "button.h"
|
|
#include "../game.h"
|
|
#include "screenIDs.h"
|
|
#include "stdio.h"
|
|
#include "stdlib.h"
|
|
#include "raylib.h"
|
|
#include "string.h"
|
|
|
|
Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int fontSize, int id){
|
|
Button *button = malloc(sizeof(Button));
|
|
|
|
button->textures[0] = textures[0];
|
|
button->textures[1] = textures[1];
|
|
button->textures[2] = textures[2];
|
|
button->textures[3] = textures[3];
|
|
|
|
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;
|
|
|
|
return button;
|
|
}
|
|
|
|
void ButtonExecuteButton(Button *button, Game *game){
|
|
button->state = BUTTON_STATE_DEFAULT;
|
|
switch(button->id){
|
|
case BUTTON_ID_CONTINUE: // continue game
|
|
game->screen = SCREEN_GAME;
|
|
break;
|
|
case BUTTON_ID_EXIT:
|
|
// wahrscheinlich kein guter stil, es muss noch zeug freigegeben werden oder soos... keine Ahnung
|
|
game->screen = SCREEN_EXIT;
|
|
break;
|
|
case BUTTON_ID_START_GAME:
|
|
game->screen = SCREEN_GAME;
|
|
break;
|
|
default:
|
|
printf("\n\n\n\n\n\n Unsupported Button ID %d \n\n\n\n\n\n", button->id);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ButtonDrawButton(Button * button){
|
|
// erst Button Texture, dann Text zentriert drauf
|
|
DrawTexture(button->textures[button->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){
|
|
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
|
|
){
|
|
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;
|
|
}
|
|
|