2021-01-29 12:32:38 +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.
|
2021-01-24 19:22:48 +01:00
|
|
|
const ray = @cImport({
|
|
|
|
@cInclude("raylib.h");
|
|
|
|
});
|
2021-01-26 17:44:40 +01:00
|
|
|
const std = @import("std");
|
2021-01-28 16:12:41 +01:00
|
|
|
const maxInt = std.math.maxInt;
|
2021-02-01 00:30:28 +01:00
|
|
|
const log = std.log.info;
|
2021-01-26 17:44:40 +01:00
|
|
|
|
2021-02-01 00:30:28 +01:00
|
|
|
const conf = @import("conf.zig");
|
2021-01-26 17:44:40 +01:00
|
|
|
const Level = @import("level.zig");
|
|
|
|
const Vec2 = @import("vec2.zig");
|
2021-02-01 00:30:28 +01:00
|
|
|
const Rect = @import("rect.zig");
|
|
|
|
const Mouse = @import("mouse.zig");
|
2021-01-27 17:23:45 +01:00
|
|
|
const movement = @import("movement.zig");
|
2021-01-30 16:43:55 +01:00
|
|
|
const scaling = @import("scaling.zig");
|
2021-01-30 16:13:06 +01:00
|
|
|
const actions = @import("actions.zig");
|
|
|
|
const ActionsDef = actions.ActionsDef;
|
|
|
|
const ActionCat = actions.ActionCat;
|
|
|
|
|
|
|
|
const char_range = 255;
|
2021-01-24 19:22:48 +01:00
|
|
|
|
2021-01-26 13:10:16 +01:00
|
|
|
pub fn main() !void {
|
|
|
|
// Create allocator
|
2021-01-26 23:38:41 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
const allocator = &arena.allocator;
|
2021-01-24 19:22:48 +01:00
|
|
|
|
2021-01-26 17:44:40 +01:00
|
|
|
// Create window.
|
2021-01-24 19:22:48 +01:00
|
|
|
ray.SetConfigFlags(ray.FLAG_WINDOW_RESIZABLE);
|
2021-01-26 13:10:16 +01:00
|
|
|
ray.InitWindow(640, 480, "KBLE");
|
2021-01-24 19:22:48 +01:00
|
|
|
defer ray.CloseWindow();
|
|
|
|
|
2021-01-26 17:44:40 +01:00
|
|
|
// Limit FPS for performance.
|
2021-01-24 19:22:48 +01:00
|
|
|
ray.SetTargetFPS(60);
|
|
|
|
|
2021-01-27 14:12:44 +01:00
|
|
|
// Create level.
|
2021-01-28 16:52:49 +01:00
|
|
|
var level: Level = try Level.init(allocator, 16, 16);
|
2021-01-26 13:10:16 +01:00
|
|
|
defer level.deinit(allocator);
|
|
|
|
|
2021-01-27 14:12:44 +01:00
|
|
|
// Create camera.
|
2021-01-26 17:44:40 +01:00
|
|
|
var camera: Vec2 = Vec2.init(0, 0);
|
|
|
|
|
2021-01-30 16:43:55 +01:00
|
|
|
// Init scale, used by drawing code.
|
|
|
|
var scale: scaling.scale_type = scaling.scale_default;
|
2021-01-28 16:12:41 +01:00
|
|
|
|
2021-02-01 00:30:28 +01:00
|
|
|
// Create mouse.
|
|
|
|
var mouse: Mouse = Mouse.init();
|
|
|
|
|
2021-01-28 13:46:02 +01:00
|
|
|
// Create cursor.
|
|
|
|
var cursor: Vec2 = Vec2.init(0, 0);
|
|
|
|
|
2021-01-30 16:13:06 +01:00
|
|
|
// Create binding "table".
|
|
|
|
var bindings = [_]*const actions.Action{&actions.ActionsDef.none} ** char_range;
|
2021-01-30 16:43:55 +01:00
|
|
|
// Set default bindings.
|
|
|
|
// Movement.
|
2021-01-30 23:59:25 +01:00
|
|
|
bindings[' '] = &ActionsDef.move_but_dont;
|
2021-01-30 16:13:06 +01:00
|
|
|
bindings['h'] = &ActionsDef.move_left;
|
|
|
|
bindings['H'] = &ActionsDef.move_LEFT;
|
|
|
|
bindings['j'] = &ActionsDef.move_down;
|
|
|
|
bindings['J'] = &ActionsDef.move_DOWN;
|
|
|
|
bindings['k'] = &ActionsDef.move_up;
|
|
|
|
bindings['K'] = &ActionsDef.move_UP;
|
|
|
|
bindings['l'] = &ActionsDef.move_right;
|
|
|
|
bindings['L'] = &ActionsDef.move_RIGHT;
|
|
|
|
bindings['y'] = &ActionsDef.move_up_left;
|
|
|
|
bindings['Y'] = &ActionsDef.move_UP_LEFT;
|
|
|
|
bindings['u'] = &ActionsDef.move_up_right;
|
|
|
|
bindings['U'] = &ActionsDef.move_UP_RIGHT;
|
|
|
|
bindings['b'] = &ActionsDef.move_down_left;
|
|
|
|
bindings['B'] = &ActionsDef.move_DOWN_LEFT;
|
|
|
|
bindings['n'] = &ActionsDef.move_down_right;
|
|
|
|
bindings['N'] = &ActionsDef.move_DOWN_RIGHT;
|
2021-01-30 23:59:25 +01:00
|
|
|
// Verbs.
|
2021-01-30 16:47:49 +01:00
|
|
|
bindings['d'] = &ActionsDef.verb_delete;
|
2021-01-30 23:59:25 +01:00
|
|
|
bindings['r'] = &ActionsDef.verb_replace;
|
2021-01-30 16:43:55 +01:00
|
|
|
// Scale.
|
|
|
|
bindings['='] = &ActionsDef.scale_reset;
|
|
|
|
bindings['+'] = &ActionsDef.scale_up;
|
|
|
|
bindings['-'] = &ActionsDef.scale_down;
|
2021-01-30 16:13:06 +01:00
|
|
|
|
2021-01-30 16:43:55 +01:00
|
|
|
// Create input buffer.
|
2021-01-27 19:26:12 +01:00
|
|
|
const input_buffer_len = 255;
|
2021-01-27 14:12:44 +01:00
|
|
|
var input_buffer: [input_buffer_len]u32 = undefined;
|
|
|
|
comptime {
|
2021-01-27 19:26:12 +01:00
|
|
|
var i: u8 = 0;
|
2021-01-27 14:12:44 +01:00
|
|
|
while (i < input_buffer_len) : (i += 1) {
|
|
|
|
input_buffer[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 19:26:12 +01:00
|
|
|
var input_cursor: u8 = 0;
|
2021-01-27 14:12:44 +01:00
|
|
|
|
2021-01-26 13:10:16 +01:00
|
|
|
while (!ray.WindowShouldClose()) {
|
2021-01-30 16:13:06 +01:00
|
|
|
{
|
|
|
|
// Get keyboard input.
|
|
|
|
var key = ray.GetCharPressed();
|
|
|
|
// Check if more characters have been pressed.
|
|
|
|
while (key != 0) {
|
|
|
|
// Add key to buffer.
|
|
|
|
input_buffer[input_cursor] = @intCast(u32, key);
|
|
|
|
input_cursor += 1;
|
|
|
|
// Avoid writing out of memory.
|
|
|
|
if (input_cursor >= input_buffer_len)
|
|
|
|
input_cursor = input_buffer_len - 1;
|
|
|
|
|
|
|
|
key = ray.GetCharPressed();
|
|
|
|
}
|
2021-01-27 14:12:44 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 17:23:45 +01:00
|
|
|
// Process buffer content.
|
|
|
|
// Read the buffer backwards. This is placeholder logic.
|
|
|
|
while (input_cursor > 0) {
|
|
|
|
input_cursor -= 1;
|
2021-01-30 16:13:06 +01:00
|
|
|
const key = if (input_buffer[input_cursor] <= char_range)
|
|
|
|
input_buffer[input_cursor]
|
|
|
|
else
|
|
|
|
0;
|
|
|
|
|
|
|
|
const action: actions.Action = bindings[key].*;
|
|
|
|
|
|
|
|
switch (action.category) {
|
2021-01-30 23:59:25 +01:00
|
|
|
.none => std.log.info("No action bound to {}.", .{key}),
|
2021-01-30 16:13:06 +01:00
|
|
|
.movement => {
|
|
|
|
const selection_update: movement.SelectionUpdate =
|
|
|
|
action.function_move(&cursor, 1, action.toggle);
|
|
|
|
if (selection_update.active) {
|
|
|
|
level.apply_selection_update(selection_update);
|
|
|
|
}
|
2021-01-28 16:12:41 +01:00
|
|
|
},
|
2021-01-30 16:13:06 +01:00
|
|
|
.verb => {
|
2021-01-30 23:59:25 +01:00
|
|
|
action.function_verb(&level, 1);
|
2021-01-30 16:13:06 +01:00
|
|
|
},
|
|
|
|
.scale => {
|
2021-01-30 16:43:55 +01:00
|
|
|
scale = action.function_scale(scale);
|
2021-01-30 16:13:06 +01:00
|
|
|
},
|
2021-01-29 18:06:41 +01:00
|
|
|
}
|
2021-01-27 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 00:30:28 +01:00
|
|
|
// Mouse operations.
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-02-01 10:37:58 +01:00
|
|
|
const left_click: bool = ray.IsMouseButtonPressed(conf.mouse_left_btn);
|
|
|
|
const right_click: bool = ray.IsMouseButtonPressed(conf.mouse_right_btn);
|
|
|
|
const click: bool = left_click or right_click;
|
|
|
|
|
|
|
|
// If click, get out of current mode and apply changes if necessary.
|
|
|
|
if (click and mouse.mode != Mouse.MouseMode.idle) {
|
|
|
|
defer mouse.mode = Mouse.MouseMode.wait;
|
|
|
|
|
|
|
|
// Select area.
|
|
|
|
if (mouse.mode == Mouse.MouseMode.rect_sel) {
|
|
|
|
const selection = Rect.init_from_vec2(mouse.start_pos, mouse.pos);
|
|
|
|
level.select_rect(selection, true);
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 00:30:28 +01:00
|
|
|
|
|
|
|
// State machine.
|
|
|
|
switch (mouse.mode) {
|
2021-02-01 10:37:58 +01:00
|
|
|
.wait => mouse.mode = Mouse.MouseMode.idle,
|
2021-02-01 00:30:28 +01:00
|
|
|
// See if can switch mode.
|
|
|
|
.idle => {
|
|
|
|
const mod_rect_sel: bool = ray.IsKeyDown(ray.KEY_LEFT_SHIFT) or
|
|
|
|
ray.IsKeyDown(ray.KEY_RIGHT_SHIFT);
|
2021-02-01 10:37:58 +01:00
|
|
|
if (click) { // Switch mode.
|
2021-02-01 00:30:28 +01:00
|
|
|
cursor = mouse.pos;
|
2021-02-01 10:37:58 +01:00
|
|
|
// Reset selection on right click.
|
|
|
|
if (right_click) {
|
|
|
|
level.apply_selection_update(movement.move_but_dont(&cursor, 1, true));
|
|
|
|
}
|
|
|
|
// Set position.
|
2021-02-01 00:30:28 +01:00
|
|
|
mouse.start_pos = mouse.pos;
|
|
|
|
mouse.mode = if (mod_rect_sel)
|
|
|
|
Mouse.MouseMode.rect_sel
|
|
|
|
else
|
|
|
|
Mouse.MouseMode.sel;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Select stuff under the cursor.
|
|
|
|
.sel => {
|
|
|
|
cursor = mouse.pos;
|
|
|
|
level.select_cell(mouse.pos, true);
|
|
|
|
},
|
|
|
|
// Rectangle selection, see `left_release` for handling.
|
|
|
|
.rect_sel => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 19:22:48 +01:00
|
|
|
ray.BeginDrawing();
|
|
|
|
defer ray.EndDrawing();
|
|
|
|
|
2021-01-26 17:44:40 +01:00
|
|
|
ray.ClearBackground(ray.BLACK);
|
2021-01-28 16:12:41 +01:00
|
|
|
level.draw(scale, camera);
|
|
|
|
level.draw_selection(scale, camera);
|
2021-01-28 13:46:02 +01:00
|
|
|
//ray.DrawFPS(0, 0);
|
2021-01-24 19:22:48 +01:00
|
|
|
}
|
|
|
|
}
|