imagelabel.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // ImageLabel style
  26. type ImageLabelStyle struct {
  27. Border RectBounds
  28. Paddings RectBounds
  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(icon string) {
  58. if il.image != nil {
  59. il.Panel.Remove(il.image)
  60. il.image = nil
  61. }
  62. if il.icon == nil {
  63. il.icon = NewLabel(icon, true)
  64. il.icon.SetFontSize(il.label.FontSize() * 1.4)
  65. il.Panel.Add(il.icon)
  66. }
  67. il.icon.SetText(icon)
  68. il.recalc()
  69. }
  70. // SetImageVisible sets the image visibility
  71. func (il *ImageLabel) SetImageVisible(vis bool) {
  72. if il.image == nil {
  73. return
  74. }
  75. il.image.SetVisible(vis)
  76. }
  77. // ImageVisible returns the image visibility
  78. func (il *ImageLabel) ImageVisible() bool {
  79. if il.image == nil {
  80. return false
  81. }
  82. return il.image.Visible()
  83. }
  84. // SetImage sets the image label image
  85. func (il *ImageLabel) SetImage(img *Image) {
  86. if il.icon != nil {
  87. il.Panel.Remove(il.icon)
  88. il.icon = nil
  89. }
  90. if il.image != nil {
  91. il.Panel.Remove(il.image)
  92. }
  93. il.image = img
  94. if img != nil {
  95. il.Panel.Add(il.image)
  96. }
  97. il.recalc()
  98. }
  99. // SetImageFromFile sets the image label image from the specified filename
  100. // If there is currently a selected icon, it is removed
  101. func (il *ImageLabel) SetImageFromFile(imgfile string) error {
  102. img, err := NewImage(imgfile)
  103. if err != nil {
  104. return err
  105. }
  106. il.SetImage(img)
  107. return nil
  108. }
  109. // SetColor sets the color of the label and icon text
  110. func (il *ImageLabel) SetColor(color *math32.Color) {
  111. il.label.SetColor(color)
  112. if il.icon != nil {
  113. il.icon.SetColor(color)
  114. }
  115. }
  116. // SetColor4 sets the color4 of the label and icon
  117. func (il *ImageLabel) SetColor4(color *math32.Color4) {
  118. il.label.SetColor4(color)
  119. if il.icon != nil {
  120. il.icon.SetColor4(color)
  121. }
  122. }
  123. // SetBgColor sets the color of the image label background
  124. // The color alpha is set to 1.0
  125. func (il *ImageLabel) SetBgColor(color *math32.Color) {
  126. il.Panel.SetColor(color)
  127. if il.icon != nil {
  128. il.icon.SetColor(color)
  129. }
  130. il.label.SetBgColor(color)
  131. }
  132. // SetBgColor4 sets the color4 of the image label background
  133. func (il *ImageLabel) SetBgColor4(color *math32.Color4) {
  134. il.Panel.SetColor4(color)
  135. if il.icon != nil {
  136. il.icon.SetColor4(color)
  137. }
  138. il.label.SetBgColor4(color)
  139. }
  140. // SetFontSize sets the size of the image label font size
  141. func (il *ImageLabel) SetFontSize(size float64) {
  142. il.label.SetFontSize(size)
  143. }
  144. // CopyFields copies another image label icon/image and text to this one
  145. func (il *ImageLabel) CopyFields(other *ImageLabel) {
  146. il.label.SetText(other.label.Text())
  147. if other.icon != nil {
  148. il.SetIcon(other.icon.Text())
  149. }
  150. if other.image != nil {
  151. // TODO li.SetImage(other.image.Clone())
  152. }
  153. il.recalc()
  154. }
  155. // applyStyle applies the specified image label style
  156. func (il *ImageLabel) applyStyle(s *ImageLabelStyle) {
  157. il.SetBordersColor4(&s.BorderColor)
  158. il.SetBordersFrom(&s.Border)
  159. il.SetPaddingsFrom(&s.Paddings)
  160. il.SetColor4(&s.BgColor)
  161. if il.icon != nil {
  162. il.icon.SetColor4(&s.FgColor)
  163. }
  164. il.label.SetColor4(&s.FgColor)
  165. }
  166. // recalc recalculates dimensions and positions from inside out
  167. func (il *ImageLabel) recalc() {
  168. // Current width and height the content area
  169. width := il.Panel.ContentWidth()
  170. height := il.Panel.ContentHeight()
  171. // Image or icon width
  172. var imgWidth float32 = 0
  173. var spacing float32
  174. if il.image != nil {
  175. imgWidth = il.image.Width()
  176. spacing = 4
  177. } else if il.icon != nil {
  178. imgWidth = il.icon.Width()
  179. spacing = 4
  180. }
  181. // Sets new content width and height if necessary
  182. minWidth := imgWidth + spacing + il.label.Width()
  183. minHeight := il.label.Height()
  184. resize := false
  185. if width < minWidth {
  186. width = minWidth
  187. resize = true
  188. }
  189. if height < minHeight {
  190. height = minHeight
  191. resize = true
  192. }
  193. if resize {
  194. il.Panel.SetContentSize(width, height)
  195. }
  196. // Centralize horizontally
  197. px := (width - minWidth) / 2
  198. // Set label position
  199. ly := (height - il.label.Height()) / 2
  200. il.label.SetPosition(px+imgWidth+spacing, ly)
  201. // Image/icon position
  202. if il.image != nil {
  203. iy := (height - il.image.height) / 2
  204. il.image.SetPosition(px, iy)
  205. } else if il.icon != nil {
  206. il.icon.SetPosition(px, ly)
  207. }
  208. }