label.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. type Label struct {
  12. Panel // Embedded panel
  13. fontSize float64
  14. fontDPI float64
  15. lineSpacing float64
  16. bgColor math32.Color4
  17. fgColor math32.Color4
  18. font *text.Font
  19. tex *texture.Texture2D // Pointer to texture with drawed text
  20. currentText string
  21. }
  22. // NewLabel creates and returns a label panel with the specified text
  23. // drawn using the current default font.
  24. func NewLabel(msg string) *Label {
  25. l := new(Label)
  26. l.initialize(msg, StyleDefault().Font)
  27. return l
  28. }
  29. // NewIconLabel creates and returns a label panel using the specified text
  30. // drawn using the default icon font.
  31. func NewIconLabel(msg string) *Label {
  32. l := new(Label)
  33. l.initialize(msg, StyleDefault().FontIcon)
  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.Black4
  46. l.SetText(msg)
  47. }
  48. // SetText draws the label text using the current font
  49. func (l *Label) SetText(msg string) {
  50. // Do not allow empty labels
  51. str := msg
  52. if len(msg) == 0 {
  53. str = " "
  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(str)
  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, str, 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. l.currentText = str
  80. }
  81. // Text returns the current label text
  82. func (l *Label) Text() string {
  83. return l.currentText
  84. }
  85. // SetColor sets the color of the label text
  86. // The color alpha is set to 1.0
  87. func (l *Label) SetColor(color *math32.Color) *Label {
  88. l.fgColor.FromColor(color, 1.0)
  89. l.SetText(l.currentText)
  90. return l
  91. }
  92. // SetColor4 sets the color4 of the label text
  93. func (l *Label) SetColor4(color4 *math32.Color4) *Label {
  94. l.fgColor = *color4
  95. l.SetText(l.currentText)
  96. return l
  97. }
  98. // Color returns the current color of the label text
  99. func (l *Label) Color() math32.Color4 {
  100. return l.fgColor
  101. }
  102. // SetBgColor sets the color of the label background
  103. // The color alpha is set to 1.0
  104. func (l *Label) SetBgColor(color *math32.Color) *Label {
  105. l.bgColor.FromColor(color, 1.0)
  106. l.Panel.SetColor4(&l.bgColor)
  107. l.SetText(l.currentText)
  108. return l
  109. }
  110. // SetBgColor4 sets the color4 of the label background
  111. func (l *Label) SetBgColor4(color *math32.Color4) *Label {
  112. l.bgColor = *color
  113. l.Panel.SetColor4(&l.bgColor)
  114. l.SetText(l.currentText)
  115. return l
  116. }
  117. // BgColor returns the current color the label background
  118. func (l *Label) BgColor() math32.Color4 {
  119. return l.bgColor
  120. }
  121. // SetFontSize sets label font size
  122. func (l *Label) SetFontSize(size float64) *Label {
  123. l.fontSize = size
  124. l.SetText(l.currentText)
  125. return l
  126. }
  127. // FontSize returns the current label font size
  128. func (l *Label) FontSize() float64 {
  129. return l.fontSize
  130. }
  131. // SetFontDPI sets the font dots per inch
  132. func (l *Label) SetFontDPI(dpi float64) *Label {
  133. l.fontDPI = dpi
  134. l.SetText(l.currentText)
  135. return l
  136. }
  137. // SetLineSpacing sets the spacing between lines.
  138. // The default value is 1.0
  139. func (l *Label) SetLineSpacing(spacing float64) *Label {
  140. l.lineSpacing = spacing
  141. l.SetText(l.currentText)
  142. return l
  143. }
  144. // setTextCaret sets the label text and draws a caret at the
  145. // specified line and column.
  146. // It is normally used by the Edit widget.
  147. func (l *Label) setTextCaret(msg string, mx, width, line, col int) {
  148. // Set font properties
  149. l.font.SetSize(l.fontSize)
  150. l.font.SetDPI(l.fontDPI)
  151. l.font.SetLineSpacing(l.lineSpacing)
  152. l.font.SetBgColor4(&l.bgColor)
  153. l.font.SetFgColor4(&l.fgColor)
  154. // Create canvas and draw text
  155. _, height := l.font.MeasureText(msg)
  156. canvas := text.NewCanvas(width, height, &l.bgColor)
  157. canvas.DrawTextCaret(mx, 0, msg, l.font, line, col)
  158. // Creates texture if if doesnt exist.
  159. if l.tex == nil {
  160. l.tex = texture.NewTexture2DFromRGBA(canvas.RGBA)
  161. l.Panel.Material().AddTexture(l.tex)
  162. // Otherwise update texture with new image
  163. } else {
  164. l.tex.SetFromRGBA(canvas.RGBA)
  165. }
  166. // Set texture filtering parameters for text
  167. l.tex.SetMagFilter(gls.NEAREST)
  168. l.tex.SetMinFilter(gls.NEAREST)
  169. // Updates label panel dimensions
  170. l.Panel.SetContentSize(float32(width), float32(height))
  171. l.currentText = msg
  172. }