-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmuray.cpp
More file actions
185 lines (157 loc) · 5.64 KB
/
muray.cpp
File metadata and controls
185 lines (157 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Node.js plugin that integrates together Raylib and MicroUI
#ifdef __LINUX__
#include <print>
#endif
#include <napi.h>
#include <raylib.h>
extern "C" {
#include "microui.h"
}
#define FONT_SIZE 20
static mu_Context ctx = {0};
static char input_buf[128];
static mu_Rect unclipped_rect = { 0, 0, 0x1000000, 0x1000000 };
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);
}
Napi::Value MuLabel(const Napi::CallbackInfo &args) {
auto env = args.Env();
mu_label(&ctx, args[0].ToString().Utf8Value().c_str());
return env.Undefined();
}
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);
return Napi::String::New(env, input_buf);
}
return env.Undefined();
}
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();
}
Napi::Value MuEndWindow(const Napi::CallbackInfo &args) {
mu_end_window(&ctx);
return args.Env().Undefined();
}
Napi::Value MuUpdateInput(const Napi::CallbackInfo &args) {
int x = GetMouseX();
int y = GetMouseY();
mu_input_mousemove(&ctx, x, y);
for (int button = MOUSE_BUTTON_LEFT; button <= MOUSE_BUTTON_MIDDLE; ++button) {
if (IsMouseButtonPressed (button)) mu_input_mousedown(&ctx, x, y, 1 << button);
if (IsMouseButtonReleased(button)) mu_input_mouseup (&ctx, x, y, 1 << button);
}
char c;
char input[2];
if ((c = GetCharPressed()))
{
input[0] = c;
input[1] = '\0';
mu_input_text(&ctx, input);
}
return args.Env().Undefined();
}
Napi::Value MuBegin(const Napi::CallbackInfo &args) {
mu_begin(&ctx);
return args.Env().Undefined();
}
Napi::Value MuEnd(const Napi::CallbackInfo &args) {
mu_end(&ctx);
mu_Command *cmd = NULL;
while (mu_next_command(&ctx, &cmd)) {
switch (cmd->type) {
case MU_COMMAND_JUMP: printf("MU_COMMAND_JUMP\n"); break;
case MU_COMMAND_CLIP: {
int posX = cmd->clip.rect.x;
int posY = cmd->clip.rect.y;
int width = cmd->clip.rect.w;
int height = cmd->clip.rect.h;
if (memcmp(&cmd->clip.rect, &unclipped_rect, sizeof(unclipped_rect)) == 0) {
EndScissorMode();
} else {
BeginScissorMode(posX, posY, width, height);
}
} break;
case MU_COMMAND_RECT: {
int posX = cmd->rect.rect.x;
int posY = cmd->rect.rect.y;
int width = cmd->rect.rect.w;
int height = cmd->rect.rect.h;
DrawRectangle(posX, posY, width, height, *(Color*)&cmd->rect.color);
} break;
case MU_COMMAND_TEXT: {
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();
}
Napi::Value InitWindowAdapter(const Napi::CallbackInfo &args) {
auto env = args.Env();
int width = 0;
if (args.Length() > 0) width = args[0].ToNumber().Int32Value();
int height = 0;
if (args.Length() > 1) height = args[1].ToNumber().Int32Value();
const char *title = "Hardcoded Title";
if (args.Length() > 2) {
title = strdup(args[2].ToString().Utf8Value().c_str()); // memory leak
}
InitWindow(width, height, title);
return env.Undefined();
}
Napi::Value WindowShouldCloseAdapter(const Napi::CallbackInfo &args) {
return Napi::Boolean::New(args.Env(), WindowShouldClose());
}
Napi::Value BeginDrawingAdapter(const Napi::CallbackInfo &args) {
BeginDrawing();
return args.Env().Undefined();
}
Napi::Value EndDrawingAdapter(const Napi::CallbackInfo &args) {
EndDrawing();
return args.Env().Undefined();
}
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)
{
int x = MeasureText(TextFormat("%.*s", len, str), FONT_SIZE);
return x;
}
int text_height(mu_Font font)
{
return FONT_SIZE;
}
#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;
NODE_SET_METHOD(exports, "InitWindow", InitWindowAdapter);
NODE_SET_METHOD(exports, "WindowShouldClose", WindowShouldCloseAdapter);
NODE_SET_METHOD(exports, "BeginDrawing", BeginDrawingAdapter);
NODE_SET_METHOD(exports, "EndDrawing", EndDrawingAdapter);
NODE_SET_METHOD(exports, "ClearBackground", ClearBackgroundAdapter);
NODE_SET_METHOD(exports, "mu_update_input", MuUpdateInput);
NODE_SET_METHOD(exports, "mu_begin", MuBegin);
NODE_SET_METHOD(exports, "mu_end", MuEnd);
NODE_SET_METHOD(exports, "mu_begin_window", MuBeginWindow);
NODE_SET_METHOD(exports, "mu_end_window", MuEndWindow);
NODE_SET_METHOD(exports, "mu_button", MuButton);
NODE_SET_METHOD(exports, "mu_label", MuLabel);
NODE_SET_METHOD(exports, "mu_input", MuInput);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Initialize)