Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.model; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 4 | import java.util.List; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 5 | |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 6 | import android.graphics.PointF; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 7 | import android.util.Log; |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 8 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 9 | public class BubbleModel { |
| 10 | private static final String TAG = BubbleModel.class.getSimpleName(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 11 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 12 | private long lastUpdate = 0; |
| 13 | public int width, height; |
| 14 | private ArrayList<Bubble> bubbles = new ArrayList<Bubble>(); |
| 15 | private ArrayList<Attractor> attractors = new ArrayList<Attractor>(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 16 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 17 | private static final double BUBBLE_RETURN_TIME_HALF_LIFE = .3; |
| 18 | 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] | 19 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 20 | 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] | 21 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 22 | private static final float BUBBLE_MAX_SPEED = 2500.f; // px.s-1 : Max target speed in px/sec |
| 23 | private static final float ATTRACTOR_SMOOTH_DIST = 50.f; // px : Size of the "gravity hole" around the attractor |
| 24 | private static final float ATTRACTOR_STALL_DIST = 15.f; // px : Size of the "gravity hole" flat bottom |
| 25 | private static final float ATTRACTOR_DIST_SUCK = 20.f; // px |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 26 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 27 | private static final float BORDER_REPULSION = 60000; // px.s^-2 |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 28 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 29 | private final float border_repulsion; |
| 30 | private final float bubble_max_speed; |
| 31 | private final float attractor_smooth_dist; |
| 32 | private final float attractor_stall_dist; |
| 33 | private final float attractor_dist_suck; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 34 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 35 | private float density = 1.f; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 36 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 37 | public BubbleModel(float screen_density) { |
| 38 | Log.d(TAG, "Creating BubbleModel"); |
| 39 | this.density = screen_density; |
| 40 | attractor_dist_suck = ATTRACTOR_DIST_SUCK * density; |
| 41 | bubble_max_speed = BUBBLE_MAX_SPEED * density; |
| 42 | attractor_smooth_dist = ATTRACTOR_SMOOTH_DIST * density; |
| 43 | attractor_stall_dist = ATTRACTOR_STALL_DIST * density; |
| 44 | border_repulsion = BORDER_REPULSION * density; |
| 45 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 46 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 47 | public void addBubble(Bubble b) { |
| 48 | b.setDensity(density); |
| 49 | bubbles.add(b); |
| 50 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 51 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 52 | public List<Bubble> getBubbles() { |
| 53 | return bubbles; |
| 54 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 55 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 56 | public void addAttractor(Attractor a) { |
| 57 | a.setDensity(density); |
| 58 | attractors.add(a); |
| 59 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 60 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 61 | public List<Attractor> getAttractors() { |
| 62 | return attractors; |
| 63 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 64 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 65 | public void clearAttractors() { |
| 66 | attractors.clear(); |
| 67 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 68 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 69 | public void clear() { |
| 70 | clearAttractors(); |
| 71 | bubbles.clear(); |
| 72 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 73 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 74 | public void update() { |
| 75 | long now = System.nanoTime(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 76 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 77 | // Do nothing if lastUpdate is in the future. |
| 78 | if (lastUpdate > now) |
| 79 | return; |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 80 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 81 | double ddt = Math.min((now - lastUpdate) / 1000000000.0, .2); |
| 82 | lastUpdate = now; |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 83 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 84 | float dt = (float) ddt; |
| 85 | // Log.w(TAG, "update dt="+dt); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 86 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 87 | int attr_n = attractors.size(); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 88 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 89 | // Iterators should not be used in frequently called methods |
| 90 | // to avoid garbage collection glitches caused by iterator objects. |
| 91 | for (int i = 0, n = bubbles.size(); i < n; i++) { |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 92 | |
| 93 | if (i > bubbles.size()) { // prevent updating a bubble already removed |
Alexandre Lision | 4e7b783 | 2013-10-29 15:55:06 -0400 | [diff] [blame] | 94 | return; |
| 95 | } |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 96 | Bubble b = bubbles.get(i); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 97 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 98 | if (b.markedToDie) { |
| 99 | continue; |
| 100 | } |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 101 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 102 | if (!b.dragged) { |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 103 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 104 | float bx = b.getPosX(), by = b.getPosY(); |
| 105 | |
| 106 | Attractor attractor = null; |
| 107 | PointF attractor_pos = b.attractor; |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 108 | 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] | 109 | |
| 110 | for (int j = 0; j < attr_n; j++) { |
| 111 | try { |
| 112 | Attractor t = attractors.get(j); |
| 113 | |
| 114 | float dx = t.pos.x - bx, dy = t.pos.y - by; |
| 115 | float adist = dx * dx + dy * dy; |
| 116 | if (adist < attractor_dist) { |
| 117 | attractor = t; |
| 118 | attractor_pos = t.pos; |
| 119 | attractor_dist = adist; |
| 120 | } |
| 121 | } catch (IndexOutOfBoundsException e) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 122 | // Try to update when layout was changing |
| 123 | } |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 124 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 125 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 126 | // float friction_coef = 1.f-FRICTION_VISCOUS*dt; |
| 127 | double friction_coef = 1 + Math.expm1(-FRICTION_VISCOUS * ddt); |
| 128 | b.speed.x *= friction_coef; |
| 129 | b.speed.y *= friction_coef; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 130 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 131 | // if(attractor != null) { |
| 132 | float target_speed; |
| 133 | float tdx = attractor_pos.x - bx, tdy = attractor_pos.y - by; |
| 134 | float dist = Math.max(1.f, (float) Math.sqrt(tdx * tdx + tdy * tdy)); |
| 135 | if (dist > attractor_smooth_dist) |
| 136 | target_speed = bubble_max_speed; |
| 137 | else if (dist < attractor_stall_dist) |
| 138 | target_speed = 0; |
| 139 | else { |
| 140 | float a = (dist - attractor_stall_dist) / (attractor_smooth_dist - attractor_stall_dist); |
| 141 | target_speed = bubble_max_speed * a; |
| 142 | } |
| 143 | if (attractor != null) { |
| 144 | if (dist > attractor_smooth_dist) |
| 145 | b.target_scale = 1.f; |
| 146 | else if (dist < attractor_stall_dist) |
| 147 | b.target_scale = .2f; |
| 148 | else { |
| 149 | float a = (dist - attractor_stall_dist) / (attractor_smooth_dist - attractor_stall_dist); |
| 150 | b.target_scale = a * .8f + .2f; |
| 151 | } |
| 152 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 153 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 154 | // border repulsion |
| 155 | if (bx < 0 && b.speed.x < 0) { |
| 156 | b.speed.x += dt * border_repulsion; |
| 157 | } else if (bx > width && b.speed.x > 0) { |
| 158 | b.speed.x -= dt * border_repulsion; |
| 159 | } |
| 160 | if (by < 0 && b.speed.y < 0) { |
| 161 | b.speed.y += dt * border_repulsion; |
| 162 | } else if (by > height && b.speed.y > 0) { |
| 163 | b.speed.y -= dt * border_repulsion; |
| 164 | } |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 165 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 166 | b.speed.x += dt * target_speed * tdx / dist; |
| 167 | b.speed.y += dt * target_speed * tdy / dist; |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 168 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 169 | double edt = -Math.expm1(-BUBBLE_RETURN_TIME_LAMBDA * ddt); |
| 170 | double dx = (attractor_pos.x - bx) * edt + Math.min(bubble_max_speed, b.speed.x) * dt; |
| 171 | double dy = (attractor_pos.y - by) * edt + Math.min(bubble_max_speed, b.speed.y) * dt; |
| 172 | // Log.w(TAG, "update dx="+dt+" dy="+dy); |
| 173 | b.setPos((float) (bx + dx), (float) (by + dy)); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 174 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 175 | if (attractor != null && attractor_dist < attractor_dist_suck * attractor_dist_suck) { |
| 176 | b.dragged = false; |
| 177 | if (attractor.callback.onBubbleSucked(b)) { |
| 178 | bubbles.remove(b); |
| 179 | n--; |
| 180 | } else { |
| 181 | b.target_scale = 1.f; |
| 182 | } |
| 183 | } |
| 184 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 185 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 186 | 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] | 187 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 188 | } |
| 189 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 190 | |
Alexandre Lision | ee2494d | 2013-10-09 17:14:00 -0400 | [diff] [blame] | 191 | public Bubble getBubble(String call) { |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 192 | for (Bubble b : bubbles) { |
| 193 | if (!b.isUser && b.callIDEquals(call)) |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 194 | return b; |
| 195 | } |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | public void removeBubble(SipCall sipCall) { |
Alexandre Lision | ee2494d | 2013-10-09 17:14:00 -0400 | [diff] [blame] | 200 | bubbles.remove(getBubble(sipCall.getCallId())); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 201 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 204 | public Bubble getUser() { |
| 205 | for (Bubble b : bubbles) { |
| 206 | if (b.isUser) |
| 207 | return b; |
| 208 | } |
| 209 | return null; |
| 210 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 211 | |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 212 | } |