lux: brilliant interactive programs
(require lux) | package: lux |
The lux module provides an efficient way to build interactive programs that consist of plain mathematical functions. It is comparable to 2htdp/universe, although designed to allow more parameterization of how the program interacts.
1 Structure of a lux Program
A lux program chooses how it will interact be selecting a chaos and calling call-with-chaos with that chaos and a thunk that calls fiat-lux with a word. fiat-lux may be called any number of nested times within a call to call-with-chaos. Each subsequent fiat-lux takes over the chaos until the word completes. It is not typically possible to use words with arbitrary chaoses, as the chaos specifies how the word interacts through events and output values.
When designing words, it is important to realize that word updating functions like word-tick do not have to return a word of the same kind.
procedure
(call-with-chaos c t) → any
c : chaos? t : (-> any)
2 The Word
A word is a generic interface the encapsulates the interactive behavior of a lux program.
The word methods are as follows:
2.1 Helpers
procedure
(lux-standard-label s frame-time) → string?
s : string? frame-time : flonum?
3 Chaos
A chaos is generic interface for an empty manifestation of an interactive space that is given form by the word and fiat-lux.
3.1 Racket GUI Chaos
(require lux/chaos/gui) | package: lux |
This module provides the standard chaos that most users of lux will use.
procedure
(make-gui [ #:mode mode #:opengl-hires? opengl-hires? #:start-fullscreen? start-fullscreen? #:frame-style frame-style #:icon icon #:width width #:height height]) → chaos?
mode :
(or/c (one-of/c 'draw 'gl-compat 'gl-core) (is-a?/c gl-config%)) = 'draw opengl-hires? : boolean? = #f start-fullscreen? : boolean? = #f frame-style : (listof symbol?) = '() icon : (or/c #f path-string? (is-a?/c bitmap%)) = #f width : exact-nonnegative-integer? = 800 height : exact-nonnegative-integer? = 600
The canvas is set up for drawing based on mode. If mode is 'draw, then the canvas assumes that racket/draw is used. If other values are used, then the canvas is drawn with OpenGL. If mode is 'gl-compat, then a compatibility OpenGL profile is used. If mode is 'gl-core, then a core OpenGL profile is used. If mode is a gl-config% object, then it is used to initialize the canvas. If opengl-hires? is #t, then the resulting gl-config% object will have high resolution mode set.
The values that word-event is called with are either 'close (for when the window’s close button is pressed), a key-event% object for when keys are pressed, or a mouse-event% object for when the mouse is used.
The values that word-output should return are functions that satisfy the contract (-> real? real? (is-a?/c dc<%>) any) where the first argument is the width of the canvas, the second is the height, and the third is the canvas’s drawing context.
3.1.1 Drawing Values
(require lux/chaos/gui/val) | package: lux |
This module provides a helpful function for drawing functional images with lux/chaos/gui.
3.1.2 Tracking Keyboard State
(require lux/chaos/gui/key) | package: lux |
This module provides a set of functions for tracking keyboard state for use inside of word-tick, rather than updating word state with each event as in word-event. Such as system may be appropriate for interactive programs where input is only has an impact at a consistent tick rate.
struct
(struct key-state ( keys shift? control? meta? alt? mod3? mod4? mod5?)) keys : hash? shift? : boolean? control? : boolean? meta? : boolean? alt? : boolean? mod3? : boolean? mod4? : boolean? mod5? : boolean?
procedure
procedure
(key-event? x) → boolean?
x : any/c
procedure
(key-event-code ke)
→ (or/c (cons/c 'release (or/c char? key-code-symbol?)) (or/c char? key-code-symbol?)) ke : key-event?
procedure
(key-state-update! ks ke) → any
ks : key-state? ke : key-event?
procedure
(key-state-set? ks kc) → boolean?
ks : key-state? kc : (or/c char? key-code-symbol?)
procedure
(key-state-set?! ks kc) → boolean?
ks : key-state? kc : (or/c char? key-code-symbol?)
3.1.3 Tracking Mouse State
(require lux/chaos/gui/mouse) | package: lux |
This module provides a set of functions for tracking mouse state for use inside of word-tick, rather than updating word state with each event as in word-event. Such as system may be appropriate for interactive programs where input is only has an impact at a consistent tick rate.
struct
(struct mouse-state ( x y left? right? middle? shift? control? meta? alt? mod3? mod4? mod5?)) x : real? y : real? left? : boolean? right? : boolean? middle? : boolean? shift? : boolean? control? : boolean? meta? : boolean? alt? : boolean? mod3? : boolean? mod4? : boolean? mod5? : boolean?
procedure
procedure
(mouse-event? x) → boolean?
x : any/c
procedure
(mouse-event-xy me) →
real? real? me : mouse-event?
procedure
(mouse-state-update! ms me) → any
ms : mouse-state? me : mouse-event?
3.2 Pair Chaos
(require lux/chaos/pair) | package: lux |
This module provides a chaos that pairs two other chaos objects for lux programs with multiple interfaces.
3.3 Implementing a Chaos
(require lux/chaos) | package: lux |
Users of lux will probably not need to implement chaoses, but will use those that are standard.
The chaos methods are as follows:
procedure
(chaos-start! c) → any
c : chaos?
procedure
(chaos-yield c e) → any
c : chaos? e : evt?
procedure
(chaos-event c) → evt?
c : chaos?
procedure
(chaos-output! c o) → any
c : chaos? o : any/c
procedure
(chaos-label! c s) → any
c : chaos? s : string?
procedure
(chaos-swap! c t) → any
c : chaos? t : (-> any)
procedure
(chaos-stop! c) → any
c : chaos?