get needed mouse states for UI

main
Patrick Cleavelin 2024-04-17 17:22:03 -05:00
parent 2eaca7a4b9
commit 383478cf15
2 changed files with 37 additions and 4 deletions

View File

@ -60,6 +60,7 @@ typedef struct {
bool keep_running;
int mouse_x, mouse_y;
bool mouse_left_down, mouse_right_down;
// Metal objects
id<MTLDevice> device;
@ -73,7 +74,8 @@ typedef struct {
} _metal_gfx_context;
#endif
typedef void (*_gfx_frame_func)(int mouse_x, int mouse_y);
typedef void (*_gfx_frame_func)(int mouse_x, int mouse_y, bool mouse_left_down,
bool mouse_right_down);
typedef struct {
#if defined(__APPLE__)
_metal_gfx_context backend;
@ -175,6 +177,32 @@ void gfx_update_buffer(gfx_context_t *cx, size_t buffer_index, const void *data,
return self;
}
- (void)mouseDown:(NSEvent *)event {
if ((unsigned long)event.type == NSEventTypeLeftMouseDown) {
_gfx_context.backend.mouse_left_down = true;
}
if (event.type == NSEventTypeRightMouseUp) {
_gfx_context.backend.mouse_right_down = true;
}
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)event {
if ((unsigned long)event.type == NSEventTypeLeftMouseUp) {
_gfx_context.backend.mouse_left_down = false;
}
if (event.type == NSEventTypeRightMouseDown) {
_gfx_context.backend.mouse_right_down = false;
}
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)event {
[self setNeedsDisplay:YES];
}
- (void)mouseMoved:(NSEvent *)event {
// NSPoint location = NSEvent.mouseLocation;
NSPoint location = [self convertPoint:[event locationInWindow]
@ -387,7 +415,8 @@ static void _metal_gfx_present(_metal_gfx_context *cx) {
_gfx_context.gpu_glyphs.size = 0;
_gfx_context.gpu_ui_rects.size = 0;
_gfx_context.frame_func(cx->mouse_x,
_gfx_context.frame_height - cx->mouse_y);
_gfx_context.frame_height - cx->mouse_y,
cx->mouse_left_down, cx->mouse_right_down);
if (_gfx_context.gpu_glyphs.size > 0) {
gfx_update_buffer(&_gfx_context, 2, _gfx_context.gpu_glyphs.data,
@ -470,6 +499,9 @@ static void _metal_gfx_present(_metal_gfx_context *cx) {
}
[encoder endEncoding];
// FIXME: `afterMinimumDuration` causes the weird re-size scaling, but I
// need to figure why the heck the NSView doesn't get the rendered
// contents unless `afterMinimumDuration` is here
[command_buffer presentDrawable:drawable
afterMinimumDuration:1.0 / 144.0];
[command_buffer commit];

View File

@ -122,7 +122,8 @@ void render_ui_rect(float position[2], float size[2], float color[4]) {
gfx_queue_ui_rect(state.gfx_cx, position, size, 0, color);
}
void ed_frame(int mouse_x, int mouse_y) {
void ed_frame(int mouse_x, int mouse_y, bool mouse_left_down,
bool mouse_right_down) {
state.ui_cx.frame_elements.data[0].size.computed_size[0] =
state.gfx_cx->frame_width;
state.ui_cx.frame_elements.data[0].size.computed_size[1] =
@ -130,7 +131,7 @@ void ed_frame(int mouse_x, int mouse_y) {
uint8_t buffer[256] = {};
snprintf(buffer, 256, "Mouse X: %d, Mouse Y: %d, Mouse %s", mouse_x,
mouse_y, state.ui_cx.input.mouse_left_down ? "Down" : "Up");
mouse_y, mouse_left_down ? "Down" : "Up");
render_ui_rect((float[2]){mouse_x, mouse_y}, (float[2]){32, 32},
(float[4]){1, 1, 1, 1});