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
use bevy_egui::egui::Color32;

use crate::{
    egui::{self, widgets},
    utils::ui::label_button,
};
use crate::{Context, Inspectable};
use std::{
    ops::{Range, RangeInclusive},
    time::Duration,
};

use super::NumberAttributes;

#[derive(Clone, Debug, Default)]
pub struct StringAttributes {
    pub multiline: bool,
}

impl Inspectable for String {
    type Attributes = StringAttributes;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, _: &Context) -> bool {
        let widget = match options.multiline {
            false => widgets::TextEdit::singleline(self),
            true => widgets::TextEdit::multiline(self),
        };

        // PERF: this is changed if text if highlighted
        ui.add(widget).changed()
    }
}
impl<'a> Inspectable for &'a str {
    type Attributes = ();

    fn ui(&mut self, ui: &mut egui::Ui, _: Self::Attributes, _: &Context) -> bool {
        ui.label(*self);
        false
    }
}

impl Inspectable for bool {
    type Attributes = ();
    fn ui(&mut self, ui: &mut egui::Ui, _: Self::Attributes, _: &Context) -> bool {
        ui.checkbox(self, "").changed()
    }
}

impl<T> Inspectable for RangeInclusive<T>
where
    T: Inspectable + Default,
{
    type Attributes = T::Attributes;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &Context) -> bool {
        let mut changed = false;
        ui.horizontal(|ui| {
            let replacement = T::default()..=T::default();
            let (mut start, mut end) = std::mem::replace(self, replacement).into_inner();

            changed |= start.ui(ui, options.clone(), &context.with_id(0));
            ui.label("..=");
            changed |= end.ui(ui, options, &context.with_id(1));

            *self = start..=end;
        });
        changed
    }
}

impl<T> Inspectable for Range<T>
where
    T: Inspectable + Default,
{
    type Attributes = T::Attributes;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &Context) -> bool {
        let mut changed = false;
        ui.horizontal(|ui| {
            changed |= self.start.ui(ui, options.clone(), &context.with_id(0));
            ui.label("..");
            changed |= self.end.ui(ui, options, &context.with_id(1));
        });
        changed
    }
}

pub struct OptionAttributes<T: Inspectable> {
    pub replacement: Option<fn() -> T>,
    pub deletable: bool,
    pub inner: T::Attributes,
}
impl<T: Inspectable> Clone for OptionAttributes<T> {
    fn clone(&self) -> Self {
        OptionAttributes {
            replacement: self.replacement,
            deletable: self.deletable,
            inner: self.inner.clone(),
        }
    }
}
impl<T: Inspectable> Default for OptionAttributes<T> {
    fn default() -> Self {
        OptionAttributes {
            replacement: None,
            deletable: true,
            inner: T::Attributes::default(),
        }
    }
}

impl<T: Inspectable> Inspectable for Option<T> {
    type Attributes = OptionAttributes<T>;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &Context) -> bool {
        let mut changed = false;
        match self {
            Some(val) => {
                changed |= val.ui(ui, options.inner, context);
                if options.deletable {
                    if label_button(ui, "✖", Color32::RED) {
                        *self = None;
                        changed = true;
                    }
                }
            }
            None => {
                ui.label("None");
                if let Some(replacement) = options.replacement {
                    if label_button(ui, "+", Color32::GREEN) {
                        *self = Some(replacement());
                        changed = true;
                    }
                }
            }
        }
        changed
    }
}

impl Inspectable for Duration {
    type Attributes = ();

    fn ui(&mut self, ui: &mut egui::Ui, _: Self::Attributes, context: &Context) -> bool {
        let mut seconds = self.as_secs_f32();
        let attributes = NumberAttributes {
            min: Some(0.0),
            suffix: "s".to_string(),
            ..Default::default()
        };
        let changed = seconds.ui(ui, attributes, context);
        *self = Duration::from_secs_f32(seconds);
        changed
    }
}