Skip to content

Commit

Permalink
ability to create projects from project manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jun 4, 2024
1 parent 9d250db commit 1b36a94
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 20 deletions.
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions project-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ impl ProjectManager {
}

fn handle_ui_message(&mut self, message: &UiMessage, ui: &mut UserInterface) {
if let Some(project_wizard) = self.project_wizard.as_mut() {
project_wizard.handle_ui_message(message, ui)
}

if let Some(ButtonMessage::Click) = message.data() {
self.on_button_click(message.destination, ui);
} else if let Some(ListViewMessage::SelectionChanged(selection)) = message.data() {
Expand Down
180 changes: 160 additions & 20 deletions project-manager/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,87 @@ use crate::{make_button, utils::make_dropdown_list_option};
use fyrox::{
core::pool::Handle,
gui::{
dropdown_list::DropdownListBuilder,
button::ButtonMessage,
dropdown_list::{DropdownListBuilder, DropdownListMessage},
grid::{Column, GridBuilder, Row},
message::MessageDirection,
message::{MessageDirection, UiMessage},
path::{PathEditorBuilder, PathEditorMessage},
stack_panel::StackPanelBuilder,
text::TextBuilder,
text::{TextBuilder, TextMessage},
text_box::TextBoxBuilder,
widget::WidgetBuilder,
widget::{WidgetBuilder, WidgetMessage},
window::{WindowBuilder, WindowMessage, WindowTitle},
BuildContext, HorizontalAlignment, Orientation, Thickness, UiNode, VerticalAlignment,
BuildContext, HorizontalAlignment, Orientation, Thickness, UiNode, UserInterface,
VerticalAlignment,
},
};
use std::path::PathBuf;

enum Style {
TwoD,
ThreeD,
}

impl Style {
fn from_index(index: usize) -> Self {
match index {
0 => Self::TwoD,
1 => Self::ThreeD,
_ => unreachable!(),
}
}

fn as_str(&self) -> &'static str {
match self {
Style::TwoD => "2d",
Style::ThreeD => "3d",
}
}
}

enum Vcs {
None,
Git,
Mercurial,
Pijul,
Fossil,
}

impl Vcs {
fn from_index(index: usize) -> Self {
match index {
0 => Self::None,
1 => Self::Git,
2 => Self::Mercurial,
3 => Self::Pijul,
4 => Self::Fossil,
_ => unreachable!(),
}
}

fn as_str(&self) -> &'static str {
match self {
Vcs::None => "none",
Vcs::Git => "git",
Vcs::Mercurial => "hg",
Vcs::Pijul => "pijul",
Vcs::Fossil => "fossil",
}
}
}

pub struct ProjectWizard {
pub window: Handle<UiNode>,
create: Handle<UiNode>,
cancel: Handle<UiNode>,
path_field: Handle<UiNode>,
name_field: Handle<UiNode>,
style_field: Handle<UiNode>,
vcs_field: Handle<UiNode>,
name: String,
style: Style,
vcs: Vcs,
path: PathBuf,
}

fn make_text(text: &str, row: usize, ctx: &mut BuildContext) -> Handle<UiNode> {
Expand All @@ -36,20 +103,30 @@ fn make_text(text: &str, row: usize, ctx: &mut BuildContext) -> Handle<UiNode> {

impl ProjectWizard {
pub fn new(ctx: &mut BuildContext) -> Self {
let name = TextBoxBuilder::new(
let path_field = PathEditorBuilder::new(
WidgetBuilder::new()
.with_height(22.0)
.with_margin(Thickness::uniform(1.0))
.on_row(0)
.on_column(1),
)
.with_path("./")
.build(ctx);

let name_field = TextBoxBuilder::new(
WidgetBuilder::new()
.with_margin(Thickness::uniform(1.0))
.on_row(1)
.on_column(1),
)
.with_text("MyProject")
.build(ctx);

let style = DropdownListBuilder::new(
let style_field = DropdownListBuilder::new(
WidgetBuilder::new()
.with_margin(Thickness::uniform(1.0))
.with_height(22.0)
.on_row(1)
.on_row(2)
.on_column(1),
)
.with_items(vec![
Expand All @@ -59,11 +136,11 @@ impl ProjectWizard {
.with_selected(1)
.build(ctx);

let vcs = DropdownListBuilder::new(
let vcs_field = DropdownListBuilder::new(
WidgetBuilder::new()
.with_margin(Thickness::uniform(1.0))
.with_height(22.0)
.on_row(2)
.on_row(3)
.on_column(1),
)
.with_items(vec![
Expand All @@ -76,13 +153,13 @@ impl ProjectWizard {
.with_selected(1)
.build(ctx);

let ok = make_button("Create", 100.0, 22.0, 0, ctx);
let create = make_button("Create", 100.0, 22.0, 0, ctx);
let cancel = make_button("Cancel", 100.0, 22.0, 0, ctx);
let buttons = StackPanelBuilder::new(
WidgetBuilder::new()
.on_row(1)
.with_horizontal_alignment(HorizontalAlignment::Right)
.with_child(ok)
.with_child(create)
.with_child(cancel),
)
.with_orientation(Orientation::Horizontal)
Expand All @@ -91,16 +168,19 @@ impl ProjectWizard {
let grid = GridBuilder::new(
WidgetBuilder::new()
.on_row(0)
.with_child(make_text("Name", 0, ctx))
.with_child(name)
.with_child(make_text("Style", 1, ctx))
.with_child(style)
.with_child(make_text("Version Control", 2, ctx))
.with_child(vcs),
.with_child(make_text("Path", 0, ctx))
.with_child(path_field)
.with_child(make_text("Name", 1, ctx))
.with_child(name_field)
.with_child(make_text("Style", 2, ctx))
.with_child(style_field)
.with_child(make_text("Version Control", 3, ctx))
.with_child(vcs_field),
)
.add_row(Row::auto())
.add_row(Row::auto())
.add_row(Row::auto())
.add_row(Row::auto())
.add_row(Row::stretch())
.add_column(Column::strict(120.0))
.add_column(Column::stretch())
Expand All @@ -113,7 +193,7 @@ impl ProjectWizard {
.add_column(Column::auto())
.build(ctx);

let window = WindowBuilder::new(WidgetBuilder::new().with_width(300.0).with_height(130.0))
let window = WindowBuilder::new(WidgetBuilder::new().with_width(300.0).with_height(160.0))
.with_content(outer_grid)
.open(false)
.with_title(WindowTitle::text("Project Wizard"))
Expand All @@ -128,6 +208,66 @@ impl ProjectWizard {
))
.unwrap();

Self { window }
Self {
window,
name: "MyProject".to_string(),
style: Style::ThreeD,
vcs: Vcs::Git,
create,
cancel,
path_field,
name_field,
style_field,
vcs_field,
path: Default::default(),
}
}

fn close_and_remove(&self, ui: &UserInterface) {
ui.send_message(WindowMessage::close(
self.window,
MessageDirection::ToWidget,
));
ui.send_message(WidgetMessage::remove(
self.window,
MessageDirection::ToWidget,
));
}

pub fn handle_ui_message(&mut self, message: &UiMessage, ui: &UserInterface) {
if let Some(ButtonMessage::Click) = message.data() {
if message.destination() == self.create {
let _ = fyrox_template_core::init_project(
&self.path,
&self.name,
self.style.as_str(),
self.vcs.as_str(),
true,
);
self.close_and_remove(ui);
} else if message.destination() == self.cancel {
self.close_and_remove(ui);
}
} else if let Some(TextMessage::Text(text)) = message.data() {
if message.direction() == MessageDirection::FromWidget
&& message.destination() == self.name_field
{
self.name.clone_from(text);
}
} else if let Some(DropdownListMessage::SelectionChanged(Some(index))) = message.data() {
if message.direction() == MessageDirection::FromWidget {
if message.destination() == self.style_field {
self.style = Style::from_index(*index);
} else if message.destination() == self.vcs_field {
self.vcs = Vcs::from_index(*index);
}
}
} else if let Some(PathEditorMessage::Path(path)) = message.data() {
if message.destination() == self.path_field
&& message.direction() == MessageDirection::FromWidget
{
self.path.clone_from(path);
}
}
}
}

0 comments on commit 1b36a94

Please sign in to comment.