label.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/surface"
  7. "github.com/cuu/gogame/rect"
  8. "github.com/cuu/gogame/color"
  9. "github.com/cuu/gogame/font"
  10. "github.com/cuu/gogame/draw"
  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. }
  25. type Label struct {
  26. Widget
  27. Text string
  28. FontObj *ttf.Font
  29. Color *color.Color
  30. CanvasHWND *sdl.Surface
  31. // TextSurf *sdl.Surface
  32. }
  33. func NewLabel() *Label {
  34. l := &Label{}
  35. l.Color = &color.Color{83,83,83,255}
  36. return l
  37. }
  38. func (self *Label) Init(text string, font_obj *ttf.Font,col *color.Color ) {
  39. if col != nil {
  40. self.Color = col
  41. }
  42. self.Text = text
  43. self.FontObj = font_obj
  44. self.Width,self.Height = font.Size(self.FontObj, self.Text)
  45. }
  46. func (self *Label) SetCanvasHWND( canvas *sdl.Surface) {
  47. self.CanvasHWND = canvas
  48. }
  49. func (self *Label) Coord() (int,int) {
  50. return self.PosX,self.PosY
  51. }
  52. func (self *Label) Size() (int,int) {
  53. return self.Width,self.Height
  54. }
  55. func (self *Label) NewCoord(x,y int) {
  56. self.PosX = x
  57. self.PosY = y
  58. }
  59. func (self *Label) SetColor(col *color.Color){
  60. if col != nil {
  61. self.Color = col
  62. }
  63. }
  64. func (self *Label) GetText() string {
  65. return self.Text
  66. }
  67. func (self *Label) SetText(text string) {
  68. self.Text = text
  69. self.Width,self.Height = font.Size(self.FontObj, self.Text)
  70. }
  71. func (self *Label) DrawCenter(bold bool) { // default bold is false
  72. font.SetBold(self.FontObj,bold)
  73. my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)
  74. rect_ := draw.MidRect(self.PosX,self.PosY,self.Width,self.Height,Width,Height)
  75. surface.Blit(self.CanvasHWND,my_text,rect_,nil)
  76. }
  77. func (self *Label) Draw() {
  78. font.SetBold(self.FontObj,false) // avoing same font tangling set_bold to others
  79. my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)
  80. rect_ := rect.Rect(self.PosX,self.PosY,self.Width,self.Height)
  81. surface.Blit(self.CanvasHWND,my_text,&rect_,nil)
  82. }