summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Bubble.java
blob: e048e1b22834e3af240ed7a1f45c987143fe465b (plain)
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
package com.mapbox.mapboxsdk.annotations;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;

@Deprecated
class Bubble extends Drawable {

  private RectF rect;
  private float arrowWidth;
  private float arrowHeight;
  private float arrowPosition;
  private float cornersRadius;
  @NonNull
  private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  private float strokeWidth;
  private Paint strokePaint;
  private Path strokePath;
  @NonNull
  private Path path = new Path();

  Bubble(@NonNull RectF rect, @NonNull ArrowDirection arrowDirection, float arrowWidth, float arrowHeight,
         float arrowPosition, float cornersRadius, int bubbleColor, float strokeWidth, int strokeColor) {
    this.rect = rect;
    this.arrowWidth = arrowWidth;
    this.arrowHeight = arrowHeight;
    this.arrowPosition = arrowPosition;
    this.cornersRadius = cornersRadius;
    paint.setColor(bubbleColor);
    this.strokeWidth = strokeWidth;

    if (strokeWidth > 0) {
      strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      strokePaint.setColor(strokeColor);
      strokePath = new Path();
      initPath(arrowDirection, path, strokeWidth);
      initPath(arrowDirection, strokePath, 0);
    } else {
      initPath(arrowDirection, path, 0);
    }
  }

  @Override
  protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
  }

  @Override
  public void draw(@NonNull Canvas canvas) {
    if (strokeWidth > 0) {
      canvas.drawPath(strokePath, strokePaint);
    }
    canvas.drawPath(path, paint);
  }

  @Override
  public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
  }

  @Override
  public void setAlpha(int alpha) {
    paint.setAlpha(alpha);
  }

  @Override
  public void setColorFilter(ColorFilter cf) {
    paint.setColorFilter(cf);
  }

  @Override
  public int getIntrinsicWidth() {
    return (int) rect.width();
  }

  @Override
  public int getIntrinsicHeight() {
    return (int) rect.height();
  }

  private void initPath(@NonNull ArrowDirection arrowDirection, @NonNull Path path, float strokeWidth) {
    switch (arrowDirection.getValue()) {
      case ArrowDirection.LEFT:
        if (cornersRadius <= 0) {
          initLeftSquarePath(rect, path, strokeWidth);
          break;
        }

        if (strokeWidth > 0 && strokeWidth > cornersRadius) {
          initLeftSquarePath(rect, path, strokeWidth);
          break;
        }

        initLeftRoundedPath(rect, path, strokeWidth);
        break;
      case ArrowDirection.TOP:
        if (cornersRadius <= 0) {
          initTopSquarePath(rect, path, strokeWidth);
          break;
        }

        if (strokeWidth > 0 && strokeWidth > cornersRadius) {
          initTopSquarePath(rect, path, strokeWidth);
          break;
        }

        initTopRoundedPath(rect, path, strokeWidth);
        break;
      case ArrowDirection.RIGHT:
        if (cornersRadius <= 0) {
          initRightSquarePath(rect, path, strokeWidth);
          break;
        }

        if (strokeWidth > 0 && strokeWidth > cornersRadius) {
          initRightSquarePath(rect, path, strokeWidth);
          break;
        }

        initRightRoundedPath(rect, path, strokeWidth);
        break;
      case ArrowDirection.BOTTOM:
        if (cornersRadius <= 0) {
          initBottomSquarePath(rect, path, strokeWidth);
          break;
        }

        if (strokeWidth > 0 && strokeWidth > cornersRadius) {
          initBottomSquarePath(rect, path, strokeWidth);
          break;
        }

        initBottomRoundedPath(rect, path, strokeWidth);
        break;
    }
  }

  private void initLeftSquarePath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(arrowWidth + rect.left + strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.width() - strokeWidth, rect.top + strokeWidth);

    path.lineTo(rect.right - strokeWidth, rect.bottom - strokeWidth);

    path.lineTo(rect.left + arrowWidth + strokeWidth, rect.bottom - strokeWidth);

    path.lineTo(rect.left + arrowWidth + strokeWidth, arrowHeight + arrowPosition - (strokeWidth / 2));
    path.lineTo(rect.left + strokeWidth + strokeWidth, arrowPosition + arrowHeight / 2);
    path.lineTo(rect.left + arrowWidth + strokeWidth, arrowPosition + (strokeWidth / 2));

    path.lineTo(rect.left + arrowWidth + strokeWidth, rect.top + strokeWidth);

    path.close();
  }

  private void initLeftRoundedPath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(arrowWidth + rect.left + cornersRadius + strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.width() - cornersRadius - strokeWidth, rect.top + strokeWidth);
    path.arcTo(new RectF(rect.right - cornersRadius, rect.top + strokeWidth, rect.right - strokeWidth,
      cornersRadius + rect.top), 270, 90);

    path.lineTo(rect.right - strokeWidth, rect.bottom - cornersRadius - strokeWidth);
    path.arcTo(new RectF(rect.right - cornersRadius, rect.bottom - cornersRadius,
      rect.right - strokeWidth, rect.bottom - strokeWidth), 0, 90);

    path.lineTo(rect.left + arrowWidth + cornersRadius + strokeWidth, rect.bottom - strokeWidth);

    path.arcTo(new RectF(rect.left + arrowWidth + strokeWidth, rect.bottom - cornersRadius,
      cornersRadius + rect.left + arrowWidth, rect.bottom - strokeWidth), 90, 90);

    path.lineTo(rect.left + arrowWidth + strokeWidth, arrowHeight + arrowPosition - (strokeWidth / 2));

    path.lineTo(rect.left + strokeWidth + strokeWidth, arrowPosition + arrowHeight / 2);

    path.lineTo(rect.left + arrowWidth + strokeWidth, arrowPosition + (strokeWidth / 2));

    path.lineTo(rect.left + arrowWidth + strokeWidth, rect.top + cornersRadius + strokeWidth);

    path.arcTo(new RectF(rect.left + arrowWidth + strokeWidth, rect.top + strokeWidth, cornersRadius
      + rect.left + arrowWidth, cornersRadius + rect.top), 180, 90);

    path.close();
  }

  private void initTopSquarePath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(rect.left + arrowPosition + strokeWidth, rect.top + arrowHeight + strokeWidth);

    path.lineTo(rect.left + arrowPosition + (strokeWidth / 2), rect.top + arrowHeight + strokeWidth);
    path.lineTo(rect.left + arrowWidth / 2 + arrowPosition, rect.top + strokeWidth + strokeWidth);
    path.lineTo(rect.left + arrowWidth + arrowPosition - (strokeWidth / 2), rect.top + arrowHeight + strokeWidth);
    path.lineTo(rect.right - strokeWidth, rect.top + arrowHeight + strokeWidth);

    path.lineTo(rect.right - strokeWidth, rect.bottom - strokeWidth);

    path.lineTo(rect.left + strokeWidth, rect.bottom - strokeWidth);

    path.lineTo(rect.left + strokeWidth, rect.top + arrowHeight + strokeWidth);

    path.lineTo(rect.left + arrowPosition + strokeWidth, rect.top + arrowHeight + strokeWidth);

    path.close();
  }

  private void initTopRoundedPath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(rect.left + Math.min(arrowPosition, cornersRadius) + strokeWidth, rect.top + arrowHeight
      + strokeWidth);
    path.lineTo(rect.left + arrowPosition + (strokeWidth / 2), rect.top + arrowHeight + strokeWidth);
    path.lineTo(rect.left + arrowWidth / 2 + arrowPosition, rect.top + strokeWidth + strokeWidth);
    path.lineTo(rect.left + arrowWidth + arrowPosition - (strokeWidth / 2), rect.top + arrowHeight + strokeWidth);
    path.lineTo(rect.right - cornersRadius - strokeWidth, rect.top + arrowHeight + strokeWidth);

    path.arcTo(new RectF(rect.right - cornersRadius,
        rect.top + arrowHeight + strokeWidth, rect.right - strokeWidth, cornersRadius + rect.top + arrowHeight),
      270, 90);
    path.lineTo(rect.right - strokeWidth, rect.bottom - cornersRadius - strokeWidth);

    path.arcTo(new RectF(rect.right - cornersRadius, rect.bottom - cornersRadius,
      rect.right - strokeWidth, rect.bottom - strokeWidth), 0, 90);
    path.lineTo(rect.left + cornersRadius + strokeWidth, rect.bottom - strokeWidth);

    path.arcTo(new RectF(rect.left + strokeWidth, rect.bottom - cornersRadius,
      cornersRadius + rect.left, rect.bottom - strokeWidth), 90, 90);

    path.lineTo(rect.left + strokeWidth, rect.top + arrowHeight + cornersRadius + strokeWidth);

    path.arcTo(new RectF(rect.left + strokeWidth, rect.top + arrowHeight + strokeWidth, cornersRadius
      + rect.left, cornersRadius + rect.top + arrowHeight), 180, 90);

    path.close();
  }

  private void initRightSquarePath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(rect.left + strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.width() - arrowWidth - strokeWidth, rect.top + strokeWidth);

    path.lineTo(rect.right - arrowWidth - strokeWidth, arrowPosition + (strokeWidth / 2));
    path.lineTo(rect.right - strokeWidth - strokeWidth, arrowPosition + arrowHeight / 2);
    path.lineTo(rect.right - arrowWidth - strokeWidth, arrowPosition + arrowHeight - (strokeWidth / 2));

    path.lineTo(rect.right - arrowWidth - strokeWidth, rect.bottom - strokeWidth);

    path.lineTo(rect.left + strokeWidth, rect.bottom - strokeWidth);
    path.lineTo(rect.left + strokeWidth, rect.top + strokeWidth);

    path.close();
  }

  private void initRightRoundedPath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(rect.left + cornersRadius + strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.width() - cornersRadius - arrowWidth - strokeWidth, rect.top + strokeWidth);
    path.arcTo(new RectF(rect.right - cornersRadius - arrowWidth,
      rect.top + strokeWidth, rect.right - arrowWidth - strokeWidth, cornersRadius + rect.top), 270, 90);

    path.lineTo(rect.right - arrowWidth - strokeWidth, arrowPosition + (strokeWidth / 2));
    path.lineTo(rect.right - strokeWidth - strokeWidth, arrowPosition + arrowHeight / 2);
    path.lineTo(rect.right - arrowWidth - strokeWidth, arrowPosition + arrowHeight - (strokeWidth / 2));
    path.lineTo(rect.right - arrowWidth - strokeWidth, rect.bottom - cornersRadius - strokeWidth);

    path.arcTo(new RectF(rect.right - cornersRadius - arrowWidth, rect.bottom - cornersRadius,
      rect.right - arrowWidth - strokeWidth, rect.bottom - strokeWidth), 0, 90);
    path.lineTo(rect.left + arrowWidth + strokeWidth, rect.bottom - strokeWidth);

    path.arcTo(new RectF(rect.left + strokeWidth, rect.bottom - cornersRadius,
      cornersRadius + rect.left, rect.bottom - strokeWidth), 90, 90);

    path.arcTo(new RectF(rect.left + strokeWidth, rect.top + strokeWidth, cornersRadius
      + rect.left, cornersRadius + rect.top), 180, 90);
    path.close();
  }

  private void initBottomSquarePath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(rect.left + strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.right - strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.right - strokeWidth, rect.bottom - arrowHeight - strokeWidth);

    path.lineTo(rect.left + arrowWidth + arrowPosition - (strokeWidth / 2), rect.bottom - arrowHeight - strokeWidth);
    path.lineTo(rect.left + arrowPosition + arrowWidth / 2, rect.bottom - strokeWidth - strokeWidth);
    path.lineTo(rect.left + arrowPosition + (strokeWidth / 2), rect.bottom - arrowHeight - strokeWidth);
    path.lineTo(rect.left + arrowPosition + strokeWidth, rect.bottom - arrowHeight - strokeWidth);

    path.lineTo(rect.left + strokeWidth, rect.bottom - arrowHeight - strokeWidth);
    path.lineTo(rect.left + strokeWidth, rect.top + strokeWidth);
    path.close();
  }

  private void initBottomRoundedPath(@NonNull RectF rect, @NonNull Path path, float strokeWidth) {
    path.moveTo(rect.left + cornersRadius + strokeWidth, rect.top + strokeWidth);
    path.lineTo(rect.width() - cornersRadius - strokeWidth, rect.top + strokeWidth);
    path.arcTo(new RectF(rect.right - cornersRadius,
      rect.top + strokeWidth, rect.right - strokeWidth, cornersRadius + rect.top), 270, 90);

    path.lineTo(rect.right - strokeWidth, rect.bottom - arrowHeight - cornersRadius - strokeWidth);
    path.arcTo(new RectF(rect.right - cornersRadius, rect.bottom - cornersRadius - arrowHeight,
      rect.right - strokeWidth, rect.bottom - arrowHeight - strokeWidth), 0, 90);

    path.lineTo(rect.left + arrowWidth + arrowPosition - (strokeWidth / 2), rect.bottom - arrowHeight - strokeWidth);
    path.lineTo(rect.left + arrowPosition + arrowWidth / 2, rect.bottom - strokeWidth - strokeWidth);
    path.lineTo(rect.left + arrowPosition + (strokeWidth / 2), rect.bottom - arrowHeight - strokeWidth);
    path.lineTo(rect.left + Math.min(cornersRadius, arrowPosition) + strokeWidth, rect.bottom - arrowHeight
      - strokeWidth);

    path.arcTo(new RectF(rect.left + strokeWidth, rect.bottom - cornersRadius - arrowHeight,
      cornersRadius + rect.left, rect.bottom - arrowHeight - strokeWidth), 90, 90);
    path.lineTo(rect.left + strokeWidth, rect.top + cornersRadius + strokeWidth);
    path.arcTo(new RectF(rect.left + strokeWidth, rect.top + strokeWidth, cornersRadius
      + rect.left, cornersRadius + rect.top), 180, 90);
    path.close();
  }
}