Lamthai/game/interface/ui_elements.cpp

261 lines
9.1 KiB
C++
Raw Permalink 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;
2025-07-16 19:45:21 +02:00
case MANA:
return GREEN;
break;
case ENERGY:
2025-07-12 23:31:14 +02:00
return YELLOW;
break;
2025-07-16 19:45:21 +02:00
case TAME:
return MAROON;
break;
default:
return BLACK;
break;
2025-07-12 23:31:14 +02:00
}
}
2025-07-16 18:31:15 +02:00
Bar create_bar(Stats type_arg, float max_arg, float actual_arg, const std::vector<float>& position_arg, const Rectangle& container_arg) {
2025-07-14 15:12:29 +02:00
float width = container_arg.width * 0.9f;
2025-07-16 19:45:21 +02:00
float height = container_arg.width * 0.12f;
2025-07-14 15:12:29 +02:00
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-16 18:31:15 +02:00
float progression_width = (width - 2.0f) * (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-16 19:45:21 +02:00
float font_size = height - 4.0f;
2025-07-16 18:31:15 +02:00
std::string value = std::to_string((int)actual_arg) + "/" + std::to_string((int)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};
}
2025-07-16 18:31:15 +02:00
void actualise_bar_progression(Bar& bar_arg, float actual_arg) {
if(bar_arg.actual != actual_arg) {
bar_arg.actual = actual_arg;
bar_arg.progression.width = (bar_arg.complete.width - 2.0f) * (bar_arg.actual / bar_arg.max);
bar_arg.text.text = std::to_string((int)bar_arg.actual) + "/" + std::to_string((int)bar_arg.max);
}
}
void draw_bar(Bar& bar_arg, float actual_arg) {
actualise_bar_progression(bar_arg, actual_arg);
2025-07-12 23:31:14 +02:00
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};
}
2025-07-16 18:31:15 +02:00
void draw_ennemy_stats(Ennemy_stats& ennemy_stats_arg, Ennemy& ennemy_arg) {
2025-07-16 19:45:21 +02:00
DrawRectangleRec(ennemy_stats_arg.card, PURPLE);
2025-07-14 14:38:08 +02:00
draw_text(ennemy_stats_arg.name);
draw_text(ennemy_stats_arg.level);
2025-07-16 18:31:15 +02:00
draw_bar(ennemy_stats_arg.health, ennemy_arg.health);
draw_bar(ennemy_stats_arg.shield, ennemy_arg.shield);
draw_bar(ennemy_stats_arg.speed, ennemy_arg.speed);
draw_bar(ennemy_stats_arg.tame, ennemy_arg.tame);
2025-07-12 23:31:14 +02:00
}
2025-07-16 19:45:21 +02:00
Player_stats create_player_stats(Player& player_arg, const std::vector<float>& container_arg) {
std::vector<float> size = {container_arg[0] * 0.1f, container_arg[1] * 0.2f};
float position_x = container_arg[0] * 0.25 - size[0] / 2;
float position_y = container_arg[1] * 0.25 - size[1] / 2;
Rectangle card = {position_x, position_y, size[0], size[1]};
Font font = GetFontDefault();
float font_size = 15.0f;
std::string value = player_arg.name;
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};
Text name = {font, value, text_position, font_size, 2.0f, YELLOW};
text_position.x += MeasureText(value.c_str(), font_size) + value.size() * 2;
value = std::to_string(player_arg.xp);
Text level = {font, value, text_position, font_size, 2.0f, YELLOW};
std::vector<std::string> jobs = {"Mage", "Alchimist", "Warrior", "Engineer", "Archer", "Tamer"};
value = jobs[player_arg.job];
text_position.x = size[0] * 0.5f + position_x - MeasureText(value.c_str(), font_size) / 2;
text_position.y = size[1] * 0.225f + position_y - font_size / 2;
Text job = {font, value, text_position, font_size, 2.0f, YELLOW};
std::vector<float> health_position = {0.5f, 0.36f};
std::vector<float> shield_position = {0.5f, 0.50f};
std::vector<float> speed_position = {0.5f, 0.64f};
std::vector<float> mana_position = {0.5f, 0.78f};
std::vector<float> energy_position = {0.5f, 0.92f};
Bar health = create_bar(HEALTH, player_arg.max_health, player_arg.health, health_position, card);
Bar shield = create_bar(SHIELD, player_arg.max_shield, player_arg.shield, shield_position, card);
Bar speed = create_bar(SPEED, player_arg.max_speed, player_arg.speed, speed_position, card);
Bar mana = create_bar(MANA, player_arg.max_mana, player_arg.mana, mana_position, card);
Bar energy = create_bar(ENERGY, player_arg.max_energy, player_arg.energy, energy_position, card);
return Player_stats {card, name, level, job, health, shield, speed, mana, energy};
}
void draw_player_stats(Player_stats& player_stats_arg, Player& player_arg) {
DrawRectangleRec(player_stats_arg.card, BLUE);
draw_text(player_stats_arg.name);
draw_text(player_stats_arg.level);
draw_text(player_stats_arg.job);
draw_bar(player_stats_arg.health, player_arg.health);
draw_bar(player_stats_arg.shield, player_arg.shield);
draw_bar(player_stats_arg.speed, player_arg.speed);
draw_bar(player_stats_arg.mana, player_arg.mana);
draw_bar(player_stats_arg.energy, player_arg.energy);
}
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
}