1 ! Copyright (C) 2008 Jeff Bigot
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
35 ui.gadgets.labelled
\r
42 ui.gadgets.scrollers
\r
52 4DNav.space-file-decoder
\r
60 VALUE: selected-file
\r
61 VALUE: translation-step
\r
62 VALUE: rotation-step
\r
64 3 to: translation-step
\r
67 VAR: selected-file-model
\r
75 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
77 ! replacement of namespaces.lib
\r
79 : make* ( seq -- seq ) [ dup quotation? [ call ] [ ] if ] map ;
\r
81 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
82 ! waiting for deep-cleave-quots
\r
84 : 4D-Rxy ( angle -- Rx ) deg>rad
\r
85 [ 1.0 , 0.0 , 0.0 , 0.0 ,
\r
86 0.0 , 1.0 , 0.0 , 0.0 ,
\r
87 0.0 , 0.0 , dup cos , dup sin neg ,
\r
88 0.0 , 0.0 , dup sin , dup cos , ] 4 make-matrix nip ;
\r
90 : 4D-Rxz ( angle -- Ry ) deg>rad
\r
91 [ 1.0 , 0.0 , 0.0 , 0.0 ,
\r
92 0.0 , dup cos , 0.0 , dup sin neg ,
\r
93 0.0 , 0.0 , 1.0 , 0.0 ,
\r
94 0.0 , dup sin , 0.0 , dup cos , ] 4 make-matrix nip ;
\r
96 : 4D-Rxw ( angle -- Rz ) deg>rad
\r
97 [ 1.0 , 0.0 , 0.0 , 0.0 ,
\r
98 0.0 , dup cos , dup sin neg , 0.0 ,
\r
99 0.0 , dup sin , dup cos , 0.0 ,
\r
100 0.0 , 0.0 , 0.0 , 1.0 , ] 4 make-matrix nip ;
\r
102 : 4D-Ryz ( angle -- Rx ) deg>rad
\r
103 [ dup cos , 0.0 , 0.0 , dup sin neg ,
\r
104 0.0 , 1.0 , 0.0 , 0.0 ,
\r
105 0.0 , 0.0 , 1.0 , 0.0 ,
\r
106 dup sin , 0.0 , 0.0 , dup cos , ] 4 make-matrix nip ;
\r
108 : 4D-Ryw ( angle -- Ry ) deg>rad
\r
109 [ dup cos , 0.0 , dup sin neg , 0.0 ,
\r
110 0.0 , 1.0 , 0.0 , 0.0 ,
\r
111 dup sin , 0.0 , dup cos , 0.0 ,
\r
112 0.0 , 0.0 , 0.0 , 1.0 , ] 4 make-matrix nip ;
\r
114 : 4D-Rzw ( angle -- Rz ) deg>rad
\r
115 [ dup cos , dup sin neg , 0.0 , 0.0 ,
\r
116 dup sin , dup cos , 0.0 , 0.0 ,
\r
117 0.0 , 0.0 , 1.0 , 0.0 ,
\r
118 0.0 , 0.0 , 0.0 , 1.0 , ] 4 make-matrix nip ;
\r
120 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
122 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
124 : button* ( string quot -- button ) closed-quot <repeat-button> ;
\r
126 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
128 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
130 : model-projection-chooser ( -- gadget )
\r
131 observer3d> projection-mode>>
\r
132 { { 1 "perspective" } { 0 "orthogonal" } } <toggle-buttons> ;
\r
134 : collision-detection-chooser ( -- gadget )
\r
135 observer3d> collision-mode>>
\r
136 { { t "on" } { f "off" } } <toggle-buttons>
\r
139 : model-projection ( x -- space ) present-space> swap space-project ;
\r
141 : update-observer-projections ( -- )
\r
145 view4> relayout-1 ;
\r
147 : update-model-projections ( -- )
\r
148 0 model-projection <model> view1> (>>model)
\r
149 1 model-projection <model> view2> (>>model)
\r
150 2 model-projection <model> view3> (>>model)
\r
151 3 model-projection <model> view4> (>>model) ;
\r
153 : camera-action ( quot -- quot )
\r
154 [ drop [ ] observer3d> with-self update-observer-projections ]
\r
155 make* closed-quot ;
\r
157 : win3D ( text gadget -- ) "navigateur 4D : " rot append open-window ;
\r
159 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
160 ! 4D object manipulation
\r
161 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
163 : (mvt-4D) ( quot -- )
\r
165 swap call space-ensure-solids
\r
167 update-model-projections
\r
168 update-observer-projections ;
\r
170 : rotation-4D ( m -- )
\r
171 '[ _ [ [ middle-of-space dup vneg ] keep swap space-translate ] dip
\r
173 swap space-translate
\r
176 : translation-4D ( v -- ) '[ _ space-translate ] (mvt-4D) ;
\r
178 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
180 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
182 : menu-rotations-4D ( -- gadget )
\r
185 "XY +" [ drop rotation-step 4D-Rxy rotation-4D ] button* add-gadget
\r
186 "XY -" [ drop rotation-step neg 4D-Rxy rotation-4D ] button* add-gadget
\r
187 @top-left grid-add
\r
189 "XZ +" [ drop rotation-step 4D-Rxz rotation-4D ] button* add-gadget
\r
190 "XZ -" [ drop rotation-step neg 4D-Rxz rotation-4D ] button* add-gadget
\r
193 "YZ +" [ drop rotation-step 4D-Ryz rotation-4D ] button* add-gadget
\r
194 "YZ -" [ drop rotation-step neg 4D-Ryz rotation-4D ] button* add-gadget
\r
197 "XW +" [ drop rotation-step 4D-Rxw rotation-4D ] button* add-gadget
\r
198 "XW -" [ drop rotation-step neg 4D-Rxw rotation-4D ] button* add-gadget
\r
199 @top-right grid-add
\r
201 "YW +" [ drop rotation-step 4D-Ryw rotation-4D ] button* add-gadget
\r
202 "YW -" [ drop rotation-step neg 4D-Ryw rotation-4D ] button* add-gadget
\r
205 "ZW +" [ drop rotation-step 4D-Rzw rotation-4D ] button* add-gadget
\r
206 "ZW -" [ drop rotation-step neg 4D-Rzw rotation-4D ] button* add-gadget
\r
207 @bottom-right grid-add
\r
210 : menu-translations-4D ( -- gadget )
\r
214 "X+" [ drop { 1 0 0 0 } translation-step v*n translation-4D ]
\r
216 "X-" [ drop { -1 0 0 0 } translation-step v*n translation-4D ]
\r
217 button* add-gadget
\r
219 "YZW" <label> add-gadget
\r
220 @bottom-right grid-add
\r
222 "XZW" <label> add-gadget
\r
224 "Y+" [ drop { 0 1 0 0 } translation-step v*n translation-4D ]
\r
226 "Y-" [ drop { 0 -1 0 0 } translation-step v*n translation-4D ]
\r
227 button* add-gadget
\r
229 @top-right grid-add
\r
231 "XYW" <label> add-gadget
\r
233 "Z+" [ drop { 0 0 1 0 } translation-step v*n translation-4D ]
\r
235 "Z-" [ drop { 0 0 -1 0 } translation-step v*n translation-4D ]
\r
236 button* add-gadget
\r
238 @top-left grid-add
\r
241 "W+" [ drop { 0 0 0 1 } translation-step v*n translation-4D ]
\r
243 "W-" [ drop { 0 0 0 -1 } translation-step v*n translation-4D ]
\r
244 button* add-gadget
\r
246 "XYZ" <label> add-gadget
\r
247 @bottom-left grid-add
\r
248 "X" <label> @center grid-add
\r
251 : menu-4D ( -- gadget )
\r
253 "rotations" <label> add-gadget
\r
254 menu-rotations-4D add-gadget
\r
255 "translations" <label> add-gadget
\r
256 menu-translations-4D add-gadget
\r
262 ! ------------------------------------------------------
\r
264 : redraw-model ( space -- )
\r
266 update-model-projections
\r
267 update-observer-projections ;
\r
269 : load-model-file ( -- )
\r
270 selected-file dup selected-file-model> set-model read-model-file
\r
273 : mvt-3D-X ( turn pitch -- quot )
\r
274 '[ turtle-pos> norm neg reset-turtle
\r
279 : mvt-3D-1 ( -- quot ) 90 0 mvt-3D-X ; inline
\r
280 : mvt-3D-2 ( -- quot ) 0 90 mvt-3D-X ; inline
\r
281 : mvt-3D-3 ( -- quot ) 0 0 mvt-3D-X ; inline
\r
282 : mvt-3D-4 ( -- quot ) 45 45 mvt-3D-X ; inline
\r
284 : camera-button ( string quot -- button )
\r
285 [ <label> ] dip camera-action <repeat-button> ;
\r
287 ! ----------------------------------------------------------
\r
289 ! ----------------------------------------------------------
\r
290 : <run-file-button> ( file-name -- button )
\r
291 dup '[ drop _ \ selected-file set-value load-model-file
\r
293 closed-quot <roll-button> { 0 0 } >>align ;
\r
295 : <list-runner> ( -- gadget )
\r
296 "resource:extra/4DNav"
\r
298 over dup directory-files
\r
299 [ ".xml" tail? ] filter
\r
300 [ append-path ] with map
\r
301 [ <run-file-button> add-gadget ] each
\r
302 swap <labelled-gadget> ;
\r
304 ! -----------------------------------------------------
\r
306 : menu-rotations-3D ( -- gadget )
\r
308 "Turn\n left" [ rotation-step turn-left ] camera-button
\r
310 "Turn\n right" [ rotation-step turn-right ] camera-button
\r
312 "Pitch down" [ rotation-step pitch-down ] camera-button
\r
314 "Pitch up" [ rotation-step pitch-up ] camera-button
\r
317 "Roll left\n (ctl)" [ rotation-step roll-left ] camera-button
\r
319 "Roll right\n(ctl)" [ rotation-step roll-right ] camera-button
\r
324 : menu-translations-3D ( -- gadget )
\r
326 "left\n(alt)" [ translation-step strafe-left ] camera-button
\r
328 "right\n(alt)" [ translation-step strafe-right ] camera-button
\r
330 "Strafe up \n (alt)" [ translation-step strafe-up ] camera-button
\r
332 "Strafe down \n (alt)" [ translation-step strafe-down ] camera-button
\r
335 "Forward (ctl)" [ translation-step step-turtle ] camera-button
\r
337 "Backward (ctl)" [ translation-step neg step-turtle ] camera-button
\r
342 : menu-quick-views ( -- gadget )
\r
344 "View 1 (1)" mvt-3D-1 camera-button add-gadget
\r
345 "View 2 (2)" mvt-3D-2 camera-button add-gadget
\r
346 "View 3 (3)" mvt-3D-3 camera-button add-gadget
\r
347 "View 4 (4)" mvt-3D-4 camera-button add-gadget
\r
350 : menu-3D ( -- gadget )
\r
353 menu-rotations-3D add-gadget
\r
354 menu-translations-3D add-gadget
\r
358 menu-quick-views add-gadget ;
\r
360 : add-keyboard-delegate ( obj -- obj )
\r
363 { T{ key-down f f "LEFT" }
\r
364 [ [ rotation-step turn-left ] camera-action ] }
\r
365 { T{ key-down f f "RIGHT" }
\r
366 [ [ rotation-step turn-right ] camera-action ] }
\r
367 { T{ key-down f f "UP" }
\r
368 [ [ rotation-step pitch-down ] camera-action ] }
\r
369 { T{ key-down f f "DOWN" }
\r
370 [ [ rotation-step pitch-up ] camera-action ] }
\r
372 { T{ key-down f { C+ } "UP" }
\r
373 [ [ translation-step step-turtle ] camera-action ] }
\r
374 { T{ key-down f { C+ } "DOWN" }
\r
375 [ [ translation-step neg step-turtle ] camera-action ] }
\r
376 { T{ key-down f { C+ } "LEFT" }
\r
377 [ [ rotation-step roll-left ] camera-action ] }
\r
378 { T{ key-down f { C+ } "RIGHT" }
\r
379 [ [ rotation-step roll-right ] camera-action ] }
\r
381 { T{ key-down f { A+ } "LEFT" }
\r
382 [ [ translation-step strafe-left ] camera-action ] }
\r
383 { T{ key-down f { A+ } "RIGHT" }
\r
384 [ [ translation-step strafe-right ] camera-action ] }
\r
385 { T{ key-down f { A+ } "UP" }
\r
386 [ [ translation-step strafe-up ] camera-action ] }
\r
387 { T{ key-down f { A+ } "DOWN" }
\r
388 [ [ translation-step strafe-down ] camera-action ] }
\r
391 { T{ key-down f f "1" } [ mvt-3D-1 camera-action ] }
\r
392 { T{ key-down f f "2" } [ mvt-3D-2 camera-action ] }
\r
393 { T{ key-down f f "3" } [ mvt-3D-3 camera-action ] }
\r
394 { T{ key-down f f "4" } [ mvt-3D-4 camera-action ] }
\r
396 } [ make* ] map >hashtable >>table
\r
399 ! --------------------------------------------
\r
401 ! --------------------------------------------
\r
404 GENERIC: adsoda-display-model ( x -- )
\r
406 M: light adsoda-display-model
\r
409 [ direction>> "direction : " pprint . ]
\r
410 [ color>> "color : " pprint . ]
\r
414 M: face adsoda-display-model
\r
416 [ halfspace>> "halfspace : " pprint . ]
\r
417 [ touching-corners>> "touching corners : " pprint . ]
\r
420 M: solid adsoda-display-model
\r
422 [ name>> "solid called : " pprint . ]
\r
423 [ color>> "color : " pprint . ]
\r
424 [ dimension>> "dimension : " pprint . ]
\r
425 [ faces>> "composed of faces : " pprint [ adsoda-display-model ] each ]
\r
428 M: space adsoda-display-model
\r
430 [ dimension>> "dimension : " pprint . ]
\r
431 [ ambient-color>> "ambient-color : " pprint . ]
\r
432 [ solids>> "composed of solids : " pprint [ adsoda-display-model ] each ]
\r
433 [ lights>> "composed of lights : " pprint [ adsoda-display-model ] each ]
\r
437 ! ----------------------------------------------
\r
438 : menu-bar ( -- gadget )
\r
440 "reinit" [ drop load-model-file ] button* add-gadget
\r
441 selected-file-model> <label-control> add-gadget
\r
445 : controller-window* ( -- gadget )
\r
447 menu-bar f track-add
\r
449 <limited-scroller>
\r
450 { 200 400 } >>max-dim
\r
453 "Projection mode : " <label> add-gadget
\r
454 model-projection-chooser add-gadget
\r
457 "Collision detection (slow and buggy ) : " <label> add-gadget
\r
458 collision-detection-chooser add-gadget
\r
462 menu-4D add-gadget
\r
463 light-purple solid-interior
\r
464 "4D movements" <labelled-gadget>
\r
470 light-purple solid-interior
\r
471 "Camera 3D" <labelled-gadget>
\r
473 gray solid-interior
\r
476 : viewer-windows* ( -- )
\r
477 "YZW" view1> win3D
\r
478 "XZW" view2> win3D
\r
479 "XYW" view3> win3D
\r
480 "XYZ" view4> win3D
\r
483 : navigator-window* ( -- )
\r
486 add-keyboard-delegate
\r
487 "navigateur 4D" open-window
\r
490 : windows ( -- ) [ [ navigator-window* ] with-scope ] with-ui ;
\r
493 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
495 : init-variables ( -- )
\r
496 "choose a file" <model> >selected-file-model
\r
497 <observer> >observer3d
\r
498 [ observer3d> >self
\r
508 : init-models ( -- )
\r
509 0 model-projection observer3d> <window3D> >view1
\r
510 1 model-projection observer3d> <window3D> >view2
\r
511 2 model-projection observer3d> <window3D> >view3
\r
512 3 model-projection observer3d> <window3D> >view4
\r
517 selected-file read-model-file >present-space
\r