github.com/jmigpin/editor@v1.6.0/util/drawutil/drawer4/cursor.go (about)

     1  package drawer4
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  
     7  	"github.com/jmigpin/editor/util/imageutil"
     8  )
     9  
    10  type Cursor struct {
    11  	d *Drawer
    12  }
    13  
    14  func (c *Cursor) Init() {}
    15  
    16  func (c *Cursor) Iter() {
    17  	if c.d.Opt.Cursor.On {
    18  		if c.d.iters.runeR.isNormal() {
    19  			c.iter2()
    20  		}
    21  	}
    22  	if !c.d.iterNext() {
    23  		return
    24  	}
    25  }
    26  
    27  func (c *Cursor) iter2() {
    28  	if c.d.st.runeR.ri == c.d.opt.cursor.offset {
    29  		c.draw()
    30  	}
    31  	// delayed draw
    32  	if c.d.st.cursor.delay != nil {
    33  		c.draw2(c.d.st.cursor.delay.penb, c.d.st.cursor.delay.col)
    34  		c.d.st.cursor.delay = nil
    35  	}
    36  }
    37  
    38  func (c *Cursor) End() {}
    39  
    40  //----------
    41  
    42  func (c *Cursor) draw() {
    43  	// pen bounds
    44  	penb := c.d.iters.runeR.penBoundsRect()
    45  
    46  	// color
    47  	col := c.d.Opt.Cursor.Fg
    48  	if col == nil {
    49  		col = c.d.st.curColors.fg
    50  	}
    51  
    52  	//// draw now
    53  	//c.draw2(penb, col)
    54  	//return
    55  
    56  	// delay drawing by one rune to allow drawing the kern bg correctly. The last position is also drawn because the runereader emits a final ru=0 at the end
    57  	c.d.st.cursor.delay = &CursorDelay{penb: penb, col: col}
    58  }
    59  
    60  func (c *Cursor) draw2(dr image.Rectangle, col color.Color) {
    61  	img := c.d.st.drawR.img
    62  	bounds := c.d.Bounds()
    63  
    64  	vbw := 1 // default vertical bar width
    65  
    66  	// vertical bar
    67  	r3 := dr
    68  	r3.Min.X -= vbw / 2
    69  	r3.Max.X = r3.Min.X + vbw
    70  	r4 := r3.Intersect(bounds)
    71  	imageutil.FillRectangle(img, r4, col)
    72  
    73  	// squares width
    74  	aw := vbw // added width
    75  	if c.d.Opt.Cursor.AddedWidth > 0 {
    76  		aw = c.d.Opt.Cursor.AddedWidth
    77  	}
    78  	w := vbw + aw*2 // width
    79  
    80  	// upper square
    81  	r1 := r3
    82  	r1.Min.X -= aw
    83  	r1.Max.X += aw
    84  	r1.Max.Y = r1.Min.Y + w
    85  	r1 = r1.Intersect(bounds)
    86  	imageutil.FillRectangle(img, r1, col)
    87  	// lower square
    88  	r2 := r3
    89  	r2.Min.X -= aw
    90  	r2.Max.X += aw
    91  	r2.Min.Y = r2.Max.Y - w
    92  	r2 = r2.Intersect(bounds)
    93  	imageutil.FillRectangle(img, r2, col)
    94  }
    95  
    96  //----------
    97  
    98  type CursorDelay struct {
    99  	penb image.Rectangle
   100  	col  color.Color
   101  }