pub struct TextButton { /* private fields */ }Implementations§
Source§impl TextButton
impl TextButton
Sourcepub fn new(
text: String,
size: f32,
pos: (f32, f32),
half_size: (f32, f32),
normal_color: [f32; 4],
hover_color: [f32; 4],
) -> Self
pub fn new( text: String, size: f32, pos: (f32, f32), half_size: (f32, f32), normal_color: [f32; 4], hover_color: [f32; 4], ) -> Self
Examples found in repository?
examples/ui/mod.rs (line 33)
28 pub fn new(display: &Display) -> Result<Self, UIError> {
29 Ok(Self {
30 enabled: false,
31 buttons: vec![
32 (MainMenuAction::Start,
33 TextButton::new("Start".to_string(), 0.15, (0., -0.3), (0.2, 0.05), NORMAL_COLOR, HOVER_COLOR)),
34 (MainMenuAction::Quit,
35 TextButton::new("Quit".to_string(), 0.15, (0., -0.5), (0.2, 0.05), NORMAL_COLOR, HOVER_COLOR))
36 ],
37 bg: ImageBackground::new(display, "./textures/mainmenu.jpg", APP_ID, (0., 0.), (3.55, 2.))?,
38 btn_font: LoadedFont::load(display, "./fonts/SourceCodePro-Light.otf", APP_ID, 80.)?,
39 start_dialog: StartDialog::new(display)?,
40 result: None
41 })
42 }
43
44 pub fn draw(&mut self, target: &mut Frame, display: &Display, ui_program: &glium::Program) -> Result<Option<MainMenuAction>, UIError> {
45 self.bg.draw(target, ui_program);
46 for (_, button) in &mut self.buttons {
47 button.draw(target, display, ui_program, &self.btn_font)?;
48 }
49 if self.start_dialog.enabled { self.result = self.start_dialog.draw(target, display, ui_program, &self.btn_font)?; }
50
51 if let Some(result) = self.result {
52 self.result = None;
53 return Ok(Some(result));
54 }
55 Ok(None)
56 }
57}
58
59impl InputListener for MainMenu {
60 fn handle_key_ev(&mut self, key: Option<VirtualKeyCode>, pressed: bool) -> bool {
61 if !self.enabled { return false; }
62 self.start_dialog.handle_key_ev(key, pressed)
63 }
64
65 fn handle_mouse_pos_ev(&mut self, pos: (f32, f32), display: &Display) -> bool {
66 if !self.enabled { return false; }
67 for (_, button) in &mut self.buttons {
68 button.handle_mouse_pos_ev(pos, display);
69 }
70 self.start_dialog.handle_mouse_pos_ev(pos, display);
71 true
72 }
73
74 fn handle_mouse_ev(&mut self, mouse_button: MouseButton, state: ElementState) -> bool {
75 if !self.enabled { return false; }
76 for (name, button) in &mut self.buttons {
77 if button.handle_mouse_ev(mouse_button, state) {
78 match name {
79 MainMenuAction::Start => self.start_dialog.enabled = true,
80 _ => self.result = Some(*name)
81 };
82 return true;
83 }
84 }
85 if self.start_dialog.handle_mouse_ev(mouse_button, state) { return true; }
86 false
87 }
88
89 fn handle_char_ev(&mut self, ch: char) -> bool {
90 if !self.enabled { return false; }
91 self.start_dialog.handle_char_ev(ch)
92 }
93}
94
95struct StartDialog {
96 bg: ImageBackground,
97 ip_input: TextInput,
98 start_btn: TextButton,
99 enabled: bool,
100 result: Option<MainMenuAction>
101}
102
103impl StartDialog {
104 pub fn new(display: &Display) -> Result<Self, UIError> {
105 Ok(Self {
106 bg: ImageBackground::new(display, "./textures/dialog.png", APP_ID, (0., -0.17), (1.0, 0.7))?,
107 start_btn: TextButton::new("Start".to_string(), 0.08, (0.2, -0.2), (0.2, 0.05), NORMAL_COLOR, HOVER_COLOR),
108 ip_input: TextInput::new((-0.45, -0.15), (0.85, 0.12), WHITE),
109 enabled: false,
110 result: None
111 })
112 }Sourcepub fn draw(
&mut self,
target: &mut Frame,
display: &Display,
ui_program: &Program,
font: &LoadedFont,
) -> Result<(), UIError>
pub fn draw( &mut self, target: &mut Frame, display: &Display, ui_program: &Program, font: &LoadedFont, ) -> Result<(), UIError>
Examples found in repository?
examples/ui/mod.rs (line 47)
44 pub fn draw(&mut self, target: &mut Frame, display: &Display, ui_program: &glium::Program) -> Result<Option<MainMenuAction>, UIError> {
45 self.bg.draw(target, ui_program);
46 for (_, button) in &mut self.buttons {
47 button.draw(target, display, ui_program, &self.btn_font)?;
48 }
49 if self.start_dialog.enabled { self.result = self.start_dialog.draw(target, display, ui_program, &self.btn_font)?; }
50
51 if let Some(result) = self.result {
52 self.result = None;
53 return Ok(Some(result));
54 }
55 Ok(None)
56 }
57}
58
59impl InputListener for MainMenu {
60 fn handle_key_ev(&mut self, key: Option<VirtualKeyCode>, pressed: bool) -> bool {
61 if !self.enabled { return false; }
62 self.start_dialog.handle_key_ev(key, pressed)
63 }
64
65 fn handle_mouse_pos_ev(&mut self, pos: (f32, f32), display: &Display) -> bool {
66 if !self.enabled { return false; }
67 for (_, button) in &mut self.buttons {
68 button.handle_mouse_pos_ev(pos, display);
69 }
70 self.start_dialog.handle_mouse_pos_ev(pos, display);
71 true
72 }
73
74 fn handle_mouse_ev(&mut self, mouse_button: MouseButton, state: ElementState) -> bool {
75 if !self.enabled { return false; }
76 for (name, button) in &mut self.buttons {
77 if button.handle_mouse_ev(mouse_button, state) {
78 match name {
79 MainMenuAction::Start => self.start_dialog.enabled = true,
80 _ => self.result = Some(*name)
81 };
82 return true;
83 }
84 }
85 if self.start_dialog.handle_mouse_ev(mouse_button, state) { return true; }
86 false
87 }
88
89 fn handle_char_ev(&mut self, ch: char) -> bool {
90 if !self.enabled { return false; }
91 self.start_dialog.handle_char_ev(ch)
92 }
93}
94
95struct StartDialog {
96 bg: ImageBackground,
97 ip_input: TextInput,
98 start_btn: TextButton,
99 enabled: bool,
100 result: Option<MainMenuAction>
101}
102
103impl StartDialog {
104 pub fn new(display: &Display) -> Result<Self, UIError> {
105 Ok(Self {
106 bg: ImageBackground::new(display, "./textures/dialog.png", APP_ID, (0., -0.17), (1.0, 0.7))?,
107 start_btn: TextButton::new("Start".to_string(), 0.08, (0.2, -0.2), (0.2, 0.05), NORMAL_COLOR, HOVER_COLOR),
108 ip_input: TextInput::new((-0.45, -0.15), (0.85, 0.12), WHITE),
109 enabled: false,
110 result: None
111 })
112 }
113
114 pub fn draw(&mut self, target: &mut Frame, display: &Display, ui_program: &glium::Program, font: &LoadedFont) -> Result<Option<MainMenuAction>, UIError> {
115 self.bg.draw(target, ui_program);
116 self.ip_input.draw(target, display, ui_program, font)?;
117 self.start_btn.draw(target, display, ui_program, font)?;
118 if let Some(result) = self.result {
119 self.result = None;
120 return Ok(Some(result));
121 }
122 Ok(None)
123 }Trait Implementations§
Source§impl InputListener for TextButton
impl InputListener for TextButton
fn handle_char_ev(&mut self, _ch: char) -> bool
fn handle_key_ev( &mut self, _key: Option<VirtualKeyCode>, _pressed: bool, ) -> bool
fn handle_mouse_pos_ev( &mut self, mouse_pos: (f32, f32), _display: &Display, ) -> bool
fn handle_mouse_ev(&mut self, button: MouseButton, state: ElementState) -> bool
Auto Trait Implementations§
impl Freeze for TextButton
impl !RefUnwindSafe for TextButton
impl !Send for TextButton
impl !Sync for TextButton
impl Unpin for TextButton
impl !UnwindSafe for TextButton
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more