From fd9dfc613a1586e3e98f9c4efb64657d814a6368 Mon Sep 17 00:00:00 2001 From: Janis Date: Sat, 17 Dec 2022 16:57:06 +0100 Subject: [PATCH] hopefully sound selection rectangle and surface offset logic --- main.zig | 81 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/main.zig b/main.zig index 9a60299..ad2a448 100644 --- a/main.zig +++ b/main.zig @@ -27,6 +27,17 @@ const Point = struct { .y = self.y * -1, }; } + + fn add(self: *Self, other: Self) void { + self.x += other.x; + self.y += other.x; + } + + fn added(self: *const Self, other: Self) Self { + var new = self.*; + new.add(other); + return new; + } }; const Size = struct { @@ -288,8 +299,7 @@ const Box = struct { } fn translate(self: *Self, delta: Point) void { - self.position.x += delta.x; - self.position.y += delta.y; + self.position.add(delta); } fn translated(self: Self, delta: Point) Self { @@ -712,7 +722,7 @@ const Seat = struct { pointer: ?struct { pointer: *wl.Pointer, - button_state: ?wl.Pointer.ButtonState = null, + current_surface_offset: ?Point = null, selection: SelectionState = SelectionState.None(), } = null, @@ -735,6 +745,22 @@ const Seat = struct { return SelectionState.none; } + fn translate(self: SelectionState, delta: Point) void { + switch (self) { + .pre => |*point| { + point.add(delta); + }, + .updating => |*selection| { + selection.start.add(delta); + selection.end.add(delta); + }, + .post => |*box| { + box.translate(delta); + }, + else => {return null;}, + } + } + fn getSelectionBox(self: SelectionState) ?Box { switch (self) { .updating => |selection| { @@ -773,24 +799,35 @@ const Seat = struct { _ = pointer; switch (event) { - .enter => {}, - .leave => {}, + .enter => |enter| { + if (self.state.getOutputForSurface(enter.surface.?)) |output| { + self.pointer.?.current_surface_offset = output.logical_geometry.position; + } + + std.debug.print("surface offset: {?}\n", .{self.pointer.?.current_surface_offset}); + }, + .leave => { + self.pointer.?.current_surface_offset = null; + }, .motion => |motion| { const x = motion.surface_x.toInt(); const y = motion.surface_y.toInt(); + const xy = Point{.x = x, .y = y,}; - const end = Point{.x = x, .y = y,}; + if (self.pointer.?.current_surface_offset) |offset| { + const point = xy.added(offset); - switch (self.pointer.?.selection) { - .pre => { - self.pointer.?.selection = .{.updating = .{.start = end, .end = end}}; - return self.calculateRedraws(); - }, - .updating => |*selection| { - selection.end = end; - return self.calculateRedraws(); - }, - else => {}, + switch (self.pointer.?.selection) { + .pre => { + self.pointer.?.selection = .{.updating = .{.start = point, .end = point}}; + return self.calculateRedraws(); + }, + .updating => |*selection| { + selection.end = point; + return self.calculateRedraws(); + }, + else => {}, + } } }, .button => |button| { @@ -1210,6 +1247,18 @@ const State = struct { self.ally.destroy(link); } + fn getOutputForSurface(self: *Self, surface: *wl.Surface) ?*Output { + var output_iter = self.outputs.iterator(.forward); + while (output_iter.next()) |next| { + const output = next.get(); + if (output.surface == surface) { + return output; + } + } + + return null; + } + fn deinit(self: *Self) void { defer self.ally.destroy(self);