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
use crate::{
    drawme::{Annotation, Stroke},
    UpdateAnnos,
};

use self::bbox_data::OUTLINE_THICKNESS_CONVERSION;
pub use self::{
    bbox_data::BboxExportData, bbox_data::BboxSpecificData, brush_data::BrushToolData,
    coco_io::write_coco,
};
pub mod annotations;
pub mod bbox_data;
pub mod brush_data;
pub mod coco_io;

macro_rules! variant_access {
    ($variant:ident, $func_name:ident, $self:ty, $return_type:ty) => {
        pub fn $func_name(self: $self) -> $return_type {
            match self {
                ToolSpecifics::$variant(x) => x,
                _ => panic!("this is not a {}", stringify!($variant)),
            }
        }
    };
}

#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum ToolSpecifics {
    Bbox(BboxSpecificData),
    Brush(BrushToolData),
}
impl ToolSpecifics {
    variant_access!(Bbox, bbox, &Self, &BboxSpecificData);
    variant_access!(Brush, brush, &Self, &BrushToolData);
    variant_access!(Bbox, bbox_mut, &mut Self, &mut BboxSpecificData);
    variant_access!(Brush, brush_mut, &mut Self, &mut BrushToolData);

    pub fn to_annotations_view(&self, file_path: &str) -> UpdateAnnos {
        match &self {
            ToolSpecifics::Bbox(bb_data) => {
                if let Some(annos) = bb_data.get_annos(file_path) {
                    let bbs = annos.geos();
                    let cats = annos.cat_idxs();
                    let selected_bbs = annos.selected_bbs();
                    let labels = bb_data.labels();
                    let colors = bb_data.colors();

                    let bbs_colored = bbs
                        .iter()
                        .zip(cats.iter())
                        .zip(selected_bbs.iter())
                        .map(|((bb, cat_idx), is_selected)| Annotation {
                            geofig: bb.clone(),
                            fill_color: Some(colors[*cat_idx]),
                            fill_alpha: bb_data.options.fill_alpha,
                            label: Some(labels[*cat_idx].clone()),
                            outline: Stroke {
                                thickness: bb_data.options.outline_thickness as f32
                                    / OUTLINE_THICKNESS_CONVERSION,
                                color: colors[*cat_idx],
                            },
                            outline_alpha: bb_data.options.outline_alpha,
                            is_selected: Some(*is_selected),
                        })
                        .collect::<Vec<Annotation>>();
                    UpdateAnnos::Yes((bbs_colored, None))
                } else {
                    UpdateAnnos::clear()
                }
            }
            ToolSpecifics::Brush(_) => {
                // TODO: draw polygon
                UpdateAnnos::default()
            }
        }
    }
}
impl Default for ToolSpecifics {
    fn default() -> Self {
        ToolSpecifics::Bbox(BboxSpecificData::default())
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct ToolsData {
    pub specifics: ToolSpecifics,
    pub menu_active: bool,
}
impl ToolsData {
    pub fn new(specifics: ToolSpecifics) -> Self {
        ToolsData {
            specifics,
            menu_active: false,
        }
    }
}