imagelabel.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/math32"
  7. )
  8. /***************************************
  9. ImageLabel
  10. +--------------------------------+
  11. | Image or Icon Label |
  12. | +-----------+ +----------+ |
  13. | | | | | |
  14. | | | | | |
  15. | +-----------+ +----------+ |
  16. +--------------------------------+
  17. ****************************************/
  18. type ImageLabel struct {
  19. Panel // Embedded panel
  20. label Label // internal label
  21. image *Image // optional internal image
  22. icon *Label // optional internal icon label
  23. icode int // icon code (if icon is set)
  24. }
  25. // ImageLabel style
  26. type ImageLabelStyle struct {
  27. Border BorderSizes
  28. Paddings BorderSizes
  29. BorderColor math32.Color4
  30. BgColor math32.Color4
  31. FgColor math32.Color4
  32. }
  33. // NewImageLabel creates and returns a pointer to a new image label widget
  34. // with the specified text for the label and no image/icon
  35. func NewImageLabel(text string) *ImageLabel {
  36. il := new(ImageLabel)
  37. // Initializes the panel
  38. il.Panel.Initialize(0, 0)
  39. il.Panel.Subscribe(OnResize, func(evname string, ev interface{}) { il.recalc() })
  40. // Initializes the label
  41. il.label.initialize(text, StyleDefault.Font)
  42. il.label.Subscribe(OnResize, func(evname string, ev interface{}) { il.recalc() })
  43. il.Panel.Add(&il.label)
  44. il.recalc()
  45. return il
  46. }
  47. // SetText sets the text of the image label
  48. func (il *ImageLabel) SetText(text string) {
  49. il.label.SetText(text)
  50. }
  51. // Text returns the current label text
  52. func (il *ImageLabel) Text() string {
  53. return il.label.Text()
  54. }
  55. // SetIcon sets the image label icon from the default Icon font.
  56. // If there is currently a selected image, it is removed
  57. func (il *ImageLabel) SetIcon(icode int) {
  58. if il.image != nil {
  59. il.Panel.Remove(il.image)
  60. il.image = nil
  61. }
  62. if il.icon == nil {
  63. il.icon = NewIconLabel(string(icode))
  64. il.icon.SetFontSize(il.label.FontSize() * 1.4)
  65. il.Panel.Add(il.icon)
  66. }
  67. il.icon.SetText(string(icode))
  68. il.icode = icode
  69. il.recalc()
  70. }
  71. // SetImage sets the image label image
  72. func (il *ImageLabel) SetImage(img *Image) {
  73. if il.icon != nil {
  74. il.Panel.Remove(il.icon)
  75. il.icon = nil
  76. }
  77. if il.image != nil {
  78. il.Panel.Remove(il.image)
  79. }
  80. il.image = img
  81. il.Panel.Add(il.image)
  82. il.recalc()
  83. }
  84. // SetImageFromFile sets the image label image from the specified filename
  85. // If there is currently a selected icon, it is removed
  86. func (il *ImageLabel) SetImageFromFile(imgfile string) error {
  87. img, err := NewImage(imgfile)
  88. if err != nil {
  89. return err
  90. }
  91. il.SetImage(img)
  92. return nil
  93. }
  94. // SetColor sets the color of the label and icon text
  95. func (il *ImageLabel) SetColor(color *math32.Color) {
  96. il.label.SetColor(color)
  97. if il.icon != nil {
  98. il.icon.SetColor(color)
  99. }
  100. }
  101. // SetColor4 sets the color4 of the label and icon
  102. func (il *ImageLabel) SetColor4(color *math32.Color4) {
  103. il.label.SetColor4(color)
  104. if il.icon != nil {
  105. il.icon.SetColor4(color)
  106. }
  107. }
  108. // SetBgColor sets the color of the image label background
  109. // The color alpha is set to 1.0
  110. func (il *ImageLabel) SetBgColor(color *math32.Color) {
  111. il.Panel.SetColor(color)
  112. if il.icon != nil {
  113. il.icon.SetColor(color)
  114. }
  115. il.label.SetBgColor(color)
  116. }
  117. // SetBgColor4 sets the color4 of the image label background
  118. func (il *ImageLabel) SetBgColor4(color *math32.Color4) {
  119. il.Panel.SetColor4(color)
  120. if il.icon != nil {
  121. il.icon.SetColor4(color)
  122. }
  123. il.label.SetBgColor4(color)
  124. }
  125. // SetFontSize sets the size of the image label font size
  126. func (il *ImageLabel) SetFontSize(size float64) {
  127. il.label.SetFontSize(size)
  128. }
  129. // CopyFields copies another image label icon/image and text to this one
  130. func (il *ImageLabel) CopyFields(other *ImageLabel) {
  131. il.label.SetText(other.label.Text())
  132. if other.icon != nil {
  133. il.SetIcon(other.icode)
  134. }
  135. if other.image != nil {
  136. // TODO li.SetImage(other.image.Clone())
  137. }
  138. il.recalc()
  139. }
  140. // applyStyle applies the specified image label style
  141. func (il *ImageLabel) applyStyle(s *ImageLabelStyle) {
  142. il.SetBordersColor4(&s.BorderColor)
  143. il.SetBordersFrom(&s.Border)
  144. il.SetPaddingsFrom(&s.Paddings)
  145. il.SetColor4(&s.BgColor)
  146. if il.icon != nil {
  147. il.icon.SetColor4(&s.FgColor)
  148. }
  149. il.label.SetColor4(&s.FgColor)
  150. }
  151. // recalc recalculates dimensions and positions from inside out
  152. func (il *ImageLabel) recalc() {
  153. // Current width and height the content area
  154. width := il.Panel.ContentWidth()
  155. height := il.Panel.ContentHeight()
  156. // Image or icon width
  157. var imgWidth float32 = 0
  158. var spacing float32
  159. if il.image != nil {
  160. imgWidth = il.image.Width()
  161. spacing = 4
  162. } else if il.icon != nil {
  163. imgWidth = il.icon.Width()
  164. spacing = 4
  165. }
  166. // Sets new content width and height if necessary
  167. minWidth := imgWidth + spacing + il.label.Width()
  168. minHeight := il.label.Height()
  169. resize := false
  170. if width < minWidth {
  171. width = minWidth
  172. resize = true
  173. }
  174. if height < minHeight {
  175. height = minHeight
  176. resize = true
  177. }
  178. if resize {
  179. il.Panel.SetContentSize(width, height)
  180. }
  181. // Centralize horizontally
  182. px := (width - minWidth) / 2
  183. // Set label position
  184. ly := (height - il.label.Height()) / 2
  185. il.label.SetPosition(px+imgWidth+spacing, ly)
  186. // Image/icon position
  187. if il.image != nil {
  188. iy := (height - il.image.height) / 2
  189. il.image.SetPosition(px, iy)
  190. } else if il.icon != nil {
  191. il.icon.SetPosition(px, ly)
  192. }
  193. }