-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoolroundrect.go
54 lines (50 loc) · 1.15 KB
/
toolroundrect.go
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
package main
import (
"github.com/shahfarhadreza/go-gdiplus"
)
type RoundRectDrawer struct {
ShapeDrawer
}
func NewRoundedRectPath(bound *gdiplus.Rect, radius int32) *gdiplus.GraphicsPath {
path := gdiplus.NewPath(gdiplus.FillModeAlternate)
diameter := radius * 2
arc := gdiplus.NewRect(bound.X, bound.Y, diameter, diameter)
// top left arc
path.AddArcRect(arc, 180, 90)
// top right arc
arc.X = bound.Right() - diameter
path.AddArcRect(arc, 270, 90)
// bottom right arc
arc.Y = bound.Bottom() - diameter
path.AddArcRect(arc, 0, 90)
// bottom left arc
arc.X = bound.X
path.AddArcRect(arc, 90, 90)
path.CloseFigure()
return path
}
func (tool *RoundRectDrawer) draw(args *ToolDrawShapeArgs) {
g := args.context
pen := args.pen
brush := args.brush
rect := args.rect
radius := int32(10)
// oh god this hardcoded fix.....
if rect.Width < 40 || rect.Height < 40 {
radius = 8
}
if rect.Width < 30 || rect.Height < 30 {
radius = 5
}
if rect.Width < 10 || rect.Height < 10 {
radius = 1
}
round := NewRoundedRectPath(&rect, radius)
if brush != nil {
g.FillPath(brush, round)
}
if pen != nil {
g.DrawPath(pen, round)
}
round.Dispose()
}