-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualEffects.groovy
More file actions
2345 lines (2187 loc) · 108 KB
/
Copy pathVisualEffects.groovy
File metadata and controls
2345 lines (2187 loc) · 108 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2026 euu2021 (Github)
// SPDX-License-Identifier: GPL-2.0-or-later
// Discussion thread: https://github.com/freeplane/freeplane/discussions/2951
// Version: 1.0
/***
* Visual effects for map editing actions.
*
* Companion of SoundEffects.groovy: instead of playing a sound, it draws a short
* animation at the place where the action happened. Covered:
*
* select -> a light mark on the node that just took the selection
* (styles, see SELECT_STYLE)
* create -> a ring expands from the new node and fades out
* move -> the node's trip from old place to new (styles, see MOVE_STYLE)
* delete -> the node leaving, drawn entirely from geometry captured before
* the deletion (styles, see DELETE_STYLE)
* fold / unfold -> the branch collapsing into the node or growing out of it
* (styles, see FOLD_STYLE)
*
* Running the script again switches the effects off (toggle), so a single keyboard
* shortcut turns the whole thing on and off.
*
* HOW IT WORKS
* ------------
* The animation is painted by a transparent JPanel hung on the MapViewScrollPane of
* the tab, exactly like the connection lines of UtilityPanels.groovy. The panel is
* mouse-transparent and reserves no viewport area, so it changes nothing about the
* way the map behaves - it only paints.
*
* Geometry is resolved on EVERY frame rather than captured once, so an effect keeps
* sitting on its node while the map moves, scrolls or relayouts under it. Two actions
* are the exception, because the place they act on is gone by the time they fire:
* a moved node's ORIGIN and everything about a DELETED node are measured in the
* pre-event (onPreNodeMoved / onPreNodeDelete), in map coordinates so they survive
* scrolling, and the effect then draws from those fixed rectangles.
*/
import java.awt.AWTEvent
import java.awt.AlphaComposite
import java.awt.BasicStroke
import java.awt.Color
import java.awt.Composite
import java.awt.Component
import java.awt.Container
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.Point
import java.awt.Rectangle
import java.awt.RenderingHints
import java.awt.Toolkit
import java.awt.Window
import java.awt.event.AWTEventListener
import java.awt.event.AWTEventListenerProxy
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent
import java.awt.event.MouseEvent
import java.awt.geom.Ellipse2D
import java.awt.geom.Line2D
import java.awt.geom.QuadCurve2D
import java.awt.geom.RoundRectangle2D
import java.awt.image.BufferedImage
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.SwingUtilities
import javax.swing.Timer
import javax.swing.UIManager
import org.freeplane.features.map.IMapChangeListener
import org.freeplane.features.map.INodeSelectionListener
import org.freeplane.features.map.MapModel
import org.freeplane.features.map.NodeDeletionEvent
import org.freeplane.features.map.NodeModel
import org.freeplane.features.map.NodeMoveEvent
import org.freeplane.features.mode.Controller
import org.freeplane.view.swing.map.MapView
import org.freeplane.view.swing.map.MapViewScrollPane
import org.freeplane.view.swing.map.NodeView
// ---------------------------------------------------------------------------
// configuration
// ---------------------------------------------------------------------------
final String INSTALL_KEY = 'VisualEffects.teardown'
final boolean EFFECT_ON_CREATE = true
final boolean EFFECT_ON_MOVE = true
final boolean EFFECT_ON_DELETE = true
// 'ripple' - a flash on the node, rings rippling outwards, sparks flying off (the original)
// 'pop' - the outline springs up from nothing, overshoots, and settles (a bounce)
// 'burst' - no rings: a dense burst of sparks and dots shooting out of the node
// 'halo' - a single soft glow swells around the node once and fades (the quiet one)
final String CREATE_STYLE = 'ripple'
// Pasting a branch fires ONE insertion - for the top of the pasted subtree - so the style
// above only ever marks that node. With this on, a rectangle additionally grows from it to
// cover the whole new branch: the exact inverse of the delete sweep. Costs nothing on a
// plain new node, where the subtree is just the node itself and nothing extra is drawn.
final boolean CREATE_SWEEP_BRANCH = true
// 'trail' - a comet tail is dragged from the old place to the new one
// 'ghost' - a translucent copy of the node slides over to the new place
// 'comet' - the sliding copy, dragging a tail behind it
// 'arc' - a curved line is drawn between the two places, with a head running along it
// 'pulse' - a ring implodes where the node was and one expands where it landed
final String MOVE_STYLE = 'arc'
// Where the 'arc' starts. The other styles always use the old SLOT, because they depict the
// node itself travelling; the arc depicts the relationship, so it has a choice.
//
// 'slot' - the spot the node used to occupy. Honest about pixels, but the map rearranges
// around the move, so that spot can end up meaning nothing.
// 'parent' - the node's PREVIOUS PARENT, at wherever it sits after the rearrangement. Both
// ends of the arc are then live nodes, so nothing can go stale, and it reads as
// "this came from there" rather than "this was at these coordinates".
// 'auto' - 'parent' when the move changed parents, 'slot' when it was a reorder inside the
// same parent (where an arc from the parent says nothing about what happened).
final String ARC_ORIGIN = 'auto'
final boolean EFFECT_ON_FOLD = true
final boolean EFFECT_ON_UNFOLD = true
// 'ripple' - rings, closing in on the node when it folds, opening when it unfolds
// 'sweep' - the area the children occupy shrinks into the node / grows out of it
// 'chevrons' - three chevrons on the children's side, pointing in or out
// 'spokes' - one line from the node to each direct child, lit in sequence
final String FOLD_STYLE = 'sweep'
// 'implode' - the node shrinks into its own centre, with a ring falling in after it
// 'shatter' - the node breaks into pieces that fly apart and drop
// 'sweep' - the whole branch that went away collapses into the place it left
// 'ripple' - the sweep, plus the create ripple's flash, rings and sparks fired from the
// spot the node vacated: the same visual vocabulary as a creation, run in the
// delete colour, so the two actions rhyme. The rings close INWARDS here, so
// they converge with the sweep instead of crossing it.
// 'sparks' - the same, without the rings: sweep + flash + sparks only
final String DELETE_STYLE = 'sparks'
// Which node the delete effect hangs from once the map has relaid out. The deleted branch is
// gone by the time anything is painted, so its position has to be reconstructed from a node
// that is still there - and the two candidates disagree whenever the deletion changes the
// layout, which is most of the time.
// 'parent' - the effect stays glued to the node the branch hung from. It follows the parent
// when the relayout moves it, which is what the eye expects: the ghost belongs to
// that node.
// 'root' - the effect stays at the spot the branch occupied on screen just before it went.
// Historically exact, and the root really is the fixed point (measured: 0 px on
// screen while the viewport compensates the layout). The catch is that everything
// BETWEEN the root and the deletion does move, so on a deep map the ghost is left
// behind, detached from the parent by however far the parent travelled.
final String DELETE_ANCHOR = 'parent'
// ---- how long a sweep lasts, wherever it appears ---------------------------------------
// There are two, one per direction, and every action defers to them - so a nudge here moves
// the unfold, the paste, the fold and the delete together. Both are longer than the movement
// itself because every sweep holds (SWEEP_OPEN_TRAVEL / SWEEP_CLOSE_HOLD): the surplus is the
// pause, not a faster animation.
final int SWEEP_OPEN_DURATION_MS = 820 // unfold, paste
final int SWEEP_CLOSE_DURATION_MS = 800 // fold, delete
// A closing sweep can additionally outline every node that stood inside the branch, so the
// shape of what is going is visible before it goes. Off = the rectangles are never captured
// either, so it costs exactly nothing.
final boolean SWEEP_SKELETON = true
final boolean EFFECT_ON_SELECT = true
// Selection is by far the most frequent action - it fires on every arrow key - so these are
// deliberately the quietest and shortest effects here.
// 'halo' - a soft glow swells around the node once and fades (the create halo, quicker)
// 'corners' - four brackets snap onto the node's corners, like a camera focusing
// 'ring' - a single ring falls inwards and lands on the node
final String SELECT_STYLE = 'halo'
final int SELECT_DURATION_MS = 260
// Held arrow keys fire selections faster than any animation can finish. Below this distance
// the effect is simply not started again - the running one is left to play out.
final int SELECT_THROTTLE_MS = 90
// Moving or deleting reselects a neighbour BEFORE the edit event arrives, so without this a
// structural edit would always flash a selection on the wrong node first.
final int SELECT_MUTE_AFTER_EDIT_MS = 350
final Color SELECT_COLOR = null
final int CREATE_DURATION_MS = 520 // the create RIPPLE; a pasted branch's sweep uses the
// shared opening length above, on its own clock
final int MOVE_DURATION_MS = 900
// these exist so an action can be broken out of the shared timing if it ever needs to
final int DELETE_DURATION_MS = SWEEP_CLOSE_DURATION_MS
final int FOLD_DURATION_MS = SWEEP_CLOSE_DURATION_MS
// Folding reaches no listener at all, so it is detected by comparing snapshots of the
// visible node views shortly after each input. Lower = snappier, but more scans.
final int FOLD_POLL_DELAY_MS = 60
// a structural edit reshapes the tree too: keep the two effects from firing on top of
// each other
final int FOLD_MUTE_AFTER_EDIT_MS = 450
final int FRAME_MS = 16 // ~60 fps
final int GROW_PX = 26 // how far the outer ring travels past the node
final int RINGS = 2 // 2 rings give the ripple feel; 1 is drier
final boolean SPARKS = true // short radial strokes leaving a created node
final int SPARK_COUNT = 10
// null = derived from the map background (a strong colour on light maps, a bright
// one on dark maps). Set a Color here to force it.
final Color CREATE_COLOR = null
final Color MOVE_COLOR = null
final Color FOLD_COLOR = null
// delete reads better in a warning colour; null falls back to the shared default
final Color DELETE_COLOR = new Color(255, 90, 70)
// ---------------------------------------------------------------------------
// geometry: the two coordinate systems that matter
// ---------------------------------------------------------------------------
class VisualFxGeometry {
// The node's content rectangle in MAP coordinates. Map coordinates are what a captured
// position must be stored in: they do not move when the view scrolls, while overlay
// coordinates do.
static Rectangle contentRectInMap(MapView mapView, NodeModel node) {
NodeView view = visibleViewOf(mapView, node)
if (view == null) {
return null
}
// getContent() is public and inert, and is the very component that
// getNodeContentLocation measured - getContentPane() is private and mutates the view
JComponent content = view.getContent()
if (content == null || content.getWidth() <= 0) {
return null
}
Point location = mapView.getNodeContentLocation(view)
return new Rectangle((int) location.getX(), (int) location.getY(),
content.getWidth(), content.getHeight())
}
// Everything the node's branch covers on screen, in MAP coordinates. Used by the delete
// effect, which has to know the whole extent BEFORE the deletion - afterwards there is
// no view left to measure.
static Rectangle subtreeRectInMap(MapView mapView, NodeModel node) {
Rectangle box = contentRectInMap(mapView, node)
if (node.isFolded()) {
return box
}
for (NodeModel child : node.getChildren()) {
Rectangle childBox = subtreeRectInMap(mapView, child)
if (childBox != null) {
box = box == null ? childBox : box.union(childBox)
}
}
return box
}
// The node itself, or the outermost folded ancestor standing in for it
static NodeView visibleViewOf(MapView mapView, NodeModel node) {
NodeView view = mapView.getNodeView(node)
if (view != null) {
return view
}
NodeModel ancestor = node.getParentNode()
while (ancestor != null) {
view = mapView.getNodeView(ancestor)
if (view != null) {
return view
}
ancestor = ancestor.getParentNode()
}
return null
}
}
// ---------------------------------------------------------------------------
// folding: nobody is told about it, so it has to be watched
//
// NodeModel.setFolded only notifies the views - neither IMapChangeListener nor
// INodeChangeListener ever hears about a fold. The way out is to walk the tree of
// NODE VIEWS (which exists only for what is on screen) right after each user input
// and compare it with the previous walk: a node that just entered the folded set
// folded, one that left it unfolded. The same walk collects the geometry, because a
// folded node's children are gone by the time the fold is noticed - the area they
// used to occupy is only knowable from the PREVIOUS snapshot.
// ---------------------------------------------------------------------------
@groovy.transform.CompileStatic
class VisualFxFoldScanner {
// Walks the visible node views, filling `folded` with the models of folded nodes and
// `geometry` with [subtree box, direct children boxes, own box, from, to] for expanded
// nodes near the viewport. Returns the subtree box of this view, in MAP coordinates.
//
// Coordinates are accumulated on the way down (each node view is a child component of
// its parent's view), which keeps the whole walk at one addition per node - no
// convertPoint call per node.
//
// `flat` receives EVERY visited rectangle, once, in DFS order. That order is what makes
// the skeleton affordable: a node's subtree is a CONTIGUOUS range of that list, so each
// candidate only has to remember two ints instead of a copy of its descendants. Keeping a
// per-node list would cost O(nodes x depth); this is O(nodes) for the whole scan.
static Rectangle scan(NodeView view, int offsetX, int offsetY, Rectangle viewportInMap,
Set<NodeModel> folded, Map<NodeModel, Object[]> geometry, List<Rectangle> flat) {
int x = offsetX + view.getX()
int y = offsetY + view.getY()
JComponent content = view.getContent()
Rectangle own = content == null ? null :
new Rectangle(x + content.getX(), y + content.getY(),
content.getWidth(), content.getHeight())
int from = flat.size()
if (own != null) {
flat.add(own)
}
Rectangle box = own
if (view.isFolded()) {
folded.add(view.getNode())
return box
}
List<NodeView> children = view.getChildrenViews()
List<Rectangle> childBoxes = new ArrayList<Rectangle>()
for (NodeView child : children) {
Rectangle childBox = scan(child, x, y, viewportInMap, folded, geometry, flat)
if (childBox == null) {
continue
}
box = box == null ? new Rectangle(childBox) : box.union(childBox)
JComponent childContent = child.getContent()
if (childContent != null) {
childBoxes.add(new Rectangle(x + child.getX() + childContent.getX(),
y + child.getY() + childContent.getY(),
childContent.getWidth(), childContent.getHeight()))
}
}
// Remember only what the user can actually fold: keeping a rectangle per node of a
// 25 000 node map would cost far more memory than the effect is worth.
//
// The node's OWN rectangle is kept alongside the subtree box, because the effect
// needs the two as an offset, not as absolute map coordinates: folding relayouts
// the map and moves every node, so an absolute rectangle captured before the fold
// points somewhere else afterwards.
if (!childBoxes.isEmpty() && box != null && box.intersects(viewportInMap)) {
geometry.put(view.getNode(), [box, childBoxes, own, from, flat.size()] as Object[])
}
return box
}
}
// ---------------------------------------------------------------------------
// one running effect
// ---------------------------------------------------------------------------
class VisualFxPulse {
String kind // 'create', 'move', 'delete', 'fold' or 'unfold'
String style // per-action style; ignored by 'create' when it has none
NodeModel node // the anchor: the node itself, or the parent for a deleted one
// Every edit relayouts the map, so where a captured rectangle should be pinned is a
// question with a MEASURED answer, not an obvious one - and it differs per action:
//
// fold/unfold : offset from `node` (the folded node). It re-centres against the
// children that just vanished and moves a lot - measured 339 px, enough
// to put a branch that was above the node below it.
// delete : offset from `node` (the parent). Measured across four shapes of
// deletion: 0-1 px off, against 15-28 px for absolute coordinates.
// move : ABSOLUTE map coordinates, on purpose. Anchoring the origin to the old
// parent is WORSE (26 px off vs 2 px), because that parent re-centres
// when it loses a child while the screen position barely moves.
Rectangle areaOffset
List<Rectangle> childrenOffset
// fold/delete: one rectangle per node that was inside the branch, so the sweep can show
// the shape of what is about to go. Offsets, like everything else here.
List<Rectangle> skeletonOffsets
// the same outlines pre-rendered once, for when there are too many to stroke per frame;
// `skeletonImage` is positioned at the node plus `skeletonImageAt`
BufferedImage skeletonImage
Rectangle skeletonImageAt
boolean skeletonImageTried
boolean sweepBranch // create: also sweep the whole subtree the node brought with it
Rectangle originOffset // delete: the deleted node's rect, relative to the parent
Rectangle originInMap // move: where the node came from, in map coordinates
// move + arc: when set, the arc starts at THIS node's live position instead of the slot
// above, which sidesteps staleness entirely - both ends are then resolved every frame
NodeModel originAnchor
long startedAt
// The pulse lives for durationMs, but its STYLE may be a shorter animation riding inside
// it. That is what lets a pasted branch keep the create ripple exactly as it was (520 ms)
// while its sweep runs on the same clock as every other opening sweep (820 ms). For every
// other action the two are equal and this costs nothing.
int durationMs
int styleDurationMs
int growPx
int rings
boolean sparks
int sparkCount
Color color
long seed
private static double clamp(long elapsed, int span) {
double t = (double) elapsed / (double) span
return t < 0d ? 0d : (t > 1d ? 1d : t)
}
// the whole pulse, which is also the sweep's clock
double progress(long now) {
return clamp(now - startedAt, durationMs)
}
// the style's own clock: flash, rings, sparks, pop, halo
double styleProgress(long now) {
return clamp(now - startedAt, styleDurationMs > 0 ? styleDurationMs : durationMs)
}
}
// ---------------------------------------------------------------------------
// the overlay: one per map view, created on demand, paints every running effect
// ---------------------------------------------------------------------------
class VisualFxOverlay extends JPanel {
static final String OVERLAY_NAME = 'freeplane-visual-effects-overlay'
// fraction of the move animation the 'arc' style stays at full strength before fading
static final double ARC_HOLD = 0.55d
// ---- the sweep, shared by unfold, fold, delete and paste ------------------------------
// Tuning any of these changes EVERY sweep at once, which is the point of there being one.
// fraction of an opening sweep spent growing; the rest is it resting on the branch, fading
static final double SWEEP_OPEN_TRAVEL = 0.55d
// fraction of a closing sweep spent resting on the branch before it starts collapsing
static final double SWEEP_CLOSE_HOLD = 0.4d
static final double SWEEP_FILL_ALPHA = 45d
static final double SWEEP_EDGE_ALPHA = 200d
static final float SWEEP_EDGE_WIDTH = 2f
// how far outside the node the 'corners' brackets come to rest
static final double SELECT_CORNER_REST = 5d
// the skeleton: outlines of the nodes that were inside a branch being folded or deleted
static final double SKELETON_ALPHA = 120d
static final float SKELETON_EDGE_WIDTH = 1.2f
// A hard ceiling on how many outlines are ever captured. Measured on live maps, a fold
// draws 13-23 outlines in the median case and up to ~230 for a big open branch.
static final int SKELETON_MAX = 400
// Above this many outlines, stroke them ONCE into an image and blit that every frame.
// Measured at 45 us per stroked outline, so the per-frame cost grows without bound; a
// blit does not. Below the threshold the blit is the slower of the two, hence the switch.
// 250 outlines: 13.2 ms/frame stroked -> 3.1 ms/frame blitted
// 30 outlines: 1.7 ms/frame stroked -> 1.4 ms/frame blitted (plus 2.2 ms to build)
static final int SKELETON_CACHE_MIN = 40
private final MapView mapView
private final List<VisualFxPulse> pulses = new ArrayList<VisualFxPulse>()
private final Timer timer
VisualFxOverlay(MapView mapView, int frameMs) {
this.mapView = mapView
setName(OVERLAY_NAME)
setOpaque(false)
setFocusable(false)
this.timer = new Timer(frameMs, { ActionEvent event -> tick() } as ActionListener)
}
// The overlay covers the whole scroll pane, so it must not swallow the clicks meant
// for the map. A component with no mouse listener is already skipped by the Swing
// dispatcher, but saying it explicitly keeps that guarantee independent of what gets
// added to the panel later.
@Override
boolean contains(int x, int y) {
return false
}
void add(VisualFxPulse pulse) {
syncBounds()
// A selection supersedes the previous one - arrow-key navigation would otherwise pile
// up a dozen overlapping effects, each still repainting its own region.
if ('select' == pulse.kind) {
for (Iterator<VisualFxPulse> it = pulses.iterator(); it.hasNext();) {
VisualFxPulse old = it.next()
if ('select' == old.kind) {
repaintPulse(old)
it.remove()
}
}
}
pulses.add(pulse)
if (!timer.isRunning()) {
timer.start()
}
repaintPulse(pulse)
}
// Follow the scroll pane instead of listening to it: a component listener on a target
// that outlives this panel is exactly the kind of leftover that piles up across runs,
// and there is nothing to animate while no effect is running anyway.
void syncBounds() {
Container host = getParent()
if (host == null) {
return
}
if (getX() != 0 || getY() != 0 || getWidth() != host.getWidth() || getHeight() != host.getHeight()) {
setBounds(0, 0, host.getWidth(), host.getHeight())
}
}
void stop() {
timer.stop()
pulses.clear()
}
private void tick() {
if (getParent() == null || !isShowing()) {
// the tab was closed or hidden: nothing to animate against
pulses.clear()
timer.stop()
return
}
syncBounds()
long now = System.currentTimeMillis()
List<VisualFxPulse> finished = new ArrayList<VisualFxPulse>()
for (VisualFxPulse pulse : pulses) {
repaintPulse(pulse)
if (pulse.progress(now) >= 1d) {
finished.add(pulse)
}
}
pulses.removeAll(finished)
if (pulses.isEmpty()) {
timer.stop()
}
}
private void repaintPulse(VisualFxPulse pulse) {
Rectangle anchor = nodeBounds(pulse.node)
Rectangle dirty = anchor
// resolve the pasted branch here too, not only while painting: the dirty region has to
// already cover it on the first frame it becomes measurable, or that frame is clipped
if (pulse.sweepBranch && pulse.areaOffset == null && anchor != null) {
branchOffsetOf(pulse)
}
for (Rectangle extra : [atNode(anchor, pulse.areaOffset),
atNode(anchor, pulse.originOffset),
pulse.originInMap == null ? null : toOverlay(pulse.originInMap),
// the arc can start at the previous parent, which may sit
// well outside the node/slot pair - without it in the dirty
// region the curve gets clipped or leaves a trail
nodeBounds(pulse.originAnchor)]) {
if (extra != null) {
dirty = dirty == null ? extra : dirty.union(extra)
}
}
if (dirty == null) {
return
}
int margin = pulse.growPx + 10
// Rectangle fields read as double in Groovy (the Rectangle2D getters win over the
// int fields), and repaint(double, double, double, double) does not exist
repaint((int) (dirty.getX() - margin), (int) (dirty.getY() - margin),
(int) (dirty.getWidth() + 2 * margin), (int) (dirty.getHeight() + 2 * margin))
}
// A map rectangle in this panel's coordinates. convertPointToAncestor is NOT usable
// here: it walks up from the source and would run past this panel (a sibling of the
// viewport, not an ancestor of the map view) all the way to the screen.
Rectangle toOverlay(Rectangle inMap) {
if (inMap == null) {
return null
}
Point location = SwingUtilities.convertPoint(mapView, (int) inMap.getX(), (int) inMap.getY(), this)
return new Rectangle((int) location.getX(), (int) location.getY(),
(int) inMap.getWidth(), (int) inMap.getHeight())
}
// The branch a freshly created node brought with it, as an offset from the node itself.
//
// Unlike delete and fold, a creation can still be MEASURED - the nodes are on screen. It
// is measured once, on the first frame where the views exist (they may not yet exist when
// the insertion is broadcast), and then kept as an offset so a relayout during the
// animation cannot make it stale. Returns null while it cannot be resolved, and when the
// subtree is just the node itself, which is the ordinary "new empty node" case.
Rectangle branchOffsetOf(VisualFxPulse pulse) {
if (pulse.areaOffset != null) {
return pulse.areaOffset
}
Rectangle own = VisualFxGeometry.contentRectInMap(mapView, pulse.node)
Rectangle subtree = VisualFxGeometry.subtreeRectInMap(mapView, pulse.node)
if (own == null || subtree == null) {
return null
}
if (subtree.getWidth() <= own.getWidth() + 2d && subtree.getHeight() <= own.getHeight() + 2d) {
return null
}
pulse.areaOffset = new Rectangle((int) (subtree.getX() - own.getX()),
(int) (subtree.getY() - own.getY()),
(int) subtree.getWidth(), (int) subtree.getHeight())
return pulse.areaOffset
}
// An offset captured relative to the node, placed against wherever the node is NOW.
static Rectangle atNode(Rectangle node, Rectangle offset) {
if (node == null || offset == null) {
return null
}
// every read below goes through the Rectangle2D getters and comes back as double
return new Rectangle((int) (node.getX() + offset.getX()), (int) (node.getY() + offset.getY()),
(int) offset.getWidth(), (int) offset.getHeight())
}
// Where the node is right now, in this panel's coordinates; null when it has no view
// (hidden inside a folded ancestor) and no visible ancestor either.
Rectangle nodeBounds(NodeModel node) {
if (node == null || getParent() == null) {
return null
}
return toOverlay(VisualFxGeometry.contentRectInMap(mapView, node))
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g)
if (pulses.isEmpty()) {
return
}
long now = System.currentTimeMillis()
Graphics2D g2 = (Graphics2D) g.create()
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE)
// keep the effect inside the viewport: it must not paint over the scroll bars
Rectangle viewport = getParent() instanceof MapViewScrollPane ?
((MapViewScrollPane) getParent()).getViewport().getBounds() : null
if (viewport != null) {
g2.clip(viewport)
}
for (VisualFxPulse pulse : new ArrayList<VisualFxPulse>(pulses)) {
if ('move' == pulse.kind) {
paintMove(g2, pulse, now)
}
else if ('fold' == pulse.kind || 'unfold' == pulse.kind) {
paintFolding(g2, pulse, now)
}
else if ('delete' == pulse.kind) {
paintDelete(g2, pulse, now)
}
else if ('select' == pulse.kind) {
paintSelect(g2, pulse, now)
}
else {
paintCreate(g2, pulse, now)
}
}
}
finally {
g2.dispose()
}
}
// -----------------------------------------------------------------------
// create: something appearing where the new node is
// -----------------------------------------------------------------------
private void paintCreate(Graphics2D g2, VisualFxPulse pulse, long now) {
Rectangle area = nodeBounds(pulse.node)
if (area == null) {
return
}
// two clocks: the sweep runs the full pulse, the style runs its own shorter one
double t = pulse.styleProgress(now)
int red = pulse.color.getRed()
int green = pulse.color.getGreen()
int blue = pulse.color.getBlue()
// the pasted branch, opening out of the node - the delete sweep run backwards. Painted
// first so the style's own marks stay on top of it.
if (pulse.sweepBranch) {
Rectangle branch = atNode(area, branchOffsetOf(pulse))
if (branch != null) {
// a paste is an opening sweep, exactly like an unfold - same function, same
// hold, same duration
paintSweep(g2, area, branch, pulse.progress(now), true, red, green, blue)
}
}
if ('pop' == pulse.style) {
paintCreatePop(g2, pulse, area, t, red, green, blue)
}
else if ('burst' == pulse.style) {
paintFlash(g2, area, t, red, green, blue)
paintSparks(g2, pulse, area, t, red, green, blue, 1.4d)
paintDots(g2, pulse, area, t, red, green, blue)
}
else if ('halo' == pulse.style) {
paintCreateHalo(g2, pulse, area, t, red, green, blue)
}
else {
paintFlash(g2, area, t, red, green, blue)
paintRings(g2, pulse, area, t, red, green, blue, false)
if (pulse.sparks) {
paintSparks(g2, pulse, area, t, red, green, blue, 1d)
}
}
}
// -----------------------------------------------------------------------
// select: the lightest effects here, because this one fires constantly
// -----------------------------------------------------------------------
private void paintSelect(Graphics2D g2, VisualFxPulse pulse, long now) {
Rectangle area = nodeBounds(pulse.node)
if (area == null) {
return
}
double t = pulse.progress(now)
int red = pulse.color.getRed()
int green = pulse.color.getGreen()
int blue = pulse.color.getBlue()
if ('corners' == pulse.style) {
paintSelectCorners(g2, pulse, area, t, red, green, blue)
}
else if ('ring' == pulse.style) {
paintSelectRing(g2, pulse, area, t, red, green, blue)
}
else {
// the create halo, on a shorter clock: one breath around the node
paintCreateHalo(g2, pulse, area, t, red, green, blue)
}
}
// four brackets that fly in and settle on the corners, like a camera focusing
private void paintSelectCorners(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue) {
double close = 1d - Math.pow(1d - t, 3d)
// They settle a few pixels OUTSIDE the node, never on its outline: Freeplane already
// draws a strong selection border there, and brackets landing on it simply vanish.
double away = SELECT_CORNER_REST + pulse.growPx * 0.65d * (1d - close)
// fade in fast, out slow, so the arrival is what the eye catches
double fade = t < 0.25d ? t / 0.25d : Math.pow(1d - (t - 0.25d) / 0.75d, 1.4d)
int alpha = (int) Math.round(fade * 230d)
if (alpha <= 3) {
return
}
double arm = Math.max(6d, Math.min(18d, Math.min(area.width, area.height) * 0.35d))
g2.setColor(new Color(red, green, blue, alpha))
g2.setStroke(new BasicStroke(2.2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND))
double left = area.x - away, right = area.x + area.width + away
double top = area.y - away, bottom = area.y + area.height + away
for (int corner = 0; corner < 4; corner++) {
double cx = (corner == 0 || corner == 2) ? left : right
double cy = corner < 2 ? top : bottom
double dx = (corner == 0 || corner == 2) ? arm : -arm
double dy = corner < 2 ? arm : -arm
g2.draw(new Line2D.Double(cx, cy, cx + dx, cy))
g2.draw(new Line2D.Double(cx, cy, cx, cy + dy))
}
}
// one ring falling inwards, landing on the node
private void paintSelectRing(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue) {
double spread = pulse.growPx * 0.8d * Math.pow(1d - t, 1.6d)
double fade = t < 0.2d ? t / 0.2d : Math.pow(1d - (t - 0.2d) / 0.8d, 1.2d)
int alpha = (int) Math.round(fade * 225d)
if (alpha <= 3) {
return
}
g2.setColor(new Color(red, green, blue, alpha))
g2.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND))
double arc = 12d + spread
g2.draw(new RoundRectangle2D.Double(area.x - spread, area.y - spread,
area.width + 2d * spread, area.height + 2d * spread, arc, arc))
}
// -----------------------------------------------------------------------
// shared building blocks: the pieces the 'ripple' look is made of. Used by the create
// ripple and by the delete ripple, which is why they take a plain rectangle rather than
// reading the node - a deleted node has none.
// -----------------------------------------------------------------------
// the rectangle lights up and fades quickly. Kept light on purpose: it paints OVER the
// node, and a strong flash hides the text the user just typed.
private void paintFlash(Graphics2D g2, Rectangle area, double t, int red, int green, int blue) {
double flash = Math.pow(1d - t, 2.6d) * 0.26d
if (flash > 0.01d) {
g2.setColor(new Color(red, green, blue, (int) Math.round(flash * 255d)))
g2.fill(new RoundRectangle2D.Double(area.x - 2d, area.y - 2d,
area.width + 4d, area.height + 4d, 12d, 12d))
}
}
// Rings that ripple outwards from the rectangle, each starting a bit later so they chase
// each other - or, with `inward`, the same rings closing ONTO it.
//
// Direction is not decoration: an expanding ring next to the delete sweep, which is
// contracting at the same time, gives two large rounded rectangles crossing each other
// and reads as noise. Everything converging on the vacated spot reads as one event.
private void paintRings(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue, boolean inward) {
for (int ring = 0; ring < pulse.rings; ring++) {
double delay = ring * 0.16d
if (t <= delay) {
continue
}
double ringT = Math.min(1d, (t - delay) / (1d - delay))
double ringFade = Math.pow(1d - ringT, 1.8d) * (ring == 0 ? 1d : 0.6d)
double spread = inward ? pulse.growPx * Math.pow(1d - ringT, 1.1d)
: pulse.growPx * (1d - Math.pow(1d - ringT, 3d))
int alpha = (int) Math.round(ringFade * 235d)
if (alpha <= 3) {
continue
}
g2.setColor(new Color(red, green, blue, alpha))
g2.setStroke(new BasicStroke((float) (1d + 2.4d * ringFade),
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND))
double arc = 14d + spread
g2.draw(new RoundRectangle2D.Double(area.x - spread, area.y - spread,
area.width + 2d * spread, area.height + 2d * spread, arc, arc))
}
}
// short radial strokes that shoot out of the node's edge and die
private void paintSparks(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue, double strength) {
if (t >= 0.85d) {
return
}
double eased = 1d - Math.pow(1d - t, 3d)
double sparkFade = Math.pow(1d - t / 0.85d, 1.5d)
int alpha = (int) Math.round(sparkFade * 220d)
if (alpha <= 3) {
return
}
g2.setColor(new Color(red, green, blue, alpha))
g2.setStroke(new BasicStroke((float) (1d + 1.4d * sparkFade * strength),
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND))
Random random = new Random(pulse.seed)
double centerX = area.x + area.width / 2d
double centerY = area.y + area.height / 2d
double halfWidth = area.width / 2d
double halfHeight = area.height / 2d
for (int i = 0; i < pulse.sparkCount; i++) {
double angle = 2d * Math.PI * i / pulse.sparkCount + (random.nextDouble() - 0.5d) * 0.35d
double reach = pulse.growPx * strength * (0.75d + random.nextDouble() * 0.75d)
double cos = Math.cos(angle)
double sin = Math.sin(angle)
// leave from the node's RECTANGLE, not from an inscribed ellipse: on a wide node the
// ellipse bunches every spark up against the two short sides
double toEdge = Math.min(
Math.abs(cos) < 1e-6d ? Double.MAX_VALUE : halfWidth / Math.abs(cos),
Math.abs(sin) < 1e-6d ? Double.MAX_VALUE : halfHeight / Math.abs(sin))
double fromX = centerX + cos * (toEdge + 3d)
double fromY = centerY + sin * (toEdge + 3d)
double travel = reach * eased
double length = 5d + 5d * sparkFade
g2.draw(new Line2D.Double(fromX + cos * travel, fromY + sin * travel,
fromX + cos * (travel + length), fromY + sin * (travel + length)))
}
}
// small dots flung outward, the confetti half of the burst
private void paintDots(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue) {
if (t >= 0.9d) {
return
}
double eased = 1d - Math.pow(1d - t, 2.4d)
double fade = Math.pow(1d - t / 0.9d, 1.5d)
Random random = new Random(pulse.seed * 31L + 7L)
double centerX = area.x + area.width / 2d
double centerY = area.y + area.height / 2d
int dots = pulse.sparkCount + 6
for (int i = 0; i < dots; i++) {
double angle = random.nextDouble() * 2d * Math.PI
double reach = pulse.growPx * (0.9d + random.nextDouble() * 1.3d)
double travel = reach * eased
double radius = (1.4d + random.nextDouble() * 2.2d) * fade
int alpha = (int) Math.round(fade * 235d)
if (alpha <= 3 || radius <= 0.3d) {
continue
}
double x = centerX + Math.cos(angle) * travel
double y = centerY + Math.sin(angle) * travel
g2.setColor(new Color(red, green, blue, alpha))
g2.fill(new Ellipse2D.Double(x - radius, y - radius, radius * 2d, radius * 2d))
}
}
// the outline springs up from a smaller size, overshoots, and settles - a bounce
private void paintCreatePop(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue) {
// a damped spring: overshoots 1.0 once around t=0.5, then settles back to it
double scale = 1d + Math.sin(t * Math.PI * 1.6d) * 0.16d * Math.pow(1d - t, 1.2d)
double fade = Math.pow(1d - t, 1.1d)
double centerX = area.x + area.width / 2d
double centerY = area.y + area.height / 2d
double width = area.width * scale
double height = area.height * scale
double x = centerX - width / 2d
double y = centerY - height / 2d
g2.setColor(new Color(red, green, blue, (int) Math.round(fade * 55d)))
g2.fill(new RoundRectangle2D.Double(x, y, width, height, 12d, 12d))
g2.setColor(new Color(red, green, blue, (int) Math.round(fade * 235d)))
g2.setStroke(new BasicStroke((float) (1.5d + 1.5d * fade),
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND))
g2.draw(new RoundRectangle2D.Double(x, y, width, height, 12d, 12d))
}
// one soft glow that swells around the node and fades - the quiet option
private void paintCreateHalo(Graphics2D g2, VisualFxPulse pulse, Rectangle area, double t,
int red, int green, int blue) {
double grow = 1d - Math.pow(1d - t, 2d)
double reach = Math.max(pulse.growPx * 0.7d, Math.min(area.width, area.height) * 0.4d) * grow
// fade in fast, out slow: a breath around the node
double fade = Math.sin(Math.min(1d, t) * Math.PI)
int layers = 4
for (int i = layers; i >= 1; i--) {
double spread = reach * i / (double) layers
int alpha = (int) Math.round(fade * 42d * (1d - (i - 1) / (double) layers))
if (alpha <= 2) {
continue
}
g2.setColor(new Color(red, green, blue, alpha))
double arc = 16d + spread
g2.fill(new RoundRectangle2D.Double(area.x - spread, area.y - spread,
area.width + 2d * spread, area.height + 2d * spread, arc, arc))
}
}
// -----------------------------------------------------------------------
// move: from where the node was to where it is now
// -----------------------------------------------------------------------
private void paintMove(Graphics2D g2, VisualFxPulse pulse, long now) {
Rectangle to = nodeBounds(pulse.node)
// Absolute, and measured to be the better choice for depicting the old SLOT: the old
// parent re-centres when it loses a child, so pinning the slot to it drifts 26 px
// where absolute drifts 2 px.
Rectangle from = toOverlay(pulse.originInMap)
if (pulse.originAnchor != null) {
// ...but the arc can skip the slot altogether and start at the previous parent,
// resolved live. Nothing to go stale, at the cost of pointing at the parent
// rather than at the exact spot the node vacated.
Rectangle anchored = nodeBounds(pulse.originAnchor)
if (anchored != null) {
from = anchored
}
}
if (to == null || from == null) {
return
}
double t = pulse.progress(now)
double eased = 1d - Math.pow(1d - t, 2.6d)
int red = pulse.color.getRed()
int green = pulse.color.getGreen()
int blue = pulse.color.getBlue()
double fromX = from.x + from.width / 2d
double fromY = from.y + from.height / 2d
double toX = to.x + to.width / 2d
double toY = to.y + to.height / 2d
if ('ghost' == pulse.style || 'comet' == pulse.style) {
paintGhost(g2, pulse, t, eased, from, to, red, green, blue)
}
else if ('arc' == pulse.style) {
paintArc(g2, pulse, t, eased, fromX, fromY, toX, toY, to, red, green, blue)
}
else if ('pulse' == pulse.style) {
paintImplodeExplode(g2, pulse, t, from, to, red, green, blue)
}
else {
paintTrail(g2, pulse, t, eased, from, to, fromX, fromY, toX, toY, red, green, blue)
}
}
// a comet: a thick head running to the new place, dragging a tail that thins out
private void paintTrail(Graphics2D g2, VisualFxPulse pulse, double t, double eased,
Rectangle from, Rectangle to, double fromX, double fromY, double toX, double toY,
int red, int green, int blue) {
double fade = Math.pow(1d - t, 1.4d)
// the node's outline stays behind for a moment where it used to be
double ghostFade = Math.pow(1d - Math.min(1d, t / 0.55d), 1.6d)
if (ghostFade > 0.02d) {
g2.setColor(new Color(red, green, blue, (int) Math.round(ghostFade * 150d)))
g2.setStroke(new BasicStroke(1.6f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1f, [3f, 4f] as float[], 0f))
g2.draw(new RoundRectangle2D.Double(from.x, from.y, from.width, from.height, 12d, 12d))
}
paintTail(g2, t, eased, from, to, fromX, fromY, toX, toY, red, green, blue, 1d)
// arriving: the destination lights up
if (t > 0.55d) {
double landing = (t - 0.55d) / 0.45d
double landingFade = Math.pow(1d - landing, 1.8d)
double spread = pulse.growPx * 0.6d * (1d - Math.pow(1d - landing, 3d))
g2.setColor(new Color(red, green, blue, (int) Math.round(landingFade * 220d)))
g2.setStroke(new BasicStroke((float) (1d + 2d * landingFade),
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND))
g2.draw(new RoundRectangle2D.Double(to.x - spread, to.y - spread,
to.width + 2d * spread, to.height + 2d * spread, 14d + spread, 14d + spread))
}
}
// The tail itself: segments from the tail point up to the head, each thicker and more
// opaque than the last, which is what reads as motion. The band is scaled to the node's
// height - a fixed few pixels disappears next to a real node and reads as a scratch.
private void paintTail(Graphics2D g2, double t, double eased, Rectangle from, Rectangle to,
double fromX, double fromY, double toX, double toY, int red, int green, int blue,
double strength) {
double fade = Math.pow(1d - t, 1.4d) * strength