Refractor and cleanup binding logic
This commit is contained in:
+2
-292
@@ -1,297 +1,7 @@
|
||||
#include "app/app.h"
|
||||
|
||||
app::App *app_ = nullptr;
|
||||
|
||||
DEF_RB(
|
||||
Scene,
|
||||
({
|
||||
app::Scene *scene = app::scene_pool[id];
|
||||
if (scene != nullptr)
|
||||
scene->unused = true;
|
||||
}),
|
||||
({
|
||||
app::Scene *loading_scene = new app::Scene{};
|
||||
s->id = app::scene_pool.acquire(loading_scene);
|
||||
})
|
||||
)
|
||||
|
||||
static mrb_value scene_add_element(mrb_state *mrb, mrb_value self) {
|
||||
mrb_value element_val;
|
||||
mrb_get_args(mrb, "o", &element_val);
|
||||
|
||||
if (!mrb_obj_is_kind_of(mrb, element_val, mrb_class_get(mrb, "ElementText")) && !mrb_obj_is_kind_of(mrb, element_val, mrb_class_get(mrb, "ElementRect"))) {
|
||||
mrb_raise(mrb, mrb_class_get(Ruby::mrb, "Exception"), "Expected an ElementText or ElementRect object");
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
int id = get_id(self);
|
||||
app::Scene *scene = app::scene_pool[id];
|
||||
|
||||
int element_id;
|
||||
if (
|
||||
mrb_obj_is_kind_of(mrb, element_val, mrb_class_get(mrb, "ElementText")) || mrb_obj_is_kind_of(mrb, element_val, mrb_class_get(mrb, "ElementRect"))
|
||||
)
|
||||
element_id = get_id(element_val);
|
||||
|
||||
scene->add_element(element_id);
|
||||
return self;
|
||||
}
|
||||
|
||||
DEF_RB(
|
||||
Font,
|
||||
({ window.unload_font(id); }),
|
||||
({
|
||||
char *path;
|
||||
mrb_int size;
|
||||
mrb_value kwargs;
|
||||
mrb_get_args(mrb, "zi|H", &path, &size, &kwargs);
|
||||
|
||||
bool pixel_art = false;
|
||||
bool bold = false;
|
||||
bool italic = false;
|
||||
bool underline = false;
|
||||
if (!mrb_nil_p(kwargs)) {
|
||||
HASH_BOOL(kwargs, pixel_art, pixel_art);
|
||||
HASH_BOOL(kwargs, bold, bold);
|
||||
HASH_BOOL(kwargs, italic, italic);
|
||||
HASH_BOOL(kwargs, underline, underline);
|
||||
}
|
||||
s->id = window.load_font(path, size, bold, italic, underline, pixel_art);
|
||||
})
|
||||
)
|
||||
|
||||
DEF_RB(
|
||||
ElementText,
|
||||
({
|
||||
app::Element *element = app::element_pool[id];
|
||||
if (element != nullptr)
|
||||
element->reference_count--;
|
||||
|
||||
if (element != nullptr && element->reference_count <= 0) {
|
||||
delete element;
|
||||
app::element_pool.release(id);
|
||||
}
|
||||
}),
|
||||
({
|
||||
mrb_value font_val;
|
||||
mrb_sym key;
|
||||
mrb_value kwargs;
|
||||
mrb_get_args(mrb, "no|H", &key, &font_val, &kwargs);
|
||||
|
||||
auto key_id = Localization::key_from_sym(mrb, key);
|
||||
|
||||
if (!mrb_obj_is_kind_of(mrb, font_val, mrb_class_get(mrb, "Font"))) {
|
||||
mrb_raise(mrb, mrb_class_get(Ruby::mrb, "Exception"), "Expected a Font object");
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
float z = 0;
|
||||
bool click_through = false;
|
||||
if (!mrb_nil_p(kwargs)) {
|
||||
mrb_value pos_hash = HASH_GET(kwargs, position);
|
||||
HASH_FLOAT(pos_hash, x, x);
|
||||
HASH_FLOAT(pos_hash, y, y);
|
||||
|
||||
HASH_FLOAT(kwargs, z, z);
|
||||
HASH_BOOL(kwargs, click_through, click_through);
|
||||
}
|
||||
|
||||
Vec2<float> position = {x, y};
|
||||
|
||||
int id = get_id(font_val);
|
||||
|
||||
app::EText *element = new app::EText(key_id, id, {255, 255, 255, 255});
|
||||
element->reference_count++;
|
||||
element->position = position;
|
||||
element->z = z;
|
||||
element->click_through = click_through;
|
||||
|
||||
s->id = app::element_pool.acquire(element);
|
||||
})
|
||||
)
|
||||
|
||||
DEF_RB(
|
||||
ElementRect,
|
||||
({
|
||||
app::Element *element = app::element_pool[id];
|
||||
if (element != nullptr)
|
||||
element->reference_count--;
|
||||
|
||||
if (element != nullptr && element->reference_count <= 0) {
|
||||
delete element;
|
||||
app::element_pool.release(id);
|
||||
}
|
||||
}),
|
||||
({
|
||||
mrb_value shape_val;
|
||||
mrb_int color;
|
||||
mrb_value kwargs;
|
||||
mrb_get_args(mrb, "Hi|H", &shape_val, &color, &kwargs);
|
||||
|
||||
float x = 0, y = 0, w = 0, h = 0;
|
||||
if (!mrb_nil_p(shape_val)) {
|
||||
HASH_FLOAT(shape_val, x, x);
|
||||
HASH_FLOAT(shape_val, y, y);
|
||||
HASH_FLOAT(shape_val, w, w);
|
||||
HASH_FLOAT(shape_val, h, h);
|
||||
}
|
||||
|
||||
float z = 0;
|
||||
if (!mrb_nil_p(kwargs))
|
||||
HASH_FLOAT(kwargs, z, z);
|
||||
|
||||
Vec2<float> position = {x, y};
|
||||
Vec2<float> shape = {w, h};
|
||||
|
||||
app::ERect *element = new app::ERect(
|
||||
shape,
|
||||
{(uint8_t)((color >> 24) & 0xFF),
|
||||
(uint8_t)((color >> 16) & 0xFF),
|
||||
(uint8_t)((color >> 8) & 0xFF),
|
||||
(uint8_t)(color & 0xFF)}
|
||||
);
|
||||
element->reference_count++;
|
||||
element->position = position;
|
||||
element->z = z;
|
||||
|
||||
s->id = app::element_pool.acquire(element);
|
||||
})
|
||||
)
|
||||
|
||||
mrb_value app_run(mrb_state *mrb, mrb_value self) {
|
||||
mrb_int width, height;
|
||||
char *title = nullptr;
|
||||
mrb_get_args(mrb, "iiz", &width, &height, &title);
|
||||
|
||||
int loading_image = window.load_image("assets/images/menu_bg.png", 1.0f, true);
|
||||
|
||||
Animation loading_animation = {
|
||||
.frames = {loading_image},
|
||||
.frame_rate = 1.0f,
|
||||
.loop = false
|
||||
};
|
||||
|
||||
app_ = new app::App({(float)width, (float)height}, title, loading_animation);
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
mrb_value app_start(mrb_state *mrb, mrb_value self) {
|
||||
mrb_value scene_val;
|
||||
mrb_get_args(mrb, "o", &scene_val);
|
||||
|
||||
if (!mrb_obj_is_kind_of(mrb, scene_val, mrb_class_get(mrb, "Scene"))) {
|
||||
mrb_raise(mrb, mrb_class_get(Ruby::mrb, "Exception"), "Expected a Scene object");
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
int id = get_id(scene_val);
|
||||
app_->switch_to_scene(id);
|
||||
app_->loop();
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
mrb_value app_switch_to(mrb_state *mrb, mrb_value self) {
|
||||
mrb_value scene_val;
|
||||
mrb_get_args(mrb, "o", &scene_val);
|
||||
|
||||
if (!mrb_obj_is_kind_of(mrb, scene_val, mrb_class_get(mrb, "Scene"))) {
|
||||
mrb_raise(mrb, mrb_class_get(Ruby::mrb, "Exception"), "Expected a Scene object");
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
int id = get_id(scene_val);
|
||||
app_->switch_to_scene(id);
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
static mrb_value app_localize(mrb_state *mrb, mrb_value self) {
|
||||
mrb_sym lang_sym, key_sym;
|
||||
char *text;
|
||||
|
||||
mrb_get_args(mrb, "nnz", &lang_sym, &key_sym, &text);
|
||||
|
||||
auto lang = Localization::lang_from_sym(mrb, lang_sym);
|
||||
|
||||
const char *key_name = mrb_sym2name(mrb, key_sym);
|
||||
|
||||
auto it = Localization::localization_key_map.find(key_name);
|
||||
Localization::Key key_id;
|
||||
|
||||
if (it == Localization::localization_key_map.end()) {
|
||||
key_id = (Localization::Key)Localization::localization_key_map.size();
|
||||
Localization::localization_key_map[key_name] = key_id;
|
||||
} else {
|
||||
key_id = it->second;
|
||||
}
|
||||
|
||||
Localization::localized_strings[Localization::join_keys(lang, key_id)] = text;
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
#include "binding/definitions.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
RClass *font_class = mrb_define_class(Ruby::mrb, "Font", Ruby::mrb->object_class);
|
||||
mrb_define_method(Ruby::mrb, font_class, "initialize", Font_init, MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1));
|
||||
|
||||
RClass *element_text_class = mrb_define_class(Ruby::mrb, "ElementText", Ruby::mrb->object_class);
|
||||
mrb_define_method(Ruby::mrb, element_text_class, "initialize", ElementText_init, MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1));
|
||||
mrb_define_method(Ruby::mrb, element_text_class, "on_click", [](mrb_state *mrb, mrb_value self) {
|
||||
mrb_value block;
|
||||
mrb_get_args(mrb, "&", &block);
|
||||
int id = get_id(self);
|
||||
app::Element *element = app::element_pool[id];
|
||||
element->on_click.set_proc(block);
|
||||
return self; }, MRB_ARGS_BLOCK());
|
||||
|
||||
RClass *element_rect_class = mrb_define_class(Ruby::mrb, "ElementRect", Ruby::mrb->object_class);
|
||||
mrb_define_method(Ruby::mrb, element_rect_class, "initialize", ElementRect_init, MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1));
|
||||
mrb_define_method(Ruby::mrb, element_rect_class, "on_click", [](mrb_state *mrb, mrb_value self) {
|
||||
mrb_value block;
|
||||
mrb_get_args(mrb, "&", &block);
|
||||
int id = get_id(self);
|
||||
app::Element *element = app::element_pool[id];
|
||||
element->on_click.set_proc(block);
|
||||
return self; }, MRB_ARGS_BLOCK());
|
||||
mrb_define_method(Ruby::mrb, element_rect_class, "color=", [](mrb_state *mrb, mrb_value self) {
|
||||
mrb_value color_val;
|
||||
mrb_get_args(mrb, "o", &color_val);
|
||||
int id = get_id(self);
|
||||
app::ERect *element = (app::ERect *)app::element_pool[id];
|
||||
element->color = {
|
||||
(uint8_t)((mrb_fixnum(color_val) >> 24) & 0xFF),
|
||||
(uint8_t)((mrb_fixnum(color_val) >> 16) & 0xFF),
|
||||
(uint8_t)((mrb_fixnum(color_val) >> 8) & 0xFF),
|
||||
(uint8_t)(mrb_fixnum(color_val) & 0xFF)
|
||||
};
|
||||
return self; }, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(Ruby::mrb, element_rect_class, "color", [](mrb_state *mrb, mrb_value self) {
|
||||
int id = get_id(self);
|
||||
app::ERect *element = (app::ERect *)app::element_pool[id];
|
||||
uint32_t color = ((uint32_t)element->color.r << 24) |
|
||||
((uint32_t)element->color.g << 16) |
|
||||
((uint32_t)element->color.b << 8) |
|
||||
(uint32_t)element->color.a;
|
||||
return mrb_fixnum_value(color); }, MRB_ARGS_NONE());
|
||||
|
||||
RClass *scene_class = mrb_define_class(Ruby::mrb, "Scene", Ruby::mrb->object_class);
|
||||
mrb_define_method(Ruby::mrb, scene_class, "initialize", Scene_init, MRB_ARGS_NONE());
|
||||
mrb_define_method(Ruby::mrb, scene_class, "<<", scene_add_element, MRB_ARGS_REQ(1));
|
||||
|
||||
RClass *app_module = mrb_define_module(Ruby::mrb, "App");
|
||||
mrb_define_class_method(Ruby::mrb, app_module, "run", app_run, MRB_ARGS_REQ(3));
|
||||
mrb_define_class_method(Ruby::mrb, app_module, "start", app_start, MRB_ARGS_REQ(1));
|
||||
mrb_define_class_method(Ruby::mrb, app_module, "switch_to", app_switch_to, MRB_ARGS_REQ(1));
|
||||
mrb_define_class_method(Ruby::mrb, app_module, "localize", app_localize, MRB_ARGS_REQ(3));
|
||||
mrb_define_class_method(Ruby::mrb, app_module, "language=", [](mrb_state *mrb, mrb_value self) {
|
||||
mrb_sym lang_sym;
|
||||
mrb_get_args(mrb, "n", &lang_sym);
|
||||
Localization::current_language = Localization::lang_from_sym(mrb, lang_sym);
|
||||
return mrb_nil_value(); }, MRB_ARGS_REQ(1));
|
||||
definitions::setup();
|
||||
|
||||
char *startup_script = nullptr;
|
||||
if (argc > 1)
|
||||
|
||||
Reference in New Issue
Block a user