Skip to content

Commit

Permalink
feat: UI: (WIP) add mouse support to examine
Browse files Browse the repository at this point in the history
  • Loading branch information
kiedtl committed Nov 13, 2023
1 parent a074525 commit db6ad59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
40 changes: 16 additions & 24 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1748,11 +1748,12 @@ fn _mobs_can_see(moblist: []const *Mob, coord: Coord) bool {
return false;
}

pub fn screenCoordToNormal(coord: Coord, refpoint: Coord) ?Coord {
const x = @intCast(isize, refpoint.x) +
pub fn screenCoordToNormal(coord: Coord, absrefpoint: Coord) ?Coord {
const refpoint = coordToScreenFromRefpoint(absrefpoint, absrefpoint).?;
const x = @intCast(isize, absrefpoint.x) +
@divFloor(@intCast(isize, coord.x) - @intCast(isize, refpoint.x), 2);
const y = @intCast(isize, refpoint.y) +
(@intCast(isize, coord.y) - @intCast(isize, refpoint.y));
const y = @intCast(isize, absrefpoint.y) +
(@intCast(isize, coord.y) - @intCast(isize, refpoint.y) - 1);
if (x < 0 or y < 0 or x >= WIDTH or y >= HEIGHT)
return null;
return Coord.new(@intCast(usize, x), @intCast(usize, y));
Expand Down Expand Up @@ -1838,7 +1839,9 @@ pub fn drawMap(console: *Console, moblist: []const *Mob, refpoint: Coord) void {
const map_startx = refpointx - @intCast(isize, MAP_WIDTH_R);
const map_endx = refpointx + @intCast(isize, MAP_WIDTH_R);

console.clearMouseTriggers();
if (console == &map_win.map) // yuck
console.clearMouseTriggers();

console.clearTo(.{ .fl = .{ .wide = true } });

var y = map_starty;
Expand Down Expand Up @@ -3165,32 +3168,19 @@ pub fn drawExamineScreen(starting_focus: ?ExamineTileFocus, start_coord: ?Coord)

{
const dcoord = coordToScreenFromRefpoint(coord, coord).?;
mp3_win.setCell(dcoord.x - 2, dcoord.y - 1, .{ .ch = '╭', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 0, dcoord.y - 1, .{ .ch = '─', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 2, dcoord.y - 1, .{ .ch = '╮', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x - 2, dcoord.y + 0, .{ .ch = '│', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 2, dcoord.y + 0, .{ .ch = '│', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x - 2, dcoord.y + 1, .{ .ch = '╰', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 0, dcoord.y + 1, .{ .ch = '─', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 2, dcoord.y + 1, .{ .ch = '╯', .fg = colors.CONCRETE, .bg = colors.BG, .fl = .{ .wide = true } });
mp3_win.drawOutlineAround(dcoord, .{ .fg = colors.CONCRETE, .fl = .{ .wide = true } });
}

if (highlight) |_highlight| {
const dcoord = coordToScreenFromRefpoint(_highlight, coord).?;
mp3_win.setCell(dcoord.x - 2, dcoord.y - 1, .{ .ch = '╭', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 0, dcoord.y - 1, .{ .ch = '─', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 2, dcoord.y - 1, .{ .ch = '╮', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x - 2, dcoord.y + 0, .{ .ch = '│', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 2, dcoord.y + 0, .{ .ch = '│', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x - 2, dcoord.y + 1, .{ .ch = '╰', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 0, dcoord.y + 1, .{ .ch = '─', .fg = 0xcacbca, .fl = .{ .wide = true } });
mp3_win.setCell(dcoord.x + 2, dcoord.y + 1, .{ .ch = '╯', .fg = 0xcacbca, .fl = .{ .wide = true } });
if (coordToScreenFromRefpoint(_highlight, coord)) |dcoord| {
mp3_win.drawOutlineAround(dcoord, .{ .fg = 0xaaaaaa, .fl = .{ .wide = true } });
}
}

container.renderFully(0, 0);
display.present();

var evgen = Generator(display.getEvents).init(null);
var evgen = Generator(display.getEvents).init(FRAMERATE);
while (evgen.next()) |ev| switch (ev) {
.Quit => {
state.state = .Quit;
Expand All @@ -3207,7 +3197,9 @@ pub fn drawExamineScreen(starting_focus: ?ExamineTileFocus, start_coord: ?Coord)
}
},
.Click => |c| {
switch (container.handleMouseEvent(c, .Click)) {
const r = container.handleMouseEvent(c, .Click);
std.log.info("r: {}", .{r});
switch (r) {
.Coord => |screenc| {
if (screenCoordToNormal(screenc, coord)) |mapc|
coord = mapc;
Expand Down
35 changes: 29 additions & 6 deletions src/ui/Console.zig
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,13 @@ fn _handleMouseEvent(self: *Self, abscoord: Coord, kind: MouseTrigger.Kind, dim:
return .Outside;
for (self.subconsoles.items) |*subconsole| {
const r = Rect.new(Coord.new(dim.start.x + subconsole.x, dim.start.y + subconsole.y), subconsole.console.width, subconsole.console.height);
switch (_handleMouseEvent(subconsole.console, abscoord, kind, r)) {
.Outside, .Unhandled => {},
.Void => return .Void,
.Signal => |s| return .{ .Signal = s },
.Coord => |c| return .{ .Coord = c },
}
const res = _handleMouseEvent(subconsole.console, abscoord, kind, r);
// std.log.debug("HANDLED abscoord={}, r={}, res={}, subconsole: {},{}", .{ abscoord, r, res, subconsole.console.width, subconsole.console.height });
if (res != .Outside and res != .Unhandled)
return res;
}
for (self.mouse_triggers.items) |t| {
// std.log.debug("HANDLING abscoord={}, kind={}, dim={}, coord={}, coordrel={}, tkind={}, trect={}", .{ abscoord, kind, dim, coord, coord.relTo(dim), t.kind, t.rect });
if (t.kind == kind and t.rect.intersects(&coord.relTo(dim), 1)) {
switch (t.action) {
.Signal => |s| return .{ .Signal = s },
Expand Down Expand Up @@ -376,6 +375,30 @@ pub fn stepRevealAnimation(self: *Self) void {
_ = self._reveal_animation.?.next();
}

pub fn drawOutlineAround(self: *const Self, coord: Coord, style: display.Cell) void {
const f: isize = if (style.fl.wide) 2 else 1;
const c = [_]struct { x: isize, y: isize, ch: u21 }{
// zig fmt: off
.{ .x = -f, .y = -1, .ch = '╭' },
.{ .x = 0, .y = -1, .ch = '─' },
.{ .x = f, .y = -1, .ch = '╮' },
.{ .x = -f, .y = 0, .ch = '│' },
.{ .x = f, .y = 0, .ch = '│' },
.{ .x = -f, .y = 1, .ch = '╰' },
.{ .x = 0, .y = 1, .ch = '─' },
.{ .x = f, .y = 1, .ch = '╯' },
// zig fmt: on
};
for (c) |i| {
var cell = style;
cell.ch = i.ch;
const dx = @intCast(isize, coord.x) + i.x;
const dy = @intCast(isize, coord.y) + i.y;
if (dx < 0 or dy < 0) continue;
self.setCell(@intCast(usize, dx), @intCast(usize, dy), cell);
}
}

// TODO: draw multiple layers as needed
pub fn drawXP(self: *const Self, map: *const RexMap, startx: usize, starty: usize, pmrect: ?Rect, wide: bool) void {
const mrect = pmrect orelse Rect.new(Coord.new(0, 0), map.width, map.height);
Expand Down

0 comments on commit db6ad59

Please sign in to comment.