2020-05-01 00:08:01 +00:00
// #![allow(dead_code, unused_variables)] // should be commented out
2020-05-10 17:04:10 +00:00
use std ::sync ::Arc ;
use serde_derive ::{ Deserialize , Serialize } ;
2020-05-01 00:08:01 +00:00
2020-04-25 20:49:57 +00:00
use crate ::{ color ::* , containers ::* , widgets ::* , * } ;
2018-12-26 09:46:23 +00:00
2020-05-10 17:04:10 +00:00
// ----------------------------------------------------------------------------
#[ derive(Default, Deserialize, Serialize) ]
pub struct ExampleApp {
example_window : ExampleWindow ,
open_windows : OpenWindows ,
}
impl ExampleApp {
pub fn ui ( & mut self , ctx : & Arc < Context > ) {
// TODO: Make it even simpler to show a window
Window ::new ( " Examples " )
. default_pos ( pos2 ( 32.0 , 100.0 ) )
. default_size ( vec2 ( 430.0 , 600.0 ) )
. show ( ctx , | ui | {
show_menu_bar ( ui , & mut self . open_windows ) ;
self . example_window . ui ( ui ) ;
} ) ;
Window ::new ( " Settings " )
. open ( & mut self . open_windows . settings )
. default_pos ( pos2 ( 500.0 , 100.0 ) )
. default_size ( vec2 ( 350.0 , 200.0 ) )
. show ( ctx , | ui | {
ctx . settings_ui ( ui ) ;
} ) ;
Window ::new ( " Inspection " )
. open ( & mut self . open_windows . inspection )
. default_pos ( pos2 ( 500.0 , 400.0 ) )
. default_size ( vec2 ( 400.0 , 300.0 ) )
. show ( ctx , | ui | {
ctx . inspection_ui ( ui ) ;
} ) ;
Window ::new ( " Memory " )
. open ( & mut self . open_windows . memory )
. default_pos ( pos2 ( 700.0 , 350.0 ) )
. auto_sized ( )
. show ( ctx , | ui | {
ctx . memory_ui ( ui ) ;
} ) ;
}
}
#[ derive(Deserialize, Serialize) ]
struct OpenWindows {
settings : bool ,
inspection : bool ,
memory : bool ,
}
impl Default for OpenWindows {
fn default ( ) -> Self {
Self {
settings : false ,
inspection : true ,
memory : false ,
}
}
}
fn show_menu_bar ( ui : & mut Ui , windows : & mut OpenWindows ) {
menu ::bar ( ui , | ui | {
menu ::menu ( ui , " File " , | ui | {
ui . add ( Button ::new ( " Do nothing " ) ) ;
ui . add ( Button ::new ( " Carry on " ) ) ;
ui . add ( Button ::new ( " Don't Quit " ) ) ;
} ) ;
menu ::menu ( ui , " Windows " , | ui | {
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 | {
ui . add ( label! ( " This is Emigui, but you already knew that! " ) ) ;
} ) ;
} ) ;
}
// ----------------------------------------------------------------------------
2020-05-08 20:42:31 +00:00
/// Showcase some ui code
2020-05-10 17:04:10 +00:00
#[ derive(Deserialize, Serialize) ]
2020-05-01 00:08:01 +00:00
pub struct ExampleWindow {
2018-12-26 21:17:33 +00:00
checked : bool ,
2019-03-11 12:32:44 +00:00
count : usize ,
radio : usize ,
2020-04-29 19:25:49 +00:00
text_inputs : [ String ; 3 ] ,
2018-12-26 16:32:58 +00:00
2019-01-06 23:03:29 +00:00
size : Vec2 ,
2018-12-26 16:32:58 +00:00
corner_radius : f32 ,
stroke_width : f32 ,
2019-03-11 12:32:44 +00:00
num_boxes : usize ,
num_columns : usize ,
2019-03-11 12:32:54 +00:00
slider_value : usize ,
2020-04-24 16:32:27 +00:00
painting : Painting ,
2018-12-26 09:46:23 +00:00
}
2020-05-01 00:08:01 +00:00
impl Default for ExampleWindow {
fn default ( ) -> ExampleWindow {
ExampleWindow {
2019-01-05 15:23:40 +00:00
checked : true ,
2019-01-16 15:28:43 +00:00
radio : 0 ,
2018-12-26 16:32:58 +00:00
count : 0 ,
2020-05-10 17:04:10 +00:00
text_inputs : [ " Hello " . to_string ( ) , " World " . to_string ( ) , " " . to_string ( ) ] ,
2020-04-29 19:25:49 +00:00
2019-01-06 23:03:29 +00:00
size : vec2 ( 100.0 , 50.0 ) ,
2018-12-26 16:32:58 +00:00
corner_radius : 5.0 ,
stroke_width : 2.0 ,
2019-02-10 15:10:08 +00:00
num_boxes : 1 ,
2019-03-11 12:32:44 +00:00
num_columns : 2 ,
2019-03-11 12:32:54 +00:00
slider_value : 100 ,
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-05-01 00:08:01 +00:00
impl ExampleWindow {
2020-05-08 20:42:31 +00:00
pub fn ui ( & mut self , ui : & mut Ui ) {
ui . collapsing ( " About Emigui " , | ui | {
ui . add ( label! (
2020-04-21 18:48:31 +00:00
" Emigui is an experimental immediate mode GUI written in Rust. "
) ) ;
2020-04-23 17:15:17 +00:00
2020-05-08 20:42:31 +00:00
ui . horizontal ( | ui | {
ui . add_label ( " Project home page: " ) ;
ui . add_hyperlink ( " https://github.com/emilk/emigui/ " ) ;
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-04-29 19:25:49 +00:00
. default_open ( )
2020-05-08 20:42:31 +00:00
. show ( ui , | 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 (
2019-01-21 07:48:32 +00:00
" 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. " ,
) ;
} ) ;
2018-12-27 23:51:40 +00:00
2020-05-08 20:42:31 +00:00
ui . add ( Checkbox ::new ( & mut self . checked , " checkbox " ) ) ;
2018-12-28 22:53:15 +00:00
2020-05-08 20:42:31 +00:00
ui . horizontal ( | ui | {
if ui . add ( radio ( self . radio = = 0 , " First " ) ) . clicked {
2019-01-21 07:48:32 +00:00
self . radio = 0 ;
}
2020-05-08 20:42:31 +00:00
if ui . add ( radio ( self . radio = = 1 , " Second " ) ) . clicked {
2019-01-21 07:48:32 +00:00
self . radio = 1 ;
}
2020-05-08 20:42:31 +00:00
if ui . add ( radio ( self . radio = = 2 , " Final " ) ) . clicked {
2019-01-21 07:48:32 +00:00
self . radio = 2 ;
}
} ) ;
2019-01-16 15:28:43 +00:00
2020-05-08 20:42:31 +00:00
ui . horizontal ( | ui | {
if ui
2019-01-21 07:48:32 +00:00
. add ( Button ::new ( " Click me " ) )
. tooltip_text ( " This will just increase a counter. " )
. clicked
{
self . count + = 1 ;
}
2020-05-08 20:42:31 +00:00
ui . add ( label! (
2020-05-07 15:37:17 +00:00
" The button has been clicked {} times " ,
2019-01-21 07:48:32 +00:00
self . count
) ) ;
} ) ;
2020-04-21 18:52:17 +00:00
2020-05-08 20:42:31 +00:00
ui . add ( Slider ::usize ( & mut self . slider_value , 1 ..= 1000 ) . text ( " value " ) ) ;
if ui . add ( Button ::new ( " Double it " ) ) . clicked {
2020-04-22 17:39:51 +00:00
self . slider_value * = 2 ;
2020-04-21 18:52:17 +00:00
}
2020-04-29 19:25:49 +00:00
for ( i , text ) in self . text_inputs . iter_mut ( ) . enumerate ( ) {
2020-05-08 20:42:31 +00:00
ui . horizontal ( | ui | {
ui . add ( label! ( " Text input {}: " , i ) ) ;
ui . add ( TextEdit ::new ( text ) . id ( i ) ) ;
2020-04-29 19:25:49 +00:00
} ) ; // TODO: .tooltip_text("Enter text to edit me")
}
2018-12-28 09:39:08 +00:00
} ) ;
2018-12-26 21:26:15 +00:00
2020-05-08 20:42:31 +00:00
ui . collapsing ( " Layouts " , | ui | {
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-08 20:42:31 +00:00
ui . collapsing ( " Test box rendering " , | 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 " ) ) ;
2018-12-26 16:32:58 +00:00
2020-05-08 20:42:31 +00:00
let pos = ui
2019-02-10 15:10:08 +00:00
. reserve_space (
vec2 ( self . size . x * ( self . num_boxes as f32 ) , self . size . y ) ,
None ,
)
. rect
2020-04-25 13:45:38 +00:00
. min ;
2019-02-10 15:10:08 +00:00
2019-02-10 19:56:59 +00:00
let mut cmds = vec! [ ] ;
2019-02-10 15:10:08 +00:00
for i in 0 .. self . num_boxes {
2019-02-10 19:56:59 +00:00
cmds . push ( PaintCmd ::Rect {
2019-02-10 15:10:08 +00:00
corner_radius : self . corner_radius ,
2019-03-11 14:30:32 +00:00
fill_color : Some ( gray ( 136 , 255 ) ) ,
2019-02-10 15:10:08 +00:00
rect : Rect ::from_min_size (
2020-04-21 14:50:56 +00:00
pos2 ( 10.0 + pos . x + ( i as f32 ) * ( self . size . x * 1.1 ) , pos . y ) ,
2019-02-10 15:10:08 +00:00
self . size ,
) ,
2020-04-19 09:11:41 +00:00
outline : Some ( Outline ::new ( self . stroke_width , gray ( 255 , 255 ) ) ) ,
2019-02-10 19:56:59 +00:00
} ) ;
2019-02-10 15:10:08 +00:00
}
2020-05-08 20:42:31 +00:00
ui . add_paint_cmds ( cmds ) ;
2019-01-06 23:03:29 +00:00
} ) ;
2019-03-11 12:32:54 +00:00
2020-04-22 16:15:27 +00:00
CollapsingHeader ::new ( " Scroll area " )
2020-04-23 07:50:03 +00:00
// .default_open()
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | {
ScrollArea ::default ( ) . show ( ui , | ui | {
ui . add_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-04-25 12:37:39 +00:00
// .default_open()
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-04-29 19:25:49 +00:00
// .default_open()
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | {
2020-04-25 12:37:39 +00:00
Resize ::default ( )
. default_height ( 200.0 )
// .as_wide_as_possible()
2020-04-26 20:30:24 +00:00
. auto_shrink_height ( false )
2020-05-08 20:42:31 +00:00
. show ( ui , | ui | {
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-05-08 20:42:31 +00:00
ui . collapsing ( " Name clash example " , | ui | {
ui . add_label ( " \
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-08 20:42:31 +00:00
ui . add_label ( " \
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 | {
ui . add_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 | {
ui . add_label ( " Contents of second folddable ui " ) ;
2020-04-19 09:13:24 +00:00
} ) ;
2020-05-08 20:42:31 +00:00
ui . add_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
// ----------------------------------------------------------------------------
#[ derive(Default, Deserialize, Serialize) ]
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 ) {
ui . add_label ( " Draw with your mouse to paint " ) ;
if ui . add ( Button ::new ( " Clear " ) ) . clicked {
2020-04-24 16:32:27 +00:00
self . lines . clear ( ) ;
}
2020-05-08 20:42:31 +00:00
ui . add_custom_contents ( vec2 ( f32 ::INFINITY , 200.0 ) , | ui | {
let canvas_corner = ui . cursor ( ) ;
let interact = ui . reserve_space ( ui . available_space ( ) , Some ( ui . id ( ) ) ) ;
ui . set_clip_rect ( ui . clip_rect ( ) . intersect ( interact . rect ) ) ; // Make sure we don't paint out of bounds
2020-04-26 20:25:23 +00:00
if self . lines . is_empty ( ) {
self . lines . push ( vec! [ ] ) ;
}
let current_line = self . lines . last_mut ( ) . unwrap ( ) ;
2020-04-24 16:32:27 +00:00
if interact . active {
2020-05-08 20:42:31 +00:00
if let Some ( mouse_pos ) = ui . input ( ) . mouse_pos {
2020-04-26 20:25:23 +00:00
let canvas_pos = mouse_pos - canvas_corner ;
if current_line . last ( ) ! = Some ( & canvas_pos ) {
current_line . push ( canvas_pos ) ;
2020-04-24 16:32:27 +00:00
}
}
2020-04-26 20:25:23 +00:00
} else if ! current_line . is_empty ( ) {
self . lines . push ( vec! [ ] ) ;
2020-04-24 16:32:27 +00:00
}
for line in & self . lines {
if line . len ( ) > = 2 {
2020-05-08 20:42:31 +00:00
ui . add_paint_cmd ( PaintCmd ::Line {
2020-04-26 20:25:23 +00:00
points : line . iter ( ) . map ( | p | canvas_corner + * p ) . collect ( ) ,
2020-04-24 16:32:27 +00:00
color : LIGHT_GRAY ,
width : 2.0 ,
} ) ;
}
}
// Frame it:
2020-05-08 20:42:31 +00:00
ui . add_paint_cmd ( PaintCmd ::Rect {
rect : ui . rect ( ) ,
2020-04-24 16:32:27 +00:00
corner_radius : 0.0 ,
fill_color : None ,
outline : Some ( Outline ::new ( 1.0 , WHITE ) ) ,
} ) ;
} ) ;
}
}
2020-05-10 17:04:10 +00:00
// ----------------------------------------------------------------------------
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 . " ;