Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 2 |
|
| 3 | import android.content.Context;
|
| 4 | import android.graphics.Canvas;
|
| 5 | import android.graphics.Color;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 6 | import android.graphics.Paint;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 7 | import android.graphics.Paint.Align;
|
| 8 | import android.graphics.RectF;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 9 | import android.os.Handler;
|
| 10 | import android.os.Message;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 11 | import android.util.AttributeSet;
|
| 12 | import android.util.Log;
|
| 13 | import android.view.MotionEvent;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 14 | import android.view.SurfaceHolder;
|
| 15 | import android.view.SurfaceView;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 16 | import android.view.View;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 17 | import android.view.View.OnTouchListener;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 18 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 19 | public class BubblesView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener
|
| 20 | {
|
| 21 | private static final String TAG = BubblesView.class.getSimpleName();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 22 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 23 | private BubblesThread thread = null;
|
| 24 | private BubbleModel model;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 25 |
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 26 | private Paint attractor_paint = new Paint();
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 27 | private Paint name_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
| 28 |
|
| 29 | private float density;
|
| 30 | private float textDensity;
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 31 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 32 | public BubblesView(Context context, AttributeSet attrs)
|
| 33 | {
|
| 34 | super(context, attrs);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 35 |
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 36 | density = getResources().getDisplayMetrics().density;
|
| 37 | textDensity = getResources().getDisplayMetrics().scaledDensity;
|
| 38 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 39 | SurfaceHolder holder = getHolder();
|
| 40 | holder.addCallback(this);
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 41 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 42 | // create thread only; it's started in surfaceCreated()
|
| 43 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 44 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 45 | setOnTouchListener(this);
|
| 46 | setFocusable(true);
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 47 |
|
| 48 | attractor_paint.setColor(Color.RED);
|
| 49 | //attractor_paint.set
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 50 | name_paint.setTextSize(20*textDensity);
|
| 51 | name_paint.setColor(0xFF303030);
|
| 52 | name_paint.setTextAlign(Align.CENTER);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 53 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 54 |
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 55 | private void createThread()
|
| 56 | {
|
| 57 | if (thread != null)
|
| 58 | return;
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 59 | thread = new BubblesThread(getHolder(), getContext(), new Handler() {
|
| 60 | @Override
|
| 61 | public void handleMessage(Message m)
|
| 62 | {
|
| 63 | /* mStatusText.setVisibility(m.getData().getInt("viz"));
|
| 64 | mStatusText.setText(m.getData().getString("text"));*/
|
| 65 | }
|
| 66 | });
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 67 | if (model != null)
|
Adrien Béraud | 65ba5b1 | 2013-05-06 15:42:50 +1000 | [diff] [blame] | 68 | thread.setModel(model);
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 69 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 70 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 71 | public void setModel(BubbleModel model)
|
| 72 | {
|
| 73 | this.model = model;
|
| 74 | thread.setModel(model);
|
| 75 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 76 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 77 | /*@Override
|
| 78 | public void onWindowFocusChanged(boolean hasWindowFocus) {
|
| 79 | if (!hasWindowFocus) {
|
| 80 | thread.pause();
|
| 81 | }
|
| 82 | }*/
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 83 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 84 | @Override
|
| 85 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
|
| 86 | {
|
| 87 | Log.w(TAG, "surfaceChanged");
|
| 88 | thread.setSurfaceSize(width, height);
|
| 89 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 90 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 91 | /*
|
| 92 | * Callback invoked when the Surface has been created and is ready to be
|
| 93 | * used.
|
| 94 | */
|
| 95 | @Override
|
| 96 | public void surfaceCreated(SurfaceHolder holder)
|
| 97 | {
|
| 98 | // start the thread here so that we don't busy-wait in run()
|
| 99 | // waiting for the surface to be created
|
| 100 | createThread();
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 101 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 102 | Log.w(TAG, "surfaceCreated");
|
| 103 | thread.setRunning(true);
|
| 104 | thread.start();
|
| 105 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 106 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 107 | /*
|
| 108 | * Callback invoked when the Surface has been destroyed and must no longer
|
| 109 | * be touched. WARNING: after this method returns, the Surface/Canvas must
|
| 110 | * never be touched again!
|
| 111 | */
|
| 112 | @Override
|
| 113 | public void surfaceDestroyed(SurfaceHolder holder)
|
| 114 | {
|
| 115 | // we have to tell thread to shut down & wait for it to finish, or else
|
| 116 | // it might touch the Surface after we return and explode
|
| 117 | Log.w(TAG, "surfaceDestroyed");
|
| 118 | boolean retry = true;
|
| 119 | thread.setRunning(false);
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 120 | while (retry) {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 121 | try {
|
| 122 | thread.join();
|
| 123 | retry = false;
|
| 124 | } catch (InterruptedException e) {
|
| 125 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 126 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 127 | thread = null;
|
| 128 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 129 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 130 | @Override
|
| 131 | public boolean onTouch(View v, MotionEvent event)
|
| 132 | {
|
| 133 | Log.w(TAG, "onTouch " + event.getAction());
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 134 |
|
| 135 | int action = event.getActionMasked();
|
| 136 |
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 137 | if (action == MotionEvent.ACTION_DOWN) {
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 138 | for (Bubble b : model.listBubbles) {
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 139 | if (b.intersects(event.getX(), event.getY())) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 140 | b.dragged = true;
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 141 | b.last_drag = System.nanoTime();
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 142 | b.setPos(event.getX(), event.getY());
|
| 143 | b.target_scale = .8f;
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 144 | }
|
| 145 | }
|
| 146 | } else if (action == MotionEvent.ACTION_MOVE) {
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 147 | long now = System.nanoTime();
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 148 | for (Bubble b : model.listBubbles) {
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 149 | if (b.dragged) {
|
| 150 | float x = event.getX(), y = event.getY();
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 151 | float dt = (float) ((now-b.last_drag)/1000000000.);
|
| 152 | float dx = x - b.getPosX(), dy = y - b.getPosY();
|
| 153 | b.last_drag = now;
|
| 154 |
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 155 | b.setPos(event.getX(), event.getY());
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 156 | /*int hn = event.getHistorySize() - 2;
|
| 157 | Log.w(TAG, "event.getHistorySize() : " + event.getHistorySize());
|
| 158 | if(hn > 0) {
|
| 159 | float dx = x-event.getHistoricalX(hn);
|
| 160 | float dy = y-event.getHistoricalY(hn);
|
| 161 | float dt = event.getHistoricalEventTime(hn)/1000.f;*/
|
| 162 | b.speed.x = dx/dt;
|
| 163 | b.speed.y = dy/dt;
|
| 164 | //Log.w(TAG, "onTouch dx:" + b.speed.x + " dy:" + b.speed.y);
|
| 165 | //}
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 166 | return true;
|
| 167 | }
|
| 168 | }
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 169 | } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 170 | for (Bubble b : model.listBubbles) {
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 171 | if (b.dragged) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 172 | b.dragged = false;
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 173 | b.target_scale = 1.f;
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 174 | }
|
| 175 | }
|
| 176 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 177 | return true;
|
| 178 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 179 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 180 | class BubblesThread extends Thread
|
| 181 | {
|
| 182 | private boolean running = false;
|
| 183 | private SurfaceHolder surfaceHolder;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 184 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 185 | BubbleModel model = null;
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 186 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 187 | public BubblesThread(SurfaceHolder holder, Context context, Handler handler)
|
| 188 | {
|
| 189 | surfaceHolder = holder;
|
| 190 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 191 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 192 | public void setModel(BubbleModel model)
|
| 193 | {
|
| 194 | this.model = model;
|
| 195 | }
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 196 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 197 | @Override
|
| 198 | public void run()
|
| 199 | {
|
| 200 | while (running) {
|
| 201 | Canvas c = null;
|
| 202 | try {
|
| 203 | c = surfaceHolder.lockCanvas(null);
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 204 |
|
| 205 | // for the case the surface is destroyed while already in the loop
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 206 | if (c == null || model == null)
|
| 207 | continue;
|
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 208 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 209 | synchronized (surfaceHolder) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 210 | //Log.w(TAG, "Thread doDraw");
|
| 211 | model.update();
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 212 | doDraw(c);
|
| 213 | }
|
| 214 | } finally {
|
| 215 | // do this in a finally so that if an exception is thrown
|
| 216 | // during the above, we don't leave the Surface in an
|
| 217 | // inconsistent state
|
| 218 | if (c != null)
|
| 219 | surfaceHolder.unlockCanvasAndPost(c);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | public void setRunning(boolean b)
|
| 225 | {
|
| 226 | running = b;
|
| 227 | }
|
| 228 |
|
| 229 | public void setSurfaceSize(int width, int height)
|
| 230 | {
|
| 231 | synchronized (surfaceHolder) {
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 232 | if (model != null) {
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 233 | model.width = width;
|
| 234 | model.height = height;
|
| 235 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 236 |
|
| 237 | // don't forget to resize the background image
|
| 238 | // mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, width, height, true);
|
| 239 | }
|
| 240 | }
|
| 241 |
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 242 | private void doDraw(Canvas canvas)
|
| 243 | {
|
| 244 | canvas.drawColor(Color.WHITE);
|
| 245 |
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 246 | synchronized (model) {
|
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 247 | for (int i = 0; i < model.attractors.size(); i++) {
|
| 248 | Attractor a = model.attractors.get(i);
|
| 249 | canvas.drawCircle(a.pos.x, a.pos.y, 10, attractor_paint);
|
| 250 | }
|
| 251 |
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 252 | for (int i = 0; i < model.listBubbles.size(); i++) {
|
| 253 | Bubble b = model.listBubbles.get(i);
|
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame^] | 254 | RectF bounds = new RectF(b.getBounds());
|
| 255 | /*if(b.dragged) {
|
| 256 | float width = bounds.left - bounds.right;
|
| 257 | float red = width/4;
|
| 258 | bounds.left += red;
|
| 259 | bounds.right -= red;
|
| 260 | bounds.top += red;
|
| 261 | bounds.bottom -= red;
|
| 262 | }*/
|
| 263 | canvas.drawBitmap(b.getBitmap(), null, bounds, null);
|
| 264 | canvas.drawText(b.contact.getmDisplayName(), b.getPosX(), b.getPosY()-50*density, name_paint);
|
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 265 | }
|
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 266 | }
|
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 267 | }
|
| 268 | }
|
| 269 |
|
alision | fe9cf71 | 2013-05-03 17:26:08 -0400 | [diff] [blame] | 270 | }
|