phi/internal/buff/cursor.go

93 lines
1.7 KiB
Go
Raw Normal View History

2018-06-22 13:36:52 +03:00
package buff
2018-06-09 12:58:06 +03:00
import (
"github.com/felixangell/phi/internal/cfg"
2018-06-09 12:58:06 +03:00
"github.com/felixangell/strife"
)
type Cursor struct {
2018-06-09 12:58:06 +03:00
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,
}
}
// SetPos will set the cursor position
// FIXME this should update the rx, ry:
// there are two ways to do this (as far as I know):
// - re-process the line somehow?
// - 'simulate' what steps to move the cursor from old pos to new pos.
// using the move() func which updates rx, ry for us.
func (c *Cursor) SetPos(x, y int) {
// STUB!
}
2018-06-09 12:58:06 +03:00
func (c *Cursor) SetSize(w, h int) {
c.width = w
c.height = h
}
func (c *Cursor) gotoStart() {
for c.x > 1 {
c.move(-1, 0)
}
}
func (c *Cursor) move(x, y int) {
c.moveRender(x, y, x, y)
}
// moves the cursors position, and the
// rendered coordinates by the given amount
func (c *Cursor) moveRender(x, y, rx, ry int) {
if x > 0 {
c.dx = 1
} else if x < 0 {
c.dx = -1
}
if y > 0 {
c.dy = 1
} else if y < 0 {
c.dy = -1
}
c.moving = true
c.x += x
c.y += y
c.rx += rx
c.ry += ry
}
2018-06-09 12:58:06 +03:00
func (c *Cursor) Render(ctx *strife.Renderer, xOff, yOff int) {
b := c.parent
2018-06-22 12:20:55 +03:00
xPos := b.ex + (xOff + c.rx*lastCharW) - (b.cam.x * lastCharW)
2018-06-09 12:58:06 +03:00
yPos := b.ey + (yOff + c.ry*c.height) - (b.cam.y * c.height)
// NOTE: we dont have to scale the cursor here because
2019-03-16 22:15:26 +03:00
// it's based off the font size which has already been scaled.
2018-06-09 12:58:06 +03:00
ctx.SetColor(strife.HexRGB(b.buffOpts.cursor))
ctx.Rect(xPos, yPos, c.width, c.height, strife.Fill)
2018-06-22 13:36:52 +03:00
if cfg.DebugMode {
2021-01-10 16:10:16 +03:00
ctx.SetColor(strife.HexRGB(cfg.DebugModeRenderColour))
2018-06-09 12:58:06 +03:00
ctx.Rect(xPos, yPos, c.width, c.height, strife.Line)
}
}