diff --git a/main.go b/main.go index f237640..0fab1db 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,9 @@ package main import ( + "io/fs" + "os" + "github.com/bloeys/nmage/engine" "github.com/bloeys/nmage/input" "github.com/bloeys/nmage/logging" @@ -9,13 +12,20 @@ import ( "github.com/veandco/go-sdl2/sdl" ) +//TODO: Cache os.ReadDir so we don't have to use lots of disk + type Gopad struct { Win *engine.Window + mainFont imgui.Font ImGUIInfo nmageimgui.ImguiInfo Quitting bool - mainFont imgui.Font - buffer []rune + sidebarSize float32 + + CurrDir string + CurrDirContents []fs.DirEntry + + buffer []rune } var () @@ -32,10 +42,16 @@ func main() { } defer window.Destroy() + dir, err := os.Getwd() + if err != nil { + panic(err) + } + g := Gopad{ Win: window, ImGUIInfo: nmageimgui.NewImGUI(), buffer: make([]rune, 0, 10000), + CurrDir: dir, } engine.Run(&g) @@ -45,14 +61,20 @@ func (g *Gopad) Init() { g.Win.EventCallbacks = append(g.Win.EventCallbacks, g.handleWindowEvents) + //Setup font var fontSize float32 = 16 - fConfig := imgui.NewFontConfig() defer fConfig.Delete() fConfig.SetOversampleH(2) fConfig.SetOversampleV(2) g.mainFont = g.ImGUIInfo.AddFontTTF("./res/fonts/courier-prime.regular.ttf", fontSize, &fConfig, nil) + + //Sidebar + g.CurrDirContents = getDirContents(g.CurrDir) + + w, _ := g.Win.SDLWin.GetSize() + g.sidebarSize = float32(w) * 0.10 } func (g *Gopad) handleWindowEvents(event sdl.Event) { @@ -62,6 +84,12 @@ 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 + } } } @@ -74,13 +102,11 @@ func (g *Gopad) Update() { g.Quitting = true } - if x, y := input.GetMousePos(); x > 0 && y > 0 && input.MouseClicked(sdl.BUTTON_LEFT) { - println("Start") + if x, _ := input.GetMousePos(); x > int32(g.sidebarSize) && input.MouseClicked(sdl.BUTTON_LEFT) { sdl.StartTextInput() } if input.KeyClicked(sdl.K_ESCAPE) { - println("End") sdl.StopTextInput() } @@ -97,7 +123,6 @@ func (g *Gopad) Render() { open := true w, h := g.Win.SDLWin.GetSize() - sidebarSize := float32(w) * 0.10 //Global imgui settings imgui.PushStyleColor(imgui.StyleColorText, imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}) @@ -105,13 +130,16 @@ func (g *Gopad) Render() { //Sidebar imgui.SetNextWindowPos(imgui.Vec2{X: 0, Y: 0}) - imgui.SetNextWindowSize(imgui.Vec2{X: sidebarSize, Y: float32(h)}) + imgui.SetNextWindowSize(imgui.Vec2{X: g.sidebarSize, Y: float32(h)}) imgui.BeginV("sidebar", &open, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove) + + g.refreshSidebar() + imgui.End() //Text area - imgui.SetNextWindowPos(imgui.Vec2{X: sidebarSize, Y: 0}) - imgui.SetNextWindowSize(imgui.Vec2{X: float32(w) - sidebarSize, Y: float32(h)}) + imgui.SetNextWindowPos(imgui.Vec2{X: g.sidebarSize, Y: 0}) + imgui.SetNextWindowSize(imgui.Vec2{X: float32(w) - g.sidebarSize, Y: float32(h)}) imgui.BeginV("editor", &open, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove) imgui.Text(string(g.buffer) + "|") imgui.End() @@ -120,6 +148,38 @@ func (g *Gopad) Render() { imgui.PopStyleColor() } +func (g *Gopad) refreshSidebar() { + + for i := 0; i < len(g.CurrDirContents); i++ { + + c := g.CurrDirContents[i] + if c.IsDir() { + drawDir(c, g.CurrDir+"/"+c.Name()+"/") + } else { + imgui.Button(c.Name()) + } + } +} + +func drawDir(dir fs.DirEntry, path string) { + + isEnabled := imgui.TreeNode(dir.Name()) + if !isEnabled { + return + } + + contents := getDirContents(path) + for _, c := range contents { + if c.IsDir() { + drawDir(c, path+c.Name()+"/") + } else { + imgui.Button(c.Name()) + } + } + + imgui.TreePop() +} + func (g *Gopad) FrameEnd() { } @@ -139,3 +199,13 @@ func (g *Gopad) GetImGUI() nmageimgui.ImguiInfo { func (g *Gopad) Deinit() { g.Win.Destroy() } + +func getDirContents(dir string) []fs.DirEntry { + + contents, err := os.ReadDir(dir) + if err != nil { + panic(err) + } + + return contents +}