label.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. )
  11. type LabelInterface interface {
  12. Init( text string, font_obj *ttf.Font,col *color.Color )
  13. SetCanvasHWND( canvas *sdl.Surface)
  14. Coord() (int,int)
  15. Size() (int,int)
  16. NewCoord(x,y int)
  17. NewSize(w,h int)
  18. SetColor(col *color.Color )
  19. GetText() string
  20. SetText(text string)
  21. Draw()
  22. DrawCenter(bold bool)
  23. }
  24. type Label struct {
  25. Widget
  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) DrawCenter(bold bool) { // default bold is false
  71. font.SetBold(self.FontObj,bold)
  72. my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)
  73. rect_ := draw.MidRect(self.PosX,self.PosY,self.Width,self.Height,Width,Height)
  74. surface.Blit(self.CanvasHWND,my_text,rect_,nil)
  75. }
  76. func (self *Label) Draw() {
  77. font.SetBold(self.FontObj,false) // avoing same font tangling set_bold to others
  78. my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)
  79. rect_ := rect.Rect(self.PosX,self.PosY,self.Width,self.Height)
  80. surface.Blit(self.CanvasHWND,my_text,&rect_,nil)
  81. }