Skip to content

Commit 24cf1d3

Browse files
committed
Make Element.Area editable
So the user can update area anytime.
1 parent 2d76d07 commit 24cf1d3

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

_examples/gui/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ func main() {
6161
func attachPanel(parent *pigui.Element, x, y, w, h int) *pigui.Element {
6262
panel := pigui.Attach(parent, x, y, w, h)
6363
panel.OnDraw = func(event pigui.DrawEvent) {
64-
area := panel.Area()
6564
pi.SetColor(lightBlue)
66-
pi.Rect(0, 0, area.W-1, area.H-1)
65+
pi.Rect(0, 0, panel.W-1, panel.H-1)
6766
pi.SetColor(darkBlue)
68-
pi.RectFill(1, 1, area.W-2, area.H-2)
67+
pi.RectFill(1, 1, panel.W-2, panel.H-2)
6968
}
7069
return panel
7170
}

pigui/pigui.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"github.com/elgopher/pi/pimouse"
1212
)
1313

14-
// New creates a new GUI root element.
14+
// New creates a new GUI root element with the current screen size.
1515
//
1616
// To update and draw the element along with its children,
1717
// add it to your game loop by calling Element.Update and Element.Draw.
1818
func New() *Element {
1919
return &Element{
20-
area: pi.IntArea{
21-
W: pi.Screen().W(), // TODO Should not be fixed
20+
Area: pi.IntArea{
21+
W: pi.Screen().W(),
2222
H: pi.Screen().H(),
2323
},
2424
}
@@ -29,19 +29,20 @@ func New() *Element {
2929
// It returns the newly created element.
3030
func Attach(parent *Element, x, y, w, h int) *Element {
3131
ch := &Element{
32-
area: pi.IntArea{X: x, Y: y, W: w, H: h},
32+
Area: pi.IntArea{X: x, Y: y, W: w, H: h},
3333
}
3434
parent.Attach(ch)
3535
return ch
3636
}
3737

3838
type Element struct {
39+
pi.Area[int]
40+
3941
OnDraw func(DrawEvent)
4042
OnUpdate func(UpdateEvent)
4143
OnPressed func(Event)
4244
OnReleased func(Event)
4345
OnTapped func(Event)
44-
area pi.Area[int]
4546
children []*Element
4647
pressed bool
4748
}
@@ -66,13 +67,13 @@ func (e *Element) Update() {
6667
pi.Camera = prevCamera
6768
}()
6869

69-
pi.Camera.X -= e.area.X // musze przesuwac kamere, zeby dzieci dzieciow zbieraly eventy
70-
pi.Camera.Y -= e.area.Y
70+
pi.Camera.X -= e.X // I have to move the camera so that the children's children can pick up events
71+
pi.Camera.Y -= e.Y
7172

7273
mousePosition := pimouse.Position.Add(pi.Camera)
7374

74-
hasPointer := mousePosition.X >= 0 && mousePosition.X < e.area.W &&
75-
mousePosition.Y >= 0 && mousePosition.Y < e.area.H
75+
hasPointer := mousePosition.X >= 0 && mousePosition.X < e.W &&
76+
mousePosition.Y >= 0 && mousePosition.Y < e.H
7677

7778
propagate := getPropagateToChildrenFromThePool()
7879

@@ -132,12 +133,12 @@ func (e *Element) Draw() {
132133
pi.Camera = prevCamera
133134
}()
134135

135-
pi.Camera.X -= e.area.X
136-
pi.Camera.Y -= e.area.Y
136+
pi.Camera.X -= e.X
137+
pi.Camera.Y -= e.Y
137138

138139
prevClip := pi.SetClip(pi.IntArea{
139140
X: -pi.Camera.X, Y: -pi.Camera.Y,
140-
W: e.area.W, H: e.area.H,
141+
W: e.W, H: e.H,
141142
})
142143
defer func() {
143144
pi.SetClip(prevClip)
@@ -146,8 +147,8 @@ func (e *Element) Draw() {
146147
propagate := getPropagateToChildrenFromThePool()
147148

148149
mousePosition := pimouse.Position.Add(pi.Camera)
149-
hasPointer := mousePosition.X >= 0 && mousePosition.X < e.area.W &&
150-
mousePosition.Y >= 0 && mousePosition.Y < e.area.H
150+
hasPointer := mousePosition.X >= 0 && mousePosition.X < e.W &&
151+
mousePosition.Y >= 0 && mousePosition.Y < e.H
151152

152153
drawEvent := DrawEvent{
153154
Element: e,
@@ -169,6 +170,3 @@ func (e *Element) Draw() {
169170
}
170171
}
171172
}
172-
func (e *Element) Area() pi.IntArea {
173-
return e.area
174-
}

0 commit comments

Comments
 (0)