]> gitweb.factorcode.org Git - factor.git/blob - library/ui/world.factor
139de9e0e9dd8cb9f093011134c4a77f62d45ea7
[factor.git] / library / ui / world.factor
1 ! Copyright (C) 2005, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: gadgets
4 USING: arrays errors freetype gadgets-frames generic hashtables
5 kernel math models namespaces opengl sequences ;
6
7 ! The world gadget is the top level gadget that all (visible)
8 ! gadgets are contained in. There is one world per top-level
9 ! native window.
10
11 ! fonts: mapping font tuples to sprite vectors
12 ! handle: native resource
13 ! loc: location of native window on the screen.
14 !   we don't store this in the world's rect-loc, since the
15 !   co-ordinate system might be different, and generally the
16 !   UI code assumes that everything starts at { 0 0 }.
17 TUPLE: world
18 active?
19 gadget glass
20 title status
21 focus focused?
22 fonts handle
23 loc ;
24
25 SYMBOL: menu-mode?
26
27 : free-fonts ( world -- )
28     dup world-handle select-gl-context
29     world-fonts hash-values [ second free-sprites ] each ;
30
31 DEFER: request-focus
32
33 C: world ( gadget -- world )
34     f <model> over set-world-status
35     [ >r dup gadget-title r> set-world-title ] keep
36     { { f set-world-gadget f @center } } make-frame*
37     t over set-gadget-root?
38     t over set-world-active?
39     H{ } clone over set-world-fonts
40     { 0 0 } over set-world-loc
41     dup world-gadget request-focus ;
42
43 : find-world [ world? ] find-parent ;
44
45 M: world pref-dim*
46     delegate pref-dim* [ >fixnum ] map { 1024 768 } vmin ;
47
48 M: world graft*
49     dup dup world-title add-connection
50     dup dup world-status add-connection
51     model-changed ;
52
53 M: world ungraft*
54     dup
55     dup world-title remove-connection
56     dup world-status remove-connection ;
57
58 M: world model-changed
59     dup world-title model-value swap set-title ;
60
61 : focused-ancestors ( world -- seq )
62     world-focus parents <reversed> ;
63
64 : font-sprites ( font world -- pair )
65     world-fonts [ lookup-font V{ } clone 2array ] cache ;
66
67 : draw-string ( font string -- )
68     >r world get font-sprites first2 r> (draw-string) ;
69
70 M: world gadget-title world-gadget gadget-title ;
71
72 M: world layout*
73     dup delegate layout*
74     dup world-glass [
75         >r dup rect-dim r> set-layout-dim
76     ] when* drop ;
77
78 : hide-glass ( world -- )
79     f menu-mode? set-global
80     dup world-glass [ unparent ] when*
81     f swap set-world-glass ;
82
83 : show-glass ( gadget world -- )
84     [ hide-glass ] keep
85     [ add-gadget ] 2keep
86     set-world-glass ;