Browse Source

Добавление стиля курсора, доработка ячейки

SVI 3 năm trước cách đây
mục cha
commit
84d7abff5e

+ 54 - 5
libtui/v0/cell/cell.go

@@ -2,6 +2,7 @@
 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"
@@ -9,9 +10,18 @@ import (
 
 // Cell -- ячейка для отрисовки на экране
 type Cell struct {
-	pos   types.IPos
-	lit   alias.Lit
-	style alias.Style
+	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 -- возвращает новую знакоместо экрана
@@ -47,7 +57,46 @@ 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(style alias.Style) {
-	sf.style = style
+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)
 }

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

@@ -24,7 +24,7 @@ func NewScrCursor(scr types.IScreen) (*ScrCursor, error) {
 		scr:  scr,
 		Cell: cell.NewCell(),
 	}
-	sf.SetLit(alias.Lit([]rune("")[0]))
+	sf.SetLit(alias.Lit([]rune("")[0]))
 	return sf, nil
 }
 
@@ -32,3 +32,11 @@ func NewScrCursor(scr types.IScreen) (*ScrCursor, error) {
 func (sf *ScrCursor) Draw() {
 	sf.scr.Set(sf)
 }
+
+// SetPos -- устанавливает позицию курсора с подстройкой фона
+func (sf *ScrCursor) SetPos(posX alias.PosX, posY alias.PosY) {
+	cell := sf.scr.GetCell(posX, posY)
+	color := cell.BgColor()
+	sf.BgColorSet(color)
+	sf.Cell.SetPos(posX, posY)
+}

+ 13 - 2
libtui/v0/screen/screen.go

@@ -6,6 +6,7 @@ import (
 
 	"github.com/gdamore/tcell"
 	"p78git.ddns.net/svi/libtui/alias"
+	"p78git.ddns.net/svi/libtui/cell"
 	"p78git.ddns.net/svi/libtui/screen/scr_cursor"
 	"p78git.ddns.net/svi/libtui/screen/win_debug"
 	"p78git.ddns.net/svi/libtui/size"
@@ -40,6 +41,7 @@ func NewScreen(app types.IApp) (*Screen, error) {
 	if err != nil {
 		return nil, fmt.Errorf("NewScreen(): in create ScrCursor, err=\n\t%w", err)
 	}
+	sf.scr.HideCursor()
 	return sf, nil
 }
 
@@ -76,6 +78,15 @@ func (sf *Screen) MousePos() (alias.PosX, alias.PosY) {
 	return sf.cursor.Pos()
 }
 
+// GetCell -- возвращает ячейку по указанному знакоместу
+func (sf *Screen) GetCell(posX alias.PosX, posY alias.PosY) types.ICell {
+	rune_, _, style, _ := sf.scr.GetContent(int(posX), int(posY))
+	cell := cell.NewCell()
+	cell.SetLit(alias.Lit(rune_))
+	cell.SetStyle(alias.Style(style))
+	return cell
+}
+
 // Set -- устанавливает ячейку на экран
 func (sf *Screen) Set(cell types.ICell) {
 	x, y := cell.Pos()
@@ -93,6 +104,6 @@ func (sf *Screen) Draw() {
 }
 
 // Clear -- очистка экрана
-func (sf *Screen)Clear(){
+func (sf *Screen) Clear() {
 	sf.scr.Clear() // Зачистить экран перед началом работы
-}
+}

+ 10 - 2
libtui/v0/types/icell.go

@@ -10,10 +10,18 @@ type ICell interface {
 	SetPos(alias.PosX, alias.PosY)
 	// Style -- стиль ячейки
 	Style() alias.Style
-	// SetStyle -- устанавливает стиль ячейки
-	SetStyle(alias.Style)
 	// Lit -- литера ячейки
 	Lit() alias.Lit
 	// SetLit -- устанавливает литеру для отображения
 	SetLit(alias.Lit)
+	// BgColor -- возвращает цвет фона
+	BgColor() alias.Color
+	// BgColorSet -- устанавливает цвет фона
+	BgColorSet(alias.Color)
+	// FgColor -- возвращает передний цвет
+	FgColor()alias.Color
+	// FgColorSet -- устанавливает передний фон
+	FgColorSet(alias.Color)
+	// SetStyle -- устанавливает стиль ячейки
+	SetStyle(alias.Style)
 }

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

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