mirror of
https://github.com/bloeys/gopad.git
synced 2025-12-29 06:58:21 +00:00
Cleanup position calculations
This commit is contained in:
91
editor.go
91
editor.go
@ -72,7 +72,7 @@ func (e *Editor) RoundToGrid(x float32) float32 {
|
|||||||
return float32(math.Round(float64(x/e.CharWidth))) * e.CharWidth
|
return float32(math.Round(float64(x/e.CharWidth))) * e.CharWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
func (e *Editor) UpdateAndDraw(drawStartPos, winSize *imgui.Vec2, newRunes []rune) {
|
||||||
|
|
||||||
//Draw window
|
//Draw window
|
||||||
imgui.SetNextWindowPos(*drawStartPos)
|
imgui.SetNextWindowPos(*drawStartPos)
|
||||||
@ -87,17 +87,54 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
|||||||
drawStartPos.Y += textPadding
|
drawStartPos.Y += textPadding
|
||||||
paddedDrawStartPos := *drawStartPos
|
paddedDrawStartPos := *drawStartPos
|
||||||
|
|
||||||
//Draw lines
|
//Draw text
|
||||||
linesToDraw := int(winSize.Y / e.LineHeight)
|
|
||||||
// println("Lines to draw:", linesToDraw)
|
|
||||||
|
|
||||||
dl := imgui.WindowDrawList()
|
dl := imgui.WindowDrawList()
|
||||||
|
linesToDraw := int(winSize.Y / e.LineHeight)
|
||||||
startLine := clampInt(int(e.StartPos), 0, e.LineCount)
|
startLine := clampInt(int(e.StartPos), 0, e.LineCount)
|
||||||
for i := startLine; i < startLine+linesToDraw; i++ {
|
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 += e.LineHeight
|
drawStartPos.Y += e.LineHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
|
posInfo := e.getPositions(&paddedDrawStartPos)
|
||||||
|
tabCount, charsToOffsetBy := getTabs(posInfo.Line, posInfo.GridXEditor)
|
||||||
|
|
||||||
|
textWidth := float32(len(posInfo.Line.chars)-tabCount+tabCount*settings.TabSize) * e.CharWidth
|
||||||
|
lineX := clampF32(float32(posInfo.GridXGlobal)+float32(charsToOffsetBy)*e.CharWidth, 0, paddedDrawStartPos.X+textWidth)
|
||||||
|
|
||||||
|
lineStart := imgui.Vec2{
|
||||||
|
X: lineX,
|
||||||
|
Y: paddedDrawStartPos.Y + float32(posInfo.GridYEditor)*e.LineHeight - e.LineHeight*0.25,
|
||||||
|
}
|
||||||
|
lineEnd := imgui.Vec2{
|
||||||
|
X: lineX,
|
||||||
|
Y: paddedDrawStartPos.Y + float32(posInfo.GridYEditor)*e.LineHeight + e.LineHeight*0.75,
|
||||||
|
}
|
||||||
|
dl.AddLineV(lineStart, lineEnd, imgui.PackedColorFromVec4(settings.CursorColor), settings.CursorWidthFactor*e.CharWidth)
|
||||||
|
|
||||||
|
// charAtCursor := getCharFromCursor(clickedLine, clickedColGridXEditor)
|
||||||
|
// println("Chars:", "'"+charAtCursor+"'", ";", clickedColGridXEditor)
|
||||||
|
}
|
||||||
|
|
||||||
|
type mousePosInfo struct {
|
||||||
|
|
||||||
|
//Global represents a grid on the whole window (i.e. 0,0 is the top left of the program window)
|
||||||
|
GridXGlobal int
|
||||||
|
//Global represents a grid on the whole window (i.e. 0,0 is the top left of the program window)
|
||||||
|
GridYGlobal int
|
||||||
|
|
||||||
|
//Editor represents a grid on the editor window (i.e. 0,0 is the top left of the editor window)
|
||||||
|
GridXEditor int
|
||||||
|
//Editor represents a grid on the editor window (i.e. 0,0 is the top left of the editor window)
|
||||||
|
GridYEditor int
|
||||||
|
|
||||||
|
//Line is the currently selected line
|
||||||
|
Line *Line
|
||||||
|
LineNum int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Editor) getPositions(paddedDrawStartPos *imgui.Vec2) mousePosInfo {
|
||||||
|
|
||||||
//Calculate position of cursor in window and grid coords.
|
//Calculate position of cursor in window and grid coords.
|
||||||
//Window coords are as reported by SDL, but we correct for padding and snap to the nearest
|
//Window coords are as reported by SDL, but we correct for padding and snap to the nearest
|
||||||
//char window pos.
|
//char window pos.
|
||||||
@ -107,36 +144,34 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
|||||||
//
|
//
|
||||||
//'Global' suffix means the position is in window coords.
|
//'Global' suffix means the position is in window coords.
|
||||||
//'Editor' suffix means coords are within the text editor coords, where sidebar and tabs have been adjusted for
|
//'Editor' suffix means coords are within the text editor coords, where sidebar and tabs have been adjusted for
|
||||||
clickedColWindowYEditor := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, math.MaxInt)
|
|
||||||
clickedColGridYEditor := clampInt(clickedColWindowYEditor/int(e.LineHeight), 0, e.LineCount)
|
|
||||||
|
|
||||||
roundedMouseX := e.RoundToGrid(float32(e.MouseX))
|
roundedMouseX := e.RoundToGrid(float32(e.MouseX))
|
||||||
clickedColWindowXGlobal := clampInt(int(roundedMouseX), 0, math.MaxInt)
|
gridXGlobal := clampInt(int(roundedMouseX), 0, math.MaxInt)
|
||||||
clickedColGridXEditor := int(
|
|
||||||
|
roundedMouseY := e.RoundToGrid(float32(e.MouseY))
|
||||||
|
gridYGlobal := clampInt(int(roundedMouseY), 0, math.MaxInt)
|
||||||
|
|
||||||
|
gridXEditor := int(
|
||||||
roundF32(
|
roundF32(
|
||||||
(float32(clickedColWindowXGlobal) - paddedDrawStartPos.X) / e.CharWidth,
|
(float32(gridXGlobal) - paddedDrawStartPos.X) / e.CharWidth,
|
||||||
))
|
),
|
||||||
|
)
|
||||||
|
|
||||||
//Draw cursor
|
windowYEditor := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, math.MaxInt)
|
||||||
clickedLine := e.GetLine(startLine + clickedColGridYEditor)
|
gridYEditor := clampInt(windowYEditor/int(e.LineHeight), 0, e.LineCount)
|
||||||
tabCount, charsToOffsetBy := getTabs(clickedLine, clickedColGridXEditor)
|
|
||||||
println("!!", charsToOffsetBy)
|
|
||||||
|
|
||||||
textWidth := float32(len(clickedLine.chars)-tabCount+tabCount*settings.TabSize) * e.CharWidth
|
startLineIndex := clampInt(int(e.StartPos), 0, e.LineCount)
|
||||||
lineX := clampF32(float32(clickedColWindowXGlobal)+float32(charsToOffsetBy)*e.CharWidth, 0, paddedDrawStartPos.X+textWidth)
|
return mousePosInfo{
|
||||||
|
|
||||||
lineStart := imgui.Vec2{
|
GridXGlobal: gridXGlobal,
|
||||||
X: lineX,
|
GridYGlobal: gridYGlobal,
|
||||||
Y: paddedDrawStartPos.Y + float32(clickedColGridYEditor)*e.LineHeight - e.LineHeight*0.25,
|
|
||||||
|
GridXEditor: gridXEditor,
|
||||||
|
GridYEditor: gridYEditor,
|
||||||
|
|
||||||
|
Line: e.GetLine(startLineIndex + gridYEditor),
|
||||||
|
LineNum: startLineIndex + gridYEditor,
|
||||||
}
|
}
|
||||||
lineEnd := imgui.Vec2{
|
|
||||||
X: lineX,
|
|
||||||
Y: paddedDrawStartPos.Y + float32(clickedColGridYEditor)*e.LineHeight + e.LineHeight*0.75,
|
|
||||||
}
|
|
||||||
dl.AddLineV(lineStart, lineEnd, imgui.PackedColorFromVec4(settings.CursorColor), settings.CursorWidthFactor*e.CharWidth)
|
|
||||||
|
|
||||||
// charAtCursor := getCharFromCursor(clickedLine, clickedColGridXEditor)
|
|
||||||
// println("Chars:", "'"+charAtCursor+"'", ";", clickedColGridXEditor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCharFromCursor(l *Line, cursorGridX int) string {
|
func getCharFromCursor(l *Line, cursorGridX int) string {
|
||||||
|
|||||||
14
main.go
14
main.go
@ -35,6 +35,7 @@ type Gopad struct {
|
|||||||
editorToClose int
|
editorToClose int
|
||||||
activeEditor int
|
activeEditor int
|
||||||
lastActiveEditor int
|
lastActiveEditor int
|
||||||
|
newRunes []rune
|
||||||
|
|
||||||
//Errors
|
//Errors
|
||||||
haveErr bool
|
haveErr bool
|
||||||
@ -76,6 +77,7 @@ func main() {
|
|||||||
editors: []Editor{*NewScratchEditor()},
|
editors: []Editor{*NewScratchEditor()},
|
||||||
editorToClose: -1,
|
editorToClose: -1,
|
||||||
sidebarWidthFactor: 0.15,
|
sidebarWidthFactor: 0.15,
|
||||||
|
newRunes: []rune{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// engine.SetVSync(true)
|
// engine.SetVSync(true)
|
||||||
@ -137,6 +139,7 @@ func (g *Gopad) handleWindowEvents(event sdl.Event) {
|
|||||||
|
|
||||||
case *sdl.TextEditingEvent:
|
case *sdl.TextEditingEvent:
|
||||||
case *sdl.TextInputEvent:
|
case *sdl.TextInputEvent:
|
||||||
|
g.newRunes = append(g.newRunes, []rune(e.GetText())...)
|
||||||
case *sdl.WindowEvent:
|
case *sdl.WindowEvent:
|
||||||
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
||||||
w, h := g.Win.SDLWin.GetSize()
|
w, h := g.Win.SDLWin.GetSize()
|
||||||
@ -191,8 +194,9 @@ func (g *Gopad) Update() {
|
|||||||
g.editorToClose = -1
|
g.editorToClose = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
//Save if needed
|
|
||||||
e := g.getActiveEditor()
|
e := g.getActiveEditor()
|
||||||
|
|
||||||
|
//Save if needed
|
||||||
if !e.IsModified {
|
if !e.IsModified {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -352,8 +356,11 @@ func (g *Gopad) drawEditors() {
|
|||||||
tabsHeight := imgui.WindowHeight()
|
tabsHeight := imgui.WindowHeight()
|
||||||
imgui.End()
|
imgui.End()
|
||||||
|
|
||||||
//Draw text area
|
g.getActiveEditor().UpdateAndDraw(
|
||||||
g.getActiveEditor().Render(&imgui.Vec2{X: g.sidebarWidthPx, Y: g.mainMenuBarHeight + tabsHeight}, &imgui.Vec2{X: g.winWidth - g.sidebarWidthPx, Y: g.winHeight - g.mainMenuBarHeight - tabsHeight})
|
&imgui.Vec2{X: g.sidebarWidthPx, Y: g.mainMenuBarHeight + tabsHeight},
|
||||||
|
&imgui.Vec2{X: g.winWidth - g.sidebarWidthPx, Y: g.winHeight - g.mainMenuBarHeight - tabsHeight},
|
||||||
|
g.newRunes,
|
||||||
|
)
|
||||||
|
|
||||||
if shouldForceSwitch || prevActiveEditor != g.activeEditor {
|
if shouldForceSwitch || prevActiveEditor != g.activeEditor {
|
||||||
imgui.SetKeyboardFocusHereV(-1)
|
imgui.SetKeyboardFocusHereV(-1)
|
||||||
@ -438,6 +445,7 @@ func (g *Gopad) handleFileClick(fPath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Gopad) FrameEnd() {
|
func (g *Gopad) FrameEnd() {
|
||||||
|
g.newRunes = []rune{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Gopad) ShouldRun() bool {
|
func (g *Gopad) ShouldRun() bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user