@@ -3,32 +3,32 @@ import { vec3 } from "../math/types";
33import type { Line } from "../math/types" ;
44import { Inspector } from "./inspector" ;
55import { create_arrow } from "../rendering/utils/primitives" ;
6- import { Mesh } from "../rendering/types/mesh" ;
76import type { Scene } from "../rendering/types/scene" ;
7+ import { scale } from "../math/transformations" ;
88
9- export enum EditorMode {
10- IDLE ,
11- TRANSLATE_X ,
12- TRANSLATE_Y ,
13- TRANSLATE_Z
14- }
9+ export type EditorMode = "IDLE" | "TRANSLATE_X" | "TRANSLATE_Y" | "TRANSLATE_Z" ;
1510
1611export class EditorState {
17- public mode : EditorMode = EditorMode . IDLE ;
12+ public mode : EditorMode = " IDLE" ;
1813 public selected_node : Node | null = null ;
1914
2015 public gizmo_x ! : Node ;
2116 public gizmo_y ! : Node ;
2217 public gizmo_z ! : Node ;
2318
19+ private is_dragging = false ;
20+ private last_x = 0 ;
21+ private last_y = 0 ;
22+
2423 private inspector : Inspector ;
2524
26- constructor ( inspector : Inspector , scene :Scene ) {
25+ constructor ( inspector : Inspector , scene : Scene ) {
2726 this . inspector = inspector ;
2827 this . setup_gizmos ( scene ) ;
28+ this . select ( null ) ;
2929 }
3030
31- private setup_gizmos ( scene :Scene ) {
31+ private setup_gizmos ( scene : Scene ) {
3232 const arrow_geo = create_arrow ( ) ;
3333
3434 const mesh_z = scene . add_mesh ( arrow_geo , vec3 ( 0 , 0 , 1 ) , 0 ) ;
@@ -45,16 +45,34 @@ export class EditorState {
4545 this . gizmo_x . update_matrix ( ) ;
4646 this . gizmo_y . update_matrix ( ) ;
4747 this . gizmo_z . update_matrix ( ) ;
48- }
48+ }
4949
5050 public select ( node : Node | null ) {
5151 this . selected_node = node ;
5252 this . inspector . inspect ( node ) ;
53- this . update_gizmo_transforms ( ) ;
53+
54+ if ( node ) {
55+ const scale_amount = node . scale_vec ;
56+ this . gizmo_x . scale_vec = scale_amount ;
57+ this . gizmo_y . scale_vec = scale_amount ;
58+ this . gizmo_z . scale_vec = scale_amount ;
59+ this . update_gizmo_transforms ( ) ;
60+ } else {
61+ this . gizmo_x . scale_vec = vec3 ( 0 , 0 , 0 ) ;
62+ this . gizmo_y . scale_vec = vec3 ( 0 , 0 , 0 ) ;
63+ this . gizmo_z . scale_vec = vec3 ( 0 , 0 , 0 ) ;
64+ this . update_gizmo_transforms ( ) ;
65+ }
5466 }
5567
5668 public update_gizmo_transforms ( ) {
57- if ( ! this . selected_node ) return ;
69+ if ( ! this . selected_node ) {
70+ this . gizmo_x . update_matrix ( ) ;
71+ this . gizmo_y . update_matrix ( ) ;
72+ this . gizmo_z . update_matrix ( ) ;
73+ return ;
74+ }
75+
5876 const pos = this . selected_node . position ;
5977
6078 this . gizmo_x . position = vec3 ( pos [ 0 ] , pos [ 1 ] , pos [ 2 ] ) ;
@@ -66,42 +84,64 @@ export class EditorState {
6684 this . gizmo_z . update_matrix ( ) ;
6785 }
6886
69- public process_raycast ( ray : Line , scene_nodes : Node [ ] ) {
87+ public handle_mouse_down ( ray : Line , mouse_x : number , mouse_y : number , scene_nodes : Node [ ] ) {
88+ this . is_dragging = true ;
89+ this . last_x = mouse_x ;
90+ this . last_y = mouse_y ;
91+
7092 if ( this . selected_node ) {
7193 if ( this . gizmo_x . intersects_with ( ray ) ) {
72- this . mode = EditorMode . TRANSLATE_X ;
94+ this . mode = " TRANSLATE_X" ;
7395 return ;
7496 }
7597 if ( this . gizmo_y . intersects_with ( ray ) ) {
76- this . mode = EditorMode . TRANSLATE_Y ;
98+ this . mode = " TRANSLATE_Y" ;
7799 return ;
78100 }
79101 if ( this . gizmo_z . intersects_with ( ray ) ) {
80- this . mode = EditorMode . TRANSLATE_Z ;
102+ this . mode = " TRANSLATE_Z" ;
81103 return ;
82104 }
83105 }
84-
85106 for ( const node of scene_nodes ) {
86107 if ( node . intersects_with ( ray ) ) {
87108 this . select ( node ) ;
88109 return ;
89110 }
90111 }
91-
92112 this . select ( null ) ;
93113 }
94114
95- public process_drag ( dx : number , dy : number ) {
96- if ( ! this . selected_node || this . mode === EditorMode . IDLE ) return ;
115+ public handle_mouse_move ( mouse_x : number , mouse_y : number ) {
116+ if ( ! this . is_dragging ) return ;
97117
98- console . log ( `Dragging mode: ${ this . mode } , Delta: ${ dx } , ${ dy } ` ) ;
118+ const dx = mouse_x - this . last_x ;
119+ const dy = mouse_y - this . last_y ;
99120
121+ this . last_x = mouse_x ;
122+ this . last_y = mouse_y ;
123+
124+ if ( ! this . selected_node || this . mode === "IDLE" ) return ;
125+
126+ const sensitivity = 0.02 ;
127+
128+ if ( this . mode === "TRANSLATE_X" ) {
129+ this . selected_node . position [ 0 ] += dx * sensitivity ;
130+ } else if ( this . mode === "TRANSLATE_Y" ) {
131+ this . selected_node . position [ 1 ] -= dy * sensitivity ;
132+ } else if ( this . mode === "TRANSLATE_Z" ) {
133+ this . selected_node . position [ 2 ] -= ( dx + dy ) * sensitivity ;
134+ }
135+
136+ this . selected_node . update_matrix ( ) ;
100137 this . update_gizmo_transforms ( ) ;
138+
139+ this . inspector . inspect ( this . selected_node ) ;
101140 }
102141
103- public release_drag ( ) {
104- this . mode = EditorMode . IDLE ;
142+ public handle_mouse_up ( ) {
143+ this . is_dragging = false ;
144+ this . mode = "IDLE" ;
105145 }
106146
107147 public get_active_gizmos ( ) : Node [ ] {
0 commit comments