aboutsummaryrefslogtreecommitdiff
path: root/src/client/tool/icon_tool.rs
diff options
context:
space:
mode:
authorArne Dußin2021-01-27 14:01:50 +0100
committerArne Dußin2021-02-02 22:16:15 +0100
commitf92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch)
tree20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client/tool/icon_tool.rs
parentc60a6d07efb120724b308e29e8e70f27c87c952d (diff)
downloadgraf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz
graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client/tool/icon_tool.rs')
-rw-r--r--src/client/tool/icon_tool.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/client/tool/icon_tool.rs b/src/client/tool/icon_tool.rs
new file mode 100644
index 0000000..caf9d60
--- /dev/null
+++ b/src/client/tool/icon_tool.rs
@@ -0,0 +1,88 @@
+//! Tool for creating icons. For explanation of icons, please see
+//! [the icon module](crate::map::icon).
+
+use crate::client::config::IconToolBinds;
+use crate::client::input::Input;
+use crate::client::map::{icon_texture_manager::IconTextureManager, IconMark, Map, Mappable};
+use crate::client::tool::Tool;
+use crate::client::transform::Transform;
+use crate::math::Vec2;
+use crate::net::Cargo;
+use crate::net::Connection;
+use raylib::core::drawing::RaylibDrawHandle;
+use std::ops::Deref;
+use std::rc::Rc;
+
+/// The icon tool itself.
+pub struct IconTool {
+ keybindings: IconToolBinds,
+ /// 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: IconMark,
+ textures: Rc<IconTextureManager>,
+}
+
+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: IconToolBinds, textures: Rc<IconTextureManager>) -> Self {
+ Self {
+ keybindings,
+ active: false,
+ current_icon: IconMark::new(0, Vec2::default(), 0., textures.clone()),
+ textures,
+ }
+ }
+}
+
+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,
+ server: &Connection<Cargo>,
+ _mouse_pos_m: &Vec2<f64>,
+ ) {
+ server.send(Cargo::AddIcon(self.current_icon.deref().clone()));
+ }
+
+ fn handle_custom_bindings(
+ &mut self,
+ _map: &mut Map,
+ _server: &Connection<Cargo>,
+ input: &mut Input,
+ ) {
+ if input.poll_global(&self.keybindings.next) {
+ self.current_icon.id = (self.current_icon.id + 1) % self.textures.num_icons();
+ }
+ if input.poll_global(&self.keybindings.previous) {
+ self.current_icon.id =
+ (self.current_icon.id + self.textures.num_icons() - 1) % self.textures.num_icons();
+ }
+ if input.poll_global(&self.keybindings.rotate_clockwise) {
+ self.current_icon.rotation += 45.;
+ }
+ if input.poll_global(&self.keybindings.rotate_counterclockwise) {
+ self.current_icon.rotation -= 45.;
+ }
+ }
+}