src: transform all old writable in annotations
[nit.git] / lib / mnit / tileset.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Manage images that are tileset or glyphset (for bitmap fonts)
16 module tileset
17
18 import mnit_display
19
20 # Efficienly retrieve tiles in a big image
21 class TileSet
22 # The image containing the tileset
23 var image: Image
24
25 # The witdh of a tile
26 var width: Int
27
28 # The height of a tile
29 var height: Int
30
31 init(image: Image, width: Int, height: Int)
32 do
33 self.image = image
34 self.width = width
35 self.height = height
36
37 self.nb_cols = image.width / width
38 self.nb_rows = image.height / height
39
40 for j in [0..nb_rows[ do
41 for i in [0..nb_cols[ do
42 subimages.add image.subimage(i*width,j*height,width,height)
43 end
44 end
45 end
46
47 # The number of columns of tiles in the image
48 var nb_cols: Int
49
50 # The number of rows of tiles in the image
51 var nb_rows: Int
52
53 # Cache for images of tiles
54 private var subimages = new Array[Image]
55
56 # The subimage of given tile
57 # Aborts if x or y are out of bound
58 fun [](x,y: Int): Image
59 do
60 assert x >= 0 and x < nb_cols and y >= 0 and y <= nb_rows else print "{x}x{y}<?{nb_cols}x{nb_rows}"
61 var idx = x + y * nb_cols
62 return subimages[idx]
63 end
64 end
65
66 # A monospace bitmap font where glyphs are stored in a tileset
67 class TileSetFont
68 super TileSet
69
70 # Each caracter in the image
71 # in left->right, then top->bottom order
72 # Use space (' ') for holes in the tileset
73 var chars: String
74
75 init(image: Image, width: Int, height: Int, chars: String)
76 do
77 super
78 self.chars = chars
79 end
80
81 # Additional space to insert horizontally between characters
82 # A negave value will display tile overlaped
83 var hspace: Int = 0 is writable
84
85 # Additional space to insert vertically between characters
86 # A negave value will display tile overlaped
87 var vspace: Int = 0 is writable
88
89 # The glyph (tile) associated to the caracter `c` according to `chars`
90 # Returns null if `c` is not in `chars`
91 fun char(c: Char): nullable Image
92 do
93 var i = chars.index_of(c)
94 if i == -1 then return null
95 return subimages[i]
96 end
97 end
98
99 redef class Display
100 # Blit the text using a monospace bitmap font
101 # '\n' are rendered as carriage return
102 fun text(text: String, font: TileSetFont, x, y: Int)
103 do
104 var cx = x
105 var cy = y
106 var sw = font.width + font.hspace
107 var sh = font.height + font.vspace
108 for c in text.chars do
109 if c == '\n' then
110 cx = x
111 cy += sh
112 continue
113 end
114 if c == ' ' then
115 cx += sw
116 continue
117 end
118 var image = font.char(c)
119 if image != null then
120 blit(image, cx, cy)
121 end
122 cx += sw
123 end
124 end
125 end