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);