label.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package UI
  2. import (
  3. "fmt"
  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/draw"
  8. "github.com/cuu/gogame/font"
  9. "github.com/cuu/gogame/rect"
  10. "github.com/cuu/gogame/surface"
  11. )
  12. type LabelInterface interface {
  13. Init(text string, font_obj *ttf.Font, col *color.Color)
  14. SetCanvasHWND(canvas *sdl.Surface)
  15. Coord() (int, int)
  16. Size() (int, int)
  17. NewCoord(x, y int)
  18. NewSize(w, h int)
  19. SetColor(col *color.Color)
  20. GetText() string
  21. SetText(text string)
  22. Draw()
  23. DrawCenter(bold bool)
  24. SetBold(b bool)
  25. }
  26. type Label struct {
  27. Widget
  28. Text string
  29. FontObj *ttf.Font
  30. Color *color.Color
  31. CanvasHWND *sdl.Surface
  32. Bold bool
  33. // TextSurf *sdl.Surface
  34. }
  35. func NewLabel() *Label {
  36. l := &Label{}
  37. l.Color = &color.Color{83, 83, 83, 255}
  38. l.Bold = false
  39. return l
  40. }
  41. func (self *Label) Init(text string, font_obj *ttf.Font, col *color.Color) {
  42. if col != nil {
  43. self.Color = col
  44. }
  45. self.Text = text
  46. self.FontObj = font_obj
  47. self.Width, self.Height = font.Size(self.FontObj, self.Text)
  48. }
  49. func (self *Label) SetCanvasHWND(canvas *sdl.Surface) {
  50. self.CanvasHWND = canvas
  51. }
  52. func (self *Label) Coord() (int, int) {
  53. return self.PosX, self.PosY
  54. }
  55. func (self *Label) Size() (int, int) {
  56. return self.Width, self.Height
  57. }
  58. func (self *Label) NewCoord(x, y int) {
  59. self.PosX = x
  60. self.PosY = y
  61. }
  62. func (self *Label) SetColor(col *color.Color) {
  63. if col != nil {
  64. self.Color = col
  65. }
  66. }
  67. func (self *Label) GetText() string {
  68. return self.Text
  69. }
  70. func (self *Label) SetText(text string) {
  71. self.Text = text
  72. self.Width, self.Height = font.Size(self.FontObj, self.Text)
  73. }
  74. func (self *Label) SetBold(b bool) {
  75. self.Bold = b
  76. }
  77. func (self *Label) DrawCenter(bold bool) { // default bold is false
  78. font.SetBold(self.FontObj, bold)
  79. my_text := font.Render(self.FontObj, self.Text, true, self.Color, nil)
  80. rect_ := draw.MidRect(self.PosX, self.PosY, self.Width, self.Height, Width, Height)
  81. surface.Blit(self.CanvasHWND, my_text, rect_, nil)
  82. my_text.Free()
  83. }
  84. func (self *Label) Draw() {
  85. font.SetBold(self.FontObj, self.Bold) // avoing same font tangling set_bold to others
  86. if len(self.Text) == 0 {
  87. return
  88. }
  89. my_text := font.Render(self.FontObj, self.Text, true, self.Color, nil)
  90. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  91. if my_text == nil || my_text.W <= 0 {
  92. fmt.Println("%#v",my_text,self.FontObj)
  93. }else{
  94. surface.Blit(self.CanvasHWND, my_text, &rect_, nil)
  95. my_text.Free()
  96. }
  97. }