This commit is contained in:
bloeys
2022-02-24 07:23:59 +04:00
parent 1a5af7512a
commit a6abed0d35
8 changed files with 242 additions and 0 deletions

132
main.go Normal file
View File

@ -0,0 +1,132 @@
package main
import (
"github.com/bloeys/nmage/engine"
"github.com/bloeys/nmage/input"
"github.com/bloeys/nmage/logging"
nmageimgui "github.com/bloeys/nmage/ui/imgui"
"github.com/inkyblackness/imgui-go/v4"
"github.com/veandco/go-sdl2/sdl"
)
type Gopad struct {
Win *engine.Window
ImGUIInfo nmageimgui.ImguiInfo
Quitting bool
mainFont imgui.Font
buffer []rune
}
var ()
func main() {
if err := engine.Init(); err != nil {
panic(err)
}
window, err := engine.CreateOpenGLWindowCentered("nMage", 1280, 720, engine.WindowFlags_RESIZABLE)
if err != nil {
logging.ErrLog.Fatalln("Failed to create window. Err: ", err)
}
defer window.Destroy()
g := Gopad{
Win: window,
ImGUIInfo: nmageimgui.NewImGUI(),
}
engine.Run(&g)
}
func (g *Gopad) Init() {
g.Win.EventCallbacks = append(g.Win.EventCallbacks, g.handleWindowEvents)
g.mainFont = g.ImGUIInfo.AddFontTTF("./res/fonts/courier-new.ttf", 24)
}
func (g *Gopad) handleWindowEvents(event sdl.Event) {
switch e := event.(type) {
case *sdl.TextEditingEvent:
case *sdl.TextInputEvent:
g.buffer = append(g.buffer, []rune(e.GetText())...)
}
}
func (g *Gopad) FrameStart() {
}
func (g *Gopad) Update() {
if input.IsQuitClicked() {
g.Quitting = true
}
if x, y := input.GetMousePos(); x > 0 && y > 0 && input.MouseClicked(sdl.BUTTON_LEFT) {
println("Start")
sdl.StartTextInput()
}
if input.KeyClicked(sdl.K_ESCAPE) {
println("End")
sdl.StopTextInput()
}
if input.KeyClicked(sdl.K_BACKSPACE) && len(g.buffer) > 0 {
g.buffer = g.buffer[:len(g.buffer)-1]
}
if input.KeyClicked(sdl.K_RETURN) || input.KeyClicked(sdl.K_RETURN2) {
g.buffer = append(g.buffer, rune('\n'))
}
}
func (g *Gopad) Render() {
open := true
w, h := g.Win.SDLWin.GetSize()
sidebarSize := float32(w) * 0.25
//Global imgui settings
imgui.PushStyleColor(imgui.StyleColorText, imgui.Vec4{X: 1, Y: 0, Z: 1, W: 1})
imgui.PushFont(g.mainFont)
//Sidebar
imgui.SetNextWindowPos(imgui.Vec2{X: 0, Y: 0})
imgui.SetNextWindowSize(imgui.Vec2{X: sidebarSize, Y: float32(h)})
imgui.BeginV("sidebar", &open, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
imgui.End()
//Text area
imgui.SetNextWindowPos(imgui.Vec2{X: sidebarSize, Y: 0})
imgui.SetNextWindowSize(imgui.Vec2{X: float32(w) - sidebarSize, Y: float32(h)})
imgui.BeginV("editor", &open, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
imgui.Text(string(g.buffer))
imgui.End()
imgui.PopFont()
imgui.PopStyleColor()
}
func (g *Gopad) FrameEnd() {
}
func (g *Gopad) ShouldRun() bool {
return !g.Quitting
}
func (g *Gopad) GetWindow() *engine.Window {
return g.Win
}
func (g *Gopad) GetImGUI() nmageimgui.ImguiInfo {
return g.ImGUIInfo
}
func (g *Gopad) Deinit() {
g.Win.Destroy()
}