kble/src/mouse.zig

54 lines
1.5 KiB
Zig

// SPDX-License-Identifier: MIT
// Copyright (c) 2021 KikooDX
// This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is
// 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 draw_fn = @import("draw.zig");
const Self = @This();
pub const MouseMode = enum {
wait, // Skip one turn.
idle,
sel,
rect_sel,
};
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
.pos = Vec2{ .x = 0, .y = 0 },
.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, ray.SKYBLUE);
},
MouseMode.rect_sel => {
const rect = Rect.init_from_vec2(self.pos, self.start_pos);
draw_fn.rectangle_selection(scale, offset, rect);
},
else => {},
}
}