1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Android client with a joystick
16 module android_client
is
18 app_namespace
"net.xymus.tinks"
19 android_manifest
"""<uses-permission android:name="android.permission.INTERNET" />"""
25 import android
::vibration
26 import android
::landscape
33 # Tank direction control
34 var joystick
= new Joystick is lazy
36 redef var controls
= new Array[Control].with_items
(joystick
) is lazy
38 redef fun input
(event
)
40 var local_player
= context
.local_player
41 var local_tank
= local_tank
42 if local_player
!= null and local_tank
!= null and event
isa ControlEvent then
43 local_player
.orders
.add
new TankDirectionOrder(local_tank
, joystick
.value_x
, joystick
.value_y
)
47 if event
isa AndroidKeyEvent then
48 if event
.is_back_key
then
50 native_activity
.finish
59 redef class ExplosionEvent
60 redef fun client_react
(display
, turn
)
64 var local_tank
= app
.local_tank
66 if local_tank
!= null then
67 d
= local_tank
.pos
.dist
(pos
).to_i
72 app
.vibrator
.vibrate d
76 # On-demand joystick that popups up when tapping the left border of the screen
80 # Current position of the joystick from its center on the X axis, in `[-1.0..1.0]`
83 # Current position of the joystick from its center on the Y axis, in `[-1.0..1.0]`
86 # Deadzone at the center of the joystick where the values are set at 0.0
89 # Position of the center of the joystick, set at where the screen is first tapped
90 var center
: nullable ScreenPos
92 # Position of the top of the joystick, the one that follows the finger
93 var handle
: nullable ScreenPos
95 # Id of the pointer/finger that triggered the joystick
96 var captured_pointer
: Int = -1
98 # Image of the left border
99 var img_zone
: Image = app
.assets
.drawing
.joystick_zone
101 # Image of the joystick base
102 var img_back
: Image = app
.assets
.drawing
.joystick_back
104 # Image of the top of the joystick
105 var img_handle
: Image = app
.assets
.drawing
.joystick_handle
107 # Radius where the top of the joystick can move around the center
108 var radius
: Float = img_back
.width
.to_f
/ 2.0 - 4.0
110 # Width of the left border used to trigger the joystick
111 var capture_width
= 400.0
113 redef fun draw
(display
)
115 display
.blit_stretched
(img_zone
,
117 0, display
.height
+128,
118 capture_width
, display
.height
+128,
123 if center
!= null and handle
!= null then
125 img_handle
.scale
= 1.0
126 display
.blit_centered
(img_back
, center
.x
, center
.y
)
127 display
.blit_centered
(img_handle
, handle
.x
, handle
.y
)
131 redef fun input
(event
)
133 if event
isa AndroidPointerEvent then
135 if center
== null then
137 print
"New joystick? {event.just_went_down} {event.x} < {capture_width}"
138 if event
.just_went_down
and
139 event
.x
< capture_width
then
141 self.center
= new ScreenPos(event
.x
, event
.y
)
142 self.captured_pointer
= event
.pointer_id
149 # Is it the finger already on the joystick?
150 if captured_pointer
!= event
.pointer_id
then return false
152 if event
.depressed
then
159 var dx
= center
.x
- event
.x
160 var dy
= center
.y
- event
.y
# This is inverted, as is the input
161 var d
= dx
.hypot_with
(dy
)
167 var a
= atan2
(dx
, dy
)+pi
/2.0
171 if d
> radius
then d
= radius
172 self.handle
= new ScreenPos(center
.x
+a
.cos
*d
, center
.y-a
.sin
*d
)
176 app
.input
new ControlEvent(self)