mirror of
https://github.com/bloeys/gopad.git
synced 2025-12-29 06:58:21 +00:00
Scrolling
This commit is contained in:
35
editor.go
35
editor.go
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -39,6 +38,8 @@ type Editor struct {
|
||||
|
||||
LinesHead *LinesNode
|
||||
LineCount int
|
||||
|
||||
StartPos float32
|
||||
}
|
||||
|
||||
func (e *Editor) SetCursorPos(x, y int) {
|
||||
@ -46,6 +47,10 @@ func (e *Editor) SetCursorPos(x, y int) {
|
||||
e.MouseY = y
|
||||
}
|
||||
|
||||
func (e *Editor) SetStartPos(mouseDeltaNorm int32) {
|
||||
e.StartPos = clampF32(e.StartPos+float32(-mouseDeltaNorm)*settings.ScrollSpeed, 0, float32(e.LineCount))
|
||||
}
|
||||
|
||||
func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
||||
|
||||
//Draw window
|
||||
@ -68,17 +73,20 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
||||
// println("Lines to draw:", linesToDraw)
|
||||
|
||||
dl := imgui.WindowDrawList()
|
||||
for i := 0; i < linesToDraw; i++ {
|
||||
startLine := clampInt(int(e.StartPos), 0, e.LineCount)
|
||||
println("Start Pos: ", e.StartPos, "; Start line:", startLine)
|
||||
for i := startLine; i < startLine+linesToDraw; i++ {
|
||||
dl.AddText(*drawStartPos, imgui.PackedColorFromVec4(imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}), string(e.GetLine(0+i).chars))
|
||||
drawStartPos.Y += lineHeight
|
||||
}
|
||||
|
||||
//Draw cursor
|
||||
cx := clampInt(e.MouseX-int(paddedDrawStartPos.X), 0, int(winSize.X))
|
||||
cy := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, int(winSize.Y))
|
||||
|
||||
clickedLine := clampInt(cy/int(lineHeight), 0, e.LineCount)
|
||||
clickedCol := cx / int(charWidth)
|
||||
fmt.Printf("line,col: %v,%v\n", clickedLine, clickedCol)
|
||||
// fmt.Printf("line,col: %v,%v\n", clickedLine, clickedCol)
|
||||
|
||||
eee := e.GetLine(clickedLine)
|
||||
tabCount, tabChars := getTabs(eee, clickedCol)
|
||||
@ -88,10 +96,10 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
||||
maxCol += clampInt(tabCount*settings.TabSize, 0, math.MaxInt)
|
||||
}
|
||||
finalCol := clampInt(clickedCol+tabChars, 0, maxCol)
|
||||
if len(eee.chars) > 0 && finalCol > 0 {
|
||||
x := finalCol - tabCount*settings.TabSize
|
||||
println("!!!!", len(string(eee.chars)), "; C:", string(eee.chars[x]))
|
||||
}
|
||||
// if len(eee.chars) > 0 && finalCol > 0 {
|
||||
// x := finalCol - tabCount*settings.TabSize
|
||||
// println("!!!!", len(string(eee.chars)), "; C:", string(eee.chars[x]))
|
||||
// }
|
||||
|
||||
lineX := paddedDrawStartPos.X + float32(finalCol)*charWidth
|
||||
lineStart := imgui.Vec2{
|
||||
@ -203,6 +211,19 @@ func NewLineNode() *LinesNode {
|
||||
return &n
|
||||
}
|
||||
|
||||
func clampF32(x, min, max float32) float32 {
|
||||
|
||||
if x > max {
|
||||
return max
|
||||
}
|
||||
|
||||
if x < min {
|
||||
return min
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
func clampInt(x, min, max int) int {
|
||||
|
||||
if x > max {
|
||||
|
||||
4
main.go
4
main.go
@ -171,6 +171,10 @@ func (g *Gopad) Update() {
|
||||
g.getActiveEditor().SetCursorPos(int(x), int(y))
|
||||
}
|
||||
|
||||
if yMove := input.GetMouseWheelYNorm(); yMove != 0 {
|
||||
g.getActiveEditor().SetStartPos(yMove)
|
||||
}
|
||||
|
||||
//Close editor if needed
|
||||
if input.KeyDown(sdl.K_LCTRL) && input.KeyClicked(sdl.K_w) {
|
||||
g.closeEditor(g.activeEditor)
|
||||
|
||||
@ -6,4 +6,5 @@ var (
|
||||
TextSelectionColor imgui.Vec4 = imgui.Vec4{X: 84 / 255.0, Y: 153 / 255.0, Z: 199 / 255.0, W: 0.4}
|
||||
EditorBgColor imgui.Vec4 = imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}
|
||||
TabSize int = 4
|
||||
ScrollSpeed float32 = 4
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user