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