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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use ilmenite::{Ilmenite, ImtFillQuality, ImtFont, ImtRasterOpts, ImtSampleQuality, ImtWeight};
use interface::{hook::HookManager, odb::OrderedDualBuffer};
use parking_lot::{Mutex, RwLock};
use std::{
	collections::BTreeMap,
	sync::{Arc, Weak},
};
use Basalt;
use crate::interface::bin::Bin;

impl_vertex!(ItfVertInfo, position, coords, color, ty);
#[derive(Clone, Debug)]
#[repr(C)]
pub(crate) struct ItfVertInfo {
	pub position: (f32, f32, f32),
	pub coords: (f32, f32),
	pub color: (f32, f32, f32, f32),
	pub ty: i32,
}

impl Default for ItfVertInfo {
	fn default() -> Self {
		ItfVertInfo {
			position: (0.0, 0.0, 0.0),
			coords: (0.0, 0.0),
			color: (0.0, 0.0, 0.0, 0.0),
			ty: 0,
		}
	}
}

pub(crate) fn scale_verts(win_size: &[f32; 2], scale: f32, verts: &mut Vec<ItfVertInfo>) {
	for vert in verts {
		vert.position.0 *= scale;
		vert.position.1 *= scale;
		vert.position.0 += win_size[0] / -2.0;
		vert.position.0 /= win_size[0] / 2.0;
		vert.position.1 += win_size[1] / -2.0;
		vert.position.1 /= win_size[1] / 2.0;
	}
}

#[allow(dead_code)]
struct BinBufferData {
	atlas_i: usize,
	pos: usize,
	len: usize,
}

pub(crate) enum ItfEvent {
	MSAAChanged,
	ScaleChanged,
}

pub struct Interface {
	basalt: Arc<Basalt>,
	bin_i: Mutex<u64>,
	bin_map: Arc<RwLock<BTreeMap<u64, Weak<Bin>>>>,
	scale: Mutex<f32>,
	msaa: Mutex<u32>,
	pub(crate) ilmenite: Arc<Ilmenite>,
	pub(crate) odb: Arc<OrderedDualBuffer>,
	pub(crate) itf_events: Mutex<Vec<ItfEvent>>,
	pub(crate) hook_manager: Arc<HookManager>,
}

impl Interface {
	pub(crate) fn scale(&self) -> f32 {
		*self.scale.lock()
	}

	pub(crate) fn set_scale(&self, to: f32) {
		*self.scale.lock() = to;
		self.itf_events.lock().push(ItfEvent::ScaleChanged);
	}

	pub fn msaa(&self) -> u32 {
		*self.msaa.lock()
	}

	pub fn set_msaa(&self, amt: u32) -> Result<(), String> {
		let amt = match amt {
			1 => 1,
			2 => 2,
			4 => 4,
			8 => 8,
			a => return Err(format!("Invalid MSAA amount {}X", a)),
		};

		*self.msaa.lock() = amt;
		self.itf_events.lock().push(ItfEvent::MSAAChanged);
		Ok(())
	}

	pub fn increase_msaa(&self) {
		let mut msaa = self.msaa.lock();

		*msaa = match *msaa {
			1 => 2,
			2 => 4,
			4 => 8,
			8 => 8,
			_ => panic!("Invalid MSAA level set!"),
		};

		self.itf_events.lock().push(ItfEvent::MSAAChanged);
	}

	pub fn decrease_msaa(&self) {
		let mut msaa = self.msaa.lock();

		*msaa = match *msaa {
			1 => 1,
			2 => 1,
			4 => 2,
			8 => 4,
			_ => panic!("Invalid MSAA level set!"),
		};

		self.itf_events.lock().push(ItfEvent::MSAAChanged);
	}

	pub(crate) fn new(basalt: Arc<Basalt>) -> Arc<Self> {
		let bin_map: Arc<RwLock<BTreeMap<u64, Weak<Bin>>>> =
			Arc::new(RwLock::new(BTreeMap::new()));
		let ilmenite = Arc::new(Ilmenite::new());

		ilmenite.add_font(
			ImtFont::from_bytes(
				"ABeeZee",
				ImtWeight::Normal,
				ImtRasterOpts {
					fill_quality: ImtFillQuality::Normal,
					sample_quality: ImtSampleQuality::Normal,
					..ImtRasterOpts::default()
				},
				basalt.device(),
				basalt.compute_queue(),
				include_bytes!("ABeeZee-Regular.ttf").to_vec(),
			)
			.unwrap(),
		);

		Arc::new(Interface {
			odb: OrderedDualBuffer::new(basalt.clone(), bin_map.clone()),
			bin_i: Mutex::new(0),
			bin_map,
			scale: Mutex::new(1.0),
			msaa: Mutex::new(4),
			itf_events: Mutex::new(Vec::new()),
			hook_manager: HookManager::new(basalt.clone()),
			ilmenite,
			basalt,
		})
	}

	pub fn get_bin_id_atop(&self, mut x: f32, mut y: f32) -> Option<u64> {
		let scale = self.scale();
		x /= scale;
		y /= scale;

		let bins: Vec<Arc<Bin>> =
			self.bin_map.read().iter().filter_map(|(_, b)| b.upgrade()).collect();
		let mut inside = Vec::new();

		for bin in bins {
			if bin.mouse_inside(x, y) {
				if !bin.style_copy().pass_events.unwrap_or(false) {
					let z = bin.post_update().z_index;
					inside.push((z, bin));
				}
			}
		}

		inside.sort_by_key(|&(z, _)| z);
		inside.pop().map(|v| v.1.id())
	}

	pub fn get_bin_atop(&self, mut x: f32, mut y: f32) -> Option<Arc<Bin>> {
		let scale = self.scale();
		x /= scale;
		y /= scale;

		let bins: Vec<Arc<Bin>> =
			self.bin_map.read().iter().filter_map(|(_, b)| b.upgrade()).collect();
		let mut inside = Vec::new();

		for bin in bins {
			if bin.mouse_inside(x, y) {
				if !bin.style_copy().pass_events.unwrap_or(false) {
					let z = bin.post_update().z_index;
					inside.push((z, bin));
				}
			}
		}

		inside.sort_by_key(|&(z, _)| z);
		inside.pop().map(|v| v.1)
	}

	/// Returns a list of all bins that have a strong reference. Note keeping this
	/// list will keep all bins returned alive and prevent them from being dropped.
	/// This list should be dropped asap to prevent issues with bins being dropped.
	pub fn bins(&self) -> Vec<Arc<Bin>> {
		self.bin_map.read().iter().filter_map(|(_, b)| b.upgrade()).collect()
	}

	pub fn new_bins(&self, amt: usize) -> Vec<Arc<Bin>> {
		let mut out = Vec::with_capacity(amt);
		let mut bin_i = self.bin_i.lock();
		let mut bin_map = self.bin_map.write();

		for _ in 0..amt {
			let id = *bin_i;
			*bin_i += 1;
			let bin = Bin::new(id.clone(), self.basalt.clone());
			bin_map.insert(id, Arc::downgrade(&bin));
			out.push(bin);
		}

		out
	}

	pub fn new_bin(&self) -> Arc<Bin> {
		self.new_bins(1).pop().unwrap()
	}

	pub fn get_bin(&self, id: u64) -> Option<Arc<Bin>> {
		match self.bin_map.read().get(&id) {
			Some(some) => some.upgrade(),
			None => None,
		}
	}

	pub fn mouse_inside(&self, mut mouse_x: f32, mut mouse_y: f32) -> bool {
		let scale = self.scale();
		mouse_x /= scale;
		mouse_y /= scale;

		for bin in self.bins() {
			if bin.mouse_inside(mouse_x, mouse_y) {
				return true;
			}
		}
		false
	}
}