1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 12:50:04 -04:00

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

@@ -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,