aboutsummaryrefslogtreecommitdiff
path: root/src/map/icon_renderer.rs
diff options
context:
space:
mode:
authorArne Dußin2020-12-15 00:46:54 +0100
committerArne Dußin2020-12-15 22:51:46 +0100
commit9799d3c6a8f0c242668203a1c70d7b6cfed3e855 (patch)
tree9116acbc886f680f82309a42b4e6147e65c1433b /src/map/icon_renderer.rs
parent3bc690803fb59493ea8180fd630d65b3e26642d0 (diff)
downloadgraf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.tar.gz
graf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.zip
Refactor to make interaction between tools easier
Diffstat (limited to 'src/map/icon_renderer.rs')
-rw-r--r--src/map/icon_renderer.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/map/icon_renderer.rs b/src/map/icon_renderer.rs
new file mode 100644
index 0000000..fb81e24
--- /dev/null
+++ b/src/map/icon_renderer.rs
@@ -0,0 +1,77 @@
+use crate::math::Vec2;
+use raylib::core::texture::Texture2D;
+use raylib::{RaylibHandle, RaylibThread};
+use ron::de::from_reader;
+use serde::Deserialize;
+use std::fs::{self, File};
+
+pub const ICON_DIR: &str = "assets/icons";
+
+#[derive(Deserialize)]
+pub(super) struct IconFileInfo {
+ /// The position the icon should be anchored in pixels. This is the Vector it will be moved by
+ /// relative to the mouse pointer (to the left and up).
+ pub anchor: Vec2<f64>,
+ /// The scale of the icon as expressed in image pixels per real meter.
+ pub pixels_per_m: f64,
+}
+
+pub struct IconRenderer {
+ texture_data: Vec<(Texture2D, IconFileInfo)>,
+}
+
+impl IconRenderer {
+ pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread) -> Self {
+ /* Read all available icons from the icon directory. SVGs do not need any special scale
+ * file, but pixel-based file formats require a RON-file declaring what the scale of the
+ * picture is right beside them.
+ */
+ let mut image_files = Vec::new();
+ for entry in fs::read_dir(ICON_DIR).expect("Could not open icon directory") {
+ let entry = entry.expect("Failed to read file from icon directory");
+
+ // Ignore the RON-files for now and put the image files into the vec
+ if entry
+ .path()
+ .extension()
+ .expect("Entry does not have a file extension")
+ != "ron"
+ {
+ image_files.push(entry);
+ }
+ }
+
+ // Read the RON-files where it is necessary.
+ let mut texture_data = Vec::with_capacity(image_files.len());
+ for file in image_files {
+ // TODO: Handle svg
+
+ let texture = rl
+ .load_texture(
+ rlt,
+ file.path()
+ .to_str()
+ .expect("Unable to convert path to string."),
+ )
+ .expect("Could not read image file");
+
+ let mut file = file.path();
+ file.set_extension("ron");
+ let ron = File::open(file).expect("Could not read ron file for icon information.");
+ let icon_info: IconFileInfo =
+ from_reader(ron).expect("Could not parse icon info from reader.");
+
+ texture_data.push((texture, icon_info));
+ }
+
+ Self { texture_data }
+ }
+
+ pub(super) fn get(&self, icon_id: usize) -> &(Texture2D, IconFileInfo) {
+ &self.texture_data[icon_id]
+ }
+
+ pub fn num_icons(&self) -> usize {
+ self.texture_data.len()
+ }
+}