From ffda7f22796657a406db53c68fdffe16c9840753 Mon Sep 17 00:00:00 2001 From: szymon Date: Wed, 19 Aug 2026 22:09:19 +0200 Subject: [PATCH] feat: add server connection screen --- src/bin/macroquad_client.rs | 20 +++++------- src/ui/macroquad/main_menu.rs | 19 ----------- src/ui/macroquad/menu_builder.rs | 22 ++++++++++--- src/ui/macroquad/mod.rs | 1 + src/ui/macroquad/server_selection.rs | 47 ++++++++++++++++++++++++++++ src/ui/state.rs | 1 + 6 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 src/ui/macroquad/server_selection.rs diff --git a/src/bin/macroquad_client.rs b/src/bin/macroquad_client.rs index 08f6986..5fd2b96 100644 --- a/src/bin/macroquad_client.rs +++ b/src/bin/macroquad_client.rs @@ -1,21 +1,17 @@ -use checkers::{ - super_advanced_ai::BotDificulty, - ui::{ - macroquad::{ - difficulty_selection::draw_dificulty_selection, - main_menu::{draw_main_menu, get_main_menu_style}, - mode_selection::draw_mode_selection, - }, - state::{GameContext, GuiState}, +use checkers::ui::{ + macroquad::{ + difficulty_selection::draw_dificulty_selection, main_menu::draw_main_menu, + mode_selection::draw_mode_selection, server_selection::draw_server_selection, }, + state::{GameContext, GuiState}, }; use macroquad::{ - color::{BLACK, BLUE, RED, WHITE}, + color::{BLACK, WHITE}, math::vec2, miniquad::{self, conf::LinuxBackend::WaylandWithX11Fallback}, texture::{DrawTextureParams, draw_texture_ex, load_texture}, ui::{Skin, root_ui}, - window::{Conf, clear_background, next_frame, screen_height, screen_width}, + window::{Conf, screen_height, screen_width}, }; fn window_conf() -> Conf { @@ -88,7 +84,7 @@ pub async fn main() { } GuiState::ServerSelection => { - todo!(); + draw_server_selection(&mut state, &mut context).await; } GuiState::Settings => { diff --git a/src/ui/macroquad/main_menu.rs b/src/ui/macroquad/main_menu.rs index c60d940..d941fdd 100644 --- a/src/ui/macroquad/main_menu.rs +++ b/src/ui/macroquad/main_menu.rs @@ -8,25 +8,6 @@ use macroquad::{ use crate::ui::{macroquad::menu_builder::MenuBuilder, state::GuiState}; -pub async fn get_main_menu_style() -> Skin { - let label_style = root_ui() - .style_builder() - .text_color(WHITE) - .font_size(64) - .build(); - - let button_style = root_ui() - .style_builder() - .text_color(BLACK) - .font_size(32) - .build(); - - Skin { - label_style, - button_style, - ..root_ui().default_skin() - } -} pub async fn draw_main_menu(state: &mut GuiState) { let menu_size = vec2(screen_width() * 0.5, screen_height() * 0.5); diff --git a/src/ui/macroquad/menu_builder.rs b/src/ui/macroquad/menu_builder.rs index 5f7ac05..fd94cf4 100644 --- a/src/ui/macroquad/menu_builder.rs +++ b/src/ui/macroquad/menu_builder.rs @@ -1,8 +1,8 @@ use macroquad::{ math::{Vec2, vec2}, ui::{ - Ui, root_ui, - widgets::{Button, Label}, + Ui, hash, + widgets::{Button, InputText, Label}, }, }; @@ -24,13 +24,22 @@ impl MenuBuilder { } } + pub fn text_input(&mut self, ui: &mut Ui, buffer: &mut String) { + InputText::new(hash!()) + .position(vec2(self.center_x, self.current_y)) + .size(self.button_size) + .ui(ui, buffer); + + self.update_height(); + } + pub fn label(&mut self, ui: &mut Ui, label: &str) { Label::new(label) .position(vec2(self.center_x, self.current_y)) .size(self.button_size) .ui(ui); - self.current_y += self.button_size.y + self.spacing; + self.update_height(); } pub fn button(&mut self, ui: &mut Ui, label: &str) -> bool { @@ -39,8 +48,11 @@ impl MenuBuilder { .size(self.button_size) .ui(ui); - self.current_y += self.button_size.y + self.spacing; - + self.update_height(); clicked } + + fn update_height(&mut self) { + self.current_y += self.button_size.y + self.spacing; + } } diff --git a/src/ui/macroquad/mod.rs b/src/ui/macroquad/mod.rs index cae3309..3b2fad6 100644 --- a/src/ui/macroquad/mod.rs +++ b/src/ui/macroquad/mod.rs @@ -2,3 +2,4 @@ pub mod difficulty_selection; pub mod main_menu; mod menu_builder; pub mod mode_selection; +pub mod server_selection; diff --git a/src/ui/macroquad/server_selection.rs b/src/ui/macroquad/server_selection.rs new file mode 100644 index 0000000..4e03e70 --- /dev/null +++ b/src/ui/macroquad/server_selection.rs @@ -0,0 +1,47 @@ +use macroquad::{ + color::GRAY, + math::vec2, + shapes::draw_rectangle, + ui::{hash, root_ui, widgets::Group}, + window::{next_frame, screen_height, screen_width}, +}; + +use crate::ui::{ + macroquad::menu_builder::MenuBuilder, + state::{ + GameContext, + GuiState::{self}, + }, +}; + +pub async fn draw_server_selection(state: &mut GuiState, context: &mut GameContext) { + let menu_size = vec2(screen_width() * 0.5, screen_height() * 0.5); + let menu_pos = vec2( + screen_width() / 2.0 - menu_size.x / 2.0, + screen_height() / 2.0 - menu_size.y / 2.0, + ); + + let mut menu_builder = MenuBuilder::new(menu_size.x, menu_size.y * 0.1); + + draw_rectangle(menu_pos.x, menu_pos.y, menu_size.x, menu_size.y, GRAY); + + Group::new(hash!(), menu_size) + .position(menu_pos) + .ui(&mut root_ui(), |ui| { + menu_builder.label(ui, "Server selection"); + + menu_builder.text_input(ui, &mut context.server_url_buffer); + //context.normalise_url(); + + if menu_builder.button(ui, "Connect") { + context.is_single = false; + *state = GuiState::Game; + } + + if menu_builder.button(ui, "Go back") { + *state = GuiState::ModeSelection; + } + }); + + next_frame().await; +} diff --git a/src/ui/state.rs b/src/ui/state.rs index 89003c4..ffd3e7f 100644 --- a/src/ui/state.rs +++ b/src/ui/state.rs @@ -18,4 +18,5 @@ pub enum GuiState { pub struct GameContext { pub difficulty: BotDificulty, pub is_single: bool, + pub server_url_buffer: String, }