2020-12-29 12:40:11 +00:00
use egui ::{ color ::* , * } ;
2020-09-13 19:54:31 +00:00
2020-09-25 12:43:16 +00:00
#[ derive(Debug, PartialEq) ]
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
enum Enum {
First ,
Second ,
Third ,
}
impl Default for Enum {
fn default ( ) -> Self {
Enum ::First
}
}
2020-09-13 19:54:31 +00:00
#[ cfg_attr(feature = " serde " , derive(serde::Deserialize, serde::Serialize)) ]
#[ cfg_attr(feature = " serde " , serde(default)) ]
pub struct Widgets {
button_enabled : bool ,
count : usize ,
2020-09-25 12:43:16 +00:00
radio : Enum ,
2020-12-29 12:40:11 +00:00
sliders : crate ::Sliders ,
2020-09-13 19:54:31 +00:00
angle : f32 ,
color : Srgba ,
single_line_text_input : String ,
multiline_text_input : String ,
toggle_switch : bool ,
}
impl Default for Widgets {
fn default ( ) -> Self {
Self {
button_enabled : true ,
2020-09-25 12:43:16 +00:00
radio : Enum ::First ,
2020-09-13 19:54:31 +00:00
count : 0 ,
2020-10-07 07:59:49 +00:00
sliders : Default ::default ( ) ,
2020-12-27 10:24:08 +00:00
angle : std ::f32 ::consts ::TAU / 3.0 ,
2020-09-13 19:54:31 +00:00
color : ( Rgba ::new ( 0.0 , 1.0 , 0.5 , 1.0 ) * 0.75 ) . into ( ) ,
single_line_text_input : " Hello World! " . to_owned ( ) ,
multiline_text_input : " Text can both be so wide that it needs a line break, but you can also add manual line break by pressing enter, creating new paragraphs. \n This is the start of the next paragraph. \n \n Click me to edit me! " . to_owned ( ) ,
toggle_switch : false ,
}
}
}
impl Widgets {
pub fn ui ( & mut self , ui : & mut Ui ) {
2020-12-29 12:40:11 +00:00
ui . add ( crate ::__egui_github_link_file_line! ( ) ) ;
2020-09-13 19:54:31 +00:00
2020-12-08 21:09:32 +00:00
ui . horizontal_wrapped_for_text ( TextStyle ::Body , | ui | {
ui . add ( Label ::new ( " Text can have " ) . text_color ( srgba ( 110 , 255 , 110 , 255 ) ) ) ;
2020-12-11 11:25:28 +00:00
ui . colored_label ( srgba ( 128 , 140 , 255 , 255 ) , " color " ) ; // Shortcut version
ui . label ( " and tooltips. " ) . on_hover_text (
2020-09-13 19:54:31 +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. " ,
) ;
2020-12-09 20:32:28 +00:00
2020-12-13 19:37:44 +00:00
ui . label ( " You can mix in other widgets into text, like " ) ;
let _ = ui . small_button ( " this button " ) ;
2020-12-09 20:39:49 +00:00
ui . label ( " . " ) ;
2020-12-09 20:32:28 +00:00
2020-12-13 19:37:44 +00:00
ui . label ( " The default font supports all latin and cyrillic characters (ИÅđ…), common math symbols (∫√∞²⅓…), and many emojis (💓🌟🖩…). " )
. on_hover_text ( " There is currently no support for right-to-left languages. " ) ;
ui . label ( " See the 🔤 Font Book for more! " ) ;
ui . monospace ( " There is also a monospace font. " ) ;
2020-12-08 21:09:32 +00:00
} ) ;
2020-09-13 19:54:31 +00:00
2020-12-09 20:32:28 +00:00
let tooltip_ui = | ui : & mut Ui | {
ui . heading ( " The name of the tooltip " ) ;
ui . horizontal ( | ui | {
ui . label ( " This tooltip was created with " ) ;
ui . monospace ( " .on_hover_ui(...) " ) ;
} ) ;
let _ = ui . button ( " A button you can never press " ) ;
} ;
ui . label ( " Tooltips can be more than just simple text. " )
. on_hover_ui ( tooltip_ui ) ;
2020-09-13 19:54:31 +00:00
ui . horizontal ( | ui | {
2020-10-10 10:49:02 +00:00
ui . radio_value ( & mut self . radio , Enum ::First , " First " ) ;
ui . radio_value ( & mut self . radio , Enum ::Second , " Second " ) ;
ui . radio_value ( & mut self . radio , Enum ::Third , " Third " ) ;
2020-09-25 12:43:16 +00:00
} ) ;
2020-12-29 12:40:11 +00:00
egui ::combo_box_with_label ( ui , " Combo Box " , format! ( " {:?} " , self . radio ) , | ui | {
2020-11-30 06:17:03 +00:00
ui . selectable_value ( & mut self . radio , Enum ::First , " First " ) ;
ui . selectable_value ( & mut self . radio , Enum ::Second , " Second " ) ;
ui . selectable_value ( & mut self . radio , Enum ::Third , " Third " ) ;
2020-09-13 19:54:31 +00:00
} ) ;
2020-10-10 11:04:40 +00:00
ui . checkbox ( & mut self . button_enabled , " Button enabled " ) ;
2020-09-13 19:54:31 +00:00
2020-09-18 21:41:02 +00:00
ui . horizontal ( | ui | {
2020-09-13 19:54:31 +00:00
if ui
. add ( Button ::new ( " Click me " ) . enabled ( self . button_enabled ) )
2020-10-01 20:53:11 +00:00
. on_hover_text ( " This will just increase a counter. " )
2020-09-13 19:54:31 +00:00
. clicked
{
self . count + = 1 ;
}
2020-12-13 19:37:44 +00:00
ui . label ( format! ( " The button has been clicked {} times. " , self . count ) ) ;
2020-09-13 19:54:31 +00:00
} ) ;
ui . separator ( ) ;
{
ui . horizontal ( | ui | {
2020-10-07 07:59:49 +00:00
ui . label ( " Drag this value to change it: " ) ;
ui . add ( DragValue ::f64 ( & mut self . sliders . value ) . speed ( 0.01 ) ) ;
2020-09-13 19:54:31 +00:00
} ) ;
2020-10-07 07:59:49 +00:00
ui . add (
2020-12-29 12:40:11 +00:00
egui ::Slider ::f64 ( & mut self . sliders . value , 1. 0 ..= 100.0 )
2020-10-07 07:59:49 +00:00
. logarithmic ( true )
. text ( " A slider " ) ,
) ;
2020-12-29 12:40:11 +00:00
egui ::CollapsingHeader ::new ( " More sliders " )
2020-10-07 07:59:49 +00:00
. default_open ( false )
. show ( ui , | ui | {
self . sliders . ui ( ui ) ;
} ) ;
2020-09-13 19:54:31 +00:00
}
2020-12-13 19:37:44 +00:00
2020-09-13 19:54:31 +00:00
ui . separator ( ) ;
2020-12-13 19:37:44 +00:00
ui . horizontal_for_text ( TextStyle ::Body , | ui | {
ui . label ( " An angle: " ) ;
ui . drag_angle ( & mut self . angle ) ;
ui . label ( format! ( " ≈ {:.3} τ " , self . angle / std ::f32 ::consts ::TAU ) )
. on_hover_text ( " Each τ represents one turn (τ = 2π) " ) ;
} )
. 1
. on_hover_text ( " The angle is stored in radians, but presented in degrees " ) ;
2020-09-13 19:54:31 +00:00
ui . separator ( ) ;
2020-09-18 21:41:02 +00:00
ui . horizontal ( | ui | {
2020-12-29 12:40:11 +00:00
ui . colored_label ( self . color , " Click to select a different text color: " ) ;
2020-09-13 19:54:31 +00:00
ui . color_edit_button_srgba ( & mut self . color ) ;
} ) ;
ui . separator ( ) ;
ui . horizontal ( | ui | {
2020-10-10 11:04:40 +00:00
ui . label ( " Single line text input: " ) ;
2020-11-13 10:22:00 +00:00
let response = ui . text_edit_singleline ( & mut self . single_line_text_input ) ;
2020-11-09 17:42:54 +00:00
if response . lost_kb_focus {
// The user pressed enter.
}
} ) ;
2020-09-13 19:54:31 +00:00
2020-10-10 11:04:40 +00:00
ui . label ( " Multiline text input: " ) ;
2020-11-13 10:22:00 +00:00
ui . text_edit_multiline ( & mut self . multiline_text_input ) ;
2020-09-13 19:54:31 +00:00
ui . separator ( ) ;
2020-12-29 12:40:11 +00:00
crate ::toggle_switch ::demo ( ui , & mut self . toggle_switch ) ;
2020-09-13 19:54:31 +00:00
}
}