MCPcopy
hub / github.com/g3n/engine / recalc

Method recalc

gui/button.go:208–263  ·  view source on GitHub ↗

recalc recalculates all dimensions and position from inside out

()

Source from the content-addressed store, hash-verified

206
207// recalc recalculates all dimensions and position from inside out
208func (b *Button) recalc() {
209
210 // Current width and height of button content area
211 width := b.Panel.ContentWidth()
212 height := b.Panel.ContentHeight()
213
214 // Image or icon width
215 imgWidth := float32(0)
216 spacing := float32(4)
217 if b.image != nil {
218 imgWidth = b.image.Width()
219 } else if b.icon != nil {
220 imgWidth = b.icon.Width()
221 }
222 if imgWidth == 0 {
223 spacing = 0
224 }
225
226 // If the label is empty and an icon of image was defined ignore the label widthh
227 // to centralize the icon/image in the button
228 labelWidth := spacing + b.Label.Width()
229 if b.Label.Text() == "" && imgWidth > 0 {
230 labelWidth = 0
231 }
232
233 // Sets new content width and height if necessary
234 minWidth := imgWidth + labelWidth
235 minHeight := b.Label.Height()
236 resize := false
237 if width < minWidth {
238 width = minWidth
239 resize = true
240 }
241 if height < minHeight {
242 height = minHeight
243 resize = true
244 }
245 if resize {
246 b.SetContentSize(width, height)
247 }
248
249 // Centralize horizontally
250 px := (width - minWidth) / 2
251
252 // Set label position
253 ly := (height - b.Label.Height()) / 2
254 b.Label.SetPosition(px+imgWidth+spacing, ly)
255
256 // Image/icon position
257 if b.image != nil {
258 iy := (height - b.image.height) / 2
259 b.image.SetPosition(px, iy)
260 } else if b.icon != nil {
261 b.icon.SetPosition(px, ly)
262 }
263}

Callers 3

SetIconMethod · 0.95
SetImageMethod · 0.95
NewButtonFunction · 0.45

Calls 7

ContentWidthMethod · 0.80
ContentHeightMethod · 0.80
SetContentSizeMethod · 0.80
WidthMethod · 0.65
HeightMethod · 0.65
SetPositionMethod · 0.65
TextMethod · 0.45

Tested by

no test coverage detected