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.

129 lines
5.4 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, float scale){
Selectable *selectable = malloc(sizeof(Selectable));
selectable->texture = texture;
selectable->hasBackground = hasBackground;
selectable->scale = scale;
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;
int i = 0;
if(selectable->id > SELECTABLE_ID_DRAWING_TILE && selectable->id < ISOMETRICMAP_TILE_TEXTURE_AMOUNT + SELECTABLE_ID_DRAWING_TILE){
selectable->onSelected = &OnSelectedDrawTile;
return selectable;
}
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;
case SELECTABLE_ID_DRAWING_TILE:
selectable->onSelected = &OnSelectedDrawTile;
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 * selectable->scale &&
GetMouseY() > selectable->position.y &&
GetMouseY() < selectable->position.y + (*selectable->backgroundTexture)[selectable->state].height * selectable->scale
){
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 padding = 19;
Rectangle from = {0, 0, selectable->backgroundTexture[0]->width, selectable->backgroundTexture[0]->height};
Rectangle to = {selectable->position.x, selectable->position.y , selectable->backgroundTexture[0]->width* selectable->scale, selectable->backgroundTexture[0]->height* selectable->scale};
if(selectable->hasBackground == 1) {
DrawTexturePro((*(selectable->backgroundTexture))[selectable->state], from, to, (Vector2){0,0}, 0.0f, WHITE);
}
from.width = selectable->texture->width;
from.height = selectable->texture->height;
to.x += padding * selectable->scale;
to.y += padding * selectable->scale;
to.width -= 2*padding * selectable->scale;
to.height -= 2*padding * selectable->scale;
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);
}
}