Skip to content

Commit

Permalink
app: save position correctly
Browse files Browse the repository at this point in the history
respect viewport pos everywhere
  • Loading branch information
gucio321 committed Nov 28, 2024
1 parent 16031d8 commit 3b98e6c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
28 changes: 20 additions & 8 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"github.com/AllenDang/cimgui-go/imgui"
"github.com/gucio321/HellSpawner/pkg/app/config"
"log"
"os"
Expand Down Expand Up @@ -139,10 +140,8 @@ func (a *App) Run() (err error) {
}()
}

err = a.setup()
if err != nil {
return err
}
dialog.Init()
a.setupMasterWindow()

a.masterWindow.Run(a.render)

Expand All @@ -151,11 +150,20 @@ func (a *App) Run() (err error) {

func (a *App) render() {
// unfortunately can't do that in Run as this requires imgui.MainViewport
if a.justStarted && a.config.OpenMostRecentOnStartup && len(a.config.RecentProjects) > 0 {
err := a.loadProjectFromFile(a.config.RecentProjects[0])
if a.justStarted {
a.justStarted = false

fmt.Println("start setup")
err := a.setup()
if err != nil {
logErr("could not load most recent project on startup: %v", err)
logErr("could not set up application: %v", err)
}

if a.config.OpenMostRecentOnStartup && len(a.config.RecentProjects) > 0 {
err = a.loadProjectFromFile(a.config.RecentProjects[0])
if err != nil {
logErr("could not load most recent project on startup: %v", err)
}
}
}

Expand Down Expand Up @@ -229,11 +237,15 @@ func (a *App) openEditor(path *common.PathEntry) {

a.editorManagerMutex.RUnlock()

//since we sue multiviewport, we need to get base position of the main window - we want an editor
// inside window
basePos := imgui.MainViewport().Pos()

// w, h = 0, because we're creating a new editor,
// width and height aren't saved, so we give 0 and
// editors without AutoResize flag sets w, h to default
a.editorManagerMutex.Lock()
a.createEditor(path, nil, editorWindowDefaultX, editorWindowDefaultY, 0, 0)
a.createEditor(path, nil, editorWindowDefaultX+basePos.X, editorWindowDefaultY+basePos.Y, 0, 0)
a.editorManagerMutex.Unlock()
}

Expand Down
19 changes: 11 additions & 8 deletions pkg/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package app

import (
"fmt"
"github.com/AllenDang/cimgui-go/imgui"
"github.com/gucio321/HellSpawner/pkg/app/assets"
"image/color"
"log"
"strconv"
"time"

"github.com/OpenDiablo2/dialog"

"github.com/faiface/beep"
"github.com/faiface/beep/speaker"

Expand Down Expand Up @@ -41,9 +40,6 @@ import (
)

func (a *App) setup() (err error) {
dialog.Init()

a.setupMasterWindow()
a.setupConsole()
a.setupAutoSave()
a.registerGlobalKeyboardShortcuts()
Expand Down Expand Up @@ -138,7 +134,11 @@ func (a *App) registerEditors() {
}

func (a *App) setupMainMpqExplorer() error {
window, err := mpqexplorer.Create(a.openEditor, a.config, mpqExplorerDefaultX, mpqExplorerDefaultY)
// normalization
basePos := imgui.MainViewport().Pos()

fmt.Println(basePos)
window, err := mpqexplorer.Create(a.openEditor, a.config, mpqExplorerDefaultX+basePos.X, mpqExplorerDefaultY+basePos.Y)
if err != nil {
return fmt.Errorf("error creating a MPQ explorer: %w", err)
}
Expand All @@ -149,9 +149,12 @@ func (a *App) setupMainMpqExplorer() error {
}

func (a *App) setupProjectExplorer() error {
x, y := float32(projectExplorerDefaultX), float32(projectExplorerDefaultY)
basePos := imgui.MainViewport().Pos()

window, err := projectexplorer.Create(a.openEditor, x, y)
window, err := projectexplorer.Create(
a.openEditor,
projectExplorerDefaultX+basePos.X, projectExplorerDefaultY+basePos.Y,
)
if err != nil {
return fmt.Errorf("error creating a project explorer: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/window/toolwindow/toolwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ToolWindow interface {
SetVisible(bool)
BringToFront()
State() state.ToolWindowState
Pos(x, y float32) *giu.WindowWidget
Pos(x, y float32) *window.Window
Size(float32, float32) *giu.WindowWidget
CurrentSize() (float32, float32)
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/window/window.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package window

import (
"github.com/AllenDang/cimgui-go/imgui"
"github.com/AllenDang/giu"
"github.com/gucio321/HellSpawner/pkg/app/state"
)
Expand All @@ -14,9 +13,9 @@ type Window struct {

// New creates new window
func New(title string, x, y float32) *Window {
return &Window{
WindowWidget: giu.Window(title).Pos(x, y),
}
return (&Window{
WindowWidget: giu.Window(title),
}).Pos(x, y)
}

// State returns window's state
Expand Down Expand Up @@ -73,8 +72,7 @@ func (t *Window) Cleanup() {
t.Visible = false
}

func (t *Window) Pos(x, y float32) *giu.WindowWidget {
// normalize this by main viewports pos
pos0 := imgui.MainViewport().Pos()
return t.WindowWidget.Pos(x+pos0.X, y+pos0.Y)
func (t *Window) Pos(x, y float32) *Window {
t.WindowWidget.Pos(x, y)
return t
}

0 comments on commit 3b98e6c

Please sign in to comment.