Made Single and Multi Managers

Feels like something kind of cool to do...
This commit is contained in:
2023-06-16 22:12:11 +01:00
parent b60eb87e34
commit bf41c9d70a
2 changed files with 164 additions and 107 deletions

View File

@@ -20,60 +20,109 @@ pub struct LauncherInfo {
pub name: String,
}
pub struct LauncherManager {
pub launchers: Vec<LauncherInfo>,
//trait Manager {}
pub struct MultiManager<T> {
pub selectable: Vec<T>,
pub current_selected: Vec<usize>,
}
impl <T> MultiManager<T> {
pub fn new() -> Self {
Self {
selectable: Vec::new(),
current_selected: Vec::new(),
}
}
pub fn add(&mut self, item: T) {
self.selectable.push(item);
}
pub fn get(&self, index: usize) -> Option<(&T, bool)> {
if index > self.selectable.len() {
None
} else {
Some((self.selectable.get(index).unwrap(), self.current_selected.contains(&index)))
}
}
pub fn get_current(&self) -> Vec<&T> {
let mut current_selected_items = Vec::new();
for item in &self.current_selected {
assert!(item < &self.selectable.len());
current_selected_items.push(self.selectable.get(*item).unwrap());
}
current_selected_items
}
pub fn add_current(&mut self, index: usize) {
assert!(index < self.selectable.len());
self.current_selected.push(index);
}
pub fn remove_current(&mut self, index: usize) {
assert!(index < self.current_selected.len());
self.current_selected.remove(index);
}
pub fn remove_selectable(&mut self, index: usize) {
assert!(index < self.selectable.len());
self.selectable.remove(index);
}
pub fn iter_selectable_with_pos_and_selected(
&mut self,
) -> impl Iterator<Item = (&mut T, usize, bool)> + '_ {
self.selectable
.iter_mut()
.enumerate()
.map(|(i, l)| (l, i, self.current_selected.contains(&i)))
}
}
pub struct SingleManager<T> {
pub selectable: Vec<T>,
pub current_selected: Option<usize>,
}
impl LauncherManager {
pub fn new() -> LauncherManager {
LauncherManager {
launchers: Vec::new(),
impl <T> SingleManager<T> {
pub fn new() -> Self {
Self {
selectable: Vec::new(),
current_selected: None,
}
}
pub fn add(&mut self, launcher: LauncherInfo) {
self.launchers.push(launcher);
pub fn add(&mut self, item: T) {
self.selectable.push(item);
}
pub fn get(&self, index: usize) -> Option<(&LauncherInfo, bool)> {
if index > self.launchers.len() {
pub fn get(&self, index: usize) -> Option<(&T, bool)> {
if index > self.selectable.len() {
None
} else {
Some((self.launchers.get(index).unwrap(), index == self.current_selected.unwrap_or(std::usize::MAX)))
Some((self.selectable.get(index).unwrap(), index == self.current_selected.unwrap_or(std::usize::MAX)))
}
}
pub fn get_current(&self) -> Option<&LauncherInfo> {
pub fn get_current(&self) -> Option<&T> {
match self.current_selected {
None => None,
Some(pos) => Some(self.launchers.get(pos).unwrap()),
Some(pos) => Some(self.selectable.get(pos).unwrap()),
}
}
pub fn set_current(&mut self, index: usize) {
assert!(index <= self.launchers.len());
assert!(index < self.selectable.len());
self.current_selected = Some(index);
}
pub fn clear_current(&mut self) {
pub fn remove_current(&mut self) {
self.current_selected = None;
}
pub fn remove_launcher(&mut self, index: usize) {
assert!(index <= self.launchers.len());
self.launchers.remove(index);
pub fn remove_selectable(&mut self, index: usize) {
assert!(index < self.selectable.len());
self.selectable.remove(index);
}
pub fn iter_with_pos_and_selected(
pub fn iter_selectable_with_pos_and_selected(
&mut self,
) -> impl Iterator<Item = (&mut LauncherInfo, usize, bool)> + '_ {
self.launchers
) -> impl Iterator<Item = (&mut T, usize, bool)> + '_ {
self.selectable
.iter_mut()
.enumerate()
.map(|(i, l)| (l, i, i == self.current_selected.unwrap_or(std::usize::MAX)))
}
}
pub struct IwadManager {
pub wads: Vec<WadInfo>,
pub current_selected: Option<usize>,
}
#[derive(PartialEq, Clone, Copy)]
pub enum Difficulty {
None,