diff --git a/.gitignore b/.gitignore index 66fd13c..46ea32c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +imgui.ini \ No newline at end of file diff --git a/README.md b/README.md index f1abdf1..63cf85c 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# gopad \ No newline at end of file +# gopad + +Notepad in Golang and using nMage and ImGUI. diff --git a/go.mod b/go.mod index bb6496a..a2c39cc 100755 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bloeys/gopad go 1.17 require ( - github.com/bloeys/nmage v0.0.7 + github.com/bloeys/nmage v0.0.8 github.com/inkyblackness/imgui-go/v4 v4.3.0 github.com/veandco/go-sdl2 v0.4.14 ) diff --git a/go.sum b/go.sum index bf10c40..0812842 100755 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/bloeys/assimp-go v0.4.2/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0= github.com/bloeys/gglm v0.3.1 h1:Sy9upW7SBsBfDXrSmEhid3aQ+7J7itej+upwcxOnPMQ= github.com/bloeys/gglm v0.3.1/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk= -github.com/bloeys/nmage v0.0.7 h1:MXl/sLWq1e+HLX5X8KbdNb3pbMroQEyuvvltT4HgAt8= -github.com/bloeys/nmage v0.0.7/go.mod h1:4h2tKtMvk9ab8r/+rem4QonPXEBTho6VWvpCMm0M6iM= +github.com/bloeys/nmage v0.0.8 h1:HCoEaTBWTucnXrjQ+8OCUTzG/3rjpV1eliXWUW44+FY= +github.com/bloeys/nmage v0.0.8/go.mod h1:4h2tKtMvk9ab8r/+rem4QonPXEBTho6VWvpCMm0M6iM= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= diff --git a/imgui.ini b/imgui.ini index f0429c9..186e936 100755 --- a/imgui.ini +++ b/imgui.ini @@ -10,16 +10,21 @@ Collapsed=0 [Window][sidebar] Pos=0,0 -Size=307,1657 +Size=128,720 Collapsed=0 [Window][editor] -Pos=307,0 -Size=2764,38 +Pos=128,0 +Size=1152,38 Collapsed=0 [Window][editorText] -Pos=307,38 -Size=2764,1619 +Pos=128,38 +Size=1152,682 +Collapsed=0 + +[Window][err] +Pos=36,-20 +Size=3072,1657 Collapsed=0 diff --git a/main.go b/main.go index 6e48ece..2161cd6 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ type Editor struct { fileName string filePath string fileContents string + isModified bool } type Gopad struct { @@ -37,6 +38,14 @@ type Gopad struct { editorToClose int activeEditor int lastActiveEditor int + + //Errors + haveErr bool + errMsg string + + //Settings + textSelectionColor imgui.Vec4 + editorBgColor imgui.Vec4 } var () @@ -65,7 +74,9 @@ func main() { editors: []Editor{ {fileName: "**scratch**"}, }, - editorToClose: -1, + editorToClose: -1, + textSelectionColor: imgui.Vec4{X: 84 / 255.0, Y: 153 / 255.0, Z: 199 / 255.0, W: 0.4}, + editorBgColor: imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}, } // engine.SetVSync(true) @@ -99,12 +110,10 @@ func (g *Gopad) handleWindowEvents(event sdl.Event) { case *sdl.TextEditingEvent: case *sdl.TextInputEvent: - // g.buffer = append(g.buffer, []rune(e.GetText())...) - case *sdl.WindowEvent: if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED { w, _ := g.Win.SDLWin.GetSize() - g.sidebarSize = float32(w) * 0.10 + g.sidebarSize = float32(w) * 0.15 } } } @@ -132,21 +141,57 @@ func (g *Gopad) Update() { g.Quitting = true } - if x, _ := input.GetMousePos(); x > int32(g.sidebarSize) && input.MouseClicked(sdl.BUTTON_LEFT) { - sdl.StartTextInput() + if g.haveErr { + g.showErrorPopup() } - if input.KeyClicked(sdl.K_ESCAPE) { - sdl.StopTextInput() + e := g.getActiveEditor() + if !e.isModified { + return } - // if input.KeyClicked(sdl.K_BACKSPACE) && len(g.buffer) > 0 { - // g.buffer = g.buffer[:len(g.buffer)-1] - // } + if !input.KeyDown(sdl.K_LCTRL) || !input.KeyClicked(sdl.K_s) { + return + } - // if input.KeyClicked(sdl.K_RETURN) || input.KeyClicked(sdl.K_RETURN2) { - // g.buffer = append(g.buffer, rune('\n')) - // } + if e.fileName == "**scratch**" { + e.isModified = false + return + } + + err := os.WriteFile(e.filePath, []byte(e.fileContents), os.ModePerm) + if err != nil { + g.fireError("Failed to save file. Error: " + err.Error()) + return + } + + e.isModified = false +} + +func (g *Gopad) fireError(errMsg string) { + imgui.OpenPopup("err") + g.haveErr = true + g.errMsg = errMsg +} + +func (g *Gopad) showErrorPopup() { + + w, h := g.Win.SDLWin.GetSize() + imgui.SetNextWindowPos(imgui.Vec2{X: float32(w) * 0.5, Y: float32(h) * 0.5}) + + shouldEnd := imgui.BeginPopup("err") + + imgui.Text(g.errMsg) + if imgui.Button("OK") { + g.haveErr = false + imgui.CloseCurrentPopup() + } + + if shouldEnd { + imgui.EndPopup() + } else { + g.haveErr = false + } } func (g *Gopad) Render() { @@ -202,6 +247,10 @@ func (g *Gopad) drawEditors() { flags = imgui.TabItemFlagsSetSelected } + if e.isModified { + flags |= imgui.TabItemFlagsUnsavedDocument + } + open := true if !imgui.BeginTabItemV(e.fileName, &open, flags) { @@ -237,16 +286,19 @@ func (g *Gopad) drawEditors() { imgui.SetNextWindowSize(fullWinSize) imgui.BeginV("editorText", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove) - imgui.PushStyleColor(imgui.StyleColorFrameBg, imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}) + imgui.PushStyleColor(imgui.StyleColorFrameBg, g.editorBgColor) + imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, g.textSelectionColor) fullWinSize.Y -= 18 - imgui.InputTextMultilineV("", &g.getActiveEditor().fileContents, fullWinSize, 0, nil) + imgui.InputTextMultilineV("", &g.getActiveEditor().fileContents, fullWinSize, imgui.ImGuiInputTextFlagsCallbackEdit|imgui.InputTextFlagsAllowTabInput, g.textEditCB) + imgui.PopStyleColor() imgui.PopStyleColor() imgui.End() } -// func (g *Gopad) textEditCB(d imgui.InputTextCallbackData) int32 { -// return 0 -// } +func (g *Gopad) textEditCB(d imgui.InputTextCallbackData) int32 { + g.getActiveEditor().isModified = true + return 0 +} func (g *Gopad) getActiveEditor() *Editor { return g.getEditor(g.activeEditor)