2020-05-10 17:04:10 +00:00
use std ::sync ::Arc ;
2020-07-23 16:54:16 +00:00
use crate ::{ app , color ::* , containers ::* , demos ::FractalClock , paint ::* , widgets ::* , * } ;
2018-12-26 09:46:23 +00:00
2020-05-10 17:04:10 +00:00
// ----------------------------------------------------------------------------
2020-05-30 09:04:40 +00:00
#[ derive(Default) ]
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
#[ cfg_attr(feature = " serde " , serde(default)) ]
2020-07-23 10:01:48 +00:00
pub struct DemoApp {
2020-05-21 10:04:14 +00:00
previous_web_location_hash : String ,
2020-05-10 17:04:10 +00:00
open_windows : OpenWindows ,
2020-07-23 10:01:48 +00:00
demo_window : DemoWindow ,
2020-05-11 18:21:24 +00:00
fractal_clock : FractalClock ,
2020-07-23 16:54:16 +00:00
num_frames_painted : u64 ,
2020-05-10 17:04:10 +00:00
}
2020-07-23 10:01:48 +00:00
impl DemoApp {
2020-05-21 10:04:14 +00:00
/// `web_location_hash`: for web demo only. e.g. "#fragmet".
pub fn ui ( & mut self , ui : & mut Ui , web_location_hash : & str ) {
if self . previous_web_location_hash ! = web_location_hash {
// #fragment end of URL:
if web_location_hash = = " #clock " {
self . open_windows = OpenWindows {
fractal_clock : true ,
.. OpenWindows ::none ( )
} ;
}
self . previous_web_location_hash = web_location_hash . to_owned ( ) ;
}
2020-05-12 05:27:14 +00:00
show_menu_bar ( ui , & mut self . open_windows ) ;
self . windows ( ui . ctx ( ) ) ;
}
pub fn windows ( & mut self , ctx : & Arc < Context > ) {
2020-07-23 10:01:48 +00:00
let DemoApp {
2020-05-11 18:21:24 +00:00
open_windows ,
2020-07-23 10:01:48 +00:00
demo_window ,
2020-05-11 18:21:24 +00:00
fractal_clock ,
2020-05-21 10:04:14 +00:00
..
2020-05-11 18:21:24 +00:00
} = self ;
2020-07-23 10:01:48 +00:00
Window ::new ( " Demo " )
. open ( & mut open_windows . demo )
2020-05-10 17:04:10 +00:00
. show ( ctx , | ui | {
2020-07-23 10:01:48 +00:00
demo_window . ui ( ui ) ;
2020-05-10 17:04:10 +00:00
} ) ;
Window ::new ( " Settings " )
2020-05-11 18:21:24 +00:00
. open ( & mut open_windows . settings )
2020-05-10 17:04:10 +00:00
. show ( ctx , | ui | {
ctx . settings_ui ( ui ) ;
} ) ;
Window ::new ( " Inspection " )
2020-05-11 18:21:24 +00:00
. open ( & mut open_windows . inspection )
2020-05-10 17:04:10 +00:00
. show ( ctx , | ui | {
ctx . inspection_ui ( ui ) ;
} ) ;
Window ::new ( " Memory " )
2020-05-11 18:21:24 +00:00
. open ( & mut open_windows . memory )
2020-05-30 15:51:01 +00:00
. resizable ( false )
2020-05-10 17:04:10 +00:00
. show ( ctx , | ui | {
ctx . memory_ui ( ui ) ;
} ) ;
2020-05-11 18:21:24 +00:00
fractal_clock . window ( ctx , & mut open_windows . fractal_clock ) ;
2020-05-10 17:04:10 +00:00
}
2020-07-23 16:54:16 +00:00
fn backend_ui ( & mut self , ui : & mut Ui , backend : & mut dyn app ::Backend ) {
let is_web = backend . web_info ( ) . is_some ( ) ;
if is_web {
ui . label ( " Egui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL. " ) ;
ui . label (
" Everything you see is rendered as textured triangles. There is no DOM. There are no HTML elements. "
) ;
ui . label ( " This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech. " ) ;
ui . label ( " This is also work in progress, and not ready for production... yet :) " ) ;
ui . horizontal ( | ui | {
ui . label ( " Project home page: " ) ;
2020-08-10 17:38:46 +00:00
ui . hyperlink ( " https://github.com/emilk/egui " ) ;
2020-07-23 16:54:16 +00:00
} ) ;
} else {
ui . add ( label! ( " Egui " ) . text_style ( TextStyle ::Heading ) ) ;
if ui . add ( Button ::new ( " Quit " ) ) . clicked {
backend . quit ( ) ;
return ;
}
}
ui . separator ( ) ;
ui . add (
label! (
" CPU usage: {:.2} ms / frame (excludes painting) " ,
1e3 * backend . cpu_time ( )
)
. text_style ( TextStyle ::Monospace ) ,
) ;
ui . separator ( ) ;
ui . horizontal ( | ui | {
let mut run_mode = backend . run_mode ( ) ;
ui . label ( " Run mode: " ) ;
ui . radio_value ( " Continuous " , & mut run_mode , app ::RunMode ::Continuous )
. tooltip_text ( " Repaint everything each frame " ) ;
ui . radio_value ( " Reactive " , & mut run_mode , app ::RunMode ::Reactive )
. tooltip_text ( " Repaint when there are animations or input (e.g. mouse movement) " ) ;
backend . set_run_mode ( run_mode ) ;
} ) ;
if backend . run_mode ( ) = = app ::RunMode ::Continuous {
ui . add (
label! ( " Repainting the UI each frame. FPS: {:.1} " , backend . fps ( ) )
. text_style ( TextStyle ::Monospace ) ,
) ;
} else {
ui . label ( " Only running UI code when there are animations or input " ) ;
}
self . num_frames_painted + = 1 ;
ui . label ( format! ( " Total frames painted: {} " , self . num_frames_painted ) ) ;
}
}
impl app ::App for DemoApp {
fn ui ( & mut self , ui : & mut Ui , backend : & mut dyn app ::Backend ) {
Window ::new ( " Backend " )
. default_width ( 500.0 )
. show ( ui . ctx ( ) , | ui | {
self . backend_ui ( ui , backend ) ;
} ) ;
let web_info = backend . web_info ( ) ;
let web_location_hash = web_info
. as_ref ( )
. map ( | info | info . web_location_hash . as_str ( ) )
. unwrap_or_default ( ) ;
self . ui ( ui , web_location_hash ) ;
}
2020-08-09 15:34:26 +00:00
#[ cfg(feature = " serde_json " ) ]
2020-07-23 16:54:16 +00:00
fn on_exit ( & mut self , storage : & mut dyn app ::Storage ) {
app ::set_value ( storage , app ::APP_KEY , self ) ;
}
2020-05-10 17:04:10 +00:00
}
2020-05-12 21:00:20 +00:00
// ----------------------------------------------------------------------------
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
2020-05-10 17:04:10 +00:00
struct OpenWindows {
2020-07-23 10:01:48 +00:00
demo : bool ,
2020-05-19 21:59:37 +00:00
fractal_clock : bool ,
2020-05-30 08:22:35 +00:00
// egui stuff:
2020-05-10 17:04:10 +00:00
settings : bool ,
inspection : bool ,
memory : bool ,
}
impl Default for OpenWindows {
fn default ( ) -> Self {
Self {
2020-07-23 10:01:48 +00:00
demo : true ,
2020-05-12 15:09:54 +00:00
.. OpenWindows ::none ( )
}
}
}
impl OpenWindows {
fn none ( ) -> Self {
Self {
2020-07-23 10:01:48 +00:00
demo : false ,
2020-05-19 21:59:37 +00:00
fractal_clock : false ,
2020-05-10 17:04:10 +00:00
settings : false ,
2020-05-12 05:27:14 +00:00
inspection : false ,
2020-05-10 17:04:10 +00:00
memory : false ,
}
}
}
fn show_menu_bar ( ui : & mut Ui , windows : & mut OpenWindows ) {
menu ::bar ( ui , | ui | {
menu ::menu ( ui , " File " , | ui | {
2020-05-16 15:28:15 +00:00
if ui . add ( Button ::new ( " Clear memory " ) ) . clicked {
* ui . ctx ( ) . memory ( ) = Default ::default ( ) ;
}
2020-05-10 17:04:10 +00:00
} ) ;
menu ::menu ( ui , " Windows " , | ui | {
2020-07-23 10:01:48 +00:00
ui . add ( Checkbox ::new ( & mut windows . demo , " Demo " ) ) ;
2020-05-19 21:59:37 +00:00
ui . add ( Checkbox ::new ( & mut windows . fractal_clock , " Fractal Clock " ) ) ;
ui . add ( Separator ::new ( ) ) ;
2020-05-10 17:04:10 +00:00
ui . add ( Checkbox ::new ( & mut windows . settings , " Settings " ) ) ;
ui . add ( Checkbox ::new ( & mut windows . inspection , " Inspection " ) ) ;
ui . add ( Checkbox ::new ( & mut windows . memory , " Memory " ) ) ;
} ) ;
menu ::menu ( ui , " About " , | ui | {
2020-05-30 08:22:35 +00:00
ui . add ( label! ( " This is Egui " ) ) ;
2020-08-10 17:38:46 +00:00
ui . add ( Hyperlink ::new ( " https://github.com/emilk/egui " ) . text ( " Egui home page " ) ) ;
2020-05-10 17:04:10 +00:00
} ) ;
2020-05-13 20:56:37 +00:00
if let Some ( time ) = ui . input ( ) . seconds_since_midnight {
let time = format! (
" {:02}:{:02}:{:02}.{:02} " ,
( time . rem_euclid ( 24.0 * 60.0 * 60.0 ) / 3600.0 ) . floor ( ) ,
( time . rem_euclid ( 60.0 * 60.0 ) / 60.0 ) . floor ( ) ,
( time . rem_euclid ( 60.0 ) ) . floor ( ) ,
( time . rem_euclid ( 1.0 ) * 100.0 ) . floor ( )
) ;
ui . inner_layout ( Layout ::horizontal ( Align ::Max ) . reverse ( ) , | ui | {
if ui
. add ( Button ::new ( time ) . text_style ( TextStyle ::Monospace ) )
. clicked
{
windows . fractal_clock = ! windows . fractal_clock ;
}
} ) ;
}
2020-05-10 17:04:10 +00:00
} ) ;
}
// ----------------------------------------------------------------------------
2020-05-08 20:42:31 +00:00
/// Showcase some ui code
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
2020-07-23 10:01:48 +00:00
pub struct DemoWindow {
2019-03-11 12:32:44 +00:00
num_columns : usize ,
2019-03-11 12:32:54 +00:00
2020-05-12 21:00:20 +00:00
widgets : Widgets ,
2020-07-23 10:01:48 +00:00
layout : LayoutDemo ,
2020-05-19 21:59:37 +00:00
tree : Tree ,
2020-05-12 21:00:20 +00:00
box_painting : BoxPainting ,
2020-04-24 16:32:27 +00:00
painting : Painting ,
2018-12-26 09:46:23 +00:00
}
2020-07-23 10:01:48 +00:00
impl Default for DemoWindow {
fn default ( ) -> DemoWindow {
DemoWindow {
2019-03-11 12:32:44 +00:00
num_columns : 2 ,
2019-03-11 12:32:54 +00:00
2020-05-12 21:00:20 +00:00
widgets : Default ::default ( ) ,
layout : Default ::default ( ) ,
2020-07-23 10:01:48 +00:00
tree : Tree ::demo ( ) ,
2020-05-12 21:00:20 +00:00
box_painting : Default ::default ( ) ,
2020-04-24 16:32:27 +00:00
painting : Default ::default ( ) ,
2018-12-26 16:32:58 +00:00
}
}
2018-12-26 21:17:33 +00:00
}
2018-12-26 16:32:58 +00:00
2020-07-23 10:01:48 +00:00
impl DemoWindow {
2020-05-08 20:42:31 +00:00
pub fn ui ( & mut self , ui : & mut Ui ) {
2020-05-30 08:22:35 +00:00
ui . collapsing ( " About Egui " , | ui | {
2020-05-08 20:42:31 +00:00
ui . add ( label! (
2020-05-30 08:22:35 +00:00
" Egui is an experimental immediate mode GUI written in Rust. "
2020-04-21 18:48:31 +00:00
) ) ;
2020-04-23 17:15:17 +00:00
2020-05-08 20:42:31 +00:00
ui . horizontal ( | ui | {
2020-05-30 07:51:57 +00:00
ui . label ( " Project home page: " ) ;
2020-08-10 17:38:46 +00:00
ui . hyperlink ( " https://github.com/emilk/egui " ) ;
2020-04-23 17:15:17 +00:00
} ) ;
2020-04-12 10:07:51 +00:00
} ) ;
2020-04-23 07:50:03 +00:00
CollapsingHeader ::new ( " Widgets " )
2020-05-19 21:57:48 +00:00
. default_open ( true )
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | {
2020-05-12 21:00:20 +00:00
self . widgets . ui ( ui ) ;
2019-01-21 07:48:32 +00:00
} ) ;
2020-04-21 18:52:17 +00:00
2020-05-12 21:00:20 +00:00
CollapsingHeader ::new ( " Layout " )
2020-05-19 21:57:48 +00:00
. default_open ( false )
2020-05-12 21:00:20 +00:00
. show ( ui , | ui | self . layout . ui ( ui ) ) ;
2018-12-26 21:26:15 +00:00
2020-05-19 21:57:48 +00:00
CollapsingHeader ::new ( " Tree " )
. default_open ( true )
. show ( ui , | ui | self . tree . ui ( ui ) ) ;
2020-05-12 21:00:20 +00:00
ui . collapsing ( " Columns " , | ui | {
2020-05-08 20:42:31 +00:00
ui . add ( Slider ::usize ( & mut self . num_columns , 1 ..= 10 ) . text ( " Columns " ) ) ;
ui . columns ( self . num_columns , | cols | {
2019-03-11 12:32:44 +00:00
for ( i , col ) in cols . iter_mut ( ) . enumerate ( ) {
col . add ( label! ( " Column {} out of {} " , i + 1 , self . num_columns ) ) ;
2020-04-24 16:47:14 +00:00
if i + 1 = = self . num_columns & & col . add ( Button ::new ( " Delete this " ) ) . clicked {
self . num_columns - = 1 ;
2019-03-11 12:32:44 +00:00
}
}
} ) ;
} ) ;
2020-05-12 21:00:20 +00:00
ui . collapsing ( " Test box rendering " , | ui | self . box_painting . ui ( ui ) ) ;
2019-03-11 12:32:54 +00:00
2020-04-22 16:15:27 +00:00
CollapsingHeader ::new ( " Scroll area " )
2020-05-19 21:57:48 +00:00
. default_open ( false )
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | {
ScrollArea ::default ( ) . show ( ui , | ui | {
2020-05-30 07:51:57 +00:00
ui . label ( LOREM_IPSUM ) ;
2020-04-22 16:15:27 +00:00
} ) ;
2020-04-21 18:52:17 +00:00
} ) ;
2020-04-19 09:13:24 +00:00
2020-04-24 16:32:27 +00:00
CollapsingHeader ::new ( " Painting " )
2020-05-19 21:57:48 +00:00
. default_open ( false )
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | self . painting . ui ( ui ) ) ;
2020-04-24 16:32:27 +00:00
2020-04-25 12:37:39 +00:00
CollapsingHeader ::new ( " Resize " )
2020-05-19 21:57:48 +00:00
. default_open ( false )
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | {
2020-07-18 22:16:04 +00:00
Resize ::default ( ) . default_height ( 100.0 ) . show ( ui , | ui | {
2020-05-30 15:51:01 +00:00
ui . add ( label! ( " This ui can be resized! " ) ) ;
ui . add ( label! ( " Just pull the handle on the bottom right " ) ) ;
} ) ;
2020-04-25 12:37:39 +00:00
} ) ;
2020-07-23 10:01:48 +00:00
ui . collapsing ( " Name clash demo " , | ui | {
2020-05-30 07:51:57 +00:00
ui . label ( " \
2020-05-08 20:42:31 +00:00
Widgets that store state require unique identifiers so we can track their state between frames . \
2020-04-20 21:42:11 +00:00
Identifiers are normally derived from the titles of the widget . " );
2020-04-19 09:13:24 +00:00
2020-05-30 07:51:57 +00:00
ui . label ( " \
2020-05-08 20:42:31 +00:00
For instance , collapsable headers needs to store wether or not they are open . \
2020-04-19 09:13:24 +00:00
If you fail to give them unique names then clicking one will open both . \
2020-04-21 18:48:31 +00:00
To help you debug this , an error message is printed on screen :" );
2020-04-19 09:13:24 +00:00
2020-05-08 20:42:31 +00:00
ui . collapsing ( " Collapsing header " , | ui | {
2020-05-30 07:51:57 +00:00
ui . label ( " Contents of first folddable ui " ) ;
2020-04-19 09:13:24 +00:00
} ) ;
2020-05-08 20:42:31 +00:00
ui . collapsing ( " Collapsing header " , | ui | {
2020-05-30 07:51:57 +00:00
ui . label ( " Contents of second folddable ui " ) ;
2020-04-19 09:13:24 +00:00
} ) ;
2020-05-30 07:51:57 +00:00
ui . label ( " \
2020-04-20 21:42:11 +00:00
Most widgets don ' t need unique names , but are tracked \
based on their position on screen . For instance , buttons :" );
2020-05-08 20:42:31 +00:00
ui . add ( Button ::new ( " Button " ) ) ;
ui . add ( Button ::new ( " Button " ) ) ;
2020-04-19 09:13:24 +00:00
} ) ;
2018-12-26 22:08:50 +00:00
}
}
2020-04-12 10:07:51 +00:00
2020-05-10 17:04:10 +00:00
// ----------------------------------------------------------------------------
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
#[ cfg_attr(feature = " serde " , serde(default)) ]
2020-05-12 21:00:20 +00:00
struct Widgets {
2020-06-22 08:36:27 +00:00
button_enabled : bool ,
2020-05-12 21:00:20 +00:00
count : usize ,
radio : usize ,
2020-06-10 14:23:49 +00:00
slider_value : f32 ,
2020-05-17 07:44:09 +00:00
single_line_text_input : String ,
multiline_text_input : String ,
2020-05-12 21:00:20 +00:00
}
impl Default for Widgets {
fn default ( ) -> Self {
Self {
2020-06-22 08:36:27 +00:00
button_enabled : true ,
2020-05-12 21:00:20 +00:00
radio : 0 ,
count : 0 ,
2020-07-30 10:30:20 +00:00
slider_value : 3.4 ,
2020-05-17 07:44:09 +00:00
single_line_text_input : " Hello World! " . to_owned ( ) ,
multiline_text_input : " Text can both be so wide that it needs a linebreak, but you can also add manual linebreak by pressing enter, creating new paragraphs. \n This is the start of the next paragraph. \n \n Click me to edit me! " . to_owned ( ) ,
2020-05-12 21:00:20 +00:00
}
}
}
impl Widgets {
pub fn ui ( & mut self , ui : & mut Ui ) {
ui . horizontal ( | ui | {
ui . add ( label! ( " Text can have " ) . text_color ( srgba ( 110 , 255 , 110 , 255 ) ) ) ;
ui . add ( label! ( " color " ) . text_color ( srgba ( 128 , 140 , 255 , 255 ) ) ) ;
ui . add ( label! ( " and tooltips (hover me) " ) ) . tooltip_text (
" This is a multiline tooltip that demonstrates that you can easily add tooltips to any element. \n This is the second line. \n This is the third. " ,
) ;
} ) ;
ui . horizontal ( | ui | {
2020-07-23 12:35:12 +00:00
ui . radio_value ( " First " , & mut self . radio , 0 ) ;
ui . radio_value ( " Second " , & mut self . radio , 1 ) ;
ui . radio_value ( " Final " , & mut self . radio , 2 ) ;
2020-05-12 21:00:20 +00:00
} ) ;
2020-06-22 08:36:27 +00:00
ui . add ( Checkbox ::new ( & mut self . button_enabled , " Button enabled " ) ) ;
2020-05-17 07:44:09 +00:00
ui . inner_layout ( Layout ::horizontal ( Align ::Center ) , | ui | {
2020-05-12 21:00:20 +00:00
if ui
2020-06-22 08:36:27 +00:00
. add ( Button ::new ( " Click me " ) . enabled ( self . button_enabled ) )
2020-05-12 21:00:20 +00:00
. tooltip_text ( " This will just increase a counter. " )
. clicked
{
self . count + = 1 ;
}
ui . add ( label! ( " The button has been clicked {} times " , self . count ) ) ;
} ) ;
2020-06-10 14:23:49 +00:00
ui . add (
Slider ::f32 ( & mut self . slider_value , - 10. 0 ..= 10.0 )
. text ( " value " )
. precision ( 2 ) ,
) ;
2020-05-12 21:00:20 +00:00
if ui . add ( Button ::new ( " Double it " ) ) . clicked {
2020-06-10 14:23:49 +00:00
self . slider_value * = 2.0 ;
2020-05-12 21:00:20 +00:00
}
2020-06-10 14:23:49 +00:00
ui . horizontal ( | ui | {
ui . label ( " drag this number: " ) ;
ui . add ( DragValue ::f32 ( & mut self . slider_value ) . speed ( 0.01 ) ) ;
} ) ;
2020-05-12 21:00:20 +00:00
2020-05-17 07:44:09 +00:00
ui . horizontal ( | ui | {
ui . add ( label! ( " Single line text input: " ) ) ;
ui . add (
TextEdit ::new ( & mut self . single_line_text_input )
. multiline ( false )
. id ( " single line " ) ,
) ;
} ) ; // TODO: .tooltip_text("Enter text to edit me")
ui . add ( label! ( " Multiline text input: " ) ) ;
ui . add ( TextEdit ::new ( & mut self . multiline_text_input ) . id ( " multiline " ) ) ;
2020-05-12 21:00:20 +00:00
}
}
// ----------------------------------------------------------------------------
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
#[ cfg_attr(feature = " serde " , serde(default)) ]
2020-05-12 21:00:20 +00:00
struct BoxPainting {
size : Vec2 ,
corner_radius : f32 ,
stroke_width : f32 ,
num_boxes : usize ,
}
impl Default for BoxPainting {
fn default ( ) -> Self {
Self {
size : vec2 ( 100.0 , 50.0 ) ,
corner_radius : 5.0 ,
stroke_width : 2.0 ,
num_boxes : 1 ,
}
}
}
impl BoxPainting {
pub fn ui ( & mut self , ui : & mut Ui ) {
ui . add ( Slider ::f32 ( & mut self . size . x , 0. 0 ..= 500.0 ) . text ( " width " ) ) ;
ui . add ( Slider ::f32 ( & mut self . size . y , 0. 0 ..= 500.0 ) . text ( " height " ) ) ;
ui . add ( Slider ::f32 ( & mut self . corner_radius , 0. 0 ..= 50.0 ) . text ( " corner_radius " ) ) ;
ui . add ( Slider ::f32 ( & mut self . stroke_width , 0. 0 ..= 10.0 ) . text ( " stroke_width " ) ) ;
ui . add ( Slider ::usize ( & mut self . num_boxes , 0 ..= 5 ) . text ( " num_boxes " ) ) ;
let pos = ui
2020-05-23 10:38:52 +00:00
. allocate_space ( vec2 ( self . size . x * ( self . num_boxes as f32 ) , self . size . y ) )
2020-05-12 21:00:20 +00:00
. min ;
let mut cmds = vec! [ ] ;
for i in 0 .. self . num_boxes {
cmds . push ( PaintCmd ::Rect {
corner_radius : self . corner_radius ,
2020-05-23 12:17:40 +00:00
fill : Some ( gray ( 136 , 255 ) ) ,
2020-05-12 21:00:20 +00:00
rect : Rect ::from_min_size (
pos2 ( 10.0 + pos . x + ( i as f32 ) * ( self . size . x * 1.1 ) , pos . y ) ,
self . size ,
) ,
2020-05-23 12:07:49 +00:00
outline : Some ( LineStyle ::new ( self . stroke_width , gray ( 255 , 255 ) ) ) ,
2020-05-12 21:00:20 +00:00
} ) ;
}
2020-07-30 12:11:09 +00:00
ui . painter ( ) . extend ( cmds ) ;
2020-05-12 21:00:20 +00:00
}
}
// ----------------------------------------------------------------------------
2020-05-30 09:04:40 +00:00
#[ derive(Default) ]
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
#[ cfg_attr(feature = " serde " , serde(default)) ]
2020-04-24 16:32:27 +00:00
struct Painting {
2020-04-26 20:25:23 +00:00
lines : Vec < Vec < Vec2 > > ,
2020-04-24 16:32:27 +00:00
}
impl Painting {
2020-05-08 20:42:31 +00:00
pub fn ui ( & mut self , ui : & mut Ui ) {
2020-05-30 07:51:57 +00:00
ui . label ( " Draw with your mouse to paint " ) ;
2020-05-08 20:42:31 +00:00
if ui . add ( Button ::new ( " Clear " ) ) . clicked {
2020-04-24 16:32:27 +00:00
self . lines . clear ( ) ;
}
2020-05-17 07:44:09 +00:00
Resize ::default ( )
2020-07-18 22:16:04 +00:00
. default_size ( [ 200.0 , 200.0 ] )
2020-05-17 07:44:09 +00:00
. show ( ui , | ui | self . content ( ui ) ) ;
}
2020-04-26 20:25:23 +00:00
2020-05-17 07:44:09 +00:00
fn content ( & mut self , ui : & mut Ui ) {
2020-05-23 10:38:52 +00:00
let rect = ui . allocate_space ( ui . available_finite ( ) . size ( ) ) ;
let interact = ui . interact ( rect , ui . id ( ) , Sense ::drag ( ) ) ;
2020-05-17 07:44:09 +00:00
let rect = interact . rect ;
2020-07-30 12:11:09 +00:00
let clip_rect = ui . clip_rect ( ) . intersect ( rect ) ; // Make sure we don't paint out of bounds
let painter = Painter ::new ( ui . ctx ( ) . clone ( ) , ui . layer ( ) , clip_rect ) ;
2020-04-26 20:25:23 +00:00
2020-05-17 07:44:09 +00:00
if self . lines . is_empty ( ) {
self . lines . push ( vec! [ ] ) ;
}
2020-04-24 16:32:27 +00:00
2020-05-17 07:44:09 +00:00
let current_line = self . lines . last_mut ( ) . unwrap ( ) ;
2020-04-24 16:32:27 +00:00
2020-05-17 07:44:09 +00:00
if interact . active {
2020-05-21 08:20:16 +00:00
if let Some ( mouse_pos ) = ui . input ( ) . mouse . pos {
2020-05-17 07:44:09 +00:00
let canvas_pos = mouse_pos - rect . min ;
if current_line . last ( ) ! = Some ( & canvas_pos ) {
current_line . push ( canvas_pos ) ;
2020-04-24 16:32:27 +00:00
}
}
2020-05-17 07:44:09 +00:00
} else if ! current_line . is_empty ( ) {
self . lines . push ( vec! [ ] ) ;
}
2020-04-24 16:32:27 +00:00
2020-05-17 07:44:09 +00:00
for line in & self . lines {
if line . len ( ) > = 2 {
2020-05-23 12:14:36 +00:00
let points : Vec < Pos2 > = line . iter ( ) . map ( | p | rect . min + * p ) . collect ( ) ;
2020-07-30 12:11:09 +00:00
painter . add ( PaintCmd ::Path {
2020-05-23 12:14:36 +00:00
path : Path ::from_open_points ( & points ) ,
closed : false ,
outline : Some ( LineStyle ::new ( 2.0 , LIGHT_GRAY ) ) ,
2020-05-23 12:17:40 +00:00
fill : None ,
2020-05-17 07:44:09 +00:00
} ) ;
}
}
2020-04-24 16:32:27 +00:00
}
}
2020-05-10 17:04:10 +00:00
// ----------------------------------------------------------------------------
2020-05-12 21:00:20 +00:00
use crate ::layout ::* ;
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
#[ cfg_attr(feature = " serde " , serde(default)) ]
2020-07-23 10:01:48 +00:00
struct LayoutDemo {
2020-05-12 21:00:20 +00:00
dir : Direction ,
2020-05-13 19:36:15 +00:00
align : Option < Align > , // None == jusitifed
2020-05-13 20:24:32 +00:00
reversed : bool ,
2020-05-12 21:00:20 +00:00
}
2020-07-23 10:01:48 +00:00
impl Default for LayoutDemo {
2020-05-12 21:00:20 +00:00
fn default ( ) -> Self {
Self {
dir : Direction ::Vertical ,
2020-05-13 19:36:15 +00:00
align : Some ( Align ::Center ) ,
2020-05-13 20:24:32 +00:00
reversed : false ,
2020-05-12 21:00:20 +00:00
}
}
}
2020-07-23 10:01:48 +00:00
impl LayoutDemo {
2020-05-12 21:00:20 +00:00
pub fn ui ( & mut self , ui : & mut Ui ) {
2020-05-13 19:36:15 +00:00
Resize ::default ( )
2020-07-18 22:16:04 +00:00
. default_size ( [ 200.0 , 100.0 ] )
2020-05-23 09:28:21 +00:00
. show ( ui , | ui | self . content_ui ( ui ) ) ;
2020-05-13 19:36:15 +00:00
}
2020-05-23 09:28:21 +00:00
pub fn content_ui ( & mut self , ui : & mut Ui ) {
2020-05-13 20:24:32 +00:00
let layout = Layout ::from_dir_align ( self . dir , self . align ) ;
if self . reversed {
ui . set_layout ( layout . reverse ( ) ) ;
} else {
ui . set_layout ( layout ) ;
}
2020-05-12 21:00:20 +00:00
2020-05-13 20:56:37 +00:00
// ui.add(label!("Available space: {:?}", ui.available().size()));
2020-05-12 21:00:20 +00:00
if ui . add ( Button ::new ( " Reset " ) ) . clicked {
* self = Default ::default ( ) ;
}
ui . add ( Separator ::new ( ) ) ;
ui . add ( label! ( " Direction: " ) ) ;
// TODO: enum iter
for & dir in & [ Direction ::Horizontal , Direction ::Vertical ] {
if ui
. add ( RadioButton ::new ( self . dir = = dir , format! ( " {:?} " , dir ) ) )
. clicked
{
self . dir = dir ;
}
}
2020-05-13 20:24:32 +00:00
ui . add ( Checkbox ::new ( & mut self . reversed , " Reversed " ) ) ;
2020-05-12 21:00:20 +00:00
ui . add ( Separator ::new ( ) ) ;
2020-05-13 19:36:15 +00:00
2020-05-12 21:00:20 +00:00
ui . add ( label! ( " Align: " ) ) ;
2020-05-13 19:36:15 +00:00
for & align in & [ Align ::Min , Align ::Center , Align ::Max ] {
2020-05-12 21:00:20 +00:00
if ui
. add ( RadioButton ::new (
2020-05-13 19:36:15 +00:00
self . align = = Some ( align ) ,
2020-05-12 21:00:20 +00:00
format! ( " {:?} " , align ) ,
) )
. clicked
{
2020-05-13 19:36:15 +00:00
self . align = Some ( align ) ;
2020-05-12 21:00:20 +00:00
}
}
2020-05-13 19:36:15 +00:00
if ui
. add ( RadioButton ::new ( self . align = = None , " Justified " ) )
. tooltip_text ( " Try to fill full width/heigth (e.g. buttons) " )
. clicked
{
self . align = None ;
}
2020-05-12 21:00:20 +00:00
}
}
// ----------------------------------------------------------------------------
2020-05-19 21:59:37 +00:00
#[ derive(Clone, Copy, PartialEq) ]
enum Action {
Keep ,
Delete ,
}
2020-05-30 09:04:40 +00:00
#[ derive(Clone, Default) ]
2020-08-09 15:34:26 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
2020-05-19 21:59:37 +00:00
struct Tree ( Vec < Tree > ) ;
impl Tree {
2020-07-23 10:01:48 +00:00
pub fn demo ( ) -> Self {
2020-05-19 21:59:37 +00:00
Self ( vec! [
Tree ( vec! [ Tree ::default ( ) ; 4 ] ) ,
Tree ( vec! [ Tree ( vec! [ Tree ::default ( ) ; 2 ] ) ; 3 ] ) ,
] )
}
pub fn ui ( & mut self , ui : & mut Ui ) -> Action {
self . ui_impl ( ui , 0 , " root " )
}
fn ui_impl ( & mut self , ui : & mut Ui , depth : usize , name : & str ) -> Action {
CollapsingHeader ::new ( name )
. default_open ( depth < 1 )
. show ( ui , | ui | self . children_ui ( ui , depth ) )
. unwrap_or ( Action ::Keep )
}
fn children_ui ( & mut self , ui : & mut Ui , depth : usize ) -> Action {
if depth > 0 & & ui . add ( Button ::new ( " delete " ) . text_color ( color ::RED ) ) . clicked {
return Action ::Delete ;
}
self . 0 = std ::mem ::take ( self )
. 0
. into_iter ( )
. enumerate ( )
. filter_map ( | ( i , mut tree ) | {
if tree . ui_impl ( ui , depth + 1 , & format! ( " child # {} " , i ) ) = = Action ::Keep {
Some ( tree )
} else {
None
}
} )
. collect ( ) ;
2020-05-30 07:51:57 +00:00
if ui . button ( " + " ) . clicked {
2020-05-19 21:59:37 +00:00
self . 0. push ( Tree ::default ( ) ) ;
}
Action ::Keep
}
}
// ----------------------------------------------------------------------------
2020-04-21 18:52:17 +00:00
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.
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 . " ;