label.go 5.3 KB

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