text_item.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package UI
  2. import (
  3. "github.com/veandco/go-sdl2/ttf"
  4. "github.com/cuu/gogame/color"
  5. "github.com/cuu/gogame/draw"
  6. "github.com/cuu/gogame/font"
  7. "github.com/cuu/gogame/surface"
  8. )
  9. type TextItemInterface interface {
  10. IconItemInterface
  11. GetBold() bool
  12. SetBold(bold bool)
  13. GetStr() string
  14. }
  15. type TextItem struct {
  16. IconItem
  17. Str string
  18. Color *color.Color
  19. FontObj *ttf.Font
  20. Bold bool
  21. }
  22. func NewTextItem() *TextItem {
  23. p := &TextItem{}
  24. p.Align = ALIGN["VCenter"]
  25. p.Color = &color.Color{83, 83, 83, 255}
  26. p.MyType = ICON_TYPES["LETTER"]
  27. p.Bold = false
  28. return p
  29. }
  30. func (self *TextItem) GetBold() bool {
  31. return self.Bold
  32. }
  33. func (self *TextItem) SetBold(bold bool) {
  34. self.Bold = bold
  35. }
  36. func (self *TextItem) GetStr() string {
  37. return self.Str
  38. }
  39. func (self *TextItem) Draw() {
  40. font.SetBold(self.FontObj, self.Bold)
  41. my_text := font.Render(self.FontObj, self.Str, true, self.Color, nil)
  42. if surface.GetWidth(my_text) != self.Width {
  43. self.Width = surface.GetWidth(my_text)
  44. }
  45. if surface.GetHeight(my_text) != self.Height {
  46. self.Height = surface.GetHeight(my_text)
  47. }
  48. rect_ := draw.MidRect(self.PosX, self.PosY, self.Width, self.Height, Width, Height)
  49. surface.Blit(self.Parent.GetCanvasHWND(), my_text, rect_, nil)
  50. my_text.Free()
  51. }