factor/extra/boids/ui/ui.factor

177 lines
5.3 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
USING: combinators.short-circuit kernel namespaces
2007-09-20 18:09:08 -04:00
math
2008-07-28 01:50:22 -04:00
math.trig
math.functions
2007-09-20 18:09:08 -04:00
math.vectors
math.parser
hashtables sequences threads
colors
opengl
opengl.gl
ui
ui.gadgets
ui.gadgets.handler
ui.gadgets.slate
ui.gadgets.theme
ui.gadgets.frames
ui.gadgets.labels
ui.gadgets.buttons
ui.gadgets.packs
ui.gadgets.grids
ui.gestures
2008-07-11 20:03:15 -04:00
assocs.lib vars rewrite-closures boids accessors
2008-07-12 16:48:20 -04:00
math.geometry.rect
2008-07-28 01:50:22 -04:00
newfx
processing.shapes ;
2007-09-20 18:09:08 -04:00
IN: boids.ui
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! draw-boid
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-07-28 01:50:22 -04:00
: draw-boid ( boid -- )
glPushMatrix
dup pos>> gl-translate-2d
vel>> first2 rect> arg rad>deg 0 0 1 glRotated
{ { 0 5 } { 0 -5 } { 20 0 } } triangle
fill-mode
2008-07-28 01:50:22 -04:00
glPopMatrix ;
2007-09-20 18:09:08 -04:00
: draw-boids ( -- ) boids> [ draw-boid ] each ;
2008-07-28 15:59:40 -04:00
: boid-color ( -- color ) T{ rgba f 1.0 0 0 0.3 } ;
2008-07-28 01:50:22 -04:00
: display ( -- )
boid-color >fill-color
draw-boids ;
2007-09-20 18:09:08 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
VAR: slate
VAR: loop
: run ( -- )
slate> rect-dim >world-size
iterate-boids
slate> relayout-1
yield
loop> [ run ] when ;
: button* ( string quot -- button ) closed-quot <bevel-button> ;
: toggle-loop ( -- ) loop> [ loop off ] [ loop on [ run ] in-thread ] if ;
VARS: population-label cohesion-label alignment-label separation-label ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: update-population-label ( -- )
"Population: " boids> length number>string append
20 32 pad-right population-label> set-label-string ;
: add-10-boids ( -- )
boids> 10 random-boids append >boids update-population-label ;
: sub-10-boids ( -- )
boids> 10 tail >boids update-population-label ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: truncate-value ( n -- n ) 10 * round 10 / ;
: update-cohesion-label ( -- )
"Cohesion: " cohesion-weight> truncate-value number>string append
20 32 pad-right cohesion-label> set-label-string ;
: update-alignment-label ( -- )
"Alignment: " alignment-weight> truncate-value number>string append
20 32 pad-right alignment-label> set-label-string ;
: update-separation-label ( -- )
"Separation: " separation-weight> truncate-value number>string append
20 32 pad-right separation-label> set-label-string ;
: inc-cohesion-weight ( -- ) cohesion-weight inc* update-cohesion-label ;
: dec-cohesion-weight ( -- ) cohesion-weight dec* update-cohesion-label ;
: inc-alignment-weight ( -- ) alignment-weight inc* update-alignment-label ;
: dec-alignment-weight ( -- ) alignment-weight dec* update-alignment-label ;
: inc-separation-weight ( -- ) separation-weight inc* update-separation-label ;
: dec-separation-weight ( -- ) separation-weight dec* update-separation-label ;
: boids-window* ( -- )
init-variables init-world-size init-boids loop on
2008-06-18 23:30:54 -04:00
"" <label> reverse-video-theme >population-label update-population-label
"" <label> reverse-video-theme >cohesion-label update-cohesion-label
"" <label> reverse-video-theme >alignment-label update-alignment-label
"" <label> reverse-video-theme >separation-label update-separation-label
2007-09-20 18:09:08 -04:00
<frame>
2008-07-25 17:55:10 -04:00
<shelf>
2008-07-13 18:12:34 -04:00
2008-07-25 17:55:10 -04:00
1 >>fill
"ESC - Pause" [ drop toggle-loop ] button* add-gadget
2008-07-25 17:41:03 -04:00
2008-07-25 17:55:10 -04:00
"1 - Randomize" [ drop randomize ] button* add-gadget
2008-07-25 17:41:03 -04:00
2008-09-02 13:36:43 -04:00
<pile> 1 >>fill
2008-07-25 17:55:10 -04:00
population-label> add-gadget
"3 - Add 10" [ drop add-10-boids ] button* add-gadget
"2 - Sub 10" [ drop sub-10-boids ] button* add-gadget
add-gadget
2008-07-25 17:41:03 -04:00
2008-09-02 13:36:43 -04:00
<pile> 1 >>fill
2008-07-25 17:55:10 -04:00
cohesion-label> add-gadget
"q - +0.1" [ drop inc-cohesion-weight ] button* add-gadget
"a - -0.1" [ drop dec-cohesion-weight ] button* add-gadget
add-gadget
2008-09-02 13:36:43 -04:00
<pile> 1 >>fill
2008-07-25 17:55:10 -04:00
alignment-label> add-gadget
"w - +0.1" [ drop inc-alignment-weight ] button* add-gadget
"s - -0.1" [ drop dec-alignment-weight ] button* add-gadget
add-gadget
2008-09-02 13:36:43 -04:00
<pile> 1 >>fill
2008-07-25 17:55:10 -04:00
separation-label> add-gadget
"e - +0.1" [ drop inc-separation-weight ] button* add-gadget
"d - -0.1" [ drop dec-separation-weight ] button* add-gadget
add-gadget
@top grid-add
C[ display ] <slate>
dup >slate
t >>clipped?
{ 600 400 } >>pdim
C[ [ run ] in-thread ] >>graft
C[ loop off ] >>ungraft
@center grid-add
2007-09-20 18:09:08 -04:00
<handler>
2008-07-25 17:55:10 -04:00
H{ } clone
T{ key-down f f "1" } C[ drop randomize ] is
T{ key-down f f "2" } C[ drop sub-10-boids ] is
T{ key-down f f "3" } C[ drop add-10-boids ] is
T{ key-down f f "q" } C[ drop inc-cohesion-weight ] is
T{ key-down f f "a" } C[ drop dec-cohesion-weight ] is
T{ key-down f f "w" } C[ drop inc-alignment-weight ] is
T{ key-down f f "s" } C[ drop dec-alignment-weight ] is
T{ key-down f f "e" } C[ drop inc-separation-weight ] is
T{ key-down f f "d" } C[ drop dec-separation-weight ] is
T{ key-down f f "ESC" } C[ drop toggle-loop ] is
>>table
"Boids" open-window ;
2007-09-20 18:09:08 -04:00
: boids-window ( -- ) [ [ boids-window* ] with-scope ] with-ui ;
2008-01-16 16:25:29 -05:00
MAIN: boids-window