From 1fd13fa66f1cb37c28bbe9ab7fb426581aa9ca88 Mon Sep 17 00:00:00 2001 From: InventorXtreme Date: Sun, 21 Apr 2024 00:14:03 -0400 Subject: [PATCH] first --- go.mod | 10 +++++ go.sum | 6 +++ other.go | 7 +++ window.go | 101 +++++++++++++++++++++++++++++++++++++++++ windowmanager.go | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 239 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 other.go create mode 100644 window.go create mode 100644 windowmanager.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..141bfa9 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module git.dummkopf.live/InventorX/pinwindows + +go 1.22.2 + +require github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6 + +require ( + github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa // indirect + golang.org/x/sys v0.14.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6276b23 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa h1:Ik7QikRgeH+bFOfAcMpttCbs6XxWXxCLXMm4awxtOXk= +github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= +github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6 h1:mNKFgLZIU0eEHKHjb7Uk9ZuSy65DdgmEf2xxum0Tof4= +github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6/go.mod h1:P/hDjVwz/9fhR0ww3+umzDpDA7Bf7Tce4xNChHIEFqE= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/other.go b/other.go new file mode 100644 index 0000000..30ed308 --- /dev/null +++ b/other.go @@ -0,0 +1,7 @@ +package pinwindows + +import rl "github.com/gen2brain/raylib-go/raylib" + +func DrawTextureFlipped(rentex *rl.Texture2D, posx, posy int32) { + rl.DrawTexturePro(*rentex, rl.NewRectangle(0, 0, float32(rentex.Width), -float32(rentex.Height)), rl.NewRectangle(float32(posx), float32(posy), float32(rentex.Width), float32(rentex.Height)), rl.NewVector2(0, 0), 0, rl.White) +} diff --git a/window.go b/window.go new file mode 100644 index 0000000..f8e6b1b --- /dev/null +++ b/window.go @@ -0,0 +1,101 @@ +package pinwindows + +import ( + "fmt" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type RayGuiWindow struct { + Texture *rl.RenderTexture2D + X int32 + Y int32 + W int32 + H int32 + DrawBorder bool + DrawFunc windowdrawfunc + ManageFunc managewindowfunc + AcceptingInput bool +} + +const ( + WINDOW_MODE_DRAG = iota + WINDOW_MODE_RESZIE = iota + WINDOW_MODE_NORMAL = iota + + WINDOW_NO_EVENT = -1 +) + +type windowdrawfunc func(*RayGuiWindow) + +func NewRayGuiWindow(x, y, w, h int) RayGuiWindow { + texture := rl.LoadRenderTexture(int32(w), int32(h)) + + return RayGuiWindow{Texture: &texture, X: int32(x), Y: int32(y), W: int32(w), H: int32(h), DrawBorder: true, AcceptingInput: false} +} + +func (win *RayGuiWindow) Resize(x, y int) { + rl.UnloadRenderTexture(*win.Texture) + win.W += int32(x) + win.H += int32(y) + *win.Texture = rl.LoadRenderTexture(int32(win.W), int32(win.H)) +} + +func (win *RayGuiWindow) DidStartMoveDrag() bool { + if win.IsTouchingRegion(rl.GetMousePosition(), rl.NewRectangle(0, -20, float32(win.W), 20)) { + if rl.IsMouseButtonPressed(rl.MouseButtonLeft) { + return true + } + } + return false +} + +func (win *RayGuiWindow) DidStartResizeDrag() bool { + if win.IsTouchingRegion(rl.GetMousePosition(), rl.NewRectangle(0, 0, float32(win.W), float32(win.H))) { + if rl.IsMouseButtonPressed(rl.MouseButtonRight) { + return true + } + } + return false +} + +func (window *RayGuiWindow) isLeftClicked() bool { + return rl.IsMouseButtonPressed(rl.MouseButtonLeft) && rl.CheckCollisionPointRec(rl.GetMousePosition(), rl.NewRectangle(float32(window.X), float32(window.Y-20), float32(window.W), float32(window.H+20))) +} + +func (window *RayGuiWindow) BeginDrawMode() { + rl.BeginTextureMode(*window.Texture) + if window.AcceptingInput { + rl.SetMouseOffset(int(-window.X), int(-window.Y)) + } else { + rl.SetMouseOffset(-1000000, -1000000) + } +} + +func (window *RayGuiWindow) EndDrawMode() { + rl.SetMouseOffset(0, 0) + rl.EndTextureMode() +} + +func (window RayGuiWindow) IsTouchingRegion(posvec rl.Vector2, rect rl.Rectangle) bool { + + if rl.CheckCollisionPointRec(posvec, rl.Rectangle{X: rect.X + float32(window.X), Y: rect.Y + float32(window.Y), Width: rect.Width, Height: rect.Height}) { + return true + } + return false +} + +func (window *RayGuiWindow) Draw() { + rl.DrawRectangle(window.X, window.Y-20, window.W, 20, rl.Gray) + rl.DrawText(fmt.Sprint(window.AcceptingInput), window.X-20, window.Y-20, 12, rl.Blue) + //fmt.Printf("WindowDraw at %i, %i\n", window.X, window.Y) + + DrawTextureFlipped(&window.Texture.Texture, window.X, window.Y) +} + +func (window *RayGuiWindow) DrawInternals() { + if window.DrawFunc != nil { + window.DrawFunc(window) + } + +} diff --git a/windowmanager.go b/windowmanager.go new file mode 100644 index 0000000..b45eb42 --- /dev/null +++ b/windowmanager.go @@ -0,0 +1,115 @@ +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.X += int32(rl.GetMouseDelta().X) + winman.movingwindow.Y += int32(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 +}