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