use sfml::system::Vector2; use sfml::window::{Event, Key, mouse::Button as MouseButton}; use sfml::graphics::{Color, RectangleShape, RenderTarget, Transformable, Shape}; use crate::engine::renderer::Renderer; use crate::engine::map::{CollisionTile, Map}; use crate::engine::texture::SPRITE_SIZE_I32; use crate::engine::font::Font; use crate::Result; /// An action caused by a button. #[derive(Copy, Clone)] pub enum Action { /// Saves the level. Save, } /// A button that can be clicked. pub struct Button { /// The label of the button. pub label: String, /// The position of the button. pub position: Vector2, /// The size of the button. pub size: Vector2, /// The action of the button. action: Action, } impl Button { /// Creates a new button. pub fn new(label: &str, action: Action, x: f32, y: f32, w: f32, h: f32) -> Button { Button { action, label: label.to_owned(), position: Vector2::new(x, y), size: Vector2::new(w, h), } } /// Returns the shape of the button. pub fn shape(&self) -> RectangleShape { let mut shape = RectangleShape::new(); shape.set_position(self.position + 0.1 * self.size); shape.set_size(self.size - 0.2 * self.size); shape.set_fill_color(&Color::rgb(255, 255, 255)); shape } /// Draws the button on the renderer. pub fn render_on(&self, renderer: &mut Renderer) { renderer.window_mut().draw(&self.shape()); renderer.draw_text( &self.label, Font::Sansation, self.size.y as u32 / 2, &Color::rgb(0, 0, 0) ); } /// Returns true if the coordinates fall inside the shape of the button. pub fn contains(&self, x: f32, y: f32) -> bool { let shape = self.shape(); x > shape.position().x && x < shape.position().x + shape.size().x && y > shape.position().y && y < shape.position().y + shape.size().y } } /// Represents a level editor. pub struct Editor { /// The renderer used by the editor. renderer: Renderer, /// The map being currently edited. map: Map, /// Indicates whether the editor is running or not. running: bool, /// The size of the left panel. left_panel_size: Vector2, /// The size of the top panel. top_panel_size: Vector2, /// The buttons of the menu bar. menu_bar: Vec