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 )
\r
125 closed-quot <repeat-button> ;
\r
127 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
129 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
131 : model-projection-chooser ( -- gadget )
\r
132 observer3d> projection-mode>>
\r
133 { { 1 "perspective" } { 0 "orthogonal" } }
\r
136 : collision-detection-chooser ( -- gadget )
\r
137 observer3d> collision-mode>>
\r
138 { { t "on" } { f "off" } } <toggle-buttons> ;
\r
140 : model-projection ( x -- space )
\r
141 present-space> swap space-project ;
\r
143 : update-observer-projections ( -- )
\r
147 view4> relayout-1 ;
\r
149 : update-model-projections ( -- )
\r
150 0 model-projection <model> view1> (>>model)
\r
151 1 model-projection <model> view2> (>>model)
\r
152 2 model-projection <model> view3> (>>model)
\r
153 3 model-projection <model> view4> (>>model) ;
\r
155 : camera-action ( quot -- quot )
\r
156 [ drop [ ] observer3d>
\r
157 with-self update-observer-projections ]
\r
158 make* closed-quot ;
\r
160 : win3D ( text gadget -- )
\r
161 "navigateur 4D : " rot append open-window ;
\r
163 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
164 ! 4D object manipulation
\r
165 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
167 : (mvt-4D) ( quot -- )
\r
169 swap call space-ensure-solids
\r
171 update-model-projections
\r
172 update-observer-projections ;
\r
174 : rotation-4D ( m -- )
\r
175 '[ _ [ [ middle-of-space dup vneg ] keep
\r
176 swap space-translate ] dip
\r
178 swap space-translate
\r
181 : translation-4D ( v -- ) '[ _ space-translate ] (mvt-4D) ;
\r
183 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
185 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
187 : menu-rotations-4D ( -- gadget )
\r
190 "XY +" [ drop rotation-step 4D-Rxy rotation-4D ]
\r
192 "XY -" [ drop rotation-step neg 4D-Rxy rotation-4D ]
\r
193 button* add-gadget
\r
194 @top-left grid-add
\r
196 "XZ +" [ drop rotation-step 4D-Rxz rotation-4D ]
\r
198 "XZ -" [ drop rotation-step neg 4D-Rxz rotation-4D ]
\r
199 button* add-gadget
\r
202 "YZ +" [ drop rotation-step 4D-Ryz rotation-4D ]
\r
204 "YZ -" [ drop rotation-step neg 4D-Ryz rotation-4D ]
\r
205 button* add-gadget
\r
208 "XW +" [ drop rotation-step 4D-Rxw rotation-4D ]
\r
210 "XW -" [ drop rotation-step neg 4D-Rxw rotation-4D ]
\r
211 button* add-gadget
\r
212 @top-right grid-add
\r
214 "YW +" [ drop rotation-step 4D-Ryw rotation-4D ]
\r
216 "YW -" [ drop rotation-step neg 4D-Ryw rotation-4D ]
\r
217 button* add-gadget
\r
220 "ZW +" [ drop rotation-step 4D-Rzw rotation-4D ]
\r
222 "ZW -" [ drop rotation-step neg 4D-Rzw rotation-4D ]
\r
223 button* add-gadget
\r
224 @bottom-right grid-add
\r
227 : menu-translations-4D ( -- gadget )
\r
231 "X+" [ drop { 1 0 0 0 } translation-step v*n
\r
234 "X-" [ drop { -1 0 0 0 } translation-step v*n
\r
236 button* add-gadget
\r
238 "YZW" <label> add-gadget
\r
239 @bottom-right grid-add
\r
241 "XZW" <label> add-gadget
\r
243 "Y+" [ drop { 0 1 0 0 } translation-step v*n
\r
246 "Y-" [ drop { 0 -1 0 0 } translation-step v*n
\r
248 button* add-gadget
\r
250 @top-right grid-add
\r
252 "XYW" <label> add-gadget
\r
254 "Z+" [ drop { 0 0 1 0 } translation-step v*n
\r
257 "Z-" [ drop { 0 0 -1 0 } translation-step v*n
\r
259 button* add-gadget
\r
261 @top-left grid-add
\r
264 "W+" [ drop { 0 0 0 1 } translation-step v*n
\r
267 "W-" [ drop { 0 0 0 -1 } translation-step v*n
\r
269 button* add-gadget
\r
271 "XYZ" <label> add-gadget
\r
272 @bottom-left grid-add
\r
273 "X" <label> @center grid-add
\r
276 : menu-4D ( -- gadget )
\r
278 "rotations" <label> add-gadget
\r
279 menu-rotations-4D add-gadget
\r
280 "translations" <label> add-gadget
\r
281 menu-translations-4D add-gadget
\r
287 ! ------------------------------------------------------
\r
289 : redraw-model ( space -- )
\r
291 update-model-projections
\r
292 update-observer-projections ;
\r
294 : load-model-file ( -- )
\r
295 selected-file dup selected-file-model> set-model
\r
299 : mvt-3D-X ( turn pitch -- quot )
\r
300 '[ turtle-pos> norm neg reset-turtle
\r
305 : mvt-3D-1 ( -- quot ) 90 0 mvt-3D-X ; inline
\r
306 : mvt-3D-2 ( -- quot ) 0 90 mvt-3D-X ; inline
\r
307 : mvt-3D-3 ( -- quot ) 0 0 mvt-3D-X ; inline
\r
308 : mvt-3D-4 ( -- quot ) 45 45 mvt-3D-X ; inline
\r
310 : camera-button ( string quot -- button )
\r
311 [ <label> ] dip camera-action <repeat-button> ;
\r
313 ! ----------------------------------------------------------
\r
315 ! ----------------------------------------------------------
\r
316 : <run-file-button> ( file-name -- button )
\r
317 dup '[ drop _ \ selected-file set-value load-model-file
\r
319 closed-quot <roll-button> { 0 0 } >>align ;
\r
321 : <list-runner> ( -- gadget )
\r
322 "resource:extra/4DNav"
\r
324 over dup directory-files
\r
325 [ ".xml" tail? ] filter
\r
326 [ append-path ] with map
\r
327 [ <run-file-button> add-gadget ] each
\r
328 swap <labelled-gadget> ;
\r
330 ! -----------------------------------------------------
\r
332 : menu-rotations-3D ( -- gadget )
\r
334 "Turn\n left" [ rotation-step turn-left ]
\r
335 camera-button @left grid-add
\r
336 "Turn\n right" [ rotation-step turn-right ]
\r
337 camera-button @right grid-add
\r
338 "Pitch down" [ rotation-step pitch-down ]
\r
339 camera-button @bottom grid-add
\r
340 "Pitch up" [ rotation-step pitch-up ]
\r
341 camera-button @top grid-add
\r
343 "Roll left\n (ctl)" [ rotation-step roll-left ]
\r
344 camera-button add-gadget
\r
345 "Roll right\n(ctl)" [ rotation-step roll-right ]
\r
346 camera-button add-gadget
\r
350 : menu-translations-3D ( -- gadget )
\r
352 "left\n(alt)" [ translation-step strafe-left ]
\r
353 camera-button @left grid-add
\r
354 "right\n(alt)" [ translation-step strafe-right ]
\r
355 camera-button @right grid-add
\r
356 "Strafe up \n (alt)" [ translation-step strafe-up ]
\r
357 camera-button @top grid-add
\r
358 "Strafe down\n (alt)" [ translation-step strafe-down ]
\r
359 camera-button @bottom grid-add
\r
361 "Forward (ctl)" [ translation-step step-turtle ]
\r
362 camera-button add-gadget
\r
364 [ translation-step neg step-turtle ]
\r
365 camera-button add-gadget
\r
369 : menu-quick-views ( -- gadget )
\r
371 "View 1 (1)" mvt-3D-1 camera-button add-gadget
\r
372 "View 2 (2)" mvt-3D-2 camera-button add-gadget
\r
373 "View 3 (3)" mvt-3D-3 camera-button add-gadget
\r
374 "View 4 (4)" mvt-3D-4 camera-button add-gadget
\r
377 : menu-3D ( -- gadget )
\r
380 menu-rotations-3D add-gadget
\r
381 menu-translations-3D add-gadget
\r
385 menu-quick-views add-gadget ;
\r
387 : add-keyboard-delegate ( obj -- obj )
\r
390 { T{ key-down f f "LEFT" }
\r
391 [ [ rotation-step turn-left ] camera-action ] }
\r
392 { T{ key-down f f "RIGHT" }
\r
393 [ [ rotation-step turn-right ] camera-action ] }
\r
394 { T{ key-down f f "UP" }
\r
395 [ [ rotation-step pitch-down ] camera-action ] }
\r
396 { T{ key-down f f "DOWN" }
\r
397 [ [ rotation-step pitch-up ] camera-action ] }
\r
399 { T{ key-down f { C+ } "UP" }
\r
400 [ [ translation-step step-turtle ] camera-action ] }
\r
401 { T{ key-down f { C+ } "DOWN" }
\r
402 [ [ translation-step neg step-turtle ]
\r
404 { T{ key-down f { C+ } "LEFT" }
\r
405 [ [ rotation-step roll-left ] camera-action ] }
\r
406 { T{ key-down f { C+ } "RIGHT" }
\r
407 [ [ rotation-step roll-right ] camera-action ] }
\r
409 { T{ key-down f { A+ } "LEFT" }
\r
410 [ [ translation-step strafe-left ] camera-action ] }
\r
411 { T{ key-down f { A+ } "RIGHT" }
\r
412 [ [ translation-step strafe-right ] camera-action ] }
\r
413 { T{ key-down f { A+ } "UP" }
\r
414 [ [ translation-step strafe-up ] camera-action ] }
\r
415 { T{ key-down f { A+ } "DOWN" }
\r
416 [ [ translation-step strafe-down ] camera-action ] }
\r
419 { T{ key-down f f "1" } [ mvt-3D-1 camera-action ] }
\r
420 { T{ key-down f f "2" } [ mvt-3D-2 camera-action ] }
\r
421 { T{ key-down f f "3" } [ mvt-3D-3 camera-action ] }
\r
422 { T{ key-down f f "4" } [ mvt-3D-4 camera-action ] }
\r
424 } [ make* ] map >hashtable >>table
\r
427 ! --------------------------------------------
\r
429 ! --------------------------------------------
\r
432 GENERIC: adsoda-display-model ( x -- )
\r
434 M: light adsoda-display-model
\r
437 [ direction>> "direction : " pprint . ]
\r
438 [ color>> "color : " pprint . ]
\r
442 M: face adsoda-display-model
\r
444 [ halfspace>> "halfspace : " pprint . ]
\r
445 [ touching-corners>> "touching corners : " pprint . ]
\r
448 M: solid adsoda-display-model
\r
450 [ name>> "solid called : " pprint . ]
\r
451 [ color>> "color : " pprint . ]
\r
452 [ dimension>> "dimension : " pprint . ]
\r
453 [ faces>> "composed of faces : " pprint
\r
454 [ adsoda-display-model ] each ]
\r
457 M: space adsoda-display-model
\r
459 [ dimension>> "dimension : " pprint . ]
\r
460 [ ambient-color>> "ambient-color : " pprint . ]
\r
461 [ solids>> "composed of solids : " pprint
\r
462 [ adsoda-display-model ] each ]
\r
463 [ lights>> "composed of lights : " pprint
\r
464 [ adsoda-display-model ] each ]
\r
468 ! ----------------------------------------------
\r
469 : menu-bar ( -- gadget )
\r
471 "reinit" [ drop load-model-file ] button* add-gadget
\r
472 selected-file-model> <label-control> add-gadget
\r
476 : controller-window* ( -- gadget )
\r
478 menu-bar f track-add
\r
480 <limited-scroller>
\r
481 { 200 400 } >>max-dim
\r
484 "Projection mode : " <label> add-gadget
\r
485 model-projection-chooser add-gadget
\r
488 "Collision detection (slow and buggy ) : "
\r
490 collision-detection-chooser add-gadget
\r
494 menu-4D add-gadget
\r
495 light-purple solid-interior
\r
496 "4D movements" <labelled-gadget>
\r
502 light-purple solid-interior
\r
503 "Camera 3D" <labelled-gadget>
\r
505 gray solid-interior
\r
508 : viewer-windows* ( -- )
\r
509 "YZW" view1> win3D
\r
510 "XZW" view2> win3D
\r
511 "XYW" view3> win3D
\r
512 "XYZ" view4> win3D
\r
515 : navigator-window* ( -- )
\r
518 add-keyboard-delegate
\r
519 "navigateur 4D" open-window
\r
522 : windows ( -- ) [ [ navigator-window* ] with-scope ] with-ui ;
\r
525 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
527 : init-variables ( -- )
\r
528 "choose a file" <model> >selected-file-model
\r
529 <observer> >observer3d
\r
530 [ observer3d> >self
\r
540 : init-models ( -- )
\r
541 0 model-projection observer3d> <window3D> >view1
\r
542 1 model-projection observer3d> <window3D> >view2
\r
543 2 model-projection observer3d> <window3D> >view3
\r
544 3 model-projection observer3d> <window3D> >view4
\r
549 selected-file read-model-file >present-space
\r