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