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
use winit_input_helper::WinitInputHelper;

use crate::{
    domain::{self, Shape, BB},
    file_util::MetaData,
    history::History,
    types::ViewImage,
    world::World,
};

#[derive(Debug, Clone)]
pub struct InitialView {
    file_path: Option<String>,
    image: Option<ViewImage>,
}
impl InitialView {
    fn updated_needed(&self, world_meta: &MetaData) -> bool {
        if let Some(is_active) = world_meta.is_loading_screen_active {
            !is_active
                && (self.file_path != world_meta.file_path
                    || (self.file_path.is_some() && self.image.is_none()))
        } else {
            false
        }
    }
    pub fn update(&mut self, world: &World, shape_win: Shape) -> bool {
        let is_update_needed = self.updated_needed(&world.data.meta_data);
        if is_update_needed {
            self.file_path = world
                .data
                .meta_data
                .file_path
                .as_ref()
                .map(|s| s.to_string());
            self.image = Some(
                world
                    .data
                    .bg_to_unannotated_view(world.zoom_box(), shape_win),
            );
        }
        is_update_needed
    }
    pub fn image(&self) -> &Option<ViewImage> {
        &self.image
    }
    pub fn new() -> Self {
        Self {
            file_path: None,
            image: None,
        }
    }
}

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

    fn on_activate(
        &mut self,
        world: World,
        history: History,
        _shape_win: Shape,
    ) -> (World, History) {
        (world, history)
    }
    fn on_deactivate(
        &mut self,
        world: World,
        history: History,
        _shape_win: Shape,
    ) -> (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,
        shape_win: Shape,
        mouse_pos: Option<(usize, usize)>,
        input_event: &WinitInputHelper,
    ) -> (World, History);
}

#[derive(Clone, Copy, Debug)]
pub struct Mover {
    mouse_pos_start: Option<(usize, usize)>,
}
impl Mover {
    pub fn new() -> Self {
        Self {
            mouse_pos_start: None,
        }
    }
    pub fn move_mouse_held<T, F: FnOnce((u32, u32), (u32, u32)) -> T>(
        &mut self,
        f_move: F,
        mouse_pos: Option<(usize, usize)>,
        shape_win: Shape,
        shape_orig: Shape,
        zoom_box: &Option<BB>,
    ) -> Option<T> {
        let res = if let (Some(mp_start), Some(mp)) = (self.mouse_pos_start, mouse_pos) {
            let mpo_from =
                domain::mouse_pos_to_orig_pos(Some(mp_start), shape_orig, shape_win, zoom_box);
            let mpo_to = domain::mouse_pos_to_orig_pos(Some(mp), shape_orig, shape_win, zoom_box);
            match (mpo_from, mpo_to) {
                (Some(mpso), Some(mpo)) => Some(f_move(mpso, mpo)),
                _ => None,
            }
        } else {
            None
        };
        self.mouse_pos_start = mouse_pos;
        res
    }
    pub fn move_mouse_pressed(&mut self, mouse_pos: Option<(usize, usize)>) {
        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,
        $shape_win:expr,
        $mouse_pos:expr,
        $event:expr,
        [$(($mouse_event:ident, $mouse_btn:expr)),*],
        [$(($key_event:ident, $key_btn:expr)),*]
    ) => {
        if false {
            ($world, $history)
        }
        $(else if $event.$mouse_event($mouse_btn) {
            $self.$mouse_event($event, $shape_win, $mouse_pos, $world, $history)
        })*
        $(else if $event.$key_event($key_btn) {
            $self.$key_event($event, $shape_win, $mouse_pos, $world, $history)
        })*
        else {
            ($world, $history)
        }
    };
}