Skip to content

Commit

Permalink
Move GUI code to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed May 5, 2024
1 parent 8bba2b5 commit e59f81e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
17 changes: 4 additions & 13 deletions client/src/graphics/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use ash::vk;
use common::traversal;
use lahar::Staged;
use metrics::histogram;
use yakui::Color;

use super::gui::{self, GuiState};
use super::{fog, voxels, Base, Fog, Frustum, GltfScene, Meshes, Voxels};
use crate::{Asset, Config, Loader, Sim};
use common::proto::{Character, Position};
Expand Down Expand Up @@ -271,9 +271,11 @@ impl Draw {
///
/// Submits commands that wait on `image_acquired` before writing to `framebuffer`'s color
/// attachment.
#[allow(clippy::too_many_arguments)] // Every argument is of a different type, making this less of a problem.
pub unsafe fn draw(
&mut self,
mut sim: Option<&mut Sim>,
gui_state: &GuiState,
framebuffer: vk::Framebuffer,
depth_view: vk::ImageView,
extent: vk::Extent2D,
Expand Down Expand Up @@ -304,18 +306,7 @@ impl Draw {

self.yak.start();
if let Some(sim) = sim.as_ref() {
// Cursor
yakui::align(yakui::Alignment::CENTER, || {
yakui::colored_box(yakui::Color::BLACK.with_alpha(0.9), [5.0, 5.0]);
});

yakui::align(yakui::Alignment::TOP_LEFT, || {
yakui::pad(yakui::widgets::Pad::all(8.0), || {
yakui::colored_box_container(Color::BLACK.with_alpha(0.7), || {
yakui::label(format!("Selected material: {:?}", sim.selected_material()));
});
});
});
gui::gui(gui_state, sim);
}
self.yak.finish();

Expand Down
37 changes: 37 additions & 0 deletions client/src/graphics/gui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use yakui::{
align, colored_box, colored_box_container, label, pad, widgets::Pad, Alignment, Color,
};

use crate::Sim;

pub struct GuiState {
show_gui: bool,
}

impl GuiState {
pub fn new() -> Self {
GuiState { show_gui: true }
}

pub fn toggle_gui(&mut self) {
self.show_gui = !self.show_gui;
}
}

pub fn gui(gui_state: &GuiState, sim: &Sim) {
if !gui_state.show_gui {
return;
}

align(Alignment::CENTER, || {
colored_box(Color::BLACK.with_alpha(0.9), [5.0, 5.0]);
});

align(Alignment::TOP_LEFT, || {
pad(Pad::all(8.0), || {
colored_box_container(Color::BLACK.with_alpha(0.7), || {
label(format!("Selected material: {:?}", sim.selected_material()));
});
});
});
}
1 change: 1 addition & 0 deletions client/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod draw;
mod fog;
mod frustum;
mod gltf_mesh;
mod gui;
mod meshes;
mod png_array;
pub mod voxels;
Expand Down
7 changes: 7 additions & 0 deletions client/src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use winit::{
window::{CursorGrabMode, Window as WinitWindow, WindowBuilder},
};

use super::gui::GuiState;
use super::{Base, Core, Draw, Frustum};
use crate::Net;
use crate::{net, Config, Sim};
Expand Down Expand Up @@ -57,6 +58,7 @@ pub struct Window {
swapchain_needs_update: bool,
draw: Option<Draw>,
sim: Option<Sim>,
gui_state: GuiState,
net: Net,
}

Expand Down Expand Up @@ -93,6 +95,7 @@ impl Window {
swapchain_needs_update: false,
draw: None,
sim: None,
gui_state: GuiState::new(),
net,
}
}
Expand Down Expand Up @@ -261,6 +264,9 @@ impl Window {
sim.toggle_no_clip();
}
}
KeyCode::F1 if state == ElementState::Pressed => {
self.gui_state.toggle_gui();
}
KeyCode::Escape => {
let _ = self.window.set_cursor_grab(CursorGrabMode::None);
self.window.set_cursor_visible(true);
Expand Down Expand Up @@ -352,6 +358,7 @@ impl Window {
// Render the frame
draw.draw(
self.sim.as_mut(),
&self.gui_state,
frame.buffer,
frame.depth_view,
swapchain.state.extent,
Expand Down

0 comments on commit e59f81e

Please sign in to comment.