diff options
Diffstat (limited to 'pixel-client')
-rw-r--r-- | pixel-client/src/main.rs | 4 | ||||
-rw-r--r-- | pixel-client/src/menu.rs | 7 | ||||
-rw-r--r-- | pixel-client/src/ui.rs | 55 |
3 files changed, 63 insertions, 3 deletions
diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs index 05e19194..d1b7e7a9 100644 --- a/pixel-client/src/main.rs +++ b/pixel-client/src/main.rs @@ -148,6 +148,10 @@ fn main() { State::Menu(menu) => menu.keyboard_event(keycode, true), _ => (), }, + Event::TextInput { text, .. } => match &mut state { + State::Menu(menu) => menu.ui_state.text_input(text), + _ => (), + }, _ => {} } } diff --git a/pixel-client/src/menu.rs b/pixel-client/src/menu.rs index 86c71d1f..5b2c9d29 100644 --- a/pixel-client/src/menu.rs +++ b/pixel-client/src/menu.rs @@ -36,8 +36,9 @@ use sdl2::{ pub struct Menu { map: Tilemap, fade_in: f32, - ui_state: UiState, + pub ui_state: UiState, background: Vec2, + server_address: String, next_state: Option<Box<State>>, } @@ -74,6 +75,7 @@ impl Menu { Self { map, fade_in: 0., + server_address: String::from("ws://127.0.0.1"), ui_state: UiState::default(), background: Vec2::ZERO, next_state: None, @@ -125,6 +127,7 @@ impl Menu { if ui.button(80., "Join") { request_join = true } + ui.textedit(80., &mut self.server_address); if ui.button(80., "Settings") { eprintln!("settings button") } @@ -135,7 +138,7 @@ impl Menu { }); if request_join { self.next_state = Some(Box::new(State::Ingame(Box::new(Game::new( - Network::connect("ws://127.0.0.1").unwrap(), + Network::connect(&self.server_address).unwrap(), ctx.atlas_layout(), ))))) } diff --git a/pixel-client/src/ui.rs b/pixel-client/src/ui.rs index fa3a9691..045fc46b 100644 --- a/pixel-client/src/ui.rs +++ b/pixel-client/src/ui.rs @@ -36,6 +36,9 @@ pub struct UiState { mouse_position: Vec2, ui_scale: Vec2, + backspace: bool, + text_input: String, + keyboard_focus: FocusDevice, mouse_focus: FocusDevice, } @@ -58,11 +61,15 @@ impl UiState { self.keyboard_focus .update(keyboard.is_scancode_pressed(Scancode::Space)); } + pub fn text_input(&mut self, text: String) { + self.text_input = text; + } pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) { if down { match keycode { Keycode::DOWN => self.keyboard_focus.focus += 1, Keycode::UP if self.keyboard_focus.focus > 0 => self.keyboard_focus.focus -= 1, + Keycode::BACKSPACE => self.backspace = true, _ => (), } } @@ -81,13 +88,17 @@ impl UiState { index: 0, }; ui(&mut u); - + self.end() + } + fn end(&mut self) { if self.mouse_focus.interact_just_released { self.mouse_focus.pressing = None; } if self.keyboard_focus.interact_just_released { self.keyboard_focus.pressing = None; } + self.text_input.clear(); + self.backspace = false; } } @@ -156,6 +167,48 @@ impl<'a, 'b> Ui<'a, 'b> { released } + pub fn textedit(&mut self, w: f32, content: &mut String) { + let c = self.cursor; + let margin = Vec2::splat(4.); + let text_size = self.renderer.draw_text(self.cursor + margin, &content); + let size = margin + Vec2::new(w, text_size.y) + margin; + + self.index += 1; + + let mouse_rel = self.state.mouse_position - c; + if mouse_rel.x >= 0. && mouse_rel.y >= 0. && mouse_rel.x < size.x && mouse_rel.y < size.y { + self.state.mouse_focus.focus = self.index; + } + + if self.state.mouse_focus.interact_just_pressed + && self.state.mouse_focus.focus == self.index + { + self.state.keyboard_focus.focus = self.index; + } + + let keyboard_focus = self.state.keyboard_focus.focus == self.index; + + if keyboard_focus { + *content += &self.state.text_input; + self.state.text_input.clear(); + if self.state.backspace { + content.pop(); + } + } + + let focus = self.state.mouse_focus.focus == self.index || keyboard_focus; + let l = if focus { 50 } else { 30 }; + self.renderer.draw_ui(SpriteDraw::screen( + self.renderer.misc_textures.solid, + i32::MAX - 1, + c, + size, + Some([l, l, l, 200]), + )); + + self.advance(size); + } + pub fn fill(&mut self) { self.renderer.draw_ui(SpriteDraw::screen( self.renderer.misc_textures.solid, |