Skip to content

Commit

Permalink
Update settings and improve UI
Browse files Browse the repository at this point in the history
  • Loading branch information
addiswebb committed Jul 24, 2024
1 parent b2c5024 commit 4959aea
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
28 changes: 17 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,28 +400,30 @@ impl eframe::App for App {
ui.close_menu();
}
if ui
.add(egui::Button::new("Save File...").shortcut_text("Ctrl+S"))
.add(egui::Button::new("Open File...").shortcut_text("Ctrl+O"))
.clicked()
{
self.save_file();
self.open_file();
self.set_title(ctx);
ui.close_menu();
}
if ui
.add(egui::Button::new("Open File...").shortcut_text("Ctrl+O"))
.add(egui::Button::new("Save File...").shortcut_text("Ctrl+S"))
.clicked()
{
self.open_file();
self.save_file();
self.set_title(ctx);
ui.close_menu();
}
ui.separator();
if ui
.add(egui::Button::new("Settings").shortcut_text("Ctrl+,"))
.clicked()
{
self.settings.show = true;
ui.close_menu();
}
ui.separator();
if ui
.add(egui::Button::new("Exit").shortcut_text("Alt+F4"))
.clicked()
Expand Down Expand Up @@ -564,6 +566,7 @@ impl eframe::App for App {
ui.separator();
ui.add_space(4.);
egui::ScrollArea::vertical().show(ui, |ui| {
// Monitor offset
ui.vertical(|ui| {
ui.horizontal(|ui|{
ui.strong("Monitor Offset ");
Expand All @@ -576,7 +579,7 @@ impl eframe::App for App {
)
.on_hover_text("Y");
});
ui.label("Monitor Offset is used to correctly simulate mouse movements when using multiple monitors");
ui.label("Monitor Offset is used to correctly simulate mouse movements when using multiple monitors.");
ui.add_space(4.);
ui.horizontal(|ui|{
if ui.add(egui::Button::new("Calibrate")).on_hover_text("Calibrates the offset necessary to correctly move the mouse when using multiple monitors").clicked() {
Expand All @@ -602,6 +605,8 @@ impl eframe::App for App {
ui.add_space(6.);
ui.separator();
ui.add_space(6.);

// Recording resolution
ui.vertical(|ui|{
ui.horizontal(|ui|{
ui.strong("Recording Resolution");
Expand All @@ -625,6 +630,7 @@ impl eframe::App for App {
ui.add_space(6.);
ui.separator();
ui.add_space(6.);
// Fail safe
ui.vertical(|ui|{
ui.horizontal(|ui|{
ui.strong("Fail safe");
Expand All @@ -646,18 +652,18 @@ impl eframe::App for App {
ui.add_space(6.);
ui.separator();
ui.add_space(6.);
// Fail detection
ui.vertical(|ui|{
ui.horizontal(|ui|{
ui.strong("Error detection");
ui.add(egui::DragValue::new(&mut self.sequencer.max_percentage_error).speed(0.01).range(0.0..=1.0));
ui.strong("Fail detection");
ui.checkbox(&mut self.settings.fail_detection, "");
ui.add(egui::DragValue::new(&mut self.settings.max_fail_error).speed(0.01).range(0.0..=1.0));
});
ui.label("Incase of failure during playback, quickly slam the mouse into the selected edge to stop.");
ui.label("Computes the percentage different between the keyframe's expect screenshot vs what is on the screen and stops execution if it is beyond the threshold above, using computer vision.");
ui.small("Only works for main monitor");
});
ui.add_space(6.);
});
// ui.spacing();
// ui.separator();
}
SettingsPage::Shortcuts => {
ui.heading(egui::RichText::new("Shortcuts").strong());
Expand Down Expand Up @@ -722,7 +728,7 @@ impl eframe::App for App {
});

self.sequencer
.update(&mut self.last_instant, ctx, &self.settings.offset);
.update(&mut self.last_instant, ctx, &self.settings.offset, (self.settings.fail_detection,self.settings.max_fail_error));
self.sequencer.show(ctx);
self.sequencer.debug_panel(ctx);
self.sequencer.selected_panel(ctx);
Expand Down
58 changes: 24 additions & 34 deletions src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::util::*;
use eframe::egui::{self, pos2, Ui, Vec2};
use egui::{vec2, Align2, ColorImage, FontId, TextureHandle};
use egui::{Pos2, Rect};
use image::{DynamicImage, ImageBuffer, Rgba};
use opencv::core::VecN;
use serde::{Deserialize, Serialize};
use uuid::{Bytes, Uuid};

Expand Down Expand Up @@ -80,9 +78,7 @@ pub struct Sequencer {
current_image_uid: Bytes,
#[serde(skip)]
texture_handles: Vec<TextureHandle>,
x: (u8, f32),
pub failsafe_edge: Arc<Mutex<MonitorEdge>>,
pub max_percentage_error: f32,
}

impl Sequencer {
Expand Down Expand Up @@ -340,9 +336,7 @@ impl Sequencer {
current_image_uid: Uuid::nil().to_bytes_le(),
images,
texture_handles: Vec::new(),
x: (2, 0.999),
failsafe_edge,
max_percentage_error: 0.1,
}
}
/// Returns the current time where the playhead is
Expand Down Expand Up @@ -1196,20 +1190,7 @@ impl Sequencer {
self.combine_into_keystrokes();
}
ui.label(format!("scale: {:?}", self.scale));

ui.label(format!("scroll: {:?}", self.scroll));
ui.label("Tolerance");
ui.add(
egui::DragValue::new(&mut self.x.0)
.speed(1)
.range(0.0..=255.0),
);
ui.label("Confidence");
ui.add(
egui::DragValue::new(&mut self.x.1)
.speed(0.05)
.range(0.0..=1.0),
)
});
}
/// Renders the editable data of the selected keyframe
Expand Down Expand Up @@ -1428,7 +1409,13 @@ impl Sequencer {
});
}
/// Handles keeping state, and replaying keystrokes when playing
pub fn update(&mut self, last_instant: &mut Instant, ctx: &egui::Context, offset: &Vec2) {
pub fn update(
&mut self,
last_instant: &mut Instant,
ctx: &egui::Context,
offset: &Vec2,
fail_detection: (bool, f32),
) {
// Handle focus of the window when recording and when not
if self.was_recording != self.recording.load(Ordering::Relaxed) {
self.was_recording = self.recording.load(Ordering::Relaxed);
Expand Down Expand Up @@ -1573,13 +1560,13 @@ impl Sequencer {
self.current_image = Some(texture_handle.clone());
} else {
// Otherwise load it
let x = ColorImage::from_rgba_unmultiplied(
let image = ColorImage::from_rgba_unmultiplied(
[1920, 1080],
&screenshot.as_slice(),
);
let texture_handle = ctx.load_texture(
Uuid::from_bytes_le(keyframe.uid).to_string(),
x,
image,
Default::default(),
);
self.texture_handles.push(texture_handle.clone());
Expand All @@ -1592,21 +1579,22 @@ impl Sequencer {
if current_keyframe_state != keyframe_state[i] {
// If so and the sequencer is playing
if play {
// Check if the keyframe has a screenshot
if let Some(src1) = self.images.lock().unwrap().get(&keyframe.uid) {
if let Some(src2) = screenshot() {
let percentage_err = image_dif_opencv(src1, &src2);
if percentage_err > self.max_percentage_error {
self.play.swap(false, Ordering::Relaxed);
log::warn!(
"Fail Detected: {:?}% err",
percentage_err * 100.
);
break;
// When fail detection is enabled check if the keyframe has a screenshot
if fail_detection.0 {
if let Some(src1) = self.images.lock().unwrap().get(&keyframe.uid) {
if let Some(src2) = screenshot() {
let percentage_err = image_dif_opencv(src1, &src2);
if percentage_err > fail_detection.1 {
self.play.swap(false, Ordering::Relaxed);
log::warn!(
"Fail Detected: {:?}% err",
percentage_err * 100.
);
break;
}
}
}
}
println!("handling");
self.handle_playing_keyframe(keyframe, true, offset);
}
}
Expand Down Expand Up @@ -1774,7 +1762,9 @@ impl Sequencer {
KeyframeType::MagicMove(path) => {
if start {
let target = image::ImageReader::open(path).unwrap().decode().unwrap();
let now = Instant::now();
if let Some(target_center) = template_match_opencv(target.clone()) {
log::info!("Magic found target in {:?}", now.elapsed());
simulate_move(&target_center, offset);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct Settings {
#[serde(skip)]
pub keybind_search: String,
pub keybinds: Vec<Keybind>,
pub fail_detection: bool,
pub max_fail_error: f32,
pub offset: Vec2,
#[serde(skip)]
pub page: SettingsPage,
Expand Down Expand Up @@ -140,6 +142,8 @@ impl Default for Settings {
KeyboardShortcut::new(egui::Modifiers::CTRL, egui::Key::A),
),
],
fail_detection: true,
max_fail_error: 0.2,
offset: Vec2::NAN,
page: SettingsPage::Preferences,
show: false,
Expand Down
2 changes: 0 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ pub fn within_tolerance(value1: u8, value2: u8, tolerance: u8) -> bool {
use opencv::core::{Mat, MatTraitConst, Point, VecN};

pub fn template_match_opencv(target: DynamicImage) -> Option<Vec2> {
let now = Instant::now();
if let Some(screenshot) = screenshot() {
let screenshot: ImageBuffer<Rgba<u8>, Vec<u8>> =
ImageBuffer::from_vec(1920, 1080, screenshot).unwrap();
Expand Down Expand Up @@ -475,7 +474,6 @@ pub fn template_match_opencv(target: DynamicImage) -> Option<Vec2> {

let top_left = min_loc;

log::info!("Magic found target in {:?}", now.elapsed());
let pos = vec2(
(top_left.x as u32 + target.width() / 2) as f32,
(top_left.y as u32 + target.height() / 2) as f32,
Expand Down

0 comments on commit 4959aea

Please sign in to comment.