source: other-projects/tipple-android/tipple-ar/tipple-standalone-hpg/src/org/greenstone/android/tipple/base/AROverlay.java@ 26528

Last change on this file since 26528 was 26528, checked in by davidb, 11 years ago

Code developed by the Tipple-AR Smoke and Mirrors team, based on the main tipple trunk

File size: 5.3 KB
Line 
1package org.greenstone.android.tipple.base;
2
3import java.text.DecimalFormat;
4import java.util.Collections;
5import java.util.Comparator;
6
7import org.mapsforge.core.GeoPoint;
8
9import android.app.AlertDialog;
10import android.content.DialogInterface;
11import android.content.DialogInterface.OnCancelListener;
12import android.graphics.Canvas;
13import android.graphics.Color;
14import android.graphics.Paint;
15import android.location.Location;
16import android.speech.tts.TextToSpeech;
17import android.view.MotionEvent;
18import android.view.View;
19
20public class AROverlay extends View {
21 ARActivity activity;
22 Paint paintFill;
23 Paint paintOutline;
24 Paint paintText;
25 Paint borderText;
26
27 DecimalFormat df;
28
29 boolean isDialogBoxShowing = false;
30
31 public AROverlay(ARActivity context) {
32 super(context);
33
34 df = new DecimalFormat("#.#");
35
36 activity = context;
37 paintFill = new Paint();
38 paintFill.setStyle(Paint.Style.FILL);
39 paintFill.setAntiAlias(true);
40 paintOutline = new Paint();
41 paintOutline.setStyle(Paint.Style.STROKE);
42 paintOutline.setStrokeWidth(5);
43 paintOutline.setAntiAlias(true);
44 paintText = new Paint();
45 paintText.setTextAlign(Paint.Align.CENTER);
46 paintText.setTextSize(35);
47 paintText.setColor(Color.BLACK);
48 paintText.setAntiAlias(true);
49 paintText.setShadowLayer(3, 1, 1, Color.WHITE);
50 }
51
52 @Override
53 public boolean onTouchEvent(MotionEvent event) {
54
55 if (isDialogBoxShowing) return false;
56
57 float x_finger = event.getX();
58 float y_finger = event.getY();
59
60 for (int i = Global.locations.size() - 1; i >= 0; i--)
61 {
62 TipLocation loc = Global.locations.get(i);
63
64 if (loc.hits(x_finger, y_finger))
65 {
66 isDialogBoxShowing = true;
67
68 AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
69 dialog.setTitle(loc.getTitle());
70 dialog.setMessage(loc.getText());
71
72 dialog.setOnCancelListener(new OnCancelListener() {
73 @Override
74 public void onCancel(DialogInterface dialog) {
75 Global.tts.stop();
76 isDialogBoxShowing = false;
77 }
78 });
79
80 dialog.show();
81
82 Global.tts.speak(loc.getTitleTTS(), TextToSpeech.QUEUE_FLUSH, null);
83 Global.tts.speak(loc.getTTSText(), TextToSpeech.QUEUE_ADD, null);
84
85 ArDirection.setGPSpidouble(new GeoPoint(loc.getLatitude(), loc.getLongitude()));
86 return true;
87 }
88 }
89
90 return false;
91 }
92
93 @Override
94 protected void onDraw(Canvas canvas) {
95
96 // recalculate the screen locations
97 for (int i = 0; i < Global.locations.size(); i++) {
98 Global.locations.get(i).projectToScreen(LocationFusion.getInstance().lastKnownLocation,
99 SensorFusion.getInstance().getRotationMatrix());
100 }
101
102 // sort the points, closest first
103 Collections.sort(Global.locations, new Comparator<TipLocation>() {
104 public int compare(TipLocation left, TipLocation right) {
105 return Float.compare(left.getDistance(), right.getDistance());
106 }
107 });
108
109 float screenAngle = SensorFusion.getInstance().getScreenAngleDegrees();
110 float[] markerHitCircle = new float[3];
111
112 // draw the locations (closest last)
113 for (int i = Global.locations.size() - 1; i >= 0; i--) {
114
115 TipLocation loc = Global.locations.get(i);
116
117 if (loc.isVisible()) {
118
119 canvas.save(); // save the default canvas matrix
120
121 // shift the canvas so that the location can be drawn at (0, 0)
122 canvas.translate(loc.getScreenX(), loc.getScreenY());
123 // rotate the canvas
124 canvas.rotate(screenAngle);
125
126 canvas.save(); // save the rotated & translated canvas
127
128 // scale the canvas for the text
129 float textScale = Math.min(200 * loc.getScreenScale(), 1);
130 canvas.scale(textScale, textScale);
131
132 // draw the text
133 //String text = String.format("%s (%1.1fm)", loc.getTitle(), loc.getDistance());
134 canvas.drawText(loc.getTitle() + " (" + df.format(loc.getDistance()) + "m)", 0, paintText.getTextSize(), paintText);
135
136 canvas.restore(); // undo the scaling (go back to the rotated & scaled version)
137
138 // scale the canvas for the bitmap
139 float markerScale = Math.min(500 * loc.getScreenScale(), 4);
140 canvas.scale(markerScale, markerScale);
141
142 // now draw the bitmap onto the centre of the canvas and let the canvas' matrix
143 // transform that to the right location, rotation and size
144 float x = -loc.getMarkerBitmap().getWidth() / 2;
145 float y = -loc.getMarkerBitmap().getHeight();
146 canvas.drawBitmap(loc.getMarkerBitmap(), x, y, paintFill);
147
148 // save the marker hit circle
149 markerHitCircle[0] = 0;
150 markerHitCircle[1] = -loc.getMarkerBitmap().getHeight() / 2;
151 canvas.getMatrix().mapPoints(markerHitCircle);
152 loc.setHitCircle(markerHitCircle[0], markerHitCircle[1], markerScale * 12 + 5);
153
154 canvas.restore(); // undo the rotation & translation
155 }
156 }
157 //drawInfo(canvas);
158
159 super.onDraw(canvas);
160 }
161
162 private void drawInfo(Canvas canvas) {
163 Paint back = new Paint();
164 back.setColor(Color.WHITE);
165 back.setAlpha(150);
166 Paint fore = new Paint();
167 fore.setColor(Color.BLACK);
168 fore.setTextSize(25);
169 fore.setAntiAlias(true);
170
171 Location loc = LocationFusion.getInstance().lastKnownLocation;
172 String[] text = new String[] {
173 String.format("GPS: lat=%.1f, lon=%.1f, alt=%.1f, accuracy=%.2f",
174 loc.getLatitude(), loc.getLongitude(), loc.getAltitude(), loc.getAccuracy()),
175 String.format("Number of points: %d", Global.locations.size()) };
176
177 canvas.drawRect(0, 0, 800, 100, back);
178
179 for (int row = 0; row < text.length; row++) {
180 canvas.drawText(text[row], 10, 30 + 30 * row, fore);
181 }
182 }
183}
Note: See TracBrowser for help on using the repository browser.