Added radial bars

master
Mark 2024-01-08 18:12:13 -08:00
parent 45bc3d3b41
commit 744533c05c
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
4 changed files with 59 additions and 63 deletions

View File

@ -27,9 +27,9 @@ fn anchor(
trans += vec2(-dim.x, dim.y) / 2.0; trans += vec2(-dim.x, dim.y) / 2.0;
} else if anchor == 5u { // NE NE } else if anchor == 5u { // NE NE
trans += vec2(window_dim.x, window_dim.y) / 2.0; trans += vec2(window_dim.x, window_dim.y) / 2.0;
trans += vec2(dim.x, -dim.y) / 2.0; trans += vec2(-dim.x, -dim.y) / 2.0;
} else { // center / center as default, since it's the most visible variant. } else { // center / center as default, since it's the most visible variant.
trans += vec2(0.0, 0.0); trans += vec2(0.0, 0.0) / 2.0;
trans += vec2(0.0, 0.0) / 2.0; trans += vec2(0.0, 0.0) / 2.0;
} }

View File

@ -29,6 +29,9 @@ var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1) @group(0) @binding(1)
var sampler_array: binding_array<sampler>; var sampler_array: binding_array<sampler>;
// INCLUDE: anchor.wgsl
@vertex @vertex
fn vertex_main( fn vertex_main(
vertex: VertexInput, vertex: VertexInput,
@ -43,14 +46,12 @@ fn vertex_main(
// Center of this radial bar, in logical pixels, // Center of this radial bar, in logical pixels,
// with (0, 0) at the center of the screen. // with (0, 0) at the center of the screen.
if instance.anchor == u32(0) { out.center = anchor(
out.center = instance.position + ( instance.anchor,
(global_data.window_size / global_data.window_scale.x) instance.position,
- vec2(instance.diameter, instance.diameter) vec2(instance.diameter, instance.diameter)
) / 2.0; ) / 2.0 * (global_data.window_size / global_data.window_scale.x);
} else { // ^ slight correction, since anchor gives us a different result than we need here
out.center = vec2(0.0, 0.0);
}
return out; return out;
} }

View File

@ -3,7 +3,13 @@
use cgmath::{Deg, InnerSpace, Point2, Rad, Vector2}; use cgmath::{Deg, InnerSpace, Point2, Rad, Vector2};
use galactica_world::util; use galactica_world::util;
use crate::{vertexbuffer::types::UiInstance, GPUState, PositionAnchor, RenderState}; use crate::{
vertexbuffer::{
types::{RadialBarInstance, UiInstance},
BufferObject,
},
GPUState, PositionAnchor, RenderState,
};
impl GPUState { impl GPUState {
pub(super) fn hud_add_radar(&mut self, state: &RenderState, instances: &mut Vec<UiInstance>) { pub(super) fn hud_add_radar(&mut self, state: &RenderState, instances: &mut Vec<UiInstance>) {
@ -20,15 +26,6 @@ impl GPUState {
let ship_sprite = state.content.get_sprite_handle("ui::shipblip"); let ship_sprite = state.content.get_sprite_handle("ui::shipblip");
let arrow_sprite = state.content.get_sprite_handle("ui::centerarrow"); let arrow_sprite = state.content.get_sprite_handle("ui::centerarrow");
instances.push(UiInstance {
anchor: PositionAnchor::NeNe.to_int(),
position: [0.0, 0.0],
angle: 0.0,
size: radar_size,
color: [1.0, 1.0, 1.0, 1.0],
sprite_index: state.content.get_sprite_handle("ui::status").get_index(),
});
instances.push(UiInstance { instances.push(UiInstance {
anchor: PositionAnchor::NwNw.to_int(), anchor: PositionAnchor::NwNw.to_int(),
position: [10.0, -10.0], position: [10.0, -10.0],
@ -195,4 +192,42 @@ impl GPUState {
}); });
} }
} }
pub(super) fn hud_add_status(&mut self, state: &RenderState, instances: &mut Vec<UiInstance>) {
instances.push(UiInstance {
anchor: PositionAnchor::NeNe.to_int(),
position: [-10.0, -10.0],
angle: 0.0,
size: 200.0,
color: [1.0, 1.0, 1.0, 1.0],
sprite_index: state.content.get_sprite_handle("ui::status").get_index(),
});
// TODO: counters for each buffer, remove arrays
self.queue.write_buffer(
&self.vertex_buffers.radialbar.instances,
RadialBarInstance::SIZE * 0,
bytemuck::cast_slice(&[RadialBarInstance {
position: [-19.0, -19.0],
anchor: PositionAnchor::NeNe.to_int(),
diameter: 182.0,
stroke: 5.0,
color: [0.3, 0.6, 0.8, 1.0],
angle: -state.current_time / 2.0,
}]),
);
self.queue.write_buffer(
&self.vertex_buffers.radialbar.instances,
RadialBarInstance::SIZE * 1,
bytemuck::cast_slice(&[RadialBarInstance {
position: [-27.0, -27.0],
anchor: PositionAnchor::NeNe.to_int(),
diameter: 166.0,
stroke: 5.0,
color: [0.8, 0.7, 0.5, 1.0],
angle: state.current_time / 5.0,
}]),
);
}
} }

View File

@ -381,6 +381,7 @@ impl GPUState {
let mut ui_instances: Vec<UiInstance> = Vec::new(); let mut ui_instances: Vec<UiInstance> = Vec::new();
self.hud_add_radar(state, &mut ui_instances); self.hud_add_radar(state, &mut ui_instances);
self.hud_add_status(state, &mut ui_instances);
if ui_instances.len() as u64 > galactica_constants::UI_SPRITE_INSTANCE_LIMIT { if ui_instances.len() as u64 > galactica_constants::UI_SPRITE_INSTANCE_LIMIT {
panic!("Ui sprite limit exceeded!") panic!("Ui sprite limit exceeded!")
@ -545,52 +546,11 @@ impl GPUState {
render_pass.set_pipeline(&self.ui_pipeline); render_pass.set_pipeline(&self.ui_pipeline);
render_pass.draw_indexed(0..SPRITE_INDICES.len() as u32, 0, 0..n_ui as _); render_pass.draw_indexed(0..SPRITE_INDICES.len() as u32, 0, 0..n_ui as _);
/* // Radial progress bars
let mut i = 0;
for b in &state.render_elements.radial_bars {
self.queue.write_buffer(
&self.vertex_buffers.radialbar.instances,
RadialBarInstance::SIZE * i,
bytemuck::cast_slice(&[RadialBarInstance {
position: b.pos.position().clone().into(),
anchor: b.pos.to_anchor_int(),
diameter: b.diameter,
stroke: b.stroke,
color: b.color,
angle: b.angle.0,
}]),
);
i += 1;
}
*/
/*
self.queue.write_buffer(
&self.vertex_buffers.radialbar.instances,
0,
bytemuck::cast_slice(&[
RadialBarInstance {
position: [-23.0, -23.0],
diameter: 274.0,
stroke: 10.0,
color: [0.3, 0.6, 0.8, 1.0],
angle: -state.current_time / 2.0,
},
RadialBarInstance {
position: [-35.0, -35.0],
diameter: 250.0,
stroke: 10.0,
color: [0.8, 0.7, 0.5, 1.0],
angle: -state.current_time / 5.0,
},
]),
);*/
// Radial progress bar
// TODO: do we need to do this every time? // TODO: do we need to do this every time?
self.vertex_buffers.radialbar.set_in_pass(&mut render_pass); self.vertex_buffers.radialbar.set_in_pass(&mut render_pass);
render_pass.set_pipeline(&self.radialbar_pipeline); render_pass.set_pipeline(&self.radialbar_pipeline);
//render_pass.draw_indexed(0..SPRITE_INDICES.len() as u32, 0, 0..2); render_pass.draw_indexed(0..SPRITE_INDICES.len() as u32, 0, 0..2);
// begin_render_pass borrows encoder mutably, so we can't call finish() // begin_render_pass borrows encoder mutably, so we can't call finish()
// without dropping this variable. // without dropping this variable.