mirror of
https://github.com/bloeys/gopad.git
synced 2025-12-29 06:58:21 +00:00
Saving
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@
|
|||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
imgui.ini
|
||||||
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/bloeys/gopad
|
|||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
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/inkyblackness/imgui-go/v4 v4.3.0
|
||||||
github.com/veandco/go-sdl2 v0.4.14
|
github.com/veandco/go-sdl2 v0.4.14
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@ -1,8 +1,8 @@
|
|||||||
github.com/bloeys/assimp-go v0.4.2/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
|
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 h1:Sy9upW7SBsBfDXrSmEhid3aQ+7J7itej+upwcxOnPMQ=
|
||||||
github.com/bloeys/gglm v0.3.1/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
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.8 h1:HCoEaTBWTucnXrjQ+8OCUTzG/3rjpV1eliXWUW44+FY=
|
||||||
github.com/bloeys/nmage v0.0.7/go.mod h1:4h2tKtMvk9ab8r/+rem4QonPXEBTho6VWvpCMm0M6iM=
|
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 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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=
|
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
|
||||||
|
|||||||
15
imgui.ini
15
imgui.ini
@ -10,16 +10,21 @@ Collapsed=0
|
|||||||
|
|
||||||
[Window][sidebar]
|
[Window][sidebar]
|
||||||
Pos=0,0
|
Pos=0,0
|
||||||
Size=307,1657
|
Size=128,720
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][editor]
|
[Window][editor]
|
||||||
Pos=307,0
|
Pos=128,0
|
||||||
Size=2764,38
|
Size=1152,38
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][editorText]
|
[Window][editorText]
|
||||||
Pos=307,38
|
Pos=128,38
|
||||||
Size=2764,1619
|
Size=1152,682
|
||||||
|
Collapsed=0
|
||||||
|
|
||||||
|
[Window][err]
|
||||||
|
Pos=36,-20
|
||||||
|
Size=3072,1657
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
|
|||||||
90
main.go
90
main.go
@ -20,6 +20,7 @@ type Editor struct {
|
|||||||
fileName string
|
fileName string
|
||||||
filePath string
|
filePath string
|
||||||
fileContents string
|
fileContents string
|
||||||
|
isModified bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Gopad struct {
|
type Gopad struct {
|
||||||
@ -37,6 +38,14 @@ type Gopad struct {
|
|||||||
editorToClose int
|
editorToClose int
|
||||||
activeEditor int
|
activeEditor int
|
||||||
lastActiveEditor int
|
lastActiveEditor int
|
||||||
|
|
||||||
|
//Errors
|
||||||
|
haveErr bool
|
||||||
|
errMsg string
|
||||||
|
|
||||||
|
//Settings
|
||||||
|
textSelectionColor imgui.Vec4
|
||||||
|
editorBgColor imgui.Vec4
|
||||||
}
|
}
|
||||||
|
|
||||||
var ()
|
var ()
|
||||||
@ -65,7 +74,9 @@ func main() {
|
|||||||
editors: []Editor{
|
editors: []Editor{
|
||||||
{fileName: "**scratch**"},
|
{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)
|
// engine.SetVSync(true)
|
||||||
@ -99,12 +110,10 @@ func (g *Gopad) handleWindowEvents(event sdl.Event) {
|
|||||||
|
|
||||||
case *sdl.TextEditingEvent:
|
case *sdl.TextEditingEvent:
|
||||||
case *sdl.TextInputEvent:
|
case *sdl.TextInputEvent:
|
||||||
// g.buffer = append(g.buffer, []rune(e.GetText())...)
|
|
||||||
|
|
||||||
case *sdl.WindowEvent:
|
case *sdl.WindowEvent:
|
||||||
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
||||||
w, _ := g.Win.SDLWin.GetSize()
|
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
|
g.Quitting = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if x, _ := input.GetMousePos(); x > int32(g.sidebarSize) && input.MouseClicked(sdl.BUTTON_LEFT) {
|
if g.haveErr {
|
||||||
sdl.StartTextInput()
|
g.showErrorPopup()
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.KeyClicked(sdl.K_ESCAPE) {
|
e := g.getActiveEditor()
|
||||||
sdl.StopTextInput()
|
if !e.isModified {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if input.KeyClicked(sdl.K_BACKSPACE) && len(g.buffer) > 0 {
|
if !input.KeyDown(sdl.K_LCTRL) || !input.KeyClicked(sdl.K_s) {
|
||||||
// g.buffer = g.buffer[:len(g.buffer)-1]
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if input.KeyClicked(sdl.K_RETURN) || input.KeyClicked(sdl.K_RETURN2) {
|
if e.fileName == "**scratch**" {
|
||||||
// g.buffer = append(g.buffer, rune('\n'))
|
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() {
|
func (g *Gopad) Render() {
|
||||||
@ -202,6 +247,10 @@ func (g *Gopad) drawEditors() {
|
|||||||
flags = imgui.TabItemFlagsSetSelected
|
flags = imgui.TabItemFlagsSetSelected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if e.isModified {
|
||||||
|
flags |= imgui.TabItemFlagsUnsavedDocument
|
||||||
|
}
|
||||||
|
|
||||||
open := true
|
open := true
|
||||||
if !imgui.BeginTabItemV(e.fileName, &open, flags) {
|
if !imgui.BeginTabItemV(e.fileName, &open, flags) {
|
||||||
|
|
||||||
@ -237,16 +286,19 @@ func (g *Gopad) drawEditors() {
|
|||||||
imgui.SetNextWindowSize(fullWinSize)
|
imgui.SetNextWindowSize(fullWinSize)
|
||||||
imgui.BeginV("editorText", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
|
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
|
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.PopStyleColor()
|
||||||
imgui.End()
|
imgui.End()
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (g *Gopad) textEditCB(d imgui.InputTextCallbackData) int32 {
|
func (g *Gopad) textEditCB(d imgui.InputTextCallbackData) int32 {
|
||||||
// return 0
|
g.getActiveEditor().isModified = true
|
||||||
// }
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func (g *Gopad) getActiveEditor() *Editor {
|
func (g *Gopad) getActiveEditor() *Editor {
|
||||||
return g.getEditor(g.activeEditor)
|
return g.getEditor(g.activeEditor)
|
||||||
|
|||||||
Reference in New Issue
Block a user