draw_rectangle_filled_imp.rs - icy_draw - icy_draw is the successor to mystic draw. fork / mirror
 (HTM) git clone https://git.drkhsh.at/icy_draw.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       draw_rectangle_filled_imp.rs (2879B)
       ---
            1 use eframe::egui;
            2 use i18n_embed_fl::fl;
            3 use icy_engine::Position;
            4 use icy_engine_egui::TerminalCalc;
            5 
            6 use crate::{
            7     paint::{fill_rectangle, BrushMode, ColorMode},
            8     AnsiEditor, Event, Message,
            9 };
           10 
           11 use super::Tool;
           12 
           13 pub struct DrawRectangleFilledTool {
           14     draw_mode: BrushMode,
           15     color_mode: ColorMode,
           16     char_code: std::rc::Rc<std::cell::RefCell<char>>,
           17     old_pos: Position,
           18 }
           19 
           20 impl Default for DrawRectangleFilledTool {
           21     fn default() -> Self {
           22         Self {
           23             draw_mode: BrushMode::HalfBlock,
           24             color_mode: crate::paint::ColorMode::Both,
           25             char_code: std::rc::Rc::new(std::cell::RefCell::new('\u{00B0}')),
           26             old_pos: Position::default(),
           27         }
           28     }
           29 }
           30 
           31 impl Tool for DrawRectangleFilledTool {
           32     fn get_icon(&self) -> &egui::Image<'static> {
           33         &super::icons::RECTANGLE_FILLED_SVG
           34     }
           35 
           36     fn tool_name(&self) -> String {
           37         fl!(crate::LANGUAGE_LOADER, "tool-filled_rectangle_name")
           38     }
           39 
           40     fn tooltip(&self) -> String {
           41         fl!(crate::LANGUAGE_LOADER, "tool-filled_rectangle_tooltip")
           42     }
           43 
           44     fn use_caret(&self, _editor: &AnsiEditor) -> bool {
           45         false
           46     }
           47     fn use_selection(&self) -> bool {
           48         false
           49     }
           50 
           51     fn show_ui(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui, editor_opt: Option<&mut AnsiEditor>) -> Option<Message> {
           52         self.color_mode.show_ui(ui);
           53         self.draw_mode
           54             .show_ui(ui, editor_opt, self.char_code.clone(), crate::paint::BrushUi::HideOutline)
           55     }
           56 
           57     fn handle_hover(&mut self, _ui: &egui::Ui, response: egui::Response, _editor: &mut AnsiEditor, _cur: Position, _cur_abs: Position) -> egui::Response {
           58         response.on_hover_cursor(egui::CursorIcon::Crosshair)
           59     }
           60 
           61     fn handle_drag_begin(&mut self, _editor: &mut AnsiEditor, _response: &egui::Response) -> Event {
           62         self.old_pos = Position::new(-1, -1);
           63         Event::None
           64     }
           65 
           66     fn handle_drag(&mut self, _ui: &egui::Ui, response: egui::Response, editor: &mut AnsiEditor, _calc: &TerminalCalc) -> egui::Response {
           67         let p2 = editor.half_block_click_pos;
           68         if self.old_pos == p2 {
           69             return response;
           70         }
           71         self.old_pos = p2;
           72 
           73         editor.clear_overlay_layer();
           74         let p1 = editor.drag_pos.start_half_block;
           75         let start = Position::new(p1.x.min(p2.x), p1.y.min(p2.y));
           76         let end = Position::new(p1.x.max(p2.x), p1.y.max(p2.y));
           77         fill_rectangle(&mut editor.buffer_view.lock(), start, end, self.draw_mode.clone(), self.color_mode);
           78         response
           79     }
           80 
           81     fn handle_drag_end(&mut self, editor: &mut AnsiEditor) -> Option<Message> {
           82         if editor.drag_pos.start == editor.drag_pos.cur {
           83             editor.buffer_view.lock().get_buffer_mut().remove_overlay();
           84         } else {
           85             editor.join_overlay(fl!(crate::LANGUAGE_LOADER, "undo-draw-rectangle"));
           86         }
           87         None
           88     }
           89 }