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