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.

115 lines
4.9 KiB

#include "selectable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../definitions.h"
#include "../MapObject/building.h"
#include "../Sprite/sprite.h"
#include "../Input/inputHandler.h"
#include "../Entity/entity.h"
#include "onClickFunctions.h"
Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, int hasBackground, Vector2 *position, char *description,
int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute){
Selectable *selectable = malloc(sizeof(Selectable));
selectable->texture = texture;
selectable->hasBackground = hasBackground;
if(selectable->hasBackground == 1){
selectable->backgroundTexture = backgroundTextures;
}
//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 = SELECTABLE_STATE_DEFAULT;
selectable->groupID = groupID;
selectable->unselectAfterExecute = unselectAfterExecute;
switch(selectable->id){
case SELECTABLE_ID_SPAWN_BUILDING:
selectable->onSelected = &OnSelectedSpawnBuilding;
break;
case SELECTABLE_ID_SPAWN_WORKER:
selectable->onSelected = &OnSelectedSpawnWorker;
break;
case SELECTABLE_ID_SPAWN_LUMBERJACK:
selectable->onSelected = &OnSelectedSpawnLumberjack;
break;
default:
selectable->onSelected = &OnSelectedSelectable;
printf("\n\n\n\n\n\n WARNING: Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id);
break;
}
return selectable;
}
void SelectableExecuteSelectable(Selectable *selectable, Game * game){
// CALL SELECTABLE ONCLICK
selectable->onSelected(game, selectable);
}
int SelectableUpdateSelectableState(Selectable * selectable, Game *game){
int mouseOnElement = 0;
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
){
mouseOnElement = 1;
game->mouseOnUI = 1;
}
if(selectable->state == SELECTABLE_STATE_SELECTED){
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
if(UNSELECT_SELECTABLE_ON_SELECT && mouseOnElement && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
return SELECTABLE_STATE_SELECTED;
}
if(mouseOnElement == 1){
if(IsMouseButtonPressed(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){
if(selectable->state == SELECTABLE_STATE_SELECTED){
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
return selectable->state;
}
void SelectableDrawSelectable(Selectable * selectable){
int targetWidth = 0;
if(selectable->hasBackground == 1) {
targetWidth = selectable->backgroundTexture[selectable->state]->width;
DrawTexture((*(selectable->backgroundTexture))[selectable->state], selectable->position.x, selectable->position.y, WHITE);
}
// ich weiss zwar nicht wieso genau aber das hier skaliert die Texturen richtig
int padding = 19;
Rectangle from = {0, 0 , selectable->texture->width, selectable->texture->height};
Rectangle to = {selectable->position.x +padding, selectable->position.y + padding, selectable->backgroundTexture[0]->width - 2*padding, selectable->backgroundTexture[0]->height - 2*padding};
DrawTexturePro(*selectable->texture, from, to, (Vector2){0,0}, 0.0f, WHITE);
if(selectable->state == SELECTABLE_STATE_SELECTED){
DrawTexture(*selectable->texture, GetMouseX() - selectable->texture->width / 2, GetMouseY() - selectable->texture->height*0.75, (Color){255, 255, 255, 150});
DrawRectangleLines(GetMouseX() - selectable->texture->width / 2, GetMouseY() - selectable->texture->height*0.75, selectable->texture->width, selectable->texture->height, GREEN);
}
if(selectable->showDescription == 1){
DrawText(selectable->description, selectable->position.x, selectable->position.y, selectable->fontSize, WHITE);
}
}