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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#![allow(clippy::unnecessary_operation)]

pub mod cursor;
pub mod overlay;

// use cursor::Cursor;

use crate::phasmo;
use druid::{
    im::Vector,
    widget::{Button, Flex, Label},
    Data, Lens, Widget, WidgetExt,
};

#[derive(Debug, Clone, PartialEq, Data, Lens)]
pub struct AppData {
    evidence_status: Vector<EvidenceStatus>,
    selected: Wghost,
}

struct Delegate {}

const SELECT_GHOST: druid::Selector<phasmo::Ghost> = druid::Selector::new("select_ghost");

impl druid::AppDelegate<AppData> for Delegate {
    fn command(
        &mut self,
        _ctx: &mut druid::DelegateCtx,
        _target: druid::Target,
        cmd: &druid::Command,
        data: &mut AppData,
        _env: &druid::Env,
    ) -> bool {
        if let Some(ghost) = cmd.get(SELECT_GHOST) {
            data.selected = Wghost(*ghost);
        }
        true
    }
}

#[derive(Debug, Clone, PartialEq, Data)]
pub struct Wghost(#[data(same_fn = "PartialEq::eq")] pub phasmo::Ghost);

#[derive(Debug, Clone, PartialEq, Data)]
#[repr(C)]
pub enum EvidenceStatus {
    Unknown,
    Found,
    Forbidden,
}

impl Default for AppData {
    fn default() -> Self {
        let evidence_status: Vector<_> = phasmo::EVIDENCES
            .iter()
            .map(|_e| EvidenceStatus::default())
            .collect();
        #[allow(clippy::deref_addrof)]
        let selected = Wghost(phasmo::GHOSTS[0]);
        Self {
            evidence_status,
            selected,
        }
    }
}

impl Default for EvidenceStatus {
    fn default() -> Self {
        Self::Unknown
    }
}

impl EvidenceStatus {
    pub fn next_status(&mut self) {
        use EvidenceStatus::*;
        *self = match self {
            Unknown => Found,
            Found => Forbidden,
            Forbidden => Unknown,
        };
    }
}

impl AppData {
    fn possible_ghosts(&self) -> Vec<phasmo::Ghost> {
        use phasmo::Ghost;
        let required: Vec<_> = phasmo::EVIDENCES
            .iter()
            .cloned()
            .zip(self.evidence_status.iter())
            .filter_map(|(evd, status)| {
                if let EvidenceStatus::Found = status {
                    Some(evd)
                } else {
                    None
                }
            })
            .collect();
        let forbid: Vec<_> = phasmo::EVIDENCES
            .iter()
            .cloned()
            .zip(self.evidence_status.iter())
            .filter_map(|(evd, status)| {
                if let EvidenceStatus::Forbidden = status {
                    Some(evd)
                } else {
                    None
                }
            })
            .collect();

        let ghosts =
            Ghost::filter_by_required_evidences(phasmo::GHOSTS.iter().cloned(), required.as_ref());
        Ghost::filter_by_forbid_evidences(ghosts.into_iter(), forbid.as_ref())
    }
}

pub fn ui_builder() -> impl Widget<AppData> {
    let mut root = Flex::column();

    // let mut summary_ghosts_flex = Flex::row();

    let mut summary_features_flex = Flex::row();

    // summary

    {
        let mut ghost_icons = Flex::row();

        for g in phasmo::GHOSTS.iter().cloned() {
            let w = Button::new(move |evd_status: &Vector<EvidenceStatus>, _env: &_| {
                let (required, forbid): (Vec<_>, Vec<_>) = phasmo::EVIDENCES
                    .iter()
                    .cloned()
                    .zip(evd_status.iter())
                    .filter(|(_evd, status)| !matches!(status, EvidenceStatus::Unknown))
                    .partition(|(_evd, status)| matches!(status, EvidenceStatus::Found));
                let required: Vec<_> = required.into_iter().map(|(evd, _status)| evd).collect();
                let forbid: Vec<_> = forbid.into_iter().map(|(evd, _status)| evd).collect();
                if g.is_valid(required.as_ref(), forbid.as_ref()) {
                    g.to_string()
                } else {
                    String::new()
                }
            })
            .on_click(
                move |ctx: &mut druid::EventCtx, _e: &mut Vector<EvidenceStatus>, _env: &_| {
                    ctx.submit_command(druid::Command::new(SELECT_GHOST, g), None);
                },
            )
            .expand_width()
            .lens(AppData::evidence_status);
            ghost_icons.add_flex_child(w, 1.0);
        }
        root.add_flex_child(ghost_icons, 1.0);

        let feature_icons = Label::new(|data: &AppData, _env: &_| {
            use phasmo::Ghost;
            let ghosts = data.possible_ghosts();

            let mut caution: Vec<_> = Ghost::filter_by_caution_features(ghosts.iter().cloned())
                .into_iter()
                .collect();
            caution.sort_unstable();

            let mut useful: Vec<_> = Ghost::filter_by_useful_features(ghosts.iter().cloned())
                .into_iter()
                .collect();
            useful.sort_unstable();

            let caution_str: String = caution.iter().map(|c| c.to_string() + " ").collect();
            let useful_str: String = useful.iter().map(|u| u.to_string() + " ").collect();

            caution_str + "/ " + &useful_str
        });

        summary_features_flex.add_flex_child(feature_icons, 1.0);
        root.add_flex_child(summary_features_flex.padding((0., 10., 0., 10.)), 1.0);
    };

    // evidences

    {
        // let evidence_status = || {

        //     Label::new(|evidence: &EvidenceState, _env: &_| {

        //         match evidence.status {

        //             // ☐

        //             // ❓ ➖ ❔

        //             EvidenceStatus::Unknown => "➖",

        //             // ☑

        //             // ✔️

        //             EvidenceStatus::Found => "✔️",

        //             // ☒

        //             // ❌

        //             EvidenceStatus::Forbidden => "❌",

        //         }

        //         .to_string()

        //     })

        // };


        let evidence_status = || {
            Label::new(|evidence: &EvidenceStatus, _env: &_| {
                match evidence {
                    // ☐

                    // ❓ ➖ ❔

                    EvidenceStatus::Unknown => "➖",
                    // ☑

                    // ✔️

                    EvidenceStatus::Found => "✔️",
                    // ☒

                    // ❌

                    EvidenceStatus::Forbidden => "❌",
                }
                .to_string()
            })
        };

        let mut evidence_icons = Flex::row();
        for (i, e) in phasmo::EVIDENCES.iter().cloned().enumerate() {
            use druid::LensExt;
            let l = (AppData::evidence_status).index(i);
            let mut w = Flex::column();
            let button = Button::new(move |_evd: &EvidenceStatus, _env: &_| e.to_string())
                .on_click(
                    move |_ctx: &mut druid::EventCtx, e: &mut EvidenceStatus, _env: &_| {
                        e.next_status();
                    },
                )
                .expand_width()
                .lens(l);
            let l = (AppData::evidence_status).index(i);
            let status = evidence_status().lens(l);
            w.add_flex_child(button, 1.0);
            w.add_flex_child(status, 1.0);
            evidence_icons.add_flex_child(w, 1.0);
        }
        root.add_flex_child(evidence_icons, 1.0);

        /*
        let evidence_button = || {
            Button::new(|evidence: &EvidenceState, _env: &_| evidence.kind.to_string()).on_click(
                |_ctx, e: &mut EvidenceState, _env| {
                    e.status.next_status();
                },
            )
        };

        let evidences = Hlist::new(move || {
            Flex::column()
                .with_child(evidence_button())
                .with_child(evidence_status())
            // .with_flex_spacer(1.0)
            // .padding(10.0)
            // .background(Color::rgb(0.5, 0.0, 0.5))
            // .fix_height(50.0)
        })
        .lens(AppData::evidences);

        let mut evidences_flex_list = Flex::row();
        evidences_flex_list.add_flex_child(evidences, 1.0);

        root.add_flex_child(evidences_flex_list, 1.0);
        */
    }

    // reset button

    {
        let mut reset_flex = Flex::row();
        let reset_button = Button::new("🔄")
            .on_click(|_ctx, evidences: &mut Vector<EvidenceStatus>, _env: _| {
                for e in evidences.iter_mut() {
                    *e = EvidenceStatus::default();
                }
            })
            .padding((0., 0., 0., 0.))
            .lens(AppData::evidence_status);

        reset_flex.add_flex_child(reset_button, 1.0);
        root.add_flex_child(reset_flex, 1.0);
    }

    // general ghost info

    {
        let ghost_info = Label::new(|g: &Wghost, _env: &_| {
            format!(
                "{} {:?}\n\n🕵️ {}\n\n{}",
                g.0,
                g.0,
                g.0.evidences().map(|e| e.to_string()).collect::<String>(),
                g.0.description()
            )
        })
        .with_text_size(18.0)
        .padding((0., 18., 0., 0.))
        .lens(AppData::selected);

        let mut ghost_flex = Flex::row();
        ghost_flex.add_flex_child(ghost_info, 1.0);

        root.add_flex_child(ghost_flex, 1.0);
    }

    // cursor overlay

    // {

    //     let cursor = Cursor::default();

    //     overlay::Overlay::new(root, cursor)

    // }

    root.padding((20., 10.0, 20., 10.))
}

pub fn run() {
    use druid::{AppLauncher, LocalizedString, WindowDesc};

    let main_window = WindowDesc::new(ui_builder).title(
        LocalizedString::new("app-window-title").with_placeholder("Phasmo Evidence Tracker"),
    );
    // .with_min_size((610.0, 420.0));


    // Set our initial data

    let delegate = Delegate {};
    let data = AppData::default();
    AppLauncher::with_window(main_window)
        .delegate(delegate)
        // changes the default theme

        .configure_env(|env: &mut _, _t: &AppData| {
            use druid::{theme, Color};

            // tomorrow night-based theme

            let dark_bg = &Color::from_rgba32_u32(0x1D1F21FF);
            let dark_bg2 = &Color::from_rgba32_u32(0x29211AFF);
            let light_fg = &Color::from_rgba32_u32(0xC5C8C6FF);

            // solarized theme

            // let base03 = &Color::from_rgba32_u32(0x002b36ff);

            // let base02 = &Color::from_rgba32_u32(0x073642ff);

            // let base01 = &Color::from_rgba32_u32(0x586e75ff);

            // let base00 = &Color::from_rgba32_u32(0x657b83ff);

            // let base0 = &Color::from_rgba32_u32(0x839496ff);

            // let base1 = &Color::from_rgba32_u32(0x93a1a1ff);

            // let base2 = &Color::from_rgba32_u32(0xeee8d5ff);

            // let base3 = &Color::from_rgba32_u32(0xfdf6e3ff);

            // let yellow = &Color::from_rgba32_u32(0xb58900ff);

            // let orange = &Color::from_rgba32_u32(0xcb4b16ff);

            // let red = &Color::from_rgba32_u32(0xdc322fff);

            // let magenta = &Color::from_rgba32_u32(0xd33682ff);

            // let violet = &Color::from_rgba32_u32(0x6c71c4ff);

            // let blue = &Color::from_rgba32_u32(0x268bd2ff);

            // let cyan = &Color::from_rgba32_u32(0x2aa198ff);

            // let green = &Color::from_rgba32_u32(0x859900ff);


            env.set(theme::LABEL_COLOR, light_fg.clone());

            // env.set(theme::PRIMARY_LIGHT, blue.clone());

            // env.set(theme::PRIMARY_DARK, yellow.clone());

            // env.set(theme::FOREGROUND_LIGHT, red.clone());

            // env.set(theme::FOREGROUND_DARK, green.clone());

            // env.set(theme::BACKGROUND_LIGHT, red.clone());

            // env.set(theme::BACKGROUND_DARK, green.clone());

            // env.set(theme::SELECTION_COLOR, red.clone());


            env.set(theme::BUTTON_LIGHT, dark_bg2.clone());
            env.set(theme::BUTTON_DARK, dark_bg.clone());
            env.set(theme::WINDOW_BACKGROUND_COLOR, dark_bg.clone());
            env.set(theme::TEXT_SIZE_NORMAL, 22.0f64);
            env.set(theme::TEXT_SIZE_LARGE, 30.0f64);
        })
        .use_simple_logger()
        .launch(data)
        .expect("launch failed")
}