Bladeren bron

Борьба с виджетом, тесты

SVI 3 jaren geleden
bovenliggende
commit
d5f8d17859

+ 1 - 1
libtui/v0/color/color.go

@@ -46,5 +46,5 @@ func (sf *Color) G() uint8 {
 
 // B -- возвращает голубую компоненту
 func (sf *Color) B() uint8 {
-	return sf.g
+	return sf.b
 }

+ 65 - 0
libtui/v0/color/color_test.go

@@ -0,0 +1,65 @@
+package color
+
+import (
+	"testing"
+
+	"p78git.ddns.net/svi/libtui/types"
+)
+
+/*
+	Тест для объекта цвета
+*/
+
+type tester struct {
+	t    *testing.T
+	col  *Color
+	icol types.IColor
+}
+
+func TestColor(t *testing.T) {
+	test := &tester{
+		t: t,
+	}
+	test.create()
+	test.set()
+}
+
+// устанавливает цвет
+func (sf *tester) set() {
+	sf.t.Logf("set")
+	sf.icol.Set(1, 3, 5)
+	if col := sf.icol.Get(); col != 16843525 {
+		sf.t.Fatalf("create(): color(%v)!=16843525", col)
+	}
+	if r := sf.icol.R(); r != 1 {
+		sf.t.Fatalf("create(): red!=1")
+	}
+	if g := sf.icol.G(); g != 3 {
+		sf.t.Fatalf("create(): green!=3")
+	}
+	if b := sf.icol.B(); b != 5 {
+		sf.t.Fatalf("create(): blue(%v)!=5", b)
+	}
+}
+
+// Создание цвета
+func (sf *tester) create() {
+	sf.t.Logf("create")
+	sf.col = NewColor()
+	if sf.col == nil {
+		sf.t.Fatalf("create(): color==nil")
+	}
+	sf.icol = sf.col
+	if col := sf.icol.Get(); col != 0 {
+		sf.t.Fatalf("create(): color!=0")
+	}
+	if r := sf.icol.R(); r != 0 {
+		sf.t.Fatalf("create(): red!=0")
+	}
+	if g := sf.icol.G(); g != 0 {
+		sf.t.Fatalf("create(): green!=0")
+	}
+	if b := sf.icol.B(); b != 0 {
+		sf.t.Fatalf("create(): blue!=0")
+	}
+}

+ 102 - 0
libtui/v0/line_cell/cell/cell.go

@@ -0,0 +1,102 @@
+// package cell -- ячейка для отрисовки на экране
+package cell
+
+import (
+	"github.com/gdamore/tcell"
+	"p78git.ddns.net/svi/libtui/alias"
+	"p78git.ddns.net/svi/libtui/pos"
+	"p78git.ddns.net/svi/libtui/types"
+)
+
+// Cell -- ячейка для отрисовки на экране
+type Cell struct {
+	pos         types.IPos
+	lit         alias.Lit
+	style       alias.Style
+	bgColor     alias.Color
+	fgColor     alias.Color
+	isBold      bool
+	isBlink     bool
+	isReverse   bool
+	isUnderline bool
+	isDim       bool
+	isItalic    bool
+	isNormal    bool
+}
+
+// NewCell -- возвращает новую знакоместо экрана
+func NewCell() *Cell {
+	sf := &Cell{
+		pos: pos.NewPos(),
+	}
+	return sf
+}
+
+// Pos -- возвращает позицию ячейки на экране
+func (sf *Cell) Pos() (alias.PosX, alias.PosY) {
+	return sf.pos.Get()
+}
+
+// SetPos -- устанавливает позицию литеры на экране
+func (sf *Cell) SetPos(posX alias.PosX, posY alias.PosY) {
+	sf.pos.Set(posX, posY)
+}
+
+// Lit -- возвращает хранимую литеру
+func (sf *Cell) Lit() alias.Lit {
+	return sf.lit
+}
+
+// SetLit -- устанавливае тлитеру для отображения
+func (sf *Cell) SetLit(lit alias.Lit) {
+	sf.lit = lit
+}
+
+// Style -- возвращает стиль ячейки
+func (sf *Cell) Style() alias.Style {
+	return sf.style
+}
+
+// BgColorSet -- устанавливает фон ячейки
+func (sf *Cell) BgColorSet(bgColor alias.Color) {
+	sf.bgColor = bgColor
+	st := tcell.Style(sf.style)
+	st = st.Background(tcell.Color(bgColor))
+	sf.style = alias.Style(st)
+}
+
+// BgColor -- возвращает цвет фона ячейки
+func (sf *Cell) BgColor() alias.Color {
+	return sf.bgColor
+}
+
+// SetStyle -- устанавливает стиль ячейки
+func (sf *Cell) SetStyle(st alias.Style) {
+	fg, bg, attr := tcell.Style(st).Decompose()
+	sf.bgColor = alias.Color(bg)
+	sf.fgColor = alias.Color(fg)
+	sf.isBold = attr&tcell.AttrBold != 0
+	sf.isBlink = attr&tcell.AttrBlink != 0
+	sf.isReverse = attr&tcell.AttrReverse != 0
+	sf.isUnderline = attr&tcell.AttrUnderline != 0
+	sf.isDim = attr&tcell.AttrDim != 0
+	sf.isItalic = attr&tcell.AttrItalic != 0
+	sf.isNormal = false
+	if !(sf.isBlink || sf.isBold || sf.isReverse || sf.isUnderline || sf.isDim || sf.isItalic) {
+		sf.isNormal = true
+	}
+	sf.style = st
+}
+
+// FgColor -- возвращает цвет литеры
+func (sf *Cell) FgColor() alias.Color {
+	return sf.fgColor
+}
+
+// FgColorSet -- устанавливает новый цвет литеры
+func (sf *Cell) FgColorSet(fgColor alias.Color) {
+	sf.fgColor = fgColor
+	st := tcell.Style(sf.style)
+	st = st.Foreground(tcell.Color(fgColor))
+	sf.style = alias.Style(st)
+}

+ 76 - 0
libtui/v0/line_cell/line_cell.go

@@ -0,0 +1,76 @@
+// package line_cell -- строка ячеек
+package line_cell
+
+import (
+	"fmt"
+
+	"github.com/gdamore/tcell"
+
+	"p78git.ddns.net/svi/libtui/alias"
+	"p78git.ddns.net/svi/libtui/line_cell/cell"
+	"p78git.ddns.net/svi/libtui/types"
+)
+
+// LineCell -- строка ячеек
+type LineCell struct {
+	app types.IApp
+	scr types.IScreen
+	lst []types.ICell
+}
+
+// NewLineCell -- возвращает новую строку ячеек
+func NewLineCell(app types.IApp) (*LineCell, error) {
+	if app == nil {
+		return nil, fmt.Errorf("NewLineCell(): IApp==nil")
+	}
+	sf := &LineCell{
+		app: app,
+		scr: app.Screen(),
+		lst: make([]types.ICell, 0),
+	}
+	return sf, nil
+}
+
+// Draw -- отрисовывает линию ячеек
+func (sf *LineCell) Draw() {
+	// for adr, _ := range sf.lst {
+	// fmt.Printf("LineCell.Draw(): %v\n", adr)
+	// sf.scr.SetCell(cell)
+	cell := cell.NewCell()
+	cell.SetLit(alias.Lit([]rune("=")[0]))
+	st0 := tcell.StyleDefault
+	st0 = st0.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow)
+	x0, y0 := sf.app.Screen().Cursor().Pos()
+	scr := sf.app.Scr()
+	for x := x0; x < x0+20; x++ {
+		for y := y0; y < y0+5; y++ {
+			// cell.SetPos(x, y)
+			scr.SetCell(int(x), int(y), st0, []rune("`")[0])
+		}
+	}
+	// }
+}
+
+// GetCell -- возвращает ячейку по смещению
+func (sf *LineCell) GetCell(adr int) (types.ICell, error) {
+	if adr >= len(sf.lst) {
+		return nil, fmt.Errorf("LineCell.GetCell(): adr(%v)>=lenLine(%v)", adr, len(sf.lst))
+	}
+	return sf.lst[adr], nil
+}
+
+// AddCell -- добавляет ячейку в линию
+func (sf *LineCell) AddCell(cell types.ICell) error {
+	if cell == nil {
+		return fmt.Errorf("LineCell.AddCell(): ICell==nil")
+	}
+	sf.lst = append(sf.lst, cell)
+	return nil
+}
+
+// SetPos -- устанавливает новую позицию линии
+func (sf *LineCell) SetPos(posX alias.PosX, posY alias.PosY) {
+	for adr, cell := range sf.lst {
+		cell.SetPos(posX+alias.PosX(adr), posY)
+	}
+}

+ 1 - 1
libtui/v0/screen/scr_cursor/scr_cursor.go

@@ -5,7 +5,7 @@ import (
 	"fmt"
 
 	"p78git.ddns.net/svi/libtui/alias"
-	"p78git.ddns.net/svi/libtui/cell"
+	"p78git.ddns.net/svi/libtui/line_cell/cell"
 	"p78git.ddns.net/svi/libtui/types"
 )
 

+ 10 - 3
libtui/v0/screen/screen.go

@@ -5,8 +5,9 @@ import (
 	"fmt"
 
 	"github.com/gdamore/tcell"
+
 	"p78git.ddns.net/svi/libtui/alias"
-	"p78git.ddns.net/svi/libtui/cell"
+	"p78git.ddns.net/svi/libtui/line_cell/cell"
 	"p78git.ddns.net/svi/libtui/screen/scr_cursor"
 	"p78git.ddns.net/svi/libtui/screen/win_debug"
 	"p78git.ddns.net/svi/libtui/size"
@@ -17,8 +18,8 @@ import (
 type Screen struct {
 	app      types.IApp
 	scr      tcell.Screen
-	size     types.ISize     // Размер экрана
-	winDebug types.IWinDebug // Окно отладки
+	size     types.ISize         // Размер экрана
+	winDebug *win_debug.WinDebug // Окно отладки
 	cursor   *scr_cursor.ScrCursor
 }
 
@@ -101,9 +102,15 @@ func (sf *Screen) Draw() {
 	sf.winDebug.Draw()
 	sf.cursor.Draw()
 	sf.scr.Show() // Обновить экран
+
 }
 
 // Clear -- очистка экрана
 func (sf *Screen) Clear() {
 	sf.scr.Clear() // Зачистить экран перед началом работы
 }
+
+// Cursor -- возвращает курсор экрана
+func (sf *Screen) Cursor() types.ICell {
+	return sf.cursor
+}

+ 19 - 25
libtui/v0/screen/win_debug/win_debug.go

@@ -5,9 +5,9 @@ import (
 	"fmt"
 
 	"github.com/gdamore/tcell"
+
 	"p78git.ddns.net/svi/libtui/alias"
-	"p78git.ddns.net/svi/libtui/cell"
-	"p78git.ddns.net/svi/libtui/safe_bool"
+	"p78git.ddns.net/svi/libtui/line_cell/cell"
 	"p78git.ddns.net/svi/libtui/types"
 	"p78git.ddns.net/svi/libtui/widget"
 )
@@ -15,9 +15,8 @@ import (
 // WinDebug -- окно отладки для экрана
 type WinDebug struct {
 	*widget.Widget
-	app       types.IApp
-	screen    types.IScreen
-	isVisible types.ISafeBool
+	app    types.IApp
+	screen types.IScreen
 }
 
 // NewWinDebug -- возвращает новое окно отладки для экрана
@@ -30,27 +29,21 @@ func NewWinDebug(screen types.IScreen) (*WinDebug, error) {
 		return nil, fmt.Errorf("NewWinDebug(): in create IWidget, err=\n\t%w", err)
 	}
 	sf := &WinDebug{
-		Widget:    widget,
-		app:       screen.App(),
-		screen:    screen,
-		isVisible: safe_bool.NewSafeBool(),
+		Widget: widget,
+		app:    screen.App(),
+		screen: screen,
 	}
 	return sf, nil
 }
 
-// IsVisible -- возвращает признак видимости окна экрана
-func (sf *WinDebug) IsVisible() bool {
-	return sf.isVisible.Get()
-}
-
 // Hide -- скрыть окно отладки экрана
 func (sf *WinDebug) Hide() {
-	sf.isVisible.Reset()
+	sf.Widget.Hide()
 }
 
 // Show -- показать окно отладки экрана
 func (sf *WinDebug) Show() {
-	sf.isVisible.Set()
+	sf.Widget.Show()
 }
 
 // SetPos -- устанавливает позицию окна отладки
@@ -72,27 +65,28 @@ func (sf *WinDebug) SetPos(x alias.PosX, y alias.PosY) {
 
 // Draw -- перерисовывает окноотладки по требованию
 func (sf *WinDebug) Draw() {
-	if !sf.isVisible.Get() {
+	if !sf.Widget.IsVisible() {
 		return
 	}
-	color := tcell.NewRGBColor(0, 0, 255)
-	// style := tcell.StyleDefault
-	// style = style.Background(color)
-	posX, posY := sf.Pos()
 	sf.Widget.Draw()
+
+	posX, posY := sf.screen.Cursor().Pos()
 	strOut := fmt.Sprintf("WinDebug.Draw(): pos=%v:%v", posX, posY)
-	color1 := tcell.NewRGBColor(200, 200, 200)
+	colorFg := tcell.NewRGBColor(200, 200, 200)
 	style1 := tcell.StyleDefault
-	style1 = style1.Foreground(color1).Background(color)
-	sf.drawText(style1, strOut)
+	colorBg := tcell.NewRGBColor(0, 0, 255)
+	style1 = style1.Foreground(colorFg).Background(colorBg)
+	sf.drawText(alias.Style(style1), strOut)
+
 }
 
-func (sf *WinDebug) drawText(style tcell.Style, text string) {
+func (sf *WinDebug) drawText(style alias.Style, text string) {
 	posX, posY := sf.Pos()
 	for adr, r := range text {
 		cell := cell.NewCell()
 		cell.SetPos(posX+alias.PosX(adr), posY)
 		cell.SetLit(alias.Lit(r))
+		cell.SetStyle(style)
 		sf.screen.SetCell(cell)
 	}
 }

+ 1 - 1
libtui/v0/types/icell.go

@@ -19,7 +19,7 @@ type ICell interface {
 	// BgColorSet -- устанавливает цвет фона
 	BgColorSet(alias.Color)
 	// FgColor -- возвращает передний цвет
-	FgColor()alias.Color
+	FgColor() alias.Color
 	// FgColorSet -- устанавливает передний фон
 	FgColorSet(alias.Color)
 	// SetStyle -- устанавливает стиль ячейки

+ 6 - 0
libtui/v0/types/icolor.go

@@ -10,4 +10,10 @@ type IColor interface {
 	Get() alias.Color
 	// Set -- Устанавливает цвет
 	Set(r, g, b uint8)
+	// R -- возвращает красную компоненту
+	R() uint8
+	// G -- возвращает зелёную компоненту
+	G() uint8
+	// B -- возвращает голубую компоненту
+	B() uint8
 }

+ 1 - 1
libtui/v0/types/ilit.go

@@ -1,4 +1,4 @@
 package types
 
 // ILit -- Интерфейс литеры
-type ILit interface{}
+type ILit interface{}

+ 2 - 0
libtui/v0/types/iscreen.go

@@ -26,4 +26,6 @@ type IScreen interface {
 	Clear()
 	// GetCell -- возвращает ячейку по указанным координатам
 	GetCell(alias.PosX, alias.PosY) ICell
+	// Cursor -- возвращает курсор экрана
+	Cursor() ICell
 }

+ 6 - 0
libtui/v0/types/iwidget.go

@@ -10,4 +10,10 @@ type IWidget interface {
 	Pos() (alias.PosX, alias.PosY)
 	// SetPos -- устаналвивает позицию виджета на экране
 	SetPos(alias.PosX, alias.PosY)
+	// Show -- показывает виджет
+	Show()
+	// Hide -- скрывает виджет
+	Hide()
+	// IsVisible -- признак видимости виджета
+	IsVisible() alias.IsVisible
 }

+ 1 - 1
libtui/v0/types/iwindebug.go

@@ -15,5 +15,5 @@ type IWinDebug interface {
 	// Draw -- перерисовывает окно отладки по требованию
 	Draw()
 	// IsVisible -- признак, что окно отладки показано
-	IsVisible() bool
+	IsVisible() alias.IsVisible
 }

+ 68 - 30
libtui/v0/widget/widget.go

@@ -7,8 +7,9 @@ import (
 	"github.com/gdamore/tcell"
 
 	"p78git.ddns.net/svi/libtui/alias"
-	"p78git.ddns.net/svi/libtui/cell"
 	"p78git.ddns.net/svi/libtui/color"
+	"p78git.ddns.net/svi/libtui/line_cell"
+	"p78git.ddns.net/svi/libtui/line_cell/cell"
 	"p78git.ddns.net/svi/libtui/pos"
 	"p78git.ddns.net/svi/libtui/safe_bool"
 	"p78git.ddns.net/svi/libtui/size"
@@ -21,12 +22,13 @@ type Widget struct {
 	screen    types.IScreen
 	pos       types.IPos
 	size      types.ISize
-	bufCell   []types.ICell
+	bufLine   []*line_cell.LineCell
 	bgColor   types.IColor
 	fgColor   types.IColor
 	isVisible types.ISafeBool
 	style     alias.Style
 	lit       alias.Lit
+	isWisible types.ISafeBool
 }
 
 // NewWidget -- возвращает новый виджет
@@ -39,18 +41,20 @@ func NewWidget(screen types.IScreen) (*Widget, error) {
 		screen:    screen,
 		pos:       pos.NewPos(),
 		size:      size.NewSize(),
-		bufCell:   make([]types.ICell, 0),
+		bufLine:   make([]*line_cell.LineCell, 0),
 		bgColor:   color.NewColor(),
 		fgColor:   color.NewColor(),
 		isVisible: safe_bool.NewSafeBool(),
 		style:     0,
-		lit:       alias.Lit([]rune(" ")[0]),
+		lit:       alias.Lit([]rune("d")[0]),
+		isWisible: safe_bool.NewSafeBool(),
 	}
-	sf.bgColor.Set(0,0,255)
-	sf.fgColor.Set(0,0,255)
+	sf.bgColor.Set(0, 0, 255)
+	sf.fgColor.Set(0, 255, 0)
 	color := tcell.Color(sf.bgColor.Get())
-	sf.style = alias.Style(tcell.StyleDefault.Background(color))
+	sf.style = alias.Style(tcell.StyleDefault.Background(color).Foreground(tcell.Color(sf.fgColor.Get())))
 	sf.Resize(15, 5)
+	sf.isVisible.Set()
 	return sf, nil
 }
 
@@ -59,9 +63,23 @@ func (sf *Widget) Draw() {
 	if !sf.isVisible.Get() {
 		return
 	}
-	for _, cell := range sf.bufCell {
-		sf.screen.SetCell(cell)
+	for _, line := range sf.bufLine {
+		line.Draw()
 	}
+	/*
+		cell := cell.NewCell()
+		cell.SetLit(alias.Lit([]rune("=")[0]))
+		st0 := tcell.StyleDefault
+		st0 = st0.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow)
+		scr := sf.app.Scr()
+		x0, y0 := sf.Pos()
+		for x := x0; x < x0+20; x++ {
+			for y := y0; y < y0+5; y++ {
+				// cell.SetPos(x, y)
+				scr.SetCell(int(x), int(y), st0, []rune("`")[0])
+			}
+		}
+	*/
 }
 
 // FgColor -- возвращает цвет переднего фона
@@ -81,32 +99,34 @@ func (sf *Widget) Size() (alias.SizeX, alias.SizeY) {
 
 // Resize -- изменяет размер виджета
 func (sf *Widget) Resize(sizeX alias.SizeX, sizeY alias.SizeY) {
-	x_, y_ := sf.size.Get()
-	if x_ == sizeX && y_ == sizeY {
-		return
-	}
 	sf.size.Set(sizeX, sizeY)
 	posX, posY := sf.pos.Get()
-	bufCellNew := make([]types.ICell, 0)
-	lenBuf := len(sf.bufCell)
-	for dx := alias.PosX(0); dx < alias.PosX(sizeX); dx++ {
-		for dy := alias.PosY(0); dy < alias.PosY(sizeY); dy++ {
-			ind := int(dx) * int(dy)
-			var cell_ types.ICell
-			if lenBuf != 0 && lenBuf > ind {
-				cell_ = sf.bufCell[ind]
-			} else {
-				cell_ = cell.NewCell()
-				cell_.SetStyle(sf.style)
-				cell_.BgColorSet(sf.bgColor.Get())
-				cell_.FgColorSet(sf.FgColor().Get())
-				cell_.SetLit(sf.lit)
+	bufLine := make([]*line_cell.LineCell, 0)
+	fnAddLine := func(_y_ alias.PosY) {
+		line, err := line_cell.NewLineCell(sf.app)
+		if err != nil {
+			sf.app.Scr().Fini()
+			fmt.Printf("Widget.Resize().fnAddLine(): in crete line_cell, err=\n\t%v\n", err)
+		}
+		for dx := posX; dx < posX+alias.PosX(sizeX); dx++ {
+			cell_ := cell.NewCell()
+			cell_.SetStyle(sf.style)
+			cell_.BgColorSet(sf.bgColor.Get())
+			cell_.FgColorSet(sf.FgColor().Get())
+			cell_.SetLit(sf.lit)
+			cell_.SetPos(dx, _y_)
+			if err = line.AddCell(cell_); err != nil {
+				sf.app.Scr().Fini()
+				fmt.Printf("Widget.Resize().fnAddLine(): in crete cell, err=\n\t%v\n", err)
 			}
-			cell_.SetPos(dx+posX, dy+posY)
-			bufCellNew = append(bufCellNew, cell_)
 		}
+		sf.bufLine = append(sf.bufLine, line)
+	}
+
+	for dy := posY; dy < posY+alias.PosY(sizeY); dy++ {
+		fnAddLine(dy)
 	}
-	sf.bufCell = bufCellNew
+	sf.bufLine = bufLine
 }
 
 // Pos -- возвращает размера виджета
@@ -117,4 +137,22 @@ func (sf *Widget) Pos() (alias.PosX, alias.PosY) {
 // SetPos -- устанавливает позицию виджета на экране
 func (sf *Widget) SetPos(posX alias.PosX, posY alias.PosY) {
 	sf.pos.Set(posX, posY)
+	for _, line := range sf.bufLine {
+		line.SetPos(posX, posY)
+	}
+}
+
+// Show -- показать виджет
+func (sf *Widget) Show() {
+	sf.isVisible.Set()
+}
+
+// Hide -- скрывает виджет
+func (sf *Widget) Hide() {
+	sf.isVisible.Reset()
+}
+
+// IsVisible -- признак видимости окна
+func (sf *Widget) IsVisible() alias.IsVisible {
+	return alias.IsVisible(sf.isVisible.Get())
 }