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.

83 lines
3.4 KiB

#include "selectable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id){
Selectable *selectable = malloc(sizeof(Selectable));
selectable->texture[0] = textures[0];
selectable->texture[1] = textures[1];
selectable->texture[2] = textures[2];
selectable->hasBackground = hasBackground;
if(selectable->hasBackground == 1){
selectable->backgroundTexture[0] = backgroundTextures[0];
selectable->backgroundTexture[1] = backgroundTextures[1];
selectable->backgroundTexture[2] = backgroundTextures[2];
}
//else{
// selectable->backgroundTexture[0] = 0;
// selectable->backgroundTexture[1] = 0;
// selectable->backgroundTexture[2] = 0;
//}
selectable->position.x = position->x;
selectable->position.y = position->y;
strncpy(selectable->description, description, descriptionLEN);
selectable->showDescription = showDescripton;
selectable->fontSize = fontSize;
selectable->id = id;
selectable->state = 0;
return selectable;
}
// TODO: Variable die den Maus Input freigibt solange nichts mit der Maus gemacht wurde
// Sobald ein Button gedrückt wurde in diesem Frame oder rect gezogen wird oder so ist sind Maus Funktionen gesperrt
// verhindert Spawnen von entities wenn man UI Elemente Drückt etc
void SelectableExecuteSelectable(Selectable *selectable, Game * game){
// reset state after executing?
//selectable->state = SELECTABLE_STATE_DEFAULT;
switch(selectable->id){
case SELECTABLE_ID_TEST: // Test Case Platzhalter
//printf("SELECTED\n");
break;
default:
printf("\n\n\n\n\n\n Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id);
break;
}
}
int SelectableUpdateSelectableState(Selectable * selectable){
if(selectable->state == SELECTABLE_STATE_SELECTED){
if(IsMouseButtonDown(MOUSE_BUTTON_RIGHT)){
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
return SELECTABLE_STATE_SELECTED;
}
if(GetMouseX() > selectable->position.x &&
GetMouseX() < selectable->position.x + selectable->backgroundTexture[selectable->state].width &&
GetMouseY() > selectable->position.y &&
GetMouseY() < selectable->position.y + selectable->backgroundTexture[selectable->state].height
){
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){
return selectable->state = SELECTABLE_STATE_SELECTED;
}
return selectable->state = SELECTABLE_STATE_HOVERED;
}
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
int SelectableUnselectSelectable(Selectable * selectable){
selectable->state = SELECTABLE_STATE_DEFAULT;
}
void SelectableDrawSelectable(Selectable * selectable){
if(selectable->hasBackground) {
DrawTexture(selectable->backgroundTexture[selectable->state], selectable->position.x, selectable->position.y, WHITE);
}
DrawTexture(selectable->texture[selectable->state], selectable->position.x, selectable->position.y, WHITE);
if(selectable->showDescription == 1){
DrawText(selectable->description, selectable->position.x, selectable->position.y, selectable->fontSize, WHITE);
}
}