edit_layer_dialog.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
       ---
       edit_layer_dialog.rs (6263B)
       ---
            1 use eframe::egui::{self, color_picker, Layout, RichText};
            2 use egui_modal::Modal;
            3 use i18n_embed_fl::fl;
            4 use icy_engine::{Color, Mode, Properties};
            5 
            6 use crate::{AnsiEditor, Message, ModalDialog, TerminalResult};
            7 
            8 pub struct EditLayerDialog {
            9     pub should_commit: bool,
           10 
           11     layer: usize,
           12 
           13     properties: Properties,
           14 }
           15 
           16 impl EditLayerDialog {
           17     pub fn new(buf: &icy_engine::Buffer, layer: usize) -> Self {
           18         let l = &buf.layers[layer];
           19         EditLayerDialog {
           20             should_commit: false,
           21             layer,
           22             properties: l.properties.clone(),
           23         }
           24     }
           25 }
           26 
           27 impl ModalDialog for EditLayerDialog {
           28     fn show(&mut self, ctx: &egui::Context) -> bool {
           29         let mut result = false;
           30         let modal: Modal = Modal::new(ctx, "edit_layer_dialog");
           31 
           32         modal.show(|ui| {
           33             modal.title(ui, fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-title"));
           34 
           35             modal.frame(ui, |ui| {
           36                 egui::Grid::new("some_unique_id").num_columns(2).spacing([4.0, 8.0]).show(ui, |ui| {
           37                     ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
           38                         ui.label(fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-name-label"));
           39                     });
           40                     ui.add(egui::TextEdit::singleline(&mut self.properties.title));
           41                     ui.end_row();
           42 
           43                     if self.properties.color.is_some() {
           44                         let mut use_color = true;
           45 
           46                         if let Some(color) = &mut self.properties.color {
           47                             ui.label("");
           48                             ui.horizontal(|ui| {
           49                                 let mut c: [u8; 3] = (color.clone()).into();
           50                                 color_picker::color_edit_button_srgb(ui, &mut c);
           51                                 *color = c.into();
           52 
           53                                 ui.checkbox(&mut use_color, "Use Color");
           54                             });
           55                             ui.end_row();
           56                         }
           57 
           58                         if !use_color {
           59                             self.properties.color = None;
           60                         }
           61                     } else {
           62                         ui.label("");
           63                         let mut use_color = false;
           64                         ui.checkbox(&mut use_color, "Use Color");
           65                         ui.end_row();
           66                         if use_color {
           67                             self.properties.color = Some(Color::new(255, 255, 255));
           68                         }
           69                     }
           70 
           71                     ui.label("");
           72                     ui.checkbox(
           73                         &mut self.properties.is_visible,
           74                         fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-is-visible-checkbox"),
           75                     );
           76                     ui.end_row();
           77                     ui.label("");
           78                     ui.checkbox(
           79                         &mut self.properties.is_locked,
           80                         fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-is-edit-locked-checkbox"),
           81                     );
           82                     ui.end_row();
           83                     ui.label("");
           84                     ui.checkbox(
           85                         &mut self.properties.is_position_locked,
           86                         fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-is-position-locked-checkbox"),
           87                     );
           88                     ui.end_row();
           89 
           90                     ui.label("");
           91                     ui.checkbox(
           92                         &mut self.properties.has_alpha_channel,
           93                         fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-has-alpha-checkbox"),
           94                     );
           95                     ui.end_row();
           96 
           97                     if self.properties.has_alpha_channel {
           98                         ui.label("");
           99                         ui.checkbox(
          100                             &mut self.properties.is_alpha_channel_locked,
          101                             fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-is-alpha-locked-checkbox"),
          102                         );
          103                         ui.end_row();
          104                     }
          105 
          106                     ui.label("Mode:");
          107 
          108                     egui::ComboBox::from_id_source("combobox1")
          109                         .width(150.)
          110                         .selected_text(RichText::new(match self.properties.mode {
          111                             Mode::Normal => "Normal",
          112                             Mode::Chars => "Chars only",
          113                             Mode::Attributes => "Attribute only",
          114                         }))
          115                         .show_ui(ui, |ui| {
          116                             ui.selectable_value(&mut self.properties.mode, Mode::Normal, "Normal");
          117                             ui.selectable_value(&mut self.properties.mode, Mode::Chars, "Chars only");
          118                             ui.selectable_value(&mut self.properties.mode, Mode::Attributes, "Attribute only");
          119                         });
          120                     ui.end_row();
          121 
          122                     ui.label(fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-is-x-offset-label"));
          123                     ui.add(egui::DragValue::new(&mut self.properties.offset.x));
          124 
          125                     ui.end_row();
          126 
          127                     ui.label(fl!(crate::LANGUAGE_LOADER, "edit-layer-dialog-is-y-offset-label"));
          128                     ui.add(egui::DragValue::new(&mut self.properties.offset.y));
          129                     ui.end_row();
          130                 });
          131                 ui.add_space(16.0);
          132             });
          133 
          134             modal.buttons(ui, |ui| {
          135                 if ui.button(fl!(crate::LANGUAGE_LOADER, "new-file-ok")).clicked() {
          136                     self.should_commit = true;
          137                     result = true;
          138                 }
          139                 if ui.button(fl!(crate::LANGUAGE_LOADER, "new-file-cancel")).clicked() {
          140                     result = true;
          141                 }
          142             });
          143         });
          144         modal.open();
          145         result
          146     }
          147 
          148     fn should_commit(&self) -> bool {
          149         self.should_commit
          150     }
          151 
          152     fn commit(&self, editor: &mut AnsiEditor) -> TerminalResult<Option<Message>> {
          153         let mut bv = editor.buffer_view.lock();
          154 
          155         if bv.get_buffer_mut().layers[self.layer].properties != self.properties {
          156             bv.get_edit_state_mut().update_layer_properties(self.layer, self.properties.clone())?;
          157         }
          158         Ok(None)
          159     }
          160 }