mirror of https://git.sr.ht/~kikoodx/kble
First action (delete) for testing, misc improvements here and there.
This commit is contained in:
parent
1750fe8cd0
commit
178d6e53df
|
@ -34,10 +34,10 @@ pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Self {
|
|||
errdefer allocator.free(self.content);
|
||||
self.selection = try allocator.alloc(bool, size);
|
||||
|
||||
// Fill with 0s to avoid undefined behavior.
|
||||
// Fill with default value to avoid undefined behavior.
|
||||
var i: u32 = 0;
|
||||
while (i < size) : (i += 1) {
|
||||
self.content[i] = 0;
|
||||
self.content[i] = 1;
|
||||
self.selection[i] = false;
|
||||
}
|
||||
return self;
|
||||
|
@ -62,7 +62,7 @@ pub fn draw(self: *Self, scale: u16, offset: Vec2) void {
|
|||
const cell_content: cell_t = self.content[cy * self.width + cx];
|
||||
const color = switch (cell_content) {
|
||||
0 => ray.GRAY,
|
||||
1 => ray.WHITE,
|
||||
1 => ray.RAYWHITE,
|
||||
else => ray.PURPLE, //unknown
|
||||
};
|
||||
ray.DrawRectangle(x + 1, y + 1, scale - 2, scale - 2, color);
|
||||
|
@ -125,6 +125,7 @@ fn select_rect(self: *Self, rect: Rect, state: bool) void {
|
|||
}
|
||||
}
|
||||
|
||||
/// Apply selection update to selection *kof*.
|
||||
pub fn apply_selection_update(self: *Self, selection_update: SelectionUpdate) void {
|
||||
// The update is exclusive, forget everything before it.
|
||||
if (selection_update.exclusive) {
|
||||
|
@ -136,6 +137,15 @@ 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);
|
||||
|
|
|
@ -34,7 +34,8 @@ pub fn main() !void {
|
|||
|
||||
// Scale, used by drawing code.
|
||||
const scale_type = u16;
|
||||
var scale: scale_type = 16;
|
||||
const scale_default = 16;
|
||||
var scale: scale_type = scale_default;
|
||||
|
||||
// Create cursor.
|
||||
var cursor: Vec2 = Vec2.init(0, 0);
|
||||
|
@ -93,6 +94,8 @@ pub fn main() !void {
|
|||
'U' => selection_update = movement.move_up_right(&cursor, 1, false),
|
||||
'B' => selection_update = movement.move_down_left(&cursor, 1, false),
|
||||
'N' => selection_update = movement.move_down_right(&cursor, 1, false),
|
||||
// Actions.
|
||||
'd' => level.action_delete(),
|
||||
// Zoom (pog feature).
|
||||
'-' => if (scale > 3) {
|
||||
scale -= 1;
|
||||
|
@ -101,7 +104,7 @@ pub fn main() !void {
|
|||
scale += 1;
|
||||
},
|
||||
// Reset zoom.
|
||||
'0' => scale = 4,
|
||||
'0' => scale = scale_default,
|
||||
else => log("No action mapped to key {}.", .{action}),
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue