experimental WIP

this commit experiments with node and edge rendering.

For these I'm trying a Page element with a list of Node and Edge
primitives.

Node primitives are textviews.

Edges are a new, custom primitive.

the edge only draws a line from 0,0 to 5,5 right now but shows that I'm
able to implement a Draw that just writes arbitrary characters to the
screen.

I need to do several things, some of which suggest that i'm abusing the
Pages primitive in terrible ways:

- Add and remove nodes as needed. Pages has no Clear() or Reset(). I
  will have to manually keep track of the page -> node relationship
- Give nodes and edges page names that are unique. I won't really be
  using these and I must avoid collisions.
- Relate edges to nodes somehow so they can be drawn properly
- Ensure that all edges are drawn before all nodes

and a lot more

A tremendous amount of work is ahead but I at least have some basic
stuff proven to be workable.
trunk
vilmibm 2023-06-11 21:51:22 -04:00
parent 2bc70d3377
commit 4ee66d876e
1 changed files with 73 additions and 2 deletions

75
main.go
View File

@ -2,7 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"os" "os"
"time"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -22,11 +24,12 @@ type UI struct {
Mode mode Mode mode
App *tview.Application App *tview.Application
Fields []field Fields []field
Nodes []node
// UI things // UI things
Pages *tview.Pages Pages *tview.Pages
TopFlex *tview.Flex TopFlex *tview.Flex
Field *tview.Box Field *tview.Pages
FieldBar *tview.Flex FieldBar *tview.Flex
BottomBar *tview.Pages BottomBar *tview.Pages
ExInput *tview.InputField ExInput *tview.InputField
@ -83,15 +86,57 @@ type field struct {
Selected bool Selected bool
} }
type node struct {
Text string
Edges []*node
X int
Y int
}
type Edge struct {
}
func (e Edge) Draw(screen tcell.Screen) {
screen.SetContent(0, 0, '!', []rune{}, tcell.StyleDefault)
}
func (e Edge) GetRect() (int, int, int, int) {
return 0, 0, 0, 0
}
func (e Edge) SetRect(x, y, width, height int) {
}
func (e Edge) InputHandler() func(_ *tcell.EventKey, _ func(p tview.Primitive)) {
return func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {}
}
func (e Edge) HasFocus() bool {
return false
}
func (e Edge) Focus(delegate func(p tview.Primitive)) {
}
func (e Edge) Blur() {
}
func (e Edge) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return false, nil
}
}
func NewUI() *UI { func NewUI() *UI {
app := tview.NewApplication() app := tview.NewApplication()
ui := UI{ ui := UI{
Mode: modeNormal, Mode: modeNormal,
App: app, App: app,
Fields: []field{{"scratch", true}, {"test", false}}, Fields: []field{{"scratch", true}, {"test", false}},
Nodes: []node{},
Pages: tview.NewPages(), Pages: tview.NewPages(),
TopFlex: tview.NewFlex(), TopFlex: tview.NewFlex(),
Field: tview.NewBox(), Field: tview.NewPages(),
FieldBar: tview.NewFlex(), FieldBar: tview.NewFlex(),
BottomBar: tview.NewPages(), BottomBar: tview.NewPages(),
ExInput: tview.NewInputField(), ExInput: tview.NewInputField(),
@ -118,7 +163,33 @@ func NewUI() *UI {
ui.FieldBar.SetDirection(tview.FlexColumn) ui.FieldBar.SetDirection(tview.FlexColumn)
ui.Nodes = append(ui.Nodes, node{
Text: "foobar\nbaz\nquux",
})
ui.Nodes = append(ui.Nodes, node{
Text: "hello\nthere\nhow",
})
x := 0
y := 0
rand.Seed(time.Now().Unix())
for _, n := range ui.Nodes {
b := tview.NewTextView()
b.SetText(n.Text)
b.SetBorder(true)
b.SetRect(x, y, 10, 5)
x += 20
ui.Field.AddPage(fmt.Sprintf("%d", rand.Intn(10000)), b, false, true)
}
ui.Field.AddPage("edge", Edge{}, false, true)
app.SetBeforeDrawFunc(func(_ tcell.Screen) bool { app.SetBeforeDrawFunc(func(_ tcell.Screen) bool {
// Handle nodes
// TODO
// Handle field bar // Handle field bar
ui.FieldBar.Clear() ui.FieldBar.Clear()