From 9d234eab11681d45d7ec84463a9dcfae402172c6 Mon Sep 17 00:00:00 2001 From: Janis Date: Fri, 16 Dec 2022 15:44:14 +0100 Subject: [PATCH] selection math --- main.zig | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 89 insertions(+), 10 deletions(-) diff --git a/main.zig b/main.zig index 09721b8..c0e68a7 100644 --- a/main.zig +++ b/main.zig @@ -30,6 +30,24 @@ const Size = struct { } }; +const Selection = struct { + const Self = @This(); + + start: Point, + end: Point, + + fn fromPoint(point: Point) Self { + return .{ + .start = point, + .end = point, + }; + } + + fn asBox(self: Self) Box { + return Box.fromCorners(self.start, self.end); + } +}; + const Box = struct { const Self = @This(); @@ -74,8 +92,8 @@ const Box = struct { const pos = Point{.x = left, .y = top,}; const extents = Size{ - .width = right - left, - .height = bot - top, + .width = @intCast(u32, right - left), + .height = @intCast(u32, bot - top), }; return Self { @@ -370,8 +388,29 @@ const Seat = struct { pointer: ?struct { pointer: *wl.Pointer, button_state: ?wl.Pointer.ButtonState = null, + selection: SelectionState = SelectionState.None(), } = null, + + const SelectionTag = enum { + none, + pre, + updating, + post, + }; + + const SelectionState = union(SelectionTag) { + + none: void, + pre: Point, + updating: Selection, + post: Box, + + fn None() SelectionState { + return SelectionState.none; + } + }; + fn init(self: *Self, state: *State.Init) void { self.state = state; @@ -381,8 +420,47 @@ const Seat = struct { fn pointerListener(pointer: *wl.Pointer, event: wl.Pointer.Event, self: *Self) void { _ = pointer; - _ = self; - _ = event; + + std.debug.print("selection: {}\n", .{self.pointer.?.selection}); + + switch (event) { + .enter => {}, + .leave => {}, + .motion => |motion| { + const x = motion.surface_x.toInt(); + const y = motion.surface_y.toInt(); + + const end = Point{.x = x, .y = y,}; + + switch (self.pointer.?.selection) { + .pre => { + self.pointer.?.selection = .{.updating = .{.start = end, .end = end}}; + }, + .updating => |*selection| { + selection.end = end; + }, + else => {}, + } + }, + .button => |button| { + switch (button.state) { + .pressed => { + self.pointer.?.selection = SelectionState{.pre = .{}}; + }, + .released => { + switch (self.pointer.?.selection) { + .updating => |selection| { + const box = selection.asBox(); + self.pointer.?.selection = .{.post = box}; + }, + else => {}, + } + }, + else => {}, + } + }, + else => {}, + } } @@ -440,12 +518,13 @@ const Seat = struct { if (self.keyboard.?.xkb) |xkb_| { const keysym = xkb.xkb_state_key_get_one_sym(xkb_.state, key.key + 8); - const buf_len = 32; - const buf = self.state.ally.alloc(u8, buf_len) catch null; - if (buf) |buffer| { - _ = xkb.xkb_keysym_get_name(keysym, @ptrCast([*c]u8, buffer), buf_len); - std.debug.print("key: {s} {s}\n", .{buffer, if (key.state == .pressed) "pressed" else "released"}); - } + // const buf_len = 32; + // const buf = self.state.ally.alloc(u8, buf_len) catch null; + // if (buf) |buffer| { + // _ = xkb.xkb_keysym_get_name(keysym, @ptrCast([*c]u8, buffer), buf_len); + // std.debug.print("key: {s} {s}\n", .{buffer, if (key.state == .pressed) "pressed" else "released"}); + // } + switch (key.state) { .pressed => { switch (keysym) {