imagelabel.go 5.7 KB

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