fdb94093e995d9bad8aa920b78d36b61b2cc456e
[nit.git] / lib / mnit / mnit_app.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 # General Mnit application structure
18 module mnit_app
19
20 import mnit_display
21
22 # An App instance serves as base to every Mnit projects.
23 #
24 # This class is redefed by plateforme modules and so
25 # App can be specialized directly in the user app.
26 abstract class App
27 type IE: InputEvent
28 type D: Display
29 type I: Image
30
31 # Display to use by apps
32 # Is null if the display is not available or not yet ready
33 var display: nullable D protected writable = null
34
35 # Received quit order
36 var quit: Bool writable = false
37
38 init do end
39
40 # App is visible? (vs minimized or in background)
41 fun visible: Bool is abstract
42
43 # Invoqued at each frame
44 # Usually you want to redef frame_core instead of this
45 fun full_frame
46 do
47 var display = self.display
48 if display != null then
49 display.begin
50 frame_core( display )
51 display.finish
52 end
53 end
54
55 # Main frame method to redef
56 # Is called between readying display and flipping it
57 fun frame_core( display: D ) is abstract
58
59 #fun start do end
60 #fun stop do end
61 #fun destroy do end
62
63 # Called when asked by the system (mainly for Android)
64 fun save do end
65
66 # Called when asked by the system (mainly for Android)
67 fun pause do end
68
69 # Called when asked by the system (mainly for Android)
70 fun resume do end
71
72 # System notification
73 fun gained_focus do end
74
75 # System notification
76 fun lost_focus do end
77
78 # Main init method for graphical stuff
79 # Is called when display is ready so graphical assets
80 # can be loaded at this time.
81 fun init_window do end
82
83 # Called before destroying the window
84 fun term_window do end
85
86 # Helper function for logging
87 fun log_error( msg: String ) do print "#nit error: {msg}"
88
89 # Helper function for logging
90 fun log_warning( msg: String ) do print "#nit warn: {msg}"
91
92 # Helper function for logging
93 fun log_info( msg: String ) do print "#nit info: {msg}"
94
95 # Receive and deal with all inputs
96 fun input( event: InputEvent ): Bool
97 do
98 return false
99 end
100
101 # Internal method to generate inputs
102 protected fun generate_input is abstract
103
104 # Main app loop
105 # Usually you want to redef framw_core instead of this
106 fun main_loop
107 do
108 while not quit do
109 generate_input
110 full_frame
111 end
112 end
113 end