lib/shoes/swt/gradient.rb in shoes-swt-4.0.0.pre12 vs lib/shoes/swt/gradient.rb in shoes-swt-4.0.0.rc1
- old
+ new
@@ -1,14 +1,28 @@
# frozen_string_literal: true
+
+# Gradients are a little confusing, but don't let all the math below scare you.
+# Here's the (relatively) simple explanation of what's going on.
+#
+# ::SWT::Pattern, the underlying class we use, defines a gradient by a starting
+# and ending point. We account the angle into it by changing these points.
+#
+# To look right, the start/end points must be outside our shape! If not, then
+# we get a hard line where the color resets without fading, which looks bad.
+# Given that, we must keep those points outside our bounds of the element,
+# which are reported via the redraw_left, redraw_top, etc. methods.
+
class Shoes
module Swt
class Gradient
include Common::Remove
def initialize(dsl)
@dsl = dsl
@patterns = []
+ @color1 = nil
+ @color2 = nil
end
def dispose
@color1&.dispose
@color2&.dispose
@@ -40,15 +54,19 @@
gc.set_foreground_pattern pattern
end
private
- def create_pattern(dsl)
- width = dsl.element_width * 0.5
- height = dsl.element_height * 0.5
- angle = normalize_angle(-dsl.angle)
- left, top, width, height = determine_args_based_on_angle(angle, dsl.element_left,
- dsl.element_top, width, height)
+ def create_pattern(gui)
+ dsl = gui.dsl
+ width = dsl.redraw_width * 0.5
+ height = dsl.redraw_height * 0.5
+ angle = normalize_angle(-(dsl.angle || 0))
+ left, top, width, height = determine_args_based_on_angle(angle,
+ dsl.redraw_left,
+ dsl.redraw_top,
+ width,
+ height)
pattern = ::Swt::Pattern.new Shoes.display, left, top, width, height,
color1.real, color2.real
@patterns << pattern
pattern