From 03e110124873e8d2391a1e687a24fa7e566f5945 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 3 Jun 2024 17:44:17 +0200 Subject: [PATCH] lots changes: - renamed scene files - fixed options back using scene.Prev - fixed initial zooming (finally) - fixed reset zoom (key r) - fixed initial size, now works with state loading as well --- TODO.md | 13 -------- camera.go | 31 +++++++++++++------ config.go | 54 +++++++++++++++++++++++----------- game.go | 1 + scene-menu.go => menu.go | 11 +++++-- scene-options.go => options.go | 9 ++++-- scene-play.go => play.go | 18 ++++++++++-- scene.go | 1 + widgets.go | 4 +-- 9 files changed, 94 insertions(+), 48 deletions(-) rename scene-menu.go => menu.go (92%) rename scene-options.go => options.go (93%) rename scene-play.go => play.go (97%) diff --git a/TODO.md b/TODO.md index 171c165..ff41160 100644 --- a/TODO.md +++ b/TODO.md @@ -2,16 +2,3 @@ - changing options mid-game has no effect in most cases, even after a restart -- Statefile loading does not work correclty anymore. With larger grids - everything is empty. With square grids part of the grid is cut - off. Smaller grids load though - -- Also when loading a state file, centering doesn't work anymore, I - think the geom calculation is overthrown by the parser func. So, put - this calc into its own func and always call. Or - as stated below - - put it onto camera.go and call from Init(). - -- Zoom 0 on reset only works when world= config.ScreenWidth { + // must be positive if world wider than screen + config.InitialCamPos[0] = math.Abs(config.InitialCamPos[0]) + } + + // for Y we need only positive (really?) + if config.Height*config.Cellsize > config.ScreenHeight { + config.InitialCamPos[1] = math.Abs( + float64(((config.ScreenHeight - (config.Height * config.Cellsize)) / 2))) + } + + // Calculate zoom out factor, which shows 100% of the world. We + // need to reverse math.Pow(1.01, $zoomfactor) to get the correct + // percentage of the world to show. I.e: with a ScreenHeight of + // 384px and a world of 800px the factor to show 100% of the world + // is -75: math.Log(384/800) / math.Log(1.01). The 1.01 constant + // is being used in camera.go:worldMatrix(). + + // FIXME: determine if the diff is larger on width, then calc with + // widh instead of height + config.ZoomOutFactor = int( + math.Log(float64(config.ScreenHeight)/(float64(config.Height)*float64(config.Cellsize))) / + math.Log(1.01)) +} + // parse given window geometry and adjust game settings according to it func (config *Config) ParseGeom(geom string) error { // force a geom @@ -73,22 +108,6 @@ func (config *Config) ParseGeom(geom string) error { config.ScreenHeight = height config.Cellsize = DEFAULT_CELLSIZE - config.Zoomfactor = DEFAULT_ZOOMFACTOR - - // calculate the initial cam pos. It is negative if the total grid - // size is smaller than the screen in a centered position, but - // it's zero if it's equal or larger than the screen. - config.InitialCamPos = make([]float64, 2) - - config.InitialCamPos[0] = float64(((config.ScreenWidth - (config.Width * config.Cellsize)) / 2) * -1) - if config.Width*config.Cellsize >= config.ScreenWidth { - // must be positive if world wider than screen - config.InitialCamPos[0] = math.Abs(config.InitialCamPos[0]) - } - - if config.Height*config.Cellsize > config.ScreenHeight { - config.InitialCamPos[1] = math.Abs(float64(((config.ScreenHeight - (config.Height * config.Cellsize)) / 2))) - } return nil } @@ -145,7 +164,6 @@ func (config *Config) ParseStatefile() error { config.Width = grid.Width config.Height = grid.Height - config.Cellsize = config.ScreenWidth / config.Width config.StateGrid = grid return nil @@ -225,6 +243,8 @@ func ParseCommandline() (*Config, error) { config.Rule = ParseGameRule(rule) } + config.SetupCamera() + //repr.Println(config) return &config, nil } diff --git a/game.go b/game.go index 30e032d..9f73464 100644 --- a/game.go +++ b/game.go @@ -53,6 +53,7 @@ func (game *Game) Update() error { next := scene.GetNext() if next != game.CurrentScene { + game.Scenes[next].SetPrevious(game.CurrentScene) scene.ResetNext() game.CurrentScene = next } diff --git a/scene-menu.go b/menu.go similarity index 92% rename from scene-menu.go rename to menu.go index 6d1c385..0c4d8db 100644 --- a/scene-menu.go +++ b/menu.go @@ -14,6 +14,7 @@ type SceneMenu struct { Game *Game Config *Config Next SceneName + Prev SceneName Whoami SceneName Ui *ebitenui.UI FontColor color.RGBA @@ -38,6 +39,10 @@ func (scene *SceneMenu) GetNext() SceneName { return scene.Next } +func (scene *SceneMenu) SetPrevious(prev SceneName) { + scene.Prev = prev +} + func (scene *SceneMenu) ResetNext() { scene.Next = scene.Whoami } @@ -99,9 +104,9 @@ func (scene *SceneMenu) Init() { scene.SetNext(Options) }) - separator1 := NewSeparator() - separator2 := NewSeparator() - separator3 := NewSeparator() + separator1 := NewSeparator(3) + separator2 := NewSeparator(3) + separator3 := NewSeparator(10) cancel := NewMenuButton("Back", func(args *widget.ButtonClickedEventArgs) { diff --git a/scene-options.go b/options.go similarity index 93% rename from scene-options.go rename to options.go index ab9fe04..0c883fe 100644 --- a/scene-options.go +++ b/options.go @@ -13,6 +13,7 @@ type SceneOptions struct { Game *Game Config *Config Next SceneName + Prev SceneName Whoami SceneName Ui *ebitenui.UI FontColor color.RGBA @@ -36,6 +37,10 @@ func (scene *SceneOptions) GetNext() SceneName { return scene.Next } +func (scene *SceneOptions) SetPrevious(prev SceneName) { + scene.Prev = prev +} + func (scene *SceneOptions) ResetNext() { scene.Next = scene.Whoami } @@ -100,11 +105,11 @@ func (scene *SceneOptions) Init() { }) scene.SetInitialValue(gridlines, scene.Config.ShowGrid) - separator := NewSeparator() + separator := NewSeparator(3) cancel := NewMenuButton("Close", func(args *widget.ButtonClickedEventArgs) { - scene.SetNext(Menu) + scene.SetNext(scene.Prev) }) rowContainer.AddChild(pause) diff --git a/scene-play.go b/play.go similarity index 97% rename from scene-play.go rename to play.go index 3669f3a..2c974f3 100644 --- a/scene-play.go +++ b/play.go @@ -26,6 +26,7 @@ type ScenePlay struct { Game *Game Config *Config Next SceneName + Prev SceneName Whoami SceneName Clear bool @@ -73,6 +74,10 @@ func (scene *ScenePlay) GetNext() SceneName { return scene.Next } +func (scene *ScenePlay) SetPrevious(prev SceneName) { + scene.Prev = prev +} + func (scene *ScenePlay) ResetNext() { scene.Next = scene.Whoami } @@ -166,6 +171,10 @@ func (scene *ScenePlay) CheckInput() { scene.SetNext(Menu) } + if inpututil.IsKeyJustPressed(ebiten.KeyO) { + scene.SetNext(Options) + } + if inpututil.IsKeyJustPressed(ebiten.KeyC) { fmt.Println("mark mode on") scene.Config.Markmode = true @@ -625,6 +634,12 @@ func (scene *ScenePlay) Init() { float64(scene.Config.ScreenWidth), float64(scene.Config.ScreenHeight), }, + InitialZoomFactor: scene.Config.Zoomfactor, + InitialPosition: f64.Vec2{ + scene.Config.InitialCamPos[0], + scene.Config.InitialCamPos[1], + }, + ZoomOutFactor: scene.Config.ZoomOutFactor, } scene.World = ebiten.NewImage( @@ -659,8 +674,7 @@ func (scene *ScenePlay) Init() { scene.Camera.ZoomFactor = scene.Config.Zoomfactor } - scene.Camera.Position[0] = scene.Config.InitialCamPos[0] - scene.Camera.Position[1] = scene.Config.InitialCamPos[1] + scene.Camera.Setup() } // count the living neighbors of a cell diff --git a/scene.go b/scene.go index 0f35bfa..86caf39 100644 --- a/scene.go +++ b/scene.go @@ -13,6 +13,7 @@ type SceneName int type Scene interface { SetNext(SceneName) GetNext() SceneName + SetPrevious(SceneName) ResetNext() Update() error Draw(screen *ebiten.Image) diff --git a/widgets.go b/widgets.go index 4c1cbbc..358bd07 100644 --- a/widgets.go +++ b/widgets.go @@ -64,12 +64,12 @@ func NewCheckbox( ) } -func NewSeparator() widget.PreferredSizeLocateableWidget { +func NewSeparator(padding int) widget.PreferredSizeLocateableWidget { c := widget.NewContainer( widget.ContainerOpts.Layout(widget.NewRowLayout( widget.RowLayoutOpts.Direction(widget.DirectionVertical), widget.RowLayoutOpts.Padding(widget.Insets{ - Top: 3, + Top: padding, Bottom: 0, }))), widget.ContainerOpts.WidgetOpts(