2021-02-01 00:30:28 +01:00
|
|
|
// 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.
|
2021-02-01 11:16:44 +01:00
|
|
|
const ray = @cImport({
|
|
|
|
@cInclude("raylib.h");
|
|
|
|
});
|
2021-02-01 00:30:28 +01:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const Vec2 = @import("vec2.zig");
|
2021-02-01 11:16:44 +01:00
|
|
|
const Rect = @import("rect.zig");
|
|
|
|
const scaling = @import("scaling.zig");
|
2021-02-01 00:30:28 +01:00
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2021-02-01 11:16:44 +01:00
|
|
|
const preview_color = ray.SKYBLUE;
|
|
|
|
|
2021-02-01 00:30:28 +01:00
|
|
|
pub const MouseMode = enum {
|
2021-02-01 10:37:58 +01:00
|
|
|
wait, // Skip one turn.
|
2021-02-01 00:30:28 +01:00
|
|
|
idle,
|
|
|
|
sel,
|
|
|
|
rect_sel,
|
|
|
|
};
|
|
|
|
|
|
|
|
mode: MouseMode, // State.
|
|
|
|
pos: Vec2, // Where the cursor is.
|
|
|
|
start_pos: Vec2, // Used for rectangle selection.
|
|
|
|
|
2021-02-01 11:16:44 +01:00
|
|
|
/// Create structure with correct default values.
|
2021-02-01 00:30:28 +01:00
|
|
|
pub fn init() Self {
|
|
|
|
return Self{
|
|
|
|
.mode = MouseMode.idle, // state
|
|
|
|
.pos = Vec2{ .x = 0, .y = 0 },
|
|
|
|
.start_pos = Vec2{ .x = 0, .y = 0 },
|
|
|
|
};
|
|
|
|
}
|
2021-02-01 11:16:44 +01:00
|
|
|
|
|
|
|
/// 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 => {},
|
|
|
|
}
|
|
|
|
}
|