widget.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // package widget -- базовый виджет для построения других виджетов
  2. package widget
  3. import (
  4. "fmt"
  5. "github.com/gdamore/tcell"
  6. "p78git.ddns.net/svi/libtui/alias"
  7. "p78git.ddns.net/svi/libtui/color"
  8. "p78git.ddns.net/svi/libtui/line_cell"
  9. "p78git.ddns.net/svi/libtui/line_cell/cell"
  10. "p78git.ddns.net/svi/libtui/pos"
  11. "p78git.ddns.net/svi/libtui/safe_bool"
  12. "p78git.ddns.net/svi/libtui/size"
  13. "p78git.ddns.net/svi/libtui/types"
  14. )
  15. // Widget -- базовый виджет для построения других виджетов
  16. type Widget struct {
  17. app types.IKernel
  18. screen types.IScreen
  19. pos types.IPos
  20. size types.ISize
  21. bufLine []*line_cell.LineCell
  22. bgColor types.IColor
  23. fgColor types.IColor
  24. isVisible types.ISafeBool
  25. style alias.Style
  26. lit alias.Lit
  27. isWisible types.ISafeBool
  28. }
  29. // NewWidget -- возвращает новый виджет
  30. func NewWidget(app types.IKernel) (*Widget, error) {
  31. if app == nil {
  32. return nil, fmt.Errorf("NewWidget(): IApp==nil")
  33. }
  34. sf := &Widget{
  35. app: app,
  36. screen: app.Screen(),
  37. pos: pos.NewPos(),
  38. size: size.NewSize(),
  39. bufLine: make([]*line_cell.LineCell, 0),
  40. bgColor: color.NewColor(),
  41. fgColor: color.NewColor(),
  42. isVisible: safe_bool.NewSafeBool(),
  43. style: 0,
  44. lit: alias.Lit([]rune("d")[0]),
  45. isWisible: safe_bool.NewSafeBool(),
  46. }
  47. sf.bgColor.Set(0, 0, 255)
  48. sf.fgColor.Set(0, 255, 0)
  49. color := tcell.Color(sf.bgColor.Get())
  50. sf.style = alias.Style(tcell.StyleDefault.Background(color).Foreground(tcell.Color(sf.fgColor.Get())))
  51. sf.Resize(15, 5)
  52. sf.isVisible.Set()
  53. return sf, nil
  54. }
  55. // Draw -- отрисовывает виджет потребованию
  56. func (sf *Widget) Draw() {
  57. if !sf.isVisible.Get() {
  58. return
  59. }
  60. for _, line := range sf.bufLine {
  61. line.Draw()
  62. }
  63. /*
  64. cell := cell.NewCell()
  65. cell.SetLit(alias.Lit([]rune("=")[0]))
  66. st0 := tcell.StyleDefault
  67. st0 = st0.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow)
  68. scr := sf.app.Scr()
  69. x0, y0 := sf.Pos()
  70. for x := x0; x < x0+20; x++ {
  71. for y := y0; y < y0+5; y++ {
  72. // cell.SetPos(x, y)
  73. scr.SetCell(int(x), int(y), st0, []rune("`")[0])
  74. }
  75. }
  76. */
  77. }
  78. // FgColor -- возвращает цвет переднего фона
  79. func (sf *Widget) FgColor() types.IColor {
  80. return sf.fgColor
  81. }
  82. // BgColor -- возвращает цвет фона
  83. func (sf *Widget) BgColor() types.IColor {
  84. return sf.bgColor
  85. }
  86. // Size -- возвращает объект размера виджета
  87. func (sf *Widget) Size() (alias.SizeX, alias.SizeY) {
  88. return sf.size.Get()
  89. }
  90. // Resize -- изменяет размер виджета
  91. func (sf *Widget) Resize(sizeX alias.SizeX, sizeY alias.SizeY) {
  92. sf.size.Set(sizeX, sizeY)
  93. posX, posY := sf.pos.Get()
  94. bufLine := make([]*line_cell.LineCell, 0)
  95. fnAddLine := func(_y_ alias.PosY) {
  96. line, err := line_cell.NewLineCell(sf.app)
  97. if err != nil {
  98. fmt.Printf("Widget.Resize().fnAddLine(): in crete line_cell, err=\n\t%v\n", err)
  99. sf.app.CancelApp()
  100. return
  101. }
  102. for dx := posX; dx < posX+alias.PosX(sizeX); dx++ {
  103. cell_ := cell.NewCell()
  104. cell_.SetStyle(sf.style)
  105. cell_.BgColorSet(sf.bgColor.Get())
  106. cell_.FgColorSet(sf.FgColor().Get())
  107. cell_.SetLit(sf.lit)
  108. cell_.SetPos(dx, _y_)
  109. if err = line.AddCell(cell_); err != nil {
  110. fmt.Printf("Widget.Resize().fnAddLine(): in crete cell, err=\n\t%v\n", err)
  111. sf.app.CancelApp()
  112. return
  113. }
  114. }
  115. sf.bufLine = append(sf.bufLine, line)
  116. }
  117. for dy := posY; dy < posY+alias.PosY(sizeY); dy++ {
  118. fnAddLine(dy)
  119. }
  120. sf.bufLine = bufLine
  121. }
  122. // Pos -- возвращает размера виджета
  123. func (sf *Widget) Pos() (alias.PosX, alias.PosY) {
  124. return sf.pos.Get()
  125. }
  126. // SetPos -- устанавливает позицию виджета на экране
  127. func (sf *Widget) SetPos(posX alias.PosX, posY alias.PosY) {
  128. sf.pos.Set(posX, posY)
  129. for _, line := range sf.bufLine {
  130. line.SetPos(posX, posY)
  131. }
  132. }
  133. // Show -- показать виджет
  134. func (sf *Widget) Show() {
  135. sf.isVisible.Set()
  136. }
  137. // Hide -- скрывает виджет
  138. func (sf *Widget) Hide() {
  139. sf.isVisible.Reset()
  140. }
  141. // IsVisible -- признак видимости окна
  142. func (sf *Widget) IsVisible() alias.IsVisible {
  143. return alias.IsVisible(sf.isVisible.Get())
  144. }