multilabel.go 2.5 KB

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