Lamthai/game/interface/ui_elements.cpp

188 lines
6.0 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 "../define.h"
#include "ui_elements.h"
2025-07-14 15:12:29 +02:00
Button create_button(Button_types type_arg, Button_sizes size_arg, const std::vector<float>& position_arg, std::string text_arg, const std::vector<float>& 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-14 14:38:08 +02:00
draw_text(button.text);
2025-07-12 17:28:49 +02:00
}
}
2025-07-14 15:12:29 +02:00
std::vector<float> button_size(Button_sizes size_arg, const std::vector<float>& screen_arg) {
2025-07-12 17:28:49 +02:00
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;
}
2025-07-14 14:38:08 +02:00
float text_size(Button_sizes size_arg) {
2025-07-12 17:28:49 +02:00
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
}
2025-07-14 14:38:08 +02:00
Color type_color(Stats type_arg) {
2025-07-12 23:31:14 +02:00
switch(type_arg) {
case HEALTH:
return RED;
break;
case SHIELD:
return GRAY;
break;
case SPEED:
return BLUE;
break;
default:
return YELLOW;
break;
}
}
2025-07-14 15:12:29 +02:00
Bar create_bar(Stats type_arg, int& max_arg, int& actual_arg, const std::vector<float>& position_arg, const Rectangle& container_arg) {
float width = container_arg.width * 0.9f;
float height = container_arg.height * 0.15f;
float position_x = container_arg.width * position_arg[0] + container_arg.x - width / 2;
float position_y = container_arg.height * position_arg[1] + container_arg.y - height / 2;
2025-07-12 23:31:14 +02:00
Rectangle complete = {position_x, position_y, width, height};
2025-07-14 14:38:08 +02:00
float progression_width = (width - 2.0f) * ((float)actual_arg / max_arg);
2025-07-12 23:31:14 +02:00
float progression_heigth = height - 2.0f;
Rectangle progression = {position_x+1, position_y+1, progression_width, progression_heigth};
Font font = GetFontDefault();
2025-07-14 15:12:29 +02:00
float font_size = container_arg.height * 0.12f;
2025-07-12 23:31:14 +02:00
std::string value = std::to_string(actual_arg) + "/" + std::to_string(max_arg);
2025-07-14 15:12:29 +02:00
float text_x = container_arg.width * position_arg[0] + container_arg.x - MeasureText(value.c_str(), font_size) / 2;
float text_y = container_arg.height * position_arg[1] + container_arg.y - font_size / 2;
2025-07-14 14:38:08 +02:00
Text text = {font, value, Vector2{text_x, text_y}, font_size, 2.0f, WHITE};
2025-07-12 23:31:14 +02:00
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);
2025-07-14 14:38:08 +02:00
draw_text(bar_arg.text);
}
2025-07-14 15:12:29 +02:00
Ennemy_stats create_ennemy_stats(Ennemy& ennemy_arg, const std::vector<float>& position_arg, const std::vector<float>& container_arg) {
std::vector<float> size = {container_arg[0] * 0.1f, container_arg[1] * 0.15f};
2025-07-14 14:38:08 +02:00
float position_x = container_arg[0] * position_arg[0] - size[0] / 2;
float position_y = container_arg[1] * position_arg[1] - size[1] / 2;
Rectangle card = {position_x, position_y, size[0], size[1]};
Font font = GetFontDefault();
float font_size = 15.0f;
std::string value = ennemy_arg.name;
2025-07-14 15:12:29 +02:00
float text_x = size[0] * 0.5f + position_x - MeasureText(value.c_str(), font_size) / 2;
float text_y = size[1] * 0.1f + position_y - font_size / 2;
Vector2 text_position = {text_x, text_y};
2025-07-14 14:38:08 +02:00
Text name = {font, value, text_position, font_size, 2.0f, YELLOW};
text_position.x += MeasureText(value.c_str(), font_size) + value.size() * 2;
font_size = 10.0f;
value = std::to_string(ennemy_arg.xp);
Text level = {font, value, text_position, font_size, 2.0f, YELLOW};
std::vector<float> health_position = {0.5f, 0.3f};
std::vector<float> shield_position = {0.5f, 0.5f};
std::vector<float> speed_position = {0.5f, 0.7f};
std::vector<float> tame_position = {0.5f, 0.9f};
2025-07-14 15:12:29 +02:00
Bar health = create_bar(HEALTH, ennemy_arg.max_health, ennemy_arg.health, health_position, card);
Bar shield = create_bar(SHIELD, ennemy_arg.max_shield, ennemy_arg.shield, shield_position, card);
Bar speed = create_bar(SPEED, ennemy_arg.max_speed, ennemy_arg.speed, speed_position, card);
Bar tame = create_bar(TAME, ennemy_arg.max_tame, ennemy_arg.tame, tame_position, card);
2025-07-14 14:38:08 +02:00
return Ennemy_stats{card, name, level, health, shield, speed, tame};
}
void draw_ennemy_stats(const Ennemy_stats& ennemy_stats_arg) {
DrawRectangleRec(ennemy_stats_arg.card, BLUE);
draw_text(ennemy_stats_arg.name);
draw_text(ennemy_stats_arg.level);
draw_bar(ennemy_stats_arg.health);
draw_bar(ennemy_stats_arg.shield);
draw_bar(ennemy_stats_arg.speed);
draw_bar(ennemy_stats_arg.tame);
2025-07-12 23:31:14 +02:00
}
2025-07-14 14:38:08 +02:00
void draw_text(const Text& text_arg) {
DrawTextEx(text_arg.font, text_arg.text.c_str(), text_arg.position, text_arg.fontSize, text_arg.spacing, text_arg.tint);
2025-07-12 17:28:49 +02:00
}