Support choosing colors
This commit is contained in:
+43
-11
@@ -1,13 +1,15 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use egui::Widget;
|
||||
use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, RenderPipeline};
|
||||
use egui::{Color32, Rgba, Widget};
|
||||
use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, Buffer, RenderPipeline};
|
||||
|
||||
use crate::graphics::TextureSink;
|
||||
|
||||
pub struct GameScreen {
|
||||
bind_group: Arc<BindGroup>,
|
||||
pub display_mode: DisplayMode,
|
||||
color_buffer: Arc<Buffer>,
|
||||
display_mode: DisplayMode,
|
||||
colors: Colors,
|
||||
}
|
||||
|
||||
impl GameScreen {
|
||||
@@ -117,12 +119,12 @@ impl GameScreen {
|
||||
|
||||
let (sink, texture_view) = TextureSink::new(device, queue.clone());
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor::default());
|
||||
let colors = Colors {
|
||||
left: [1.0, 0.0, 0.0, 1.0],
|
||||
right: [0.0, 0.7734375, 0.9375, 1.0],
|
||||
};
|
||||
let colors = Colors::new(
|
||||
Color32::from_rgb(0xff, 0x00, 0x00),
|
||||
Color32::from_rgb(0x00, 0xc6, 0xf0),
|
||||
);
|
||||
|
||||
let color_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
let color_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("colors"),
|
||||
contents: bytemuck::bytes_of(&colors),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
@@ -145,7 +147,7 @@ impl GameScreen {
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: color_buf.as_entire_binding(),
|
||||
resource: color_buffer.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -153,11 +155,18 @@ impl GameScreen {
|
||||
(
|
||||
Self {
|
||||
bind_group: Arc::new(bind_group),
|
||||
color_buffer: Arc::new(color_buffer),
|
||||
display_mode: DisplayMode::Anaglyph,
|
||||
colors,
|
||||
},
|
||||
sink,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn update(&mut self, display_mode: DisplayMode, colors: [Color32; 2]) {
|
||||
self.display_mode = display_mode;
|
||||
self.colors = Colors::new(colors[0], colors[1]);
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for &mut GameScreen {
|
||||
@@ -167,7 +176,9 @@ impl Widget for &mut GameScreen {
|
||||
response.rect,
|
||||
GameScreenCallback {
|
||||
bind_group: self.bind_group.clone(),
|
||||
color_buffer: self.color_buffer.clone(),
|
||||
display_mode: self.display_mode,
|
||||
colors: self.colors,
|
||||
},
|
||||
);
|
||||
ui.painter().add(callback);
|
||||
@@ -177,10 +188,23 @@ impl Widget for &mut GameScreen {
|
||||
|
||||
struct GameScreenCallback {
|
||||
bind_group: Arc<BindGroup>,
|
||||
color_buffer: Arc<Buffer>,
|
||||
display_mode: DisplayMode,
|
||||
colors: Colors,
|
||||
}
|
||||
|
||||
impl egui_wgpu::CallbackTrait for GameScreenCallback {
|
||||
fn prepare(
|
||||
&self,
|
||||
_device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
_screen_descriptor: &egui_wgpu::ScreenDescriptor,
|
||||
_egui_encoder: &mut wgpu::CommandEncoder,
|
||||
_callback_resources: &mut egui_wgpu::CallbackResources,
|
||||
) -> Vec<wgpu::CommandBuffer> {
|
||||
queue.write_buffer(&self.color_buffer, 0, bytemuck::bytes_of(&self.colors));
|
||||
vec![]
|
||||
}
|
||||
fn paint(
|
||||
&self,
|
||||
info: egui::PaintCallbackInfo,
|
||||
@@ -217,8 +241,16 @@ struct SharedGameScreenResources {
|
||||
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
#[repr(C)]
|
||||
struct Colors {
|
||||
left: [f32; 4],
|
||||
right: [f32; 4],
|
||||
left: Rgba,
|
||||
right: Rgba,
|
||||
}
|
||||
impl Colors {
|
||||
fn new(left: Color32, right: Color32) -> Self {
|
||||
Self {
|
||||
left: left.into(),
|
||||
right: right.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
|
||||
Reference in New Issue
Block a user