diff --git a/src/main.zig b/src/main.zig index 38edaf9..ced4376 100644 --- a/src/main.zig +++ b/src/main.zig @@ -143,13 +143,11 @@ pub fn main() !void { if (conf.mouse_enabled) { // Update position. { - // Set mouse offset. - ray.SetMouseOffset(camera.x, camera.y); // Set mouse scaling. ray.SetMouseScale(1.0 / @intToFloat(f32, scale), 1.0 / @intToFloat(f32, scale)); - mouse.pos.x = @intCast(u16, ray.GetMouseX()); - mouse.pos.y = @intCast(u16, ray.GetMouseY()); + mouse.pos.x = @intCast(u16, ray.GetMouseX() + camera.x); + mouse.pos.y = @intCast(u16, ray.GetMouseY() + camera.y); } const left_click: bool = ray.IsMouseButtonPressed(conf.mouse_left_btn); @@ -193,8 +191,7 @@ pub fn main() !void { cursor = mouse.pos; level.select_cell(mouse.pos, true); }, - // Rectangle selection, see `left_release` for handling. - .rect_sel => {}, + else => {}, } } @@ -204,6 +201,7 @@ pub fn main() !void { ray.ClearBackground(ray.BLACK); level.draw(scale, camera); level.draw_selection(scale, camera); + mouse.draw(scale, camera); //ray.DrawFPS(0, 0); } } diff --git a/src/mouse.zig b/src/mouse.zig index 4080172..4a82549 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -4,12 +4,19 @@ // MIT licensed. The MIT license requires this copyright notice to be // included in all copies and substantial portions of the software. //! Mouse (eurk) logic and operations. +const ray = @cImport({ + @cInclude("raylib.h"); +}); const std = @import("std"); const Vec2 = @import("vec2.zig"); +const Rect = @import("rect.zig"); +const scaling = @import("scaling.zig"); const Self = @This(); +const preview_color = ray.SKYBLUE; + pub const MouseMode = enum { wait, // Skip one turn. idle, @@ -21,6 +28,7 @@ mode: MouseMode, // State. pos: Vec2, // Where the cursor is. start_pos: Vec2, // Used for rectangle selection. +/// Create structure with correct default values. pub fn init() Self { return Self{ .mode = MouseMode.idle, // state @@ -28,3 +36,23 @@ pub fn init() Self { .start_pos = Vec2{ .x = 0, .y = 0 }, }; } + +/// Draw cursor or preview rectangle selection depending on mode. +pub fn draw(self: *Self, scale: scaling.scale_type, offset: Vec2) void { + switch (self.mode) { + MouseMode.sel => { + const x = (self.pos.x - offset.y) * scale; + const y = (self.pos.y - offset.y) * scale; + ray.DrawRectangleLines(x, y, scale, scale, preview_color); + }, + MouseMode.rect_sel => { + const rect = Rect.init_from_vec2(self.pos, self.start_pos); + const x = (rect.left_x - offset.x) * scale; + const w = (rect.right_x - rect.left_x + 1) * scale; + const y = (rect.top_y - offset.y) * scale; + const h = (rect.bottom_y - rect.top_y + 1) * scale; + ray.DrawRectangleLines(x, y, w, h, preview_color); + }, + else => {}, + } +}