imagelabel.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // ImageLabel is a panel which can contain an Image or Icon plus a Label side by side.
  19. type ImageLabel struct {
  20. Panel // Embedded panel
  21. label Label // internal label
  22. image *Image // optional internal image
  23. icon *Label // optional internal icon label
  24. }
  25. // ImageLabelStyle contains the styling of an ImageLabel.
  26. type ImageLabelStyle BasicStyle
  27. // NewImageLabel creates and returns a pointer to a new image label widget
  28. // with the specified text for the label and no image/icon
  29. func NewImageLabel(text string) *ImageLabel {
  30. il := new(ImageLabel)
  31. // Initializes the panel
  32. il.Panel.Initialize(il, 0, 0)
  33. il.Panel.Subscribe(OnResize, func(evname string, ev interface{}) { il.recalc() })
  34. // Initializes the label
  35. il.label.initialize(text, StyleDefault().Font)
  36. il.label.Subscribe(OnResize, func(evname string, ev interface{}) { il.recalc() })
  37. il.Panel.Add(&il.label)
  38. il.recalc()
  39. return il
  40. }
  41. // SetText sets the text of the image label
  42. func (il *ImageLabel) SetText(text string) {
  43. il.label.SetText(text)
  44. }
  45. // Text returns the current label text
  46. func (il *ImageLabel) Text() string {
  47. return il.label.Text()
  48. }
  49. // SetIcon sets the image label icon from the default Icon font.
  50. // If there is currently a selected image, it is removed
  51. func (il *ImageLabel) SetIcon(icon string) {
  52. if il.image != nil {
  53. il.Panel.Remove(il.image)
  54. il.image = nil
  55. }
  56. if il.icon == nil {
  57. il.icon = NewIcon(icon)
  58. il.icon.SetFontSize(StyleDefault().Label.PointSize * 1.4)
  59. il.Panel.Add(il.icon)
  60. }
  61. il.icon.SetText(icon)
  62. il.recalc()
  63. }
  64. // SetImageVisible sets the image visibility
  65. func (il *ImageLabel) SetImageVisible(vis bool) {
  66. if il.image == nil {
  67. return
  68. }
  69. il.image.SetVisible(vis)
  70. }
  71. // ImageVisible returns the image visibility
  72. func (il *ImageLabel) ImageVisible() bool {
  73. if il.image == nil {
  74. return false
  75. }
  76. return il.image.Visible()
  77. }
  78. // SetImage sets the image label image
  79. func (il *ImageLabel) SetImage(img *Image) {
  80. if il.icon != nil {
  81. il.Panel.Remove(il.icon)
  82. il.icon = nil
  83. }
  84. if il.image != nil {
  85. il.Panel.Remove(il.image)
  86. }
  87. il.image = img
  88. if img != nil {
  89. il.Panel.Add(il.image)
  90. }
  91. il.recalc()
  92. }
  93. // SetImageFromFile sets the image label image from the specified filename
  94. // If there is currently a selected icon, it is removed
  95. func (il *ImageLabel) SetImageFromFile(imgfile string) error {
  96. img, err := NewImage(imgfile)
  97. if err != nil {
  98. return err
  99. }
  100. il.SetImage(img)
  101. return nil
  102. }
  103. // SetColor sets the color of the label and icon text
  104. func (il *ImageLabel) SetColor(color *math32.Color) {
  105. il.label.SetColor(color)
  106. if il.icon != nil {
  107. il.icon.SetColor(color)
  108. }
  109. }
  110. // SetColor4 sets the color4 of the label and icon
  111. func (il *ImageLabel) SetColor4(color *math32.Color4) {
  112. il.label.SetColor4(color)
  113. if il.icon != nil {
  114. il.icon.SetColor4(color)
  115. }
  116. }
  117. // SetBgColor sets the color of the image label background
  118. // The color alpha is set to 1.0
  119. func (il *ImageLabel) SetBgColor(color *math32.Color) {
  120. il.Panel.SetColor(color)
  121. if il.icon != nil {
  122. il.icon.SetColor(color)
  123. }
  124. il.label.SetBgColor(color)
  125. }
  126. // SetBgColor4 sets the color4 of the image label background
  127. func (il *ImageLabel) SetBgColor4(color *math32.Color4) {
  128. il.Panel.SetColor4(color)
  129. if il.icon != nil {
  130. il.icon.SetColor4(color)
  131. }
  132. il.label.SetBgColor4(color)
  133. }
  134. // SetFontSize sets the size of the image label font size
  135. func (il *ImageLabel) SetFontSize(size float64) {
  136. il.label.SetFontSize(size)
  137. }
  138. // CopyFields copies another image label icon/image and text to this one
  139. func (il *ImageLabel) CopyFields(other *ImageLabel) {
  140. il.label.SetText(other.label.Text())
  141. if other.icon != nil {
  142. il.SetIcon(other.icon.Text())
  143. }
  144. if other.image != nil {
  145. // TODO li.SetImage(other.image.Clone())
  146. }
  147. il.recalc()
  148. }
  149. // applyStyle applies the specified image label style
  150. func (il *ImageLabel) applyStyle(s *ImageLabelStyle) {
  151. il.Panel.ApplyStyle(&s.PanelStyle)
  152. if il.icon != nil {
  153. il.icon.SetColor4(&s.FgColor)
  154. }
  155. il.label.SetColor4(&s.FgColor)
  156. }
  157. // recalc recalculates dimensions and positions from inside out
  158. func (il *ImageLabel) recalc() {
  159. // Current width and height the content area
  160. width := il.Panel.ContentWidth()
  161. height := il.Panel.ContentHeight()
  162. // Image or icon width
  163. var imgWidth float32
  164. var spacing float32
  165. if il.image != nil {
  166. imgWidth = il.image.Width()
  167. spacing = 4
  168. } else if il.icon != nil {
  169. imgWidth = il.icon.Width()
  170. spacing = 4
  171. }
  172. // Sets new content width and height if necessary
  173. minWidth := imgWidth + spacing + il.label.Width()
  174. minHeight := il.label.Height()
  175. resize := false
  176. if width < minWidth {
  177. width = minWidth
  178. resize = true
  179. }
  180. if height < minHeight {
  181. height = minHeight
  182. resize = true
  183. }
  184. if resize {
  185. il.Panel.SetContentSize(width, height)
  186. }
  187. // Centralize horizontally
  188. px := (width - minWidth) / 2
  189. // Set label position
  190. ly := (height - il.label.Height()) / 2
  191. il.label.SetPosition(px+imgWidth+spacing, ly)
  192. // Image/icon position
  193. if il.image != nil {
  194. iy := (height - il.image.height) / 2
  195. il.image.SetPosition(px, iy)
  196. } else if il.icon != nil {
  197. il.icon.SetPosition(px, ly)
  198. }
  199. }