fix some bugs, add checkerboard pattern effect

This commit is contained in:
Tove 2023-12-21 12:38:58 +01:00
parent 0a99c41ce9
commit b23d3e1dae
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
6 changed files with 48 additions and 26 deletions

22
Cargo.lock generated
View file

@ -1748,6 +1748,17 @@ dependencies = [
"ttf-parser",
]
[[package]]
name = "paint"
version = "0.1.0"
dependencies = [
"eframe",
"egui",
"egui_file",
"image",
"micro_ndarray",
]
[[package]]
name = "parking"
version = "2.2.0"
@ -2034,17 +2045,6 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "s_it_paint"
version = "0.1.0"
dependencies = [
"eframe",
"egui",
"egui_file",
"image",
"micro_ndarray",
]
[[package]]
name = "same-file"
version = "1.0.6"

View file

@ -2,16 +2,18 @@ use std::time::SystemTime;
use crate::App;
pub struct Debug {
pub struct Effects {
rand: u64,
pub randomize_size: bool,
pub checkerboard: bool,
}
impl Default for Debug {
impl Default for Effects {
fn default() -> Self {
Self {
rand: SystemTime::UNIX_EPOCH.elapsed().unwrap().as_micros() as u64,
randomize_size: false,
checkerboard: false,
}
}
}
@ -25,12 +27,12 @@ fn rand(rand: &mut u64) -> u8 {
}
impl App {
pub fn update_debug(&mut self) {
if self.debug.randomize_size {
pub fn update_effects(&mut self) {
if self.effects.randomize_size {
self.draw.size = self
.draw
.size
.saturating_add_signed(rand(&mut self.debug.rand) as isize % 7 - 3);
.saturating_add_signed(rand(&mut self.effects.rand) as isize % 7 - 3);
}
}
}

View file

@ -86,12 +86,18 @@ impl App {
if x >= size[0] || y >= size[1] {
return; // just ignore
}
if self.effects.checkerboard && (x + y) % 2 == 0 {
return;
}
self.image[[x, y]] = Color32::from_rgb((px >> 16) as u8, (px >> 8) as u8, px as u8);
self.changes.push(x, y);
}
/// Does not ignore pixels out of bounds, panic!s instead.
pub fn set_px_unchecked(&mut self, x: usize, y: usize, col: Color32) {
if self.effects.checkerboard && (x + y) % 2 == 0 {
return;
}
self.image[[x, y]] = col;
self.changes.push(x, y);
}
@ -188,7 +194,7 @@ impl App {
// convert rotation to radians
let begin_angle = begin_angle / 180.0 * PI /*start at top:*/ + PI;
// to make pulling the usual shapes feel more natural
// to make the usual shapes feel more natural
if n == 3 {
radius_x /= (begin_angle + 2.0 / 3.0 * PI).sin();
}

9
src/help.rs Normal file
View file

@ -0,0 +1,9 @@
use egui::{vec2, Label, RichText, Ui};
use crate::App;
impl App {
pub(crate) fn render_help(&mut self, ui: &mut Ui) {
ui.add_sized(vec2(300.0, 30.0), Label::new(RichText::new("You can select tools and colors in the window menu. \nTo draw the shapes with arbitrary sizes, use the right mouse button and hold shift to draw precise squares / equilateral triangles / circles.")));
}
}

View file

@ -7,7 +7,7 @@ use eframe::CreationContext;
use egui::load::SizedTexture;
use egui::*;
use debug::Debug;
use debug::Effects;
use dialog::DialogAction;
use egui_file::FileDialog;
use micro_ndarray::Array;
@ -19,6 +19,7 @@ mod debug;
mod dialog;
mod draw;
mod fill;
mod help;
mod io;
mod mode;
mod pull;
@ -47,7 +48,7 @@ pub struct App {
pub draw: DrawParams,
pub changes: ChangeRect,
pub cur_edit: Option<String>,
pub debug: Debug,
pub effects: Effects,
pub pull_start: Option<[usize; 2]>,
}
@ -76,7 +77,7 @@ impl App {
draw: DrawParams::new(0, 0, 1, 0x000000),
changes: ChangeRect::new(20),
cur_edit: None,
debug: Debug::default(),
effects: Effects::default(),
pull_start: None,
}
}
@ -139,19 +140,21 @@ impl eframe::App for App {
self.cur_edit = None;
}
});
ui.menu_button("Debugging", |ui| {
ui.checkbox(&mut self.debug.randomize_size, "Randomize sizes");
ui.menu_button("Effects", |ui| {
ui.checkbox(&mut self.effects.randomize_size, "Randomize sizes");
ui.checkbox(&mut self.effects.checkerboard, "Checkerboard");
});
ui.menu_button("Help", |ui| self.render_help(ui));
})
})
});
// updates things set in the debug menu
self.update_debug();
self.update_effects();
CentralPanel::default().frame(f).show(ctx, |ui| {
// shows the image
let size = ui.available_size().round();
let size = ui.available_size().floor();
self.correct_tex_size(
&mut ctx.tex_manager().write(),
[size.x as usize, size.y as usize],
@ -159,7 +162,9 @@ impl eframe::App for App {
self.image_to_texture(&mut ctx.tex_manager().write());
// draw the texture
let r = ui.add(Image::from_texture(SizedTexture::new(self.tex, size)));
let r = ui.add(
Image::from_texture(SizedTexture::new(self.tex, size)).fit_to_original_size(1.0),
);
// handle keyboard and mouse input
ui.input(|inp| {

View file

@ -39,7 +39,7 @@ impl Mode {
RADIUS = (radius_x, radius_y); // need this for compatible match arms
match self {
Paintbrush => App::draw_dot,
Triangle => |this, draw| this.draw_ngon(draw, 3, RADIUS.0, -RADIUS.1, 0.0),
Triangle => |this, draw| this.draw_ngon(draw, 3, RADIUS.0, -RADIUS.1, 180.0),
Square => |this, draw| this.draw_ngon(draw, 4, RADIUS.0, -RADIUS.1, 45.0),
Circle => |this, draw| this.draw_ngon(draw, 0, RADIUS.0, -RADIUS.1, 0.0),
Fill => |this, draw| this.fill(draw),