From 59f3a28ce6c13358d52eaf707d6334fa0a4961c4 Mon Sep 17 00:00:00 2001 From: InventorXtreme Date: Fri, 24 May 2024 03:52:38 -0400 Subject: [PATCH] widget system begins --- frame.go | 97 +++++++++++++++++++++++++++++++++++++++--------- label.go | 54 +++++++++++++++++++++++++++ maintest/main.go | 41 +++++++++++++++++++- other.go | 15 ++++++++ rectwidget.go | 50 +++++++++++++++++++++++++ window.go | 28 +++++++++++++- 6 files changed, 264 insertions(+), 21 deletions(-) create mode 100644 label.go create mode 100644 rectwidget.go diff --git a/frame.go b/frame.go index 1cf181b..ba551b3 100644 --- a/frame.go +++ b/frame.go @@ -3,48 +3,111 @@ package pinwindows import ( // "fmt" + "fmt" + rl "github.com/gen2brain/raylib-go/raylib" ) // frame struct - used for grouping elements together. Will eventually be drawable and autodraw a // list of elements that will be autoreized and managed by the frame type Frame struct { - Texture *rl.RenderTexture2D - Size rl.Vector2 - ScrollH bool - ScrollW bool - ScrollOffsetH float32 - ScrollOffsetW float32 + Texture *rl.RenderTexture2D + Pos rl.Vector2 + Size rl.Vector2 + ParentObject Parent + PackFunc PackInFunc + Children []Child + prevmouseoffset rl.Vector2 } +type PackInFunc func(Parent, []Child) + func NewFrame(w, h int) Frame { texture := rl.LoadRenderTexture(int32(w), int32(h)) rframe := Frame{} + rframe.Pos = rl.Vector2Zero() rframe.Texture = &texture rframe.Size = rl.Vector2{X: float32(w), Y: float32(h)} - return Frame{Texture: &texture, Size: rl.Vector2{X: float32(w), Y: float32(h)}, ScrollW: false, ScrollH: false} + rframe.PackFunc = VerticalSplitFill + return rframe +} + +func (f *Frame) GetSize() rl.Vector2 { + return f.Size +} + +func VerticalSplitFill(parent Parent, children []Child) { + y := 0 + + size := parent.GetSize() + heighteach := int(size.Y) / len(children) + widtheach := size.X + for _, child := range children { + child.SetPos(0, y) + child.SetSize(int(widtheach), heighteach) + y = y + heighteach + } +} + +func (f *Frame) SetSize(w, h int) { + rl.UnloadRenderTexture(*f.Texture) + f.Size.X = float32(w) + f.Size.Y = float32(h) + *f.Texture = rl.LoadRenderTexture(int32(f.Size.X), int32(f.Size.Y)) + + f.PackFunc(f, f.Children) } -func (f *Frame) Resize(w, h int) { - rl.UnloadRenderTexture(*f.Texture) - f.Size.X += float32(w) - f.Size.Y += float32(h) - *f.Texture = rl.LoadRenderTexture(int32(f.Size.X), int32(f.Size.Y)) +func (f *Frame) GetPosition() rl.Vector2 { + return f.Pos +} + +func (f *Frame) SetPosition(x, y int) { + f.Pos.X = float32(x) + f.Pos.Y = float32(y) +} + +func (f *Frame) GetAbsPosition() rl.Vector2 { + return rl.Vector2Add(f.ParentObject.GetAbsPosition(), f.Pos) +} + +func (f *Frame) AddChild(c Child) { + f.Children = append(f.Children, c) + f.PackFunc(f, f.Children) +} + +func (f *Frame) ResizeDelta(w, h int) { + f.SetSize(int(f.GetSize().X+float32(w)), int(f.GetSize().Y+float32(h))) } func (f *Frame) BeginDrawMode() { rl.BeginTextureMode(*f.Texture) - var startposH int32 = int32(0 + f.ScrollOffsetH) - var startposW int32 = int32(0 + f.ScrollOffsetW) - rl.BeginScissorMode(startposW, startposH, int32(f.Size.X-float32(startposW)), int32(f.Size.Y-float32(startposH))) + rl.SetMouseOffset(-int(f.GetAbsPosition().X), -int(f.GetAbsPosition().Y)) } func (f *Frame) EndDrawMode() { - rl.EndScissorMode() rl.EndTextureMode() + rl.SetMouseOffset(0, 0) +} + +func (f *Frame) SetParent(p Parent) { + f.ParentObject = p +} + +func (f *Frame) DrawInternal() { + for _, child := range f.Children { + child.DrawInternal() + } + f.BeginDrawMode() + rl.ClearBackground(rl.White) + for _, child := range f.Children { + fmt.Println(child) + child.Draw() + } + f.EndDrawMode() } func (f *Frame) Draw() { - DrawTextureFlipped(&f.Texture.Texture, 0, 0) + DrawTextureFlipped(&f.Texture.Texture, int32(f.Pos.X), int32(f.Pos.Y)) } diff --git a/label.go b/label.go new file mode 100644 index 0000000..2ed3c4b --- /dev/null +++ b/label.go @@ -0,0 +1,54 @@ +package pinwindows + +import ( + // "fmt" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type Label struct { + ParentObject Parent + Pos rl.Vector2 + Size rl.Vector2 + Text string + Fontsize int + Color rl.Color +} + +func (this *Label) Draw() { + x := this.Pos.X + y := this.Pos.Y + rl.BeginScissorMode(int32(x), int32(y), int32(this.Size.X), int32(this.Size.Y)) + rl.DrawText(this.Text, int32(x), int32(y), int32(this.Fontsize), this.Color) + + rl.EndScissorMode() +} + +func (this *Label) DrawInternal() { + return //no deeper layers +} + +func NewLabel(pa Parent, str string) *Label { + ret := Label{} + ret.ParentObject = pa + ret.Size = rl.NewVector2(0, 0) + ret.Text = str + ret.Fontsize = 12 + ret.Color = rl.Black + pa.AddChild(&ret) + return &ret +} + +func (this *Label) GetSize() rl.Vector2 { + return this.Size +} + +func (this *Label) SetSize(x, y int) { + this.Size.X = float32(x) + this.Size.Y = float32(y) +} + +func (this *Label) SetPos(x, y int) { + this.Pos.X = float32(x) + this.Pos.Y = float32(y) +} diff --git a/maintest/main.go b/maintest/main.go index 174a838..2dee4e3 100644 --- a/maintest/main.go +++ b/maintest/main.go @@ -3,10 +3,9 @@ package main import ( "fmt" + "git.dummkopf.live/InventorX/pinwindows" gui "github.com/gen2brain/raylib-go/raygui" rl "github.com/gen2brain/raylib-go/raylib" - - "git.dummkopf.live/InventorX/pinwindows" ) const RENDERWIDTH = 1920 @@ -42,18 +41,44 @@ func main() { rendertexture := rl.LoadRenderTexture(SCREENWIDTH, SCREENHEIGHT) raywin := pinwindows.NewWindow(100, 100, 200, 200) + framewin := pinwindows.NewWindow(100, 100, 200, 200) + + mainframe := pinwindows.NewFrame(int(framewin.Size.X), int(framewin.Size.Y)) + + framewin.ResizeFunc = func(x, y int) { mainframe.ResizeDelta(x, y) } + mainframe.SetParent(&framewin) + + //_ = pinwindows.NewLabel(&mainframe, "test") + //lbl.Fontsize = 40 + + _ = pinwindows.NewRectWidget(&mainframe, rl.Red) + _ = pinwindows.NewRectWidget(&mainframe, rl.Blue) wm.AddWindow(&raywin) + wm.AddWindow(&framewin) for !rl.WindowShouldClose() { + wm.ManageWindowlist() wm.ProcessUserWindowMovement() raywin.BeginDrawMode() rl.ClearBackground(rl.White) ispressed := gui.Button(rl.NewRectangle(0, 0, 50, 50), "new") + //rl.DrawText(strconv.FormatFloat(float64(mainframe.ScrollOffsetH), 'f', -1, 32), 60, 0, 11, rl.Black) raywin.EndDrawMode() + mainframe.DrawInternal() + + //mainframe.BeginDrawMode() + //rl.ClearBackground(rl.White) + //framebutt := gui.Button(rl.NewRectangle(0, 0, 50, 50), "skib") + //mainframe.EndDrawMode() + + framewin.BeginDrawMode() + mainframe.Draw() + framewin.EndDrawMode() + if ispressed { newwindow := pinwindows.NewWindow(50, 50, 200, 200) newwindow.DrawBorder = true @@ -61,6 +86,18 @@ func main() { wm.AddWindow(&newwindow) } + // if framebutt { + // fmt.Println("skib") + // newwindow := pinwindows.NewWindow(50, 50, 200, 200) + // newwindow.DrawBorder = true + // newwindow.DrawFunc = func(w *pinwindows.Window) { + // w.BeginDrawMode() + // rl.ClearBackground(rl.Red) + // w.EndDrawMode() + // } + // wm.AddWindow(&newwindow) + // } + pinwindows.UpdateRenderTexture(&rendertexture) wm.DrawInternalWindowList() diff --git a/other.go b/other.go index 9394318..df0e1a4 100644 --- a/other.go +++ b/other.go @@ -12,3 +12,18 @@ func UpdateRenderTexture(texture *rl.RenderTexture2D) { *texture = rl.LoadRenderTexture(int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight())) } } + +type Parent interface { + GetPosition() rl.Vector2 + GetAbsPosition() rl.Vector2 + GetSize() rl.Vector2 + AddChild(Child) +} + +type Child interface { + Draw() + DrawInternal() + SetPos(int, int) + SetSize(int, int) + GetSize() rl.Vector2 +} diff --git a/rectwidget.go b/rectwidget.go new file mode 100644 index 0000000..05f08c8 --- /dev/null +++ b/rectwidget.go @@ -0,0 +1,50 @@ +package pinwindows + +import ( + // "fmt" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type RectWidget struct { + ParentObject Parent + Pos rl.Vector2 + Size rl.Vector2 + Color rl.Color +} + +func (this *RectWidget) Draw() { + x := this.Pos.X + y := this.Pos.Y + rl.BeginScissorMode(int32(x), int32(y), int32(this.Size.X), int32(this.Size.Y)) + rl.DrawRectangle(int32(x), int32(y), int32(this.Size.X), int32(this.Size.Y), this.Color) + + rl.EndScissorMode() +} + +func (this *RectWidget) DrawInternal() { + return //no deeper layers +} + +func NewRectWidget(pa Parent, co rl.Color) *RectWidget { + ret := RectWidget{} + ret.ParentObject = pa + ret.Size = rl.NewVector2(0, 0) + ret.Color = co + pa.AddChild(&ret) + return &ret +} + +func (this *RectWidget) GetSize() rl.Vector2 { + return this.Size +} + +func (this *RectWidget) SetSize(x, y int) { + this.Size.X = float32(x) + this.Size.Y = float32(y) +} + +func (this *RectWidget) SetPos(x, y int) { + this.Pos.X = float32(x) + this.Pos.Y = float32(y) +} diff --git a/window.go b/window.go index f869b12..3333f38 100644 --- a/window.go +++ b/window.go @@ -11,8 +11,9 @@ type Window struct { Pos rl.Vector2 Size rl.Vector2 DrawBorder bool - DrawFunc windowdrawfunc + DrawFunc WindowDrawFunc ManageFunc managewindowfunc + ResizeFunc WindowResizeDelta AcceptingInput bool } @@ -24,12 +25,30 @@ const ( WINDOW_NO_EVENT = -1 ) -type windowdrawfunc func(*Window) +type WindowDrawFunc func(*Window) + +type WindowResizeDelta func(int, int) func Testfunc() { fmt.Println("erm") } +func (win *Window) GetPosition() rl.Vector2 { + return win.Pos +} + +func (win *Window) GetAbsPosition() rl.Vector2 { + return win.Pos +} + +func (win *Window) GetSize() rl.Vector2 { + return win.Size +} + +func (win *Window) AddChild(c Child) { + return //pass because we do not actually manage children +} + func NewWindow(x, y, w, h int) Window { texture := rl.LoadRenderTexture(int32(w), int32(h)) @@ -49,6 +68,11 @@ func (win *Window) ResizeDelta(x, y int) { win.Size.X += float32(x) win.Size.Y += float32(y) *win.Texture = rl.LoadRenderTexture(int32(win.Size.X), int32(win.Size.Y)) + + if win.ResizeFunc != nil { + win.ResizeFunc(x, y) + } + } func (win *Window) DidStartMoveDrag() bool {