1+ import { Node } from "../rendering/types/node" ;
2+ import { vec3 } from "../math/types" ;
3+ import type { Line } from "../math/types" ;
4+ import { Inspector } from "./inspector" ;
5+ import { create_arrow } from "../rendering/utils/primitives" ;
6+ import { Mesh } from "../rendering/types/mesh" ;
7+ import type { Scene } from "../rendering/types/scene" ;
8+
9+ export enum EditorMode {
10+ IDLE ,
11+ TRANSLATE_X ,
12+ TRANSLATE_Y ,
13+ TRANSLATE_Z
14+ }
15+
16+ export class EditorState {
17+ public mode : EditorMode = EditorMode . IDLE ;
18+ public selected_node : Node | null = null ;
19+
20+ public gizmo_x ! : Node ;
21+ public gizmo_y ! : Node ;
22+ public gizmo_z ! : Node ;
23+
24+ private inspector : Inspector ;
25+
26+ constructor ( inspector : Inspector , scene :Scene ) {
27+ this . inspector = inspector ;
28+ this . setup_gizmos ( scene ) ;
29+ }
30+
31+ private setup_gizmos ( scene :Scene ) {
32+ const arrow_geo = create_arrow ( ) ;
33+
34+ const mesh_z = scene . add_mesh ( arrow_geo , vec3 ( 0 , 0 , 1 ) , 0 ) ;
35+ this . gizmo_z = new Node ( mesh_z ) ;
36+
37+ const mesh_x = scene . add_mesh ( arrow_geo , vec3 ( 1 , 0 , 0 ) , 0 ) ;
38+ this . gizmo_x = new Node ( mesh_x ) ;
39+ this . gizmo_x . rotation [ 1 ] = Math . PI / 2 ;
40+
41+ const mesh_y = scene . add_mesh ( arrow_geo , vec3 ( 0 , 1 , 0 ) , 0 ) ;
42+ this . gizmo_y = new Node ( mesh_y ) ;
43+ this . gizmo_y . rotation [ 0 ] = - Math . PI / 2 ;
44+
45+ this . gizmo_x . update_matrix ( ) ;
46+ this . gizmo_y . update_matrix ( ) ;
47+ this . gizmo_z . update_matrix ( ) ;
48+ }
49+
50+ public select ( node : Node | null ) {
51+ this . selected_node = node ;
52+ this . inspector . inspect ( node ) ;
53+ this . update_gizmo_transforms ( ) ;
54+ }
55+
56+ public update_gizmo_transforms ( ) {
57+ if ( ! this . selected_node ) return ;
58+ const pos = this . selected_node . position ;
59+
60+ this . gizmo_x . position = vec3 ( pos [ 0 ] , pos [ 1 ] , pos [ 2 ] ) ;
61+ this . gizmo_y . position = vec3 ( pos [ 0 ] , pos [ 1 ] , pos [ 2 ] ) ;
62+ this . gizmo_z . position = vec3 ( pos [ 0 ] , pos [ 1 ] , pos [ 2 ] ) ;
63+
64+ this . gizmo_x . update_matrix ( ) ;
65+ this . gizmo_y . update_matrix ( ) ;
66+ this . gizmo_z . update_matrix ( ) ;
67+ }
68+
69+ public process_raycast ( ray : Line , scene_nodes : Node [ ] ) {
70+ if ( this . selected_node ) {
71+ if ( this . gizmo_x . intersects_with ( ray ) ) {
72+ this . mode = EditorMode . TRANSLATE_X ;
73+ return ;
74+ }
75+ if ( this . gizmo_y . intersects_with ( ray ) ) {
76+ this . mode = EditorMode . TRANSLATE_Y ;
77+ return ;
78+ }
79+ if ( this . gizmo_z . intersects_with ( ray ) ) {
80+ this . mode = EditorMode . TRANSLATE_Z ;
81+ return ;
82+ }
83+ }
84+
85+ for ( const node of scene_nodes ) {
86+ if ( node . intersects_with ( ray ) ) {
87+ this . select ( node ) ;
88+ return ;
89+ }
90+ }
91+
92+ this . select ( null ) ;
93+ }
94+
95+ public process_drag ( dx : number , dy : number ) {
96+ if ( ! this . selected_node || this . mode === EditorMode . IDLE ) return ;
97+
98+ console . log ( `Dragging mode: ${ this . mode } , Delta: ${ dx } , ${ dy } ` ) ;
99+
100+ this . update_gizmo_transforms ( ) ;
101+ }
102+
103+ public release_drag ( ) {
104+ this . mode = EditorMode . IDLE ;
105+ }
106+
107+ public get_active_gizmos ( ) : Node [ ] {
108+ if ( ! this . selected_node ) return [ ] ;
109+ return [ this . gizmo_x , this . gizmo_y , this . gizmo_z ] ;
110+ }
111+ }
0 commit comments