commit
9ad56ed6bb
@ -0,0 +1,75 @@
|
||||
#include "button.h"
|
||||
#include "../game.h"
|
||||
#include "screenIDs.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "raylib.h"
|
||||
#include "string.h"
|
||||
|
||||
Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int fontSize, int id){
|
||||
Button *button = malloc(sizeof(Button));
|
||||
|
||||
button->textures[0] = textures[0];
|
||||
button->textures[1] = textures[1];
|
||||
button->textures[2] = textures[2];
|
||||
button->textures[3] = textures[3];
|
||||
|
||||
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 = BUTTON_STATE_DEFAULT;
|
||||
button->id = id;
|
||||
|
||||
button->fontSize = fontSize;
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
void ButtonExecuteButton(Button *button, Game *game){
|
||||
button->state = BUTTON_STATE_DEFAULT;
|
||||
switch(button->id){
|
||||
case BUTTON_ID_CONTINUE: // continue game
|
||||
game->screen = SCREEN_GAME;
|
||||
break;
|
||||
case BUTTON_ID_EXIT:
|
||||
// wahrscheinlich kein guter stil, es muss noch zeug freigegeben werden oder soos... keine Ahnung
|
||||
game->screen = SCREEN_EXIT;
|
||||
break;
|
||||
case BUTTON_ID_START_GAME:
|
||||
game->screen = SCREEN_GAME;
|
||||
break;
|
||||
default:
|
||||
printf("\n\n\n\n\n\n Unsupported Button ID %d \n\n\n\n\n\n", button->id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonDrawButton(Button * button){
|
||||
// erst Button Texture, dann Text zentriert drauf
|
||||
DrawTexture(button->textures[button->state], button->position.x, button->position.y, WHITE);
|
||||
DrawText(button->text, button->centerPosition.x - MeasureText(button->text, button->fontSize)/2, button->centerPosition.y - button->fontSize/2, button->fontSize, BLACK);
|
||||
}
|
||||
|
||||
int ButtonUpdateButtonState(Button * button){
|
||||
if(button->state == BUTTON_STATE_RELEASED){
|
||||
// Wir verlassen den RELEASED State automatisch wenn wir den Code des Buttons ausführen, siehe ButtonExecuteButton
|
||||
// So lange der Code nicht ausgeführt wurde bleiben wir im state damit er definitiv im nächsten Frame ausgeführt wird
|
||||
return button->state;
|
||||
}
|
||||
else 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(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){
|
||||
return button->state = BUTTON_STATE_PRESSED;
|
||||
}
|
||||
else if(button->state == BUTTON_STATE_PRESSED){
|
||||
return button->state = BUTTON_STATE_RELEASED;
|
||||
}
|
||||
return button->state = BUTTON_STATE_HOVERED;
|
||||
}
|
||||
return button->state = BUTTON_STATE_DEFAULT;
|
||||
}
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
#ifndef BUTTONS_H_
|
||||
#define BUTTONS_H_
|
||||
|
||||
#include "raylib.h"
|
||||
#include "../game.h"
|
||||
|
||||
#define BUTTON_STATE_DEFAULT 0 // button is just there
|
||||
#define BUTTON_STATE_HOVERED 1 // mouse is being hovered over the button
|
||||
#define BUTTON_STATE_PRESSED 2 // left mouse button is down while hovering the button
|
||||
#define BUTTON_STATE_RELEASED 3 // left mouse button is released while hovering the button, button code will be executed
|
||||
|
||||
#define BUTTON_ID_CONTINUE 0 // going to game screen, supposed to be used from pause screen
|
||||
#define BUTTON_ID_EXIT 1 // closing the game using exit code 0
|
||||
#define BUTTON_ID_START_GAME 2 // going to game screen, supposed to be used from mainmenu screen
|
||||
|
||||
typedef struct Button{
|
||||
Texture2D textures[4]; // [0]: Normal [1]: Hovered [2]: Pressed [3]: Released
|
||||
Vector2 position; // Linke obere Ecke des Buttons
|
||||
Vector2 centerPosition; // Die Mitte des Buttons
|
||||
char text[20]; // Text, den der Button zentriert anzeigt, aktuell max. 19 Zeichen
|
||||
int state; // 0: default 1: hovered 2: pressed 3: released
|
||||
int id; // Durch die ID wird dem Button eine Funktion zugeordnet
|
||||
int fontSize; // FontSize kann für jeden Button individuell festgelegt werden
|
||||
} Button;
|
||||
|
||||
// Textures: [0]: Normal [1]: Hovered [2]: Pressed [3]: Released
|
||||
Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int fontSize, int id);
|
||||
|
||||
// executes the logic of one button of certain id - huge switch?
|
||||
void ButtonExecuteButton(Button *button, Game * game);
|
||||
|
||||
int ButtonUpdateButtonState(Button * button);
|
||||
|
||||
void ButtonDrawButton(Button * button);
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,42 @@
|
||||
#include "debug.h"
|
||||
#include "../game.h"
|
||||
#include "uiContainer.h"
|
||||
#include "string.h"
|
||||
#include <stdio.h>
|
||||
#include "../Input/inputHandler.h"
|
||||
|
||||
void DebugDraw(Game *game, UiContainer *uiContainer){
|
||||
|
||||
char strings[30][41]; // Platz für bis zu 30 Strings der Länge 40
|
||||
int lineamount = 0; // sollte aktuell gehalten werden, wie viele Lines sind aktuell im Strings Array
|
||||
int neededWidth = MeasureText("1234567890123456789012345", DEBUG_FONT_SIZE); // sollte an den längsten debug String angepasst werden, damit dieser noch reinpasst
|
||||
|
||||
// Hier die Debug Information in den Strings Array einfügen
|
||||
// im Endeffekt einfach im Array an der Stelle lineamount++ die Elemente einfügen
|
||||
sprintf(strings[lineamount++], "Screen: %d", game->screen);
|
||||
sprintf(strings[lineamount++], "MouseScreenX: %d", GetMouseX());
|
||||
sprintf(strings[lineamount++], "MouseScreenY: %d", GetMouseY());
|
||||
sprintf(strings[lineamount++], "MouseWorldX: %d", (int)game->inputHandler->cursorWorldPos.x);
|
||||
sprintf(strings[lineamount++], "MouseWorldY: %d", (int)game->inputHandler->cursorWorldPos.y);
|
||||
sprintf(strings[lineamount++], "Selected Layer: %d", game->inputHandler->selectedLayer);
|
||||
sprintf(strings[lineamount++], "DEPTH: %d", (int)(game->inputHandler->cursorWorldPos.x + game->inputHandler->cursorWorldPos.y + game->inputHandler->selectedLayer));
|
||||
sprintf(strings[lineamount++], "Camera Zoom: %f", game->camera->zoom);
|
||||
|
||||
// Hier müssten wir eine bessere Lösung finden, das flackert weil pressed nur für einen Frame gilt. Eine ähnliche Funktion gibt es für CharDown leider nicht, müssten wir selbst programmieren. Ich habe es erstmal nicht auskommentiert. Kann man aber easy machen sollte es stören
|
||||
int pressed = GetCharPressed();
|
||||
while(pressed != 0){
|
||||
sprintf(strings[lineamount++], "Ḱey Pressed: %c", pressed);
|
||||
pressed = GetCharPressed();
|
||||
}
|
||||
|
||||
// Drawed eine Box für die Debug Info
|
||||
DrawRectangleLines(0, 0, neededWidth, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 255});
|
||||
DrawRectangle(0, 0, neededWidth, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 150});
|
||||
|
||||
// Drawed den Inhalt des "strings" Arrays
|
||||
int counter = 0;
|
||||
while(counter < lineamount){
|
||||
DrawText(strings[counter], 2, counter * DEBUG_FONT_SIZE + 2, DEBUG_FONT_SIZE, (Color){220, 25, 25, 255});
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
#ifndef DEBUG_H_
|
||||
#define DEBUG_H_
|
||||
|
||||
#include "../game.h"
|
||||
#include "uiContainer.h"
|
||||
|
||||
#define DEBUG_FONT_SIZE 20 // Font size of the Debug window
|
||||
|
||||
// Drawed das Debug Fenster in die obere Linke Ecke
|
||||
void DebugDraw(Game *game, UiContainer *uiContainer);
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,10 @@
|
||||
#ifndef SCREENIDS_H_
|
||||
#define SCREENIDS_H_
|
||||
|
||||
#define SCREEN_EXIT 0 // Will exit the game using code 0
|
||||
#define SCREEN_MAINMENU 1
|
||||
#define SCREEN_OPTIONS 2
|
||||
#define SCREEN_GAME 3
|
||||
#define SCREEN_PAUSE 4
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,78 @@
|
||||
#include "uiContainer.h"
|
||||
#include "../game.h"
|
||||
#include "button.h"
|
||||
#include "raylib.h"
|
||||
#include "stdlib.h"
|
||||
#include "stdio.h"
|
||||
|
||||
UiContainer * UiContainerInitPauseUiContainer(){
|
||||
UiContainer *uiContainer = malloc(sizeof(UiContainer));
|
||||
|
||||
Texture2D textures[4] = { LoadTexture("assets/button.png"), //DEFAULT
|
||||
LoadTexture("assets/button_hovered.png"), //HOVERED
|
||||
LoadTexture("assets/button_pressed.png"), //PRESSED
|
||||
LoadTexture("assets/button_pressed.png")}; //RELEASED
|
||||
Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50};
|
||||
int buttonFontSize = 36;
|
||||
|
||||
Button *continuebutton = ButtonInitButton(textures, &position, "Continue", 9, buttonFontSize, BUTTON_ID_CONTINUE);
|
||||
uiContainer->buttons[0] = continuebutton;
|
||||
uiContainer->buttonCounter = 1;
|
||||
|
||||
position.y += 250;
|
||||
|
||||
Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5, buttonFontSize, BUTTON_ID_EXIT);
|
||||
uiContainer->buttons[1] = exitButton;
|
||||
uiContainer->buttonCounter = 2;
|
||||
|
||||
// Methode funktioniert wieso auch immer auch ohne dieses return. C returned Implizit odder was O_o
|
||||
return uiContainer;
|
||||
}
|
||||
|
||||
UiContainer * UiContainerInitMainMenuUiContainer(){
|
||||
UiContainer *uiContainer = malloc(sizeof(UiContainer));
|
||||
|
||||
Texture2D textures[4] = { LoadTexture("assets/button.png"), //DEFAULT
|
||||
LoadTexture("assets/button_hovered.png"), //HOVERED
|
||||
LoadTexture("assets/button_pressed.png"), //PRESSED
|
||||
LoadTexture("assets/button_pressed.png")}; //RELEASED
|
||||
Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50};
|
||||
int buttonFontSize = 36;
|
||||
|
||||
Button *continuebutton = ButtonInitButton(textures, &position, "Start Game", 11, buttonFontSize, BUTTON_ID_START_GAME);
|
||||
uiContainer->buttons[0] = continuebutton;
|
||||
uiContainer->buttonCounter = 1;
|
||||
|
||||
position.y += 250;
|
||||
|
||||
Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5, buttonFontSize, BUTTON_ID_EXIT);
|
||||
uiContainer->buttons[1] = exitButton;
|
||||
uiContainer->buttonCounter = 2;
|
||||
|
||||
// Methode funktioniert wieso auch immer auch ohne dieses return. C returned Implizit odder was O_o
|
||||
return uiContainer;
|
||||
}
|
||||
|
||||
UiContainer * UiContainerInitGameUiContainer(){
|
||||
printf("\n\n\n\n UiContainerInitGameUiContainer not implemented yet!! \n\n\n\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Updated alle Buttons und führt gegebenenfalls deren Code aus
|
||||
void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){
|
||||
int i = 0;
|
||||
for(i=0 ; i < uiContainer->buttonCounter; i++){
|
||||
ButtonUpdateButtonState(uiContainer->buttons[i]);
|
||||
if(uiContainer->buttons[i]->state == BUTTON_STATE_RELEASED){
|
||||
ButtonExecuteButton(uiContainer->buttons[i], game);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Drawed alle Buttons
|
||||
void UiContainerDrawUiContainer(UiContainer *uiContainer){
|
||||
int i = 0;
|
||||
for(i=0 ; i < uiContainer->buttonCounter; i++){
|
||||
ButtonDrawButton(uiContainer->buttons[i]);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
#ifndef UICONTAINER_H_
|
||||
#define UICONTAINER_H_
|
||||
|
||||
#include "raylib.h"
|
||||
#include "button.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
|
||||
} UiContainer;
|
||||
|
||||
void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game);
|
||||
void UiContainerDrawUiContainer(UiContainer *uiContainer);
|
||||
|
||||
UiContainer * UiContainerInitPauseUiContainer();
|
||||
UiContainer * UiContainerInitGameUiContainer();
|
||||
UiContainer * UiContainerInitMainMenuUiContainer();
|
||||
|
||||
#endif
|
||||
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 8.3 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
Loading…
Reference in new issue