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
use crate::domain::Annotate;
use crate::history::Record;
use crate::result::RvResult;
use crate::tools_data::annotations::{ClipboardData, InstanceAnnotations};
use crate::tools_data::{get_mut, get_specific_mut, CoreOptions, LabelInfo, ToolSpecifics};
use crate::Shape;
use crate::{domain::PtF, events::Events, history::History, world::World};

pub(super) fn check_trigger_redraw(
    mut world: World,
    name: &'static str,
    f_tool_access: impl FnMut(&mut ToolSpecifics) -> RvResult<&mut CoreOptions> + Clone,
) -> World {
    let data_mut = get_mut(&mut world, name, "could not access data");
    let core_options = get_specific_mut(f_tool_access.clone(), data_mut).cloned();
    if core_options.map(|o| o.is_redraw_annos_triggered) == Some(true) {
        world.request_redraw_annotations(name, core_options.map(|o| o.visible) == Some(true));
        let data_mut = get_mut(&mut world, name, "could not access data");
        let core_options_mut = get_specific_mut(f_tool_access, data_mut);
        if let Some(core_options_mut) = core_options_mut {
            core_options_mut.is_redraw_annos_triggered = false;
        }
    }
    world
}

macro_rules! released_key {
    ($($key:ident),*) => {
        #[derive(Debug, Clone, Copy)]
        pub(super) enum ReleasedKey {
            None,
            $($key,)*
        }
        pub(super) fn map_released_key(event: &Events) -> ReleasedKey {
            if false {
                ReleasedKey::None
            } $(else if event.released($crate::KeyCode::$key) {
                ReleasedKey::$key
            })*
            else {
                ReleasedKey::None
            }
        }
    };
}
macro_rules! set_cat_current {
    ($num:expr, $label_info:expr) => {
        if $num < $label_info.cat_ids().len() + 1 {
            $label_info.cat_idx_current = $num - 1;
        }
    };
}

released_key!(
    A, D, E, H, C, V, L, Key0, Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Delete, Back,
    Left, Right, Up, Down
);

pub fn check_recolorboxes(
    mut world: World,
    actor: &'static str,
    mut get_core_options_mut: impl FnMut(&mut World) -> Option<&mut CoreOptions>,
    mut get_label_info_mut: impl FnMut(&mut World) -> Option<&mut LabelInfo>,
) -> World {
    let is_colorchange_triggered =
        get_core_options_mut(&mut world).map(|o| o.is_colorchange_triggered);
    if is_colorchange_triggered == Some(true) {
        let core_options = get_core_options_mut(&mut world);
        if let Some(core_options) = core_options {
            core_options.is_colorchange_triggered = false;
            core_options.visible = true;
        }
        if let Some(label_info) = get_label_info_mut(&mut world) {
            label_info.new_random_colors();
        }
    }
    let are_boxes_visible = true;
    world.request_redraw_annotations(actor, are_boxes_visible);
    world
}
pub(super) fn label_change_key(key: ReleasedKey, mut label_info: LabelInfo) -> LabelInfo {
    match key {
        ReleasedKey::Key1 => {
            set_cat_current!(1, label_info);
        }
        ReleasedKey::Key2 => {
            set_cat_current!(2, label_info);
        }
        ReleasedKey::Key3 => {
            set_cat_current!(3, label_info);
        }
        ReleasedKey::Key4 => {
            set_cat_current!(4, label_info);
        }
        ReleasedKey::Key5 => {
            set_cat_current!(5, label_info);
        }
        ReleasedKey::Key6 => {
            set_cat_current!(6, label_info);
        }
        ReleasedKey::Key7 => {
            set_cat_current!(7, label_info);
        }
        ReleasedKey::Key8 => {
            set_cat_current!(8, label_info);
        }
        ReleasedKey::Key9 => {
            set_cat_current!(9, label_info);
        }
        _ => (),
    }
    label_info
}
pub(super) fn paste<T>(
    mut world: World,
    mut history: History,
    actor: &'static str,
    mut get_annos_mut: impl FnMut(&mut World) -> Option<&mut InstanceAnnotations<T>>,
    clipboard: Option<ClipboardData<T>>,
) -> (World, History)
where
    T: Annotate + Default + PartialEq + Clone,
{
    if let Some(clipboard) = &clipboard {
        let cb_bbs = clipboard.elts();
        if !cb_bbs.is_empty() {
            let shape_orig = Shape::from_im(world.data.im_background());
            if let Some(a) = get_annos_mut(&mut world) {
                a.extend(
                    cb_bbs.iter().cloned(),
                    clipboard.cat_idxs().iter().copied(),
                    shape_orig,
                )
            }
        }
    }
    let are_boxes_visible = true;
    world.request_redraw_annotations(actor, are_boxes_visible);
    history.push(Record::new(world.data.clone(), actor));

    (world, history)
}

pub(super) fn on_selection_keys<T>(
    mut world: World,
    mut history: History,
    key: ReleasedKey,
    is_ctrl_held: bool,
    actor: &'static str,
    mut get_annos_mut: impl FnMut(&mut World) -> Option<&mut InstanceAnnotations<T>>,
    mut get_clipboard_mut: impl FnMut(&mut World) -> Option<&mut Option<ClipboardData<T>>>,
) -> (World, History)
where
    T: Annotate + PartialEq + Default + Clone,
{
    let visible = true;
    match key {
        ReleasedKey::A if is_ctrl_held => {
            // Select all
            if let Some(a) = get_annos_mut(&mut world) {
                a.select_all()
            };
            world.request_redraw_annotations(actor, visible);
        }
        ReleasedKey::D if is_ctrl_held => {
            // Deselect all
            if let Some(a) = get_annos_mut(&mut world) {
                a.deselect_all()
            };
            world.request_redraw_annotations(actor, visible);
        }
        ReleasedKey::C if is_ctrl_held => {
            // Copy to clipboard
            let clipboard_data =
                get_annos_mut(&mut world).map(|d| ClipboardData::from_annotations(d));
            if let (Some(clipboard_data), Some(clipboard_mut)) =
                (clipboard_data, get_clipboard_mut(&mut world))
            {
                *clipboard_mut = Some(clipboard_data);
            }

            world.request_redraw_annotations(actor, visible);
        }
        ReleasedKey::V if is_ctrl_held => {
            let clipboard_data = get_clipboard_mut(&mut world).cloned().flatten();
            (world, history) = paste(world, history, actor, get_annos_mut, clipboard_data);
        }
        ReleasedKey::Delete | ReleasedKey::Back => {
            // Remove selected
            let annos = get_annos_mut(&mut world);
            if let Some(annos) = annos {
                if !annos.selected_mask().is_empty() {
                    annos.remove_selected();
                    world.request_redraw_annotations(actor, visible);
                    history.push(Record::new(world.data.clone(), actor));
                }
            }
        }
        _ => (),
    }
    (world, history)
}

pub trait Manipulate {
    fn new() -> Self
    where
        Self: Sized;

    fn on_activate(&mut self, world: World, history: History) -> (World, History) {
        (world, history)
    }
    fn on_deactivate(&mut self, world: World, history: History) -> (World, History) {
        (world, history)
    }
    fn on_filechange(&mut self, world: World, history: History) -> (World, History) {
        (world, history)
    }
    /// All events that are used by a tool are implemented in here. Use the macro [`make_tool_transform`](make_tool_transform). See, e.g.,
    /// [`Zoom::events_tf`](crate::tools::Zoom::events_tf).
    fn events_tf(&mut self, world: World, history: History, events: &Events) -> (World, History);
}

const N_HIST_ELTS: usize = 8;

#[derive(Clone, Copy, Debug)]
pub struct Mover {
    mouse_pos_start: Option<PtF>,
    mouse_pos_history: [Option<PtF>; N_HIST_ELTS],
    idx_next_history_update: usize,
}
impl Mover {
    pub fn new() -> Self {
        Self {
            mouse_pos_start: None,
            mouse_pos_history: [None; N_HIST_ELTS],
            idx_next_history_update: 0,
        }
    }
    pub fn move_mouse_held<T, F: FnOnce(PtF, PtF) -> T>(
        &mut self,
        f_move: F,
        mouse_pos: Option<PtF>,
    ) -> Option<T> {
        let res = if let (Some(mp_start), Some(mp)) = (self.mouse_pos_start, mouse_pos) {
            if !self.mouse_pos_history.contains(&mouse_pos) {
                let mpo_from = Some(mp_start);
                let mpo_to = Some(mp);
                match (mpo_from, mpo_to) {
                    (Some(mp_from), Some(mp_to)) => Some(f_move(mp_from, mp_to)),
                    _ => None,
                }
            } else {
                None
            }
        } else {
            None
        };
        self.mouse_pos_history[self.idx_next_history_update % N_HIST_ELTS] = self.mouse_pos_start;
        self.mouse_pos_start = mouse_pos;
        self.idx_next_history_update = self.idx_next_history_update.wrapping_add(1);
        res
    }
    pub fn move_mouse_pressed(&mut self, mouse_pos: Option<PtF>) {
        if mouse_pos.is_some() {
            self.mouse_pos_start = mouse_pos;
        }
    }
}

// applies the tool transformation to the world
#[macro_export]
macro_rules! make_tool_transform {
    (
        $self:expr,
        $world:expr,
        $history:expr,
        $event:expr,
        [$(($key_event:ident, $key_btn:expr, $method_name:ident)),*]
    ) => {
        if false {
            ($world, $history)
        }
        $(else if $event.$key_event($key_btn) {
            $self.$method_name($event, $world, $history)
        })*
        else {
            ($world, $history)
        }
    };
}