label.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package UI
  2. import (
  3. "github.com/veandco/go-sdl2/sdl"
  4. "github.com/veandco/go-sdl2/ttf"
  5. "github.com/cuu/gogame/surface"
  6. "github.com/cuu/gogame/rect"
  7. "github.com/cuu/gogame/color"
  8. "github.com/cuu/gogame/font"
  9. )
  10. type LabelInterface interface {
  11. Init( text string, font_obj *ttf.Font,col *color.Color )
  12. SetCanvasHWND( canvas *sdl.Surface)
  13. Coord() (int,int)
  14. Size() (int,int)
  15. NewCoord(x,y int)
  16. SetColor(col *color.Color )
  17. GetText() string
  18. SetText(text string)
  19. Draw()
  20. }
  21. type Label struct {
  22. PosX int
  23. PosY int
  24. Width int
  25. Height int
  26. Text string
  27. FontObj *ttf.Font
  28. Color *color.Color
  29. CanvasHWND *sdl.Surface
  30. // TextSurf *sdl.Surface
  31. }
  32. func NewLabel() *Label {
  33. l := &Label{}
  34. l.Color = &color.Color{83,83,83,255}
  35. return l
  36. }
  37. func (self *Label) Init(text string, font_obj *ttf.Font,col *color.Color ) {
  38. if col != nil {
  39. self.Color = col
  40. }
  41. self.Text = text
  42. self.FontObj = font_obj
  43. self.Width,self.Height = font.Size(self.FontObj, self.Text)
  44. }
  45. func (self *Label) SetCanvasHWND( canvas *sdl.Surface) {
  46. self.CanvasHWND = canvas
  47. }
  48. func (self *Label) Coord() (int,int) {
  49. return self.PosX,self.PosY
  50. }
  51. func (self *Label) Size() (int,int) {
  52. return self.Width,self.Height
  53. }
  54. func (self *Label) NewCoord(x,y int) {
  55. self.PosX = x
  56. self.PosY = y
  57. }
  58. func (self *Label) SetColor(col *color.Color){
  59. if col != nil {
  60. self.Color = col
  61. }
  62. }
  63. func (self *Label) GetText() string {
  64. return self.Text
  65. }
  66. func (self *Label) SetText(text string) {
  67. self.Text = text
  68. self.Width,self.Height = font.Size(self.FontObj, self.Text)
  69. }
  70. func (self *Label) Draw() {
  71. font.SetBold(self.FontObj,false) // avoing same font tangling set_bold to others
  72. my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)
  73. rect_ := rect.Rect(self.PosX,self.PosY,self.Width,self.Height)
  74. surface.Blit(self.CanvasHWND,my_text,&rect_,nil)
  75. }