/
context.go
85 lines (73 loc) · 1.61 KB
/
context.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
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
package main
import (
"image"
"gioui.org/f32"
"gioui.org/layout"
"gioui.org/widget/material"
)
type Context struct {
Transform
layout.Context
Theme *material.Theme
Diagram *Diagram
}
func NewContext(gtx layout.Context, th *material.Theme, zoom *Zoom, diagram *Diagram) *Context {
return &Context{
Transform: NewTransform(gtx, zoom),
Context: gtx,
Theme: th,
Diagram: diagram,
}
}
type Transform struct {
Dp int
PxPerUnit int
}
func NewTransform(gtx layout.Context, zoom *Zoom) Transform {
px := gtx.Dp(30)
px = (px / 24) * 24 // make it divisible by 2,3,4,6,12
px = int(float32(px) * zoom.Multiplier())
return Transform{
Dp: gtx.Dp(1),
PxPerUnit: px,
}
}
func (tr *Transform) Px(v Unit) int {
return tr.PxPerUnit * int(v)
}
func (tr *Transform) Pt(v Vector) image.Point {
return image.Point{
X: int(v.X) * tr.PxPerUnit,
Y: int(v.Y) * tr.PxPerUnit,
}
}
func (tr *Transform) FPt(v Vector) f32.Point {
return f32.Point{
X: float32(int(v.X) * tr.PxPerUnit),
Y: float32(int(v.Y) * tr.PxPerUnit),
}
}
func (tr *Transform) Inv(p image.Point) Vector {
return Vector{
X: Unit(p.X / tr.PxPerUnit),
Y: Unit(p.Y / tr.PxPerUnit),
}
}
func (tr *Transform) FInv(p f32.Point) Vector {
return Vector{
X: Unit(int(p.X) / tr.PxPerUnit),
Y: Unit(int(p.Y) / tr.PxPerUnit),
}
}
func (tr *Transform) Bounds(box Box) image.Rectangle {
return image.Rectangle{
Min: image.Point{
X: int(box.Pos.X) * tr.PxPerUnit,
Y: int(box.Pos.Y) * tr.PxPerUnit,
},
Max: image.Point{
X: int(box.Pos.X+box.Size.X) * tr.PxPerUnit,
Y: int(box.Pos.Y+box.Size.Y) * tr.PxPerUnit,
},
}
}