From 48d0ed975e949ecdfcdf0d2f4dbe328cf22fb30b Mon Sep 17 00:00:00 2001 From: bloeys Date: Sun, 13 Mar 2022 23:59:24 +0400 Subject: [PATCH] Cleanup position calculations --- editor.go | 91 ++++++++++++++++++++++++++++++++++++++----------------- main.go | 14 +++++++-- 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/editor.go b/editor.go index 19ecf1c..4904adc 100755 --- a/editor.go +++ b/editor.go @@ -72,7 +72,7 @@ func (e *Editor) RoundToGrid(x float32) float32 { 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 imgui.SetNextWindowPos(*drawStartPos) @@ -87,17 +87,54 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) { drawStartPos.Y += textPadding paddedDrawStartPos := *drawStartPos - //Draw lines - linesToDraw := int(winSize.Y / e.LineHeight) - // println("Lines to draw:", linesToDraw) - + //Draw text dl := imgui.WindowDrawList() + linesToDraw := int(winSize.Y / e.LineHeight) startLine := clampInt(int(e.StartPos), 0, e.LineCount) 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 += 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. //Window coords are as reported by SDL, but we correct for padding and snap to the nearest //char window pos. @@ -107,36 +144,34 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) { // //'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 - 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)) - clickedColWindowXGlobal := clampInt(int(roundedMouseX), 0, math.MaxInt) - clickedColGridXEditor := int( + gridXGlobal := clampInt(int(roundedMouseX), 0, math.MaxInt) + + roundedMouseY := e.RoundToGrid(float32(e.MouseY)) + gridYGlobal := clampInt(int(roundedMouseY), 0, math.MaxInt) + + gridXEditor := int( roundF32( - (float32(clickedColWindowXGlobal) - paddedDrawStartPos.X) / e.CharWidth, - )) + (float32(gridXGlobal) - paddedDrawStartPos.X) / e.CharWidth, + ), + ) - //Draw cursor - clickedLine := e.GetLine(startLine + clickedColGridYEditor) - tabCount, charsToOffsetBy := getTabs(clickedLine, clickedColGridXEditor) - println("!!", charsToOffsetBy) + windowYEditor := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, math.MaxInt) + gridYEditor := clampInt(windowYEditor/int(e.LineHeight), 0, e.LineCount) - textWidth := float32(len(clickedLine.chars)-tabCount+tabCount*settings.TabSize) * e.CharWidth - lineX := clampF32(float32(clickedColWindowXGlobal)+float32(charsToOffsetBy)*e.CharWidth, 0, paddedDrawStartPos.X+textWidth) + startLineIndex := clampInt(int(e.StartPos), 0, e.LineCount) + return mousePosInfo{ - lineStart := imgui.Vec2{ - X: lineX, - Y: paddedDrawStartPos.Y + float32(clickedColGridYEditor)*e.LineHeight - e.LineHeight*0.25, + GridXGlobal: gridXGlobal, + GridYGlobal: gridYGlobal, + + 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 { diff --git a/main.go b/main.go index ed60151..dd9f63f 100755 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ type Gopad struct { editorToClose int activeEditor int lastActiveEditor int + newRunes []rune //Errors haveErr bool @@ -76,6 +77,7 @@ func main() { editors: []Editor{*NewScratchEditor()}, editorToClose: -1, sidebarWidthFactor: 0.15, + newRunes: []rune{}, } // engine.SetVSync(true) @@ -137,6 +139,7 @@ func (g *Gopad) handleWindowEvents(event sdl.Event) { case *sdl.TextEditingEvent: case *sdl.TextInputEvent: + g.newRunes = append(g.newRunes, []rune(e.GetText())...) case *sdl.WindowEvent: if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED { w, h := g.Win.SDLWin.GetSize() @@ -191,8 +194,9 @@ func (g *Gopad) Update() { g.editorToClose = -1 } - //Save if needed e := g.getActiveEditor() + + //Save if needed if !e.IsModified { return } @@ -352,8 +356,11 @@ func (g *Gopad) drawEditors() { tabsHeight := imgui.WindowHeight() imgui.End() - //Draw text area - 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}) + g.getActiveEditor().UpdateAndDraw( + &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 { imgui.SetKeyboardFocusHereV(-1) @@ -438,6 +445,7 @@ func (g *Gopad) handleFileClick(fPath string) { } func (g *Gopad) FrameEnd() { + g.newRunes = []rune{} } func (g *Gopad) ShouldRun() bool {