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.
84 lines
2.6 KiB
84 lines
2.6 KiB
#include "button.h"
|
|
#include "../game.h"
|
|
#include "screenIDs.h"
|
|
#include "stdio.h"
|
|
#include "stdlib.h"
|
|
#include "raylib.h"
|
|
#include "string.h"
|
|
|
|
Button * InitButton(Texture2D textures[3], Vector2 *position, char *text, int textLEN, int id){
|
|
Button *button = malloc(sizeof(Button));
|
|
|
|
button->textures[0] = textures[0];
|
|
button->textures[1] = textures[1];
|
|
button->textures[2] = textures[2];
|
|
|
|
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 = 0;
|
|
button->id = id;
|
|
|
|
return button;
|
|
}
|
|
|
|
void ButtonExecuteButton(Button *button, Game *game){
|
|
button->state = 0;
|
|
switch(button->id){
|
|
case 0: // continue game
|
|
if(game->screen == SCREEN_PAUSE){
|
|
game->screen = SCREEN_GAME;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ButtonDrawButton(Button * button){
|
|
ButtonUpdateButtonState(button);
|
|
DrawTexture(button->textures[button->state], button->position.x, button->position.y, WHITE);
|
|
DrawText(button->text, button->centerPosition.x - MeasureText(button->text, BUTTON_FONT_SIZE)/2, button->centerPosition.y - BUTTON_FONT_SIZE/2, BUTTON_FONT_SIZE, BLACK);
|
|
}
|
|
|
|
int ButtonUpdateButtonState(Button * button){
|
|
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(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
|
|
button->state = 2;
|
|
return 2;
|
|
}
|
|
button->state = 1;
|
|
return 1;
|
|
}
|
|
button->state = 0;
|
|
return 0;
|
|
}
|
|
|
|
int ButtonIsButtonHovered(Button * button){
|
|
if(GetMouseX() > button->position.x &&
|
|
GetMouseX() < button->position.x + button->textures[0].width &&
|
|
GetMouseY() > button->position.y &&
|
|
GetMouseY() < button->position.y + button->textures[0].height
|
|
){
|
|
return button->state = 1;
|
|
}
|
|
return button->state = 0;
|
|
}
|
|
|
|
int ButtonIsButtonPressed(Button * button){
|
|
if(GetMouseX() > button->position.x &&
|
|
GetMouseX() < button->position.x + button->textures[0].width &&
|
|
GetMouseY() > button->position.y &&
|
|
GetMouseY() < button->position.y + button->textures[0].height
|
|
){
|
|
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
|
|
return button->state = 2;
|
|
}
|
|
}
|
|
return button->state = 0;
|
|
}
|
|
|