Merge branch 'explain-assert' into master
[nit.git] / lib / gamnit / examples / fonts_showcase / src / fonts_showcase.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the Do What The Fuck You Want To
5 # Public License, Version 2, as published by Sam Hocevar. See
6 # http://sam.zoy.org/projects/COPYING.WTFPL for more details.
7
8 # Font support showcase
9 module fonts_showcase is
10 example
11 app_name "gamnit fonts"
12 app_namespace "org.gamnit.fonts_showcase"
13 app_version(1, 0, git_revision)
14 android_api_target 10
15 android_manifest_activity """android:screenOrientation="sensorLandscape" """
16 end
17
18 import gamnit::flat
19 import gamnit::bmfont
20
21 redef class App
22
23 # Asset font used to display text
24 var font = new BMFontAsset("Josefin_Sans/font.fnt")
25
26 # Anchor texture identifying the anchor coordinates of each `TextSprites`
27 var anchor = new Texture("anchor.png")
28
29 # Bottom right corner
30 var corner = new Texture("corner.png")
31
32 redef fun create_scene
33 do
34 super
35
36 for tex in all_root_textures do
37 var error = tex.error
38 if error != null then print_error "Texture '{tex}' failed to load: {error}"
39 end
40
41 update_text
42 end
43
44 # Draw or redraw all the `TextSprites`
45 fun update_text
46 do
47 # Remove existing text and sprites
48 ui_sprites.clear
49 var texts = new Array[TextSprites]
50
51 # Shared content
52 var description = "The anchor icon identifies the coordinate of TextSprites::anchor."
53 var lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et [dolore magna](my_link asdf) aliqua."
54 var color = [0.0, 0.25, 0.5]
55
56 # ---
57 # TextSprites (the interesting part)
58
59 # Aligned text (no max_width)
60 texts.add new TextSprites(font,
61 ui_camera.top.offset(-400.0, 0.0, 0.0),
62 "Left, align=0.0:\n"+description)
63
64 texts.add new TextSprites(font,
65 ui_camera.top.offset(-400.0, -100.0, 0.0),
66 "Right, align=1.0:\n"+description, align=1.0)
67
68 texts.add new TextSprites(font,
69 ui_camera.top.offset(-400.0, -200.0, 0.0),
70 "Center, align=0.5:\n"+description, align=0.5)
71
72 texts.add new TextSprites(font,
73 ui_camera.top.offset(-400.0, -300.0, 0.0),
74 "Weird, align=0.2:\n"+description, align=0.2)
75
76 # Aligned with max width
77 texts.add new TextSprites(font,
78 ui_camera.top_left.offset(100.0, -400.0, 0.0),
79 "Left, max_width=400.0:\n"+lorem_ipsum,
80 align=0.0, max_width=400.0)
81
82 texts.add new TextSprites(font,
83 ui_camera.top_left.offset(1000.0, -400.0, 0.0),
84 "Right, max_width=400.0, scale=0.66:\n"+lorem_ipsum,
85 align=1.0, max_width=400.0, scale=0.66)
86
87 texts.add new TextSprites(font,
88 ui_camera.top_left.offset(300.0, -700.0, 0.0),
89 "Center, max_width=400.0:\n"+lorem_ipsum,
90 align=0.5, max_width=400.0)
91
92 texts.add new TextSprites(font,
93 ui_camera.top_left.offset(680.0, -700.0, 0.0),
94 "Weird, max_width=400.0:\n"+lorem_ipsum,
95 align=0.2, max_width=400.0)
96
97 # Max width & height
98 texts.add new TextSprites(font,
99 ui_camera.top_left.offset(1100.0, -400.0, 0.0),
100 "max_width & max_height:\n"+lorem_ipsum,
101 max_width=600.0, max_height=150.0)
102
103 # Thin max_width with overflows
104 texts.add new TextSprites(font,
105 ui_camera.top_left.offset(1100.0, -600.0, 0.0),
106 "The 1{plu}st{pld} word of a line can always overflow:\n"+lorem_ipsum,
107 max_width=100.0, max_height=400.0)
108
109 # No wrap
110 texts.add new TextSprites(font,
111 ui_camera.top_left.offset(1300.0, -600.0, 0.0),
112 "wrap=false:\nLong lines are cut short blah blah blah\n"+lorem_ipsum,
113 max_width=400.0, wrap=false)
114
115 # Bottom align
116 texts.add new TextSprites(font,
117 ui_camera.top_left.offset(1300.0, -1000.0, 0.0),
118 "valign=1.0:\n"+lorem_ipsum,
119 max_width=400.0, valign=1.0)
120
121 # Center valign
122 texts.add new TextSprites(font,
123 ui_camera.top_left.offset(1500.0, -220.0, 0.0),
124 "align=0.5, valign=0.5:\n"+lorem_ipsum,
125 max_width=400.0, align=0.5, valign=0.5)
126
127 # ---
128 # Links
129
130 for ts in texts do
131 for link_name, sprites in ts.links do
132 print "Link: {link_name}"
133 for s in sprites do s.green = 0.0
134 end
135 end
136
137 # ---
138 # Anchors and background boxes
139
140 # Gradient background for the max_width texts
141 var box = new CustomTexture(400.0, 200.0)
142 for x in 400.times do for y in 150.times do
143 var p = 1.0-1.0*y.to_f/150.0
144 p = p.sqrt
145 box[x, y] = color + [p]
146 end
147 box.load
148 for i in [4..8[ do
149 var t = texts[i]
150 ui_sprites.add new Sprite(box,
151 t.anchor.offset((-t.align+0.5)*t.max_width.as(not null),
152 -100.0, -1.0))
153 end
154
155 # Plain boxes for max_width and max_height boxes
156 var large_box = new CustomTexture(600.0, 150.0)
157 large_box.fill color
158 large_box.load
159 ui_sprites.add new Sprite(large_box, texts[8].anchor.offset(300.0, -75.0, -1.0))
160
161 var thin_box = new CustomTexture(100.0, 400.0)
162 thin_box.fill color
163 thin_box.load
164 ui_sprites.add new Sprite(thin_box, ui_camera.top_left.offset(1150.0, -800.0, -1.0))
165
166 # Other TextSprites
167 ui_sprites.add new Sprite(box, texts[10].anchor.offset(200.0, -100.0, -1.0))
168
169 var s = new Sprite(box, texts[11].anchor.offset(200.0, 100.0, -1.0))
170 s.rotation = pi
171 ui_sprites.add s
172
173 ui_sprites.add new Sprite(box, texts[12].anchor.offset(0.0, 0.0, -1.0))
174
175 # Add the anchor effects to all TextSprites
176 for t in texts do ui_sprites.add new Sprite(anchor, t.anchor)
177
178 for t in texts do
179 # Bottom right
180 var br = t.anchor.offset(t.width*(1.0-t.align), -t.height*(1.0-t.valign), 1.0)
181 ui_sprites.add new Sprite(corner, br)
182 end
183 end
184
185 redef fun accept_event(event)
186 do
187 if event isa QuitEvent or
188 (event isa KeyEvent and event.name == "escape" and event.is_up) then
189 # Quit abruptly
190 exit 0
191 else if event isa KeyEvent and event.is_up then
192 update_text
193 end
194
195 return false
196 end
197 end