Mighty zoom is back (scale system)

This commit is contained in:
KikooDX 2021-01-30 16:43:55 +01:00
parent 3361e859ed
commit 11d6ebef3c
3 changed files with 72 additions and 17 deletions

View File

@ -7,6 +7,7 @@ const std = @import("std");
const expect = std.testing.expect;
const movement = @import("movement.zig");
const scaling = @import("scaling.zig");
const Vec2 = @import("vec2.zig");
const Level = @import("level.zig");
@ -23,6 +24,7 @@ pub const Action = struct {
// 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_scale: fn (scaling.scale_type) scaling.scale_type = scaling.scale_reset,
};
pub const ActionsDef = .{
@ -110,4 +112,18 @@ pub const ActionsDef = .{
.category = ActionCat.movement,
.function_move = movement.move_down_right,
},
// Scale.
.scale_reset = Action{
.category = ActionCat.scale,
.function_scale = scaling.scale_reset,
},
.scale_up = Action{
.category = ActionCat.scale,
.function_scale = scaling.scale_up,
},
.scale_down = Action{
.category = ActionCat.scale,
.function_scale = scaling.scale_down,
},
};

View File

@ -15,6 +15,7 @@ const maxInt = std.math.maxInt;
const Level = @import("level.zig");
const Vec2 = @import("vec2.zig");
const movement = @import("movement.zig");
const scaling = @import("scaling.zig");
const actions = @import("actions.zig");
const ActionsDef = actions.ActionsDef;
const ActionCat = actions.ActionCat;
@ -42,17 +43,16 @@ pub fn main() !void {
// Create camera.
var camera: Vec2 = Vec2.init(0, 0);
// Scale, used by drawing code.
const scale_type = u16;
const scale_default = 16;
var scale: scale_type = scale_default;
// Init scale, used by drawing code.
var scale: scaling.scale_type = scaling.scale_default;
// Create cursor.
var cursor: Vec2 = Vec2.init(0, 0);
// Create binding "table".
var bindings = [_]*const actions.Action{&actions.ActionsDef.none} ** char_range;
// Set default bindings
// Set default bindings.
// Movement.
bindings['h'] = &ActionsDef.move_left;
bindings['H'] = &ActionsDef.move_LEFT;
bindings['j'] = &ActionsDef.move_down;
@ -69,8 +69,12 @@ pub fn main() !void {
bindings['B'] = &ActionsDef.move_DOWN_LEFT;
bindings['n'] = &ActionsDef.move_down_right;
bindings['N'] = &ActionsDef.move_DOWN_RIGHT;
// Scale.
bindings['='] = &ActionsDef.scale_reset;
bindings['+'] = &ActionsDef.scale_up;
bindings['-'] = &ActionsDef.scale_down;
// Create input buffer (ASCII).
// Create input buffer.
const input_buffer_len = 255;
var input_buffer: [input_buffer_len]u32 = undefined;
comptime {
@ -122,17 +126,7 @@ pub fn main() !void {
action.function_verb(&level);
},
.scale => {
// TODO: move this to functions, doesn't make any sense here
// Zoom (pog feature).
if (scale > 3) {
scale -= 1;
}
if (scale < comptime maxInt(scale_type)) {
scale += 1;
}
// Reset zoom.
scale = scale_default;
log("No action for {} key.", .{action});
scale = action.function_scale(scale);
},
}
}

45
src/scaling.zig Normal file
View File

@ -0,0 +1,45 @@
// 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.
//! Operates on a scale value used by drawing code.
const std = @import("std");
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub const scale_type = u16;
pub const scale_default: comptime_int = 16;
const scale_min: comptime_int = 3;
const scale_max: comptime_int = maxInt(scale_type);
pub fn scale_reset(current_scale: scale_type) scale_type {
return scale_default;
}
pub fn scale_up(current_scale: scale_type) scale_type {
if (current_scale < scale_max) {
return current_scale + 1;
} else return current_scale;
}
pub fn scale_down(current_scale: scale_type) scale_type {
if (current_scale > scale_min) {
return current_scale - 1;
} else return current_scale;
}
test "scale reset" {
expect(scale_reset(42) == scale_default);
}
test "scale up" {
expect(scale_up(42) == 43);
expect(scale_up(scale_max) == scale_max);
}
test "scale down" {
expect(scale_down(42) == 41);
expect(scale_down(scale_min) == scale_min);
}