aboutsummaryrefslogtreecommitdiff
path: root/src/tool/icon_tool.rs
blob: c9e671ee547ab9315221c14c7b569f2272a248ae (plain) (blame)
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
//! Tool for creating icons. For explanation of icons, please see
//! [the icon module](crate::map::icon).

use crate::button::Button;
use crate::config::IconToolKeys;
use crate::map::icon_renderer::IconRenderer;
use crate::map::{Icon, Map, Mappable};
use crate::math::Vec2;
use crate::tool::Tool;
use crate::transform::Transform;
use raylib::core::drawing::RaylibDrawHandle;
use std::rc::Rc;

/// The icon tool itself.
pub struct IconTool {
    keybindings: IconToolKeys,
    /// Saves whether the IconTool is the currently active tool or not.
    active: bool,
    /// The information of the icon that should be placed / is currently being placed, if it
    /// exists.
    current_icon: Icon,
    renderer: Rc<IconRenderer>,
}

impl IconTool {
    /// Create a new icon tool that renders icons with the provided icon renderer. There should only
    /// be one instance of the tool for the program, which should be created in the editor.
    pub fn new(keybindings: IconToolKeys, renderer: Rc<IconRenderer>) -> Self {
        Self {
            keybindings,
            active: false,
            current_icon: Icon::new(0, Vec2::default(), 0., renderer.clone()),
            renderer,
        }
    }
}

impl Tool for IconTool {
    fn activate(&mut self) {
        self.active = true;
    }

    fn deactivate(&mut self) {
        self.active = false;
    }

    fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) {
        self.current_icon.position = *mouse_pos_m;
    }

    fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
        if self.active {
            self.current_icon.draw(rld, transform);
        }
    }

    fn place_single(&mut self, map: &mut Map, _mouse_pos_m: &Vec2<f64>) {
        map.push_icon(self.current_icon.clone());
    }

    fn on_button_pressed(&mut self, _map: &mut Map, button: Button) {
        if button == self.keybindings.next {
            self.current_icon.id = (self.current_icon.id + 1) % self.renderer.num_icons();
        } else if button == self.keybindings.previous {
            self.current_icon.id =
                (self.current_icon.id + self.renderer.num_icons() - 1) % self.renderer.num_icons();
        } else if button == self.keybindings.rotate_clockwise {
            self.current_icon.rotation += 45.;
        } else if button == self.keybindings.rotate_counterclockwise {
            self.current_icon.rotation -= 45.;
        }
    }
}