rvlib/tools/
rot90.rs

1use crate::{
2    annotations_accessor_mut,
3    events::{Events, KeyCode},
4    history::{History, Record},
5    make_tool_transform,
6    tools_data::rot90_data::NRotations,
7    world::{DataRaw, World},
8    world_annotations_accessor,
9};
10
11use super::Manipulate;
12
13pub const ACTOR_NAME: &str = "Rot90";
14annotations_accessor_mut!(ACTOR_NAME, rot90_mut, "Rotation 90 didn't work", NRotations);
15world_annotations_accessor!(ACTOR_NAME, rot90, "Rotation 90 didn't work", NRotations);
16
17/// rotate 90 degrees counter clockwise
18fn rot90(ims: &DataRaw, n_rotations: NRotations) -> DataRaw {
19    let mut ims = ims.clone();
20    match n_rotations {
21        NRotations::Zero => (),
22        NRotations::One => ims.apply(|im| im.rotate270()),
23        NRotations::Two => ims.apply(|im| im.rotate180()),
24        NRotations::Three => ims.apply(|im| im.rotate90()),
25    }
26    ims
27}
28#[derive(Clone, Copy, Debug)]
29pub struct Rot90;
30
31impl Rot90 {
32    fn key_pressed(
33        &mut self,
34        _events: &Events,
35        mut world: World,
36        mut history: History,
37    ) -> (World, History) {
38        world = World::new(rot90(&world.data, NRotations::One), *world.zoom_box());
39        if let Some(anno) = get_annos_mut(&mut world) {
40            *anno = anno.increase();
41        }
42        history.push(Record::new(world.clone(), ACTOR_NAME));
43        (world, history)
44    }
45}
46
47impl Manipulate for Rot90 {
48    fn new() -> Self {
49        Self {}
50    }
51
52    fn on_filechange(&mut self, mut world: World, history: History) -> (World, History) {
53        if let Some(nrot) = get_annos_if_some(&world) {
54            world = World::new(rot90(&world.data, *nrot), *world.zoom_box());
55        }
56        (world, history)
57    }
58
59    fn events_tf(&mut self, world: World, history: History, event: &Events) -> (World, History) {
60        make_tool_transform!(
61            self,
62            world,
63            history,
64            event,
65            [(pressed, KeyCode::R, key_pressed)]
66        )
67    }
68}