lib & contrib: update imports
[nit.git] / contrib / tinks / src / client / linux_client.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 # GNU/Linux client with config saved to `config.json`
16 module linux_client
17
18 import mnit::linux
19 import linux::audio
20 import json
21
22 import client
23
24 # Configuration of the client
25 class ClientConfig
26 serialize
27
28 # Resolution width
29 var res_x = 1920 is lazy
30
31 # Resolution height
32 var res_y = 1080 is lazy
33
34 # Should the client play sounds?
35 var play_sounds = true is lazy
36 end
37
38 redef class App
39 private var config_path: String = sys.program_name.dirname / "../config.json"
40
41 private var config: ClientConfig do
42 if config_path.file_exists then
43 var content = config_path.to_path.read_all
44 var deser = new JsonDeserializer(content)
45 var cc = deser.deserialize
46
47 if cc == null then
48 print_error "Client Error: Deserializing config file failed with {deser.errors.join(", ")}"
49 else if not cc isa ClientConfig then
50 print_error "Client Error: Deserializing config file failed, got '{cc}'"
51 # TODO simplify the previous lines with ? or similar
52 else return cc
53 end
54
55 # Save the default config to pretty Json
56 var cc = new ClientConfig
57 var json = cc.serialize_to_json(plain=true, pretty=true)
58 json.write_to_file config_path
59
60 return cc
61 end
62 end
63
64 redef class Display
65 redef fun wanted_width do return app.config.res_x
66 redef fun wanted_height do return app.config.res_y
67 end
68
69 redef class Sound
70 redef fun play do if app.config.play_sounds then super
71 end
72
73 redef class JsonDeserializer
74 # The only class we deserialize from pretty Json is ClientConfig
75 redef fun class_name_heuristic(object) do return "ClientConfig"
76 end