label.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/gls"
  7. "github.com/g3n/engine/math32"
  8. "github.com/g3n/engine/text"
  9. "github.com/g3n/engine/texture"
  10. )
  11. // Label is a panel which contains a texture for rendering text
  12. // The content size of the label panel is the exact size of texture
  13. type Label struct {
  14. Panel // Embedded panel
  15. fontSize float64
  16. fontDPI float64
  17. lineSpacing float64
  18. bgColor math32.Color4
  19. fgColor math32.Color4
  20. font *text.Font
  21. tex *texture.Texture2D // Pointer to texture with drawed text
  22. currentText string
  23. }
  24. // NewLabel creates and returns a label panel with the specified text
  25. // drawn using the current default text font.
  26. // If icon is true the text is drawn using the default icon font
  27. func NewLabel(msg string, icon ...bool) *Label {
  28. l := new(Label)
  29. if len(icon) > 0 && icon[0] {
  30. l.initialize(msg, StyleDefault().FontIcon)
  31. } else {
  32. l.initialize(msg, StyleDefault().Font)
  33. }
  34. return l
  35. }
  36. //// NewIconLabel creates and returns a label panel using the specified text
  37. //// drawn using the default icon font.
  38. //func NewIconLabel(msg string) *Label {
  39. //
  40. // l := new(Label)
  41. // l.initialize(msg, StyleDefault().FontIcon)
  42. // return l
  43. //}
  44. // initialize initializes this label and is normally used by other
  45. // gui types which contains a label.
  46. func (l *Label) initialize(msg string, font *text.Font) {
  47. l.font = font
  48. l.Panel.Initialize(0, 0)
  49. l.fontSize = 14
  50. l.fontDPI = 72
  51. l.lineSpacing = 1.0
  52. l.bgColor = math32.Color4{0, 0, 0, 0}
  53. l.fgColor = math32.Black4
  54. l.SetText(msg)
  55. }
  56. // SetText draws the label text using the current font
  57. func (l *Label) SetText(msg string) {
  58. // Do not allow empty labels
  59. str := msg
  60. if len(msg) == 0 {
  61. str = " "
  62. }
  63. // Set font properties
  64. l.font.SetSize(l.fontSize)
  65. l.font.SetDPI(l.fontDPI)
  66. l.font.SetLineSpacing(l.lineSpacing)
  67. l.font.SetBgColor4(&l.bgColor)
  68. l.font.SetFgColor4(&l.fgColor)
  69. // Measure text
  70. width, height := l.font.MeasureText(str)
  71. // Create image canvas with the exact size of the texture
  72. // and draw the text.
  73. canvas := text.NewCanvas(width, height, &l.bgColor)
  74. canvas.DrawText(0, 0, str, l.font)
  75. // Creates texture if if doesnt exist.
  76. if l.tex == nil {
  77. l.tex = texture.NewTexture2DFromRGBA(canvas.RGBA)
  78. l.tex.SetMagFilter(gls.NEAREST)
  79. l.tex.SetMinFilter(gls.NEAREST)
  80. l.Panel.Material().AddTexture(l.tex)
  81. // Otherwise update texture with new image
  82. } else {
  83. l.tex.SetFromRGBA(canvas.RGBA)
  84. }
  85. // Updates label panel dimensions
  86. l.Panel.SetContentSize(float32(width), float32(height))
  87. l.currentText = str
  88. }
  89. // Text returns the current label text
  90. func (l *Label) Text() string {
  91. return l.currentText
  92. }
  93. // SetColor sets the color of the label text
  94. // The color alpha is set to 1.0
  95. func (l *Label) SetColor(color *math32.Color) *Label {
  96. l.fgColor.FromColor(color, 1.0)
  97. l.SetText(l.currentText)
  98. return l
  99. }
  100. // SetColor4 sets the color4 of the label text
  101. func (l *Label) SetColor4(color4 *math32.Color4) *Label {
  102. l.fgColor = *color4
  103. l.SetText(l.currentText)
  104. return l
  105. }
  106. // Color returns the current color of the label text
  107. func (l *Label) Color() math32.Color4 {
  108. return l.fgColor
  109. }
  110. // SetBgColor sets the color of the label background
  111. // The color alpha is set to 1.0
  112. func (l *Label) SetBgColor(color *math32.Color) *Label {
  113. l.bgColor.FromColor(color, 1.0)
  114. l.Panel.SetColor4(&l.bgColor)
  115. l.SetText(l.currentText)
  116. return l
  117. }
  118. // SetBgColor4 sets the color4 of the label background
  119. func (l *Label) SetBgColor4(color *math32.Color4) *Label {
  120. l.bgColor = *color
  121. l.Panel.SetColor4(&l.bgColor)
  122. l.SetText(l.currentText)
  123. return l
  124. }
  125. // BgColor returns the current color the label background
  126. func (l *Label) BgColor() math32.Color4 {
  127. return l.bgColor
  128. }
  129. // SetFont sets this label text or icon font
  130. func (l *Label) SetFont(f *text.Font) {
  131. l.font = f
  132. l.SetText(l.currentText)
  133. }
  134. // SetFontSize sets label font size
  135. func (l *Label) SetFontSize(size float64) *Label {
  136. l.fontSize = size
  137. l.SetText(l.currentText)
  138. return l
  139. }
  140. // FontSize returns the current label font size
  141. func (l *Label) FontSize() float64 {
  142. return l.fontSize
  143. }
  144. // SetFontDPI sets the font dots per inch
  145. func (l *Label) SetFontDPI(dpi float64) *Label {
  146. l.fontDPI = dpi
  147. l.SetText(l.currentText)
  148. return l
  149. }
  150. // SetLineSpacing sets the spacing between lines.
  151. // The default value is 1.0
  152. func (l *Label) SetLineSpacing(spacing float64) *Label {
  153. l.lineSpacing = spacing
  154. l.SetText(l.currentText)
  155. return l
  156. }
  157. // setTextCaret sets the label text and draws a caret at the
  158. // specified line and column.
  159. // It is normally used by the Edit widget.
  160. func (l *Label) setTextCaret(msg string, mx, width, line, col int) {
  161. // Set font properties
  162. l.font.SetSize(l.fontSize)
  163. l.font.SetDPI(l.fontDPI)
  164. l.font.SetLineSpacing(l.lineSpacing)
  165. l.font.SetBgColor4(&l.bgColor)
  166. l.font.SetFgColor4(&l.fgColor)
  167. // Create canvas and draw text
  168. _, height := l.font.MeasureText(msg)
  169. canvas := text.NewCanvas(width, height, &l.bgColor)
  170. canvas.DrawTextCaret(mx, 0, msg, l.font, line, col)
  171. // Creates texture if if doesnt exist.
  172. if l.tex == nil {
  173. l.tex = texture.NewTexture2DFromRGBA(canvas.RGBA)
  174. l.Panel.Material().AddTexture(l.tex)
  175. // Otherwise update texture with new image
  176. } else {
  177. l.tex.SetFromRGBA(canvas.RGBA)
  178. }
  179. // Set texture filtering parameters for text
  180. l.tex.SetMagFilter(gls.NEAREST)
  181. l.tex.SetMinFilter(gls.NEAREST)
  182. // Updates label panel dimensions
  183. l.Panel.SetContentSize(float32(width), float32(height))
  184. l.currentText = msg
  185. }