Selectable Elements working

main
Jan 3 years ago
parent 4d99781ea1
commit 088f401128

@ -1,6 +1,6 @@
CC = gcc
FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
OBJS = main.o sprite.o inputHandler.o isometricMap.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o mapobject.o entity.o
OBJS = main.o sprite.o inputHandler.o isometricMap.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o mapobject.o entity.o selectable.o
spiel: $(OBJS)
$(CC) -o spiel $(OBJS) $(FLAGS)
@ -44,5 +44,8 @@ entity.o: Entity/entity.c
mapobject.o: MapObject/mapobject.c
$(CC) -c MapObject/mapobject.c $(FLAGS)
selectable.o: Ui/selectable.c
$(CC) -c Ui/selectable.c $(FLAGS)
clean:
rm *.o spiel

@ -0,0 +1,82 @@
#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);
}
}

@ -0,0 +1,42 @@
#ifndef SELECTABLE_H_
#define SELECTABLE_H_
#include "raylib.h"
#include "../game.h"
typedef struct Selectable Selctable;
#define SELECTABLE_STATE_DEFAULT 0
#define SELECTABLE_STATE_HOVERED 1
#define SELECTABLE_STATE_SELECTED 2
#define SELECTABLE_ID_TEST 0
typedef struct Selectable{
Vector2 position; // Linke obere Ecke
Vector2 centerPosition; // Die Mitte des Selectables
Texture2D texture[3];// [0]: Normal | [1]: Hovered | [2]: Selected
Texture2D backgroundTexture[3]; // [0]: Normal | [1]: Hovered | [2]: Selected
int hasBackground; // 0: hat keine Background Textur | 1: hat Background Textur
char description[20]; //Text der unter dem selectable angezeigt werden kann
int showDescription; //0: zeigt Description nicht | 1: zeigt Description
int id; // Durch die ID wird dem Selectable eine Funktion zugeordnet
int state; // 0: not selected | 1: hovered | 2: selected
int fontSize; // FontSize kann für jede Selectable Description individuell festgelegt werden
}Selectable;
// Textures+backgroundTextures: [0]: Normal | [1]: Hovered | [2]: Selected
// hasBackground: 0: hat keine Background Textur | 1: hat Background Textur
// showDescription 0: zeigt Description nicht | 1: zeigt Description
// Max Description LEN 20
Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id);
void SelectableExecuteSelectable(Selectable *selectable, Game * game);
int SelectableUpdateSelectableState(Selectable * selectable);
int SelectableUnselectSelectable(Selectable * selectable);
void SelectableDrawSelectable(Selectable * selectable);
#endif

@ -5,6 +5,7 @@
#include "stdlib.h"
#include "stdio.h"
UiContainer * UiContainerInitPauseUiContainer(){
UiContainer *uiContainer = malloc(sizeof(UiContainer));
@ -54,8 +55,43 @@ UiContainer * UiContainerInitMainMenuUiContainer(){
}
UiContainer * UiContainerInitGameUiContainer(){
printf("\n\n\n\n UiContainerInitGameUiContainer not implemented yet!! \n\n\n\n");
return 0;
UiContainer *uiContainer = malloc(sizeof(UiContainer));
uiContainer->buttonCounter = 0;
Texture2D textures[3] = { LoadTexture("assets/selectable.png"), //DEFAULT
LoadTexture("assets/selectable.png"), //HOVERED
LoadTexture("assets/selectable.png") //SELECTED
};
Texture2D backgroundTextures[3] = { LoadTexture("assets/selectable_background.png"), //DEFAULT
LoadTexture("assets/selectable_background_hovered.png"), //HOVERED
LoadTexture("assets/selectable_background_selected.png") //SELECTED
};
Vector2 position = (Vector2){50, 250};
int showDescription = 0;
Selectable *selectable1 = SelectableInit(textures, backgroundTextures, 1, &position, "Building", showDescription, 9, 16, SELECTABLE_ID_TEST);
position.y += 100;
Selectable *selectable2 = SelectableInit(textures, backgroundTextures, 1,&position, "Building2", showDescription, 10, 16, SELECTABLE_ID_TEST);
position.y += 100;
Selectable *selectable3 = SelectableInit(textures, backgroundTextures, 1,&position, "Building3", showDescription, 10, 16, SELECTABLE_ID_TEST);
position.y += 100;
Selectable *selectable4 = SelectableInit(textures, backgroundTextures, 1,&position, "Building4", showDescription, 10, 16, SELECTABLE_ID_TEST);
position.x += 100;
Selectable *selectable5 = SelectableInit(textures, backgroundTextures, 1,&position, "Building5", showDescription, 10, 16, SELECTABLE_ID_TEST);
position.x += 100;
Selectable *selectable6 = SelectableInit(textures, backgroundTextures, 1,&position, "Building6", showDescription, 10, 16, SELECTABLE_ID_TEST);
uiContainer->selectables[0] = selectable1;
uiContainer->selectables[1] = selectable2;
uiContainer->selectables[2] = selectable3;
uiContainer->selectables[3] = selectable4;
uiContainer->selectables[4] = selectable5;
uiContainer->selectables[5] = selectable6;
uiContainer->selectableCounter = 6;
return uiContainer;
}
// Updated alle Buttons und führt gegebenenfalls deren Code aus
@ -67,6 +103,21 @@ void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){
ButtonExecuteButton(uiContainer->buttons[i], game);
}
}
for(i=0 ; i < uiContainer->selectableCounter; i++){
int setState = SelectableUpdateSelectableState(uiContainer->selectables[i]);
// Unselecting every selectable if one is selected
if(setState == SELECTABLE_STATE_SELECTED){
int j;
for(j=0 ; j < uiContainer->selectableCounter; j++){
if(i != j){
SelectableUnselectSelectable(uiContainer->selectables[j]);
}
}
}
if(uiContainer->selectables[i]->state == SELECTABLE_STATE_SELECTED){
SelectableExecuteSelectable(uiContainer->selectables[i], game);
}
}
}
// Drawed alle Buttons
@ -75,4 +126,7 @@ void UiContainerDrawUiContainer(UiContainer *uiContainer){
for(i=0 ; i < uiContainer->buttonCounter; i++){
ButtonDrawButton(uiContainer->buttons[i]);
}
for(i=0 ; i < uiContainer->selectableCounter; i++){
SelectableDrawSelectable(uiContainer->selectables[i]);
}
}

@ -3,11 +3,14 @@
#include "raylib.h"
#include "button.h"
#include "selectable.h"
#include "../game.h"
typedef struct UiContainer{
Button *buttons[15]; // Platz für max. 15 Buttons
int buttonCounter; // Zeigt wie viele Buttons der UiContainer aktuell schon speichert
Selectable *selectables[20]; // Platz für max 20 Selectables
int selectableCounter; // Zeigt wie viele Selectables der UiContainer aktuell schon speichert
} UiContainer;
void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game);

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 761 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 762 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 761 B

@ -23,6 +23,7 @@ int main(){
UiContainer *pauseScreenUiContainer = UiContainerInitPauseUiContainer();
UiContainer *mainMenuScreenUiContainer = UiContainerInitMainMenuUiContainer();
UiContainer *gameScreenUiContainer = UiContainerInitGameUiContainer();
// Hides the operating systems own cursor
HideCursor();
@ -64,6 +65,10 @@ int main(){
mouseInput(game);
keyboardInput(game->inputHandler, game->camera);
// Button / UI stuff
UiContainerUpdateUiContainer(gameScreenUiContainer, game);
UiContainerDrawUiContainer(gameScreenUiContainer);
if(IsKeyPressed(KEY_P)){
game->screen = SCREEN_PAUSE;
}

Loading…
Cancel
Save