Skip to content

Commit

Permalink
refactoring the "escape route" when painting if area or clipping area…
Browse files Browse the repository at this point in the history
… are empty
  • Loading branch information
davidedc committed Oct 12, 2019
1 parent df96928 commit 905e415
Show file tree
Hide file tree
Showing 17 changed files with 519 additions and 555 deletions.
78 changes: 38 additions & 40 deletions src/DesktopAppearance.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,58 +82,56 @@ class DesktopAppearance extends RectangularAppearance


[area,sl,st,al,at,w,h] = @morph.calculateKeyValues aContext, clippingRectangle
if area.isNotEmpty()
if w < 1 or h < 1
return nil
return nil if w < 1 or h < 1 or area.isEmpty()

@morph.justBeforeBeingPainted?()
@morph.justBeforeBeingPainted?()

aContext.save()
aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @morph.alpha
if !@morph.color? then debugger
aContext.fillStyle = @morph.color.toString()
aContext.save()
aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @morph.alpha
if !@morph.color? then debugger
aContext.fillStyle = @morph.color.toString()

# paintRectangle is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio
# paintRectangle is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio

# paint the background
toBePainted = new Rectangle al, at, al + w, at + h
# paint the background
toBePainted = new Rectangle al, at, al + w, at + h

if @morph.backgroundColor?
color = @morph.backgroundColor
if appliedShadow?
color = "black"
@morph.paintRectangle aContext, toBePainted.left(), toBePainted.top(), toBePainted.width(), toBePainted.height(), color
if @morph.backgroundColor?
color = @morph.backgroundColor
if appliedShadow?
color = "black"
@morph.paintRectangle aContext, toBePainted.left(), toBePainted.top(), toBePainted.width(), toBePainted.height(), color


# now paint the actual morph, which is a rectangle
# (potentially inset because of the padding)
toBePainted = toBePainted.intersect @morph.boundingBoxTight().scaleBy ceilPixelRatio
# now paint the actual morph, which is a rectangle
# (potentially inset because of the padding)
toBePainted = toBePainted.intersect @morph.boundingBoxTight().scaleBy ceilPixelRatio

color = @morph.color
if appliedShadow?
color = "black"
color = @morph.color
if appliedShadow?
color = "black"

@morph.paintRectangle aContext, toBePainted.left(), toBePainted.top(), toBePainted.width(), toBePainted.height(), color
@morph.paintRectangle aContext, toBePainted.left(), toBePainted.top(), toBePainted.width(), toBePainted.height(), color

@drawAdditionalPartsOnBaseShape? false, false, appliedShadow, aContext, al, at, w, h
@drawAdditionalPartsOnBaseShape? false, false, appliedShadow, aContext, al, at, w, h

if !appliedShadow?
@paintStroke aContext, clippingRectangle
if !appliedShadow?
@paintStroke aContext, clippingRectangle

if @pattern?
aContext.fillStyle = @pattern
aContext.fillRect toBePainted.left(), toBePainted.top(), toBePainted.width(), toBePainted.height()
if @pattern?
aContext.fillStyle = @pattern
aContext.fillRect toBePainted.left(), toBePainted.top(), toBePainted.width(), toBePainted.height()

aContext.restore()
aContext.restore()

# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio
@paintHighlight aContext, al, at, w, h
# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio
@paintHighlight aContext, al, at, w, h

42 changes: 20 additions & 22 deletions src/HandleMorph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,33 @@ class HandleMorph extends Widget
return

[area,sl,st,al,at,w,h] = @calculateKeyValues aContext, clippingRectangle
if area.isNotEmpty()
if w < 1 or h < 1
return nil
return nil if w < 1 or h < 1 or area.isEmpty()

aContext.save()
aContext.save()

# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h
# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h

aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @alpha
aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @alpha

aContext.scale ceilPixelRatio, ceilPixelRatio
morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y
aContext.scale ceilPixelRatio, ceilPixelRatio
morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y

if @state == @STATE_NORMAL
@handleMorphRenderingHelper aContext, @color, new Color 150, 150, 150
if @state == @STATE_HIGHLIGHTED
@handleMorphRenderingHelper aContext, new Color(255, 255, 255), new Color(200, 200, 255)
if @state == @STATE_NORMAL
@handleMorphRenderingHelper aContext, @color, new Color 150, 150, 150
if @state == @STATE_HIGHLIGHTED
@handleMorphRenderingHelper aContext, new Color(255, 255, 255), new Color(200, 200, 255)

aContext.restore()
aContext.restore()

# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio (i.e. after the restore)
@paintHighlight aContext, al, at, w, h
# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio (i.e. after the restore)
@paintHighlight aContext, al, at, w, h

drawArrow: (context, leftArrowPoint, rightArrowPoint, arrowPieceLeftUp, arrowPieceLeftDown, arrowPieceRightUp, arrowPieceRightDown) ->
context.beginPath()
Expand Down
46 changes: 22 additions & 24 deletions src/LayoutElementAdderOrDropletMorph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,36 @@ class LayoutElementAdderOrDropletMorph extends Widget
return

[area,sl,st,al,at,w,h] = @calculateKeyValues aContext, clippingRectangle
if area.isNotEmpty()
if w < 1 or h < 1
return nil
return nil if w < 1 or h < 1 or area.isEmpty()

aContext.save()
aContext.save()

# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h
# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h

aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @alpha
aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @alpha

# paintRectangle here is made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, this is why
# it's called before the scaling.
@paintRectangle aContext, al, at, w, h, @color
aContext.scale ceilPixelRatio, ceilPixelRatio
# paintRectangle here is made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, this is why
# it's called before the scaling.
@paintRectangle aContext, al, at, w, h, @color
aContext.scale ceilPixelRatio, ceilPixelRatio

morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y
morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y

@spacerMorphRenderingHelper aContext, new Color(255, 255, 255), new Color(200, 200, 255)
@spacerMorphRenderingHelper aContext, new Color(255, 255, 255), new Color(200, 200, 255)

aContext.restore()
aContext.restore()

# paintHighlight here is made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, this is why
# it's called outside the effect of the scaling
# (after the restore).
@paintHighlight aContext, al, at, w, h
# paintHighlight here is made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, this is why
# it's called outside the effect of the scaling
# (after the restore).
@paintHighlight aContext, al, at, w, h

drawHandle: (context) ->
height = @height()
Expand Down
46 changes: 22 additions & 24 deletions src/LayoutSpacerMorph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,37 @@ class LayoutSpacerMorph extends Widget
return

[area,sl,st,al,at,w,h] = @calculateKeyValues aContext, clippingRectangle
if area.isNotEmpty()
if w < 1 or h < 1
return nil
return nil if w < 1 or h < 1 or area.isEmpty()

aContext.save()
aContext.save()

# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h
# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h

aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @alpha
aContext.globalAlpha = (if appliedShadow? then appliedShadow.alpha else 1) * @alpha

# paintRectangle here is made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, this is why
# it's called before the scaling.
@paintRectangle aContext, al, at, w, h, @color
aContext.scale ceilPixelRatio, ceilPixelRatio
# paintRectangle here is made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, this is why
# it's called before the scaling.
@paintRectangle aContext, al, at, w, h, @color
aContext.scale ceilPixelRatio, ceilPixelRatio


morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y
morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y

@spacerMorphRenderingHelper aContext, new Color(255, 255, 255), new Color(200, 200, 255)
@spacerMorphRenderingHelper aContext, new Color(255, 255, 255), new Color(200, 200, 255)

aContext.restore()
aContext.restore()

# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio (i.e. after the restore)
@paintHighlight aContext, al, at, w, h
# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio (i.e. after the restore)
@paintHighlight aContext, al, at, w, h

doPath: (context, leftArrowPoint, rightArrowPoint, arrowPieceLeftUp, arrowPieceLeftDown, arrowPieceRightUp, arrowPieceRightDown) ->
context.beginPath()
Expand Down
106 changes: 52 additions & 54 deletions src/PenMorph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,60 +52,58 @@ class PenMorph extends Widget
return

[area,sl,st,al,at,w,h] = @calculateKeyValues aContext, clippingRectangle
if area.isNotEmpty()
if w < 1 or h < 1
return nil

aContext.save()

# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h

aContext.globalAlpha = @alpha

aContext.scale ceilPixelRatio, ceilPixelRatio
morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y

direction = @heading
len = @width() / 2
start = @center().subtract(@position())

if @penPoint is "tip"
dest = start.distanceAngle(len * 0.75, direction - 180)
left = start.distanceAngle(len, direction + 195)
right = start.distanceAngle(len, direction - 195)
else # 'middle'
dest = start.distanceAngle(len * 0.75, direction)
left = start.distanceAngle(len * 0.33, direction + 230)
right = start.distanceAngle(len * 0.33, direction - 230)

aContext.fillStyle = @color.toString()
aContext.beginPath()

aContext.moveTo start.x, start.y
aContext.lineTo left.x, left.y
aContext.lineTo dest.x, dest.y
aContext.lineTo right.x, right.y

aContext.closePath()
aContext.strokeStyle = "white"
aContext.lineWidth = 3
aContext.stroke()
aContext.strokeStyle = "black"
aContext.lineWidth = 1
aContext.stroke()
aContext.fill()

aContext.restore()

# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio (i.e. after the restore)
@paintHighlight aContext, al, at, w, h
return nil if w < 1 or h < 1 or area.isEmpty()

aContext.save()

# clip out the dirty rectangle as we are
# going to paint the whole of the box
aContext.clipToRectangle al,at,w,h

aContext.globalAlpha = @alpha

aContext.scale ceilPixelRatio, ceilPixelRatio
morphPosition = @position()
aContext.translate morphPosition.x, morphPosition.y

direction = @heading
len = @width() / 2
start = @center().subtract(@position())

if @penPoint is "tip"
dest = start.distanceAngle(len * 0.75, direction - 180)
left = start.distanceAngle(len, direction + 195)
right = start.distanceAngle(len, direction - 195)
else # 'middle'
dest = start.distanceAngle(len * 0.75, direction)
left = start.distanceAngle(len * 0.33, direction + 230)
right = start.distanceAngle(len * 0.33, direction - 230)

aContext.fillStyle = @color.toString()
aContext.beginPath()

aContext.moveTo start.x, start.y
aContext.lineTo left.x, left.y
aContext.lineTo dest.x, dest.y
aContext.lineTo right.x, right.y

aContext.closePath()
aContext.strokeStyle = "white"
aContext.lineWidth = 3
aContext.stroke()
aContext.strokeStyle = "black"
aContext.lineWidth = 1
aContext.stroke()
aContext.fill()

aContext.restore()

# paintHighlight is usually made to work with
# al, at, w, h which are actual pixels
# rather than logical pixels, so it's generally used
# outside the effect of the scaling because
# of the ceilPixelRatio (i.e. after the restore)
@paintHighlight aContext, al, at, w, h



Expand Down
Loading

0 comments on commit 905e415

Please sign in to comment.