Scrolling

This commit is contained in:
bloeys
2022-02-27 10:57:01 +04:00
parent f0c4a82b88
commit f562ec674b
3 changed files with 33 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@ -39,6 +38,8 @@ type Editor struct {
LinesHead *LinesNode LinesHead *LinesNode
LineCount int LineCount int
StartPos float32
} }
func (e *Editor) SetCursorPos(x, y int) { func (e *Editor) SetCursorPos(x, y int) {
@ -46,6 +47,10 @@ func (e *Editor) SetCursorPos(x, y int) {
e.MouseY = y 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) { func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
//Draw window //Draw window
@ -68,17 +73,20 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
// println("Lines to draw:", linesToDraw) // println("Lines to draw:", linesToDraw)
dl := imgui.WindowDrawList() 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)) dl.AddText(*drawStartPos, imgui.PackedColorFromVec4(imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}), string(e.GetLine(0+i).chars))
drawStartPos.Y += lineHeight drawStartPos.Y += lineHeight
} }
//Draw cursor
cx := clampInt(e.MouseX-int(paddedDrawStartPos.X), 0, int(winSize.X)) cx := clampInt(e.MouseX-int(paddedDrawStartPos.X), 0, int(winSize.X))
cy := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, int(winSize.Y)) cy := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, int(winSize.Y))
clickedLine := clampInt(cy/int(lineHeight), 0, e.LineCount) clickedLine := clampInt(cy/int(lineHeight), 0, e.LineCount)
clickedCol := cx / int(charWidth) 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) eee := e.GetLine(clickedLine)
tabCount, tabChars := getTabs(eee, clickedCol) 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) maxCol += clampInt(tabCount*settings.TabSize, 0, math.MaxInt)
} }
finalCol := clampInt(clickedCol+tabChars, 0, maxCol) finalCol := clampInt(clickedCol+tabChars, 0, maxCol)
if len(eee.chars) > 0 && finalCol > 0 { // if len(eee.chars) > 0 && finalCol > 0 {
x := finalCol - tabCount*settings.TabSize // x := finalCol - tabCount*settings.TabSize
println("!!!!", len(string(eee.chars)), "; C:", string(eee.chars[x])) // println("!!!!", len(string(eee.chars)), "; C:", string(eee.chars[x]))
} // }
lineX := paddedDrawStartPos.X + float32(finalCol)*charWidth lineX := paddedDrawStartPos.X + float32(finalCol)*charWidth
lineStart := imgui.Vec2{ lineStart := imgui.Vec2{
@ -203,6 +211,19 @@ func NewLineNode() *LinesNode {
return &n 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 { func clampInt(x, min, max int) int {
if x > max { if x > max {

View File

@ -171,6 +171,10 @@ func (g *Gopad) Update() {
g.getActiveEditor().SetCursorPos(int(x), int(y)) g.getActiveEditor().SetCursorPos(int(x), int(y))
} }
if yMove := input.GetMouseWheelYNorm(); yMove != 0 {
g.getActiveEditor().SetStartPos(yMove)
}
//Close editor if needed //Close editor if needed
if input.KeyDown(sdl.K_LCTRL) && input.KeyClicked(sdl.K_w) { if input.KeyDown(sdl.K_LCTRL) && input.KeyClicked(sdl.K_w) {
g.closeEditor(g.activeEditor) g.closeEditor(g.activeEditor)

View File

@ -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} 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} EditorBgColor imgui.Vec4 = imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}
TabSize int = 4 TabSize int = 4
ScrollSpeed float32 = 4
) )