mirror of
https://github.com/bloeys/gopad.git
synced 2025-12-29 06:58:21 +00:00
Basic sidebar
This commit is contained in:
88
main.go
88
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,12 +12,19 @@ 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
|
||||
sidebarSize float32
|
||||
|
||||
CurrDir string
|
||||
CurrDirContents []fs.DirEntry
|
||||
|
||||
buffer []rune
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user