github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/dos/ui/draw.go (about)

     1  package ui
     2  
     3  import termbox "github.com/nsf/termbox-go"
     4  
     5  func (screen *Screen) DrawFlush() {
     6  	err := termbox.Flush()
     7  	if err != nil {
     8  		// TODO: handle error properly
     9  		panic(err)
    10  	}
    11  }
    12  
    13  func (screen *Screen) DrawCell(x, y int, cell termbox.Cell) {
    14  	termbox.SetCell(x, y, cell.Ch, cell.Fg, cell.Bg)
    15  }
    16  
    17  func (form *Form) DrawFlush() {
    18  	form.Screen.DrawFlush()
    19  }
    20  
    21  func (form *Form) DrawCell(x, y int, cell termbox.Cell) {
    22  	if x < 0 || form.BoundsRect.Height <= x ||
    23  		y < 0 || form.BoundsRect.Width <= y {
    24  		return
    25  	}
    26  
    27  	form.Screen.DrawCell(x+form.BoundsRect.Left, y+form.BoundsRect.Top, cell)
    28  }
    29  
    30  func (form *Form) DrawBlock(r Rect, cell termbox.Cell) {
    31  	for x := r.Left; x < r.Left+r.Width; x += 1 {
    32  		for y := r.Top; y < r.Top+r.Height; y += 1 {
    33  			form.DrawCell(x, y, cell)
    34  		}
    35  	}
    36  }
    37  
    38  func (form *Form) DrawBorder(r Rect) {
    39  	form.DrawBlock(Rect{r.Left, r.Top, r.Width, 1}, termbox.Cell{'─', termbox.ColorDefault, termbox.ColorDefault})
    40  	form.DrawBlock(Rect{r.Left, r.Top + r.Height - 1, r.Width, 1}, termbox.Cell{'─', termbox.ColorDefault, termbox.ColorDefault})
    41  	form.DrawBlock(Rect{r.Left, r.Top, 1, r.Height}, termbox.Cell{'│', termbox.ColorDefault, termbox.ColorDefault})
    42  	form.DrawBlock(Rect{r.Left + r.Width - 1, r.Top, 1, r.Height}, termbox.Cell{'│', termbox.ColorDefault, termbox.ColorDefault})
    43  
    44  	form.DrawCell(r.Left, r.Top, termbox.Cell{'┌', termbox.ColorDefault, termbox.ColorDefault})
    45  	form.DrawCell(r.Left+r.Width, r.Top, termbox.Cell{'┐', termbox.ColorDefault, termbox.ColorDefault})
    46  
    47  	form.DrawCell(r.Left, r.Top+r.Height, termbox.Cell{'└', termbox.ColorDefault, termbox.ColorDefault})
    48  	form.DrawCell(r.Left+r.Width, r.Top+r.Height, termbox.Cell{'┘', termbox.ColorDefault, termbox.ColorDefault})
    49  }
    50  
    51  func (form *Form) DrawText(r Rect, t string, active bool) {
    52  	fg, bg := termbox.ColorDefault, termbox.ColorDefault
    53  	if active {
    54  		fg, bg = termbox.ColorBlack, termbox.ColorWhite
    55  	}
    56  	form.DrawBlock(r, termbox.Cell{' ', fg, bg})
    57  
    58  	x0 := r.Left
    59  	x1 := r.Left + r.Width
    60  	for _, ch := range t {
    61  		//TODO: implement wrap
    62  		if x0 > x1 {
    63  			break
    64  		}
    65  
    66  		form.DrawCell(x0, r.Top, termbox.Cell{ch, fg, bg})
    67  		x0 += 1
    68  	}
    69  }