Basic scroll area support

Just vertical scrolling at the moment
This commit is contained in:
Emil Ernerfeldt 2020-04-21 20:52:17 +02:00
parent 4efbb94e1b
commit dbf8520e63
5 changed files with 183 additions and 10 deletions

View file

@ -1,4 +1,4 @@
use crate::{color::*, label, math::*, widgets::*, Align, Outline, PaintCmd, Region}; use crate::{color::*, label, math::*, widgets::*, Align, Outline, PaintCmd, Region, ScrollArea};
/// Showcase some region code /// Showcase some region code
pub struct ExampleApp { pub struct ExampleApp {
@ -78,6 +78,13 @@ impl ExampleApp {
self.count self.count
)); ));
}); });
let value = &mut self.slider_value;
region.add(Slider::usize(value, 1, 1000));
if region.add(Button::new("Double it")).clicked {
*value *= 2;
}
region.add(label!("Value: {}", value));
}); });
region.foldable("Layouts", |region| { region.foldable("Layouts", |region| {
@ -124,8 +131,10 @@ impl ExampleApp {
region.add_paint_cmds(cmds); region.add_paint_cmds(cmds);
}); });
region.foldable("Slider example", |region| { region.foldable("Scroll area", |region| {
value_ui(&mut self.slider_value, region); ScrollArea::default().show(region, |region| {
region.add_label(LOREM_IPSUM);
});
}); });
region.foldable("Name clash example", |region| { region.foldable("Name clash example", |region| {
@ -154,10 +163,6 @@ impl ExampleApp {
} }
} }
pub fn value_ui(value: &mut usize, region: &mut Region) { const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
region.add(Slider::usize(value, 1, 1000));
if region.add(Button::new("Double it")).clicked { Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac habitasse platea dictumst.";
*value *= 2;
}
region.add(label!("Value: {}", value));
}

View file

@ -19,6 +19,7 @@ pub mod math;
mod memory; mod memory;
pub mod mesher; pub mod mesher;
mod region; mod region;
mod scroll_area;
mod style; mod style;
mod texture_atlas; mod texture_atlas;
mod types; mod types;
@ -37,6 +38,7 @@ pub use {
memory::Memory, memory::Memory,
mesher::{Mesh, PaintBatches, Vertex}, mesher::{Mesh, PaintBatches, Vertex},
region::Region, region::Region,
scroll_area::ScrollArea,
style::Style, style::Style,
texture_atlas::Texture, texture_atlas::Texture,
types::*, types::*,

View file

@ -17,6 +17,11 @@ impl Default for FoldableState {
} }
} }
} }
#[derive(Clone, Copy, Debug, Default)]
pub struct ScrollState {
/// Positive offset means scrolling down/right
pub offset: Vec2,
}
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Memory { pub struct Memory {
@ -25,6 +30,7 @@ pub struct Memory {
// states of various types of widgets // states of various types of widgets
pub(crate) foldables: HashMap<Id, FoldableState>, pub(crate) foldables: HashMap<Id, FoldableState>,
pub(crate) scroll_areas: HashMap<Id, ScrollState>,
windows: HashMap<Id, WindowState>, windows: HashMap<Id, WindowState>,
/// Top is last /// Top is last

View file

@ -359,6 +359,24 @@ impl Region {
self.reserve_space_without_padding(size); self.reserve_space_without_padding(size);
} }
/// Create a child region from current cursor with the given dimensions.
/// Does NOT move the cursor.
pub fn create_child_region(&self, size: Vec2) -> Self {
let child_rect = Rect::from_min_size(self.cursor, size);
Region {
ctx: self.ctx.clone(),
layer: self.layer,
style: self.style,
id: self.id,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
cursor: self.cursor,
bounding_size: vec2(0.0, 0.0),
dir: self.dir,
align: self.align,
}
}
/// Start a region with horizontal layout /// Start a region with horizontal layout
pub fn horizontal<F>(&mut self, align: Align, add_contents: F) pub fn horizontal<F>(&mut self, align: Align, add_contents: F)
where where

142
emigui/src/scroll_area.rs Normal file
View file

@ -0,0 +1,142 @@
use crate::*;
pub struct ScrollArea {
max_height: f32,
}
impl Default for ScrollArea {
fn default() -> Self {
Self { max_height: 200.0 }
}
}
impl ScrollArea {
pub fn max_height(mut self, max_height: f32) -> Self {
self.max_height = max_height;
self
}
}
impl ScrollArea {
pub fn show<F>(self, outer_region: &mut Region, add_contents: F)
where
F: FnOnce(&mut Region),
{
let ctx = outer_region.ctx().clone();
let scroll_area_id = outer_region.id.with("scroll_area");
let mut state = ctx
.memory
.lock()
.scroll_areas
.get(&scroll_area_id)
.cloned()
.unwrap_or_default();
// content: size of contents (generally large)
// outer: size of scroll area including scroll bars
// inner: excluding scroll bars
let scroll_bar_width = 16.0;
let outer_size = vec2(outer_region.available_width(), self.max_height);
let outer_rect = Rect::from_min_size(outer_region.cursor, outer_size);
let inner_size = outer_size - vec2(scroll_bar_width, 0.0);
let inner_rect = Rect::from_min_size(outer_region.cursor, inner_size);
let mut content_region = outer_region.create_child_region(inner_size);
content_region.cursor -= state.offset;
content_region.desired_rect = content_region.desired_rect.translate(-state.offset);
add_contents(&mut content_region);
let content_size = content_region.bounding_size;
let show_scroll = content_size.y > inner_size.y;
if show_scroll {
let corner_radius = scroll_bar_width / 2.0;
let left = inner_rect.max().x;
let right = outer_rect.max().x;
let outer_scroll_rect = Rect::from_min_max(
pos2(left, inner_rect.min().y),
pos2(right, inner_rect.max().y),
);
let scroll_handle_min_y = remap_clamp(
state.offset.y,
0.0,
content_size.y,
inner_rect.min().y,
inner_rect.max().y,
);
let scroll_handle_max_y = remap_clamp(
state.offset.y + inner_rect.height(),
0.0,
content_size.y,
inner_rect.min().y,
inner_rect.max().y,
);
let scroll_handle_rect = Rect::from_min_max(
pos2(left, scroll_handle_min_y),
pos2(right, scroll_handle_max_y),
);
let scroll_handle_id = scroll_area_id.with("v_scroll_handle");
let handle_interact = ctx.interact(
outer_region.layer,
scroll_handle_rect,
Some(scroll_handle_id),
);
if handle_interact.active {
// state.offset.y = remap_clamp(
// ctx.input.mouse_pos.y,
// inner_rect.min().y,
// inner_rect.max().y,
// 0.0,
// content_size.y,
// );
if let Some(mouse_pos) = ctx.input.mouse_pos {
if inner_rect.min().y <= mouse_pos.y && mouse_pos.y <= inner_rect.max().y {
state.offset.y +=
ctx.input.mouse_move.y * content_size.y / inner_rect.height();
state.offset.y = state.offset.y.max(0.0);
state.offset.y = state.offset.y.min(content_size.y - inner_rect.height());
}
}
}
let style = outer_region.style();
// let background_fill_color = style.background_fill_color();
let handle_fill_color = style.interact_fill_color(&handle_interact);
let handle_outline = style.interact_outline(&handle_interact);
outer_region.add_paint_cmd(PaintCmd::Rect {
rect: outer_scroll_rect,
corner_radius,
fill_color: Some(color::BLACK),
outline: None,
});
outer_region.add_paint_cmd(PaintCmd::Rect {
rect: scroll_handle_rect,
corner_radius,
fill_color: handle_fill_color,
outline: handle_outline,
});
}
let size = content_size.min(content_region.clip_rect.size());
outer_region.reserve_space_without_padding(size);
outer_region
.ctx()
.memory
.lock()
.scroll_areas
.insert(scroll_area_id, state);
}
}