mirror of https://git.sr.ht/~kikoodx/kble
Create conf.zig, verb.zig ; clean up stuff and add replace verb + move... but don't
This commit is contained in:
parent
f14c60c630
commit
1ebed24110
|
@ -32,9 +32,11 @@ Movement, hold Shift to expand selection:
|
|||
* `u`: up-right
|
||||
* `b`: down-left
|
||||
* `n`: down-right
|
||||
* `<space>`: reset selection
|
||||
|
||||
Actions:
|
||||
* `d`: delete selection
|
||||
* `r`: replace selection
|
||||
* `+`: increase scale (zoom)
|
||||
* `-`: decrease scale (dezoom)
|
||||
* `=`: reset scale
|
||||
|
|
|
@ -6,8 +6,10 @@
|
|||
const std = @import("std");
|
||||
const expect = std.testing.expect;
|
||||
|
||||
const conf = @import("conf.zig");
|
||||
const movement = @import("movement.zig");
|
||||
const scaling = @import("scaling.zig");
|
||||
const verbs = @import("verbs.zig");
|
||||
const Vec2 = @import("vec2.zig");
|
||||
const Level = @import("level.zig");
|
||||
|
||||
|
@ -22,8 +24,8 @@ pub const Action = struct {
|
|||
category: ActionCat,
|
||||
toggle: bool = false, // bool that can be passed to actions, i.e. exclusive for movement
|
||||
// Only one of these should be set, and only one should be used.
|
||||
function_move: fn (*Vec2, u32, bool) movement.SelectionUpdate = movement.move_left,
|
||||
function_verb: fn (*Level) void = Level.action_delete,
|
||||
function_move: fn (*Vec2, conf.arg_type, bool) movement.SelectionUpdate = movement.move_left,
|
||||
function_verb: fn (*Level, conf.arg_type) void = verbs.delete,
|
||||
function_scale: fn (scaling.scale_type) scaling.scale_type = scaling.scale_reset,
|
||||
};
|
||||
|
||||
|
@ -32,6 +34,12 @@ pub const ActionsDef = .{
|
|||
.category = ActionCat.none,
|
||||
},
|
||||
// Movement.
|
||||
// Reset selection.
|
||||
.move_but_dont = Action{
|
||||
.toggle = true,
|
||||
.category = ActionCat.movement,
|
||||
.function_move = movement.move_but_dont,
|
||||
},
|
||||
// Left.
|
||||
.move_left = Action{
|
||||
.toggle = true,
|
||||
|
@ -116,7 +124,11 @@ pub const ActionsDef = .{
|
|||
// Verbs.
|
||||
.verb_delete = Action{
|
||||
.category = ActionCat.verb,
|
||||
.function_verb = Level.action_delete,
|
||||
.function_verb = verbs.delete,
|
||||
},
|
||||
.verb_replace = Action{
|
||||
.category = ActionCat.verb,
|
||||
.function_verb = verbs.replace,
|
||||
},
|
||||
|
||||
// Scale.
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
// 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.
|
||||
//! Values and types used by different files and that don't fit in any.
|
||||
/// Number passed to commands.
|
||||
pub const arg_type = u16;
|
|
@ -16,11 +16,11 @@ const SelectionUpdate = @import("movement.zig").SelectionUpdate;
|
|||
|
||||
const Self = @This();
|
||||
|
||||
const cell_t = u16;
|
||||
pub const cell_type = u16;
|
||||
|
||||
width: u16,
|
||||
height: u16,
|
||||
content: []cell_t,
|
||||
content: []cell_type,
|
||||
selection: []bool,
|
||||
|
||||
/// Create structure and allocate required memory. The `content` array size will
|
||||
|
@ -35,7 +35,7 @@ pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Self {
|
|||
|
||||
// Try to allocate necessary memory.
|
||||
const size: u32 = @intCast(u32, width) * @intCast(u32, height);
|
||||
self.content = try allocator.alloc(cell_t, size);
|
||||
self.content = try allocator.alloc(cell_type, size);
|
||||
errdefer allocator.free(self.content);
|
||||
self.selection = try allocator.alloc(bool, size);
|
||||
|
||||
|
@ -64,7 +64,7 @@ pub fn draw(self: *Self, scale: u16, offset: Vec2) void {
|
|||
while (cx < self.width) {
|
||||
var cy: Vec2.int_type = offset.y;
|
||||
while (cy < self.height) {
|
||||
const cell_content: cell_t = self.content[cy * self.width + cx];
|
||||
const cell_content: cell_type = self.content[cy * self.width + cx];
|
||||
const color = switch (cell_content) {
|
||||
0 => ray.BLACK,
|
||||
1 => ray.GRAY,
|
||||
|
@ -142,15 +142,6 @@ pub fn apply_selection_update(self: *Self, selection_update: SelectionUpdate) vo
|
|||
self.select_rect(selection_update.area, selection_update.state);
|
||||
}
|
||||
|
||||
/// Delete selected cells (set to 0).
|
||||
pub fn action_delete(self: *Self) void {
|
||||
var i: u32 = 0;
|
||||
while (i < self.width * self.height) : (i += 1) {
|
||||
if (self.selection[i])
|
||||
self.content[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
test "create level buffer" {
|
||||
// Create allocator.
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
|
|
11
src/main.zig
11
src/main.zig
|
@ -7,9 +7,6 @@ const ray = @cImport({
|
|||
@cInclude("raylib.h");
|
||||
});
|
||||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
const format = std.fmt.format;
|
||||
const log = std.log.default.debug;
|
||||
const maxInt = std.math.maxInt;
|
||||
|
||||
const Level = @import("level.zig");
|
||||
|
@ -53,6 +50,7 @@ pub fn main() !void {
|
|||
var bindings = [_]*const actions.Action{&actions.ActionsDef.none} ** char_range;
|
||||
// Set default bindings.
|
||||
// Movement.
|
||||
bindings[' '] = &ActionsDef.move_but_dont;
|
||||
bindings['h'] = &ActionsDef.move_left;
|
||||
bindings['H'] = &ActionsDef.move_LEFT;
|
||||
bindings['j'] = &ActionsDef.move_down;
|
||||
|
@ -69,8 +67,9 @@ pub fn main() !void {
|
|||
bindings['B'] = &ActionsDef.move_DOWN_LEFT;
|
||||
bindings['n'] = &ActionsDef.move_down_right;
|
||||
bindings['N'] = &ActionsDef.move_DOWN_RIGHT;
|
||||
// Verbs
|
||||
// Verbs.
|
||||
bindings['d'] = &ActionsDef.verb_delete;
|
||||
bindings['r'] = &ActionsDef.verb_replace;
|
||||
// Scale.
|
||||
bindings['='] = &ActionsDef.scale_reset;
|
||||
bindings['+'] = &ActionsDef.scale_up;
|
||||
|
@ -116,7 +115,7 @@ pub fn main() !void {
|
|||
const action: actions.Action = bindings[key].*;
|
||||
|
||||
switch (action.category) {
|
||||
.none => {},
|
||||
.none => std.log.info("No action bound to {}.", .{key}),
|
||||
.movement => {
|
||||
const selection_update: movement.SelectionUpdate =
|
||||
action.function_move(&cursor, 1, action.toggle);
|
||||
|
@ -125,7 +124,7 @@ pub fn main() !void {
|
|||
}
|
||||
},
|
||||
.verb => {
|
||||
action.function_verb(&level);
|
||||
action.function_verb(&level, 1);
|
||||
},
|
||||
.scale => {
|
||||
scale = action.function_scale(scale);
|
||||
|
|
|
@ -8,6 +8,7 @@ const std = @import("std");
|
|||
const expect = std.testing.expect;
|
||||
const maxInt = std.math.maxInt;
|
||||
|
||||
const conf = @import("conf.zig");
|
||||
const Vec2 = @import("vec2.zig");
|
||||
const Rect = @import("rect.zig");
|
||||
|
||||
|
@ -22,28 +23,28 @@ pub const SelectionUpdate = struct {
|
|||
};
|
||||
|
||||
/// Universal move system, prefer direction wrappers.
|
||||
fn move(cursor: *Vec2, n: u32, exclusive_selection: bool, dx: i2, dy: i2) SelectionUpdate {
|
||||
fn move(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool, dx: i2, dy: i2) SelectionUpdate {
|
||||
const before: Vec2 = cursor.*;
|
||||
|
||||
if (dx > 0) {
|
||||
var i: u32 = 0;
|
||||
while (i != n and cursor.x < maxIntVec2) : (i += 1) {
|
||||
var i: conf.arg_type = 0;
|
||||
while (i != arg and cursor.x < maxIntVec2) : (i += 1) {
|
||||
cursor.x += 1;
|
||||
}
|
||||
} else if (dx < 0) {
|
||||
var i: u32 = 0;
|
||||
while (i != n and cursor.x > 0) : (i += 1) {
|
||||
var i: conf.arg_type = 0;
|
||||
while (i != arg and cursor.x > 0) : (i += 1) {
|
||||
cursor.x -= 1;
|
||||
}
|
||||
}
|
||||
if (dy > 0) {
|
||||
var i: u32 = 0;
|
||||
while (i != n and cursor.y < maxIntVec2) : (i += 1) {
|
||||
var i: conf.arg_type = 0;
|
||||
while (i != arg and cursor.y < maxIntVec2) : (i += 1) {
|
||||
cursor.y += 1;
|
||||
}
|
||||
} else if (dy < 0) {
|
||||
var i: u32 = 0;
|
||||
while (i != n and cursor.y > 0) : (i += 1) {
|
||||
var i: conf.arg_type = 0;
|
||||
while (i != arg and cursor.y > 0) : (i += 1) {
|
||||
cursor.y -= 1;
|
||||
}
|
||||
}
|
||||
|
@ -59,44 +60,49 @@ fn move(cursor: *Vec2, n: u32, exclusive_selection: bool, dx: i2, dy: i2) Select
|
|||
};
|
||||
}
|
||||
|
||||
/// Just don't move.
|
||||
pub fn move_but_dont(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, 0, 0);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times left.
|
||||
pub fn move_left(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, -1, 0);
|
||||
pub fn move_left(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, -1, 0);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times right.
|
||||
pub fn move_right(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, 1, 0);
|
||||
pub fn move_right(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, 1, 0);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times up.
|
||||
pub fn move_up(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, 0, -1);
|
||||
pub fn move_up(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, 0, -1);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times down.
|
||||
pub fn move_down(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, 0, 1);
|
||||
pub fn move_down(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, 0, 1);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times up and left.
|
||||
pub fn move_up_left(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, -1, -1);
|
||||
pub fn move_up_left(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, -1, -1);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times up and right.
|
||||
pub fn move_up_right(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, 1, -1);
|
||||
pub fn move_up_right(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, 1, -1);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times down and left.
|
||||
pub fn move_down_left(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, -1, 1);
|
||||
pub fn move_down_left(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, -1, 1);
|
||||
}
|
||||
|
||||
/// Try to move the cursor `n` times down and right.
|
||||
pub fn move_down_right(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, n, exclusive_selection, 1, 1);
|
||||
pub fn move_down_right(cursor: *Vec2, arg: conf.arg_type, exclusive_selection: bool) SelectionUpdate {
|
||||
return move(cursor, arg, exclusive_selection, 1, 1);
|
||||
}
|
||||
|
||||
test "move left" {
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
// 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.
|
||||
//! Act on level using selection.
|
||||
const conf = @import("conf.zig");
|
||||
const Level = @import("level.zig");
|
||||
|
||||
/// Delete selected cells (set to 0).
|
||||
pub fn delete(level: *Level, arg: conf.arg_type) void {
|
||||
var i: u32 = 0;
|
||||
while (i < level.width * level.height) : (i += 1) {
|
||||
if (level.selection[i])
|
||||
level.content[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace selected cells with `arg`.
|
||||
pub fn replace(level: *Level, arg: conf.arg_type) void {
|
||||
const casted_arg = @intCast(Level.cell_type, arg);
|
||||
var i: u32 = 0;
|
||||
while (i < level.width * level.height) : (i += 1) {
|
||||
if (level.selection[i])
|
||||
level.content[i] = arg;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue