gamnit: add an example to showcase fonts support
[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 used to identify the anchor coordinate of each `TextSprites`
26 var anchor = new Texture("anchor.png")
27
28 redef fun on_create
29 do
30 super
31
32 for tex in all_root_textures do
33 var error = tex.error
34 if error != null then print_error "Texture '{tex}' failed to load: {error}"
35 end
36
37 update_text
38 end
39
40 # Draw or redraw all the `TextSprites`
41 fun update_text
42 do
43 # Remove existing text and sprites
44 ui_sprites.clear
45 var texts = new Array[TextSprites]
46
47 # Shared content
48 var description = "The anchor icon identifies the coordinate of TextSprites::anchor."
49 var lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
50 var color = [0.0, 0.25, 0.5]
51
52 # ---
53 # TextSprites (the interesting part)
54
55 # Aligned text (no max_width)
56 texts.add new TextSprites(font,
57 ui_camera.top.offset(-400.0, 0.0, 0.0),
58 "Left, align=0.0:\n"+description)
59
60 texts.add new TextSprites(font,
61 ui_camera.top.offset(-400.0, -100.0, 0.0),
62 "Right, align=1.0:\n"+description, align=1.0)
63
64 texts.add new TextSprites(font,
65 ui_camera.top.offset(-400.0, -200.0, 0.0),
66 "Center, align=0.5:\n"+description, align=0.5)
67
68 texts.add new TextSprites(font,
69 ui_camera.top.offset(-400.0, -300.0, 0.0),
70 "Weird, align=0.2:\n"+description, align=0.2)
71
72 # Aligned with max width
73 texts.add new TextSprites(font,
74 ui_camera.top_left.offset(100.0, -400.0, 0.0),
75 "Left, max_width=400.0:\n"+lorem_ipsum,
76 align=0.0, max_width=400.0)
77
78 texts.add new TextSprites(font,
79 ui_camera.top_left.offset(1000.0, -400.0, 0.0),
80 "Right, max_width=400.0:\n"+lorem_ipsum,
81 align=1.0, max_width=400.0)
82
83 texts.add new TextSprites(font,
84 ui_camera.top_left.offset(300.0, -700.0, 0.0),
85 "Center, max_width=400.0:\n"+lorem_ipsum,
86 align=0.5, max_width=400.0)
87
88 texts.add new TextSprites(font,
89 ui_camera.top_left.offset(680.0, -700.0, 0.0),
90 "Weird, max_width=400.0:\n"+lorem_ipsum,
91 align=0.2, max_width=400.0)
92
93 # Max width & height
94 texts.add new TextSprites(font,
95 ui_camera.top_left.offset(1100.0, -400.0, 0.0),
96 "max_width & max_height:\n"+lorem_ipsum,
97 max_width=600.0, max_height=150.0)
98
99 # Thin max_width with overflows
100 texts.add new TextSprites(font,
101 ui_camera.top_left.offset(1100.0, -600.0, 0.0),
102 "The 1{plu}st{pld} word of a line can always overflow:\n"+lorem_ipsum,
103 max_width=100.0, max_height=400.0)
104
105 # No wrap
106 texts.add new TextSprites(font,
107 ui_camera.top_left.offset(1300.0, -600.0, 0.0),
108 "wrap=false:\nLong lines are cut short blah blah blah\n"+lorem_ipsum,
109 max_width=400.0, wrap=false)
110
111 # Bottom align
112 texts.add new TextSprites(font,
113 ui_camera.top_left.offset(1300.0, -1000.0, 0.0),
114 "valign=1.0:\n"+lorem_ipsum,
115 max_width=400.0, valign=1.0)
116
117 # Center valign
118 texts.add new TextSprites(font,
119 ui_camera.top_left.offset(1500.0, -220.0, 0.0),
120 "align=0.5, valign=0.5:\n"+lorem_ipsum,
121 max_width=400.0, align=0.5, valign=0.5)
122
123 # ---
124 # Anchors and background boxes
125
126 # Gradient background for the max_width texts
127 var box = new CustomTexture(400.0, 200.0)
128 for x in 400.times do for y in 150.times do
129 var p = 1.0-1.0*y.to_f/150.0
130 p = p.sqrt
131 box[x, y] = color + [p]
132 end
133 box.load
134 for i in [4..8[ do
135 var t = texts[i]
136 ui_sprites.add new Sprite(box,
137 t.anchor.offset((-t.align+0.5)*t.max_width.as(not null),
138 -100.0, -1.0))
139 end
140
141 # Plain boxes for max_width and max_height boxes
142 var large_box = new CustomTexture(600.0, 150.0)
143 large_box.fill color
144 large_box.load
145 ui_sprites.add new Sprite(large_box, texts[8].anchor.offset(300.0, -75.0, -1.0))
146
147 var thin_box = new CustomTexture(100.0, 400.0)
148 thin_box.fill color
149 thin_box.load
150 ui_sprites.add new Sprite(thin_box, ui_camera.top_left.offset(1150.0, -800.0, -1.0))
151
152 # Other TextSprites
153 ui_sprites.add new Sprite(box, texts[10].anchor.offset(200.0, -100.0, -1.0))
154
155 var s = new Sprite(box, texts[11].anchor.offset(200.0, 100.0, -1.0))
156 s.rotation = pi
157 ui_sprites.add s
158
159 ui_sprites.add new Sprite(box, texts[12].anchor.offset(0.0, 0.0, -1.0))
160
161 # Add the anchor effects to all TextSprites
162 for t in texts do ui_sprites.add new Sprite(anchor, t.anchor)
163 end
164
165 redef fun accept_event(event)
166 do
167 if event isa QuitEvent or
168 (event isa KeyEvent and event.name == "escape" and event.is_up) then
169 # Quit abruptly
170 exit 0
171 else if event isa KeyEvent and event.is_up then
172 update_text
173 end
174
175 return false
176 end
177 end