Pārlūkot izejas kodu

centralize icon/image in button when button text is empty.

leonsal 8 gadi atpakaļ
vecāks
revīzija
b9d0f651c8
2 mainītis faili ar 15 papildinājumiem un 9 dzēšanām
  1. 9 2
      gui/button.go
  2. 6 7
      gui/label.go

+ 9 - 2
gui/button.go

@@ -224,9 +224,16 @@ func (b *Button) recalc() {
 		imgWidth = b.icon.Width()
 	}
 
-	// Sets new content width and height if necessary
+	// If the label is empty and an icon of image was defined ignore the label widthh
+	// to centralize the icon/image in the button
 	spacing := float32(4)
-	minWidth := imgWidth + spacing + b.Label.Width()
+	labelWidth := spacing + b.Label.Width()
+	if b.Label.Text() == "" && imgWidth > 0 {
+		labelWidth = 0
+	}
+
+	// Sets new content width and height if necessary
+	minWidth := imgWidth + labelWidth
 	minHeight := b.Label.Height()
 	resize := false
 	if width < minWidth {

+ 6 - 7
gui/label.go

@@ -56,10 +56,10 @@ func (l *Label) initialize(msg string, font *text.Font) {
 // SetText draws the label text using the current font
 func (l *Label) SetText(msg string) {
 
-	// Do not allow empty labels
-	str := msg
-	if len(msg) == 0 {
-		str = " "
+	// Need at least a character to get dimensions
+	l.currentText = msg
+	if msg == "" {
+		msg = " "
 	}
 
 	// Set font properties
@@ -70,11 +70,11 @@ func (l *Label) SetText(msg string) {
 	l.font.SetFgColor4(&l.fgColor)
 
 	// Measure text
-	width, height := l.font.MeasureText(str)
+	width, height := l.font.MeasureText(msg)
 	// Create image canvas with the exact size of the texture
 	// and draw the text.
 	canvas := text.NewCanvas(width, height, &l.bgColor)
-	canvas.DrawText(0, 0, str, l.font)
+	canvas.DrawText(0, 0, msg, l.font)
 
 	// Creates texture if if doesnt exist.
 	if l.tex == nil {
@@ -89,7 +89,6 @@ func (l *Label) SetText(msg string) {
 
 	// Updates label panel dimensions
 	l.Panel.SetContentSize(float32(width), float32(height))
-	l.currentText = str
 }
 
 // Text returns the current label text