1111 * Alois Zoitl - initial API and implementation
1212 *******************************************************************************/
1313
14- package org .eclipse .draw2d ;
14+ package org .eclipse .draw2d . shadows ;
1515
1616import org .eclipse .swt .graphics .Color ;
1717
18+ import org .eclipse .draw2d .AbstractBackground ;
19+ import org .eclipse .draw2d .ColorProvider ;
20+ import org .eclipse .draw2d .Graphics ;
21+ import org .eclipse .draw2d .IFigure ;
1822import org .eclipse .draw2d .geometry .Insets ;
1923import org .eclipse .draw2d .geometry .Rectangle ;
2024
21- /**
22- * A versatile border that provides a CSS-style drop shadow effect for both
23- * rectangular and rounded-rectangular figures.
24- *
25- * This border simulates visual depth by layering semi-transparent shapes using
26- * multi-pass exponential decay. It is designed to work "out of the box" for
27- * standard diagramming nodes while remaining highly tunable.
28- *
29- * @since 3.2
30- */
31- public class RectDropShadowBorder extends AbstractBackground {
32-
33- /**
34- * The default value for the corner radius is suited for rectangles
35- *
36- */
37- private static final int DEFAULT_CORNER_START_RADIUS = 4 ;
25+ public abstract class AbstractDropShadowBorder extends AbstractBackground {
3826
3927 /**
4028 * default value for the drop shadow size as suited for a figure in an diagram
4129 *
4230 */
43- private static final int DEFAULT_DROP_SHADOW_SIZE = 6 ;
44-
31+ protected static final int DEFAULT_DROP_SHADOW_SIZE = 6 ;
4532 /**
4633 * default value for the halo size as suited for a figure in an diagram
4734 *
4835 */
49- private static final int DEFAULT_HALO_SIZE = 3 ;
50-
51- private static final int DEFAULT_SHADOW_ALPHA = 25 ;
52-
53- private static final double DEFAULT_SOFTNESS = 4.0 ;
54-
36+ protected static final int DEFAULT_HALO_SIZE = 3 ;
37+ protected static final int DEFAULT_SHADOW_ALPHA = 25 ;
38+ protected static final double DEFAULT_SOFTNESS = 4.0 ;
5539 /**
5640 * Per default the shadow insets are empty so that the shadow will be outside of
5741 * the figure
5842 */
5943 private static final Insets DEFAULT_SHADOW_INSETS = new Insets ();
60-
6144 private static final Rectangle CLIP_RECT_CACHE = new Rectangle ();
62-
63- private int cornerStartRadius ;
64-
6545 private int dropShadowSize ;
66-
6746 private int haloSize ;
68-
6947 private Insets insets ;
70-
7148 private int shadowAlpha ;
72-
7349 private Color shadowColor ;
74-
7550 private double softness ;
7651
77- /**
78- * Default constructor that set everything for a rectangular shaped figure.
79- *
80- * @since 3.2
81- */
82- public RectDropShadowBorder () {
83- this (DEFAULT_CORNER_START_RADIUS );
84-
85- }
86-
87- /**
88- * Create a shadow border for a rounded rectangle with the given corner radius.
89- *
90- * @param cornerRadius The corner radius of the rounded rectangle
91- * @since 3.2
92- */
93- public RectDropShadowBorder (int cornerRadius ) {
94- cornerStartRadius = cornerRadius ;
52+ protected AbstractDropShadowBorder () {
9553 dropShadowSize = DEFAULT_DROP_SHADOW_SIZE ;
9654 haloSize = DEFAULT_HALO_SIZE ;
9755 insets = DEFAULT_SHADOW_INSETS ;
@@ -100,7 +58,7 @@ public RectDropShadowBorder(int cornerRadius) {
10058 softness = DEFAULT_SOFTNESS ;
10159 }
10260
103- private int calcAlphaValue (final double progress ) {
61+ protected int calcAlphaValue (final double progress ) {
10462 return Math .max (1 , (int ) (shadowAlpha * Math .exp (-softness * progress )));
10563 }
10664
@@ -139,56 +97,9 @@ public void paintBackground(IFigure figure, Graphics graphics, Insets insets) {
13997 graphics .clipRect (CLIP_RECT_CACHE );
14098 }
14199
142- private void paintDropShadow (final Graphics graphics , final Rectangle shadowRect , final int size ) {
143- int bottomXStart = shadowRect .x + 1 ;
144- int bottomY = shadowRect .y + 1 + shadowRect .height ;
145- final int bottomXEnd = shadowRect .x + shadowRect .width + 1 - cornerStartRadius ;
146-
147- int rightX = shadowRect .x + shadowRect .width + 1 ;
148- int rightYStart = shadowRect .y + 1 ;
149- final int rightYEnd = shadowRect .y + shadowRect .height + 1 - cornerStartRadius ;
150- int cornerDiameter = 2 * cornerStartRadius ;
151-
152- for (int i = 0 ; i <= size ; i ++) {
153- final double progress = (double ) i / size ;
154- graphics .setAlpha (calcAlphaValue (progress ));
155-
156- graphics .drawLine (bottomXStart , bottomY , bottomXEnd , bottomY );
157- graphics .drawLine (rightX , rightYStart , rightX , rightYEnd );
158- graphics .drawArc (rightX - cornerDiameter , bottomY - cornerDiameter , cornerDiameter , cornerDiameter , 270 ,
159- 90 );
160-
161- bottomXStart ++;
162- bottomY ++;
163- rightX ++;
164- rightYStart ++;
165- cornerDiameter += 2 ;
166- }
167- }
168-
169- private void paintHalo (final Graphics graphics , final Rectangle shadowRect , final int size ) {
170- final Rectangle r = shadowRect .getCopy ();
171- int cornerRadius = cornerStartRadius ;
172- for (int i = 0 ; i < size ; i ++) {
173- final double progress = (double ) i / size ;
174- graphics .setAlpha (calcAlphaValue (progress ));
175- graphics .drawRoundRectangle (r , cornerRadius , cornerRadius );
176- cornerRadius += 2 ;
177- r .expand (1 , 1 );
178- }
179- }
100+ protected abstract void paintDropShadow (final Graphics graphics , final Rectangle shadowRect , final int size );
180101
181- /**
182- * Set the corner radius for the drop shadow. The default value is suited for
183- * rectangles for rounded rectangles it should be set to the corner radius of
184- * the rounded rectangle this shadow is bounding.
185- *
186- * @param cornerRadius
187- * @since 3.2
188- */
189- public void setCornerRadius (int cornerRadius ) {
190- this .cornerStartRadius = cornerRadius ;
191- }
102+ protected abstract void paintHalo (final Graphics graphics , final Rectangle shadowRect , final int size );
192103
193104 /**
194105 * Sets the size of the directional shadow that simulates a light source.
@@ -276,4 +187,4 @@ private void updateClip(Graphics graphics, Rectangle shadowRect) {
276187 shadowRect .shrink (shadowSize , shadowSize );
277188 }
278189
279- }
190+ }
0 commit comments