multilabel.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package UI
  2. import (
  3. "strings"
  4. "github.com/veandco/go-sdl2/sdl"
  5. "github.com/veandco/go-sdl2/ttf"
  6. "github.com/cuu/gogame/color"
  7. "github.com/cuu/gogame/font"
  8. "github.com/cuu/gogame/rect"
  9. "github.com/cuu/gogame/surface"
  10. )
  11. //MultiLabel is also a LabelInterface
  12. type MultiLabel struct {
  13. Widget
  14. Text string
  15. FontObj *ttf.Font
  16. Color *color.Color
  17. CanvasHWND *sdl.Surface
  18. //TextSurf *sdl.Surface
  19. MaxWidth int
  20. Bold bool
  21. }
  22. func NewMultiLabel() *MultiLabel {
  23. l := &MultiLabel{}
  24. l.Color = &color.Color{83, 83, 83, 255}
  25. l.Width = 135
  26. l.Height = 100
  27. l.Bold = false
  28. return l
  29. }
  30. func (self *MultiLabel) Init(text string, font_obj *ttf.Font, col *color.Color) {
  31. if col != nil {
  32. self.Color = col
  33. }
  34. self.Text = text
  35. self.FontObj = font_obj
  36. if self.CanvasHWND != nil {
  37. self.Draw()
  38. }
  39. }
  40. func (self *MultiLabel) SetCanvasHWND(canvas *sdl.Surface) {
  41. self.CanvasHWND = canvas
  42. }
  43. func (self *MultiLabel) SetColor(col *color.Color) {
  44. if col != nil {
  45. self.Color = col
  46. }
  47. }
  48. func (self *MultiLabel) GetText() string {
  49. return self.Text
  50. }
  51. func (self *MultiLabel) SetText(text string) {
  52. self.Text = text
  53. }
  54. func (self *MultiLabel) SetBold(b bool) {
  55. self.Bold = b
  56. }
  57. func (self *MultiLabel) DrawCenter(bold bool) {
  58. }
  59. func (self *MultiLabel) Draw() {
  60. font.SetBold(self.FontObj, self.Bold) // avoing same font tangling set_bold to others
  61. self.blit_text(self.CanvasHWND, self.Text, self.PosX, self.PosY, self.FontObj)
  62. }
  63. // difference to Label
  64. func (self *MultiLabel) blit_text(surf *sdl.Surface, text string, pos_x, pos_y int, fnt *ttf.Font) {
  65. words := make([][]string, 0)
  66. temp := strings.Split(text, "\n")
  67. for _, v := range temp {
  68. t := strings.Split(v, " ")
  69. words = append(words, t)
  70. }
  71. space, _ := font.Size(fnt, " ")
  72. max_width := self.Width
  73. x, y := pos_x, pos_y
  74. row_total_width := 0
  75. lines := 0
  76. tmp := words
  77. if len(words) > 4 {
  78. tmp = words[:4]
  79. }
  80. for _, line := range tmp {
  81. word_height := 0
  82. tmp2 := line
  83. if len(line) > 12 {
  84. tmp2 = line[:12]
  85. }
  86. for _, word := range tmp2 {
  87. word_surface := font.Render(fnt, word, true, self.Color, nil)
  88. word_width := surface.GetWidth(word_surface)
  89. word_height = surface.GetHeight(word_surface)
  90. row_total_width += word_width
  91. if row_total_width+space >= max_width {
  92. x = pos_x
  93. y = y + word_height
  94. row_total_width = word_width
  95. if lines == 0 {
  96. lines = lines + word_height
  97. } else {
  98. lines = lines + word_height
  99. }
  100. }
  101. rect_ := rect.Rect(x, y, self.Width, self.Height)
  102. surface.Blit(surf, word_surface, &rect_, nil)
  103. word_surface.Free()
  104. x += (word_width + space)
  105. }
  106. x = pos_x
  107. y += word_height
  108. lines += word_height
  109. }
  110. self.Height = lines
  111. }