From 05b92f949e40700b70ea8b34e826de949a891f69 Mon Sep 17 00:00:00 2001 From: Felix Angell Date: Sat, 9 Jun 2018 10:58:06 +0100 Subject: [PATCH] move cursor render into cursor --- gui/buffer.go | 29 +++++++++++++---------------- gui/cursor.go | 45 +++++++++++++++++++++++++++++++++++++++++---- gui/view.go | 39 ++++++++++++++++++++++++--------------- main.go | 8 +++++++- 4 files changed, 85 insertions(+), 36 deletions(-) diff --git a/gui/buffer.go b/gui/buffer.go index 21f072f..377c6f7 100644 --- a/gui/buffer.go +++ b/gui/buffer.go @@ -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) diff --git a/gui/cursor.go b/gui/cursor.go index 21a6dee..b8f72f1 100644 --- a/gui/cursor.go +++ b/gui/cursor.go @@ -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) + } +} diff --git a/gui/view.go b/gui/view.go index df152e5..77d1924 100644 --- a/gui/view.go +++ b/gui/view.go @@ -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 } diff --git a/main.go b/main.go index a613972..5a467a0 100644 --- a/main.go +++ b/main.go @@ -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) }