add reset button
This commit is contained in:
parent
e03a5925f8
commit
10d00e73f7
@ -4,6 +4,7 @@ const gui = @import("raygui");
|
||||
const std = @import("std");
|
||||
const roms = @import("./roms.zig");
|
||||
const GUILayout = @import("./gui-layout.zig");
|
||||
const VerticalLayout = @import("./vertical-layout.zig");
|
||||
const ROM = roms.ROM;
|
||||
|
||||
const ChipContext = @import("chip.zig");
|
||||
@ -85,7 +86,11 @@ pub fn deinit(self: *Self) void {
|
||||
|
||||
pub fn set_rom(self: *Self, index: i32) void {
|
||||
self.selected_rom_index = index;
|
||||
const rom = rom_list[@intCast(index)];
|
||||
self.reset_rom();
|
||||
}
|
||||
|
||||
pub fn reset_rom(self: *Self) void {
|
||||
const rom = rom_list[@intCast(self.selected_rom_index)];
|
||||
self.chip.reset();
|
||||
self.chip.set_memory(0x200, rom.data);
|
||||
}
|
||||
@ -175,6 +180,26 @@ fn GuiListViewMinWidth(allocator: Allocator, height: i32, items: []const []const
|
||||
return min_width;
|
||||
}
|
||||
|
||||
pub fn getWindowBodyBounds(bounds: rl.Rectangle) rl.Rectangle {
|
||||
const status_bar_height = 24; // defined as RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT in raygui.h
|
||||
const margin = 8;
|
||||
|
||||
return rl.Rectangle {
|
||||
.x = bounds.x + margin,
|
||||
.y = bounds.y + margin + status_bar_height,
|
||||
.width = bounds.width - 2*margin,
|
||||
.height = bounds.height - 2*margin - status_bar_height,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn pushGuiWindowBox(layout: *GUILayout, bounds: rl.Rectangle, title: [*:0]const u8) bool {
|
||||
const body = getWindowBodyBounds(bounds);
|
||||
layout.push();
|
||||
layout.translate(body.x, body.y);
|
||||
|
||||
return gui.GuiWindowBox(bounds, title) != 0;
|
||||
}
|
||||
|
||||
pub fn drawGui(self: *Self, allocator: Allocator) !void {
|
||||
if (rl.IsKeyPressed(.KEY_TAB)) {
|
||||
self.is_gui_open = !self.is_gui_open;
|
||||
@ -182,8 +207,8 @@ pub fn drawGui(self: *Self, allocator: Allocator) !void {
|
||||
|
||||
if (!self.is_gui_open) return;
|
||||
|
||||
var layout = GUILayout.init();
|
||||
defer layout.deinit();
|
||||
var window_layout = GUILayout.init();
|
||||
defer window_layout.deinit();
|
||||
|
||||
const screen_width: f32 = @floatFromInt(rl.GetScreenWidth());
|
||||
const screen_height: f32 = @floatFromInt(rl.GetScreenHeight());
|
||||
@ -194,37 +219,48 @@ pub fn drawGui(self: *Self, allocator: Allocator) !void {
|
||||
.a = 128,
|
||||
});
|
||||
|
||||
const window_width: f32 = 300;
|
||||
const window_height: f32 = 100;
|
||||
const window_width: f32 = 400;
|
||||
const window_height: f32 = 200;
|
||||
const window = rl.Rectangle{
|
||||
.x = @trunc((screen_width-window_width)/2),
|
||||
.y = @trunc((screen_height-window_height)/2),
|
||||
.width = window_width,
|
||||
.height = window_height
|
||||
};
|
||||
if (gui.GuiWindowBox(window, "CHIP-8 Settings") == 1) {
|
||||
if (pushGuiWindowBox(&window_layout, window, "CHIP-8 Settings")) {
|
||||
self.is_gui_open = false;
|
||||
}
|
||||
|
||||
layout.push();
|
||||
layout.translate(window.x, window.y);
|
||||
var vertical_layout = VerticalLayout.init(4, getWindowBodyBounds(window));
|
||||
|
||||
const rom_list_names = try allocator.alloc([]const u8, rom_list.len);
|
||||
defer allocator.free(rom_list_names);
|
||||
|
||||
for (0.., rom_list) |i, rom| {
|
||||
rom_list_names[i] = rom.name;
|
||||
if (gui.GuiButton(vertical_layout.row_sized(100, 24), "Reset") == 1) {
|
||||
self.reset_rom();
|
||||
}
|
||||
|
||||
const list_height = 100;
|
||||
const list_width = try GuiListViewMinWidth(allocator, list_height, rom_list_names) + 20;
|
||||
_ = try GuiListView(
|
||||
allocator,
|
||||
layout.rect(10, 24, @floatFromInt(list_width), list_height),
|
||||
rom_list_names,
|
||||
&self.rom_list_scroll_index,
|
||||
&self.selected_rom_index
|
||||
);
|
||||
_ = gui.GuiLabel(vertical_layout.row(16), "Current ROM:");
|
||||
{ // ROM list view
|
||||
const rom_list_names = try allocator.alloc([]const u8, rom_list.len);
|
||||
defer allocator.free(rom_list_names);
|
||||
|
||||
layout.pop();
|
||||
for (0.., rom_list) |i, rom| {
|
||||
rom_list_names[i] = rom.name;
|
||||
}
|
||||
|
||||
const list_height = 100;
|
||||
const list_width = try GuiListViewMinWidth(allocator, list_height, rom_list_names) + 20;
|
||||
var selected_rom = self.selected_rom_index;
|
||||
_ = try GuiListView(
|
||||
allocator,
|
||||
vertical_layout.row_sized(@floatFromInt(list_width), list_height),
|
||||
rom_list_names,
|
||||
&self.rom_list_scroll_index,
|
||||
&selected_rom
|
||||
);
|
||||
|
||||
if (selected_rom != self.selected_rom_index) {
|
||||
self.set_rom(selected_rom);
|
||||
}
|
||||
}
|
||||
|
||||
window_layout.pop();
|
||||
}
|
||||
|
29
src/vertical-layout.zig
Normal file
29
src/vertical-layout.zig
Normal file
@ -0,0 +1,29 @@
|
||||
const Self = @This();
|
||||
const rl = @import("raylib");
|
||||
|
||||
bounds: rl.Rectangle,
|
||||
gap: f32,
|
||||
used: f32,
|
||||
|
||||
pub fn init(gap: f32, bounds: rl.Rectangle) Self {
|
||||
return Self{
|
||||
.bounds = bounds,
|
||||
.gap = gap,
|
||||
.used = 0
|
||||
};
|
||||
}
|
||||
|
||||
pub fn row_sized(self: *Self, width: f32, height: f32) rl.Rectangle {
|
||||
const rect = rl.Rectangle{
|
||||
.x = self.bounds.x,
|
||||
.y = self.bounds.y + self.used,
|
||||
.width = width,
|
||||
.height = height,
|
||||
};
|
||||
self.used += height + self.gap;
|
||||
return rect;
|
||||
}
|
||||
|
||||
pub fn row(self: *Self, height: f32) rl.Rectangle {
|
||||
return self.row_sized(self.bounds.width, height);
|
||||
}
|
Loading…
Reference in New Issue
Block a user