aboutsummaryrefslogtreecommitdiff
path: root/src/tool/mod.rs
blob: 70534aceed913765fd9b06684bd9f3def141c4b7 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Tools, which are user interfaces that must be specifically selected in order to do something.
//!
//! As stated, a tool is not simply everything that helps a user do something, think of it more as a
//! mode which must be elected by the user to perform a task on a specific object type or a class of
//! objects. If instead the operation is defined by the state of the program, it is not a tool, since
//! the user didn't explicitly ask for this function to be performed, but it is rather an option
//! that's inherent to the situation the user finds themselves in.

pub mod deletion_tool;
pub mod icon_tool;
pub mod polygon_room_tool;
pub mod rect_room_tool;
pub mod selection_tool;
pub mod wall_tool;

pub use deletion_tool::DeletionTool;
pub use icon_tool::IconTool;
pub use polygon_room_tool::PolygonRoomTool;
pub use rect_room_tool::RectRoomTool;
pub use selection_tool::SelectionTool;
pub use wall_tool::WallTool;

use crate::button::Button;
use crate::map::Map;
use crate::math::Vec2;
use crate::transform::Transform;
use raylib::core::drawing::RaylibDrawHandle;

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(u8)]
/// The types of tools available in graf karto. For information about the tool itself, please see the
/// referenced Tool's documentation.
pub enum ToolType {
    /// See [super::RectRoomTool] for information on this tool.
    RectRoomTool,
    /// See [super::PolygonRoomTool] for information on this tool.
    PolygonRoomTool,
    /// See [super::WallTool] for information on this tool.
    WallTool,
    /// See [super::IconTool] for information on this tool.
    IconTool,
    /// See [super::DeletionTool] for information on this tool.
    DeletionTool,
    /// See [super::SelectionTool] for information on this tool.
    SelectionTool,
    /// Not a real tool but used to know how many tools are available. New tools must be added
    /// above this variant.
    // TODO: Since we now use a hash map in the editor, check if this is still necessary at all.
    NumTools,
}

/// Base trait for tools. A tool is something that performs a specific action on one or more types of
/// elements. It must be selected in order to be active. For this reason, the selection tool is a
/// tool (it must be selected from the toolbox), but the dimension indicator for instance is not,
/// since it is automatically updated when applicable.
pub trait Tool {
    /// Code that needs to be called when this Tool is activated or reactivated goes here.
    fn activate(&mut self) {}
    /// Cleanup that needs to be done when the user switches from this tool to something else goes here.
    fn deactivate(&mut self) {}

    /// Called on each frame when this tool is active.
    fn update(&mut self, map: &Map, mouse_pos_m: &Vec2<f64>);

    /// Draw the contents of this tool.
    // TODO: Maybe make this tool mappable? This might make it easier to make things resizable while
    // it's still being drawn.
    fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform);

    /// Generic keybinding.
    /// Code to place a single node for this tool.
    fn place_single(&mut self, _map: &mut Map, _mouse_pos_m: &Vec2<f64>) {}

    /// Generic keybinding.
    /// Code to finish whatever one is doing with this tool currently and trying to apply the
    /// changes to the map data.
    fn finish(&mut self, _map: &mut Map) {}

    /// Generic keybinding.
    /// Stop whatever one is doing with this tool and do not apply any changes to the map data.
    fn abort(&mut self) {}

    /// If there are any additional keybindings that need to be handled by this tool, these can be
    /// handled here.
    fn on_button_pressed(&mut self, _map: &mut Map, _button: Button) {}
}