draw_rectangle_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_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_rectangle, BrushMode, ColorMode},
7 AnsiEditor, Event, Message,
8 };
9
10 use super::{Position, Tool};
11
12 pub struct DrawRectangleTool {
13 draw_mode: BrushMode,
14 color_mode: ColorMode,
15 pub char_code: std::rc::Rc<std::cell::RefCell<char>>,
16 old_pos: Position,
17 }
18
19 impl Default for DrawRectangleTool {
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 DrawRectangleTool {
31 fn get_icon(&self) -> &egui::Image<'static> {
32 &super::icons::RECTANGLE_OUTLINE_SVG
33 }
34
35 fn tool_name(&self) -> String {
36 fl!(crate::LANGUAGE_LOADER, "tool-rectangle_name")
37 }
38
39 fn tooltip(&self) -> String {
40 fl!(crate::LANGUAGE_LOADER, "tool-rectangle_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.show_ui(ui, editor_opt, self.char_code.clone(), crate::paint::BrushUi::All)
53 }
54
55 fn handle_hover(&mut self, _ui: &egui::Ui, response: egui::Response, _editor: &mut AnsiEditor, _cur: Position, _cur_abs: Position) -> egui::Response {
56 response.on_hover_cursor(egui::CursorIcon::Crosshair)
57 }
58
59 fn handle_drag_begin(&mut self, _editor: &mut AnsiEditor, _response: &egui::Response) -> Event {
60 self.old_pos = Position::new(-1, -1);
61 Event::None
62 }
63 fn handle_drag(&mut self, _ui: &egui::Ui, response: egui::Response, editor: &mut AnsiEditor, _calc: &TerminalCalc) -> egui::Response {
64 let p2 = editor.half_block_click_pos;
65 if self.old_pos == p2 {
66 return response;
67 }
68 self.old_pos = p2;
69
70 editor.clear_overlay_layer();
71 let p1 = editor.drag_pos.start_half_block;
72 let start = Position::new(p1.x.min(p2.x), p1.y.min(p2.y));
73 let end = Position::new(p1.x.max(p2.x), p1.y.max(p2.y));
74 draw_rectangle(&mut editor.buffer_view.lock(), start, end, self.draw_mode.clone(), self.color_mode);
75 response
76 }
77
78 fn handle_drag_end(&mut self, editor: &mut AnsiEditor) -> Option<Message> {
79 if editor.drag_pos.start == editor.drag_pos.cur {
80 editor.buffer_view.lock().get_buffer_mut().remove_overlay();
81 } else {
82 editor.join_overlay(fl!(crate::LANGUAGE_LOADER, "undo-draw-rectangle"));
83 }
84 None
85 }
86 }