Skip to content

Commit

Permalink
Merge branch 'dev' for version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Rechdan committed Dec 31, 2023
2 parents 10e0c7c + ac950da commit fddff92
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 247 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.0.6"
version = "0.1.0"
description = "W2.Rust is a set of tools and GameServer made using the Rust language."
readme = "README.md"
repository = "https://github.com/Rechdan/W2.Rust"
Expand Down
118 changes: 8 additions & 110 deletions apps/editors/src/editors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,13 @@
use egui::{Button, Ui};
use egui_extras::{Size, StripBuilder};
use rfd::FileDialog;
use std::{path::PathBuf, sync::Mutex};

use self::server_list::ServerListEditor;
use egui::Ui;
use std::path::PathBuf;

pub mod server_list;
pub mod server_name;

#[derive(Default, Clone)]
enum EditorSelected {
#[default]
None,
ServerList(ServerListEditor),
}

#[derive(Default)]
pub struct Editors {
folder_selected: Mutex<Option<PathBuf>>,
editor_selected: Mutex<EditorSelected>,
pub trait EditorRender {
fn name() -> &'static str;
fn new(folder: PathBuf) -> Option<Box<Self>>;
fn render(&mut self, ui: &mut Ui);
}

impl Editors {
pub fn render(&self, ui: &mut Ui) {
let folder_selected = self.folder_selected.lock().unwrap().clone();

match folder_selected.clone() {
Some(folder) => {
self.render_selected_folder(ui, folder.clone());
self.render_body(ui, folder.clone());
}

None => {
if ui.button("Selecionar pasta do cliente").clicked() {
self.pick_new_folder();
}
}
};
}

fn pick_new_folder(&self) {
match FileDialog::new().pick_folder() {
Some(new_folder) => {
*self.folder_selected.lock().unwrap() = Some(new_folder);
*self.editor_selected.lock().unwrap() = EditorSelected::None;
}
None => {}
};
}

fn render_selected_folder(&self, ui: &mut Ui, folder: PathBuf) {
ui.horizontal(|ui| {
ui.label("Pasta:");

if ui.link(folder.display().to_string()).clicked() {
self.pick_new_folder();
}
});
}

fn render_body(&self, ui: &mut Ui, folder: PathBuf) {
StripBuilder::new(ui)
.size(Size::exact(200.0))
.size(Size::remainder())
.horizontal(|mut strip| {
strip.cell(|ui| {
ui.group(|ui| {
ui.set_min_size(ui.available_size());

self.render_editors_list(ui, folder.clone());
});
});

strip.cell(|ui| {
ui.group(|ui| {
ui.set_min_size(ui.available_size());

match self.editor_selected.lock().unwrap().clone() {
EditorSelected::None => {
ui.label("Selecione um editor ao lado");
}

EditorSelected::ServerList(server_list) => {
server_list.render(ui);
}
};
});
});
});
}

fn render_editors_list(&self, ui: &mut Ui, folder: PathBuf) {
ui.vertical_centered_justified(|ui| {
self.render_server_list_editor_button(ui, folder);
});
}

fn render_server_list_editor_button(&self, ui: &mut Ui, folder: PathBuf) {
let mut editor_selected = self.editor_selected.lock().unwrap();

let selected = match *editor_selected {
EditorSelected::ServerList(_) => true,
_ => false,
};

let btn = ui.add(Button::new("Server List + SN").selected(selected));

if btn.clicked() && !selected {
match ServerListEditor::new(folder.clone()) {
Some(server_list) => *editor_selected = EditorSelected::ServerList(server_list),
None => {}
};
}
}
}
pub struct Editor {}
156 changes: 39 additions & 117 deletions apps/editors/src/editors/server_list.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,47 @@
use egui::{vec2, Align, Button, Layout, Ui};
use egui_extras::{Size, StripBuilder};
use egui::{Button, Ui};
use fixedstr::zstr;
use std::{
path::PathBuf,
str::FromStr,
sync::{Arc, Mutex},
};
use std::{path::PathBuf, str::FromStr};

use crate::structs::{ServerList, ServerName};
use crate::structs::ServerList;

use super::EditorRender;

#[derive(Clone)]
pub struct ServerListEditor {
folder: Arc<PathBuf>,
server_list: Arc<Mutex<ServerList>>,
server_name: Arc<Mutex<ServerName>>,
selected_world_index: Arc<Mutex<usize>>,
buttons_height: Arc<Mutex<f32>>,
folder: PathBuf,
server_list: ServerList,
selected_world_index: usize,
}

impl ServerListEditor {
pub fn new(folder: PathBuf) -> Option<Self> {
impl EditorRender for ServerListEditor {
fn name() -> &'static str {
"Server List"
}

fn new(folder: PathBuf) -> Option<Box<Self>> {
let server_list = match ServerList::new(folder.clone()) {
Some(server_list) => server_list,
None => return None,
};

let server_name = match ServerName::new(folder.clone()) {
Some(server_name) => server_name,
None => return None,
};

Some(Self {
folder: Arc::new(folder),
server_list: Arc::new(Mutex::new(server_list)),
server_name: Arc::new(Mutex::new(server_name)),
selected_world_index: Default::default(),
buttons_height: Default::default(),
})
Some(Box::new(Self {
folder,
server_list,
selected_world_index: 0,
}))
}

pub fn render(&self, ui: &mut Ui) {
StripBuilder::new(ui)
.size(Size::relative(0.5))
.size(Size::remainder())
.horizontal(|mut s| {
s.cell(|ui| {
ui.label("Server List");
ui.separator();
self.render_key_editor(ui);
self.render_world_selector(ui);
self.render_world_editor(ui);
self.footer_actions_render(ui);
});

s.cell(|ui| {
ui.label("Server Name (SN)");
ui.separator();
self.server_name_editor(ui);
});
});
fn render(&mut self, ui: &mut Ui) {
ui.set_width(240.0);
self.render_key_editor(ui);
self.render_world_selector(ui);
self.render_world_editor(ui);
self.footer_actions(ui);
}
}

fn render_key_editor(&self, ui: &mut Ui) {
let mut server_list = self.server_list.lock().unwrap();
impl ServerListEditor {
fn render_key_editor(&mut self, ui: &mut Ui) {
let server_list = &mut self.server_list;

ui.vertical(|ui| {
ui.label("Key:");
Expand All @@ -77,8 +56,8 @@ impl ServerListEditor {
});
}

fn render_world_selector(&self, ui: &mut Ui) {
let mut selected_world_index = self.selected_world_index.lock().unwrap();
fn render_world_selector(&mut self, ui: &mut Ui) {
let selected_world_index = &mut self.selected_world_index;

ui.vertical(|ui| {
ui.label("Mundo:");
Expand All @@ -98,9 +77,9 @@ impl ServerListEditor {
});
}

fn render_world_editor(&self, ui: &mut Ui) {
let mut server_list = self.server_list.lock().unwrap();
let selected_world_index = self.selected_world_index.lock().unwrap().clone();
fn render_world_editor(&mut self, ui: &mut Ui) {
let server_list = &mut self.server_list;
let selected_world_index = self.selected_world_index;

let (world_url, world_channels) = server_list.worlds.get_mut(selected_world_index).unwrap();

Expand All @@ -124,68 +103,11 @@ impl ServerListEditor {
}
}

fn server_name_editor(&self, ui: &mut Ui) {
let mut server_name = self.server_name.lock().unwrap();

StripBuilder::new(ui)
.size(Size::relative(0.5))
.size(Size::remainder())
.horizontal(|mut s| {
s.cell(|ui| {
for i in 0..10 {
let world_name = &mut server_name.worlds[i].clone();

ui.horizontal(|ui| {
let name = &mut world_name.to_string();
if ui.text_edit_singleline(name).changed() {
server_name.worlds[i] = zstr::from(name);
}
});
}
});

s.cell(|ui| {
for i in 0..10 {
let world_count = &mut server_name.counts[i][0].clone();

ui.horizontal(|ui| {
let count = &mut world_count.to_string();
if ui.text_edit_singleline(count).changed() {
server_name.counts[i][0] = match FromStr::from_str(count) {
Ok(v) => v,
Err(_error) => world_count.clone(),
};
}
});
}
});
});
}

fn footer_actions_render(&self, ui: &mut Ui) {
let folder = (*self.folder).clone();
let server_list = self.server_list.lock().unwrap();
let server_name = self.server_name.lock().unwrap();
let mut buttons_height = self.buttons_height.lock().unwrap();

let min_size = vec2(
ui.available_width(),
ui.available_height().max(*buttons_height),
);

ui.allocate_ui_with_layout(min_size.clone(), Layout::bottom_up(Align::Min), |ui| {
ui.set_min_size(min_size.clone());

*buttons_height = ui
.horizontal_wrapped(|ui| {
if ui.button("Salvar").clicked() {
server_list.save(folder.clone());
server_name.save(folder.clone());
}
})
.response
.rect
.height();
fn footer_actions(&mut self, ui: &mut Ui) {
ui.horizontal_wrapped(|ui| {
if ui.button("Salvar").clicked() {
self.server_list.save(self.folder.clone());
}
});
}
}
Loading

0 comments on commit fddff92

Please sign in to comment.