contrib & examples: update users of mnit implementations
[nit.git] / lib / mnit / assets.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Manages all assets usable by an Mnit app
18 module assets
19
20 import mnit_app
21 import mnit::display
22
23 # General asset
24 interface Asset
25 end
26
27 # An String is an asset, returned from a text file
28 redef class String
29 super Asset
30 end
31
32 # An Image is an asset
33 redef interface Image
34 super Asset
35 end
36
37 redef class App
38 # Load a genereal asset from file name
39 # Will find the file within the assets/ directory
40 # Crashes if file not found
41 fun load_asset( id: String ): Asset
42 do
43 var asset = try_loading_asset( id )
44 if asset == null then # error
45 print_error "asset <{id}> could not be loaded."
46 abort
47 else
48 return asset
49 end
50 end
51
52 # Load an Image assets
53 # Crashes if file not found or not an image
54 fun load_image( id: String ): Image
55 do
56 var asset = load_asset( id )
57 if asset isa Image then
58 return asset
59 else
60 print_error "asset <{id}> is not an image."
61 abort
62 end
63 end
64
65 # Load an assets without error if not found
66 fun try_loading_asset( id: String ): nullable Asset is abstract
67 end