egui/emgui/src/layout.rs

607 lines
18 KiB
Rust
Raw Normal View History

use std::{collections::HashSet, sync::Arc};
2018-12-27 18:08:43 +00:00
2019-01-05 14:28:07 +00:00
use crate::{font::Font, math::*, types::*};
2018-12-26 09:46:23 +00:00
2018-12-26 22:08:50 +00:00
// ----------------------------------------------------------------------------
2018-12-26 09:46:23 +00:00
2018-12-26 22:08:50 +00:00
#[derive(Clone, Copy, Debug, Serialize)]
pub struct LayoutOptions {
2018-12-28 22:29:24 +00:00
/// Horizontal and vertical padding within a window frame.
pub window_padding: Vec2,
/// Horizontal and vertical spacing between widgets
2018-12-26 22:08:50 +00:00
pub item_spacing: Vec2,
2018-12-27 18:08:43 +00:00
/// Indent foldable regions etc by this much.
pub indent: f32,
2018-12-27 22:26:05 +00:00
/// Button size is text size plus this on each side
pub button_padding: Vec2,
2018-12-26 22:08:50 +00:00
2018-12-27 22:55:16 +00:00
/// Checkboxed, radio button and foldables have an icon at the start.
/// The text starts after this many pixels.
pub start_icon_width: f32,
2018-12-26 22:08:50 +00:00
}
impl Default for LayoutOptions {
fn default() -> Self {
LayoutOptions {
2018-12-27 22:55:16 +00:00
item_spacing: vec2(8.0, 4.0),
2018-12-28 22:29:24 +00:00
window_padding: vec2(6.0, 6.0),
2018-12-27 18:08:43 +00:00
indent: 21.0,
2018-12-27 22:55:16 +00:00
button_padding: vec2(5.0, 3.0),
start_icon_width: 20.0,
2018-12-26 22:08:50 +00:00
}
}
}
// ----------------------------------------------------------------------------
2018-12-26 09:46:23 +00:00
2018-12-28 22:53:15 +00:00
// TODO: rename
pub struct GuiResponse<'a> {
/// The mouse is hovering above this
pub hovered: bool,
/// The mouse went got pressed on this thing this frame
pub clicked: bool,
/// The mouse is interacting with this thing (e.g. dragging it)
pub active: bool,
2019-01-06 15:34:01 +00:00
/// Used for showing a popup (if any)
data: &'a mut Data,
2018-12-28 22:53:15 +00:00
}
impl<'a> GuiResponse<'a> {
/// Show some stuff if the item was hovered
2019-01-06 15:34:01 +00:00
pub fn tooltip<F>(&mut self, add_contents: F) -> &mut Self
2018-12-28 22:53:15 +00:00
where
2019-01-06 15:34:01 +00:00
F: FnOnce(&mut Region),
2018-12-28 22:53:15 +00:00
{
if self.hovered {
2019-01-06 15:34:01 +00:00
let window_pos = self.data.input().mouse_pos + vec2(16.0, 16.0);
self.data.show_popup(window_pos, add_contents);
2018-12-28 22:53:15 +00:00
}
self
}
/// Show this text if the item was hovered
2019-01-06 15:34:01 +00:00
pub fn tooltip_text<S: Into<String>>(&mut self, text: S) -> &mut Self {
2018-12-28 22:53:15 +00:00
self.tooltip(|popup| {
popup.label(text);
})
}
}
// ----------------------------------------------------------------------------
2018-12-27 18:08:43 +00:00
#[derive(Clone, Debug, Default)]
2019-01-06 15:34:01 +00:00
pub struct Memory {
2018-12-26 14:28:38 +00:00
/// The widget being interacted with (e.g. dragged, in case of a slider).
2018-12-28 09:39:08 +00:00
active_id: Option<Id>,
2018-12-27 18:08:43 +00:00
/// Which foldable regions are open.
open_foldables: HashSet<Id>,
2018-12-26 09:46:23 +00:00
}
2018-12-26 22:08:50 +00:00
// ----------------------------------------------------------------------------
2018-12-27 22:26:05 +00:00
struct TextFragment {
2019-01-05 14:28:07 +00:00
/// The start of each character, starting at zero.
x_offsets: Vec<f32>,
/// 0 for the first line, n * line_spacing for the rest
y_offset: f32,
2018-12-27 22:26:05 +00:00
text: String,
}
type TextFragments = Vec<TextFragment>;
// ----------------------------------------------------------------------------
2018-12-28 09:39:08 +00:00
#[derive(Clone, Copy, Debug, PartialEq)]
2019-01-06 15:34:01 +00:00
pub enum Direction {
2018-12-28 09:39:08 +00:00
Horizontal,
Vertical,
}
impl Default for Direction {
fn default() -> Direction {
Direction::Vertical
}
}
// ----------------------------------------------------------------------------
2018-12-26 22:08:50 +00:00
type Id = u64;
2019-01-06 15:34:01 +00:00
// TODO: give a better name.
/// Contains the input, options and output of all GUI commands.
2019-01-05 14:28:07 +00:00
#[derive(Clone)]
2019-01-06 15:34:01 +00:00
pub struct Data {
pub(crate) options: LayoutOptions,
pub(crate) font: Arc<Font>,
2019-01-06 15:34:01 +00:00
pub(crate) input: GuiInput,
pub(crate) memory: Memory,
pub(crate) graphics: Vec<GuiCmd>,
pub(crate) hovering_graphics: Vec<GuiCmd>,
2018-12-26 14:28:38 +00:00
}
2018-12-26 09:46:23 +00:00
2019-01-06 15:34:01 +00:00
impl Data {
pub fn new(font: Arc<Font>) -> Data {
2019-01-06 15:34:01 +00:00
Data {
2019-01-05 14:28:07 +00:00
options: Default::default(),
2019-01-05 20:23:53 +00:00
font,
2019-01-05 14:28:07 +00:00
input: Default::default(),
memory: Default::default(),
graphics: Default::default(),
hovering_graphics: Default::default(),
}
}
2018-12-26 09:46:23 +00:00
pub fn input(&self) -> &GuiInput {
&self.input
}
2018-12-28 22:29:24 +00:00
pub fn options(&self) -> &LayoutOptions {
&self.options
}
pub fn set_options(&mut self, options: LayoutOptions) {
self.options = options;
2018-12-26 09:46:23 +00:00
}
2018-12-28 09:39:08 +00:00
// TODO: move
2018-12-28 22:29:24 +00:00
pub fn new_frame(&mut self, gui_input: GuiInput) {
2018-12-28 09:39:08 +00:00
self.input = gui_input;
if !gui_input.mouse_down {
self.memory.active_id = None;
}
}
pub fn drain_gui_commands(&mut self) -> impl ExactSizeIterator<Item = GuiCmd> {
// TODO: there must be a nicer way to do this?
let mut all_commands: Vec<_> = self.graphics.drain(..).collect();
all_commands.extend(self.hovering_graphics.drain(..));
all_commands.into_iter()
}
2019-01-06 15:34:01 +00:00
/// Show a pop-over window
pub fn show_popup<F>(&mut self, window_pos: Vec2, add_contents: F)
where
F: FnOnce(&mut Region),
{
// TODO: nicer way to do layering!
let num_graphics_before = self.graphics.len();
let window_padding = self.options.window_padding;
let mut popup_region = Region {
data: self,
id: Default::default(),
dir: Direction::Vertical,
cursor: window_pos + window_padding,
bounding_size: vec2(0.0, 0.0),
available_space: vec2(400.0, std::f32::INFINITY), // TODO
2019-01-06 15:34:01 +00:00
};
add_contents(&mut popup_region);
// TODO: handle the last item_spacing in a nicer way
let inner_size = popup_region.bounding_size - self.options.item_spacing;
2019-01-06 15:34:01 +00:00
let outer_size = inner_size + 2.0 * window_padding;
let rect = Rect::from_min_size(window_pos, outer_size);
let popup_graphics = self.graphics.split_off(num_graphics_before);
self.hovering_graphics.push(GuiCmd::Window { rect });
self.hovering_graphics.extend(popup_graphics);
}
}
// ----------------------------------------------------------------------------
/// Represents a region of the screen
/// with a type of layout (horizontal or vertical).
/// TODO: make Region a trait so we can have type-safe HorizontalRegion etc?
2019-01-06 15:34:01 +00:00
pub struct Region<'a> {
// TODO: Arc<StaticDat> + Arc<Mutex<MutableData>> for a lot less hassle.
2019-01-06 15:34:01 +00:00
pub(crate) data: &'a mut Data,
/// Unique ID of this region.
pub(crate) id: Id,
/// Doesn't change.
pub(crate) dir: Direction,
/// Changes only along self.dir
pub(crate) cursor: Vec2,
/// Bounding box children.
2019-01-06 15:34:01 +00:00
/// We keep track of our max-size along the orthogonal to self.dir
pub(crate) bounding_size: Vec2,
/// This how much space we can take up without overflowing our parent.
/// Shrinks as cursor increments.
pub(crate) available_space: Vec2,
2019-01-06 15:34:01 +00:00
}
impl<'a> Region<'a> {
/// It is up to the caller to make sure there is room for this.
/// Can be used for free painting.
/// NOTE: all coordinates are screen coordinates!
pub fn add_graphic(&mut self, gui_cmd: GuiCmd) {
self.data.graphics.push(gui_cmd)
}
pub fn options(&self) -> &LayoutOptions {
self.data.options()
}
// TODO: remove
2019-01-06 15:34:01 +00:00
pub fn set_options(&mut self, options: LayoutOptions) {
self.data.set_options(options)
}
pub fn input(&self) -> &GuiInput {
self.data.input()
}
2018-12-26 09:46:23 +00:00
pub fn cursor(&self) -> Vec2 {
self.cursor
}
2018-12-28 22:53:15 +00:00
pub fn button<S: Into<String>>(&mut self, text: S) -> GuiResponse {
2018-12-26 14:28:38 +00:00
let text: String = text.into();
let id = self.get_id(&text);
2018-12-27 22:26:05 +00:00
let (text, text_size) = self.layout_text(&text);
2019-01-06 15:34:01 +00:00
let text_cursor = self.cursor + self.options().button_padding;
2018-12-27 22:26:05 +00:00
let (rect, interact) =
2019-01-06 15:34:01 +00:00
self.reserve_space(text_size + 2.0 * self.options().button_padding, Some(id));
self.add_graphic(GuiCmd::Button { interact, rect });
2018-12-27 22:26:05 +00:00
self.add_text(text_cursor, text);
2018-12-28 22:53:15 +00:00
self.response(interact)
2018-12-26 21:17:33 +00:00
}
2018-12-28 22:53:15 +00:00
pub fn checkbox<S: Into<String>>(&mut self, text: S, checked: &mut bool) -> GuiResponse {
2018-12-27 22:55:16 +00:00
let text: String = text.into();
let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&text);
2019-01-06 15:34:01 +00:00
let text_cursor = self.cursor
+ self.options().button_padding
+ vec2(self.options().start_icon_width, 0.0);
2018-12-28 22:53:15 +00:00
let (rect, interact) = self.reserve_space(
2019-01-06 15:34:01 +00:00
self.options().button_padding
+ vec2(self.options().start_icon_width, 0.0)
2018-12-27 22:55:16 +00:00
+ text_size
2019-01-06 15:34:01 +00:00
+ self.options().button_padding,
2018-12-28 22:53:15 +00:00
Some(id),
2018-12-27 18:35:02 +00:00
);
2018-12-26 21:17:33 +00:00
if interact.clicked {
*checked = !*checked;
}
2019-01-06 15:34:01 +00:00
self.add_graphic(GuiCmd::Checkbox {
2018-12-26 21:17:33 +00:00
checked: *checked,
interact,
rect,
2018-12-26 14:28:38 +00:00
});
2018-12-27 22:55:16 +00:00
self.add_text(text_cursor, text);
2018-12-28 22:53:15 +00:00
self.response(interact)
2018-12-26 09:46:23 +00:00
}
2018-12-28 22:53:15 +00:00
pub fn label<S: Into<String>>(&mut self, text: S) -> GuiResponse {
2018-12-26 14:28:38 +00:00
let text: String = text.into();
2018-12-27 22:26:05 +00:00
let (text, text_size) = self.layout_text(&text);
2019-01-06 15:34:01 +00:00
self.add_text(self.cursor, text);
2018-12-28 22:53:15 +00:00
let (_, interact) = self.reserve_space(text_size, None);
self.response(interact)
2018-12-26 09:46:23 +00:00
}
2018-12-26 21:26:15 +00:00
/// A radio button
2018-12-28 22:53:15 +00:00
pub fn radio<S: Into<String>>(&mut self, text: S, checked: bool) -> GuiResponse {
2018-12-27 22:55:16 +00:00
let text: String = text.into();
let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&text);
2019-01-06 15:34:01 +00:00
let text_cursor = self.cursor
+ self.options().button_padding
+ vec2(self.options().start_icon_width, 0.0);
2018-12-28 22:53:15 +00:00
let (rect, interact) = self.reserve_space(
2019-01-06 15:34:01 +00:00
self.options().button_padding
+ vec2(self.options().start_icon_width, 0.0)
2018-12-27 22:55:16 +00:00
+ text_size
2019-01-06 15:34:01 +00:00
+ self.options().button_padding,
2018-12-28 22:53:15 +00:00
Some(id),
2018-12-27 18:35:02 +00:00
);
2019-01-06 15:34:01 +00:00
self.add_graphic(GuiCmd::RadioButton {
2018-12-26 21:26:15 +00:00
checked,
interact,
rect,
});
2018-12-27 22:55:16 +00:00
self.add_text(text_cursor, text);
2018-12-28 22:53:15 +00:00
self.response(interact)
2018-12-26 21:26:15 +00:00
}
2018-12-26 16:01:46 +00:00
pub fn slider_f32<S: Into<String>>(
&mut self,
2018-12-27 22:55:16 +00:00
text: S,
2018-12-26 16:01:46 +00:00
value: &mut f32,
min: f32,
max: f32,
2018-12-28 22:53:15 +00:00
) -> GuiResponse {
2018-12-27 18:35:02 +00:00
debug_assert!(min <= max);
2018-12-27 22:55:16 +00:00
let text: String = text.into();
let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&format!("{}: {:.3}", text, value));
2019-01-06 15:34:01 +00:00
self.add_text(self.cursor, text);
self.reserve_space_inner(text_size);
2018-12-28 22:53:15 +00:00
let (slider_rect, interact) = self.reserve_space(
2018-12-27 18:35:02 +00:00
Vec2 {
x: self.available_space.x,
2019-01-06 15:34:01 +00:00
y: self.data.font.line_spacing(),
2018-12-26 22:08:50 +00:00
},
2018-12-28 22:53:15 +00:00
Some(id),
2018-12-27 18:35:02 +00:00
);
2018-12-26 16:01:46 +00:00
if interact.active {
2018-12-27 22:55:16 +00:00
*value = remap_clamp(
2019-01-06 15:34:01 +00:00
self.input().mouse_pos.x,
2018-12-27 22:55:16 +00:00
slider_rect.min().x,
slider_rect.max().x,
min,
max,
);
2018-12-26 16:01:46 +00:00
}
2019-01-06 15:34:01 +00:00
self.add_graphic(GuiCmd::Slider {
2018-12-26 16:01:46 +00:00
interact,
max,
min,
2018-12-27 22:55:16 +00:00
rect: slider_rect,
2018-12-26 16:01:46 +00:00
value: *value,
});
2018-12-28 22:53:15 +00:00
self.response(interact)
2018-12-26 16:01:46 +00:00
}
2018-12-27 18:08:43 +00:00
// ------------------------------------------------------------------------
// Sub-regions:
2018-12-27 18:08:43 +00:00
2018-12-28 22:53:15 +00:00
pub fn foldable<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse
2018-12-27 18:08:43 +00:00
where
S: Into<String>,
2019-01-06 15:34:01 +00:00
F: FnOnce(&mut Region),
2018-12-27 18:08:43 +00:00
{
2018-12-28 09:39:08 +00:00
assert!(
2019-01-06 15:34:01 +00:00
self.dir == Direction::Vertical,
2018-12-28 09:39:08 +00:00
"Horizontal foldable is unimplemented"
);
2018-12-27 22:26:05 +00:00
let text: String = text.into();
let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&text);
2019-01-06 15:34:01 +00:00
let text_cursor = self.cursor + self.options().button_padding;
2018-12-28 22:53:15 +00:00
let (rect, interact) = self.reserve_space(
2018-12-27 22:26:05 +00:00
vec2(
self.available_space.x,
2019-01-06 15:34:01 +00:00
text_size.y + 2.0 * self.options().button_padding.y,
2018-12-27 22:26:05 +00:00
),
2018-12-28 22:53:15 +00:00
Some(id),
2018-12-27 18:35:02 +00:00
);
2018-12-27 18:08:43 +00:00
if interact.clicked {
2019-01-06 15:34:01 +00:00
if self.data.memory.open_foldables.contains(&id) {
self.data.memory.open_foldables.remove(&id);
2018-12-27 18:08:43 +00:00
} else {
2019-01-06 15:34:01 +00:00
self.data.memory.open_foldables.insert(id);
2018-12-27 18:08:43 +00:00
}
}
2019-01-06 15:34:01 +00:00
let open = self.data.memory.open_foldables.contains(&id);
2018-12-27 18:08:43 +00:00
2019-01-06 15:34:01 +00:00
self.add_graphic(GuiCmd::FoldableHeader {
2018-12-27 18:08:43 +00:00
interact,
rect,
open,
});
2019-01-06 15:34:01 +00:00
self.add_text(
text_cursor + vec2(self.options().start_icon_width, 0.0),
text,
);
2018-12-27 18:08:43 +00:00
if open {
2018-12-27 22:26:05 +00:00
let old_id = self.id;
self.id = id;
self.indent(add_contents);
2018-12-27 22:26:05 +00:00
self.id = old_id;
2018-12-27 18:08:43 +00:00
}
2018-12-28 22:53:15 +00:00
self.response(interact)
2018-12-27 18:08:43 +00:00
}
/// Create a child region which is indented to the right
pub fn indent<F>(&mut self, add_contents: F)
where
F: FnOnce(&mut Region),
{
let indent = vec2(self.options().indent, 0.0);
let mut child_region = Region {
data: self.data,
id: self.id,
dir: self.dir,
cursor: self.cursor + indent,
bounding_size: vec2(0.0, 0.0),
available_space: self.available_space - indent,
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
self.reserve_space_inner(indent + size);
}
/// A horizontally centered region of the given width.
pub fn centered_column(&mut self, width: f32) -> Region {
Region {
data: self.data,
id: self.id,
dir: self.dir,
cursor: vec2((self.available_space.x - width) / 2.0, self.cursor.y),
bounding_size: vec2(0.0, 0.0),
available_space: vec2(width, self.available_space.y),
}
}
2018-12-28 09:39:08 +00:00
/// Start a region with horizontal layout
pub fn horizontal<F>(&mut self, add_contents: F)
where
2019-01-06 15:34:01 +00:00
F: FnOnce(&mut Region),
2018-12-28 22:53:15 +00:00
{
let mut child_region = Region {
2019-01-06 15:34:01 +00:00
data: self.data,
2018-12-28 22:29:24 +00:00
id: self.id,
2019-01-06 15:34:01 +00:00
dir: Direction::Horizontal,
cursor: self.cursor,
bounding_size: vec2(0.0, 0.0),
available_space: self.available_space,
2018-12-28 22:29:24 +00:00
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
2019-01-06 15:34:01 +00:00
self.reserve_space_inner(size);
2018-12-28 22:29:24 +00:00
}
// TODO: we need to rethink this a lot. Passing closures have problems with borrow checker.
// Much better with temporary regions that register the final size in Drop ?
// pub fn columns_2<F0, F1>(&mut self, col0: F0, col1: F1)
// where
// F0: FnOnce(&mut Region),
// F1: FnOnce(&mut Region),
// {
// let mut max_height = 0.0;
// let num_columns = 2;
// // TODO: ensure there is space
// let padding = self.options().item_spacing.x;
// let total_padding = padding * (num_columns as f32 - 1.0);
// let column_width = (self.available_space.x - total_padding) / (num_columns as f32);
// let col_idx = 0;
// let mut child_region = Region {
// data: self.data,
// id: self.id,
// dir: Direction::Vertical,
// cursor: self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0),
// bounding_size: vec2(0.0, 0.0),
// available_space: vec2(column_width, self.available_space.y),
// };
// col0(&mut child_region);
// let size = child_region.bounding_size;
// max_height = size.y.max(max_height);
// let col_idx = 1;
// let mut child_region = Region {
// data: self.data,
// id: self.id,
// dir: Direction::Vertical,
// cursor: self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0),
// bounding_size: vec2(0.0, 0.0),
// available_space: vec2(column_width, self.available_space.y),
// };
// col1(&mut child_region);
// let size = child_region.bounding_size;
// max_height = size.y.max(max_height);
// self.reserve_space_inner(vec2(self.available_space.x, max_height));
// }
2018-12-26 09:46:23 +00:00
// ------------------------------------------------------------------------
2018-12-26 14:28:38 +00:00
pub fn reserve_space(
&mut self,
size: Vec2,
interaction_id: Option<Id>,
) -> (Rect, InteractInfo) {
2018-12-27 18:35:02 +00:00
let rect = Rect {
2019-01-06 15:34:01 +00:00
pos: self.cursor,
2018-12-27 18:35:02 +00:00
size,
};
2019-01-06 15:34:01 +00:00
self.reserve_space_inner(size + self.options().item_spacing);
let hovered = rect.contains(self.input().mouse_pos);
let clicked = hovered && self.input().mouse_clicked;
2018-12-28 22:53:15 +00:00
let active = if interaction_id.is_some() {
if clicked {
2019-01-06 15:34:01 +00:00
self.data.memory.active_id = interaction_id;
2018-12-28 22:53:15 +00:00
}
2019-01-06 15:34:01 +00:00
self.data.memory.active_id == interaction_id
2018-12-28 22:53:15 +00:00
} else {
false
};
2018-12-26 16:01:46 +00:00
2018-12-28 09:39:08 +00:00
let interact = InteractInfo {
2018-12-26 16:01:46 +00:00
hovered,
clicked,
active,
2018-12-28 09:39:08 +00:00
};
(rect, interact)
2018-12-26 16:01:46 +00:00
}
2019-01-06 15:34:01 +00:00
/// Reserve this much space and move the cursor.
fn reserve_space_inner(&mut self, size: Vec2) {
if self.dir == Direction::Horizontal {
self.cursor.x += size.x;
self.available_space.x -= size.x;
self.bounding_size.x += size.x;
self.bounding_size.y = self.bounding_size.y.max(size.y);
2019-01-06 15:34:01 +00:00
} else {
self.cursor.y += size.y;
self.available_space.y -= size.x;
self.bounding_size.y += size.y;
self.bounding_size.x = self.bounding_size.x.max(size.x);
2019-01-06 15:34:01 +00:00
}
}
2018-12-26 14:28:38 +00:00
fn get_id(&self, id_str: &str) -> Id {
use std::hash::Hasher;
let mut hasher = std::collections::hash_map::DefaultHasher::new();
2018-12-27 22:26:05 +00:00
hasher.write_u64(self.id);
2018-12-26 14:28:38 +00:00
hasher.write(id_str.as_bytes());
hasher.finish()
}
2019-01-06 15:34:01 +00:00
// TODO: move this function
2018-12-27 22:26:05 +00:00
fn layout_text(&self, text: &str) -> (TextFragments, Vec2) {
2019-01-06 15:34:01 +00:00
let line_spacing = self.data.font.line_spacing();
2018-12-27 22:26:05 +00:00
let mut cursor_y = 0.0;
let mut max_width = 0.0;
let mut text_fragments = Vec::new();
for line in text.split('\n') {
2019-01-06 15:34:01 +00:00
let x_offsets = self.data.font.layout_single_line(&line);
2019-01-05 14:28:07 +00:00
let line_width = *x_offsets.last().unwrap();
2018-12-27 22:26:05 +00:00
text_fragments.push(TextFragment {
2019-01-05 14:28:07 +00:00
x_offsets,
y_offset: cursor_y,
2018-12-27 22:26:05 +00:00
text: line.into(),
});
2019-01-05 20:23:53 +00:00
cursor_y += line_spacing;
2018-12-27 22:26:05 +00:00
max_width = line_width.max(max_width);
}
let bounding_size = vec2(max_width, cursor_y);
(text_fragments, bounding_size)
}
fn add_text(&mut self, pos: Vec2, text: Vec<TextFragment>) {
for fragment in text {
2019-01-06 15:34:01 +00:00
self.add_graphic(GuiCmd::Text {
2019-01-05 14:28:07 +00:00
pos: pos + vec2(0.0, fragment.y_offset),
2018-12-27 22:26:05 +00:00
style: TextStyle::Label,
text: fragment.text,
2019-01-05 14:28:07 +00:00
x_offsets: fragment.x_offsets,
2018-12-27 22:26:05 +00:00
});
}
2018-12-26 14:28:38 +00:00
}
2018-12-28 22:53:15 +00:00
fn response(&mut self, interact: InteractInfo) -> GuiResponse {
GuiResponse {
hovered: interact.hovered,
clicked: interact.clicked,
active: interact.active,
2019-01-06 15:34:01 +00:00
data: self.data,
2018-12-28 22:53:15 +00:00
}
}
2018-12-26 09:46:23 +00:00
}