label.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // NewLabel 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(0, 0)
  49. // 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)
  50. if font != StyleDefault().FontIcon {
  51. l.Panel.SetPaddings(2, 0, 2, 0)
  52. }
  53. // Copy the style based on the default Label style
  54. styleCopy := StyleDefault().Label
  55. l.style = &styleCopy
  56. l.SetText(msg)
  57. }
  58. // SetText sets and draws the label text using the font.
  59. func (l *Label) SetText(text string) {
  60. // Need at least a character to get dimensions
  61. l.text = text
  62. if text == "" {
  63. text = " "
  64. }
  65. // Set font properties
  66. l.font.SetAttributes(&l.style.FontAttributes)
  67. l.font.SetColor(&l.style.FgColor)
  68. // Create an image with the text
  69. textImage := l.font.DrawText(text)
  70. // Create texture if it doesn't exist yet
  71. if l.tex == nil {
  72. l.tex = texture.NewTexture2DFromRGBA(textImage)
  73. l.tex.SetMagFilter(gls.NEAREST)
  74. l.tex.SetMinFilter(gls.NEAREST)
  75. l.Panel.Material().AddTexture(l.tex)
  76. // Otherwise update texture with new image
  77. } else {
  78. l.tex.SetFromRGBA(textImage)
  79. }
  80. // Update label panel dimensions
  81. l.Panel.SetContentSize(float32(textImage.Rect.Dx()), float32(textImage.Rect.Dy()))
  82. }
  83. // Text returns the label text.
  84. func (l *Label) Text() string {
  85. return l.text
  86. }
  87. // SetColor sets the text color.
  88. // Alpha is set to 1 (opaque).
  89. func (l *Label) SetColor(color *math32.Color) *Label {
  90. l.style.FgColor.FromColor(color, 1.0)
  91. l.SetText(l.text)
  92. return l
  93. }
  94. // SetColor4 sets the text color.
  95. func (l *Label) SetColor4(color4 *math32.Color4) *Label {
  96. l.style.FgColor = *color4
  97. l.SetText(l.text)
  98. return l
  99. }
  100. // Color returns the text color.
  101. func (l *Label) Color() math32.Color4 {
  102. return l.style.FgColor
  103. }
  104. // SetBgColor sets the background color.
  105. // The color alpha is set to 1.0
  106. func (l *Label) SetBgColor(color *math32.Color) *Label {
  107. l.style.BgColor.FromColor(color, 1.0)
  108. l.Panel.SetColor4(&l.style.BgColor)
  109. l.SetText(l.text)
  110. return l
  111. }
  112. // SetBgColor4 sets the background color.
  113. func (l *Label) SetBgColor4(color *math32.Color4) *Label {
  114. l.style.BgColor = *color
  115. l.Panel.SetColor4(&l.style.BgColor)
  116. l.SetText(l.text)
  117. return l
  118. }
  119. // BgColor returns returns the background color.
  120. func (l *Label) BgColor() math32.Color4 {
  121. return l.style.BgColor
  122. }
  123. // SetFont sets the font.
  124. func (l *Label) SetFont(f *text.Font) {
  125. l.font = f
  126. l.SetText(l.text)
  127. }
  128. // Font returns the font.
  129. func (l *Label) Font() *text.Font {
  130. return l.font
  131. }
  132. // SetFontSize sets the point size of the font.
  133. func (l *Label) SetFontSize(size float64) *Label {
  134. l.style.PointSize = size
  135. l.SetText(l.text)
  136. return l
  137. }
  138. // FontSize returns the point size of the font.
  139. func (l *Label) FontSize() float64 {
  140. return l.style.PointSize
  141. }
  142. // SetFontDPI sets the resolution of the font in dots per inch (DPI).
  143. func (l *Label) SetFontDPI(dpi float64) *Label {
  144. l.style.DPI = dpi
  145. l.SetText(l.text)
  146. return l
  147. }
  148. // FontDPI returns the resolution of the font in dots per inch (DPI).
  149. func (l *Label) FontDPI() float64 {
  150. return l.style.DPI
  151. }
  152. // SetLineSpacing sets the spacing between lines.
  153. func (l *Label) SetLineSpacing(spacing float64) *Label {
  154. l.style.LineSpacing = spacing
  155. l.SetText(l.text)
  156. return l
  157. }
  158. // LineSpacing returns the spacing between lines.
  159. func (l *Label) LineSpacing() float64 {
  160. return l.style.LineSpacing
  161. }
  162. // setTextCaret sets the label text and draws a caret at the
  163. // specified line and column.
  164. // It is normally used by the Edit widget.
  165. func (l *Label) setTextCaret(msg string, mx, width, line, col int) {
  166. // Set font properties
  167. l.font.SetAttributes(&l.style.FontAttributes)
  168. l.font.SetColor(&l.style.FgColor)
  169. // Create canvas and draw text
  170. _, height := l.font.MeasureText(msg)
  171. canvas := text.NewCanvas(width, height, &l.style.BgColor)
  172. canvas.DrawTextCaret(mx, 0, msg, l.font, line, col)
  173. // Creates texture if if doesnt exist.
  174. if l.tex == nil {
  175. l.tex = texture.NewTexture2DFromRGBA(canvas.RGBA)
  176. l.Panel.Material().AddTexture(l.tex)
  177. // Otherwise update texture with new image
  178. } else {
  179. l.tex.SetFromRGBA(canvas.RGBA)
  180. }
  181. // Set texture filtering parameters for text
  182. l.tex.SetMagFilter(gls.NEAREST)
  183. l.tex.SetMinFilter(gls.NEAREST)
  184. // Updates label panel dimensions
  185. l.Panel.SetContentSize(float32(width), float32(height))
  186. l.text = msg
  187. }