Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::canvas::{CanvasTransform, Uniforms};
use crate::drawing::{DrawingElement, Tool};
use crate::drawing::{DrawingElement, Style, Tool};
use crate::state::{
Canvas, GeometryBuffers, GpuContext, InputState, TextInput, UiBuffers,
UiScreenBuffers, UiScreenUniforms, UserInputState::Idle,
Canvas, GeometryBuffers, GpuContext, InputState, TextInput, UiBuffers, UiScreenBuffers,
UiScreenUniforms, UserInputState::Idle,
};
use crate::text_renderer::TextRenderer;
use crate::ui::UiRenderer;
Expand Down Expand Up @@ -34,6 +34,7 @@ pub struct State {
pub elements: Vec<DrawingElement>,
pub current_tool: Tool,
pub current_color: [f32; 4],
pub current_style: Style,
pub stroke_width: f32,

pub ui_renderer: UiRenderer,
Expand All @@ -44,7 +45,7 @@ pub struct State {
impl State {
pub async fn new(window: Arc<Window>) -> State {
let mut size = window.inner_size();

#[cfg(target_arch = "wasm32")]
{
if size.width == 0 || size.height == 0 {
Expand Down Expand Up @@ -195,9 +196,7 @@ impl State {

let ui_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("UI Shader"),
source: wgpu::ShaderSource::Wgsl(
include_str!("../data/shaders/ui_shader.wgsl").into(),
),
source: wgpu::ShaderSource::Wgsl(include_str!("../data/shaders/ui_shader.wgsl").into()),
});

let ui_uniform_bind_group_layout =
Expand Down Expand Up @@ -311,18 +310,26 @@ impl State {
};

let ui_renderer = UiRenderer::new();
let text_renderer = TextRenderer::new(&gpu.device, &gpu.queue, surface_format, &uniform_bind_group_layout, &ui_uniform_bind_group_layout);
let text_renderer = TextRenderer::new(
&gpu.device,
&gpu.queue,
surface_format,
&uniform_bind_group_layout,
&ui_uniform_bind_group_layout,
);

let ui_screen_uniforms = UiScreenUniforms {
screen_size: [size.width as f32, size.height as f32],
_padding: [0.0, 0.0],
};

let ui_screen_uniform_buffer = gpu.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("UI Screen Uniform Buffer"),
contents: bytemuck::cast_slice(&[ui_screen_uniforms]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let ui_screen_uniform_buffer =
gpu.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("UI Screen Uniform Buffer"),
contents: bytemuck::cast_slice(&[ui_screen_uniforms]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});

let ui_screen_bind_group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &ui_uniform_bind_group_layout,
Expand All @@ -338,6 +345,8 @@ impl State {
bind_group: ui_screen_bind_group,
};

let current_style = Style::Solid;

Self {
window,
size,
Expand All @@ -349,15 +358,16 @@ impl State {
typing,
elements: Vec::new(),
current_tool: Tool::Pen,
current_color: [0.0, 0.0, 0.0, 1.0],
current_color: [0.0, 0.0, 0.0, 1.0],
stroke_width: 2.0,
ui_renderer,
text_renderer,
ui_screen,
current_style,
}
}

pub fn window(&self) -> &Arc<Window> {
&self.window
}
}
}
11 changes: 11 additions & 0 deletions src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub enum Tool {
Select,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Style {
Dotted,
Solid,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BoxState {
Idle,
Expand All @@ -31,6 +37,7 @@ pub enum DrawingElement {
color: [f32; 4],
width: f32,
rough_style: Option<crate::rough::RoughOptions>,
style: Style,
},
Rectangle {
position: [f32; 2],
Expand All @@ -39,6 +46,7 @@ pub enum DrawingElement {
fill: bool,
stroke_width: f32,
rough_style: Option<crate::rough::RoughOptions>,
style: Style,
},
Circle {
center: [f32; 2],
Expand All @@ -47,6 +55,7 @@ pub enum DrawingElement {
fill: bool,
stroke_width: f32,
rough_style: Option<crate::rough::RoughOptions>,
style: Style,
},
Diamond {
position: [f32; 2],
Expand All @@ -55,13 +64,15 @@ pub enum DrawingElement {
fill: bool,
stroke_width: f32,
rough_style: Option<crate::rough::RoughOptions>,
style: Style,
},
Arrow {
start: [f32; 2],
end: [f32; 2],
color: [f32; 4],
width: f32,
rough_style: Option<crate::rough::RoughOptions>,
style: Style,
},
Text {
position: [f32; 2],
Expand Down
23 changes: 23 additions & 0 deletions src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ impl State {
return true;
}

if let Some(style) =
self.ui_renderer.handle_style_click(self.input.mouse_pos)
{
self.current_style = style;
return true;
}

if self.ui_renderer.is_mouse_over_ui(
self.input.mouse_pos,
(self.size.width as f32, self.size.height as f32),
Expand Down Expand Up @@ -463,6 +470,7 @@ impl State {

let mut rough_options = crate::rough::RoughOptions::default();
rough_options.stroke_width = self.stroke_width;
rough_options.dotted = self.current_style == crate::drawing::Style::Dotted;

let mut rng = rand::rng();

Expand All @@ -480,6 +488,7 @@ impl State {
fill: false,
stroke_width: self.stroke_width,
rough_style: Some(rough_options),
style: self.current_style,
})
} else {
None
Expand All @@ -492,6 +501,7 @@ impl State {

let mut rough_options = crate::rough::RoughOptions::default();
rough_options.stroke_width = self.stroke_width;
rough_options.dotted = self.current_style == crate::drawing::Style::Dotted;

let mut rng = rand::rng();

Expand All @@ -510,6 +520,7 @@ impl State {
fill: false,
stroke_width: self.stroke_width,
rough_style: Some(rough_options),
style: self.current_style,
})
} else {
None
Expand All @@ -521,6 +532,7 @@ impl State {

let mut rough_options = crate::rough::RoughOptions::default();
rough_options.stroke_width = self.stroke_width;
rough_options.dotted = self.current_style == crate::drawing::Style::Dotted;

let mut rng = rand::rng();

Expand All @@ -538,6 +550,7 @@ impl State {
color: self.current_color,
width: self.stroke_width,
rough_style: Some(rough_options),
style: self.current_style,
})
} else {
None
Expand All @@ -549,6 +562,7 @@ impl State {

let mut rough_options = crate::rough::RoughOptions::default();
rough_options.stroke_width = self.stroke_width;
rough_options.dotted = self.current_style == crate::drawing::Style::Dotted;

let mut rng = rand::rng();

Expand All @@ -566,6 +580,7 @@ impl State {
color: self.current_color,
width: self.stroke_width,
rough_style: Some(rough_options),
style: self.current_style,
})
} else {
None
Expand All @@ -579,6 +594,7 @@ impl State {

let mut rough_options = crate::rough::RoughOptions::default();
rough_options.stroke_width = self.stroke_width;
rough_options.dotted = self.current_style == crate::drawing::Style::Dotted;

let mut rng = rand::rng();

Expand All @@ -596,6 +612,7 @@ impl State {
fill: false,
stroke_width: self.stroke_width,
rough_style: Some(rough_options),
style: self.current_style,
})
} else {
None
Expand Down Expand Up @@ -638,6 +655,7 @@ impl State {
fill: false,
stroke_width: self.stroke_width,
rough_style: None,
style: self.current_style,
});
}
}
Expand All @@ -658,6 +676,7 @@ impl State {
fill: false,
stroke_width: self.stroke_width,
rough_style: None,
style: self.current_style,
});
}
}
Expand All @@ -676,6 +695,7 @@ impl State {
],
width: self.stroke_width,
rough_style: None,
style: self.current_style,
});
}
}
Expand All @@ -694,6 +714,7 @@ impl State {
],
width: self.stroke_width,
rough_style: None,
style: self.current_style,
});
}
}
Expand All @@ -715,6 +736,7 @@ impl State {
fill: false,
stroke_width: self.stroke_width,
rough_style: None,
style: self.current_style,
});
}
}
Expand Down Expand Up @@ -769,6 +791,7 @@ impl State {
fill,
stroke_width,
rough_style,
style,
} => {
let half_width = size[0] / 2.0;
let half_height = size[1] / 2.0;
Expand Down
Loading