pencil_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
       ---
       pencil_imp.rs (4317B)
       ---
            1 use std::{cell::RefCell, rc::Rc};
            2 
            3 use eframe::egui::{self};
            4 use i18n_embed_fl::fl;
            5 use icy_engine::{editor::AtomicUndoGuard, TextAttribute};
            6 use icy_engine_egui::TerminalCalc;
            7 
            8 use crate::{
            9     paint::{plot_point, BrushMode, ColorMode, PointRole},
           10     AnsiEditor, Event, Message,
           11 };
           12 
           13 use super::{Position, Tool};
           14 
           15 pub struct PencilTool {
           16     char_code: std::rc::Rc<std::cell::RefCell<char>>,
           17     undo_op: Option<AtomicUndoGuard>,
           18     draw_mode: BrushMode,
           19     color_mode: ColorMode,
           20     pub attr: TextAttribute,
           21 
           22     last_pos: Position,
           23     cur_pos: Position,
           24 }
           25 
           26 impl Default for PencilTool {
           27     fn default() -> Self {
           28         Self {
           29             undo_op: None,
           30             draw_mode: BrushMode::HalfBlock,
           31             color_mode: ColorMode::Both,
           32             char_code: Rc::new(RefCell::new('\u{00B0}')),
           33             last_pos: Position::default(),
           34             cur_pos: Position::default(),
           35             attr: icy_engine::TextAttribute::default(),
           36         }
           37     }
           38 }
           39 
           40 impl Tool for PencilTool {
           41     fn get_icon(&self) -> &egui::Image<'static> {
           42         &super::icons::PENCIL_SVG
           43     }
           44 
           45     fn tool_name(&self) -> String {
           46         fl!(crate::LANGUAGE_LOADER, "tool-pencil_name")
           47     }
           48 
           49     fn tooltip(&self) -> String {
           50         fl!(crate::LANGUAGE_LOADER, "tool-pencil_tooltip")
           51     }
           52 
           53     fn use_caret(&self, _editor: &AnsiEditor) -> bool {
           54         false
           55     }
           56 
           57     fn show_ui(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui, editor_opt: Option<&mut AnsiEditor>) -> Option<Message> {
           58         self.color_mode.show_ui(ui);
           59         self.draw_mode
           60             .show_ui(ui, editor_opt, self.char_code.clone(), crate::paint::BrushUi::HideOutline)
           61     }
           62 
           63     fn handle_click(&mut self, editor: &mut AnsiEditor, button: i32, pos: Position, _pos_abs: Position, _response: &egui::Response) -> Option<Message> {
           64         if button == 1 {
           65             self.last_pos = pos;
           66             let _op: AtomicUndoGuard = editor.begin_atomic_undo(fl!(crate::LANGUAGE_LOADER, "undo-pencil"));
           67             editor.clear_overlay_layer();
           68             plot_point(
           69                 &mut editor.buffer_view.lock(),
           70                 editor.half_block_click_pos,
           71                 self.draw_mode.clone(),
           72                 self.color_mode,
           73                 PointRole::Line,
           74             );
           75             editor.join_overlay(fl!(crate::LANGUAGE_LOADER, "undo-pencil"));
           76         }
           77         None
           78     }
           79     fn handle_hover(&mut self, _ui: &egui::Ui, response: egui::Response, _editor: &mut AnsiEditor, cur: Position, _cur_abs: Position) -> egui::Response {
           80         self.cur_pos = cur;
           81         response.on_hover_cursor(egui::CursorIcon::Crosshair)
           82     }
           83 
           84     fn handle_drag(&mut self, _ui: &egui::Ui, response: egui::Response, editor: &mut AnsiEditor, _calc: &TerminalCalc) -> egui::Response {
           85         if self.last_pos == editor.half_block_click_pos {
           86             return response;
           87         }
           88         plot_point(
           89             &mut editor.buffer_view.lock(),
           90             editor.half_block_click_pos,
           91             self.draw_mode.clone(),
           92             self.color_mode,
           93             PointRole::Line,
           94         );
           95 
           96         self.last_pos = editor.half_block_click_pos;
           97         self.cur_pos = editor.drag_pos.cur;
           98         editor.buffer_view.lock().get_edit_state_mut().set_is_buffer_dirty();
           99 
          100         response
          101     }
          102 
          103     fn handle_drag_begin(&mut self, editor: &mut AnsiEditor, _response: &egui::Response) -> Event {
          104         self.undo_op = Some(editor.begin_atomic_undo(fl!(crate::LANGUAGE_LOADER, "undo-pencil")));
          105         self.last_pos = editor.half_block_click_pos;
          106         self.cur_pos = editor.drag_pos.cur;
          107         editor.clear_overlay_layer();
          108         plot_point(
          109             &mut editor.buffer_view.lock(),
          110             editor.half_block_click_pos,
          111             self.draw_mode.clone(),
          112             self.color_mode,
          113             PointRole::Line,
          114         );
          115         editor.buffer_view.lock().get_edit_state_mut().set_is_buffer_dirty();
          116         Event::None
          117     }
          118 
          119     fn handle_drag_end(&mut self, editor: &mut AnsiEditor) -> Option<Message> {
          120         editor.join_overlay(fl!(crate::LANGUAGE_LOADER, "undo-pencil"));
          121         self.undo_op = None;
          122         None
          123     }
          124 
          125     fn get_toolbar_location_text(&self, _editor: &AnsiEditor) -> String {
          126         let pos = self.cur_pos;
          127         fl!(crate::LANGUAGE_LOADER, "toolbar-position", line = (pos.y + 1), column = (pos.x + 1))
          128     }
          129 }