Listen to scroll wheel (glium only)

This commit is contained in:
Emil Ernerfeldt 2020-04-22 20:01:49 +02:00
parent 9b404159c5
commit a8d9c3fc42
6 changed files with 77 additions and 38 deletions

View file

@ -79,6 +79,7 @@
return {
mouse_down: g_mouse_down,
mouse_pos: g_mouse_pos,
// TODO: scroll_delta
screen_size: { x: window.innerWidth, y: window.innerHeight },
pixels_per_point: pixels_per_point(),
time: window.performance.now() / 1000.0,

View file

@ -16,7 +16,8 @@ This is the core library crate Emigui. It is fully platform independent without
* [ ] Scroll areas
* [x] Vertical scrolling
* [ ] Horizontal scrolling
* [ ] Scroll-wheel input
* [x] Scroll-wheel input
* [ ] Scroll-wheel input in web verions
* [x] Drag background to scroll
* [ ] Kinetic scrolling
* [ ] Menu bar (File, Edit, etc)

View file

@ -125,19 +125,21 @@ impl Context {
}
}
pub fn interact(&self, layer: Layer, rect: Rect, interaction_id: Option<Id>) -> InteractInfo {
let mut memory = self.memory.lock();
let input = &self.input;
let hovered = if let Some(mouse_pos) = input.mouse_pos {
rect.contains(mouse_pos) && layer == memory.layer_at(mouse_pos)
pub fn contains_mouse_pos(&self, layer: Layer, rect: &Rect) -> bool {
if let Some(mouse_pos) = self.input.mouse_pos {
rect.contains(mouse_pos) && layer == self.memory.lock().layer_at(mouse_pos)
} else {
false
};
}
}
pub fn interact(&self, layer: Layer, rect: Rect, interaction_id: Option<Id>) -> InteractInfo {
let hovered = self.contains_mouse_pos(layer, &rect);
let mut memory = self.memory.lock();
let active = interaction_id.is_some() && memory.active_id == interaction_id;
if input.mouse_pressed {
if self.input.mouse_pressed {
if hovered && interaction_id.is_some() {
if memory.active_id.is_some() {
// Already clicked something else this frame
@ -164,14 +166,14 @@ impl Context {
active: false,
}
}
} else if input.mouse_released {
} else if self.input.mouse_released {
InteractInfo {
rect,
hovered,
clicked: hovered && active,
active,
}
} else if input.mouse_down {
} else if self.input.mouse_down {
InteractInfo {
rect,
hovered: hovered && active,

View file

@ -68,6 +68,13 @@ impl ScrollArea {
state.offset.y -= ctx.input.mouse_move.y;
}
// TODO: check that nothing else is being inteacted with
if ctx.contains_mouse_pos(outer_region.layer, &outer_rect)
&& ctx.memory.lock().active_id.is_none()
{
state.offset.y -= ctx.input.scroll_delta.y;
}
let show_scroll = content_size.y > inner_size.y;
if show_scroll {
let corner_radius = scroll_bar_width / 2.0;

View file

@ -8,7 +8,9 @@ use crate::{
// ----------------------------------------------------------------------------
/// What the integration gives to the gui.
/// All coordinates in emigui is in point/logical coordinates.
#[derive(Clone, Copy, Debug, Default, Deserialize)]
#[serde(default)]
pub struct RawInput {
/// Is the button currently down?
pub mouse_down: bool,
@ -16,6 +18,9 @@ pub struct RawInput {
/// Current position of the mouse in points.
pub mouse_pos: Option<Pos2>,
/// How many pixels the user scrolled
pub scroll_delta: Vec2,
/// Size of the screen in points.
pub screen_size: Vec2,
@ -46,6 +51,9 @@ pub struct GuiInput {
/// How much the mouse moved compared to last frame, in points.
pub mouse_move: Vec2,
/// How many pixels the user scrolled
pub scroll_delta: Vec2,
/// Size of the screen in points.
pub screen_size: Vec2,
@ -68,6 +76,7 @@ impl GuiInput {
mouse_released: last.mouse_down && !new.mouse_down,
mouse_pos: new.mouse_pos,
mouse_move,
scroll_delta: new.scroll_delta,
screen_size: new.screen_size,
pixels_per_point: new.pixels_per_point,
time: new.time,

View file

@ -20,6 +20,14 @@ fn main() {
let context = glutin::ContextBuilder::new();
let display = glium::Display::new(window, context, &events_loop).unwrap();
display
.gl_window()
.set_inner_size(glutin::dpi::LogicalSize {
width: 1200.0,
height: 800.0,
});
display.gl_window().set_position((16, 32).into()); // Useful when debugging and constantly restarting it
let pixels_per_point = display.gl_window().get_hidpi_factor() as f32;
let mut emigui = Emigui::new(pixels_per_point);
@ -54,9 +62,9 @@ fn main() {
}
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
raw_input.scroll_delta = vec2(0.0, 0.0);
events_loop.poll_events(|event| {
match event {
events_loop.poll_events(|event| match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => quit = true,
@ -76,12 +84,22 @@ fn main() {
quit = true;
}
}
glutin::WindowEvent::MouseWheel { delta, .. } => {
match delta {
glutin::MouseScrollDelta::LineDelta(x, y) => {
raw_input.scroll_delta = vec2(x, y) * 24.0;
}
glutin::MouseScrollDelta::PixelDelta(delta) => {
// Actually point delta
raw_input.scroll_delta = vec2(delta.x as f32, delta.y as f32);
}
}
}
_ => {
// dbg!(event);
}
},
_ => (),
}
});
emigui.new_frame(raw_input);
@ -93,14 +111,15 @@ fn main() {
quit = true;
}
Window::new("Example APP")
.default_pos(pos2(200.0, 100.0))
Window::new("Examples")
.default_pos(pos2(100.0, 100.0))
.default_size(vec2(300.0, 400.0))
.show(region.ctx(), |region| {
example_app.ui(region);
});
Window::new("Emigui settings")
.default_pos(pos2(200.0, 500.0))
.default_pos(pos2(100.0, 550.0))
.show(region.ctx(), |region| {
emigui.ui(region);
});
@ -113,7 +132,7 @@ fn main() {
region.add_label("This window can be reisized, but not smaller than the contents.");
});
Window::new("Resize me!")
.default_pos(pos2(600.0, 500.0))
.default_pos(pos2(600.0, 550.0))
.expand_to_fit_content(false)
.show(region.ctx(), |region| {
region