@rafaelaugusto/jolt 0.1.0-rc.13 → 0.1.0-rc.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/README.md +2 -2
  2. package/dist/animate.d.ts +2 -0
  3. package/dist/animate.d.ts.map +1 -1
  4. package/dist/animate.js +4 -4
  5. package/dist/animate.js.map +1 -1
  6. package/dist/core/drop.d.ts +34 -1
  7. package/dist/core/drop.d.ts.map +1 -1
  8. package/dist/core/drop.js +93 -8
  9. package/dist/core/drop.js.map +1 -1
  10. package/dist/core/layout-exit.d.ts +16 -0
  11. package/dist/core/layout-exit.d.ts.map +1 -0
  12. package/dist/core/layout-exit.js +29 -0
  13. package/dist/core/layout-exit.js.map +1 -0
  14. package/dist/core/list-changes.d.ts +52 -0
  15. package/dist/core/list-changes.d.ts.map +1 -0
  16. package/dist/core/list-changes.js +153 -0
  17. package/dist/core/list-changes.js.map +1 -0
  18. package/dist/core/loading.d.ts +125 -0
  19. package/dist/core/loading.d.ts.map +1 -0
  20. package/dist/core/loading.js +119 -0
  21. package/dist/core/loading.js.map +1 -0
  22. package/dist/core/numeric-map.d.ts +3 -0
  23. package/dist/core/numeric-map.d.ts.map +1 -1
  24. package/dist/core/numeric-map.js +1 -1
  25. package/dist/core/numeric-map.js.map +1 -1
  26. package/dist/core/pointer.d.ts +1 -1
  27. package/dist/core/preset-recipe.d.ts +2 -2
  28. package/dist/core/preset-recipe.d.ts.map +1 -1
  29. package/dist/core/preset-recipe.js +3 -1
  30. package/dist/core/preset-recipe.js.map +1 -1
  31. package/dist/core/scalar-program.d.ts +22 -0
  32. package/dist/core/scalar-program.d.ts.map +1 -0
  33. package/dist/core/scalar-program.js +47 -0
  34. package/dist/core/scalar-program.js.map +1 -0
  35. package/dist/core/scalar.d.ts +25 -0
  36. package/dist/core/scalar.d.ts.map +1 -0
  37. package/dist/core/scalar.js +84 -0
  38. package/dist/core/scalar.js.map +1 -0
  39. package/dist/core/text-pieces.d.ts +18 -0
  40. package/dist/core/text-pieces.d.ts.map +1 -0
  41. package/dist/core/text-pieces.js +43 -0
  42. package/dist/core/text-pieces.js.map +1 -0
  43. package/dist/drag.d.ts +7 -1
  44. package/dist/drag.d.ts.map +1 -1
  45. package/dist/drag.js +72 -37
  46. package/dist/drag.js.map +1 -1
  47. package/dist/drop-zones.d.ts +1 -1
  48. package/dist/drop-zones.d.ts.map +1 -1
  49. package/dist/drop-zones.js +2 -0
  50. package/dist/drop-zones.js.map +1 -1
  51. package/dist/engine.d.ts.map +1 -1
  52. package/dist/engine.js +7 -2
  53. package/dist/engine.js.map +1 -1
  54. package/dist/index.d.ts +4 -2
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +1 -0
  57. package/dist/index.js.map +1 -1
  58. package/dist/layout-exit.d.ts +19 -0
  59. package/dist/layout-exit.d.ts.map +1 -0
  60. package/dist/layout-exit.js +105 -0
  61. package/dist/layout-exit.js.map +1 -0
  62. package/dist/layout.d.ts +5 -0
  63. package/dist/layout.d.ts.map +1 -1
  64. package/dist/layout.js +50 -2
  65. package/dist/layout.js.map +1 -1
  66. package/dist/list-changes.d.ts +14 -0
  67. package/dist/list-changes.d.ts.map +1 -0
  68. package/dist/list-changes.js +251 -0
  69. package/dist/list-changes.js.map +1 -0
  70. package/dist/list-focus.d.ts +10 -0
  71. package/dist/list-focus.d.ts.map +1 -0
  72. package/dist/list-focus.js +103 -0
  73. package/dist/list-focus.js.map +1 -0
  74. package/dist/live-spring-driver.d.ts +2 -0
  75. package/dist/live-spring-driver.d.ts.map +1 -1
  76. package/dist/live-spring-driver.js +12 -0
  77. package/dist/live-spring-driver.js.map +1 -1
  78. package/dist/loader.d.ts +2 -1
  79. package/dist/loader.d.ts.map +1 -1
  80. package/dist/loader.js +17 -15
  81. package/dist/loader.js.map +1 -1
  82. package/dist/motion-value.d.ts +52 -19
  83. package/dist/motion-value.d.ts.map +1 -1
  84. package/dist/motion-value.js +615 -40
  85. package/dist/motion-value.js.map +1 -1
  86. package/dist/reveal.d.ts.map +1 -1
  87. package/dist/reveal.js +4 -3
  88. package/dist/reveal.js.map +1 -1
  89. package/dist/scroll-linked.d.ts +2 -1
  90. package/dist/scroll-linked.d.ts.map +1 -1
  91. package/dist/scroll-linked.js +101 -4
  92. package/dist/scroll-linked.js.map +1 -1
  93. package/dist/scroll-source.d.ts +18 -0
  94. package/dist/scroll-source.d.ts.map +1 -0
  95. package/dist/scroll-source.js +28 -0
  96. package/dist/scroll-source.js.map +1 -0
  97. package/dist/shared-layout/native.d.ts +1 -0
  98. package/dist/shared-layout/native.d.ts.map +1 -1
  99. package/dist/shared-layout/native.js +2 -0
  100. package/dist/shared-layout/native.js.map +1 -1
  101. package/dist/shimmer.d.ts.map +1 -1
  102. package/dist/shimmer.js +6 -4
  103. package/dist/shimmer.js.map +1 -1
  104. package/dist/value-track.d.ts +4 -0
  105. package/dist/value-track.d.ts.map +1 -1
  106. package/dist/value-track.js +3 -0
  107. package/dist/value-track.js.map +1 -1
  108. package/dist/vue/index.d.ts +45 -1
  109. package/dist/vue/index.d.ts.map +1 -1
  110. package/dist/vue/index.js +41 -2
  111. package/dist/vue/index.js.map +1 -1
  112. package/dist/web/drag.d.ts +8 -0
  113. package/dist/web/drag.d.ts.map +1 -1
  114. package/dist/web/drag.js +68 -14
  115. package/dist/web/drag.js.map +1 -1
  116. package/dist/web/drop-zones.d.ts +2 -2
  117. package/dist/web/drop-zones.d.ts.map +1 -1
  118. package/dist/web/drop-zones.js +21 -4
  119. package/dist/web/drop-zones.js.map +1 -1
  120. package/dist/web/layout-exit.d.ts +9 -0
  121. package/dist/web/layout-exit.d.ts.map +1 -0
  122. package/dist/web/layout-exit.js +148 -0
  123. package/dist/web/layout-exit.js.map +1 -0
  124. package/dist/web/layout.d.ts +25 -8
  125. package/dist/web/layout.d.ts.map +1 -1
  126. package/dist/web/layout.js +83 -2
  127. package/dist/web/layout.js.map +1 -1
  128. package/dist/web/list-changes.d.ts +41 -0
  129. package/dist/web/list-changes.d.ts.map +1 -0
  130. package/dist/web/list-changes.js +126 -0
  131. package/dist/web/list-changes.js.map +1 -0
  132. package/dist/web/loading.d.ts +2 -1
  133. package/dist/web/loading.d.ts.map +1 -1
  134. package/dist/web/loading.js +19 -15
  135. package/dist/web/loading.js.map +1 -1
  136. package/dist/web/pointer.d.ts +2 -0
  137. package/dist/web/pointer.d.ts.map +1 -1
  138. package/dist/web/pointer.js +13 -0
  139. package/dist/web/pointer.js.map +1 -1
  140. package/dist/web/presentation-copy.d.ts +9 -0
  141. package/dist/web/presentation-copy.d.ts.map +1 -0
  142. package/dist/web/presentation-copy.js +104 -0
  143. package/dist/web/presentation-copy.js.map +1 -0
  144. package/dist/web/public.d.ts +6 -1
  145. package/dist/web/public.d.ts.map +1 -1
  146. package/dist/web/public.js +1 -0
  147. package/dist/web/public.js.map +1 -1
  148. package/dist/web/reveal.d.ts.map +1 -1
  149. package/dist/web/reveal.js +4 -3
  150. package/dist/web/reveal.js.map +1 -1
  151. package/dist/web/runtime.d.ts +7 -7
  152. package/dist/web/runtime.d.ts.map +1 -1
  153. package/dist/web/runtime.js +85 -8
  154. package/dist/web/runtime.js.map +1 -1
  155. package/dist/web/scroll.d.ts +12 -1
  156. package/dist/web/scroll.d.ts.map +1 -1
  157. package/dist/web/scroll.js +25 -0
  158. package/dist/web/scroll.js.map +1 -1
  159. package/dist/web/shared-layout.d.ts.map +1 -1
  160. package/dist/web/shared-layout.js +2 -101
  161. package/dist/web/shared-layout.js.map +1 -1
  162. package/dist/web/spring-value.d.ts +3 -2
  163. package/dist/web/spring-value.d.ts.map +1 -1
  164. package/dist/web/spring-value.js +5 -5
  165. package/dist/web/spring-value.js.map +1 -1
  166. package/dist/web/text-effect.d.ts.map +1 -1
  167. package/dist/web/text-effect.js +10 -16
  168. package/dist/web/text-effect.js.map +1 -1
  169. package/dist/web/value.d.ts +43 -4
  170. package/dist/web/value.d.ts.map +1 -1
  171. package/dist/web/value.js +139 -6
  172. package/dist/web/value.js.map +1 -1
  173. package/dist/web/vue/index.d.ts +49 -2
  174. package/dist/web/vue/index.d.ts.map +1 -1
  175. package/dist/web/vue/index.js +45 -2
  176. package/dist/web/vue/index.js.map +1 -1
  177. package/package.json +13 -3
  178. package/platforms/android/java/org/rafaelaugusto/jolt/LiveSpring.java +52 -6
  179. package/platforms/android/java/org/rafaelaugusto/jolt/PointerInput.java +26 -4
  180. package/platforms/android/java/org/rafaelaugusto/jolt/ScalarProgram.java +76 -0
  181. package/platforms/android/java/org/rafaelaugusto/jolt/ScrollSource.java +86 -0
  182. package/platforms/android/java/org/rafaelaugusto/jolt/SharedLayoutFlight.java +8 -2
  183. package/platforms/ios/src/JoltLiveSpring.h +2 -0
  184. package/platforms/ios/src/JoltLiveSpring.m +52 -6
  185. package/platforms/ios/src/JoltScalarProgram.h +9 -0
  186. package/platforms/ios/src/JoltScalarProgram.m +80 -0
  187. package/platforms/ios/src/JoltScrollSource.h +11 -0
  188. package/platforms/ios/src/JoltScrollSource.m +57 -0
  189. package/platforms/ios/src/JoltSharedLayoutFlight.h +1 -0
  190. package/platforms/ios/src/JoltSharedLayoutFlight.m +7 -1
  191. package/platforms/ios/src/module.modulemap +1 -0
  192. package/src/android-plugin.d.ts +25 -0
  193. package/src/animate.ts +4 -4
  194. package/src/core/drop.ts +123 -5
  195. package/src/core/layout-exit.ts +43 -0
  196. package/src/core/list-changes.ts +206 -0
  197. package/src/core/loading.ts +172 -0
  198. package/src/core/numeric-map.ts +1 -1
  199. package/src/core/preset-recipe.ts +6 -2
  200. package/src/core/scalar-program.ts +69 -0
  201. package/src/core/scalar.ts +125 -0
  202. package/src/core/text-pieces.ts +52 -0
  203. package/src/drag.ts +81 -38
  204. package/src/drop-zones.ts +13 -3
  205. package/src/engine.ts +8 -3
  206. package/src/index.ts +8 -0
  207. package/src/ios-plugin.d.ts +17 -0
  208. package/src/layout-exit.ts +120 -0
  209. package/src/layout.ts +57 -1
  210. package/src/list-changes.ts +317 -0
  211. package/src/list-focus.ts +101 -0
  212. package/src/live-spring-driver.ts +18 -0
  213. package/src/loader.ts +23 -20
  214. package/src/motion-value.ts +689 -50
  215. package/src/reveal.ts +4 -3
  216. package/src/scroll-linked.ts +121 -5
  217. package/src/scroll-source.ts +60 -0
  218. package/src/shared-layout/native.ts +3 -0
  219. package/src/shimmer.ts +6 -4
  220. package/src/value-track.ts +7 -0
  221. package/src/vue/index.ts +51 -0
  222. package/src/web/drag.ts +70 -12
  223. package/src/web/drop-zones.ts +31 -6
  224. package/src/web/layout-exit.ts +179 -0
  225. package/src/web/layout.ts +103 -2
  226. package/src/web/list-changes.ts +194 -0
  227. package/src/web/loading.ts +38 -17
  228. package/src/web/pointer.ts +14 -0
  229. package/src/web/presentation-copy.ts +125 -0
  230. package/src/web/public.ts +17 -0
  231. package/src/web/reveal.ts +7 -8
  232. package/src/web/runtime.ts +116 -9
  233. package/src/web/scroll.ts +41 -0
  234. package/src/web/shared-layout.ts +3 -115
  235. package/src/web/spring-value.ts +7 -6
  236. package/src/web/text-effect.ts +20 -17
  237. package/src/web/value.ts +167 -12
  238. package/src/web/vue/index.ts +51 -0
@@ -49,12 +49,16 @@ public final class LiveSpring {
49
49
  private final double stiffness, damping, mass, restDelta, restSpeed;
50
50
  private final ArrayList<Binding> bindings = new ArrayList<>();
51
51
  private LiveSpring source;
52
+ private LiveSpring[] graphSources;
53
+ private ScalarProgram graph;
54
+ private double[] graphValues, graphVelocities;
52
55
  private Mapping[] sourceMaps = new Mapping[0];
53
56
  private static final SampledInput[] NONE = new SampledInput[0];
54
57
  private SampledInput[] inputs = NONE;
55
58
  private double inputRest;
56
59
  private boolean direct = true;
57
- private double targetValue;
60
+ private double targetValue, dataTime;
61
+ private boolean dataInput;
58
62
  private boolean moving, reduced, disposed, paused, inputHeld, sensorInput;
59
63
 
60
64
  public LiveSpring(double initial, double stiffness, double damping, double mass,
@@ -78,6 +82,14 @@ public final class LiveSpring {
78
82
  source = parent;
79
83
  wake();
80
84
  }
85
+ public void compute(LiveSpring[] sources, String program) {
86
+ for (LiveSpring parent : sources)
87
+ if (parent == null || parent.disposed || nodes.indexOf(parent) >= nodes.indexOf(this))
88
+ throw new IllegalArgumentException("Scalar inputs must precede their output");
89
+ ScalarProgram next = new ScalarProgram(program, sources.length);
90
+ graphSources = sources.clone(); graphValues = new double[sources.length]; graphVelocities = new double[sources.length]; graph = next;
91
+ wake();
92
+ }
81
93
  public void updateInput(String snapshot) {
82
94
  if (disposed) return;
83
95
  try { inputs = new SampledInput[]{ new SampledInput(new JSONObject(snapshot)) }; }
@@ -106,9 +118,14 @@ public final class LiveSpring {
106
118
  for (SampledInput input : inputs) if (input.running(now)) return true;
107
119
  return false;
108
120
  }
121
+ public void setDataInput(double value, double velocity) {
122
+ if(disposed || !Double.isFinite(value) || !Double.isFinite(velocity))return;
123
+ dataInput=true; dataTime=SystemClock.uptimeMillis(); sensorInput=true; inputHeld=true; direct=true; inputs=NONE;
124
+ targetValue=value; state.position=value; state.velocity=velocity; moving=velocity!=0; schedule();
125
+ }
109
126
  public void setSensorInput(double value) {
110
127
  if (disposed || !Double.isFinite(value) || (sensorInput && inputHeld && state.position == value)) return;
111
- sensorInput = true; inputHeld = true; direct = true; targetValue = value; inputs = NONE;
128
+ dataInput = false; sensorInput = true; inputHeld = true; direct = true; targetValue = value; inputs = NONE;
112
129
  state.position = value; state.velocity = 0; moving = false;
113
130
  schedule();
114
131
  }
@@ -119,7 +136,7 @@ public final class LiveSpring {
119
136
  }
120
137
  public void holdInput(double value, double velocity) {
121
138
  if (disposed) return;
122
- sensorInput = false; inputHeld = true; direct = true; targetValue = value; inputs = NONE;
139
+ dataInput = false; sensorInput = false; inputHeld = true; direct = true; targetValue = value; inputs = NONE;
123
140
  state.position = value; state.velocity = velocity; moving = true;
124
141
  apply(); schedule();
125
142
  }
@@ -140,10 +157,28 @@ public final class LiveSpring {
140
157
  schedule();
141
158
  }
142
159
  private void step(double now, double dt) {
143
- if (disposed || paused || inputHeld) return;
160
+ if (disposed || paused) return;
161
+ if (inputHeld) {
162
+ if(dataInput && now-dataTime>=100) { state.velocity=0; moving=false; }
163
+ return;
164
+ }
165
+ if (graph != null) {
166
+ boolean active = false;
167
+ for (int i=0; i<graphSources.length; i++) {
168
+ LiveSpring input = graphSources[i];
169
+ graphValues[i] = input.state.position; graphVelocities[i] = input.disposed || input.paused ? 0 : input.state.velocity;
170
+ active |= input.moving;
171
+ }
172
+ graph.evaluate(graphValues, graphVelocities);
173
+ state.position = graph.value(); state.velocity = graph.velocity(); moving = active;
174
+ return;
175
+ }
144
176
  double target = source == null ? (direct ? targetValue : inputValue(now)) : map(source.state.position, sourceMaps);
145
177
  boolean inputMoving = source == null ? !direct && inputMoving(now) : source.moving;
146
- if (reduced) { state.position = target; state.velocity = 0; }
178
+ if (reduced) {
179
+ state.position = target;
180
+ state.velocity = source == null ? (direct ? 0 : (inputValue(now + .5) - inputValue(now - .5)) * 1000) : mapVelocity(source.state.position, source.state.velocity, sourceMaps);
181
+ }
147
182
  else state.advance(target, stiffness, damping, mass, dt);
148
183
  boolean rest = Math.abs(state.position - target) <= restDelta && Math.abs(state.velocity) <= restSpeed;
149
184
  if (rest && !inputMoving) { state.position = target; state.velocity = 0; }
@@ -195,7 +230,7 @@ public final class LiveSpring {
195
230
  child.setTarget(map(state.position, child.sourceMaps));
196
231
  }
197
232
  }
198
- source = null;
233
+ source = null; graph = null; graphSources = null; graphValues = graphVelocities = null;
199
234
  if (nodes.isEmpty() && scheduled) {
200
235
  Choreographer.getInstance().removeFrameCallback(clock);
201
236
  scheduled = false; previousFrame = 0;
@@ -304,6 +339,17 @@ public final class LiveSpring {
304
339
  for (Mapping mapping : maps) value = mapping.value(value);
305
340
  return value;
306
341
  }
342
+ private static double mapVelocity(double value, double velocity, Mapping[] maps) {
343
+ for (Mapping mapping : maps) {
344
+ if (mapping.colors != null || (mapping.clamp && (value<=mapping.input[0] || value>=mapping.input[mapping.input.length-1]))) velocity=0;
345
+ else {
346
+ int k=0; while(k<mapping.input.length-2 && value>mapping.input[k+1]) k++;
347
+ velocity *= (mapping.output[k+1]-mapping.output[k])/(mapping.input[k+1]-mapping.input[k]);
348
+ }
349
+ value = mapping.value(value);
350
+ }
351
+ return Double.isFinite(velocity)?velocity:0;
352
+ }
307
353
  private static double[] array(JSONArray input) throws JSONException {
308
354
  double[] result = new double[input.length()];
309
355
  for (int i = 0; i < result.length; i++) result[i] = input.getDouble(i);
@@ -1,8 +1,10 @@
1
1
  package org.rafaelaugusto.jolt;
2
2
 
3
3
  import android.app.Activity;
4
+ import android.content.Context;
4
5
  import android.graphics.Matrix;
5
6
  import android.graphics.Rect;
7
+ import android.hardware.input.InputManager;
6
8
  import android.view.*;
7
9
  import android.view.accessibility.AccessibilityEvent;
8
10
  import java.lang.ref.WeakReference;
@@ -18,6 +20,7 @@ public final class PointerInput {
18
20
  private Listener listener;
19
21
  private Hub hub;
20
22
  private boolean active;
23
+ private int device = -1;
21
24
  private float lastX, lastY;
22
25
  public PointerInput(Activity activity, View target, Listener listener) {
23
26
  Window window = activity.getWindow();
@@ -27,16 +30,19 @@ public final class PointerInput {
27
30
  }
28
31
  this.target = new WeakReference<>(target); this.listener = listener; this.hub = current;
29
32
  current.inputs.add(this);
33
+ current.watch();
30
34
  }
31
35
  private void reset() {
32
36
  if (active && listener != null) listener.onPoint(0, 0, 0, 0, false);
33
37
  active = false;
38
+ device = -1;
34
39
  }
35
40
  private void observe(MotionEvent event) {
36
41
  if (listener == null) return;
37
42
  int tool = event.getToolType(0), action = event.getActionMasked();
38
43
  if (tool != MotionEvent.TOOL_TYPE_MOUSE && tool != MotionEvent.TOOL_TYPE_STYLUS) { reset(); return; }
39
44
  if (action == MotionEvent.ACTION_HOVER_EXIT || action == MotionEvent.ACTION_CANCEL || (tool == MotionEvent.TOOL_TYPE_STYLUS && action != MotionEvent.ACTION_HOVER_MOVE && action != MotionEvent.ACTION_HOVER_ENTER)) { reset(); return; }
45
+ device = event.getDeviceId();
40
46
  lastX = event.getX(); lastY = event.getY();
41
47
  sample(lastX, lastY);
42
48
  }
@@ -58,7 +64,7 @@ public final class PointerInput {
58
64
  if (!forward.invert(inverse)) { reset(); return; }
59
65
  float[] point = {x, y}; inverse.mapPoints(point);
60
66
  Rect visible = new Rect();
61
- if (view.getWidth() <= 0 || view.getHeight() <= 0 || !view.getLocalVisibleRect(visible) || !visible.contains((int)point[0], (int)point[1]) || point[0] < 0 || point[1] < 0 || point[0] > view.getWidth() || point[1] > view.getHeight()) { reset(); return; }
67
+ if (view.getWidth() <= 0 || view.getHeight() <= 0 || !view.getGlobalVisibleRect(visible) || !visible.contains((int)x, (int)y) || point[0] < 0 || point[1] < 0 || point[0] > view.getWidth() || point[1] > view.getHeight()) { reset(); return; }
62
68
  double density = view.getResources().getDisplayMetrics().density;
63
69
  active = true;
64
70
  listener.onPoint((point[0] - view.getWidth()/2.0)/density, (point[1] - view.getHeight()/2.0)/density, view.getWidth()/density, view.getHeight()/density, true);
@@ -69,6 +75,7 @@ public final class PointerInput {
69
75
  if (previous == null) return;
70
76
  previous.inputs.remove(this);
71
77
  if (previous.inputs.isEmpty()) {
78
+ previous.release();
72
79
  Window window = previous.window.get();
73
80
  if (window != null) {
74
81
  if (window.getCallback() == previous) window.setCallback(previous.delegate);
@@ -76,11 +83,26 @@ public final class PointerInput {
76
83
  }
77
84
  }
78
85
  }
79
- private static final class Hub implements Window.Callback {
86
+ private static final class Hub implements Window.Callback, InputManager.InputDeviceListener {
80
87
  final WeakReference<Window> window;
81
88
  final Window.Callback delegate;
82
89
  final List<PointerInput> inputs = new ArrayList<>();
90
+ private InputManager devices;
83
91
  Hub(Window window) { this.window = new WeakReference<>(window); this.delegate = window.getCallback(); }
92
+ void watch() {
93
+ Window current = window.get();
94
+ if (devices != null || inputs.isEmpty() || current == null) return;
95
+ devices = (InputManager)current.getContext().getSystemService(Context.INPUT_SERVICE);
96
+ if (devices != null) devices.registerInputDeviceListener(this, null);
97
+ }
98
+ void release() {
99
+ if (devices != null) devices.unregisterInputDeviceListener(this);
100
+ devices = null;
101
+ }
102
+ private void lost(int id) { for (PointerInput input : new ArrayList<>(inputs)) if (input.device == id) input.reset(); }
103
+ public void onInputDeviceAdded(int id) {}
104
+ public void onInputDeviceRemoved(int id) { lost(id); }
105
+ public void onInputDeviceChanged(int id) { if (InputDevice.getDevice(id) == null) lost(id); }
84
106
  void observe(MotionEvent event) {
85
107
  for (PointerInput input : new ArrayList<>(inputs)) {
86
108
  try { input.observe(event); }
@@ -101,8 +123,8 @@ public final class PointerInput {
101
123
  public void onWindowAttributesChanged(WindowManager.LayoutParams params) { delegate.onWindowAttributesChanged(params); }
102
124
  public void onContentChanged() { delegate.onContentChanged(); }
103
125
  public void onWindowFocusChanged(boolean focus) { if (!focus) for (PointerInput input : new ArrayList<>(inputs)) input.reset(); delegate.onWindowFocusChanged(focus); }
104
- public void onAttachedToWindow() { delegate.onAttachedToWindow(); }
105
- public void onDetachedFromWindow() { for (PointerInput input : new ArrayList<>(inputs)) input.reset(); delegate.onDetachedFromWindow(); }
126
+ public void onAttachedToWindow() { watch(); delegate.onAttachedToWindow(); }
127
+ public void onDetachedFromWindow() { release(); for (PointerInput input : new ArrayList<>(inputs)) input.reset(); delegate.onDetachedFromWindow(); }
106
128
  public void onPanelClosed(int id, Menu menu) { delegate.onPanelClosed(id, menu); }
107
129
  public boolean onSearchRequested() { return delegate.onSearchRequested(); }
108
130
  public boolean onSearchRequested(SearchEvent event) { return delegate.onSearchRequested(event); }
@@ -0,0 +1,76 @@
1
+ package org.rafaelaugusto.jolt;
2
+
3
+ import org.json.JSONArray;
4
+ import org.json.JSONObject;
5
+ import org.json.JSONException;
6
+
7
+ /** Immutable DAG; storage is allocated at setup, never during a frame. */
8
+ final class ScalarProgram {
9
+ private final String[] operations;
10
+ private final int[] a, b;
11
+ private final double[] constants, maximum, values, velocities;
12
+ private final double[][] input, output;
13
+ private final boolean[] clamp;
14
+ ScalarProgram(String json, int sources) {
15
+ try {
16
+ JSONArray list = new JSONArray(json);
17
+ int count = list.length();
18
+ if (count < 1 || count > 1024) throw new IllegalArgumentException("Scalar graph size");
19
+ operations = new String[count]; a = new int[count]; b = new int[count];
20
+ constants = new double[count]; maximum = new double[count]; values = new double[count]; velocities = new double[count];
21
+ input = new double[count][]; output = new double[count][]; clamp = new boolean[count];
22
+ for (int i=0; i<count; i++) {
23
+ JSONObject node = list.getJSONObject(i); String op = operations[i] = node.getString("op");
24
+ a[i] = node.optInt("a", 0); b[i] = node.optInt("b", -1);
25
+ constants[i] = node.optDouble("value", 0); maximum[i] = node.optDouble("maximum", 0);
26
+ if (!op.equals("constant") && (a[i] < 0 || a[i] >= (op.equals("input") ? sources : i))) throw new IllegalArgumentException("Cyclic scalar graph");
27
+ if (b[i] < -1 || b[i] >= i) throw new IllegalArgumentException("Cyclic scalar graph");
28
+ if (op.equals("map")) {
29
+ input[i] = array(node.getJSONArray("input")); output[i] = array(node.getJSONArray("output")); clamp[i] = node.getBoolean("clamp");
30
+ if (input[i].length < 2 || input[i].length != output[i].length) throw new IllegalArgumentException("Scalar map size");
31
+ }
32
+ }
33
+ } catch (JSONException error) { throw new IllegalArgumentException(error); }
34
+ }
35
+ private static double[] array(JSONArray array) throws JSONException {
36
+ double[] result = new double[array.length()];
37
+ for (int i=0; i<result.length; i++) result[i]=array.getDouble(i);
38
+ return result;
39
+ }
40
+ void evaluate(double[] sourceValues, double[] sourceVelocities) {
41
+ for (int i=0; i<operations.length; i++) {
42
+ String op = operations[i];
43
+ boolean reads = !op.equals("input") && !op.equals("constant");
44
+ double x = reads ? values[a[i]] : 0, xv = reads ? velocities[a[i]] : 0, y = b[i] < 0 ? 0 : values[b[i]], yv = b[i] < 0 ? 0 : velocities[b[i]];
45
+ double value = 0, velocity = 0;
46
+ switch (op) {
47
+ case "input": value=sourceValues[a[i]]; velocity=sourceVelocities[a[i]]; break;
48
+ case "constant": value=constants[i]; break;
49
+ case "map": {
50
+ double[] in=input[i], out=output[i];
51
+ if (clamp[i] && x<=in[0]) { value=out[0]; break; }
52
+ if (clamp[i] && x>=in[in.length-1]) { value=out[out.length-1]; break; }
53
+ int k=0; while(k<in.length-2 && x>in[k+1]) k++;
54
+ double slope=(out[k+1]-out[k])/(in[k+1]-in[k]);
55
+ value=out[k]+(x-in[k])*slope; velocity=xv*slope; break;
56
+ }
57
+ case "add": value=x+y; velocity=xv+yv; break;
58
+ case "sub": value=x-y; velocity=xv-yv; break;
59
+ case "mul": value=x*y; velocity=xv*y+x*yv; break;
60
+ case "div": if(y!=0) { value=x/y; velocity=(xv*y-x*yv)/(y*y); } break;
61
+ case "min": value=Math.min(x,y); velocity=x<y?xv:x>y?yv:0; break;
62
+ case "max": value=Math.max(x,y); velocity=x>y?xv:x<y?yv:0; break;
63
+ case "clamp": value=Math.min(maximum[i],Math.max(y,x)); velocity=x<=y||x>=maximum[i]?0:xv; break;
64
+ case "abs": value=Math.abs(x); velocity=Math.signum(x)*xv; break;
65
+ case "sin": value=Math.sin(x); velocity=Math.cos(x)*xv; break;
66
+ case "cos": value=Math.cos(x); velocity=-Math.sin(x)*xv; break;
67
+ case "velocity": value=xv; break;
68
+ default: throw new IllegalArgumentException("Unknown scalar operation: "+op);
69
+ }
70
+ velocities[i]=Double.isFinite(value)&&Double.isFinite(velocity)?velocity:0;
71
+ values[i]=Double.isFinite(value)?value:0;
72
+ }
73
+ }
74
+ double value() { return values[values.length-1]; }
75
+ double velocity() { return velocities[velocities.length-1]; }
76
+ }
@@ -0,0 +1,86 @@
1
+ package org.rafaelaugusto.jolt;
2
+
3
+ import android.os.SystemClock;
4
+ import android.view.View;
5
+ import android.view.ViewGroup;
6
+ import android.view.ViewTreeObserver;
7
+ import android.widget.ListView;
8
+ import java.lang.reflect.Method;
9
+
10
+ /** Observes without replacing the widget's scroll listener/adapter. No JS callback unless explicitly observed. */
11
+ public final class ScrollSource implements ViewTreeObserver.OnScrollChangedListener, View.OnLayoutChangeListener {
12
+ public interface Listener { void changed(); }
13
+ private final ViewGroup source;
14
+ private final LiveSpring node;
15
+ private Listener listener;
16
+ private final boolean horizontal;
17
+ private final double density, rowHeight;
18
+ private final ListView list;
19
+ private Object manager;
20
+ private Method first, find;
21
+ private boolean enabled, observing, disposed;
22
+ private ViewTreeObserver observer;
23
+ private double offset, previousTime;
24
+ private long updates, callbacks;
25
+ private static int active;
26
+ public static int activeCount() { return active; }
27
+ public ScrollSource(View source, LiveSpring node, boolean horizontal, double rowHeight, Listener listener) {
28
+ if (!(source instanceof ViewGroup)) throw new IllegalArgumentException("Expected a native scroll container");
29
+ this.source=(ViewGroup)source; this.node=node; this.horizontal=horizontal; this.rowHeight=rowHeight; this.listener=listener;
30
+ density=source.getResources().getDisplayMetrics().density;
31
+ list=source instanceof ListView ? (ListView)source : null;
32
+ if (list != null) {
33
+ if (horizontal || rowHeight<=0) throw new IllegalArgumentException("ListView scroll values need vertical fixed-height rows and rowHeight in DIPs");
34
+ } else if (!(source instanceof android.widget.ScrollView) && !(source instanceof android.widget.HorizontalScrollView) && !(source instanceof androidx.core.widget.NestedScrollView)) {
35
+ // CollectionView's RecyclerView dependency is optional. Resolve its linear layout manager once.
36
+ try {
37
+ Class<?> recycler=Class.forName("androidx.recyclerview.widget.RecyclerView");
38
+ if (!recycler.isInstance(source)) throw new IllegalArgumentException("Unsupported scroll container");
39
+ manager=recycler.getMethod("getLayoutManager").invoke(source);
40
+ if (manager==null || !Class.forName("androidx.recyclerview.widget.LinearLayoutManager").isInstance(manager) || Class.forName("androidx.recyclerview.widget.GridLayoutManager").isInstance(manager) || rowHeight<=0)
41
+ throw new IllegalArgumentException("CollectionView needs a linear layout and fixed rowHeight (item stride for x)");
42
+ Class<?> type=manager.getClass();
43
+ int orientation=(Integer)type.getMethod("getOrientation").invoke(manager);
44
+ if ((orientation==0)!=horizontal || (Boolean)type.getMethod("getReverseLayout").invoke(manager) || (Boolean)type.getMethod("getStackFromEnd").invoke(manager))
45
+ throw new IllegalArgumentException("Reversed/staggered CollectionView offsets are unsupported");
46
+ first=type.getMethod("findFirstVisibleItemPosition"); find=type.getMethod("findViewByPosition",int.class);
47
+ } catch (ReflectiveOperationException error) { throw new IllegalArgumentException("Unsupported CollectionView layout",error); }
48
+ }
49
+ setEnabled(true);
50
+ }
51
+ private double read() {
52
+ if (list!=null) {
53
+ View child=list.getChildAt(0); if(child==null)return 0;
54
+ return list.getFirstVisiblePosition()*(rowHeight+list.getDividerHeight()/density)+(list.getPaddingTop()-child.getTop())/density;
55
+ }
56
+ if (manager!=null) {
57
+ try {
58
+ int index=(Integer)first.invoke(manager); if(index<0)return 0;
59
+ View child=(View)find.invoke(manager,index); if(child==null)return offset;
60
+ double local=horizontal ? source.getLayoutDirection()==View.LAYOUT_DIRECTION_RTL ? source.getWidth()-source.getPaddingRight()-child.getRight() : source.getPaddingLeft()-child.getLeft() : source.getPaddingTop()-child.getTop();
61
+ return index*rowHeight+local/density;
62
+ } catch(ReflectiveOperationException error) { return offset; }
63
+ }
64
+ return (horizontal?source.getScrollX():source.getScrollY())/density;
65
+ }
66
+ public void setEnabled(boolean value) {
67
+ if(disposed || enabled==value)return;
68
+ enabled=value;
69
+ if(value) { observer=source.getViewTreeObserver(); observer.addOnScrollChangedListener(this); source.addOnLayoutChangeListener(this); active++; refresh(); }
70
+ else { if(observer!=null && observer.isAlive())observer.removeOnScrollChangedListener(this); observer=null; source.removeOnLayoutChangeListener(this); active--; node.setDataInput(offset,0); }
71
+ }
72
+ public void setObserving(boolean value) { observing=value; }
73
+ public void refresh() { update(true); }
74
+ @Override public void onScrollChanged() { update(false); }
75
+ @Override public void onLayoutChange(View view,int l,int t,int r,int b,int ol,int ot,int or,int ob) { update(true); }
76
+ private void update(boolean force) {
77
+ if(disposed || !enabled)return;
78
+ double next=read(), now=SystemClock.uptimeMillis();
79
+ if(!Double.isFinite(next) || (!force && next==offset))return;
80
+ double velocity=force || previousTime==0 || now<=previousTime || now-previousTime>100 ? 0 : (next-offset)*1000/(now-previousTime);
81
+ offset=next; previousTime=now; updates++; node.setDataInput(offset,velocity);
82
+ if(observing && listener!=null) { callbacks++; listener.changed(); }
83
+ }
84
+ public String snapshot() { return "{\"offset\":"+offset+",\"updates\":"+updates+",\"callbacks\":"+callbacks+"}"; }
85
+ public void dispose() { if(disposed)return; setEnabled(false); disposed=true; listener=null; manager=null; }
86
+ }
@@ -18,7 +18,7 @@ public final class SharedLayoutFlight extends View implements Choreographer.Fram
18
18
  private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
19
19
  private Bitmap source, target, pageBitmap;
20
20
  private RectF pageFrame;
21
- private boolean fading, clippedImage;
21
+ private boolean fading, clippedImage, exiting;
22
22
  private float sourceWidth, sourceHeight, targetWidth, targetHeight;
23
23
  private boolean sourceImage, targetImage, disposed, running, navigationImage, borrowedTarget;
24
24
  private double[] interactiveStart, interactiveEnd;
@@ -220,6 +220,12 @@ public final class SharedLayoutFlight extends View implements Choreographer.Fram
220
220
  source=bitmap; target=null; sourceWidth=(float)pose[2]*density; sourceHeight=(float)pose[3]*density; sourceImage=false; pose[5]=0;
221
221
  destination=null; invalidate(); return true;
222
222
  }
223
+ /** A leaving snapshot has no destination and must not hide a recycled source. */
224
+ public void prepareExit() {
225
+ exiting=true;
226
+ for(Map.Entry<View,Integer> entry:hidden.entrySet()) if(entry.getKey().getVisibility()==INVISIBLE)entry.getKey().setVisibility(entry.getValue());
227
+ hidden.clear();
228
+ }
223
229
  public void play(String json, Listener callback) {
224
230
  if(disposed) return;
225
231
  try {
@@ -249,7 +255,7 @@ public final class SharedLayoutFlight extends View implements Choreographer.Fram
249
255
  if(!running || disposed)return;
250
256
  if(fading){double p=Math.min(1,(android.os.SystemClock.uptimeMillis()-started)/duration);setAlpha((float)(1-p));if(p>=1)end(true);else Choreographer.getInstance().postFrameCallback(this);return;}
251
257
  RectF frame=geometry(destination);
252
- if(root.getWidth()!=rootWidth || root.getHeight()!=rootHeight || frame==null || Math.abs(frame.left-destinationFrame.left)>1 || Math.abs(frame.top-destinationFrame.top)>1 || Math.abs(frame.width()-destinationFrame.width())>1 || Math.abs(frame.height()-destinationFrame.height())>1) { end(false); return; }
258
+ if(root.getWidth()!=rootWidth || root.getHeight()!=rootHeight || (!exiting && (frame==null || Math.abs(frame.left-destinationFrame.left)>1 || Math.abs(frame.top-destinationFrame.top)>1 || Math.abs(frame.width()-destinationFrame.width())>1 || Math.abs(frame.height()-destinationFrame.height())>1))) { end(false); return; }
253
259
  double now=android.os.SystemClock.uptimeMillis(); evaluate(now); invalidate();
254
260
  if(now-started>=duration)end(true); else Choreographer.getInstance().postFrameCallback(this);
255
261
  }
@@ -10,10 +10,12 @@ NS_ASSUME_NONNULL_BEGIN
10
10
  + (NSUInteger)activeCount;
11
11
  + (NSUInteger)nodeCount;
12
12
  + (void)sync:(UIView *)view;
13
+ - (void)compute:(NSArray<JoltLiveSpring *> *)sources program:(NSString *)program;
13
14
  - (void)follow:(JoltLiveSpring *)source mappings:(NSString *)mappings;
14
15
  - (void)updateInput:(NSString *)snapshot;
15
16
  - (void)setInputs:(NSString *)snapshots rest:(double)rest;
16
17
  - (void)setSensorInput:(double)value;
18
+ - (void)setDataInput:(double)value velocity:(double)velocity;
17
19
  - (void)setTarget:(double)value;
18
20
  - (void)holdInput:(double)value velocity:(double)velocity;
19
21
  - (void)releaseInput:(double)target velocity:(double)velocity;
@@ -1,4 +1,5 @@
1
1
  #import "JoltLiveSpring.h"
2
+ #import "JoltScalarProgram.h"
2
3
  #import "JoltImageEffect.h"
3
4
  #import "JoltSpringState.h"
4
5
  #import "JoltColorMath.h"
@@ -110,6 +111,7 @@ static double mapped(double value, const JoltMapping *maps, NSUInteger count) {
110
111
 
111
112
  @interface JoltLiveSpring () {
112
113
  JoltSpringState _state;
114
+ double *_graphValues, *_graphVelocities;
113
115
  JoltSampled *_inputs;
114
116
  NSUInteger _inputCount;
115
117
  JoltMapping *_sourceMaps;
@@ -118,8 +120,11 @@ static double mapped(double value, const JoltMapping *maps, NSUInteger count) {
118
120
  @property(nonatomic) double stiffness, damping, mass, restDelta, restSpeed;
119
121
  @property(nonatomic, readwrite) BOOL moving;
120
122
  @property(nonatomic) BOOL reduced, paused, disposed, direct, inputHeld, sensorDriven;
121
- @property(nonatomic) double targetValue;
123
+ @property(nonatomic) double targetValue, dataTime;
124
+ @property(nonatomic) BOOL dataInput;
122
125
  @property(nonatomic, nullable) JoltLiveSpring *source;
126
+ @property(nonatomic) NSArray<JoltLiveSpring *> *graphSources;
127
+ @property(nonatomic) JoltScalarProgram *graph;
123
128
  @property(nonatomic) double inputRest, anchor;
124
129
  @property(nonatomic) NSMutableArray<JoltLiveSpringBinding *> *bindings;
125
130
  - (void)step:(double)now seconds:(double)seconds;
@@ -192,6 +197,7 @@ static double previousFrame;
192
197
  return node;
193
198
  }
194
199
  - (void)dealloc {
200
+ free(_graphValues); free(_graphVelocities);
195
201
  freeSampled(_inputs, _inputCount);
196
202
  freeMappings(_sourceMaps, _sourceMapCount);
197
203
  }
@@ -211,6 +217,15 @@ static double previousFrame;
211
217
  _sourceMaps = decodeMappings(mappings, &_sourceMapCount);
212
218
  self.source = source; [self wake];
213
219
  }
220
+ - (void)compute:(NSArray<JoltLiveSpring *> *)sources program:(NSString *)program {
221
+ for (JoltLiveSpring *parent in sources)
222
+ if (parent.disposed || [nodes indexOfObject:parent] >= [nodes indexOfObject:self])
223
+ [NSException raise:NSInvalidArgumentException format:@"Scalar inputs must precede their output"];
224
+ JoltScalarProgram *next = [[JoltScalarProgram alloc] initWithJSON:program sources:sources.count];
225
+ free(_graphValues); free(_graphVelocities);
226
+ _graphValues=calloc(sources.count, sizeof(double)); _graphVelocities=calloc(sources.count, sizeof(double));
227
+ self.graphSources=[sources copy]; self.graph=next; [self wake];
228
+ }
214
229
  - (void)updateInput:(NSString *)snapshot {
215
230
  if (self.disposed) return;
216
231
  self.sensorDriven = NO; self.direct = NO;
@@ -232,9 +247,15 @@ static double previousFrame;
232
247
  for (NSUInteger i = 0; i < _inputCount; i++) if (sampledRunning(&_inputs[i], self.anchor, now)) return YES;
233
248
  return NO;
234
249
  }
250
+ - (void)setDataInput:(double)value velocity:(double)velocity {
251
+ if(self.disposed || !isfinite(value) || !isfinite(velocity))return;
252
+ self.dataInput=YES; self.dataTime=CACurrentMediaTime()*1000; self.sensorDriven=YES; self.inputHeld=YES; self.direct=YES;
253
+ [self replaceInputs:@[] rest:0]; self.targetValue=value;
254
+ _state=(JoltSpringState){value,velocity}; self.moving=velocity!=0; [JoltLiveSpring schedule];
255
+ }
235
256
  - (void)setSensorInput:(double)value {
236
257
  if (self.disposed || !isfinite(value) || (self.sensorDriven && self.inputHeld && _state.position == value)) return;
237
- self.sensorDriven = YES; self.inputHeld = YES; self.direct = YES; self.targetValue = value;
258
+ self.dataInput=NO; self.sensorDriven = YES; self.inputHeld = YES; self.direct = YES; self.targetValue = value;
238
259
  [self replaceInputs:@[] rest:0];
239
260
  _state = (JoltSpringState){value, 0}; self.moving = NO;
240
261
  [JoltLiveSpring schedule];
@@ -247,7 +268,7 @@ static double previousFrame;
247
268
  }
248
269
  - (void)holdInput:(double)value velocity:(double)velocity {
249
270
  if (self.disposed) return;
250
- self.sensorDriven = NO; self.inputHeld = YES; self.direct = YES; self.targetValue = value;
271
+ self.dataInput=NO; self.sensorDriven = NO; self.inputHeld = YES; self.direct = YES; self.targetValue = value;
251
272
  [self replaceInputs:@[] rest:0];
252
273
  _state = (JoltSpringState){value, velocity}; self.moving = YES;
253
274
  [CATransaction begin]; [CATransaction setDisableActions:YES]; [self apply]; [CATransaction commit];
@@ -273,10 +294,35 @@ static double previousFrame;
273
294
  [JoltLiveSpring schedule];
274
295
  }
275
296
  - (void)step:(double)now seconds:(double)seconds {
276
- if (self.disposed || self.paused || self.inputHeld) return;
297
+ if(self.disposed || self.paused)return;
298
+ if(self.inputHeld) {
299
+ if(self.dataInput && now-self.dataTime>=100) { _state.velocity=0; self.moving=NO; }
300
+ return;
301
+ }
302
+ if (self.graph) {
303
+ BOOL active=NO;
304
+ for (NSUInteger i=0; i<self.graphSources.count; i++) {
305
+ JoltLiveSpring *input=self.graphSources[i];
306
+ _graphValues[i]=input.position; _graphVelocities[i]=input.disposed||input.paused?0:input.velocity; active |= input.moving;
307
+ }
308
+ [self.graph evaluateValues:_graphValues velocities:_graphVelocities];
309
+ _state=(JoltSpringState){self.graph.value,self.graph.velocity}; self.moving=active; return;
310
+ }
277
311
  double target = self.source ? mapped(self.source.position, _sourceMaps, _sourceMapCount) : (self.direct ? self.targetValue : [self inputValue:now]);
278
312
  BOOL inputMoving = self.source ? self.source.moving : (!self.direct && [self inputMoving:now]);
279
- if (self.reduced) _state = (JoltSpringState){target, 0};
313
+ if (self.reduced) {
314
+ double velocity=0;
315
+ if (self.source) {
316
+ double value=self.source.position; velocity=self.source.velocity;
317
+ for (NSUInteger m=0; m<_sourceMapCount; m++) {
318
+ const JoltMapping *mapping=&_sourceMaps[m];
319
+ if (mapping->colors || (mapping->clamp && (value<=mapping->input[0] || value>=mapping->input[mapping->count-1]))) velocity=0;
320
+ else { NSUInteger k=0; while(k<mapping->count-2 && value>mapping->input[k+1]) k++; velocity *= (mapping->output[k+1]-mapping->output[k])/(mapping->input[k+1]-mapping->input[k]); }
321
+ value=mapped(value,mapping,1);
322
+ }
323
+ } else if (!self.direct) velocity=([self inputValue:now+.5]-[self inputValue:now-.5])*1000;
324
+ _state = (JoltSpringState){target, isfinite(velocity)?velocity:0};
325
+ }
280
326
  else _state = JoltSpringAdvance(_state, target, self.stiffness, self.damping, self.mass, seconds);
281
327
  BOOL rest = fabs(_state.position - target) <= self.restDelta && fabs(_state.velocity) <= self.restSpeed;
282
328
  if (rest && !inputMoving) _state = (JoltSpringState){target, 0};
@@ -332,7 +378,7 @@ static double previousFrame;
332
378
  for (JoltLiveSpring *child in nodes) if (child.source == self) {
333
379
  child.source = nil; [child setTarget:mapped(self.position, child->_sourceMaps, child->_sourceMapCount)];
334
380
  }
335
- self.source = nil;
381
+ self.source = nil; self.graph = nil; self.graphSources = nil;
336
382
  if (!nodes.count) { [displayLink invalidate]; displayLink = nil; previousFrame = 0; }
337
383
  }
338
384
  @end
@@ -0,0 +1,9 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ /** Validated immutable DAG with reusable sample storage. */
4
+ @interface JoltScalarProgram : NSObject
5
+ - (instancetype)initWithJSON:(NSString *)json sources:(NSUInteger)sources;
6
+ - (void)evaluateValues:(const double *)values velocities:(const double *)velocities;
7
+ @property(nonatomic, readonly) double value;
8
+ @property(nonatomic, readonly) double velocity;
9
+ @end
@@ -0,0 +1,80 @@
1
+ #import "JoltScalarProgram.h"
2
+ #import <math.h>
3
+
4
+ typedef NS_ENUM(NSUInteger, ScalarOp) { Input, Constant, Map, Add, Sub, Mul, Div, Min, Max, Clamp, Abs, Sin, Cos, Velocity };
5
+ typedef struct {
6
+ ScalarOp op;
7
+ NSUInteger a, b, count;
8
+ BOOL hasB, clamp;
9
+ double constant, maximum, value, velocity;
10
+ double *input, *output;
11
+ } ScalarNode;
12
+ @implementation JoltScalarProgram {
13
+ ScalarNode *_nodes;
14
+ NSUInteger _count;
15
+ }
16
+ - (instancetype)initWithJSON:(NSString *)json sources:(NSUInteger)sources {
17
+ if (!(self = [super init])) return nil;
18
+ NSArray *list = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
19
+ if (![list isKindOfClass:NSArray.class] || !list.count || list.count > 1024)
20
+ [NSException raise:NSInvalidArgumentException format:@"Scalar graph size"];
21
+ NSArray *names = @[@"input", @"constant", @"map", @"add", @"sub", @"mul", @"div", @"min", @"max", @"clamp", @"abs", @"sin", @"cos", @"velocity"];
22
+ _count = list.count; _nodes = calloc(_count, sizeof(ScalarNode));
23
+ for (NSUInteger i=0; i<_count; i++) {
24
+ NSDictionary *jsonNode = list[i]; ScalarNode *node = &_nodes[i];
25
+ node->op = [names indexOfObject:jsonNode[@"op"]];
26
+ node->a = [jsonNode[@"a"] unsignedIntegerValue]; node->b = [jsonNode[@"b"] unsignedIntegerValue];
27
+ node->hasB = jsonNode[@"b"] != nil;
28
+ node->constant = [jsonNode[@"value"] doubleValue]; node->maximum = [jsonNode[@"maximum"] doubleValue];
29
+ if (node->op == NSNotFound || (node->op != Constant && node->a >= (node->op == Input ? sources : i)) || (node->hasB && node->b >= i))
30
+ [NSException raise:NSInvalidArgumentException format:@"Cyclic or unknown scalar graph"];
31
+ if (node->op == Map) {
32
+ NSArray *input = jsonNode[@"input"], *output = jsonNode[@"output"];
33
+ if (input.count < 2 || input.count != output.count) [NSException raise:NSInvalidArgumentException format:@"Scalar map size"];
34
+ node->count = input.count; node->clamp = [jsonNode[@"clamp"] boolValue];
35
+ node->input = calloc(node->count, sizeof(double)); node->output = calloc(node->count, sizeof(double));
36
+ for (NSUInteger k=0; k<node->count; k++) { node->input[k]=[input[k] doubleValue]; node->output[k]=[output[k] doubleValue]; }
37
+ }
38
+ }
39
+ return self;
40
+ }
41
+ - (void)dealloc {
42
+ for (NSUInteger i=0; i<_count; i++) { free(_nodes[i].input); free(_nodes[i].output); }
43
+ free(_nodes);
44
+ }
45
+ - (void)evaluateValues:(const double *)values velocities:(const double *)velocities {
46
+ for (NSUInteger i=0; i<_count; i++) {
47
+ ScalarNode *node = &_nodes[i];
48
+ BOOL reads=node->op!=Input && node->op!=Constant;
49
+ double x=reads?_nodes[node->a].value:0, xv=reads?_nodes[node->a].velocity:0;
50
+ double y=node->hasB?_nodes[node->b].value:0, yv=node->hasB?_nodes[node->b].velocity:0;
51
+ double value=0, velocity=0;
52
+ switch (node->op) {
53
+ case Input: value=values[node->a]; velocity=velocities[node->a]; break;
54
+ case Constant: value=node->constant; break;
55
+ case Map: {
56
+ if (node->clamp && x<=node->input[0]) { value=node->output[0]; break; }
57
+ if (node->clamp && x>=node->input[node->count-1]) { value=node->output[node->count-1]; break; }
58
+ NSUInteger k=0; while(k<node->count-2 && x>node->input[k+1]) k++;
59
+ double slope=(node->output[k+1]-node->output[k])/(node->input[k+1]-node->input[k]);
60
+ value=node->output[k]+(x-node->input[k])*slope; velocity=xv*slope; break;
61
+ }
62
+ case Add: value=x+y; velocity=xv+yv; break;
63
+ case Sub: value=x-y; velocity=xv-yv; break;
64
+ case Mul: value=x*y; velocity=xv*y+x*yv; break;
65
+ case Div: if(y!=0) { value=x/y; velocity=(xv*y-x*yv)/(y*y); } break;
66
+ case Min: value=fmin(x,y); velocity=x<y?xv:x>y?yv:0; break;
67
+ case Max: value=fmax(x,y); velocity=x>y?xv:x<y?yv:0; break;
68
+ case Clamp: value=fmin(node->maximum,fmax(y,x)); velocity=x<=y||x>=node->maximum?0:xv; break;
69
+ case Abs: value=fabs(x); velocity=(x<0?-1:x>0?1:0)*xv; break;
70
+ case Sin: value=sin(x); velocity=cos(x)*xv; break;
71
+ case Cos: value=cos(x); velocity=-sin(x)*xv; break;
72
+ case Velocity: value=xv; break;
73
+ }
74
+ node->velocity=isfinite(value)&&isfinite(velocity)?velocity:0;
75
+ node->value=isfinite(value)?value:0;
76
+ }
77
+ }
78
+ - (double)value { return _nodes[_count-1].value; }
79
+ - (double)velocity { return _nodes[_count-1].velocity; }
80
+ @end
@@ -0,0 +1,11 @@
1
+ #import <UIKit/UIKit.h>
2
+ @class JoltLiveSpring;
3
+ @interface JoltScrollSource : NSObject
4
+ + (instancetype)attach:(UIScrollView *)source node:(JoltLiveSpring *)node horizontal:(BOOL)horizontal changed:(void(^)(void))changed;
5
+ + (NSUInteger)activeCount;
6
+ - (void)setEnabled:(BOOL)enabled;
7
+ - (void)setObserving:(BOOL)observing;
8
+ - (void)refresh;
9
+ - (NSString *)snapshot;
10
+ - (void)dispose;
11
+ @end