label.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 with 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. font *text.Font // TrueType font face
  16. tex *texture.Texture2D // Texture with text
  17. style *LabelStyle // The style of the panel and font attributes
  18. text string // Text being displayed
  19. }
  20. // LabelStyle contains all the styling attributes of a Label.
  21. // It's essentially a BasicStyle combined with FontAttributes.
  22. type LabelStyle struct {
  23. PanelStyle
  24. text.FontAttributes
  25. FgColor math32.Color4
  26. }
  27. // NewLabel creates and returns a label panel with
  28. // the specified text drawn using the default text font.
  29. func NewLabel(text string) *Label {
  30. return NewLabelWithFont(text, StyleDefault().Font)
  31. }
  32. // NewIcon creates and returns a label panel with
  33. // the specified text drawn using the default icon font.
  34. func NewIcon(icon string) *Label {
  35. return NewLabelWithFont(icon, StyleDefault().FontIcon)
  36. }
  37. // NewLabelWithFont creates and returns a label panel with
  38. // the specified text drawn using the specified font.
  39. func NewLabelWithFont(msg string, font *text.Font) *Label {
  40. l := new(Label)
  41. l.initialize(msg, font)
  42. return l
  43. }
  44. // initialize initializes this label and is normally used by other
  45. // components which contain a label.
  46. func (l *Label) initialize(msg string, font *text.Font) {
  47. l.font = font
  48. l.Panel.Initialize(l, 0, 0)
  49. l.Panel.mat.SetTransparent(true)
  50. // TODO: Remove this hack in an elegant way e.g. set the label style depending of if it's an icon or text label and have two defaults (one for icon labels one for text tabels)
  51. if font != StyleDefault().FontIcon {
  52. l.Panel.SetPaddings(2, 0, 2, 0)
  53. }
  54. // Copy the style based on the default Label style
  55. styleCopy := StyleDefault().Label
  56. l.style = &styleCopy
  57. l.SetText(msg)
  58. }
  59. // SetText sets and draws the label text using the font.
  60. func (l *Label) SetText(text string) {
  61. // Need at least a character to get dimensions
  62. l.text = text
  63. if text == "" {
  64. text = " "
  65. }
  66. // Set font properties
  67. l.font.SetAttributes(&l.style.FontAttributes)
  68. l.font.SetColor(&l.style.FgColor)
  69. // Create an image with the text
  70. textImage := l.font.DrawText(text)
  71. // Create texture if it doesn't exist yet
  72. if l.tex == nil {
  73. l.tex = texture.NewTexture2DFromRGBA(textImage)
  74. l.tex.SetMagFilter(gls.NEAREST)
  75. l.tex.SetMinFilter(gls.NEAREST)
  76. l.Panel.Material().AddTexture(l.tex)
  77. // Otherwise update texture with new image
  78. } else {
  79. l.tex.SetFromRGBA(textImage)
  80. }
  81. // Update label panel dimensions
  82. l.Panel.SetContentSize(float32(textImage.Rect.Dx()), float32(textImage.Rect.Dy()))
  83. }
  84. // Text returns the label text.
  85. func (l *Label) Text() string {
  86. return l.text
  87. }
  88. // SetColor sets the text color.
  89. // Alpha is set to 1 (opaque).
  90. func (l *Label) SetColor(color *math32.Color) *Label {
  91. l.style.FgColor.FromColor(color, 1.0)
  92. l.SetText(l.text)
  93. return l
  94. }
  95. // SetColor4 sets the text color.
  96. func (l *Label) SetColor4(color4 *math32.Color4) *Label {
  97. l.style.FgColor = *color4
  98. l.SetText(l.text)
  99. return l
  100. }
  101. // Color returns the text color.
  102. func (l *Label) Color() math32.Color4 {
  103. return l.style.FgColor
  104. }
  105. // SetBgColor sets the background color.
  106. // The color alpha is set to 1.0
  107. func (l *Label) SetBgColor(color *math32.Color) *Label {
  108. l.style.BgColor.FromColor(color, 1.0)
  109. l.Panel.SetColor4(&l.style.BgColor)
  110. l.SetText(l.text)
  111. return l
  112. }
  113. // SetBgColor4 sets the background color.
  114. func (l *Label) SetBgColor4(color *math32.Color4) *Label {
  115. l.style.BgColor = *color
  116. l.Panel.SetColor4(&l.style.BgColor)
  117. l.SetText(l.text)
  118. return l
  119. }
  120. // BgColor returns returns the background color.
  121. func (l *Label) BgColor() math32.Color4 {
  122. return l.style.BgColor
  123. }
  124. // SetFont sets the font.
  125. func (l *Label) SetFont(f *text.Font) {
  126. l.font = f
  127. l.SetText(l.text)
  128. }
  129. // Font returns the font.
  130. func (l *Label) Font() *text.Font {
  131. return l.font
  132. }
  133. // SetFontSize sets the point size of the font.
  134. func (l *Label) SetFontSize(size float64) *Label {
  135. l.style.PointSize = size
  136. l.SetText(l.text)
  137. return l
  138. }
  139. // FontSize returns the point size of the font.
  140. func (l *Label) FontSize() float64 {
  141. return l.style.PointSize
  142. }
  143. // SetFontDPI sets the resolution of the font in dots per inch (DPI).
  144. func (l *Label) SetFontDPI(dpi float64) *Label {
  145. l.style.DPI = dpi
  146. l.SetText(l.text)
  147. return l
  148. }
  149. // FontDPI returns the resolution of the font in dots per inch (DPI).
  150. func (l *Label) FontDPI() float64 {
  151. return l.style.DPI
  152. }
  153. // SetLineSpacing sets the spacing between lines.
  154. func (l *Label) SetLineSpacing(spacing float64) *Label {
  155. l.style.LineSpacing = spacing
  156. l.SetText(l.text)
  157. return l
  158. }
  159. // LineSpacing returns the spacing between lines.
  160. func (l *Label) LineSpacing() float64 {
  161. return l.style.LineSpacing
  162. }
  163. // setTextCaret sets the label text and draws a caret at the
  164. // specified line and column.
  165. // It is normally used by the Edit widget.
  166. func (l *Label) setTextCaret(msg string, mx, width, line, col int) {
  167. // Set font properties
  168. l.font.SetAttributes(&l.style.FontAttributes)
  169. l.font.SetColor(&l.style.FgColor)
  170. // Create canvas and draw text
  171. _, height := l.font.MeasureText(msg)
  172. canvas := text.NewCanvas(width, height, &l.style.BgColor)
  173. canvas.DrawTextCaret(mx, 0, msg, l.font, line, col)
  174. // Creates texture if if doesnt exist.
  175. if l.tex == nil {
  176. l.tex = texture.NewTexture2DFromRGBA(canvas.RGBA)
  177. l.Panel.Material().AddTexture(l.tex)
  178. // Otherwise update texture with new image
  179. } else {
  180. l.tex.SetFromRGBA(canvas.RGBA)
  181. }
  182. // Set texture filtering parameters for text
  183. l.tex.SetMagFilter(gls.NEAREST)
  184. l.tex.SetMinFilter(gls.NEAREST)
  185. // Updates label panel dimensions
  186. l.Panel.SetContentSize(float32(width), float32(height))
  187. l.text = msg
  188. }