Skip to content

Commit

Permalink
started mapping more events
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Oct 31, 2023
1 parent b6f8742 commit 4f8dd01
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 200 deletions.
84 changes: 81 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,50 @@ use egui::{
epaint::RectShape, Color32, ColorImage, Context, Image, Pos2, Rect, Rounding, Sense, Shape,
Stroke, TextureHandle, TextureOptions, Ui, Vec2,
};
use rvlib::{domain::Point, Events, MainEventLoop, UpdateImage};
use rvlib::{domain::Point, Events, KeyCode, MainEventLoop, UpdateImage};
use std::mem;

fn map_key(egui_key: egui::Key) -> Option<rvlib::KeyCode> {
match egui_key {
egui::Key::A => Some(rvlib::KeyCode::A),
egui::Key::B => Some(rvlib::KeyCode::B),
egui::Key::C => Some(rvlib::KeyCode::C),
egui::Key::D => Some(rvlib::KeyCode::D),
egui::Key::L => Some(rvlib::KeyCode::L),
egui::Key::H => Some(rvlib::KeyCode::H),
egui::Key::M => Some(rvlib::KeyCode::M),
egui::Key::Q => Some(rvlib::KeyCode::Q),
egui::Key::R => Some(rvlib::KeyCode::R),
egui::Key::T => Some(rvlib::KeyCode::T),
egui::Key::V => Some(rvlib::KeyCode::V),
egui::Key::Y => Some(rvlib::KeyCode::Y),
egui::Key::Z => Some(rvlib::KeyCode::Z),
egui::Key::Num0 => Some(rvlib::KeyCode::Key0),
egui::Key::Num1 => Some(rvlib::KeyCode::Key1),
egui::Key::Num2 => Some(rvlib::KeyCode::Key2),
egui::Key::Num3 => Some(rvlib::KeyCode::Key3),
egui::Key::Num4 => Some(rvlib::KeyCode::Key4),
egui::Key::Num5 => Some(rvlib::KeyCode::Key5),
egui::Key::Num6 => Some(rvlib::KeyCode::Key6),
egui::Key::Num7 => Some(rvlib::KeyCode::Key7),
egui::Key::Num8 => Some(rvlib::KeyCode::Key8),
egui::Key::Num9 => Some(rvlib::KeyCode::Key9),
egui::Key::PlusEquals => Some(rvlib::KeyCode::Equals),
egui::Key::Minus => Some(rvlib::KeyCode::Minus),
egui::Key::Delete => Some(rvlib::KeyCode::Delete),
egui::Key::Backspace => Some(rvlib::KeyCode::Back),
egui::Key::ArrowLeft => Some(rvlib::KeyCode::Left),
egui::Key::ArrowRight => Some(rvlib::KeyCode::Right),
egui::Key::ArrowUp => Some(rvlib::KeyCode::Up),
egui::Key::ArrowDown => Some(rvlib::KeyCode::Down),
egui::Key::F5 => Some(rvlib::KeyCode::F5),
egui::Key::PageDown => Some(rvlib::KeyCode::PageDown),
egui::Key::PageUp => Some(rvlib::KeyCode::PageUp),
egui::Key::Escape => Some(rvlib::KeyCode::Escape),
_ => None,
}
}

#[derive(Default)]
struct RvImageApp {
event_loop: MainEventLoop,
Expand Down Expand Up @@ -61,8 +102,36 @@ fn draw_bbs(ui: &mut Ui, bbs: &[rvlib::BB], stroke: rvlib::Stroke, fill_rgb: [u8

impl eframe::App for RvImageApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
let update_view = self.event_loop.one_iteration(&self.events, ctx);
egui::CentralPanel::default().show(ctx, |ui| {
if let Ok(update_view) = self.event_loop.one_iteration(&self.events, ctx) {
ui.input(|i| {
self.events.set_events(
i.events
.iter()
.flat_map(move |e| match e {
egui::Event::Key {
key,
pressed,
repeat,
modifiers,
} => {
if let Some(k) = map_key(*key) {
if !pressed {
Some(rvlib::Event::Released(k))
} else {
Some(rvlib::Event::Pressed(k))
}

} else {
None
}
}
_ => None,
})
.collect::<Vec<_>>(),
);
});
if let Ok(update_view) = update_view {
ui.label(update_view.image_info);
if let UpdateImage::Yes(im) = update_view.image {
let color_image = ColorImage::from_rgb(
Expand All @@ -86,8 +155,17 @@ impl eframe::App for RvImageApp {
let mouse_pos = image_response.hover_pos();
let mouse_pos = mouse_pos.map(|mp| Point {
x: ((mp.x - offset_x) / size.x * self.size[0] as f32) as u32,
y: ((mp.y -offset_y) / size.y * self.size[1] as f32) as u32,
y: ((mp.y - offset_y) / size.y * self.size[1] as f32) as u32,
});
if image_response.clicked() {
self.events.released(KeyCode::MouseLeft);
}
if image_response.drag_released() {
self.events.released(KeyCode::MouseLeft);
}
if image_response.drag_started() {
self.events.pressed(KeyCode::MouseLeft);
}
self.events = mem::take(&mut self.events).mousepos(mouse_pos);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/rvlib/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ impl Events {
self.mouse_pos = mouse_pos;
self
}
pub fn events(mut self, events: Vec<Event>) -> Self {
pub fn set_events(&mut self, events: Vec<Event>) {
self.events = events;
self
}
action_keycode!(held_alt, Held, Alt);
action_keycode!(held_shift, Held, Shift);
Expand Down
2 changes: 1 addition & 1 deletion src/rvlib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ mod util;
pub mod world;
pub use domain::BB;
pub use drawme::{Stroke, UpdateAnnos, UpdateImage, UpdateView, UpdateZoomBox};
pub use events::Events;
pub use events::{Events, KeyCode, Event};
pub use main_loop::MainEventLoop;
pub use tools_data::annotations;
20 changes: 15 additions & 5 deletions src/rvlib/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::result::RvResult;
use crate::tools::{make_tool_vec, Manipulate, ToolState, ToolWrapper, BBOX_NAME, ZOOM_NAME};
use crate::world::World;
use crate::{apply_tool_method_mut, httpserver, image_util, UpdateView};
use egui::Context;
use egui::{Context, Ui};
use image::{DynamicImage, GenericImageView};
use image::{ImageBuffer, Rgb};
use lazy_static::lazy_static;
Expand Down Expand Up @@ -57,7 +57,7 @@ fn pos_2_string(im: &DynamicImage, x: u32, y: u32) -> String {
)
} else {
"".to_string()
}
}
}

fn get_pixel_on_orig_str(world: &World, mouse_pos: &Option<Point>) -> Option<String> {
Expand Down Expand Up @@ -157,9 +157,19 @@ impl Default for MainEventLoop {
impl MainEventLoop {
pub fn one_iteration(&mut self, e: &Events, ctx: &Context) -> RvResult<UpdateView> {
self.menu
.ui(&ctx, &mut self.ctrl, &mut self.world.data.tools_data_map);
self.tools_select_menu
.ui(&ctx, &mut self.tools, &mut self.world.data.tools_data_map)?;
.ui(ctx, &mut self.ctrl, &mut self.world.data.tools_data_map);
egui::SidePanel::right("my_panel")
.show(ctx, |ui| {
ui.vertical(|ui| {
self.tools_select_menu.ui(
ui,
&mut self.tools,
&mut self.world.data.tools_data_map,
)
})
.inner
})
.inner?;

// update world based on tools
if self.recently_activated_tool_idx.is_none() {
Expand Down
Loading

0 comments on commit 4f8dd01

Please sign in to comment.