aboutsummaryrefslogtreecommitdiff
path: root/src/client/map/icon_mark.rs
blob: 02ed5011aed760de0889ff25943a8d143732cd69 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Icon marker on the map. For information about icons see [Icon](crate::)

use super::icon_texture_manager::IconTextureManager;
use crate::client::colours::DEFAULT_COLOURS;
use crate::client::map::Mappable;
use crate::client::transform::Transform;
use crate::math::Vec2;
use crate::world::{Component, Icon};
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use std::ops::{Deref, DerefMut};
use std::rc::Rc;

/// Describes an icon in the world and can be drawn.
#[derive(Clone)]
pub struct IconMark {
    icon: Icon,
    selected: bool,
    textures: Rc<IconTextureManager>,
}

impl IconMark {
    /// Create a new icon marker. Requires the icon textures that are used to render this icon, as well as all
    /// the information necessary to describe the icon itself.
    pub fn new(
        id: usize,
        position: Vec2<f64>,
        rotation: f64,
        renderer: Rc<IconTextureManager>,
    ) -> Self {
        Self::from_icon(
            Icon {
                id,
                position,
                rotation,
            },
            renderer,
        )
    }

    /// Like `new()`, but with the icon data bundled into the icon type.
    pub fn from_icon(icon: Icon, textures: Rc<IconTextureManager>) -> Self {
        Self {
            icon,
            selected: false,
            textures,
        }
    }

    /// Set the inner icon this icon mark is referencing.
    pub fn set_icon(&mut self, icon: Icon) {
        self.icon = icon;
    }
}

impl Mappable for IconMark {
    fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
        let (texture, info) = self.textures.get(self.id);
        // Round the position to whole pixels to fix rotation problems.
        let mut position_px =
            transform.point_m_to_px(&(self.position - (info.anchor / info.pixels_per_m)));
        position_px.x = position_px.x.floor();
        position_px.y = position_px.y.floor();
        rld.draw_texture_ex(
            texture,
            position_px,
            self.rotation as f32,
            (transform.pixels_per_m() / info.pixels_per_m) as f32,
            if self.selected() {
                DEFAULT_COLOURS.icon_selected
            } else {
                DEFAULT_COLOURS.icon_normal
            },
        );
    }

    fn set_selected(&mut self, selected: bool) {
        self.selected = selected;
    }

    fn selected(&self) -> bool {
        self.selected
    }

    fn as_component(&self) -> &dyn Component {
        self.deref()
    }
}

impl Deref for IconMark {
    type Target = Icon;

    fn deref(&self) -> &Self::Target {
        &self.icon
    }
}
impl DerefMut for IconMark {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.icon
    }
}