Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Adrien Beraud <adrien.beraud@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.model; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 35 | import java.util.List; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 36 | |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 37 | import android.graphics.PointF; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 38 | import android.util.Log; |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 39 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 40 | public class BubbleModel { |
| 41 | private static final String TAG = BubbleModel.class.getSimpleName(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 42 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 43 | private long lastUpdate = 0; |
| 44 | public int width, height; |
| 45 | private ArrayList<Bubble> bubbles = new ArrayList<Bubble>(); |
| 46 | private ArrayList<Attractor> attractors = new ArrayList<Attractor>(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 47 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 48 | private static final double BUBBLE_RETURN_TIME_HALF_LIFE = .3; |
| 49 | private static final double BUBBLE_RETURN_TIME_LAMBDA = Math.log(2) / BUBBLE_RETURN_TIME_HALF_LIFE; |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 50 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 51 | private static final double FRICTION_VISCOUS = Math.log(2) / .2f; // Viscous friction factor |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 52 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 53 | private static final float BUBBLE_MAX_SPEED = 2500.f; // px.s-1 : Max target speed in px/sec |
| 54 | private static final float ATTRACTOR_SMOOTH_DIST = 50.f; // px : Size of the "gravity hole" around the attractor |
| 55 | private static final float ATTRACTOR_STALL_DIST = 15.f; // px : Size of the "gravity hole" flat bottom |
| 56 | private static final float ATTRACTOR_DIST_SUCK = 20.f; // px |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 57 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 58 | private static final float BORDER_REPULSION = 60000; // px.s^-2 |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 59 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 60 | private final float border_repulsion; |
| 61 | private final float bubble_max_speed; |
| 62 | private final float attractor_smooth_dist; |
| 63 | private final float attractor_stall_dist; |
| 64 | private final float attractor_dist_suck; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 65 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 66 | private float density = 1.f; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 67 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 68 | public BubbleModel(float screen_density) { |
| 69 | Log.d(TAG, "Creating BubbleModel"); |
| 70 | this.density = screen_density; |
| 71 | attractor_dist_suck = ATTRACTOR_DIST_SUCK * density; |
| 72 | bubble_max_speed = BUBBLE_MAX_SPEED * density; |
| 73 | attractor_smooth_dist = ATTRACTOR_SMOOTH_DIST * density; |
| 74 | attractor_stall_dist = ATTRACTOR_STALL_DIST * density; |
| 75 | border_repulsion = BORDER_REPULSION * density; |
| 76 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 77 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 78 | public void addBubble(Bubble b) { |
| 79 | b.setDensity(density); |
| 80 | bubbles.add(b); |
| 81 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 82 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 83 | public List<Bubble> getBubbles() { |
| 84 | return bubbles; |
| 85 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 86 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 87 | public void addAttractor(Attractor a) { |
| 88 | a.setDensity(density); |
| 89 | attractors.add(a); |
| 90 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 91 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 92 | public List<Attractor> getAttractors() { |
| 93 | return attractors; |
| 94 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 95 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 96 | public void clearAttractors() { |
| 97 | attractors.clear(); |
| 98 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 99 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 100 | public void clear() { |
| 101 | clearAttractors(); |
| 102 | bubbles.clear(); |
| 103 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 104 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 105 | public void update() { |
| 106 | long now = System.nanoTime(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 107 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 108 | // Do nothing if lastUpdate is in the future. |
| 109 | if (lastUpdate > now) |
| 110 | return; |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 111 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 112 | double ddt = Math.min((now - lastUpdate) / 1000000000.0, .2); |
| 113 | lastUpdate = now; |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 114 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 115 | float dt = (float) ddt; |
| 116 | // Log.w(TAG, "update dt="+dt); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 117 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 118 | int attr_n = attractors.size(); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 119 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 120 | // Iterators should not be used in frequently called methods |
| 121 | // to avoid garbage collection glitches caused by iterator objects. |
| 122 | for (int i = 0, n = bubbles.size(); i < n; i++) { |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 123 | |
Alexandre Lision | c985b36 | 2013-12-10 17:45:03 -0500 | [diff] [blame] | 124 | if (i >= bubbles.size()) { // prevent updating a bubble already removed |
Alexandre Lision | 4e7b783 | 2013-10-29 15:55:06 -0400 | [diff] [blame] | 125 | return; |
| 126 | } |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 127 | Bubble b = bubbles.get(i); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 128 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 129 | if (b.markedToDie) { |
| 130 | continue; |
| 131 | } |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 132 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 133 | if (!b.dragged) { |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 134 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 135 | float bx = b.getPosX(), by = b.getPosY(); |
| 136 | |
| 137 | Attractor attractor = null; |
| 138 | PointF attractor_pos = b.attractor; |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 139 | float attractor_dist = (attractor_pos.x - bx) * (attractor_pos.x - bx) + (attractor_pos.y - by) * (attractor_pos.y - by); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 140 | |
| 141 | for (int j = 0; j < attr_n; j++) { |
| 142 | try { |
| 143 | Attractor t = attractors.get(j); |
| 144 | |
| 145 | float dx = t.pos.x - bx, dy = t.pos.y - by; |
| 146 | float adist = dx * dx + dy * dy; |
| 147 | if (adist < attractor_dist) { |
| 148 | attractor = t; |
| 149 | attractor_pos = t.pos; |
| 150 | attractor_dist = adist; |
| 151 | } |
| 152 | } catch (IndexOutOfBoundsException e) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 153 | // Try to update when layout was changing |
| 154 | } |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 155 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 156 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 157 | // float friction_coef = 1.f-FRICTION_VISCOUS*dt; |
| 158 | double friction_coef = 1 + Math.expm1(-FRICTION_VISCOUS * ddt); |
| 159 | b.speed.x *= friction_coef; |
| 160 | b.speed.y *= friction_coef; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 161 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 162 | // if(attractor != null) { |
| 163 | float target_speed; |
| 164 | float tdx = attractor_pos.x - bx, tdy = attractor_pos.y - by; |
| 165 | float dist = Math.max(1.f, (float) Math.sqrt(tdx * tdx + tdy * tdy)); |
| 166 | if (dist > attractor_smooth_dist) |
| 167 | target_speed = bubble_max_speed; |
| 168 | else if (dist < attractor_stall_dist) |
| 169 | target_speed = 0; |
| 170 | else { |
| 171 | float a = (dist - attractor_stall_dist) / (attractor_smooth_dist - attractor_stall_dist); |
| 172 | target_speed = bubble_max_speed * a; |
| 173 | } |
| 174 | if (attractor != null) { |
| 175 | if (dist > attractor_smooth_dist) |
| 176 | b.target_scale = 1.f; |
| 177 | else if (dist < attractor_stall_dist) |
| 178 | b.target_scale = .2f; |
| 179 | else { |
| 180 | float a = (dist - attractor_stall_dist) / (attractor_smooth_dist - attractor_stall_dist); |
| 181 | b.target_scale = a * .8f + .2f; |
| 182 | } |
| 183 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 184 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 185 | // border repulsion |
| 186 | if (bx < 0 && b.speed.x < 0) { |
| 187 | b.speed.x += dt * border_repulsion; |
| 188 | } else if (bx > width && b.speed.x > 0) { |
| 189 | b.speed.x -= dt * border_repulsion; |
| 190 | } |
| 191 | if (by < 0 && b.speed.y < 0) { |
| 192 | b.speed.y += dt * border_repulsion; |
| 193 | } else if (by > height && b.speed.y > 0) { |
| 194 | b.speed.y -= dt * border_repulsion; |
| 195 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 196 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 197 | b.speed.x += dt * target_speed * tdx / dist; |
| 198 | b.speed.y += dt * target_speed * tdy / dist; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 199 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 200 | double edt = -Math.expm1(-BUBBLE_RETURN_TIME_LAMBDA * ddt); |
| 201 | double dx = (attractor_pos.x - bx) * edt + Math.min(bubble_max_speed, b.speed.x) * dt; |
| 202 | double dy = (attractor_pos.y - by) * edt + Math.min(bubble_max_speed, b.speed.y) * dt; |
| 203 | // Log.w(TAG, "update dx="+dt+" dy="+dy); |
| 204 | b.setPos((float) (bx + dx), (float) (by + dy)); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 205 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 206 | if (attractor != null && attractor_dist < attractor_dist_suck * attractor_dist_suck) { |
| 207 | b.dragged = false; |
| 208 | if (attractor.callback.onBubbleSucked(b)) { |
| 209 | bubbles.remove(b); |
| 210 | n--; |
| 211 | } else { |
| 212 | b.target_scale = 1.f; |
| 213 | } |
| 214 | } |
| 215 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 216 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 217 | b.setScale(b.getScale() + (b.target_scale - b.getScale()) * dt * 10.f); |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 218 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 219 | } |
| 220 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 221 | |
Alexandre Lision | ee2494d | 2013-10-09 17:14:00 -0400 | [diff] [blame] | 222 | public Bubble getBubble(String call) { |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 223 | for (Bubble b : bubbles) { |
| 224 | if (!b.isUser && b.callIDEquals(call)) |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 225 | return b; |
| 226 | } |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | public void removeBubble(SipCall sipCall) { |
Alexandre Lision | ee2494d | 2013-10-09 17:14:00 -0400 | [diff] [blame] | 231 | bubbles.remove(getBubble(sipCall.getCallId())); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 232 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 235 | public Bubble getUser() { |
| 236 | for (Bubble b : bubbles) { |
| 237 | if (b.isUser) |
| 238 | return b; |
| 239 | } |
| 240 | return null; |
| 241 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 242 | |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 243 | } |