Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/muray/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
'vendor/microui.c'
],
'include_dirs': ['vendor/'],
'dependencies': [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api",
],
'conditions': [
['OS=="linux"', {
'include_dirs': ['vendor/raylib-5.5_linux_amd64/include'],
Expand All @@ -26,6 +29,7 @@
['OS=="win"', {
'include_dirs': ['vendor/raylib-5.5_win64_msvc16/include'],
"msvs_settings": {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
"VCLinkerTool": {
"AdditionalLibraryDirectories": [ "<(module_root_dir)/vendor/raylib-5.5_win64_msvc16/lib" ],
"AdditionalDependencies": [ 'raylib.lib', 'winmm.lib' ],
Expand Down
84 changes: 46 additions & 38 deletions packages/muray/muray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifdef __LINUX__
#include <print>
#endif
#include <node.h>
#include <napi.h>
#include <raylib.h>
extern "C" {
#include "microui.h"
Expand All @@ -14,42 +14,42 @@ static mu_Context ctx = {0};
static char input_buf[128];
static mu_Rect unclipped_rect = { 0, 0, 0x1000000, 0x1000000 };

void MuButton(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
bool result = mu_button(&ctx, *v8::String::Utf8Value(isolate, args[0]->ToString(context).ToLocalChecked()));
args.GetReturnValue().Set(v8::Boolean::New(isolate, result));
Napi::Value MuButton(const Napi::CallbackInfo &args) {
auto env = args.Env();
bool result = mu_button(&ctx, args[0].ToString().Utf8Value().c_str());
return Napi::Boolean::New(env, result);
}

void MuLabel(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
auto label = *v8::String::Utf8Value(isolate, args[0]->ToString(context).ToLocalChecked());
mu_label(&ctx, *v8::String::Utf8Value(isolate, args[0]->ToString(context).ToLocalChecked()));
Napi::Value MuLabel(const Napi::CallbackInfo &args) {
auto env = args.Env();
mu_label(&ctx, args[0].ToString().Utf8Value().c_str());
return env.Undefined();
}

void MuInput(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
Napi::Value MuInput(const Napi::CallbackInfo &args) {
auto env = args.Env();
int val = mu_textbox(&ctx, input_buf, sizeof(input_buf));
if (val & MU_RES_CHANGE)
{
mu_set_focus(&ctx, ctx.last_id);
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, input_buf).ToLocalChecked());
return Napi::String::New(env, input_buf);
}
return env.Undefined();
}

void MuBeginWindow(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value MuBeginWindow(const Napi::CallbackInfo &args) {
mu_begin_window(&ctx, "Murayact", mu_rect(20, 20, 300, 300));
const int widths[] = { 300, -1 };
mu_layout_row(&ctx, 2, widths, 50);
return args.Env().Undefined();
}

void MuEndWindow(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value MuEndWindow(const Napi::CallbackInfo &args) {
mu_end_window(&ctx);
return args.Env().Undefined();
}

void MuUpdateInput(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value MuUpdateInput(const Napi::CallbackInfo &args) {
int x = GetMouseX();
int y = GetMouseY();
mu_input_mousemove(&ctx, x, y);
Expand All @@ -66,13 +66,15 @@ void MuUpdateInput(const v8::FunctionCallbackInfo<v8::Value> &args) {
input[1] = '\0';
mu_input_text(&ctx, input);
}
return args.Env().Undefined();
}

void MuBegin(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value MuBegin(const Napi::CallbackInfo &args) {
mu_begin(&ctx);
return args.Env().Undefined();
}

void MuEnd(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value MuEnd(const Napi::CallbackInfo &args) {
mu_end(&ctx);

mu_Command *cmd = NULL;
Expand Down Expand Up @@ -101,46 +103,48 @@ void MuEnd(const v8::FunctionCallbackInfo<v8::Value> &args) {
DrawText(cmd->text.str, cmd->text.pos.x, cmd->text.pos.y, FONT_SIZE, *(Color*)&cmd->text.color);
} break;
// case MU_COMMAND_ICON: printf("MU_COMMAND_ICON\n"); break;
}
}
}
return args.Env().Undefined();
}

void InitWindowAdapter(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
Napi::Value InitWindowAdapter(const Napi::CallbackInfo &args) {
auto env = args.Env();

int width = 0;
if (args.Length() > 0) width = args[0]->Int32Value(context).FromJust();
if (args.Length() > 0) width = args[0].ToNumber().Int32Value();

int height = 0;
if (args.Length() > 1) height = args[1]->Int32Value(context).FromJust();
if (args.Length() > 1) height = args[1].ToNumber().Int32Value();

const char *title = "Hardcoded Title";
if (args.Length() > 2) {
title = strdup(*v8::String::Utf8Value(isolate, args[2]->ToString(context).ToLocalChecked())); // memory leak
title = strdup(args[2].ToString().Utf8Value().c_str()); // memory leak
}

InitWindow(width, height, title);
return env.Undefined();
}

void WindowShouldCloseAdapter(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto isolate = args.GetIsolate();
args.GetReturnValue().Set(v8::Boolean::New(isolate, WindowShouldClose()));
Napi::Value WindowShouldCloseAdapter(const Napi::CallbackInfo &args) {
return Napi::Boolean::New(args.Env(), WindowShouldClose());
}

void BeginDrawingAdapter(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value BeginDrawingAdapter(const Napi::CallbackInfo &args) {
BeginDrawing();
return args.Env().Undefined();
}

void EndDrawingAdapter(const v8::FunctionCallbackInfo<v8::Value> &args) {
Napi::Value EndDrawingAdapter(const Napi::CallbackInfo &args) {
EndDrawing();
return args.Env().Undefined();
}

void ClearBackgroundAdapter(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
auto color = args[0]->Uint32Value(context).FromJust();
Napi::Value ClearBackgroundAdapter(const Napi::CallbackInfo &args) {
auto env = args.Env();
auto color = args[0].ToNumber().Uint32Value();
ClearBackground(*(Color*)&color);
return env.Undefined();
}

int text_width(mu_Font font, const char *str, int len)
Expand All @@ -154,7 +158,9 @@ int text_height(mu_Font font)
return FONT_SIZE;
}

void Initialize(v8::Local<v8::Object> exports) {
#define NODE_SET_METHOD(exports, name, func) exports.Set(Napi::String::New(env, name), Napi::Function::New(env, func));

Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
mu_init(&ctx);
ctx.text_width = text_width;
ctx.text_height = text_height;
Expand All @@ -172,6 +178,8 @@ void Initialize(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "mu_button", MuButton);
NODE_SET_METHOD(exports, "mu_label", MuLabel);
NODE_SET_METHOD(exports, "mu_input", MuInput);

return exports;
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Initialize)
5 changes: 4 additions & 1 deletion packages/muray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"bugs": {
"url": "https://github.com/tsoding/Murayact/issues"
},
"homepage": "https://github.com/tsoding/Murayact#readme"
"homepage": "https://github.com/tsoding/Murayact#readme",
"dependencies": {
"node-addon-api": "*"
}
}