ui start
This commit is contained in:
1 parent
826239e8cd
commit
f6c974628f
5 files changed
+240
-1
No files matched your search
@@ -4,3 +4,12 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.18"
|
||||
bevy_ui_text_input = "0.7.0"
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 1
|
||||
debug = true
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
@@ -0,0 +1,23 @@
|
||||
use bevy::{prelude::*};
|
||||
|
||||
#[derive(States, Debug, Clone, Eq, PartialEq, Hash)]
|
||||
pub enum GameState {
|
||||
Menu,
|
||||
InGame,
|
||||
}
|
||||
|
||||
#[derive(States, Debug, Clone, Eq, PartialEq, Hash)]
|
||||
pub enum MenuState {
|
||||
Main,
|
||||
Seed,
|
||||
}
|
||||
|
||||
pub struct GameStatePlagin;
|
||||
|
||||
impl Plugin for GameStatePlagin{
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_state(GameState::Menu);
|
||||
app.insert_state(MenuState::Main);
|
||||
}
|
||||
}
|
||||
|
||||
+19
-1
@@ -1,3 +1,21 @@
|
||||
use bevy::{prelude::*};
|
||||
|
||||
mod game_state;
|
||||
use game_state::GameStatePlagin;
|
||||
use game_state::{GameState, MenuState};
|
||||
|
||||
mod ui_menu;
|
||||
use ui_menu::UIMenu;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(bevy_ui_text_input::TextInputPlugin)
|
||||
.add_plugins(UIMenu)
|
||||
.add_plugins(GameStatePlagin)
|
||||
// .add_systems(Startup, setup)
|
||||
.run();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
use bevy::{prelude::*};
|
||||
use bevy_ui_text_input::TextInputNode;
|
||||
|
||||
use super::GameState;
|
||||
use super::MenuState;
|
||||
|
||||
mod ui_systems;
|
||||
use ui_systems::*;
|
||||
|
||||
pub struct UIMenu;
|
||||
|
||||
impl Plugin for UIMenu {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, setup);
|
||||
|
||||
// запуск
|
||||
app.add_systems(OnEnter(MenuState::Main), setup_main_menu);
|
||||
app.add_systems(OnEnter(MenuState::Seed), setup_seed_menu);
|
||||
|
||||
//очистка
|
||||
app.add_systems(OnExit(MenuState::Main), cleanup_main_menu);
|
||||
app.add_systems(OnExit(MenuState::Seed), cleanup_seed_menu);
|
||||
|
||||
app.add_message::<ButtonEvent>();
|
||||
|
||||
app.add_systems(Update, (
|
||||
button_to_message,
|
||||
handle_button_events.after(button_to_message),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, assets: Res<AssetServer>){
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
|
||||
let booton_menu = (
|
||||
Button,
|
||||
BackgroundColor(Color::srgb(0.3, 0.3, 0.3)),
|
||||
Node {
|
||||
width: Val::Px(200.0),
|
||||
height: Val::Px(60.0),
|
||||
border: UiRect::all(px(5)),
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
border_radius: BorderRadius::MAX,
|
||||
display: Display::Flex,
|
||||
..default()
|
||||
},
|
||||
);
|
||||
|
||||
commands.spawn((
|
||||
Node {
|
||||
width: percent(100),
|
||||
height: percent(100),
|
||||
flex_direction: FlexDirection::Column,
|
||||
align_items: AlignItems::Center,
|
||||
justify_content: JustifyContent::Center,
|
||||
// row_gap: Val::Px(10.0),
|
||||
..Default::default()
|
||||
},
|
||||
children![(
|
||||
booton_menu.clone(),
|
||||
MenuMain,
|
||||
Name::new("start"),
|
||||
children![(
|
||||
Text::new("StartNewGame"),
|
||||
TextFont {
|
||||
font: assets.load("fronts/IBMPlexMono-Medium.ttf"),
|
||||
font_size: 33.0,
|
||||
..default()
|
||||
},
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||
TextShadow::default(),
|
||||
)],
|
||||
),(
|
||||
MenuSead,
|
||||
Node {
|
||||
width: Val::Px(200.0),
|
||||
height: Val::Px(60.0),
|
||||
display: Display::None,
|
||||
..Default::default()
|
||||
},
|
||||
BackgroundColor(Color::srgb(0.3, 0.3, 0.3)),
|
||||
TextInputNode {
|
||||
clear_on_submit: false,
|
||||
unfocus_on_submit: true,
|
||||
..Default::default()
|
||||
},
|
||||
TextFont {
|
||||
font: assets.load("fronts/IBMPlexMono-Medium.ttf"),
|
||||
font_size: 25.,
|
||||
..Default::default()
|
||||
},
|
||||
TextShadow::default(),
|
||||
),(
|
||||
booton_menu.clone(),
|
||||
MenuMain,
|
||||
Name::new("exit"),
|
||||
children![(
|
||||
Text::new("Exit"),
|
||||
TextFont {
|
||||
font: assets.load("fronts/IBMPlexMono-Medium.ttf"),
|
||||
font_size: 33.0,
|
||||
..default()
|
||||
},
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||
TextShadow::default(),
|
||||
)]
|
||||
)],
|
||||
));
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
use bevy::{prelude::*};
|
||||
|
||||
use super::GameState;
|
||||
use super::MenuState;
|
||||
|
||||
#[derive(Component, Clone)]
|
||||
pub struct MenuMain;
|
||||
|
||||
#[derive(Component, Clone)]
|
||||
pub struct MenuSead;
|
||||
|
||||
#[derive(Message)]
|
||||
pub struct MenuBootonEvent(String);
|
||||
|
||||
#[derive(Message)]
|
||||
pub enum ButtonEvent{
|
||||
Start,
|
||||
ExitApp,
|
||||
ExitSead,
|
||||
}
|
||||
|
||||
// запуск.
|
||||
pub fn setup_main_menu(mut query: Query<(&MenuMain, &mut Node)>){
|
||||
for (_, mut node) in &mut query {
|
||||
node.display = Display::Flex;
|
||||
}
|
||||
}
|
||||
pub fn setup_seed_menu(mut query: Query<(&MenuSead, &mut Node)>){
|
||||
for (_, mut node) in &mut query {
|
||||
node.display = Display::Flex;
|
||||
}
|
||||
}
|
||||
|
||||
// отключение
|
||||
pub fn cleanup_main_menu(mut query: Query<(&MenuMain, &mut Node)>){
|
||||
for (_, mut node) in &mut query {
|
||||
node.display = Display::None;
|
||||
}
|
||||
}
|
||||
pub fn cleanup_seed_menu(mut query: Query<(&MenuSead, &mut Node)>){
|
||||
for (_, mut node) in &mut query {
|
||||
node.display = Display::None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn button_to_message(
|
||||
mut writer: MessageWriter<ButtonEvent>,
|
||||
query: Query<(&Interaction, &Name), Changed<Interaction>>,
|
||||
){
|
||||
for (interaction, name) in &query {
|
||||
if *interaction == Interaction::Pressed {
|
||||
match name.as_str() {
|
||||
"start" => {writer.write(ButtonEvent::Start);},
|
||||
_ => {
|
||||
println!("Unknown button pressed: {}", name.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn handle_button_events(
|
||||
mut reader: MessageReader<ButtonEvent>,
|
||||
mut next_menu_state: ResMut<NextState<MenuState>>,
|
||||
mut next_game_state: ResMut<NextState<GameState>>,
|
||||
) {
|
||||
for event in reader.read() {
|
||||
match event {
|
||||
ButtonEvent::Start => {
|
||||
next_menu_state.set(MenuState::Seed);
|
||||
println!("ss");
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user