bevy_gearbox_editor 0.1.0

State machine system for the bevy game engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Editor state management, events, and shared types

use bevy::prelude::*;
use bevy_gearbox::InitialState;
use bevy_gearbox::active::Active;
use egui::Pos2;
use std::collections::HashMap;

use crate::components::NodeType;

/// State for managing text editing (renaming nodes)
#[derive(Default)]
pub struct TextEditingState {
    /// Entity currently being renamed
    pub editing_entity: Option<Entity>,
    /// Current text being edited
    pub current_text: String,
    /// Whether the text field should be focused
    pub should_focus: bool,
    /// Whether this is the first focus (to trigger select all)
    pub first_focus: bool,
}

impl TextEditingState {
    /// Start editing an entity's name
    pub fn start_editing(&mut self, entity: Entity, current_name: &str) {
        self.editing_entity = Some(entity);
        self.current_text = current_name.to_string();
        self.should_focus = true;
        self.first_focus = true;
    }
    
    /// Stop editing and return the final text if editing was active
    pub fn stop_editing(&mut self) -> Option<(Entity, String)> {
        if let Some(entity) = self.editing_entity {
            let text = self.current_text.clone();
            self.editing_entity = None;
            self.current_text.clear();
            self.should_focus = false;
            self.first_focus = false;
            Some((entity, text))
        } else {
            None
        }
    }
    
    /// Cancel editing without saving
    pub fn cancel_editing(&mut self) {
        self.editing_entity = None;
        self.current_text.clear();
        self.should_focus = false;
        self.first_focus = false;
    }
    
    /// Check if currently editing a specific entity
    pub fn is_editing(&self, entity: Entity) -> bool {
        self.editing_entity == Some(entity)
    }
}

/// State for managing transition creation workflow
#[derive(Default)]
pub struct TransitionCreationState {
    /// Source entity for the transition being created
    pub source_entity: Option<Entity>,
    /// Whether we're waiting for target selection
    pub awaiting_target_selection: bool,
    /// Position where the event type dropdown should appear
    pub dropdown_position: Option<Pos2>,
    /// Target entity that was selected
    pub target_entity: Option<Entity>,
    /// Whether the event type dropdown is open
    pub show_event_dropdown: bool,
    /// Available event types for TransitionListener
    pub available_event_types: Vec<String>,
}

impl TransitionConnection {
    /// Calculate connection points for the two-segment approach
    /// Returns (source_to_event_start, source_to_event_end, event_to_target_start, event_to_target_end)
    pub fn calculate_two_segment_points(&self) -> (egui::Pos2, egui::Pos2, egui::Pos2, egui::Pos2) {
        // Source to event node
        let source_to_event_start = closest_point_on_rect_edge(self.source_rect, self.event_node_position);
        let source_to_event_end = self.event_node_position;
        
        // Event node to target
        let event_to_target_start = self.event_node_position;
        let event_to_target_end = closest_point_on_rect_edge(self.target_rect, self.event_node_position);
        
        (source_to_event_start, source_to_event_end, event_to_target_start, event_to_target_end)
    }
    
    /// Get the event node position
    pub fn get_event_node_position(&self) -> egui::Pos2 {
        self.event_node_position
    }
    
    /// Get the pill-shaped event node rectangle for interaction
    pub fn get_event_node_rect(&self, text_size: egui::Vec2) -> egui::Rect {
        let padding = egui::Vec2::new(12.0, 6.0);
        let pill_size = text_size + padding * 2.0;
        egui::Rect::from_center_size(self.event_node_position, pill_size)
    }
    
    /// Update the event node position based on current source/target positions and stored offset
    pub fn update_event_node_position(&mut self) {
        let midpoint = egui::Pos2::new(
            (self.source_rect.center().x + self.target_rect.center().x) / 2.0,
            (self.source_rect.center().y + self.target_rect.center().y) / 2.0,
        );
        self.event_node_position = midpoint + self.event_node_offset;
    }
    
    /// Update the offset based on current event node position relative to source/target midpoint
    pub fn update_event_node_offset(&mut self) {
        let midpoint = egui::Pos2::new(
            (self.source_rect.center().x + self.target_rect.center().x) / 2.0,
            (self.source_rect.center().y + self.target_rect.center().y) / 2.0,
        );
        self.event_node_offset = self.event_node_position - midpoint;
    }
}

impl TransitionCreationState {
    /// Start creating a transition from the given source entity
    pub fn start_transition(&mut self, source: Entity) {
        self.source_entity = Some(source);
        self.awaiting_target_selection = true;
        self.target_entity = None;
        self.show_event_dropdown = false;
        self.dropdown_position = None;
    }
    
    /// Set the target entity and prepare for event type selection
    pub fn set_target(&mut self, target: Entity, dropdown_pos: Pos2) {
        self.target_entity = Some(target);
        self.awaiting_target_selection = false;
        self.show_event_dropdown = true;
        self.dropdown_position = Some(dropdown_pos);
    }
    
    /// Cancel the current transition creation
    pub fn cancel(&mut self) {
        *self = Default::default();
    }
    
    /// Complete the transition creation
    pub fn complete(&mut self) {
        *self = Default::default();
    }
    
    /// Check if we're currently creating a transition
    pub fn is_active(&self) -> bool {
        self.source_entity.is_some()
    }
}

/// Component that holds persistent state machine editor data
/// This lives on the root state machine entity and should be saved/loaded
#[derive(Component, Default)]
pub struct StateMachinePersistentData {
    /// Map of entity to its UI node representation (positions, sizes, etc.)
    pub nodes: HashMap<Entity, NodeType>,
    /// Visual transitions with custom layouts (draggable event nodes)
    pub visual_transitions: Vec<TransitionConnection>,
}

/// Component that holds transient state machine editor data
/// This is temporary UI state that should not be persisted
#[derive(Component, Default)]
pub struct StateMachineTransientData {
    /// Currently selected node for z-ordering
    pub selected_node: Option<Entity>,
    /// Transition creation state
    pub transition_creation: TransitionCreationState,
    /// Text editing state for renaming nodes
    pub text_editing: TextEditingState,
    /// Active transition pulses for visual feedback
    pub transition_pulses: Vec<TransitionPulse>,
}

/// Resource that holds the editor's UI/window state
/// This manages which state machine is being edited in each window
#[derive(Resource, Default)]
pub struct EditorState {
    /// Currently selected state machine root entity being edited
    pub selected_machine: Option<Entity>,
    /// Entity for which a context menu is requested
    pub context_menu_entity: Option<Entity>,
    /// Position where the context menu should appear
    pub context_menu_position: Option<Pos2>,
    /// Transition for which a context menu is requested
    pub transition_context_menu: Option<(Entity, Entity, String)>, // (source, target, event_type)
    /// Position where the transition context menu should appear
    pub transition_context_menu_position: Option<Pos2>,
    /// Entity currently being inspected
    pub inspected_entity: Option<Entity>,
    /// Current inspector tab
    pub inspector_tab: InspectorTab,
    /// Component addition UI state
    pub component_addition: ComponentAdditionState,
}

/// Inspector tabs
#[derive(Debug, Clone, PartialEq)]
pub enum InspectorTab {
    Inspect,
    Remove,
    Add,
}

impl Default for InspectorTab {
    fn default() -> Self {
        Self::Inspect
    }
}

/// State for the component addition UI
#[derive(Debug, Default)]
pub struct ComponentAdditionState {
    /// Search text for filtering components
    pub search_text: String,
    /// Whether the dropdown is open
    pub dropdown_open: bool,
    /// Hierarchical component organization (cached)
    pub component_hierarchy: Option<crate::entity_inspector::ComponentHierarchy>,
    /// Expanded state for each namespace
    pub expanded_namespaces: std::collections::HashSet<String>,
}

impl ComponentAdditionState {
    /// Update the component hierarchy
    pub fn update_hierarchy(&mut self, hierarchy: crate::entity_inspector::ComponentHierarchy) {
        self.component_hierarchy = Some(hierarchy);
    }

    /// Toggle expansion state for a namespace path
    pub fn toggle_namespace(&mut self, namespace_path: &str) {
        if self.expanded_namespaces.contains(namespace_path) {
            self.expanded_namespaces.remove(namespace_path);
        } else {
            self.expanded_namespaces.insert(namespace_path.to_string());
        }
    }

    /// Check if a namespace is expanded
    pub fn is_namespace_expanded(&self, namespace_path: &str) -> bool {
        self.expanded_namespaces.contains(namespace_path)
    }
}

impl EditorState {
    /// Get the currently selected state machine entity
    pub fn current_machine(&self) -> Option<Entity> {
        self.selected_machine
    }
    
    /// Set the currently selected state machine
    pub fn set_current_machine(&mut self, entity: Option<Entity>) {
        self.selected_machine = entity;
    }
}

/// Component marking an entity as an editor window
#[derive(Component)]
pub struct EditorWindow;

/// Event fired when a context menu is requested for a node
#[derive(Event)]
pub struct NodeContextMenuRequested {
    pub entity: Entity,
    pub position: Pos2,
}

/// Event fired when a context menu is requested for a transition
#[derive(Event)]
pub struct TransitionContextMenuRequested {
    pub source_entity: Entity,
    pub target_entity: Entity,
    pub event_type: String,
    pub position: Pos2,
}

/// Available actions that can be performed on nodes
#[derive(Debug, Clone)]
pub enum NodeAction {
    Inspect,
    AddChild,
    Rename,
    SetAsInitialState,
    Delete,
}

/// Event fired when a node action is triggered
#[derive(Event)]
pub struct NodeActionTriggered {
    pub entity: Entity,
    pub action: NodeAction,
}

/// Event fired when a node is dragged
#[derive(Event, Debug)]
pub struct NodeDragged {
    pub entity: Entity,
    pub drag_delta: egui::Vec2,
}

/// Event fired when a transition creation is requested (+ button clicked)
#[derive(Event)]
pub struct TransitionCreationRequested {
    pub source_entity: Entity,
}

/// Event fired when a transition should be created with the selected event type
#[derive(Event)]
pub struct CreateTransition {
    pub source_entity: Entity,
    pub target_entity: Entity,
    pub event_type: String,
}

/// Event fired when a state machine should be saved
#[derive(Event)]
pub struct SaveStateMachine {
    pub entity: Entity,
}

/// Event fired when a transition should be deleted
#[derive(Event)]
pub struct DeleteTransition {
    pub source_entity: Entity,
    pub target_entity: Entity,
    pub event_type: String,
}

/// Event fired when a node should be deleted
#[derive(Event)]
pub struct DeleteNode {
    pub entity: Entity,
}

/// Data to track transition pulse animation
#[derive(Clone)]
pub struct TransitionPulse {
    pub source_entity: Entity,
    pub target_entity: Entity,
    pub timer: Timer,
}

impl TransitionPulse {
    pub fn new(source_entity: Entity, target_entity: Entity) -> Self {
        Self {
            source_entity,
            target_entity,
            timer: Timer::from_seconds(0.4, TimerMode::Once),
        }
    }
    
    /// Get the current pulse intensity (1.0 at start, 0.0 at end)
    pub fn intensity(&self) -> f32 {
        1.0 - self.timer.fraction()
    }
}

/// Colors for visual feedback
pub const ACTIVE_STATE_COLOR: egui::Color32 = egui::Color32::from_rgb(255, 215, 0); // Gold
pub const NORMAL_NODE_COLOR: egui::Color32 = egui::Color32::from_rgb(60, 60, 60); // Dark grey
pub const TRANSITION_COLOR: egui::Color32 = egui::Color32::WHITE;

/// Calculate the color for a node based on its state
pub fn get_node_color(entity: Entity, active_query: &Query<&Active>) -> egui::Color32 {
    if active_query.contains(entity) {
        ACTIVE_STATE_COLOR
    } else {
        NORMAL_NODE_COLOR
    }
}

/// Calculate the color for a transition line/pill based on pulse state
pub fn get_transition_color(source: Entity, target: Entity, pulses: &[TransitionPulse]) -> egui::Color32 {
    // Base grey color for transitions (same as normal nodes)
    let base_transition_color = NORMAL_NODE_COLOR;
    
    // Find if there's an active pulse for this transition
    if let Some(pulse) = pulses.iter().find(|p| p.source_entity == source && p.target_entity == target) {
        let intensity = pulse.intensity();
        // Lerp between normal grey and gold based on pulse intensity
        lerp_color(base_transition_color, ACTIVE_STATE_COLOR, intensity)
    } else {
        base_transition_color
    }
}

/// Linear interpolation between two colors
fn lerp_color(from: egui::Color32, to: egui::Color32, t: f32) -> egui::Color32 {
    let t = t.clamp(0.0, 1.0);
    egui::Color32::from_rgb(
        ((from.r() as f32) * (1.0 - t) + (to.r() as f32) * t) as u8,
        ((from.g() as f32) * (1.0 - t) + (to.g() as f32) * t) as u8,
        ((from.b() as f32) * (1.0 - t) + (to.b() as f32) * t) as u8,
    )
}

/// Draw an interactive pill-shaped label for transition events
pub fn draw_interactive_pill_label(
    ui: &mut egui::Ui,
    position: egui::Pos2,
    text: &str,
    font_id: egui::FontId,
    is_dragging: bool,
    color: egui::Color32,
) -> egui::Response {
    // Calculate text dimensions
    let galley = ui.fonts(|f| f.layout_no_wrap(text.to_string(), font_id, egui::Color32::WHITE));
    let text_size = galley.size();
    
    // Calculate pill size with padding
    let padding = egui::Vec2::new(8.0, 4.0);
    let pill_size = text_size + padding * 2.0;
    
    // Create the pill rectangle centered on the position
    let pill_rect = egui::Rect::from_center_size(position, pill_size);
    
    // Handle interaction (including right-click for context menu)
    let response = ui.allocate_rect(pill_rect, egui::Sense::click_and_drag());
    
    // Draw the pill
    let painter = ui.painter();
    
    // Use the provided color, with slight modification for dragging state
    let bg_color = if is_dragging {
        // Lighten the color when dragging
        egui::Color32::from_rgb(
            (color.r() as f32 * 1.2).min(255.0) as u8,
            (color.g() as f32 * 1.2).min(255.0) as u8,
            (color.b() as f32 * 1.2).min(255.0) as u8,
        )
    } else {
        color
    };
    
    painter.rect_filled(
        pill_rect,
        egui::CornerRadius::same((pill_size.y / 2.0) as u8),
        bg_color,
    );
    
    // Draw border
    painter.rect_stroke(
        pill_rect,
        egui::CornerRadius::same((pill_size.y / 2.0) as u8),
        egui::Stroke::new(1.0, egui::Color32::WHITE),
        egui::StrokeKind::Outside,
    );
    
    // Draw text
    let text_pos = pill_rect.center() - text_size * 0.5;
    painter.galley(text_pos, galley, egui::Color32::WHITE);
    
    response
}





/// Item to be rendered in the node editor, with z-order information
pub struct RenderItem {
    pub entity: Entity,
    pub z_order: i32,
}

/// Visual representation of a transition connection
#[derive(Debug, Clone)]
pub struct TransitionConnection {
    pub source_entity: Entity,
    pub edge_entity: Entity,
    pub target_entity: Entity,
    pub event_type: String,
    pub source_rect: egui::Rect,
    pub target_rect: egui::Rect,
    pub event_node_position: egui::Pos2,
    pub is_dragging_event_node: bool,
    /// Offset from the midpoint between source and target nodes
    pub event_node_offset: egui::Vec2,
}

/// Get a human-readable name for an entity
pub fn get_entity_name(entity: Entity, all_entities: &Query<(Entity, Option<&Name>, Option<&InitialState>)>) -> String {
    if let Ok((_, name_opt, _)) = all_entities.get(entity) {
        if let Some(name) = name_opt {
            name.as_str().to_string()
        } else {
            format!("Entity {:?}", entity)
        }
    } else {
        format!("Unknown Entity {:?}", entity)
    }
}

/// Get a human-readable name for an entity using world access
pub fn get_entity_name_from_world(entity: Entity, world: &mut World) -> String {
    let mut query = world.query::<(Entity, Option<&Name>)>();
    if let Ok((_, name_opt)) = query.get(world, entity) {
        if let Some(name) = name_opt {
            name.as_str().to_string()
        } else {
            format!("Entity {:?}", entity)
        }
    } else {
        format!("Unknown Entity {:?}", entity)
    }
}

/// Determine if an entity should get a selection boost for z-ordering
pub fn should_get_selection_boost(
    entity: Entity,
    selected_node: Option<Entity>,
    child_of_query: &Query<&bevy_gearbox::StateChildOf>,
) -> bool {
    if let Some(selected) = selected_node {
        if entity == selected {
            return true;
        }
        
        // Check if this entity is an ancestor of the selected node
        let mut current = selected;
        while let Ok(child_of) = child_of_query.get(current) {
            if child_of.0 == entity {
                return true;
            }
            current = child_of.0;
        }
    }
    false
}

/// Find the closest point on a rectangle's edge to a given point
pub fn closest_point_on_rect_edge(rect: egui::Rect, point: egui::Pos2) -> egui::Pos2 {
    let center = rect.center();
    let direction = point - center;
    
    // Calculate intersection with rectangle edges
    let t_left = if direction.x != 0.0 { (rect.min.x - center.x) / direction.x } else { f32::INFINITY };
    let t_right = if direction.x != 0.0 { (rect.max.x - center.x) / direction.x } else { f32::INFINITY };
    let t_top = if direction.y != 0.0 { (rect.min.y - center.y) / direction.y } else { f32::INFINITY };
    let t_bottom = if direction.y != 0.0 { (rect.max.y - center.y) / direction.y } else { f32::INFINITY };
    
    // Find the smallest positive t value
    let mut min_t = f32::INFINITY;
    if t_left > 0.0 { min_t = min_t.min(t_left); }
    if t_right > 0.0 { min_t = min_t.min(t_right); }
    if t_top > 0.0 { min_t = min_t.min(t_top); }
    if t_bottom > 0.0 { min_t = min_t.min(t_bottom); }
    
    if min_t == f32::INFINITY {
        // Fallback to center if no intersection found
        center
    } else {
        center + direction * min_t
    }
}

/// Draw an arrow from start to end point
pub fn draw_arrow(painter: &egui::Painter, start: egui::Pos2, end: egui::Pos2, color: egui::Color32) {
    let stroke = egui::Stroke::new(2.0, color);
    
    // Draw the main line
    painter.line_segment([start, end], stroke);
    
    // Calculate arrow head
    let direction = (end - start).normalized();
    let arrow_length = 8.0;
    let arrow_angle = std::f32::consts::PI / 6.0; // 30 degrees
    
    let arrow_point1 = end - direction * arrow_length;
    let perpendicular = egui::Vec2::new(-direction.y, direction.x);
    
    let arrow_head1 = arrow_point1 + perpendicular * arrow_length * arrow_angle.sin();
    let arrow_head2 = arrow_point1 - perpendicular * arrow_length * arrow_angle.sin();
    
    // Draw arrow head
    painter.line_segment([end, arrow_head1], stroke);
    painter.line_segment([end, arrow_head2], stroke);
}