-
Notifications
You must be signed in to change notification settings - Fork 6
/
DrivingWheelView.java
339 lines (304 loc) · 15.3 KB
/
DrivingWheelView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.scrappers.superiorExtendedEngine.vehicles;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.scrappers.GamePad.R;
import com.scrappers.superiorExtendedEngine.gamePad.ControlButtonsView;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.annotation.XmlRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
/**
* An android view class , to create a drivingWheel for cars,vehicles,motorcycles,jets,etc.
* @author pavl_g
*/
public class DrivingWheelView extends RelativeLayout {
private boolean neutralizeWhenLostFocus=true;
private CardView drivingWheel;
private CardView hornHolder;
private ImageView axle;
private ImageView axle2;
private ImageView axle3;
private ImageView horn;
private CardView drivingPads;
private CardView drivingWheelEnclosure;
private float xOrigin;
private float yOrigin;
private float toleranceX=50;
private long drivingWheelAnimationDuration=500;
public OnSteering onSteering;
private CustomizeDrivingWheel customizeDrivingWheel;
private DynamicUTurn dynamicUTurn;
/**
* Driving Handle Style enclosure.
* @author pavl_g
*/
public enum HandleStyles {
//the enums
DRIVING_PADS_TINT(R.color.pureWhite), AXLE_TINT(R.color.pureWhite), DRIVING_WHEEL(R.drawable.driving_wheel),
DRIVING_WHEEL_TINT(R.color.fireEngineRed), WHEEL_AXLE(R.drawable.wheel_axle), DRIVING_PADS(R.drawable.driving_pads),
HORN_HOLDER(R.drawable.driving_handle), HORN(R.drawable.ic_air_horn);
public final int STYLE;
/**
* Make a call to a constant enum of couple of designs
* @param STYLE the style
*/
HandleStyles(int STYLE){
this.STYLE = STYLE;
}
}
public DrivingWheelView(@NonNull Context appCompatActivity) {
super(appCompatActivity);
}
public DrivingWheelView(@NonNull Context appCompatActivity, @Nullable AttributeSet attrs) {
super(appCompatActivity, attrs);
}
public DrivingWheelView(@NonNull Context appCompatActivity, @Nullable AttributeSet attrs, int defStyleAttr) {
super(appCompatActivity, attrs, defStyleAttr);
}
/**
* initialize a steering wheel with its default shape
*
*/
public void initializeWheel(){
((AppCompatActivity)getContext()).runOnUiThread(()->{
xOrigin=getLayoutParams().width/2f;
yOrigin=getLayoutParams().height/2f;
setBackground(ContextCompat.getDrawable(this.getContext(), ControlButtonsView.ButtonStyle.NOTHING_IMAGE.STYLE));
/*initialization of drivingWheel*/
drivingWheel =new CardView(getContext());
drivingWheel.setRotationX((float) Math.toDegrees(Math.PI/10));
drivingWheel.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.DRIVING_WHEEL.STYLE));
drivingWheel.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), HandleStyles.DRIVING_WHEEL_TINT.STYLE)));
ViewGroup.LayoutParams drivingWheelParams=new RelativeLayout.LayoutParams(getLayoutParams().width,getLayoutParams().height);
drivingWheel.setLayoutParams(drivingWheelParams);
drivingWheel.setX(getLayoutParams().width/2f-drivingWheelParams.width/2f);
drivingWheel.setY(getLayoutParams().height/2f-drivingWheelParams.height/2f);
addView(drivingWheel);
/* initialize,define driving wheels axles*/
axle=new ImageView(getContext());
axle.setRotationY(60);
axle.setRotationX(10);
axle.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.WHEEL_AXLE.STYLE));
axle.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), HandleStyles.AXLE_TINT.STYLE)));
//total distance of the upper half of the circle = half of the width of the view = half the circumference of the circle
// (because we wanna get the exact width of the axle after a rotation by some degree points in the +ve direction of x-axis
//general formula = length covered by an angle in unit circle = perimeter of the circle(circle frame length) * (theta/360 or the scaleFactor)
float radius=drivingWheel.getLayoutParams().width/2f;
float circumference= (float) (2*Math.PI*radius);
float scaleFactor=(axle.getRotationX()/360);
float lengthCoveredByRotationAngle=circumference*scaleFactor;
int strokeWidth=20;
//adding the values that have been cut off by the rotationX back to the width of the view
ViewGroup.LayoutParams axleParams=new RelativeLayout.LayoutParams((int) (getLayoutParams().width/2f+lengthCoveredByRotationAngle)+strokeWidth
,drivingWheelParams.height/5);
axle.setLayoutParams(axleParams);
//before the zero by #lengthCoveredByRotationAngle/6
axle.setX(-lengthCoveredByRotationAngle/6-15);
axle.setY(drivingWheel.getLayoutParams().height/2f-axleParams.height/2f);
drivingWheel.addView(axle);
/*Axle2*/
axle2=new ImageView(getContext());
axle2.setRotationY(-60);
axle2.setRotationX(10);
axle2.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.WHEEL_AXLE.STYLE));
axle2.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), HandleStyles.AXLE_TINT.STYLE)));
ViewGroup.LayoutParams axle2Params=new RelativeLayout.LayoutParams((int) (getLayoutParams().width/2+lengthCoveredByRotationAngle)+strokeWidth
,drivingWheelParams.height/5);
axle2.setLayoutParams(axle2Params);
//observatory number, donot know why
axle2.setX(drivingWheel.getLayoutParams().width/2f-(lengthCoveredByRotationAngle/1.2f));
axle2.setY(drivingWheel.getLayoutParams().height/2f-axle2Params.height/2f);
drivingWheel.addView(axle2);
/*Axle3*/
axle3=new ImageView(getContext());
axle3.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.WHEEL_AXLE.STYLE));
axle3.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), HandleStyles.AXLE_TINT.STYLE)));
ViewGroup.LayoutParams axle3Params=new RelativeLayout.LayoutParams(getLayoutParams().width,drivingWheelParams.height/5);
axle3.setLayoutParams(axle3Params);
axle3.setX(drivingWheel.getLayoutParams().width/2f-axle3Params.width/2f);
/*before the height of the drivingWheel by a distance stated as the quarter of its height*/
axle3.setY(drivingWheel.getLayoutParams().height-getLayoutParams().height/4f);
axle3.setRotation(90);
axle3.setRotationX(40);
drivingWheel.addView(axle3);
/*define the drivingWheelPads*/
drivingPads=new CardView(getContext());
drivingPads.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.DRIVING_PADS.STYLE));
drivingPads.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), HandleStyles.DRIVING_PADS_TINT.STYLE)));
ViewGroup.LayoutParams drivingPadsParams=new RelativeLayout.LayoutParams(getLayoutParams().width,getLayoutParams().height);
drivingPads.setLayoutParams(drivingPadsParams);
drivingPads.setX(getLayoutParams().width/2f-drivingWheelParams.width/2f);
drivingPads.setY(getLayoutParams().height/2f-drivingWheelParams.height/2f);
drivingPads.setRadius(drivingPadsParams.width/2f);
drivingPads.setRotation(-35);
/*define the hornHolder view*/
hornHolder=new CardView(drivingWheel.getContext());
hornHolder.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.HORN_HOLDER.STYLE));
hornHolder.setRotationX(drivingWheel.getRotationX());
hornHolder.setLayoutParams(new RelativeLayout.LayoutParams(drivingWheelParams.width/3, drivingWheelParams.height/3));
hornHolder.setX(drivingWheel.getLayoutParams().width/2f-hornHolder.getLayoutParams().width/2f);
//midpoint y-coordinate lies on 1/3 the height of the hornView
hornHolder.setY(drivingWheel.getLayoutParams().height/2f-hornHolder.getLayoutParams().height/3f);
hornHolder.setRadius(hornHolder.getLayoutParams().width/1.5f);
drivingWheel.addView(hornHolder);
/*define hornHolder image*/
horn=new ImageView(hornHolder.getContext());
horn.setImageDrawable(ContextCompat.getDrawable(getContext(), HandleStyles.HORN.STYLE));
horn.setLayoutParams(new RelativeLayout.LayoutParams(hornHolder.getLayoutParams().width/2,hornHolder.getLayoutParams().height/2));
horn.setX(hornHolder.getLayoutParams().width/2f-horn.getLayoutParams().width/2f);
horn.setY(hornHolder.getLayoutParams().height/2f-horn.getLayoutParams().height/2f);
hornHolder.addView(horn);
/* the driving wheel enclosure is the top part part of the driving wheel with same color*/
drivingWheelEnclosure=new CardView(getContext());
drivingWheelEnclosure.setBackground(ContextCompat.getDrawable(getContext(), HandleStyles.DRIVING_WHEEL.STYLE));
drivingWheelEnclosure.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), HandleStyles.DRIVING_WHEEL_TINT.STYLE)));
drivingWheelEnclosure.setLayoutParams(drivingWheel.getLayoutParams());
drivingWheelEnclosure.setX(drivingWheel.getX());
drivingWheelEnclosure.setY(drivingWheel.getY());
drivingWheel.addView(drivingWheelEnclosure);
drivingWheelEnclosure.addView(drivingPads);
if(customizeDrivingWheel!=null){
customizeDrivingWheel.customize(drivingWheel);
}
});
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
float xMotion;
float yMotion;
float xStart=0.0f;
float yStart=0.0f;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
xStart=event.getX();
yStart=event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
final float toleranceX=this.toleranceX;
//2 is angle scale factor
final float angleScaleFactor=2f;
xMotion = event.getX() - xStart;
yMotion=event.getY()-yStart;
float angleOfRotation = (float) Math.toDegrees(Math.atan2(yMotion, xMotion)) * angleScaleFactor;
//creating a new origin point after the center of the view to control the rotation among +ve direction of x-axis
if(event.getX()>xOrigin+toleranceX){
//Right Steering
((AppCompatActivity)getContext()).runOnUiThread(()-> drivingWheel.setRotation(angleOfRotation));
if(onSteering!=null){
onSteering.steerRight((float) Math.atan2(yMotion, xMotion));
}
if(dynamicUTurn!=null){
dynamicUTurn.animateRight();
}
//creating a new origin point before the center of the view to control the rotation among -ve direction of x-axis
}else if(event.getX()<xOrigin-toleranceX){
//Left Steering
((AppCompatActivity)getContext()).runOnUiThread(()-> drivingWheel.setRotation(-angleOfRotation));
if(onSteering!=null){
onSteering.steerLeft(-(float) Math.atan2(yMotion, xMotion));
}
if(dynamicUTurn!=null){
dynamicUTurn.animateLeft();
}
}
invalidate();
break;
case MotionEvent.ACTION_UP:
if(neutralizeWhenLostFocus){
if ( drivingWheel.getRotation() != 0 ){
((AppCompatActivity) getContext()).runOnUiThread(() -> {
drivingWheel.animate().setDuration(drivingWheelAnimationDuration).rotation(0);
});
}
if(onSteering!=null){
onSteering.neutralize(0);
}
if(dynamicUTurn!=null){
dynamicUTurn.hide();
}
invalidate();
}
break;
}
return true;
}
public ImageView getHorn() {
return horn;
}
/**
* declares by how far the driving wheel would respond from the midpoint of the drivingWheelView
* @apiNote this number would by added to the xOrigin point in case of +ve x-axis rotation(clock-wise) ,
* & would be subtracted from the xOrigin in case of -ve x-axis rotation(anti-clockwise).
* @param toleranceX the float number of the x tolerance , the default value is 50.
*/
public void setToleranceX(float toleranceX) {
this.toleranceX = toleranceX;
}
public void setDrivingWheelAnimationDuration(long drivingWheelAnimationDuration) {
this.drivingWheelAnimationDuration = drivingWheelAnimationDuration;
}
public CardView getDrivingWheel() {
return drivingWheel;
}
public CardView getHornHolder() {
return hornHolder;
}
public ImageView getAxle() {
return axle;
}
public ImageView getAxle2() {
return axle2;
}
public ImageView getAxle3() {
return axle3;
}
public CardView getDrivingPads() {
return drivingPads;
}
public CardView getDrivingWheelEnclosure() {
return drivingWheelEnclosure;
}
public long getDrivingWheelAnimationDuration() {
return drivingWheelAnimationDuration;
}
public interface OnSteering{
void steerRight(float angle);
void steerLeft(float angle);
void neutralize(float angle);
}
public void setOnSteering(OnSteering onSteering) {
this.onSteering = onSteering;
}
public interface CustomizeDrivingWheel{
void customize(CardView parentView);
}
public void setCustomizeDrivingWheel(CustomizeDrivingWheel customizeDrivingWheel) {
this.customizeDrivingWheel = customizeDrivingWheel;
}
public void setNeutralizeWhenLostFocus(boolean neutralizeWhenLostFocus) {
this.neutralizeWhenLostFocus = neutralizeWhenLostFocus;
}
public boolean isNeutralizeWhenLostFocus() {
return neutralizeWhenLostFocus;
}
public interface DynamicUTurn{
void animateRight();
void animateLeft();
void hide();
}
public void setDynamicUTurn(DynamicUTurn dynamicUTurn) {
this.dynamicUTurn = dynamicUTurn;
}
}