pinwindows/windowmanager.go
2024-04-21 20:29:11 -04:00

117 lines
3.4 KiB
Go

package pinwindows
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
type managewindowfunc func(*RayGuiWindowManager, *RayGuiWindow)
type RayGuiWindowManager struct {
winlist []*RayGuiWindow
movingwindow *RayGuiWindow // the currently moving (resize, drag, etc...)
movingwindowmode int
}
func NewRayGuiWindowManager() RayGuiWindowManager {
var windowlist []*RayGuiWindow
return RayGuiWindowManager{winlist: windowlist, movingwindow: nil}
}
func (winman *RayGuiWindowManager) ManageWindowlist() {
var windowtomovetofront *RayGuiWindow = nil
for i := 0; i < len(winman.winlist); i++ {
if winman.winlist[i].isLeftClicked() {
windowtomovetofront = winman.winlist[i]
}
}
if windowtomovetofront != nil {
windowtomovetofront.ManageFunc = MoveToTopOfWindowlist
}
for i := 0; i < len(winman.winlist); i++ {
if winman.winlist[i].ManageFunc != nil {
winman.winlist[i].ManageFunc(winman, winman.winlist[i])
}
}
}
func (winman *RayGuiWindowManager) ProcessUserWindowMovement() {
currentmovingwindow := winman.movingwindow
for i := 0; i < len(winman.winlist); i++ {
if winman.winlist[i].DidStartMoveDrag() && currentmovingwindow == nil {
winman.movingwindow = winman.winlist[i]
winman.movingwindowmode = WINDOW_MODE_DRAG
}
if winman.winlist[i].DidStartResizeDrag() && currentmovingwindow == nil {
winman.movingwindow = winman.winlist[i]
winman.movingwindowmode = WINDOW_MODE_RESZIE
}
if rl.IsMouseButtonReleased(rl.MouseButtonLeft) || rl.IsMouseButtonReleased(rl.MouseButtonRight) {
winman.movingwindow = nil
}
}
if winman.movingwindow != nil {
if winman.movingwindowmode == WINDOW_MODE_DRAG {
winman.movingwindow.Pos.X += (rl.GetMouseDelta().X)
winman.movingwindow.Pos.Y += (rl.GetMouseDelta().Y)
}
if winman.movingwindowmode == WINDOW_MODE_RESZIE {
winman.movingwindow.Resize(int(rl.GetMouseDelta().X), int(rl.GetMouseDelta().Y))
}
}
}
func (winman RayGuiWindowManager) DrawInternalWindowList() {
for i := 0; i < len(winman.winlist); i++ {
winman.winlist[i].DrawInternals()
}
}
func (winman RayGuiWindowManager) DrawWindowlist() {
for i := 0; i < len(winman.winlist); i++ {
winman.winlist[i].Draw()
}
}
func (winman *RayGuiWindowManager) WinlistUnlistWindow(windowtoremove *RayGuiWindow) {
indextoremove := -1
for i := 0; i < len(winman.winlist); i++ {
if winman.winlist[i] == windowtoremove {
indextoremove = i
break
}
}
if indextoremove == -1 {
return
}
copy(winman.winlist[indextoremove:], winman.winlist[indextoremove+1:])
winman.winlist[len(winman.winlist)-1] = nil
winman.winlist = winman.winlist[:len(winman.winlist)-1]
return
}
func CloseWindow(winman *RayGuiWindowManager, windowtoremove *RayGuiWindow) {
winman.WinlistUnlistWindow(windowtoremove)
rl.UnloadRenderTexture(*windowtoremove.Texture)
if winman.movingwindow == windowtoremove {
winman.movingwindow = nil
}
return
}
func (winman *RayGuiWindowManager) AddWindow(windowtoadd *RayGuiWindow) {
if len(winman.winlist) > 0 {
winman.winlist[len(winman.winlist)-1].AcceptingInput = false
}
winman.winlist = append(winman.winlist, windowtoadd)
winman.winlist[len(winman.winlist)-1].AcceptingInput = true
}
func MoveToTopOfWindowlist(winman *RayGuiWindowManager, windowtomove *RayGuiWindow) {
winman.winlist[len(winman.winlist)-1].AcceptingInput = false
winman.WinlistUnlistWindow(windowtomove)
winman.AddWindow(windowtomove)
windowtomove.ManageFunc = nil
}