Skip to content

Commit

Permalink
moved curve editor window into its own plugin
Browse files Browse the repository at this point in the history
- reduces total number of active widgets in the editor by 1200
  • Loading branch information
mrDIMAS committed Nov 20, 2024
1 parent 2235afc commit a95afd9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 79 deletions.
14 changes: 2 additions & 12 deletions editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub mod build;
pub mod camera;
pub mod command;
pub mod configurator;
pub mod curve_editor;
pub mod export;
pub mod gui;
pub mod highlight;
Expand Down Expand Up @@ -65,6 +64,7 @@ pub use fyrox;

use crate::plugins::absm::AbsmEditorPlugin;
use crate::plugins::animation::AnimationEditorPlugin;
use crate::plugins::curve_editor::CurveEditorPlugin;
use crate::plugins::material::MaterialPlugin;
use crate::plugins::ragdoll::RagdollPlugin;
use crate::plugins::settings::SettingsPlugin;
Expand All @@ -76,7 +76,6 @@ use crate::{
camera::panel::CameraPreviewControlPanel,
command::{panel::CommandStackViewer, Command, CommandTrait},
configurator::Configurator,
curve_editor::CurveEditorWindow,
export::ExportWindow,
fyrox::{
asset::{io::FsResourceIo, manager::ResourceManager, untyped::UntypedResource},
Expand Down Expand Up @@ -539,7 +538,6 @@ pub struct Editor {
pub navmesh_panel: NavmeshPanel,
pub settings: Settings,
pub path_fixer: PathFixer,
pub curve_editor: CurveEditorWindow,
pub audio_panel: AudioPanel,
pub mode: Mode,
pub build_window: BuildWindow,
Expand Down Expand Up @@ -640,7 +638,6 @@ impl Editor {
let menu = Menu::new(&mut engine, message_sender.clone(), &settings);
let light_panel = LightPanel::new(&mut engine, message_sender.clone());
let audio_panel = AudioPanel::new(&mut engine, message_sender.clone());

let ctx = &mut engine.user_interfaces.first_mut().build_ctx();
let navmesh_panel = NavmeshPanel::new(scene_viewer.frame(), ctx, message_sender.clone());
let scene_node_context_menu = Rc::new(RefCell::new(SceneNodeContextMenu::new(ctx)));
Expand Down Expand Up @@ -818,13 +815,8 @@ impl Editor {
.build(ctx);

let path_fixer = PathFixer::new(ctx);

let curve_editor = CurveEditorWindow::new(ctx);

let save_scene_dialog = SaveSceneConfirmationDialog::new(ctx);

let build_window = BuildWindow::new(ctx);

if let Some(layout) = settings.windows.layout.as_ref() {
engine
.user_interfaces
Expand Down Expand Up @@ -857,7 +849,6 @@ impl Editor {
validation_message_box,
settings,
path_fixer,
curve_editor,
audio_panel,
save_scene_dialog,
mode: Mode::Edit,
Expand All @@ -882,6 +873,7 @@ impl Editor {
.with(AnimationEditorPlugin::default())
.with(AbsmEditorPlugin::default())
.with(UiStatisticsPlugin::default())
.with(CurveEditorPlugin::default())
.with(inspector_plugin),
// Apparently, some window managers (like Wayland), does not send `Focused` event after the window
// was created. So we must assume that the editor is focused by default, otherwise editor's thread
Expand Down Expand Up @@ -1212,7 +1204,6 @@ impl Editor {
audio_panel: self.audio_panel.window,
configurator_window: self.configurator.window,
path_fixer: self.path_fixer.window,
curve_editor: &self.curve_editor,
command_stack_panel: self.command_stack_viewer.window,
scene_settings: &self.scene_settings,
export_window: &mut self.export_window,
Expand All @@ -1235,7 +1226,6 @@ impl Editor {
self.asset_browser
.handle_ui_message(message, engine, self.message_sender.clone());
self.command_stack_viewer.handle_ui_message(message);
self.curve_editor.handle_ui_message(message, engine);
self.path_fixer.handle_ui_message(
message,
engine.user_interfaces.first_mut(),
Expand Down
3 changes: 1 addition & 2 deletions editor/src/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
send_sync_message,
settings::Settings,
stats::StatisticsWindow,
CurveEditorWindow, Engine, Mode, SceneSettingsWindow,
Engine, Mode, SceneSettingsWindow,
};
use std::path::PathBuf;

Expand Down Expand Up @@ -77,7 +77,6 @@ pub struct Panels<'b> {
pub asset_window: Handle<UiNode>,
pub configurator_window: Handle<UiNode>,
pub path_fixer: Handle<UiNode>,
pub curve_editor: &'b CurveEditorWindow,
pub scene_settings: &'b SceneSettingsWindow,
pub export_window: &'b mut Option<ExportWindow>,
pub statistics_window: &'b mut Option<StatisticsWindow>,
Expand Down
9 changes: 0 additions & 9 deletions editor/src/menu/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ use crate::stats::StatisticsWindow;
pub struct UtilsMenu {
pub menu: Handle<UiNode>,
pub open_path_fixer: Handle<UiNode>,
pub open_curve_editor: Handle<UiNode>,
pub rendering_statistics: Handle<UiNode>,
}

impl UtilsMenu {
pub fn new(ctx: &mut BuildContext) -> Self {
let open_path_fixer;
let open_curve_editor;
let rendering_statistics;
let menu = create_root_menu_item(
"Utils",
Expand All @@ -49,10 +47,6 @@ impl UtilsMenu {
open_path_fixer = create_menu_item("Path Fixer", vec![], ctx);
open_path_fixer
},
{
open_curve_editor = create_menu_item("Curve Editor", vec![], ctx);
open_curve_editor
},
{
rendering_statistics = create_menu_item("Rendering Statistics", vec![], ctx);
rendering_statistics
Expand All @@ -64,7 +58,6 @@ impl UtilsMenu {
Self {
menu,
open_path_fixer,
open_curve_editor,
rendering_statistics,
}
}
Expand All @@ -83,8 +76,6 @@ impl UtilsMenu {
true,
true,
));
} else if message.destination() == self.open_curve_editor {
panels.curve_editor.open(ui);
} else if message.destination() == self.rendering_statistics {
*panels.statistics_window = Some(StatisticsWindow::new(
&mut ui.build_ctx(),
Expand Down
157 changes: 101 additions & 56 deletions editor/src/curve_editor.rs → editor/src/plugins/curve_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,39 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::fyrox::{
asset::{untyped::ResourceKind, Resource},
core::{
color::Color, futures::executor::block_on, math::curve::Curve, pool::Handle,
type_traits::prelude::*, visitor::prelude::*,
},
engine::Engine,
gui::{
border::BorderBuilder,
brush::Brush,
button::{ButtonBuilder, ButtonMessage},
curve::{CurveEditorBuilder, CurveEditorMessage},
file_browser::{FileBrowserMode, FileSelectorMessage},
grid::{Column, GridBuilder, Row},
menu::{MenuBuilder, MenuItemBuilder, MenuItemContent, MenuItemMessage},
message::{MessageDirection, UiMessage},
messagebox::{MessageBoxBuilder, MessageBoxResult},
stack_panel::StackPanelBuilder,
widget::{WidgetBuilder, WidgetMessage},
window::{WindowBuilder, WindowMessage, WindowTitle},
BuildContext, HorizontalAlignment, Orientation, Thickness, UiNode, UserInterface,
},
resource::curve::{CurveResource, CurveResourceState},
};
use crate::{
command::{Command, CommandContext, CommandStack, CommandTrait},
fyrox::{
asset::{untyped::ResourceKind, Resource},
core::{
color::Color, futures::executor::block_on, math::curve::Curve, pool::Handle,
type_traits::prelude::*, visitor::prelude::*,
},
engine::Engine,
gui::{
border::BorderBuilder,
brush::Brush,
button::{ButtonBuilder, ButtonMessage},
curve::{CurveEditorBuilder, CurveEditorMessage},
file_browser::{FileBrowserMode, FileSelectorMessage},
grid::{Column, GridBuilder, Row},
menu::{MenuBuilder, MenuItemBuilder, MenuItemContent, MenuItemMessage},
message::{MessageDirection, UiMessage},
messagebox::{MessageBoxBuilder, MessageBoxResult},
stack_panel::StackPanelBuilder,
widget::{WidgetBuilder, WidgetMessage},
window::{WindowBuilder, WindowMessage, WindowTitle},
BuildContext, HorizontalAlignment, Orientation, Thickness, UiNode, UserInterface,
},
resource::curve::{CurveResource, CurveResourceState},
},
menu::create_menu_item,
plugin::EditorPlugin,
send_sync_message,
utils::create_file_selector,
MessageBoxButtons, MessageBoxMessage, MSG_SYNC_FLAG,
Editor, MessageBoxButtons, MessageBoxMessage, MSG_SYNC_FLAG,
};
use fyrox::core::some_or_return;
use std::{fmt::Debug, path::PathBuf};

#[derive(Debug, ComponentProvider)]
Expand Down Expand Up @@ -277,6 +280,7 @@ impl CurveEditorWindow {
.add_column(Column::stretch())
.build(ctx),
)
.with_remove_on_close(true)
.with_title(WindowTitle::text("Curve Editor"))
.build(ctx);

Expand All @@ -301,9 +305,23 @@ impl CurveEditorWindow {
}
}

fn close(&mut self, ui: &UserInterface) {
self.clear(ui);

fn destroy(self, ui: &UserInterface) {
ui.send_message(WidgetMessage::remove(
self.cancel_message_box,
MessageDirection::ToWidget,
));
ui.send_message(WidgetMessage::remove(
self.save_changes_message_box,
MessageDirection::ToWidget,
));
ui.send_message(WidgetMessage::remove(
self.load_file_selector,
MessageDirection::ToWidget,
));
ui.send_message(WidgetMessage::remove(
self.save_file_selector,
MessageDirection::ToWidget,
));
ui.send_message(WindowMessage::close(
self.window,
MessageDirection::ToWidget,
Expand Down Expand Up @@ -381,27 +399,6 @@ impl CurveEditorWindow {
));
}

fn clear(&mut self, ui: &UserInterface) {
self.path = Default::default();
self.backup = Default::default();
self.command_stack.clear(&mut CurveEditorContext {});
self.curve_resource = None;
self.sync_title(ui);
ui.send_message(WidgetMessage::enabled(
self.curve_editor,
MessageDirection::ToWidget,
false,
));
send_sync_message(
ui,
CurveEditorMessage::sync(
self.curve_editor,
MessageDirection::ToWidget,
Default::default(),
),
);
}

fn revert(&self) {
if let Some(curve_resource) = self.curve_resource.as_ref() {
curve_resource.data_ref().curve = self.backup.clone();
Expand All @@ -423,7 +420,7 @@ impl CurveEditorWindow {
));
}

pub fn handle_ui_message(&mut self, message: &UiMessage, engine: &mut Engine) {
pub fn handle_ui_message(mut self, message: &UiMessage, engine: &mut Engine) -> Option<Self> {
let ui = &engine.user_interfaces.first_mut();

if let Some(ButtonMessage::Click) = message.data() {
Expand All @@ -436,7 +433,8 @@ impl CurveEditorWindow {
None,
));
} else {
self.close(ui);
self.destroy(ui);
return None;
}
} else if message.destination() == self.ok {
if self.modified && self.curve_resource.is_some() {
Expand All @@ -449,10 +447,12 @@ impl CurveEditorWindow {
));
} else {
self.save();
self.close(ui);
self.destroy(ui);
return None;
}
} else {
self.close(ui);
self.destroy(ui);
return None;
}
}
} else if let Some(CurveEditorMessage::Sync(curve)) = message.data() {
Expand Down Expand Up @@ -525,24 +525,69 @@ impl CurveEditorWindow {
match result {
MessageBoxResult::No => {
self.revert();
self.close(ui);
self.destroy(ui);
return None;
}
MessageBoxResult::Yes => {
if self.path == PathBuf::default() {
self.open_save_file_dialog(ui);
} else {
self.save();
self.close(ui);
self.destroy(ui);
return None;
}
}
_ => (),
}
} else if message.destination() == self.cancel_message_box {
if let MessageBoxResult::Yes = result {
self.revert();
self.close(ui);
self.destroy(ui);
return None;
}
}
}

Some(self)
}
}

#[derive(Default)]
pub struct CurveEditorPlugin {
curve_editor_window: Option<CurveEditorWindow>,
open_curve_editor: Handle<UiNode>,
}

impl CurveEditorPlugin {
fn on_open_curve_editor_clicked(&mut self, editor: &mut Editor) {
let ui = editor.engine.user_interfaces.first_mut();
let ctx = &mut ui.build_ctx();
let curve_editor = self
.curve_editor_window
.get_or_insert_with(|| CurveEditorWindow::new(ctx));
curve_editor.open(ui);
}
}

impl EditorPlugin for CurveEditorPlugin {
fn on_start(&mut self, editor: &mut Editor) {
let ui = editor.engine.user_interfaces.first_mut();
let ctx = &mut ui.build_ctx();
self.open_curve_editor = create_menu_item("Curve Editor", vec![], ctx);
ui.send_message(MenuItemMessage::add_item(
editor.menu.utils_menu.menu,
MessageDirection::ToWidget,
self.open_curve_editor,
));
}

fn on_ui_message(&mut self, message: &mut UiMessage, editor: &mut Editor) {
if let Some(MenuItemMessage::Click) = message.data() {
if message.destination() == self.open_curve_editor {
self.on_open_curve_editor_clicked(editor)
}
}
let curve_editor = some_or_return!(self.curve_editor_window.take());
self.curve_editor_window = curve_editor.handle_ui_message(message, &mut editor.engine);
}
}
1 change: 1 addition & 0 deletions editor/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
pub mod absm;
pub mod animation;
pub mod collider;
pub mod curve_editor;
pub mod material;
pub mod ragdoll;
pub mod settings;
Expand Down

0 comments on commit a95afd9

Please sign in to comment.