Merge: nitc: check pkg-config packages availability later
[nit.git] / lib / ios / glkit.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 # GLKit services to create an OpenGL ES context on iOS
16 module glkit
17
18 #import glesv2
19 import ios
20
21 in "ObjC Header" `{
22 #import <GLKit/GLKit.h>
23
24 // Nit controller for games
25 @interface GameViewController : GLKViewController
26
27 // Nit object receiving callbacks
28 @property void* nit_glk_view;
29 @end
30 `}
31
32 in "ObjC" `{
33
34 @implementation GameViewController
35
36 - (void)update
37 {
38 NitGLKView_update((NitGLKView)self.nit_glk_view);
39 }
40
41 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
42 {
43 long res = NitGLKView_supported_interface_orientations((NitGLKView)self.nit_glk_view);
44 if (res == 0) return [super supportedInterfaceOrientations];
45 return (UIInterfaceOrientationMask)res;
46 }
47
48 - (BOOL)shouldAutorotate
49 {
50 return NitGLKView_should_autorotate((NitGLKView)self.nit_glk_view);
51 }
52
53 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
54 {
55 NitGLKView_touches_began((NitGLKView)self.nit_glk_view, touches, event);
56 }
57
58 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
59 {
60 NitGLKView_touches_moved((NitGLKView)self.nit_glk_view, touches, event);
61 }
62
63 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
64 {
65 NitGLKView_touches_ended((NitGLKView)self.nit_glk_view, touches, event);
66 }
67
68 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
69 {
70 NitGLKView_touches_cancelled((NitGLKView)self.nit_glk_view, touches, event);
71 }
72
73 - (void)viewWillTransitionToSize:(CGSize)size
74 withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
75 NitGLKView_view_will_transition_to_size((NitGLKView)self.nit_glk_view, size.width, size.height);
76 }
77 @end
78 `}
79
80 # Wrapper for both an Objective-C `GLKViewController` and its `GLKView`
81 private extern class NativeGLKViewController in "ObjC" `{ GLKViewController * `}
82 super NSObject
83
84 fun content_scale_factor: Float in "ObjC" `{ return self.view.contentScaleFactor; `}
85 fun drawable_width: Int in "ObjC" `{ return ((GLKView*)self.view).drawableWidth; `}
86 fun drawable_height: Int in "ObjC" `{ return ((GLKView*)self.view).drawableHeight; `}
87 fun bind_drawable in "ObjC" `{ [((GLKView*)self.view) bindDrawable]; `}
88
89 fun multiple_touch_enabled: Bool in "ObjC" `{ return [self.view isMultipleTouchEnabled]; `}
90 fun multiple_touch_enabled=(val: Bool) in "ObjC" `{ return [self.view setMultipleTouchEnabled: val]; `}
91 end
92
93 # OpenGL view controller
94 class NitGLKView
95 private var native: NativeGLKViewController = setup(app.app_delegate)
96
97 # Scale factor from logical coordinate space to the device coordinate space
98 fun content_scale_factor: Float do return native.content_scale_factor
99
100 # Width of the underlying framebuffer
101 fun drawable_width: Int do return native.drawable_width
102
103 # Height of the underlying framebuffer
104 fun drawable_height: Int do return native.drawable_height
105
106 # Bind the view framebuffer
107 fun bind_drawable do native.bind_drawable
108
109 private fun setup(app_delegate: AppDelegate): NativeGLKViewController
110 import touches_began, touches_moved, touches_ended, touches_cancelled,
111 update, should_autorotate, supported_interface_orientations,
112 view_will_transition_to_size in "ObjC" `{
113
114 app_delegate.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
115 app_delegate.window.backgroundColor = [UIColor whiteColor]; // TODO make configurable
116
117 // Create EAGL context and view
118 EAGLContext * context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
119 GLKView *view = [[GLKView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
120 view.context = context;
121
122 // Ask for antialiasing
123 view.drawableMultisample = GLKViewDrawableMultisample4X;
124 view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
125
126 GameViewController *cont = [[GameViewController alloc] init];
127 cont.view = view;
128
129 // Setup callbacks
130 NitGLKView_incr_ref(self);
131 cont.nit_glk_view = self;
132
133 // Make our controller the root
134 view.delegate = cont;
135 [app_delegate.window setRootViewController: cont];
136
137 // Enable the context
138 [app_delegate.window makeKeyAndVisible];
139 [EAGLContext setCurrentContext: context];
140 [view bindDrawable];
141
142 return cont;
143 `}
144
145 # Is multi-touch supported?
146 fun multiple_touch_enabled: Bool do return native.multiple_touch_enabled
147
148 # Is multi-touch supported?
149 fun multiple_touch_enabled=(val: Bool) do native.multiple_touch_enabled = val
150
151 # Should the view auto rotate to follow the device orientation?
152 #
153 # Defaults to `true`.
154 fun should_autorotate: Bool do return true
155
156 # If `should_autorotate`, what are the supported interface orientations?
157 #
158 # Redef to return values of Objective-C `UIInterfaceOrientationMask`
159 fun supported_interface_orientations: Int do return 0
160
161 # Hook to update the view content, called once per frame
162 fun update do end
163
164 # Hook on a new touch event
165 fun touches_began(touches: NSSet_UITouch, event: UIEvent) do end
166
167 # Hook when a touch moves
168 fun touches_moved(touches: NSSet_UITouch, event: UIEvent) do end
169
170 # Hook on the end of a touch event
171 fun touches_ended(touches: NSSet_UITouch, event: UIEvent) do end
172
173 # Hook on a touch event cancellation
174 fun touches_cancelled(touches: NSSet_UITouch, event: UIEvent) do end
175
176 # Hook when size of the view is about to change to `width` by `height`
177 fun view_will_transition_to_size(width, height: Float) do end
178 end
179
180 # UIKit event
181 extern class UIEvent in "ObjC" `{ UIEvent * `}
182 super NSObject
183 end
184
185 # Objective-C `NSSet` of `UITouch`
186 extern class NSSet_UITouch in "ObjC" `{ NSSet<UITouch*>* `}
187 super NSObject
188
189 # Get any object of this set
190 fun any_object: UITouch in "ObjC" `{ return [self anyObject]; `}
191 end
192
193 # UIKit touch event
194 extern class UITouch in "ObjC" `{ UITouch * `}
195
196 # X coordinate
197 fun x: Float in "ObjC" `{ return [self locationInView:self.view].x; `}
198
199 # Y coordinate
200 fun y: Float in "ObjC" `{ return [self locationInView:self.view].y; `}
201
202 # Address of this object as an integer for identity detection
203 fun to_i: Int in "ObjC" `{ return (long)self; `}
204 end