diff options
Diffstat (limited to 'ui_sdl.zig')
| -rw-r--r-- | ui_sdl.zig | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -66,7 +66,7 @@ pub fn Loop(comptime EventType: type) type { return SdlLoop(EventType); } -const font_ttf = @embedFile("assets/MapleMono-NF-Regular.ttf"); +const font_ttf = @embedFile("assets/AdwaitaMono-Regular.ttf"); const vert_spv_bytes = if (is_emscripten) "" else @embedFile("ui.vert.spv"); const frag_spv_bytes = if (is_emscripten) "" else @embedFile("ui.frag.spv"); const vert_glsl_es = if (is_emscripten) @embedFile("ui.vert.es.glsl") else ""; @@ -932,6 +932,20 @@ fn floorToI16(value: f32) i16 { return @intFromFloat(floored); } +fn mousePixel(self: *const SdlBackend, x: f32, y: f32, pixel_w: u32, pixel_h: u32) struct { x: f32, y: f32 } { + if (!is_emscripten) return .{ .x = x, .y = y }; + + var ww: c_int = 0; + var wh: c_int = 0; + _ = c.SDL_GetWindowSize(self.sdl_window, &ww, &wh); + if (ww <= 0 or wh <= 0) return .{ .x = x, .y = y }; + + return .{ + .x = x * (@as(f32, @floatFromInt(pixel_w)) / @as(f32, @floatFromInt(ww))), + .y = y * (@as(f32, @floatFromInt(pixel_h)) / @as(f32, @floatFromInt(wh))), + }; +} + fn mouseCell(self: *const SdlBackend, x: f32, y: f32) MouseCell { if (!is_emscripten) return self.fixedMouseCell(x, y); @@ -941,9 +955,10 @@ fn mouseCell(self: *const SdlBackend, x: f32, y: f32) MouseCell { const sw: u32 = @max(1, @as(u32, @intCast(pw))); const sh: u32 = @max(1, @as(u32, @intCast(ph))); const layout = self.cellLayout(sw, sh); + const pixel = self.mousePixel(x, y, sw, sh); return .{ - .col = floorToI16((x - layout.x_off) / layout.w), - .row = floorToI16((y - layout.y_off) / layout.h), + .col = floorToI16((pixel.x - layout.x_off) / layout.w), + .row = floorToI16((pixel.y - layout.y_off) / layout.h), }; } |
