Skip to content

Commit

Permalink
fix: UI: mouse handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kiedtl committed Aug 20, 2023
1 parent 937ecee commit 638dfef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/colors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub const COPPER_RED: u32 = 0x985744;
pub const BG: u32 = percentageOf(CONCRETE, 10);
pub const BG_L: u32 = percentageOf(CONCRETE, 30);
pub const ABG: u32 = percentageOf(LIGHT_STEEL_BLUE, 20);
pub const ABG_L: u32 = percentageOf(LIGHT_STEEL_BLUE, 40);

// Interpolate linearly between two vals.
fn interpolate(a: u32, b: u32, f: f64) u32 {
Expand Down
51 changes: 29 additions & 22 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ pub var hud_win: struct {
return switch (ev) {
.Hover => |c| switch (self.main.handleMouseEvent(c, .Hover)) {
.Signal => err.wat(),
.Void => true,
.Unhandled => false,
.Unhandled, .Void => true,
.Outside => false,
},
.Click => |c| switch (self.main.handleMouseEvent(c, .Click)) {
.Signal => err.wat(),
.Void => true,
.Unhandled => false,
.Unhandled, .Void => true,
.Outside => false,
},
else => err.wat(),
};
Expand Down Expand Up @@ -124,13 +124,13 @@ pub var map_win: struct {
return switch (ev) {
.Hover => |c| switch (self.map.handleMouseEvent(c, .Hover)) {
.Signal => err.wat(),
.Void => true,
.Unhandled => false,
.Unhandled, .Void => true,
.Outside => false,
},
.Click => |c| switch (self.map.handleMouseEvent(c, .Click)) {
.Signal => err.wat(),
.Void => true,
.Unhandled => false,
.Unhandled, .Void => true,
.Outside => false,
},
else => err.wat(),
};
Expand Down Expand Up @@ -2422,9 +2422,10 @@ pub fn drawPlayerInfoScreen() void {
const sel = if (tabv.value == tab) "$c>" else "$g ";
const bg = if (tab_hover != null and tab_hover.? == tabv.value) colors.BG_L else colors.BG;
my += pinfo_win.left.drawTextAtf(0, my, "{s} {s}$. ", .{ sel, tabv.name }, .{ .bg = bg });
pinfo_win.left.addClickableLine(.Hover, .{ .Signal = tabv.value });
// pinfo_win.left.addClickableLine(.Hover, .{ .RecordElem = &pinfo_win.left });
pinfo_win.left.addClickableLine(.Click, .{ .Signal = tabv.value });
}
pinfo_win.left.highlightMouseArea(colors.BG_L);

pinfo_win.right.clear();
var iy: usize = 0;
Expand Down Expand Up @@ -2534,20 +2535,21 @@ pub fn drawPlayerInfoScreen() void {
break :main;
},
.Hover => |c| switch (pinfo_win.container.handleMouseEvent(c, .Hover)) {
.Signal => |sig| tab_hover = sig,
.Void => err.wat(),
.Unhandled => {},
.Signal => err.wat(),
.Void => break, // redraw, we can get rid of this after animations + timeout are added
.Outside, .Unhandled => {},
},
.Click => |c| switch (pinfo_win.container.handleMouseEvent(c, .Click)) {
.Signal => |sig| {
tab = sig;
tab_hover = null;
},
.Void => err.wat(),
.Outside => break :main,
.Unhandled => {},
},
.Key => |k| switch (k) {
.CtrlC, .CtrlG, .Esc => break,
.CtrlC, .CtrlG, .Esc => break :main,
.ArrowDown => if (tab < meta.fields(Tab).len - 1) {
tab += 1;
},
Expand Down Expand Up @@ -2617,10 +2619,10 @@ pub fn drawZapScreen() void {
break :main;
},
.Key => |k| switch (k) {
.CtrlC, .CtrlG, .Esc => break,
.CtrlC, .CtrlG, .Esc => break :main,
.ArrowUp => selected -|= 1,
.ArrowDown => selected = math.min(ring_count, selected + 1),
.Enter => if (r_error == null) {
.Enter => if (player.getRingByIndex(selected) != null and r_error == null) {
clearScreen();
player.beginUsingRing(selected);
break :main;
Expand Down Expand Up @@ -2967,15 +2969,15 @@ pub fn drawEscapeMenu() void {
.Hover => |c| switch (menu_c.handleMouseEvent(c, .Hover)) {
.Signal => |sig| tab = sig,
.Void => err.wat(),
.Unhandled => {},
.Outside, .Unhandled => {},
},
.Click => |c| switch (menu_c.handleMouseEvent(c, .Click)) {
.Signal => |sig| {
assert(tab == sig);
menu_tab_chosen = true;
},
.Void => err.wat(),
.Unhandled => {},
.Outside, .Unhandled => {},
},
.Key => |k| switch (k) {
.CtrlC, .Esc, .CtrlG => return,
Expand Down Expand Up @@ -3395,6 +3397,7 @@ pub fn drawChoicePrompt(comptime fmt: []const u8, args: anytype, options: []cons
main: while (true) {
var y: usize = 0;
options_c.clearTo(.{ .bg = colors.ABG });
options_c.clearMouseTriggers();
for (options) |option, i| {
const ind = if (chosen == i) ">" else "-";
const color = if (chosen == i) colors.LIGHT_CONCRETE else colors.GREY;
Expand All @@ -3417,9 +3420,12 @@ pub fn drawChoicePrompt(comptime fmt: []const u8, args: anytype, options: []cons
break :main;
},
.Hover => |c| switch (container_c.handleMouseEvent(c, .Hover)) {
.Signal => |sig| chosen = sig,
.Signal => |sig| {
chosen = sig;
break; // redraw, we can get rid of this hack after animations + timeout is added
},
.Void => err.wat(),
.Unhandled => {},
.Outside, .Unhandled => {},
},
.Click => |c| switch (container_c.handleMouseEvent(c, .Click)) {
.Signal => |sig| {
Expand All @@ -3428,6 +3434,7 @@ pub fn drawChoicePrompt(comptime fmt: []const u8, args: anytype, options: []cons
},
.Void => err.wat(),
.Unhandled => {},
.Outside => break :main,
},
.Key => |k| switch (k) {
.CtrlC, .Esc, .CtrlG => {
Expand Down Expand Up @@ -3563,7 +3570,7 @@ pub const Console = struct {
pub const AList = std.ArrayList(@This());
};

pub const MouseEventHandleResult = union(enum) { Unhandled, Signal: usize, Void };
pub const MouseEventHandleResult = union(enum) { Unhandled, Outside, Signal: usize, Void };

pub const Subconsole = struct {
console: *Console,
Expand Down Expand Up @@ -3678,11 +3685,11 @@ pub const Console = struct {

const coord = abscoord.asRect();
if (!dim.intersects(&coord, 0))
return .Unhandled;
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)) {
.Unhandled => {},
.Outside, .Unhandled => {},
.Void => return .Void,
.Signal => |s| return .{ .Signal = s },
}
Expand Down

0 comments on commit 638dfef

Please sign in to comment.