Quellcode durchsuchen

gui.NewIconLabel removed, use gui.NewLabel(icon, true) instead

leonsal vor 8 Jahren
Ursprung
Commit
3a5706f52a
9 geänderte Dateien mit 73 neuen und 35 gelöschten Zeilen
  1. 36 11
      gui/builder.go
  2. 1 1
      gui/button.go
  3. 1 1
      gui/checkradio.go
  4. 1 1
      gui/dropdown.go
  5. 1 1
      gui/image_button.go
  6. 5 6
      gui/imagelabel.go
  7. 25 11
      gui/label.go
  8. 2 2
      gui/menu.go
  9. 1 1
      gui/table.go

+ 36 - 11
gui/builder.go

@@ -62,6 +62,7 @@ type panelDesc struct {
 	Layout       layoutAttr
 	Styles       *panelStyles
 	Text         string   // Label, Button
+	Icons        string   // Label
 	BgColor      string   // Label
 	FontColor    string   // Label
 	FontSize     *float32 // Label
@@ -81,7 +82,7 @@ const (
 	descTypePanel       = "Panel"
 	descTypeImagePanel  = "ImagePanel"
 	descTypeLabel       = "Label"
-	descTypeIconLabel   = "IconLabel"
+	descTypeImageLabel  = "ImageLabel"
 	descTypeButton      = "Button"
 	descTypeCheckBox    = "CheckBox"
 	descTypeRadioButton = "RadioButton"
@@ -202,8 +203,8 @@ func (b *Builder) build(pd *panelDesc, iparent IPanel) (IPanel, error) {
 		pan, err = b.buildImagePanel(pd)
 	case descTypeLabel:
 		pan, err = b.buildLabel(pd)
-	case descTypeIconLabel:
-		pan, err = b.buildLabel(pd)
+	case descTypeImageLabel:
+		pan, err = b.buildImageLabel(pd)
 	case descTypeButton:
 		pan, err = b.buildButton(pd)
 	case descTypeCheckBox:
@@ -299,17 +300,19 @@ func (b *Builder) buildImagePanel(pd *panelDesc) (IPanel, error) {
 // buildLabel builds a gui object of type: "Label"
 func (b *Builder) buildLabel(pd *panelDesc) (IPanel, error) {
 
+	// Builds label with icon or text font
 	var label *Label
-	if pd.Type == descTypeLabel {
-		label = NewLabel(pd.Text)
+	icons, err := b.parseIconNames("icons", pd.Icons)
+	if err != nil {
+		return nil, err
+	}
+	if icons != "" {
+		label = NewLabel(icons, true)
 	} else {
-		icons, err := b.parseIconNames("text", pd.Text)
-		if err != nil {
-			return nil, err
-		}
-		label = NewIconLabel(icons)
+		label = NewLabel(pd.Text)
 	}
-	err := b.setCommon(pd, label)
+	// Sets common attributes
+	err = b.setCommon(pd, label)
 	if err != nil {
 		return nil, err
 	}
@@ -350,6 +353,28 @@ func (b *Builder) buildLabel(pd *panelDesc) (IPanel, error) {
 	return label, nil
 }
 
+// buildImageLabel builds a gui object of type: ImageLabel
+func (b *Builder) buildImageLabel(pd *panelDesc) (IPanel, error) {
+
+	// Builds image label and set common attributes
+	imglabel := NewImageLabel(pd.Text)
+	err := b.setCommon(pd, imglabel)
+	if err != nil {
+		return nil, err
+	}
+
+	// Sets optional icon(s)
+	icons, err := b.parseIconNames("icons", pd.Icons)
+	if err != nil {
+		return nil, err
+	}
+	if icons != "" {
+		imglabel.SetIcon(icons)
+	}
+
+	return imglabel, nil
+}
+
 // buildButton builds a gui object of type: Button
 func (b *Builder) buildButton(pd *panelDesc) (IPanel, error) {
 

+ 1 - 1
gui/button.go

@@ -85,7 +85,7 @@ func NewButton(text string) *Button {
 // If there is currently a selected image, it is removed
 func (b *Button) SetIcon(icode int) {
 
-	ico := NewIconLabel(string(icode))
+	ico := NewLabel(string(icode), true)
 	if b.image != nil {
 		b.Panel.Remove(b.image)
 		b.image = nil

+ 1 - 1
gui/checkradio.go

@@ -94,7 +94,7 @@ func newCheckRadio(check bool, text string) *CheckRadio {
 	cb.Panel.Add(cb.Label)
 
 	// Creates icon label
-	cb.icon = NewIconLabel(" ")
+	cb.icon = NewLabel(" ", true)
 	cb.Panel.Add(cb.icon)
 
 	cb.recalc()

+ 1 - 1
gui/dropdown.go

@@ -58,7 +58,7 @@ func NewDropDown(width float32, item *ImageLabel) *DropDown {
 	dd.Panel.Add(dd.litem)
 
 	// Create icon
-	dd.icon = NewIconLabel(" ")
+	dd.icon = NewLabel(" ", true)
 	dd.icon.SetFontSize(StyleDefault().Font.Size() * 1.3)
 	dd.icon.SetText(string(icon.ArrowDropDown))
 	dd.Panel.Add(dd.icon)

+ 1 - 1
gui/image_button.go

@@ -117,7 +117,7 @@ func (b *ImageButton) SetIcon(icode int) {
 	b.iconLabel = true
 	if b.label == nil {
 		// Create icon
-		b.label = NewIconLabel(string(icode))
+		b.label = NewLabel(string(icode), true)
 		b.Panel.Add(b.label)
 	} else {
 		b.label.SetText(string(icode))

+ 5 - 6
gui/imagelabel.go

@@ -21,12 +21,12 @@ import (
 
 ****************************************/
 
+// ImageLabel is a panel which can contain an Image or Icon plus a Label side by side.
 type ImageLabel struct {
 	Panel        // Embedded panel
 	label Label  // internal label
 	image *Image // optional internal image
 	icon  *Label // optional internal icon label
-	icode int    // icon code (if icon is set)
 }
 
 // ImageLabel style
@@ -71,19 +71,18 @@ func (il *ImageLabel) Text() string {
 
 // SetIcon sets the image label icon from the default Icon font.
 // If there is currently a selected image, it is removed
-func (il *ImageLabel) SetIcon(icode int) {
+func (il *ImageLabel) SetIcon(icon string) {
 
 	if il.image != nil {
 		il.Panel.Remove(il.image)
 		il.image = nil
 	}
 	if il.icon == nil {
-		il.icon = NewIconLabel(string(icode))
+		il.icon = NewLabel(icon, true)
 		il.icon.SetFontSize(il.label.FontSize() * 1.4)
 		il.Panel.Add(il.icon)
 	}
-	il.icon.SetText(string(icode))
-	il.icode = icode
+	il.icon.SetText(icon)
 	il.recalc()
 }
 
@@ -184,7 +183,7 @@ func (il *ImageLabel) CopyFields(other *ImageLabel) {
 
 	il.label.SetText(other.label.Text())
 	if other.icon != nil {
-		il.SetIcon(other.icode)
+		il.SetIcon(other.icon.Text())
 	}
 	if other.image != nil {
 		// TODO li.SetImage(other.image.Clone())

+ 25 - 11
gui/label.go

@@ -11,6 +11,8 @@ import (
 	"github.com/g3n/engine/texture"
 )
 
+// Label is a panel which contains a texture for rendering text
+// The content size of the label panel is the exact size of texture
 type Label struct {
 	Panel       // Embedded panel
 	fontSize    float64
@@ -24,22 +26,27 @@ type Label struct {
 }
 
 // NewLabel creates and returns a label panel with the specified text
-// drawn using the current default font.
-func NewLabel(msg string) *Label {
+// drawn using the current default text font.
+// If icon is true the text is drawn using the default icon font
+func NewLabel(msg string, icon ...bool) *Label {
 
 	l := new(Label)
-	l.initialize(msg, StyleDefault().Font)
+	if len(icon) > 0 && icon[0] {
+		l.initialize(msg, StyleDefault().FontIcon)
+	} else {
+		l.initialize(msg, StyleDefault().Font)
+	}
 	return l
 }
 
-// NewIconLabel creates and returns a label panel using the specified text
-// drawn using the default icon font.
-func NewIconLabel(msg string) *Label {
-
-	l := new(Label)
-	l.initialize(msg, StyleDefault().FontIcon)
-	return l
-}
+//// NewIconLabel creates and returns a label panel using the specified text
+//// drawn using the default icon font.
+//func NewIconLabel(msg string) *Label {
+//
+//	l := new(Label)
+//	l.initialize(msg, StyleDefault().FontIcon)
+//	return l
+//}
 
 // initialize initializes this label and is normally used by other
 // gui types which contains a label.
@@ -148,6 +155,13 @@ func (l *Label) BgColor() math32.Color4 {
 	return l.bgColor
 }
 
+// SetFont sets this label text or icon font
+func (l *Label) SetFont(f *text.Font) {
+
+	l.font = f
+	l.SetText(l.currentText)
+}
+
 // SetFontSize sets label font size
 func (l *Label) SetFontSize(size float64) *Label {
 

+ 2 - 2
gui/menu.go

@@ -206,7 +206,7 @@ func (m *Menu) AddMenu(text string, subm *Menu) *MenuItem {
 	mi.submenu.autoOpen = true
 	mi.menu = m
 	if !m.bar {
-		mi.ricon = NewIconLabel(string(icon.PlayArrow))
+		mi.ricon = NewLabel(string(icon.PlayArrow), true)
 		mi.Panel.Add(mi.ricon)
 	}
 	mi.Panel.Add(mi.submenu)
@@ -605,7 +605,7 @@ func (mi *MenuItem) SetIcon(icode int) *MenuItem {
 		mi.licon = nil
 	}
 	// Sets the new icon
-	mi.licon = NewIconLabel(string(icode))
+	mi.licon = NewLabel(string(icode), true)
 	mi.Panel.Add(mi.licon)
 	mi.update()
 	return mi

+ 1 - 1
gui/table.go

@@ -259,7 +259,7 @@ func NewTable(width, height float32, cols []TableColumn) (*Table, error) {
 		c.resize = cdesc.Resize
 		// Adds optional sort icon
 		if c.sort != TableSortNone {
-			c.ricon = NewIconLabel(string(tableSortedNoneIcon))
+			c.ricon = NewLabel(string(tableSortedNoneIcon), true)
 			c.Add(c.ricon)
 			c.ricon.Subscribe(OnMouseDown, func(evname string, ev interface{}) {
 				t.onRicon(evname, c)