Add ruby bindings for internal structures, allow better controlled localization

This commit is contained in:
2026-05-06 22:01:48 +01:00
parent 3894b90398
commit 65109cc3c4
8 changed files with 1047 additions and 114 deletions
+33 -12
View File
@@ -16,6 +16,7 @@ struct Element {
Vec2<float> position = {0, 0};
float rotation = 0;
Vec2<float> scale = {1, 1};
float alpha = 1.0f;
float z = 0;
bool click_through = false;
Ruby::Block on_click;
@@ -56,28 +57,38 @@ struct EImage : Element {
return;
int frame_index = (int)((abs_time * animation.frame_rate) / 1000) % animation.frames.size();
window.draw(animation.frames[frame_index], position, z, rotation, scale);
window.draw(animation.frames[frame_index], position, z, rotation, scale, alpha);
}
};
struct EText : Element {
Localization::Key key;
uint64_t font_id = UINT64_MAX;
Color color = {255, 255, 255};
mrb_value params = mrb_nil_value();
~EText() {
if (text_id != UINT64_MAX)
text_pool.release(text_id);
if (font_id != UINT64_MAX)
font_pool.release(font_id);
if (!mrb_nil_p(params))
mrb_gc_unregister(Ruby::mrb, params);
}
Color color = {255, 255, 255, 255};
EText(Localization::Key key, uint64_t font_id, Color color)
: Element(Type::TEXT), key(key), font_id(font_id), color(color) {
EText(Localization::Key key, uint64_t font_id, Color color, mrb_value params)
: Element(Type::TEXT), key(key), font_id(font_id), color(color), params(params) {
if (!mrb_nil_p(params))
mrb_gc_protect(Ruby::mrb, params);
font_pool.use(font_id);
}
void reload_params() {
// Force re-render by resetting last_key (without affecting this frame)
// this can be changed to use a dirty flag or version counter if needed
last_key = UINT16_MAX;
}
Vec2<float> size(Window *window) override {
if (text_id == UINT64_MAX)
return {0, 0};
@@ -85,26 +96,32 @@ struct EText : Element {
};
void render(Window &window, uint64_t) override {
if (last_language != Localization::current_language || last_key != key || text_id == UINT64_MAX) {
std::string localized_str = Localization::get(key);
if (
last_language != Localization::current_language
|| last_key != key || text_id == UINT64_MAX
|| last_color != color
) {
std::string localized_str = Localization::get(Ruby::mrb, key, params);
if (text_id != UINT64_MAX)
text_pool.release(text_id);
text_id = window.create_text(font_id, localized_str.c_str(), localized_str.size(), color);
last_language = Localization::current_language;
last_key = key;
last_color = color;
}
window.write(text_id, position, z, rotation, scale);
window.write(text_id, position, z, rotation, scale, alpha);
}
private:
uint64_t text_id = UINT64_MAX;
Localization::LanguageCode last_language = UINT_MAX;
Localization::Key last_key = UINT_MAX;
Color last_color = color;
Localization::LanguageCode last_language = UINT32_MAX;
Localization::Key last_key = UINT16_MAX;
};
struct ERect : Element {
Vec2<float> shape = {0, 0};
Color color = {255, 255, 255, 255};
Color color = {255, 255, 255};
Vec2<float> size(Window *) override {
return shape;
};
@@ -112,7 +129,7 @@ struct ERect : Element {
ERect(Vec2<float> shape, Color color) : Element(Type::RECT), shape(shape), color(color) {}
void render(Window &window, uint64_t) override {
window.draw_rect(Rect::from_size(position, shape), color, z, rotation);
window.draw_rect(Rect::from_size(position, shape), color, z, rotation, scale, alpha);
}
};
@@ -195,6 +212,10 @@ struct App {
bool running = true;
Vec2<float> mouse_position = {-1, -1};
void exit() {
running = false;
}
void switch_to_scene(uint64_t scene_id) {
scene_pool.release(current_scene_id);
scene_pool.use(scene_id);
File diff suppressed because it is too large Load Diff
+2
View File
@@ -94,6 +94,8 @@ DEF_SYM(bold)
DEF_SYM(italic)
DEF_SYM(underline)
DEF_SYM(position)
DEF_SYM(rotation)
DEF_SYM(scale)
DEF_SYM(alpha)
DEF_SYM(fps)
DEF_SYM(click_through)
+83 -9
View File
@@ -17,20 +17,87 @@ inline JoinedKey join_keys(LanguageCode lang_code, Key loc_key) {
return (static_cast<JoinedKey>(lang_code) << 16) | loc_key;
}
inline std::unordered_map<JoinedKey, std::string> localized_strings;
struct Segment {
bool is_var;
std::string text;
};
inline std::string get(Key key) {
auto it = localized_strings.find(join_keys(current_language, key));
if (it != localized_strings.end())
inline std::unordered_map<JoinedKey, std::vector<Segment>> localized_strings;
inline std::unordered_map<std::string, mrb_sym> var_sym_cache;
inline mrb_sym get_var_sym(mrb_state *mrb, const std::string &name) {
auto it = var_sym_cache.find(name);
if (it != var_sym_cache.end())
return it->second;
for (const auto &[joined, value] : localized_strings) {
Key k = (Key)(joined & 0xFFFF);
if (k == key)
return value;
mrb_sym sym = mrb_intern_cstr(mrb, name.c_str());
var_sym_cache[name] = sym;
return sym;
}
inline std::vector<Segment> parse_localized(const std::string &input) {
std::vector<Segment> out;
size_t i = 0;
while (i < input.size()) {
size_t start = input.find("%{", i);
if (start == std::string::npos) {
out.push_back({false, input.substr(i)});
break;
}
if (start > i) {
out.push_back({false, input.substr(i, start - i)});
}
size_t end = input.find("}", start);
if (end == std::string::npos) {
// malformed fallback
out.push_back({false, input.substr(start)});
break;
}
std::string var = input.substr(start + 2, end - (start + 2));
out.push_back({true, var});
i = end + 1;
}
return "Localization not set!";
return out;
}
inline void set(LanguageCode lang_code, Key key, const std::string &text) {
localized_strings[join_keys(lang_code, key)] = parse_localized(text);
}
inline std::string get(mrb_state *mrb, Key key, mrb_value hash) {
auto it = localized_strings.find(join_keys(current_language, key));
if (it == localized_strings.end())
return "Localization not set!";
const auto &entry = it->second;
std::string result;
result.reserve(10);
for (const auto &seg : entry) {
if (!seg.is_var) {
result += seg.text;
} else {
mrb_sym sym = get_var_sym(mrb, seg.text);
if (mrb_nil_p(hash) || mrb_nil_p(mrb_hash_get(mrb, hash, mrb_symbol_value(sym)))) {
result += "%{" + seg.text + "}";
continue;
}
mrb_value val = mrb_hash_get(mrb, hash, mrb_symbol_value(sym));
if (mrb_nil_p(val))
continue;
result += mrb_str_to_cstr(mrb, val);
}
}
return result;
}
inline LanguageCode lang_from_sym(mrb_state *mrb, mrb_sym sym) {
@@ -49,6 +116,13 @@ inline LanguageCode lang_from_sym(mrb_state *mrb, mrb_sym sym) {
return code;
}
inline mrb_sym lang_to_sym(mrb_state *mrb, LanguageCode code) {
char s[5] = {0};
for (int i = 0; i < 4; ++i)
s[i] = (code >> (8 * (3 - i))) & 0xFF;
return mrb_intern_cstr(mrb, s);
}
inline Key key_from_sym(mrb_state *mrb, mrb_sym sym) {
const char *key_name = mrb_sym2name(mrb, sym);
+5 -1
View File
@@ -9,7 +9,11 @@
#define UNUSED(x) (void)(x)
struct Color {
uint8_t r, g, b = 0, a = 255;
uint8_t r, g, b = 0;
bool operator!=(const Color &other) const {
return r != other.r || g != other.g || b != other.b;
}
};
template <typename T>
+6 -7
View File
@@ -83,8 +83,6 @@ struct Event {
struct Animation {
std::vector<uint64_t> frames;
float frame_rate;
bool loop;
int reference_count = 0;
~Animation() {
for (uint64_t frame_id : frames)
@@ -104,20 +102,20 @@ public:
~Window();
// Rendering functions
uint64_t load_image(const char *path, float alpha = 1.0f, bool pixel_art = false);
uint64_t load_image(const char *path, bool pixel_art);
Vec2<float> get_image_size(uint64_t image_id);
uint64_t load_font(const char *path, int font_size, bool bold = false, bool italic = false, bool underline = false, bool pixel_art = false);
uint64_t load_font(const char *path, int font_size, bool bold, bool italic, bool underline, bool pixel_art);
// text is copied internally, so it can be free'd by the caller after the text is created.
uint64_t create_text(uint64_t font_id, const char *text, uint32_t length, Color color);
Vec2<float> get_text_size(uint64_t text_id);
bool write(uint64_t text_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f});
bool write(uint64_t text_id, Vec2<float> position, float z, float angle, Vec2<float> scale, float alpha);
bool draw(uint64_t image_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f});
bool draw(uint64_t image_id, Vec2<float> position, float z, float angle, Vec2<float> scale, float alpha);
bool draw_rect(Rect rect, Color color, float z, float angle = 0.0f);
bool draw_rect(Rect rect, Color color, float z, float angle, Vec2<float> scale, float alpha);
void render(); // the rest of the calls only build the instruction list, this actually renders everything added to the screen (following z order)
@@ -170,6 +168,7 @@ private:
float z;
float angle;
Vec2<float> scale;
float alpha;
union {
struct {