Lamthai/game/interface/ui_elements.cpp

147 lines
4.1 KiB
C++
Raw Normal View History

2025-07-12 23:31:14 +02:00
#include <iostream>
2025-07-12 17:28:49 +02:00
#include <string>
#include <vector>
#include <raylib.h>
#include "../logic/structs.h"
#include "../define.h"
#include "ui_elements.h"
2025-07-12 23:31:14 +02:00
Button create_button(int type_arg, int size_arg, const std::vector<float>& position_arg, std::string text_arg, const std::vector<int>& screen_arg) {
2025-07-12 17:28:49 +02:00
std::vector<float> size = button_size(size_arg, screen_arg);
float position_x = screen_arg[0] * position_arg[0] - size[0] / 2;
float position_y = screen_arg[1] * position_arg[1] - size[1] / 2;
Font font = GetFontDefault();
float font_size = text_size(size_arg);
2025-07-12 23:31:14 +02:00
float text_x = screen_arg[0] * position_arg[0] - MeasureText(text_arg.c_str(), font_size) / 2;
2025-07-12 17:28:49 +02:00
float text_y = screen_arg[1] * position_arg[1] - font_size / 2;
Text text;
switch(type_arg) {
case BUTTON_BASIC:
text = {font, text_arg,Vector2{text_x, text_y}, font_size, 2.0f, BLACK};
return Button{
Rectangle{position_x, position_y, size[0], size[1]},
Color{BLUE},
text
};
break;
case BUTTON_INACTIVE:
text = {font, text_arg,Vector2{text_x, text_y}, font_size, 2.0f, BLACK};
return Button{
Rectangle{position_x, position_y, size[0], size[1]},
Color{GRAY},
text
};
break;
case BUTTON_DANGER:
default:
text = {font, text_arg,Vector2{text_x, text_y}, font_size, 2.0f, WHITE};
return Button{
Rectangle{position_x, position_y, size[0], size[1]},
Color{RED},
text
};
break;
}
}
void draw_buttons(const std::vector<Button>& buttons_arg) {
for(Button button: buttons_arg) {
DrawRectangleRec(button.rectangle, button.color);
2025-07-12 23:31:14 +02:00
DrawTextEx(button.text.font, button.text.text.c_str(), button.text.position, button.text.fontSize, button.text.spacing, button.text.tint);
2025-07-12 17:28:49 +02:00
}
}
std::vector<float> button_size(int size_arg, const std::vector<int>& screen_arg) {
std::vector<float> size;
switch(size_arg) {
case BUTTON_BIG:
size.push_back(screen_arg[0]/8);
size.push_back(screen_arg[1]/8);
break;
case BUTTON_MEDIUM:
size.push_back(screen_arg[0]/16);
size.push_back(screen_arg[1]/16);
break;
case BUTTON_SMALL:
default:
size.push_back(screen_arg[0]/32);
size.push_back(screen_arg[1]/32);
break;
}
return size;
}
float text_size(int size_arg) {
switch(size_arg) {
case BUTTON_BIG:
return 25.0f;
break;
case BUTTON_MEDIUM:
return 20.0f;
break;
case BUTTON_SMALL:
default:
return 15.0f;
}
2025-07-12 23:31:14 +02:00
}
Color type_color(int type_arg) {
switch(type_arg) {
case HEALTH:
return RED;
break;
case SHIELD:
return GRAY;
break;
case SPEED:
return BLUE;
break;
default:
return YELLOW;
break;
}
}
Bar create_bar(int type_arg, int& max_arg, int& actual_arg, const std::vector<float>& position_arg, const std::vector<int>& screen_arg) {
float width = 150.0f;
float height = 15.0f;
float position_x = screen_arg[0] * position_arg[0] - width / 2;
float position_y = screen_arg[1] * position_arg[1] - height / 2;
Rectangle complete = {position_x, position_y, width, height};
float progression_width = (width - 2.0f) * (actual_arg / max_arg);
float progression_heigth = height - 2.0f;
Rectangle progression = {position_x+1, position_y+1, progression_width, progression_heigth};
Font font = GetFontDefault();
float font_size = 10.0f;
std::string value = std::to_string(actual_arg) + "/" + std::to_string(max_arg);
float text_x = screen_arg[0] * position_arg[0] - MeasureText(value.c_str(), font_size) / 2;
float text_y = screen_arg[1] * position_arg[1] - font_size / 2;
Text text = {font, value, Vector2{text_x, text_y}, font_size, 2.0f, YELLOW};
Color color = type_color(type_arg);
return Bar{complete, progression, max_arg, actual_arg, text, color};
}
void draw_bar(const Bar& bar_arg) {
DrawRectangleRec(bar_arg.complete, WHITE);
DrawRectangleRec(bar_arg.progression, bar_arg.color);
DrawTextEx(bar_arg.text.font, bar_arg.text.text.c_str(), bar_arg.text.position, bar_arg.text.fontSize, bar_arg.text.spacing, bar_arg.text.tint);
}
Ennemy_stats create_bar(const Ennemy& ennemy_arg, const std::vector<float>& position_arg, const std::vector<int>& screen_arg) {
return Ennemy_stats{};
2025-07-12 17:28:49 +02:00
}