diff options
| author | Arne Dußin | 2021-01-27 14:01:50 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-02-02 22:16:15 +0100 |
| commit | f92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch) | |
| tree | 20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client/map/icon_mark.rs | |
| parent | c60a6d07efb120724b308e29e8e70f27c87c952d (diff) | |
| download | graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip | |
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client/map/icon_mark.rs')
| -rw-r--r-- | src/client/map/icon_mark.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/client/map/icon_mark.rs b/src/client/map/icon_mark.rs new file mode 100644 index 0000000..39fd554 --- /dev/null +++ b/src/client/map/icon_mark.rs @@ -0,0 +1,95 @@ +//! 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, + } + } +} + +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 + } +} |
