Skip to content

Commit

Permalink
Updated tests. Fixed #1
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilr612 committed Jul 19, 2024
1 parent 9f1ba9d commit 2908fd6
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 190 deletions.
221 changes: 115 additions & 106 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use raylib::ffi::{KeyboardKey, MouseButton};
use raylib::prelude::Rectangle as rayRect;
use raylib::RaylibHandle;

use crate::util::ConvertRE;

/// Struct to store values
pub struct InputOptions {
/// 'Point' to _native pixel_ conversion ratio. 'Points' are `egui`'s logical pixels.
Expand Down Expand Up @@ -41,16 +43,114 @@ impl Default for InputOptions {
}
}

fn conv_rect(r: rayRect) -> egRect {
egRect {
min: Pos2::new(r.x, r.y),
max: Pos2::new(r.x + r.width, r.y + r.height),
fn get_mouse_input(
rl: &mut RaylibHandle,
events: &mut Vec<Event>,
pixels_per_point: f32,
modifiers: Modifiers,
) {
let mouse_delta = rl.get_mouse_delta().scale_by(1.0 / pixels_per_point);
let mouse_position = rl.get_mouse_position().scale_by(1.0 / pixels_per_point);

if mouse_delta.x > 0.0 || mouse_delta.y > 0.0 {
events.push(Event::MouseMoved(Vec2::new(mouse_delta.x, mouse_delta.y)));
events.push(Event::PointerMoved(Pos2::new(
mouse_position.x,
mouse_position.y,
)));
}

if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Primary,
pressed: true,
modifiers,
})
} else if rl.is_mouse_button_released(MouseButton::MOUSE_BUTTON_LEFT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Primary,
pressed: false,
modifiers,
})
}

if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Secondary,
pressed: true,
modifiers,
})
} else if rl.is_mouse_button_released(MouseButton::MOUSE_BUTTON_RIGHT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Secondary,
pressed: false,
modifiers,
})
}
}

fn get_keyboard_input(
opt: &InputOptions,
rl: &mut RaylibHandle,
events: &mut Vec<Event>,
modifiers: Modifiers,
ctx: &egui::Context,
) {
events.extend(opt.key_map.iter().filter_map(|(&kk, &key)| {
if rl.is_key_pressed(kk) {
Some(Event::Key {
key,
physical_key: None,
pressed: true,
repeat: false,
modifiers,
})
} else if rl.is_key_released(kk) {
Some(Event::Key {
key,
physical_key: None,
pressed: false,
repeat: false,
modifiers,
})
} else {
None
}
}));

// Egui actually wants Text input right now.
if ctx.wants_keyboard_input() {
let mut buf = String::new();
// So give them that. Raylib queues characters anyways.
loop {
let r = rl.get_char_pressed();
if let Some(ch) = r {
buf.push(ch);
} else {
break;
}
}
if !buf.is_empty() {
events.push(Event::Text(buf));
}
}
}

/// Using the provided input options, gather all required input for egui.
/// `last_key` is simply an option to track the key pressed in previous frame, so that it's release event may be pushed..
pub fn gather_input(opt: &InputOptions, last_key: &mut HashSet<, ctx: &egui::Context, rl: &mut RaylibHandle) -> RawInput {
/// `last_key` is simply an option to track the key pressed in previous frame, so that it's release event may be pushed..
pub fn gather_input(opt: &InputOptions, ctx: &egui::Context, rl: &mut RaylibHandle) -> RawInput {
let monitor_id = raylib::window::get_current_monitor();
let (mw, mh) = (
raylib::window::get_monitor_width(monitor_id),
Expand Down Expand Up @@ -81,7 +181,7 @@ pub fn gather_input(opt: &InputOptions, last_key: &mut HashSet<, ctx: &egui::Con
focused: Some(rl.is_window_focused()),
};

let screen_rect = opt.region.map(conv_rect).or(window_size);
let screen_rect = opt.region.map(|r| r.convert()).or(window_size);

let modifiers = Modifiers {
alt: rl.is_key_down(KeyboardKey::KEY_LEFT_ALT)
Expand All @@ -94,56 +194,9 @@ pub fn gather_input(opt: &InputOptions, last_key: &mut HashSet<, ctx: &egui::Con
command: false,
};

let mut events: Vec<_> = opt
.key_map
.iter()
.filter_map(|(&kk, &key)| {
if rl.is_key_pressed(kk) {
Some(Event::Key {
key,
physical_key: None,
pressed: true,
repeat: false,
modifiers,
})
} else if rl.is_key_released(kk) {
Some(Event::Key {
key,
physical_key: None,
pressed: false,
repeat: false,
modifiers,
})
} else {
None
}
})
.collect();
let mut events: Vec<_> = Vec::new();

if let Some(key) = last_key.take() {
events.push(Event::Key {
key,
physical_key: None,
pressed: false,
repeat: false,
modifiers,
});
}

if let Some(key) = rl.get_char_pressed().and_then(|ch| {
let mut s = String::new();
s.push(ch);
Key::from_name(&s)
}) {
last_key.replace(key);
events.push(Event::Key {
key,
physical_key: None,
pressed: true,
repeat: false,
modifiers,
});
}
get_keyboard_input(opt, rl, &mut events, modifiers, ctx);

if rl.is_key_pressed(KeyboardKey::KEY_C) && modifiers.ctrl {
events.push(Event::Copy)
Expand All @@ -154,55 +207,9 @@ pub fn gather_input(opt: &InputOptions, last_key: &mut HashSet<, ctx: &egui::Con
}
}

let mouse_delta = rl.get_mouse_delta().scale_by(1.0 / pixels_per_point);
let mouse_position = rl.get_mouse_position().scale_by(1.0 / pixels_per_point);
if mouse_delta.x > 0.0 || mouse_delta.y > 0.0 {
events.push(Event::MouseMoved(Vec2::new(mouse_delta.x, mouse_delta.y)));
events.push(Event::PointerMoved(Pos2::new(
mouse_position.x,
mouse_position.y,
)));
}

if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Primary,
pressed: true,
modifiers,
})
} else if rl.is_mouse_button_released(MouseButton::MOUSE_BUTTON_LEFT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Primary,
pressed: false,
modifiers,
})
}

if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Secondary,
pressed: true,
modifiers,
})
} else if rl.is_mouse_button_released(MouseButton::MOUSE_BUTTON_RIGHT) {
let pos = rl.get_mouse_position();
let pos = Pos2::new(pos.x / pixels_per_point, pos.y / pixels_per_point);
events.push(Event::PointerButton {
pos,
button: egui::PointerButton::Secondary,
pressed: false,
modifiers,
})
}
// if ctx.wants_pointer_input() {
get_mouse_input(rl, &mut events, pixels_per_point, modifiers);
// }

let dropped_files = if rl.is_file_dropped() {
rl.load_dropped_files()
Expand All @@ -228,7 +235,9 @@ pub fn gather_input(opt: &InputOptions, last_key: &mut HashSet<, ctx: &egui::Con
Vec::new()
};

if !events.is_empty() { println!("Events: {events:?}"); }
if !events.is_empty() {
println!("Events: {events:?}");
}

RawInput {
viewport_id: ViewportId::ROOT,
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use raylib::{

pub mod input;
pub mod paint;
pub mod util;

#[cfg(test)]
mod tests;
Expand All @@ -65,7 +66,6 @@ pub struct RlEgui {
inopt: InputOptions,
prs: Option<paint::PreparedShapes>,
painter: paint::Painter,
last_key: Option<egui::Key>
}

impl RlEgui {
Expand All @@ -76,7 +76,6 @@ impl RlEgui {
inopt,
prs: None,
painter: Painter::default(),
last_key: None
}
}

Expand All @@ -100,7 +99,7 @@ impl RlEgui {
F: FnOnce(&egui::Context),
H: PlatformHandler,
{
let raw_input = gather_input(&self.inopt, &mut self.last_key, &self.ctx, rl);
let raw_input = gather_input(&self.inopt, &self.ctx, rl);
let output = paint::full_output(rl, raw_input, &self.ctx, run_ui, handler);
let prepared = self.painter.predraw(output, rl, rthread);
self.prs.replace(prepared);
Expand Down
Loading

0 comments on commit 2908fd6

Please sign in to comment.