-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::borrow::Cow; | ||
|
||
use arcdps::imgui::{Selectable, StyleColor, Ui}; | ||
use strum::IntoEnumIterator; | ||
|
||
/// Renders a combo box for items from an iterator. | ||
// TODO: make more generic? | ||
pub fn combo<T>( | ||
ui: &Ui, | ||
label: impl AsRef<str>, | ||
all: impl IntoIterator<Item = T>, | ||
current: &mut T, | ||
item_label: impl Fn(&T) -> Cow<str>, | ||
item_color: impl Fn(&T) -> Option<[f32; 4]>, | ||
) -> bool | ||
where | ||
T: PartialEq, | ||
{ | ||
let mut changed = false; | ||
if let Some(_token) = ui.begin_combo(label, item_label(current).as_ref()) { | ||
for entry in all { | ||
let selected = entry == *current; | ||
|
||
// apply color to selectable | ||
let style = | ||
item_color(&entry).map(|color| ui.push_style_color(StyleColor::Text, color)); | ||
if Selectable::new(item_label(&entry).as_ref()) | ||
.selected(selected) | ||
.build(ui) | ||
{ | ||
changed = true; | ||
*current = entry; | ||
} | ||
drop(style); | ||
|
||
// handle focus | ||
if selected { | ||
ui.set_item_default_focus(); | ||
} | ||
} | ||
} | ||
changed | ||
} | ||
|
||
/// Renders a combo box for an enum implementing [`IntoEnumIterator`]. | ||
pub fn enum_combo<T>(ui: &Ui, label: impl AsRef<str>, current: &mut T) -> bool | ||
where | ||
T: PartialEq + AsRef<str> + IntoEnumIterator, | ||
{ | ||
combo( | ||
ui, | ||
label, | ||
T::iter(), | ||
current, | ||
|item| item.as_ref().into(), | ||
|_| None, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters