diff --git a/src/binocle.rs b/src/binocle.rs index 689a75d..cd39899 100644 --- a/src/binocle.rs +++ b/src/binocle.rs @@ -12,7 +12,7 @@ use crate::view::View; pub struct Binocle { pub settings: Settings, - buffer: Buffer, + pub buffer: Buffer, } impl Binocle { diff --git a/src/event_loop.rs b/src/event_loop.rs index c4275e1..9bdd0ae 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -231,6 +231,30 @@ pub fn run(options: CliOptions) -> Result<()> { } if let Some((x, y)) = input.mouse() { + if let Ok((x, y)) = pixels.window_pos_to_pixel((x, y)) { + let px_size = 2usize.pow((settings.zoom - 1) as u32); + let x_px = x / px_size; + let y_px = y / px_size; + let px_window_selected = x_px + (y_px * settings.width as usize); + let offset = settings.offset + settings.offset_fine; + let byte_data_selected = px_window_selected + offset as usize; + let data = binocle.buffer.data(); + let start = data.get(byte_data_selected..); + let end = start.map(|start| start.get(..16)).flatten(); + let bytes = match (start, end) { + (None, _) => None, + (Some(partial_data), None) => { + let mut data = 0u128.to_ne_bytes(); + data[..partial_data.len()].copy_from_slice(partial_data); + Some(data) + } + (_, Some(data)) => Some(data.try_into().unwrap()), + }; + if let Some(bytes) = bytes { + settings.selected_byte = byte_data_selected; + settings.selected_data = bytes; + } + } if input.mouse_pressed(0) { if input.held_shift() { mouse_drag_action = MouseDragAction::ControlOffsetFine { diff --git a/src/gui.rs b/src/gui.rs index 21c08f9..208b5f3 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -298,6 +298,36 @@ impl Gui { ui.checkbox(&mut settings.hex_view_visible, "hex view"); ui.separator(); + ui.add(egui::Label::new("Data information").heading()); + let data_u128 = u128::from_le_bytes(settings.selected_data); + let data_u8 = data_u128 as u8; + let data_u16 = data_u128 as u16; + let data_u32 = data_u128 as u32; + let data_u64 = data_u128 as u64; + let mut to_label = |x: String| { + ui.add(egui::Label::new(x).monospace().wrap(false)); + }; + to_label(format!("byte: {}", settings.selected_byte)); + to_label(format!("u8: {data_u8}")); + to_label(format!("i8: {}", data_u8 as i8)); + to_label(format!("u16le: {data_u16}")); + to_label(format!("u16be: {}", data_u16.to_be())); + to_label(format!("i16le: {}", data_u16 as i16)); + to_label(format!("i16be: {}", data_u16.to_be() as i16)); + to_label(format!("u32le: {data_u32}")); + to_label(format!("u32be: {}", data_u32.to_be())); + to_label(format!("i32le: {}", data_u32 as i32)); + to_label(format!("i32be: {}", data_u32.to_be() as i32)); + to_label(format!("u64le: {data_u64}")); + to_label(format!("u64be: {}", data_u64.to_be())); + to_label(format!("i64le: {}", data_u64 as i64)); + to_label(format!("i64be: {}", data_u64.to_be() as i64)); + to_label(format!("f32le: {:e}", f32::from_bits(data_u32))); + to_label(format!("f32be: {:e}", f32::from_bits(data_u32.to_be()))); + to_label(format!("f64le: {:e}", f64::from_bits(data_u64))); + to_label(format!("f64be: {:e}", f64::from_bits(data_u64.to_be()))); + + ui.separator(); ui.add(egui::Label::new("Information").heading()); let file_size = settings .buffer_length diff --git a/src/settings.rs b/src/settings.rs index 5498dcf..15d2382 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -63,6 +63,9 @@ pub struct Settings { pub gui_wants_keyboard: bool, pub gui_wants_mouse: bool, + + pub selected_byte: usize, + pub selected_data: [u8; 16], } impl Settings { @@ -103,6 +106,8 @@ impl Default for Settings { hex_ascii: "".into(), gui_wants_keyboard: false, gui_wants_mouse: false, + selected_byte: 0, + selected_data: [0; 16], } } }