move cursor render into cursor

This commit is contained in:
Felix Angell 2018-06-09 10:58:06 +01:00
parent 2c0775ad55
commit 05b92f949e
4 changed files with 85 additions and 36 deletions

View File

@ -155,7 +155,7 @@ func NewBuffer(conf *cfg.TomlConfig, buffOpts BufferConfig, parent *View, index
buff := &Buffer{
index: index,
parent: parent,
curs: &Cursor{},
curs: nil,
cfg: config,
table: piecetable.MakePieceTable(""),
buffOpts: buffOpts,
@ -163,6 +163,7 @@ func NewBuffer(conf *cfg.TomlConfig, buffOpts BufferConfig, parent *View, index
cam: &camera{0, 0, 0, 0},
autoComplete: newAutoCompleteBox(),
}
buff.curs = newCursor(buff)
return buff
}
@ -1354,25 +1355,21 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
lastSelection.renderAt(ctx, b.x+b.ex, b.y+b.ey)
}
cursorHeight := last_h + pad
// render the ol' cursor
if b.HasFocus() && (renderFlashingCursor || b.curs.moving) && b.cfg.Cursor.Draw {
// calculate cursor sizes... does
// this have to be done every frame?
{
cursorWidth := b.cfg.Cursor.GetCaretWidth()
if cursorWidth == -1 {
cursorWidth = last_w
}
cursorHeight := last_h + pad
xPos := b.ex + (rx + b.curs.rx*last_w) - (b.cam.x * last_w)
yPos := b.ey + (ry + b.curs.ry*cursorHeight) - (b.cam.y * cursorHeight)
b.curs.SetSize(cursorWidth, cursorHeight)
}
ctx.SetColor(strife.HexRGB(b.buffOpts.cursor))
ctx.Rect(xPos, yPos, cursorWidth, cursorHeight, strife.Fill)
if DEBUG_MODE {
ctx.SetColor(strife.HexRGB(0xff00ff))
ctx.Rect(xPos, yPos, cursorWidth, cursorHeight, strife.Line)
}
// render the ol' cursor
if b.HasFocus() && (renderFlashingCursor || b.curs.moving) && b.cfg.Cursor.Draw {
b.curs.Render(ctx, rx, ry)
}
numLines := len(b.table.Lines)
@ -1482,9 +1479,9 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
if b.autoComplete.hasSuggestions() {
xPos := b.ex + (rx + b.curs.rx*last_w) - (b.cam.x * last_w)
yPos := b.ey + (ry + b.curs.ry*cursorHeight) - (b.cam.y * cursorHeight)
yPos := b.ey + (ry + b.curs.ry*b.curs.height) - (b.cam.y * b.curs.height)
autoCompleteBoxHeight := len(b.autoComplete.suggestions) * cursorHeight
autoCompleteBoxHeight := len(b.autoComplete.suggestions) * b.curs.height
yPos = yPos - autoCompleteBoxHeight
b.autoComplete.renderAt(xPos, yPos, ctx)

View File

@ -1,10 +1,32 @@
package gui
import (
"github.com/felixangell/strife"
)
type Cursor struct {
x, y int
rx, ry int
dx, dy int
moving bool
parent *Buffer
x, y int
rx, ry int
dx, dy int
moving bool
width, height int
}
func newCursor(parent *Buffer) *Cursor {
return &Cursor{
parent,
0, 0,
0, 0,
0, 0,
false,
0, 0,
}
}
func (c *Cursor) SetSize(w, h int) {
c.width = w
c.height = h
}
func (c *Cursor) gotoStart() {
@ -40,3 +62,18 @@ func (c *Cursor) moveRender(x, y, rx, ry int) {
c.rx += rx
c.ry += ry
}
func (c *Cursor) Render(ctx *strife.Renderer, xOff, yOff int) {
b := c.parent
xPos := b.ex + (xOff + c.rx*last_w) - (b.cam.x * last_w)
yPos := b.ey + (yOff + c.ry*c.height) - (b.cam.y * c.height)
ctx.SetColor(strife.HexRGB(b.buffOpts.cursor))
ctx.Rect(xPos, yPos, c.width, c.height, strife.Fill)
if DEBUG_MODE {
ctx.SetColor(strife.HexRGB(0xff00ff))
ctx.Rect(xPos, yPos, c.width, c.height, strife.Line)
}
}

View File

@ -307,6 +307,29 @@ func (n *View) OnUpdate() bool {
return dirty
}
func (n *View) Resize(w, h int) {
n.BaseComponent.Resize(w, h)
// dont resize any buffer panes
// because there are none.
if len(n.buffers) == 0 {
return
}
// work out the size of the buffer and set it
// note that we +1 the components because
// we haven't yet added the panel
bufferWidth := n.w / len(n.buffers)
// translate all the buffers accordingly.
idx := 0
for _, buffPane := range n.buffers {
buffPane.Resize(bufferWidth, n.h)
buffPane.SetPosition(bufferWidth*idx, 0)
idx++
}
}
func (n *View) OnRender(ctx *strife.Renderer) {
for _, buffPane := range n.buffers {
buffPane.OnRender(ctx)
@ -342,23 +365,9 @@ func (n *View) AddBuffer() *Buffer {
c.SetFocus(true)
// work out the size of the buffer and set it
// note that we +1 the components because
// we haven't yet added the panel
var bufferWidth int
bufferWidth = n.w / (c.index + 1)
n.focusedBuff = c.index
n.buffers = append(n.buffers, NewBufferPane(c))
// translate all the buffers accordingly.
idx := 0
for _, buffPane := range n.buffers {
buffPane.Resize(bufferWidth, n.h)
buffPane.SetPosition(bufferWidth*idx, 0)
idx++
}
n.Resize(n.w, n.h)
return c
}

View File

@ -24,6 +24,10 @@ type PhiEditor struct {
mainView *gui.View
}
func (n *PhiEditor) resize(w, h int) {
n.mainView.Resize(w, h)
}
func (n *PhiEditor) handleEvent(evt strife.StrifeEvent) {
}
@ -84,9 +88,11 @@ func main() {
editor := &PhiEditor{running: true}
window.HandleEvents(func(evt strife.StrifeEvent) {
switch evt.(type) {
switch event := evt.(type) {
case *strife.CloseEvent:
window.Close()
case *strife.WindowResizeEvent:
editor.resize(event.Width, event.Height)
default:
editor.handleEvent(evt)
}