use crate::engine::ecs::ComponentId;
use crate::engine::ecs::IntentValue;
use crate::engine::ecs::SignalEmitter;
use crate::engine::ecs::component::Component;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransformGizmoCoordSpace {
Local,
World,
}
impl Default for TransformGizmoCoordSpace {
fn default() -> Self {
Self::World
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditorInteractionMode {
Select,
Cursor3d,
SelectAndCursor,
}
impl Default for EditorInteractionMode {
fn default() -> Self {
Self::Select
}
}
#[derive(Debug, Clone, Copy)]
pub struct EditorComponent {
pub active: bool,
pub transform_gizmo: Option<ComponentId>,
pub selected: Option<ComponentId>,
pub interaction_mode: EditorInteractionMode,
pub transform_gizmo_translation_space: TransformGizmoCoordSpace,
pub transform_gizmo_rotation_space: TransformGizmoCoordSpace,
pub spawn_panels: bool,
pub serialize_editor_panels: bool,
pub world_panel_pos: (f32, f32, f32),
pub inspector_panel_pos: (f32, f32, f32),
component: Option<ComponentId>,
}
impl Default for EditorComponent {
fn default() -> Self {
Self {
active: false,
transform_gizmo: None,
selected: None,
interaction_mode: EditorInteractionMode::Select,
transform_gizmo_translation_space: TransformGizmoCoordSpace::World,
transform_gizmo_rotation_space: TransformGizmoCoordSpace::Local,
spawn_panels: true,
serialize_editor_panels: false,
world_panel_pos: (-0.7, 1.6, -1.2),
inspector_panel_pos: (-0.7, 1.6, -1.2),
component: None,
}
}
}
impl EditorComponent {
pub fn new() -> Self {
Self::default()
}
pub fn with_active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn with_transform_gizmo_translation_space(
mut self,
space: TransformGizmoCoordSpace,
) -> Self {
self.transform_gizmo_translation_space = space;
self
}
pub fn with_transform_gizmo_rotation_space(mut self, space: TransformGizmoCoordSpace) -> Self {
self.transform_gizmo_rotation_space = space;
self
}
pub fn with_interaction_mode(mut self, mode: EditorInteractionMode) -> Self {
self.interaction_mode = mode;
self
}
pub fn with_panels(mut self, enabled: bool) -> Self {
self.spawn_panels = enabled;
self
}
pub fn with_serialize_editor_panels(mut self, enabled: bool) -> Self {
self.serialize_editor_panels = enabled;
self
}
pub fn with_panel_positions(
mut self,
world_panel: (f32, f32, f32),
inspector_panel: (f32, f32, f32),
) -> Self {
self.world_panel_pos = world_panel;
self.inspector_panel_pos = inspector_panel;
self
}
pub fn id(&self) -> Option<ComponentId> {
self.component
}
}
impl Component for EditorComponent {
fn set_id(&mut self, component: ComponentId) {
self.component = Some(component);
}
fn name(&self) -> &'static str {
"editor"
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn init(&mut self, emit: &mut dyn SignalEmitter, component: ComponentId) {
emit.push_intent_now(
component,
IntentValue::RegisterEditor {
component_ids: vec![component],
},
);
}
fn to_mms_ast(
&self,
_world: &crate::engine::ecs::World,
) -> crate::scripting::ast::ComponentExpression {
use crate::engine::ecs::component::ce_helpers::*;
let translation = match self.transform_gizmo_translation_space {
TransformGizmoCoordSpace::Local => "local",
TransformGizmoCoordSpace::World => "world",
};
let rotation = match self.transform_gizmo_rotation_space {
TransformGizmoCoordSpace::Local => "local",
TransformGizmoCoordSpace::World => "world",
};
let interaction_mode = match self.interaction_mode {
EditorInteractionMode::Select => "select",
EditorInteractionMode::Cursor3d => "cursor_3d",
EditorInteractionMode::SelectAndCursor => "select_cursor",
};
let mut expr = ce("Editor")
.with_call("interaction_mode", vec![s(interaction_mode)])
.with_call("translation_space", vec![s(translation)])
.with_call("rotation_space", vec![s(rotation)]);
if self.active {
expr = expr.with_call("active", vec![]);
}
expr
}
}