migrate loading screen to guiv2, fix bugs

This commit is contained in:
griffi-gh 2023-11-22 19:01:32 +01:00
parent b601aea288
commit a53f6f9901
7 changed files with 57 additions and 94 deletions

View file

@ -9,5 +9,6 @@ out vec4 vtx_color;
void main() { void main() {
vtx_color = color; vtx_color = color;
gl_Position = vec4(vec2(1., -1.) * (position / resolution), 0., 1.); vec2 pos2d = (vec2(2., -2.) * (position / resolution)) + vec2(-1, 1);
gl_Position = vec4(pos2d, 0., 1.);
} }

View file

@ -33,6 +33,8 @@ pub struct GliumUiRenderer {
pub program: glium::Program, pub program: glium::Program,
pub vertex_buffer: glium::VertexBuffer<Vertex>, pub vertex_buffer: glium::VertexBuffer<Vertex>,
pub index_buffer: glium::IndexBuffer<u32>, pub index_buffer: glium::IndexBuffer<u32>,
pub vertex_count: usize,
pub index_count: usize,
} }
impl GliumUiRenderer { impl GliumUiRenderer {
@ -47,6 +49,8 @@ impl GliumUiRenderer {
program, program,
vertex_buffer, vertex_buffer,
index_buffer, index_buffer,
vertex_count: 0,
index_count: 0,
} }
} }
@ -68,12 +72,14 @@ impl GliumUiRenderer {
} }
fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) { fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) {
log::info!("uploading {} vertices and {} indices", vtx.len(), idx.len()); log::debug!("uploading {} vertices and {} indices", vtx.len(), idx.len());
self.ensure_buffer_size(vtx.len(), idx.len()); self.ensure_buffer_size(vtx.len(), idx.len());
self.vertex_buffer.invalidate(); self.vertex_buffer.invalidate();
self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx); self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx);
self.vertex_count = vtx.len();
self.index_buffer.invalidate(); self.index_buffer.invalidate();
self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx); self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx);
self.index_count = idx.len();
} }
pub fn update(&mut self, plan: &UiDrawPlan) { pub fn update(&mut self, plan: &UiDrawPlan) {
@ -83,14 +89,18 @@ impl GliumUiRenderer {
} }
pub fn draw(&self, frame: &mut glium::Frame, resolution: Vec2) { pub fn draw(&self, frame: &mut glium::Frame, resolution: Vec2) {
if self.index_count == 0 || self.vertex_count == 0 {
return
}
let params = DrawParameters { let params = DrawParameters {
blend: Blend::alpha_blending(), blend: Blend::alpha_blending(),
..Default::default() ..Default::default()
}; };
frame.draw( frame.draw(
&self.vertex_buffer, self.vertex_buffer.slice(0..self.vertex_count).unwrap(),
&self.index_buffer, self.index_buffer.slice(0..self.index_count).unwrap(),
&self.program, &self.program,
&uniform! { &uniform! {
resolution: resolution.to_array(), resolution: resolution.to_array(),

View file

@ -1,5 +1,5 @@
use glam::{Vec2, Vec4}; use glam::{Vec2, Vec4};
use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::{IsMeasurable, Response}, state::StateRepo, UiSize}; use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::Response, state::StateRepo, UiSize};
use super::UiElement; use super::UiElement;
#[derive(Default, Clone, Copy, Debug)] #[derive(Default, Clone, Copy, Debug)]

View file

@ -48,16 +48,20 @@ impl UiElement for ProgressBar {
} }
fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>) { fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>) {
let value = self.value.clamp(0., 1.);
if value < 1. {
draw.push(UiDrawCommand::Rectangle { draw.push(UiDrawCommand::Rectangle {
position: layout.position, position: layout.position,
size: measure.desired_size, size: measure.desired_size,
color: self.color_background color: self.color_background
}); });
}
if value > 0. {
draw.push(UiDrawCommand::Rectangle { draw.push(UiDrawCommand::Rectangle {
position: layout.position, position: layout.position,
size: measure.desired_size * vec2(self.value, 1.0), size: measure.desired_size * vec2(value, 1.0),
color: self.color_foreground color: self.color_foreground
}); });
} }
} }
}

View file

@ -87,9 +87,9 @@ pub enum UiDirection {
Horizontal, Horizontal,
} }
struct LayoutInfo { pub struct LayoutInfo {
///Not availabe during measuring step ///Not availabe during measuring step
position: Vec2, pub position: Vec2,
max_size: Vec2, pub max_size: Vec2,
direction: UiDirection, pub direction: UiDirection,
} }

View file

@ -1,13 +1,5 @@
use glam::Vec2; use glam::Vec2;
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum IsMeasurable {
#[default]
No,
Maybe,
Yes
}
pub struct Response { pub struct Response {
pub desired_size: Vec2 pub desired_size: Vec2
} }

View file

@ -1,60 +1,30 @@
use shipyard::{UniqueView, UniqueViewMut, Workload, IntoWorkload, EntityId, Unique, AllStoragesViewMut, ViewMut, Get, SystemModificator, track}; use kubi_ui::element::progress_bar::ProgressBar;
use shipyard::{UniqueView, UniqueViewMut, Workload, NonSendSync, IntoWorkload};
use winit::keyboard::KeyCode; use winit::keyboard::KeyCode;
use glam::{Mat3, vec2};
use crate::{ use crate::{
world::ChunkStorage, world::ChunkStorage,
state::{GameState, NextState, is_changing_state}, state::{GameState, NextState},
transform::Transform2d, rendering::WindowSize,
legacy_gui::{
LegacyGuiComponent,
progressbar::ProgressbarComponent
},
rendering::{WindowSize, if_resized},
input::RawKbmInputState, input::RawKbmInputState,
guiv2_integration::UiState,
}; };
#[derive(Unique, Clone, Copy)] fn render_progressbar(
struct ProgressbarId(EntityId); mut ui: NonSendSync<UniqueViewMut<UiState>>,
fn spawn_loading_screen(
mut storages: AllStoragesViewMut,
) {
let size = *storages.borrow::<UniqueView<WindowSize>>().unwrap();
let entity = storages.add_entity((
LegacyGuiComponent,
Transform2d(Mat3::from_scale_angle_translation(
vec2(size.0.x as f32, 16.),
0.,
vec2(0., 0.)
)),
ProgressbarComponent {
progress: 0.33
},
));
storages.add_unique(ProgressbarId(entity));
}
fn resize_progress_bar(
size: UniqueView<WindowSize>,
bar: UniqueView<ProgressbarId>,
mut transforms: ViewMut<Transform2d, track::All>
) {
let mut trans = (&mut transforms).get(bar.0).unwrap();
trans.0.x_axis.x = size.0.x as f32;
}
fn update_progress_bar_progress (
world: UniqueView<ChunkStorage>, world: UniqueView<ChunkStorage>,
mut bar: ViewMut<ProgressbarComponent>, size: UniqueView<WindowSize>
eid: UniqueView<ProgressbarId>,
) { ) {
let mut bar = (&mut bar).get(eid.0).unwrap(); let value = {
let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| { let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| {
acc + chunk.desired_state.matches_current(chunk.current_state) as usize acc + chunk.desired_state.matches_current(chunk.current_state) as usize
}); });
let total = world.chunks.len(); let total = world.chunks.len();
let progress = loaded as f32 / total as f32; loaded as f32 / total as f32
bar.progress = progress; };
ui.ui.add(
ProgressBar { value, ..Default::default() },
size.0.as_vec2()
);
} }
fn switch_to_ingame_if_loaded( fn switch_to_ingame_if_loaded(
@ -81,24 +51,10 @@ fn override_loading(
} }
} }
fn despawn_loading_screen_if_switching_state(
mut storages: AllStoragesViewMut,
) {
let state = storages.borrow::<UniqueView<NextState>>().unwrap().0.unwrap();
if state != GameState::LoadingWorld {
let progress_bar = storages.borrow::<UniqueView<ProgressbarId>>().unwrap().0;
storages.delete_entity(progress_bar);
storages.remove_unique::<ProgressbarId>().unwrap();
}
}
pub fn update_loading_screen() -> Workload { pub fn update_loading_screen() -> Workload {
( (
spawn_loading_screen.run_if_missing_unique::<ProgressbarId>(), render_progressbar,
resize_progress_bar.run_if(if_resized),
update_progress_bar_progress,
override_loading, override_loading,
switch_to_ingame_if_loaded, switch_to_ingame_if_loaded,
despawn_loading_screen_if_switching_state.run_if(is_changing_state),
).into_sequential_workload() ).into_sequential_workload()
} }