label.go 5.4 KB

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