1 ! Copyright (C) 2008 Jeff Bigot
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
41 ui.gadgets.scrollers
\r
49 4DNav.space-file-decoder
\r
55 QUALIFIED-WITH: ui.pens.solid s
\r
56 QUALIFIED-WITH: ui.gadgets.wrappers w
\r
60 VALUE: selected-file
\r
61 VALUE: translation-step
\r
62 VALUE: rotation-step
\r
64 3 \ translation-step set-value
\r
65 5 \ rotation-step set-value
\r
67 VAR: selected-file-model
\r
75 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
77 ! namespace utilities
\r
79 : closed-quot ( quot -- quot )
\r
80 namestack swap '[ namestack [ _ set-namestack @ ] dip set-namestack ] ;
\r
82 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
83 ! waiting for deep-cleave-quots
\r
85 : 4D-Rxy ( angle -- Rx ) deg>rad
\r
86 [ 1.0 , 0.0 , 0.0 , 0.0 ,
\r
87 0.0 , 1.0 , 0.0 , 0.0 ,
\r
88 0.0 , 0.0 , dup cos , dup sin neg ,
\r
89 0.0 , 0.0 , dup sin , dup cos , ] 4 make-matrix nip ;
\r
91 : 4D-Rxz ( angle -- Ry ) deg>rad
\r
92 [ 1.0 , 0.0 , 0.0 , 0.0 ,
\r
93 0.0 , dup cos , 0.0 , dup sin neg ,
\r
94 0.0 , 0.0 , 1.0 , 0.0 ,
\r
95 0.0 , dup sin , 0.0 , dup cos , ] 4 make-matrix nip ;
\r
97 : 4D-Rxw ( angle -- Rz ) deg>rad
\r
98 [ 1.0 , 0.0 , 0.0 , 0.0 ,
\r
99 0.0 , dup cos , dup sin neg , 0.0 ,
\r
100 0.0 , dup sin , dup cos , 0.0 ,
\r
101 0.0 , 0.0 , 0.0 , 1.0 , ] 4 make-matrix nip ;
\r
103 : 4D-Ryz ( angle -- Rx ) deg>rad
\r
104 [ dup cos , 0.0 , 0.0 , dup sin neg ,
\r
105 0.0 , 1.0 , 0.0 , 0.0 ,
\r
106 0.0 , 0.0 , 1.0 , 0.0 ,
\r
107 dup sin , 0.0 , 0.0 , dup cos , ] 4 make-matrix nip ;
\r
109 : 4D-Ryw ( angle -- Ry ) deg>rad
\r
110 [ dup cos , 0.0 , dup sin neg , 0.0 ,
\r
111 0.0 , 1.0 , 0.0 , 0.0 ,
\r
112 dup sin , 0.0 , dup cos , 0.0 ,
\r
113 0.0 , 0.0 , 0.0 , 1.0 , ] 4 make-matrix nip ;
\r
115 : 4D-Rzw ( angle -- Rz ) deg>rad
\r
116 [ dup cos , dup sin neg , 0.0 , 0.0 ,
\r
117 dup sin , dup cos , 0.0 , 0.0 ,
\r
118 0.0 , 0.0 , 1.0 , 0.0 ,
\r
119 0.0 , 0.0 , 0.0 , 1.0 , ] 4 make-matrix nip ;
\r
121 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
123 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
125 : button* ( string quot -- button )
\r
126 closed-quot <repeat-button> ;
\r
128 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
130 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
132 : model-projection-chooser ( -- gadget )
\r
133 observer3d> projection-mode>>
\r
134 { { 1 "perspective" } { 0 "orthogonal" } }
\r
137 : collision-detection-chooser ( -- gadget )
\r
138 observer3d> collision-mode>>
\r
139 { { t "on" } { f "off" } } <radio-buttons> ;
\r
141 : model-projection ( x -- space )
\r
142 present-space> swap space-project ;
\r
144 : update-observer-projections ( -- )
\r
148 view4> relayout-1 ;
\r
150 : update-model-projections ( -- )
\r
151 0 model-projection <model> view1> model<<
\r
152 1 model-projection <model> view2> model<<
\r
153 2 model-projection <model> view3> model<<
\r
154 3 model-projection <model> view4> model<< ;
\r
156 : camera-action ( quot -- quot )
\r
157 '[ drop _ observer3d>
\r
158 with-self update-observer-projections ]
\r
161 : win3D ( text gadget -- )
\r
162 "navigateur 4D : " rot append open-window ;
\r
164 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
165 ! 4D object manipulation
\r
166 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
168 : (mvt-4D) ( quot -- )
\r
170 swap call space-ensure-solids
\r
172 update-model-projections
\r
173 update-observer-projections ; inline
\r
175 : rotation-4D ( m -- )
\r
176 '[ _ [ [ middle-of-space dup vneg ] keep
\r
177 swap space-translate ] dip
\r
179 swap space-translate
\r
182 : translation-4D ( v -- ) '[ _ space-translate ] (mvt-4D) ;
\r
184 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
186 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
188 : menu-rotations-4D ( -- gadget )
\r
190 { 1 1 } >>filled-cell
\r
192 "XY +" [ drop rotation-step 4D-Rxy rotation-4D ]
\r
194 "XY -" [ drop rotation-step neg 4D-Rxy rotation-4D ]
\r
195 button* add-gadget
\r
196 @top-left grid-add
\r
198 "XZ +" [ drop rotation-step 4D-Rxz rotation-4D ]
\r
200 "XZ -" [ drop rotation-step neg 4D-Rxz rotation-4D ]
\r
201 button* add-gadget
\r
204 "YZ +" [ drop rotation-step 4D-Ryz rotation-4D ]
\r
206 "YZ -" [ drop rotation-step neg 4D-Ryz rotation-4D ]
\r
207 button* add-gadget
\r
210 "XW +" [ drop rotation-step 4D-Rxw rotation-4D ]
\r
212 "XW -" [ drop rotation-step neg 4D-Rxw rotation-4D ]
\r
213 button* add-gadget
\r
214 @top-right grid-add
\r
216 "YW +" [ drop rotation-step 4D-Ryw rotation-4D ]
\r
218 "YW -" [ drop rotation-step neg 4D-Ryw rotation-4D ]
\r
219 button* add-gadget
\r
222 "ZW +" [ drop rotation-step 4D-Rzw rotation-4D ]
\r
224 "ZW -" [ drop rotation-step neg 4D-Rzw rotation-4D ]
\r
225 button* add-gadget
\r
226 @bottom-right grid-add
\r
229 : menu-translations-4D ( -- gadget )
\r
231 { 1 1 } >>filled-cell
\r
234 "X+" [ drop { 1 0 0 0 } translation-step v*n
\r
237 "X-" [ drop { -1 0 0 0 } translation-step v*n
\r
239 button* add-gadget
\r
241 "YZW" <label> add-gadget
\r
242 @bottom-right grid-add
\r
244 "XZW" <label> add-gadget
\r
246 "Y+" [ drop { 0 1 0 0 } translation-step v*n
\r
249 "Y-" [ drop { 0 -1 0 0 } translation-step v*n
\r
251 button* add-gadget
\r
253 @top-right grid-add
\r
255 "XYW" <label> add-gadget
\r
257 "Z+" [ drop { 0 0 1 0 } translation-step v*n
\r
260 "Z-" [ drop { 0 0 -1 0 } translation-step v*n
\r
262 button* add-gadget
\r
264 @top-left grid-add
\r
267 "W+" [ drop { 0 0 0 1 } translation-step v*n
\r
270 "W-" [ drop { 0 0 0 -1 } translation-step v*n
\r
272 button* add-gadget
\r
274 "XYZ" <label> add-gadget
\r
275 @bottom-left grid-add
\r
276 "X" <label> @center grid-add
\r
279 : menu-4D ( -- gadget )
\r
281 "rotations" <label> add-gadget
\r
282 menu-rotations-4D add-gadget
\r
283 "translations" <label> add-gadget
\r
284 menu-translations-4D add-gadget
\r
290 ! ------------------------------------------------------
\r
292 : redraw-model ( space -- )
\r
294 update-model-projections
\r
295 update-observer-projections ;
\r
297 : load-model-file ( -- )
\r
298 selected-file dup selected-file-model> set-model
\r
302 : mvt-3D-X ( turn pitch -- quot )
\r
303 '[ turtle-pos> norm neg reset-turtle
\r
308 : mvt-3D-1 ( -- quot ) 90 0 mvt-3D-X ; inline
\r
309 : mvt-3D-2 ( -- quot ) 0 90 mvt-3D-X ; inline
\r
310 : mvt-3D-3 ( -- quot ) 0 0 mvt-3D-X ; inline
\r
311 : mvt-3D-4 ( -- quot ) 45 45 mvt-3D-X ; inline
\r
313 : camera-button ( string quot -- button )
\r
314 [ <label> ] dip camera-action <repeat-button> ;
\r
316 ! ----------------------------------------------------------
\r
318 ! ----------------------------------------------------------
\r
319 : <run-file-button> ( file-name -- button )
\r
320 dup '[ drop _ \ selected-file set-value load-model-file
\r
322 closed-quot <roll-button> { 0 0 } >>align ;
\r
324 : <list-runner> ( -- gadget )
\r
325 "resource:extra/4DNav"
\r
327 over dup directory-files
\r
328 [ ".xml" tail? ] filter
\r
329 [ append-path ] with map
\r
330 [ <run-file-button> add-gadget ] each
\r
331 swap <labeled-gadget> ;
\r
333 ! -----------------------------------------------------
\r
335 : menu-rotations-3D ( -- gadget )
\r
337 { 1 1 } >>filled-cell
\r
338 "Turn\n left" [ rotation-step turn-left ]
\r
339 camera-button @left grid-add
\r
340 "Turn\n right" [ rotation-step turn-right ]
\r
341 camera-button @right grid-add
\r
342 "Pitch down" [ rotation-step pitch-down ]
\r
343 camera-button @bottom grid-add
\r
344 "Pitch up" [ rotation-step pitch-up ]
\r
345 camera-button @top grid-add
\r
347 "Roll left\n (ctl)" [ rotation-step roll-left ]
\r
348 camera-button add-gadget
\r
349 "Roll right\n(ctl)" [ rotation-step roll-right ]
\r
350 camera-button add-gadget
\r
354 : menu-translations-3D ( -- gadget )
\r
356 { 1 1 } >>filled-cell
\r
357 "left\n(alt)" [ translation-step strafe-left ]
\r
358 camera-button @left grid-add
\r
359 "right\n(alt)" [ translation-step strafe-right ]
\r
360 camera-button @right grid-add
\r
361 "Strafe up \n (alt)" [ translation-step strafe-up ]
\r
362 camera-button @top grid-add
\r
363 "Strafe down\n (alt)" [ translation-step strafe-down ]
\r
364 camera-button @bottom grid-add
\r
366 "Forward (ctl)" [ translation-step step-turtle ]
\r
367 camera-button add-gadget
\r
369 [ translation-step neg step-turtle ]
\r
370 camera-button add-gadget
\r
374 : menu-quick-views ( -- gadget )
\r
376 "View 1 (1)" mvt-3D-1 camera-button add-gadget
\r
377 "View 2 (2)" mvt-3D-2 camera-button add-gadget
\r
378 "View 3 (3)" mvt-3D-3 camera-button add-gadget
\r
379 "View 4 (4)" mvt-3D-4 camera-button add-gadget
\r
382 : menu-3D ( -- gadget )
\r
385 menu-rotations-3D add-gadget
\r
386 menu-translations-3D add-gadget
\r
390 menu-quick-views add-gadget ;
\r
392 TUPLE: handler < w:wrapper table ;
\r
394 : <handler> ( child -- handler ) handler w:new-wrapper ;
\r
396 M: handler handle-gesture ( gesture gadget -- ? )
\r
397 tuck table>> at dup [ call( gadget -- ) f ] [ 2drop t ] if ;
\r
399 : add-keyboard-delegate ( obj -- obj )
\r
402 { T{ key-down f f "LEFT" }
\r
403 [ [ rotation-step turn-left ] camera-action ] }
\r
404 { T{ key-down f f "RIGHT" }
\r
405 [ [ rotation-step turn-right ] camera-action ] }
\r
406 { T{ key-down f f "UP" }
\r
407 [ [ rotation-step pitch-down ] camera-action ] }
\r
408 { T{ key-down f f "DOWN" }
\r
409 [ [ rotation-step pitch-up ] camera-action ] }
\r
411 { T{ key-down f { C+ } "UP" }
\r
412 [ [ translation-step step-turtle ] camera-action ] }
\r
413 { T{ key-down f { C+ } "DOWN" }
\r
414 [ [ translation-step neg step-turtle ]
\r
416 { T{ key-down f { C+ } "LEFT" }
\r
417 [ [ rotation-step roll-left ] camera-action ] }
\r
418 { T{ key-down f { C+ } "RIGHT" }
\r
419 [ [ rotation-step roll-right ] camera-action ] }
\r
421 { T{ key-down f { A+ } "LEFT" }
\r
422 [ [ translation-step strafe-left ] camera-action ] }
\r
423 { T{ key-down f { A+ } "RIGHT" }
\r
424 [ [ translation-step strafe-right ] camera-action ] }
\r
425 { T{ key-down f { A+ } "UP" }
\r
426 [ [ translation-step strafe-up ] camera-action ] }
\r
427 { T{ key-down f { A+ } "DOWN" }
\r
428 [ [ translation-step strafe-down ] camera-action ] }
\r
431 { T{ key-down f f "1" } [ mvt-3D-1 camera-action ] }
\r
432 { T{ key-down f f "2" } [ mvt-3D-2 camera-action ] }
\r
433 { T{ key-down f f "3" } [ mvt-3D-3 camera-action ] }
\r
434 { T{ key-down f f "4" } [ mvt-3D-4 camera-action ] }
\r
439 ! --------------------------------------------
\r
441 ! --------------------------------------------
\r
444 GENERIC: adsoda-display-model ( x -- )
\r
446 M: light adsoda-display-model
\r
449 [ direction>> "direction : " pprint . ]
\r
450 [ color>> "color : " pprint . ]
\r
454 M: face adsoda-display-model
\r
456 [ halfspace>> "halfspace : " pprint . ]
\r
457 [ touching-corners>> "touching corners : " pprint . ]
\r
460 M: solid adsoda-display-model
\r
462 [ name>> "solid called : " pprint . ]
\r
463 [ color>> "color : " pprint . ]
\r
464 [ dimension>> "dimension : " pprint . ]
\r
465 [ faces>> "composed of faces : " pprint
\r
466 [ adsoda-display-model ] each ]
\r
469 M: space adsoda-display-model
\r
471 [ dimension>> "dimension : " pprint . ]
\r
472 [ ambient-color>> "ambient-color : " pprint . ]
\r
473 [ solids>> "composed of solids : " pprint
\r
474 [ adsoda-display-model ] each ]
\r
475 [ lights>> "composed of lights : " pprint
\r
476 [ adsoda-display-model ] each ]
\r
480 ! ----------------------------------------------
\r
481 : menu-bar ( -- gadget )
\r
483 "reinit" [ drop load-model-file ] button* add-gadget
\r
484 selected-file-model> <label-control> add-gadget
\r
488 : controller-window* ( -- gadget )
\r
490 menu-bar f track-add
\r
495 "Projection mode : " <label> add-gadget
\r
496 model-projection-chooser add-gadget
\r
499 "Collision detection (slow and buggy ) : "
\r
501 collision-detection-chooser add-gadget
\r
505 menu-4D add-gadget
\r
506 COLOR: purple s:<solid> >>interior
\r
507 "4D movements" <labeled-gadget>
\r
513 COLOR: purple s:<solid> >>interior
\r
514 "Camera 3D" <labeled-gadget>
\r
516 COLOR: gray s:<solid> >>interior
\r
519 : viewer-windows* ( -- )
\r
520 "YZW" view1> win3D
\r
521 "XZW" view2> win3D
\r
522 "XYW" view3> win3D
\r
523 "XYZ" view4> win3D
\r
526 : navigator-window* ( -- )
\r
529 add-keyboard-delegate
\r
530 "navigateur 4D" open-window
\r
533 : windows ( -- ) [ [ navigator-window* ] with-scope ] with-ui ;
\r
536 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\r
538 : init-variables ( -- )
\r
539 "choose a file" <model> >selected-file-model
\r
540 <observer> >observer3d
\r
541 [ observer3d> >self
\r
551 : init-models ( -- )
\r
552 0 model-projection observer3d> <window3D> >view1
\r
553 1 model-projection observer3d> <window3D> >view2
\r
554 2 model-projection observer3d> <window3D> >view3
\r
555 3 model-projection observer3d> <window3D> >view4
\r
560 selected-file read-model-file >present-space
\r