feat(render): halo neon proporcional al bounding_radius de la shape (opt-out a text)
This commit is contained in:
+138
-123
@@ -4,156 +4,171 @@
|
||||
#include "core/graphics/shape.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
Shape::Shape(const std::string& filepath)
|
||||
: center_({.x = 0.0F, .y = 0.0F}),
|
||||
|
||||
nom_("unnamed") {
|
||||
load(filepath);
|
||||
}
|
||||
Shape::Shape(const std::string& filepath)
|
||||
: center_({.x = 0.0F, .y = 0.0F}),
|
||||
|
||||
auto Shape::load(const std::string& filepath) -> bool {
|
||||
// Llegir file
|
||||
std::ifstream file(filepath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "[Shape] Error: no es pot obrir " << filepath << '\n';
|
||||
return false;
|
||||
nom_("unnamed") {
|
||||
load(filepath);
|
||||
}
|
||||
|
||||
// Llegir todo el contingut
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
std::string contingut = buffer.str();
|
||||
file.close();
|
||||
|
||||
// Parsejar
|
||||
return parseFile(contingut);
|
||||
}
|
||||
|
||||
auto Shape::parseFile(const std::string& contingut) -> bool {
|
||||
std::istringstream iss(contingut);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
// Trim whitespace
|
||||
line = trim(line);
|
||||
|
||||
// Skip comments and blanks
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
auto Shape::load(const std::string& filepath) -> bool {
|
||||
// Llegir file
|
||||
std::ifstream file(filepath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "[Shape] Error: no es pot obrir " << filepath << '\n';
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse command
|
||||
if (startsWith(line, "name:")) {
|
||||
nom_ = trim(extractValue(line));
|
||||
} else if (startsWith(line, "scale:")) {
|
||||
try {
|
||||
escala_defecte_ = std::stof(extractValue(line));
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: scale invàlida, usant 1.0" << '\n';
|
||||
escala_defecte_ = 1.0F;
|
||||
// Llegir todo el contingut
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
std::string contingut = buffer.str();
|
||||
file.close();
|
||||
|
||||
// Parsejar
|
||||
return parseFile(contingut);
|
||||
}
|
||||
|
||||
auto Shape::parseFile(const std::string& contingut) -> bool {
|
||||
std::istringstream iss(contingut);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
// Trim whitespace
|
||||
line = trim(line);
|
||||
|
||||
// Skip comments and blanks
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
} else if (startsWith(line, "center:")) {
|
||||
parseCenter(extractValue(line));
|
||||
} else if (startsWith(line, "polyline:")) {
|
||||
auto points = parsePoints(extractValue(line));
|
||||
if (points.size() >= 2) {
|
||||
primitives_.push_back({PrimitiveType::POLYLINE, points});
|
||||
} else {
|
||||
std::cerr << "[Shape] Warning: polyline con menys de 2 points ignorada"
|
||||
<< '\n';
|
||||
|
||||
// Parse command
|
||||
if (startsWith(line, "name:")) {
|
||||
nom_ = trim(extractValue(line));
|
||||
} else if (startsWith(line, "scale:")) {
|
||||
try {
|
||||
escala_defecte_ = std::stof(extractValue(line));
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: scale invàlida, usant 1.0" << '\n';
|
||||
escala_defecte_ = 1.0F;
|
||||
}
|
||||
} else if (startsWith(line, "center:")) {
|
||||
parseCenter(extractValue(line));
|
||||
} else if (startsWith(line, "polyline:")) {
|
||||
auto points = parsePoints(extractValue(line));
|
||||
if (points.size() >= 2) {
|
||||
primitives_.push_back({PrimitiveType::POLYLINE, points});
|
||||
} else {
|
||||
std::cerr << "[Shape] Warning: polyline con menys de 2 points ignorada"
|
||||
<< '\n';
|
||||
}
|
||||
} else if (startsWith(line, "line:")) {
|
||||
auto points = parsePoints(extractValue(line));
|
||||
if (points.size() == 2) {
|
||||
primitives_.push_back({PrimitiveType::LINE, points});
|
||||
} else {
|
||||
std::cerr << "[Shape] Warning: line ha de tenir exactament 2 points"
|
||||
<< '\n';
|
||||
}
|
||||
}
|
||||
} else if (startsWith(line, "line:")) {
|
||||
auto points = parsePoints(extractValue(line));
|
||||
if (points.size() == 2) {
|
||||
primitives_.push_back({PrimitiveType::LINE, points});
|
||||
} else {
|
||||
std::cerr << "[Shape] Warning: line ha de tenir exactament 2 points"
|
||||
<< '\n';
|
||||
// Comandes desconegudes ignorades silenciosament
|
||||
}
|
||||
|
||||
if (primitives_.empty()) {
|
||||
std::cerr << "[Shape] Error: sin primitiva carregada" << '\n';
|
||||
return false;
|
||||
}
|
||||
|
||||
bounding_radius_ = computeBoundingRadius(primitives_, center_);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Shape::computeBoundingRadius(const std::vector<ShapePrimitive>& primitives,
|
||||
const Vec2& center) -> float {
|
||||
float max_dist_sq = 0.0F;
|
||||
for (const auto& prim : primitives) {
|
||||
for (const auto& p : prim.points) {
|
||||
const float DX = p.x - center.x;
|
||||
const float DY = p.y - center.y;
|
||||
max_dist_sq = std::max(max_dist_sq, (DX * DX) + (DY * DY));
|
||||
}
|
||||
}
|
||||
// Comandes desconegudes ignorades silenciosament
|
||||
return std::sqrt(max_dist_sq);
|
||||
}
|
||||
|
||||
if (primitives_.empty()) {
|
||||
std::cerr << "[Shape] Error: sin primitiva carregada" << '\n';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper: trim whitespace
|
||||
auto Shape::trim(const std::string& str) -> std::string {
|
||||
const char* whitespace = " \t\n\r";
|
||||
size_t start = str.find_first_not_of(whitespace);
|
||||
if (start == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
size_t end = str.find_last_not_of(whitespace);
|
||||
return str.substr(start, end - start + 1);
|
||||
}
|
||||
|
||||
// Helper: startsWith
|
||||
auto Shape::startsWith(const std::string& str,
|
||||
const std::string& prefix) -> bool {
|
||||
if (str.length() < prefix.length()) {
|
||||
return false;
|
||||
}
|
||||
return str.starts_with(prefix);
|
||||
}
|
||||
|
||||
// Helper: extract value after ':'
|
||||
auto Shape::extractValue(const std::string& line) -> std::string {
|
||||
size_t colon = line.find(':');
|
||||
if (colon == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
return line.substr(colon + 1);
|
||||
}
|
||||
|
||||
// Helper: parse center "x, y"
|
||||
void Shape::parseCenter(const std::string& value) {
|
||||
std::string val = trim(value);
|
||||
size_t comma = val.find(',');
|
||||
if (comma != std::string::npos) {
|
||||
try {
|
||||
center_.x = std::stof(trim(val.substr(0, comma)));
|
||||
center_.y = std::stof(trim(val.substr(comma + 1)));
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: centro invàlid, usant (0,0)" << '\n';
|
||||
center_ = {.x = 0.0F, .y = 0.0F};
|
||||
// Helper: trim whitespace
|
||||
auto Shape::trim(const std::string& str) -> std::string {
|
||||
const char* whitespace = " \t\n\r";
|
||||
size_t start = str.find_first_not_of(whitespace);
|
||||
if (start == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
size_t end = str.find_last_not_of(whitespace);
|
||||
return str.substr(start, end - start + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper: parse points "x1,y1 x2,y2 x3,y3"
|
||||
auto Shape::parsePoints(const std::string& str) -> std::vector<Vec2> {
|
||||
std::vector<Vec2> points;
|
||||
std::istringstream iss(trim(str));
|
||||
std::string pair;
|
||||
// Helper: startsWith
|
||||
auto Shape::startsWith(const std::string& str,
|
||||
const std::string& prefix) -> bool {
|
||||
if (str.length() < prefix.length()) {
|
||||
return false;
|
||||
}
|
||||
return str.starts_with(prefix);
|
||||
}
|
||||
|
||||
while (iss >> pair) { // Whitespace-separated
|
||||
size_t comma = pair.find(',');
|
||||
// Helper: extract value after ':'
|
||||
auto Shape::extractValue(const std::string& line) -> std::string {
|
||||
size_t colon = line.find(':');
|
||||
if (colon == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
return line.substr(colon + 1);
|
||||
}
|
||||
|
||||
// Helper: parse center "x, y"
|
||||
void Shape::parseCenter(const std::string& value) {
|
||||
std::string val = trim(value);
|
||||
size_t comma = val.find(',');
|
||||
if (comma != std::string::npos) {
|
||||
try {
|
||||
float x = std::stof(pair.substr(0, comma));
|
||||
float y = std::stof(pair.substr(comma + 1));
|
||||
points.push_back({x, y});
|
||||
center_.x = std::stof(trim(val.substr(0, comma)));
|
||||
center_.y = std::stof(trim(val.substr(comma + 1)));
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: point invàlid ignorat: " << pair
|
||||
<< '\n';
|
||||
std::cerr << "[Shape] Warning: centro invàlid, usant (0,0)" << '\n';
|
||||
center_ = {.x = 0.0F, .y = 0.0F};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
// Helper: parse points "x1,y1 x2,y2 x3,y3"
|
||||
auto Shape::parsePoints(const std::string& str) -> std::vector<Vec2> {
|
||||
std::vector<Vec2> points;
|
||||
std::istringstream iss(trim(str));
|
||||
std::string pair;
|
||||
|
||||
while (iss >> pair) { // Whitespace-separated
|
||||
size_t comma = pair.find(',');
|
||||
if (comma != std::string::npos) {
|
||||
try {
|
||||
float x = std::stof(pair.substr(0, comma));
|
||||
float y = std::stof(pair.substr(comma + 1));
|
||||
points.push_back({x, y});
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: point invàlid ignorat: " << pair
|
||||
<< '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
} // namespace Graphics
|
||||
|
||||
@@ -11,21 +11,21 @@
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Tipo de primitiva dins de una shape
|
||||
enum class PrimitiveType : std::uint8_t {
|
||||
POLYLINE, // Secuencia de points connectats
|
||||
LINE // Línia individual (2 points)
|
||||
};
|
||||
// Tipo de primitiva dins de una shape
|
||||
enum class PrimitiveType : std::uint8_t {
|
||||
POLYLINE, // Secuencia de points connectats
|
||||
LINE // Línia individual (2 points)
|
||||
};
|
||||
|
||||
// Primitiva individual (polyline o line)
|
||||
struct ShapePrimitive {
|
||||
// Primitiva individual (polyline o line)
|
||||
struct ShapePrimitive {
|
||||
PrimitiveType type;
|
||||
std::vector<Vec2> points; // 2+ points per polyline, exactament 2 per line
|
||||
};
|
||||
};
|
||||
|
||||
// Clase Shape - representa una shape vectorial carregada desde .shp
|
||||
class Shape {
|
||||
public:
|
||||
// Clase Shape - representa una shape vectorial carregada desde .shp
|
||||
class Shape {
|
||||
public:
|
||||
// Constructors
|
||||
Shape() = default;
|
||||
explicit Shape(const std::string& filepath);
|
||||
@@ -42,18 +42,22 @@ class Shape {
|
||||
}
|
||||
[[nodiscard]] auto getCenter() const -> const Vec2& { return center_; }
|
||||
[[nodiscard]] auto getDefaultScale() const -> float { return escala_defecte_; }
|
||||
// Distància màx. del center_ al vèrtex més llunyà; ús: dimensionar
|
||||
// efectes proporcionals a la mida de la shape (halos, glow).
|
||||
[[nodiscard]] auto getBoundingRadius() const -> float { return bounding_radius_; }
|
||||
[[nodiscard]] auto isValid() const -> bool { return !primitives_.empty(); }
|
||||
|
||||
// Info de depuració
|
||||
[[nodiscard]] auto getName() const -> const std::string& { return nom_; }
|
||||
[[nodiscard]] auto getNumPrimitives() const -> size_t { return primitives_.size(); }
|
||||
|
||||
private:
|
||||
private:
|
||||
std::vector<ShapePrimitive> primitives_;
|
||||
Vec2 center_; // Centro/origin de la shape
|
||||
float escala_defecte_{1.0F}; // Escala per defecte (normalment 1.0). Inicializada para
|
||||
// que el ctor por defecto no deje el campo indeterminado.
|
||||
std::string nom_; // Nom de la shape (per depuració)
|
||||
Vec2 center_; // Centro/origin de la shape
|
||||
float escala_defecte_{1.0F}; // Escala per defecte (normalment 1.0). Inicializada para
|
||||
// que el ctor por defecto no deje el campo indeterminado.
|
||||
float bounding_radius_{0.0F}; // Distància màx. del center_ al vèrtex més llunyà.
|
||||
std::string nom_; // Nom de la shape (per depuració)
|
||||
|
||||
// Helpers privats per parsejar. Son estáticos: no necesitan estado
|
||||
// de instancia, trabajan sobre el string pasado por parámetro.
|
||||
@@ -62,6 +66,9 @@ class Shape {
|
||||
[[nodiscard]] static auto extractValue(const std::string& line) -> std::string;
|
||||
void parseCenter(const std::string& value);
|
||||
[[nodiscard]] static auto parsePoints(const std::string& str) -> std::vector<Vec2>;
|
||||
};
|
||||
[[nodiscard]] static auto computeBoundingRadius(
|
||||
const std::vector<ShapePrimitive>& primitives,
|
||||
const Vec2& center) -> float;
|
||||
};
|
||||
|
||||
} // namespace Graphics
|
||||
|
||||
@@ -221,7 +221,8 @@ namespace Graphics {
|
||||
// Ajustar X e Y para que position represente esquina superior izquierda
|
||||
// (render_shape espera el centro, así que sumamos la mitad de ancho y altura)
|
||||
Vec2 char_pos = {.x = current_x + (CHAR_WIDTH_SCALED / 2.0F), .y = position.y + (CHAR_HEIGHT_SCALED / 2.0F)};
|
||||
Rendering::renderShape(renderer_, it->second, char_pos, 0.0F, scale, 1.0F, brightness, color);
|
||||
// Text opt-out del glow: HUD/marker s'ha de mantenir net.
|
||||
Rendering::renderShape(renderer_, it->second, char_pos, 0.0F, scale, 1.0F, brightness, color, 0.0F, 1.0F, /*glow=*/false);
|
||||
|
||||
// Avanzar posición
|
||||
current_x += CHAR_WIDTH_SCALED + SPACING_SCALED;
|
||||
|
||||
Reference in New Issue
Block a user