kble/src/mouse.zig

59 lines
1.8 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 = @import("raylib.zig");
const std = @import("std");
const conf = @import("conf.zig");
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,
unsel,
unrect_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) {
// Cell size cursor.
.idle, .wait, .sel, .unsel => {
const x = (self.pos.x - offset.y) * scale;
const y = (self.pos.y - offset.y) * scale;
ray.DrawRectangleLines(x, y, scale, scale, switch (self.mode) {
.sel => conf.theme.mode.select,
.unsel => conf.theme.mode.unselect,
else => conf.theme.mode.normal,
});
},
.rect_sel, .unrect_sel => {
const rect = Rect.init_from_vec2(self.pos, self.start_pos);
draw_fn.rectangle_selection(scale, offset, rect, self.mode == .rect_sel);
},
}
}