1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Utility constants, structs and functions global for both `views` and the main function.

use std::path::PathBuf;
use std::time::Instant;
use wgpu_glyph::{Section, Scale};
use std::sync::{mpsc, Arc, Mutex};
use zerocopy::{AsBytes, FromBytes};

lazy_static! {
	pub static ref ABSOLUTE_PATH: PathBuf = std::env::current_dir().unwrap();
	pub static ref WINDOW_SIZE_SIZE: wgpu::BufferAddress = std::mem::size_of::<WindowSize>() as wgpu::BufferAddress;
	pub static ref ZOOM_SIZE: wgpu::BufferAddress = std::mem::size_of::<Zoom>() as wgpu::BufferAddress;
	pub static ref POSITION_SIZE: wgpu::BufferAddress = std::mem::size_of::<Position>() as wgpu::BufferAddress;
	pub static ref ITERATIONS_SIZE: wgpu::BufferAddress = std::mem::size_of::<Iterations>() as wgpu::BufferAddress;
	pub static ref VERTEX_SIZE: wgpu::BufferAddress = std::mem::size_of::<Vertex>() as wgpu::BufferAddress;
	pub static ref JULIA_SIZE: wgpu::BufferAddress = std::mem::size_of::<Julia>() as wgpu::BufferAddress;
}

pub type AtomicDevice = Arc<Mutex<wgpu::Device>>;

#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct WindowSize {
	pub size: [f32; 2]
}

#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct Zoom {
	pub zoom: f32
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Julia {
	pub is_julia: bool
}

#[repr(C)]
#[derive(Clone, Copy, Debug, AsBytes, FromBytes)]
pub struct Iterations {
	pub iterations: f32
}

impl Default for Iterations {
	fn default() -> Self {
		Self {
			iterations: 100.0
		}
	}
}

#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct Vertex {
	pub pos: [f32; 2],
}

impl Default for Zoom {
	fn default() -> Self {
		Self {
			zoom: 0.003
		}
	}
}

#[repr(C)]
#[derive(Debug, Clone, Copy, Default, AsBytes, FromBytes)]
pub struct Position {
	pub pos: [f32; 2]
}

trait DigitsCountable {
	fn count_digits(self) -> usize;
}

impl DigitsCountable for usize {
	fn count_digits(self) -> usize {
		let mut number = self;
		let mut count: usize = 0;

		while number > 0 {
			number /= 10;
			count += 1;
		}

		count + 1
	}
}

pub struct Changed(pub bool);

impl Changed {
	pub fn set(&mut self, state: bool, desc: &str) {
		log::info!("Changed to {:?} from {:?}", state, desc);
		self.0 = state;
	}
}

pub fn fps_command(
	device: &AtomicDevice,
	glyph_brush: &mut wgpu_glyph::GlyphBrush<()>,
	size: &winit::dpi::PhysicalSize,
	frame: &wgpu::SwapChainOutput,
	past: &mut Instant
) -> wgpu::CommandBuffer {
	let mut encoder =
		device.lock().unwrap().create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
	let now = Instant::now();
	let time = now - *past;
	*past = now;
	let fps = (1.0 / time.as_secs_f32()).round() as usize;

	let number_section = Section {
		text: &format!("{}", fps),
		screen_position: (size.width as f32 / 100.0, size.height as f32 / 100.0),
		scale: Scale::uniform(32.0),
		color: [1.0f32, 1.0f32, 1.0f32, 1.0f32],
		..Section::default() // color, position, etc
	};

	let mut number_section_outline = number_section;
	number_section_outline.color = [0.0f32, 0.0f32, 0.0f32, 1.0f32];
	number_section_outline.scale = Scale::uniform(38.0);
	number_section_outline.screen_position.0 -= 3.0f32;
	number_section_outline.screen_position.1 -= 3.0f32;

	let fps_section = Section {
		text: "fps",
		screen_position: (size.width as f32 / 100.0 + 16.0 * fps.count_digits() as f32, size.height as f32 / 100.0),
		scale: Scale::uniform(32.0),
		color: [1.0f32, 1.0f32, 1.0f32, 1.0f32],
		..Section::default() // color, position, etc
	};

	let mut fps_section_outline = fps_section;
	fps_section_outline.color = [0.0f32, 0.0f32, 0.0f32, 1.0f32];
	fps_section_outline.scale = Scale::uniform(38.0);
	fps_section_outline.screen_position.0 -= 3.0f32;
	fps_section_outline.screen_position.1 -= 3.0f32;

	glyph_brush.queue(fps_section_outline);
	glyph_brush.queue(fps_section);
	glyph_brush.queue(number_section_outline);
	glyph_brush.queue(number_section);

	glyph_brush.draw_queued(
		&mut device.lock().unwrap(),
		&mut encoder,
		&frame.view,
		size.width.round() as u32,
		size.height.round() as u32,
	).expect("error drawing text");

	encoder.finish()
}
use notify::{Watcher, RecursiveMode, RecommendedWatcher};
use std::time::Duration;

pub fn create_watcher(path: &PathBuf) -> (RecommendedWatcher, mpsc::Receiver<notify::DebouncedEvent>) {
	let (tx, rx) = mpsc::channel();
	let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(500)).unwrap();

	watcher.watch(path.clone(), RecursiveMode::NonRecursive).unwrap();
	log::info!("Starting watcher on {:?}", path);

	(watcher, rx)
}

#[derive(PartialEq)]
pub enum CurrentView {
	Single,
	Double
}