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