imagelabel.go 5.4 KB

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