~mil/mepo

eb46625b39962ab068d40687d35cdff8e7f2949c — Miles Alan 2 years ago 55db723 bind_gesture
Implement logic bind_gesture command / table storage
4 files changed, 34 insertions(+), 0 deletions(-)

M src/Mepo.zig
M src/api/_FnTable.zig
A src/api/bind_gesture.zig
M src/types.zig
M src/Mepo.zig => src/Mepo.zig +2 -0
@@ 41,6 41,7 @@ pin_group_active: u8 = 0,
pin_group_active_item: ?u32 = null,
pin_groups: [10]types.PinGroup = undefined,
renderer: *sdl.SDL_Renderer,
table_gestures: std.array_hash_map.AutoArrayHashMap(types.GestureInput, []const u8),
table_clicks: std.array_hash_map.AutoArrayHashMap(types.ClickInput, []const u8),
table_keybindings: std.array_hash_map.AutoArrayHashMap(types.KeyInput, []const u8),
table_signals: std.array_hash_map.AutoArrayHashMap(u6, []const u8),


@@ 1254,6 1255,7 @@ pub fn init(allocator: std.mem.Allocator, tile_cache: *TileCache, use_sw_rendere
            -1,
            if (use_sw_renderer) sdl.SDL_RENDERER_SOFTWARE else sdl.SDL_RENDERER_ACCELERATED,
        )),
        .table_gestures = std.array_hash_map.AutoArrayHashMap(types.GestureInput, []const u8).init(allocator),
        .table_keybindings = std.array_hash_map.AutoArrayHashMap(types.KeyInput, []const u8).init(allocator),
        .table_clicks = std.array_hash_map.AutoArrayHashMap(types.ClickInput, []const u8).init(allocator),
        .table_signals = std.array_hash_map.AutoArrayHashMap(u6, []const u8).init(allocator),

M src/api/_FnTable.zig => src/api/_FnTable.zig +1 -0
@@ 5,6 5,7 @@ const fn_bindings = &[_]struct { name: []const u8, func: types.MepoFn }{
    .{ .name = "bind_button", .func = @import("bind_button.zig").f_bind_button },
    .{ .name = "bind_click", .func = @import("bind_click.zig").f_bind_click },
    .{ .name = "bind_key", .func = @import("bind_key.zig").f_bind_key },
    .{ .name = "bind_gesture", .func = @import("bind_gesture.zig").f_bind_gesture },
    .{ .name = "bind_signal", .func = @import("bind_signal.zig").f_bind_signal },
    .{ .name = "center_on_coords", .func = @import("center_on_coords.zig").f_center_on_coords },
    .{ .name = "center_on_mouse", .func = @import("center_on_mouse.zig").f_center_on_mouse },

A src/api/bind_gesture.zig => src/api/bind_gesture.zig +28 -0
@@ 0,0 1,28 @@
const Mepo = @import("../Mepo.zig");
const types = @import("../types.zig");
const std = @import("std");
const sdl = @import("../sdlshim.zig");
const utildbg = @import("../util/utildbg.zig");

pub fn f_bind_gesture(mepo: *Mepo, args: [types.MepoFnNargs]types.MepoArg) !void {
    const action : types.GestureInputAction = if (std.mem.eql(u8, args[0].Text, "pan")) .Pan else .Rotate;
    const direction : types.GestureInputDirection = if (args[1].Number == -1) .In else .Out;
    const expression = args[2].Text;
    try bind_gesture(mepo, action, direction, expression);
}

fn bind_gesture(mepo: *Mepo, action: types.GestureInputAction, direction: types.GestureInputDirection, expression: []const u8) !void {
    utildbg.log("Binding gesture: action={} direction={} to expression {s}\n", .{ action, direction, expression });

    const gest = .{
        .action = action,
        .direction = direction,
    };

    if (mepo.table_gestures.get(gest)) |heap_str| {
        mepo.allocator.free(heap_str);
        _ = mepo.table_gestures.swapRemove(gest);
    }
    const expression_mepo_allocated = try mepo.allocator.dupe(u8, expression);
    try mepo.table_gestures.put(gest, expression_mepo_allocated);
}

M src/types.zig => src/types.zig +3 -0
@@ 31,6 31,9 @@ pub const MepoFnTable = std.array_hash_map.StringArrayHashMap(MepoFn);

pub const KeyInput = struct { key: u8, keymod: sdl.SDL_Keymod };
pub const ClickInput = struct { button: u8, clicks: i8 };
pub const GestureInputAction = enum{ Pan, Rotate };
pub const GestureInputDirection = enum{ In, Out };
pub const GestureInput = struct { action: GestureInputAction, direction: GestureInputDirection };

pub const Pending = enum { None, Redraw, Drag };