TextButton

Struct TextButton 

Source
pub struct TextButton { /* private fields */ }

Implementations§

Source§

impl TextButton

Source

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	}
Source

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

Source§

fn handle_char_ev(&mut self, _ch: char) -> bool

Source§

fn handle_key_ev( &mut self, _key: Option<VirtualKeyCode>, _pressed: bool, ) -> bool

Source§

fn handle_mouse_pos_ev( &mut self, mouse_pos: (f32, f32), _display: &Display, ) -> bool

Source§

fn handle_mouse_ev(&mut self, button: MouseButton, state: ElementState) -> bool

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.