selection math

This commit is contained in:
Janis 2022-12-16 15:44:14 +01:00
parent 2be2dc6ac7
commit 9d234eab11

View file

@ -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) {