1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-10-31 16:27:18 -04:00
OpenDiablo2/Scenes/MainMenu.go

262 lines
8.7 KiB
Go
Raw Normal View History

2019-10-25 20:28:14 -04:00
package Scenes
2019-10-24 09:31:59 -04:00
import (
2019-10-26 12:55:36 -04:00
"fmt"
"image/color"
2019-10-26 12:55:36 -04:00
"log"
2019-10-25 23:41:54 -04:00
"os"
2019-10-26 12:55:36 -04:00
"os/exec"
"runtime"
"github.com/OpenDiablo2/OpenDiablo2/Common"
"github.com/OpenDiablo2/OpenDiablo2/PaletteDefs"
"github.com/OpenDiablo2/OpenDiablo2/Sound"
"github.com/OpenDiablo2/OpenDiablo2/UI"
"github.com/OpenDiablo2/OpenDiablo2/ResourcePaths"
2019-10-24 09:31:59 -04:00
"github.com/hajimehoshi/ebiten"
)
// MainMenu represents the main menu
2019-10-24 09:31:59 -04:00
type MainMenu struct {
2019-10-25 20:28:14 -04:00
uiManager *UI.Manager
soundManager *Sound.Manager
fileProvider Common.FileProvider
2019-10-26 00:26:48 -04:00
sceneProvider SceneProvider
2019-10-25 19:12:42 -04:00
trademarkBackground *Common.Sprite
background *Common.Sprite
diabloLogoLeft *Common.Sprite
diabloLogoRight *Common.Sprite
diabloLogoLeftBack *Common.Sprite
diabloLogoRightBack *Common.Sprite
2019-10-26 12:55:36 -04:00
singlePlayerButton *UI.Button
githubButton *UI.Button
exitDiabloButton *UI.Button
2019-10-26 00:06:54 -04:00
creditsButton *UI.Button
2019-10-26 12:55:36 -04:00
cinematicsButton *UI.Button
mapTestButton *UI.Button
2019-10-25 19:37:04 -04:00
copyrightLabel *UI.Label
copyrightLabel2 *UI.Label
2019-10-26 12:55:36 -04:00
openDiabloLabel *UI.Label
versionLabel *UI.Label
commitLabel *UI.Label
2019-10-26 20:09:33 -04:00
2019-10-26 00:55:59 -04:00
ShowTrademarkScreen bool
leftButtonHeld bool
2019-10-24 09:31:59 -04:00
}
// CreateMainMenu creates an instance of MainMenu
2019-10-26 00:26:48 -04:00
func CreateMainMenu(fileProvider Common.FileProvider, sceneProvider SceneProvider, uiManager *UI.Manager, soundManager *Sound.Manager) *MainMenu {
2019-10-24 09:31:59 -04:00
result := &MainMenu{
2019-10-25 20:28:14 -04:00
fileProvider: fileProvider,
uiManager: uiManager,
soundManager: soundManager,
2019-10-26 00:26:48 -04:00
sceneProvider: sceneProvider,
2019-10-26 00:55:59 -04:00
ShowTrademarkScreen: true,
leftButtonHeld: true,
2019-10-24 09:31:59 -04:00
}
return result
}
// Load is called to load the resources for the main menu
2019-10-25 20:28:14 -04:00
func (v *MainMenu) Load() []func() {
v.soundManager.PlayBGM(ResourcePaths.BGMTitle)
return []func(){
func() {
v.versionLabel = UI.CreateLabel(v.fileProvider, ResourcePaths.FontFormal12, PaletteDefs.Static)
v.versionLabel.Alignment = UI.LabelAlignRight
v.versionLabel.SetText("OpenDiablo2 - " + Common.BuildInfo.Branch)
v.versionLabel.Color = color.RGBA{255, 255, 255, 255}
v.versionLabel.MoveTo(795, -10)
},
func() {
v.commitLabel = UI.CreateLabel(v.fileProvider, ResourcePaths.FontFormal10, PaletteDefs.Static)
v.commitLabel.Alignment = UI.LabelAlignLeft
v.commitLabel.SetText(Common.BuildInfo.Commit)
v.commitLabel.Color = color.RGBA{255, 255, 255, 255}
v.commitLabel.MoveTo(2, 2)
},
2019-10-25 20:28:14 -04:00
func() {
v.copyrightLabel = UI.CreateLabel(v.fileProvider, ResourcePaths.FontFormal12, PaletteDefs.Static)
2019-10-25 19:37:04 -04:00
v.copyrightLabel.Alignment = UI.LabelAlignCenter
v.copyrightLabel.SetText("Diablo 2 is © Copyright 2000-2016 Blizzard Entertainment")
v.copyrightLabel.Color = color.RGBA{188, 168, 140, 255}
v.copyrightLabel.MoveTo(400, 500)
2019-10-25 20:28:14 -04:00
},
func() {
v.copyrightLabel2 = UI.CreateLabel(v.fileProvider, ResourcePaths.FontFormal12, PaletteDefs.Static)
2019-10-25 19:37:04 -04:00
v.copyrightLabel2.Alignment = UI.LabelAlignCenter
v.copyrightLabel2.SetText(Common.TranslateString("#1614"))
v.copyrightLabel2.Color = color.RGBA{188, 168, 140, 255}
v.copyrightLabel2.MoveTo(400, 525)
2019-10-25 20:28:14 -04:00
},
2019-10-26 12:55:36 -04:00
func() {
v.openDiabloLabel = UI.CreateLabel(v.fileProvider, ResourcePaths.FontFormal10, PaletteDefs.Static)
2019-10-26 12:55:36 -04:00
v.openDiabloLabel.Alignment = UI.LabelAlignCenter
v.openDiabloLabel.SetText("OpenDiablo2 is neither developed by, nor endorsed by Blizzard or its parent company Activision")
v.openDiabloLabel.Color = color.RGBA{255, 255, 140, 255}
v.openDiabloLabel.MoveTo(400, 580)
},
2019-10-25 20:28:14 -04:00
func() {
v.background = v.fileProvider.LoadSprite(ResourcePaths.GameSelectScreen, PaletteDefs.Sky)
v.background.MoveTo(0, 0)
2019-10-25 20:28:14 -04:00
},
func() {
v.trademarkBackground = v.fileProvider.LoadSprite(ResourcePaths.TrademarkScreen, PaletteDefs.Sky)
v.trademarkBackground.MoveTo(0, 0)
2019-10-25 20:28:14 -04:00
},
func() {
v.diabloLogoLeft = v.fileProvider.LoadSprite(ResourcePaths.Diablo2LogoFireLeft, PaletteDefs.Units)
v.diabloLogoLeft.Blend = true
v.diabloLogoLeft.Animate = true
v.diabloLogoLeft.MoveTo(400, 120)
2019-10-25 20:28:14 -04:00
},
func() {
v.diabloLogoRight = v.fileProvider.LoadSprite(ResourcePaths.Diablo2LogoFireRight, PaletteDefs.Units)
v.diabloLogoRight.Blend = true
v.diabloLogoRight.Animate = true
v.diabloLogoRight.MoveTo(400, 120)
2019-10-25 20:28:14 -04:00
},
func() {
v.diabloLogoLeftBack = v.fileProvider.LoadSprite(ResourcePaths.Diablo2LogoBlackLeft, PaletteDefs.Units)
v.diabloLogoLeftBack.MoveTo(400, 120)
2019-10-25 20:28:14 -04:00
},
func() {
v.diabloLogoRightBack = v.fileProvider.LoadSprite(ResourcePaths.Diablo2LogoBlackRight, PaletteDefs.Units)
v.diabloLogoRightBack.MoveTo(400, 120)
2019-10-25 20:28:14 -04:00
},
func() {
v.exitDiabloButton = UI.CreateButton(UI.ButtonTypeWide, v.fileProvider, Common.TranslateString("#1625"))
v.exitDiabloButton.MoveTo(264, 535)
2019-10-26 00:55:59 -04:00
v.exitDiabloButton.SetVisible(!v.ShowTrademarkScreen)
2019-10-25 23:41:54 -04:00
v.exitDiabloButton.OnActivated(func() { v.onExitButtonClicked() })
v.uiManager.AddWidget(v.exitDiabloButton)
},
2019-10-26 00:06:54 -04:00
func() {
v.creditsButton = UI.CreateButton(UI.ButtonTypeShort, v.fileProvider, Common.TranslateString("#1627"))
2019-10-26 00:06:54 -04:00
v.creditsButton.MoveTo(264, 505)
2019-10-26 00:55:59 -04:00
v.creditsButton.SetVisible(!v.ShowTrademarkScreen)
2019-10-26 00:26:48 -04:00
v.creditsButton.OnActivated(func() { v.onCreditsButtonClicked() })
2019-10-26 00:06:54 -04:00
v.uiManager.AddWidget(v.creditsButton)
},
2019-10-26 12:55:36 -04:00
func() {
v.cinematicsButton = UI.CreateButton(UI.ButtonTypeShort, v.fileProvider, Common.TranslateString("#1639"))
2019-10-26 12:55:36 -04:00
v.cinematicsButton.MoveTo(401, 505)
v.cinematicsButton.SetVisible(!v.ShowTrademarkScreen)
v.uiManager.AddWidget(v.cinematicsButton)
},
func() {
v.singlePlayerButton = UI.CreateButton(UI.ButtonTypeWide, v.fileProvider, Common.TranslateString("#1620"))
2019-10-26 12:55:36 -04:00
v.singlePlayerButton.MoveTo(264, 290)
v.singlePlayerButton.SetVisible(!v.ShowTrademarkScreen)
v.singlePlayerButton.OnActivated(func() { v.onSinglePlayerClicked() })
2019-10-26 12:55:36 -04:00
v.uiManager.AddWidget(v.singlePlayerButton)
},
func() {
v.githubButton = UI.CreateButton(UI.ButtonTypeWide, v.fileProvider, "PROJECT WEBSITE")
v.githubButton.MoveTo(264, 330)
v.githubButton.SetVisible(!v.ShowTrademarkScreen)
v.githubButton.OnActivated(func() { v.onGithubButtonClicked() })
v.uiManager.AddWidget(v.githubButton)
},
func() {
v.mapTestButton = UI.CreateButton(UI.ButtonTypeWide, v.fileProvider, "MAP ENGINE TEST")
v.mapTestButton.MoveTo(264, 450)
v.mapTestButton.SetVisible(!v.ShowTrademarkScreen)
v.mapTestButton.OnActivated(func() { v.onMapTestClicked() })
v.uiManager.AddWidget(v.mapTestButton)
},
2019-10-25 20:28:14 -04:00
}
2019-10-24 09:31:59 -04:00
}
func (v *MainMenu) onMapTestClicked() {
v.sceneProvider.SetNextScene(CreateMapEngineTest(v.fileProvider, v.sceneProvider, v.uiManager, v.soundManager))
}
2019-10-26 12:55:36 -04:00
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
func (v *MainMenu) onSinglePlayerClicked() {
// Go here only if existing characters are available to select
//v.sceneProvider.SetNextScene(CreateCharacterSelect(v.fileProvider, v.sceneProvider, v.uiManager, v.soundManager))
v.sceneProvider.SetNextScene(CreateSelectHeroClass(v.fileProvider, v.sceneProvider, v.uiManager, v.soundManager))
}
2019-10-26 12:55:36 -04:00
func (v *MainMenu) onGithubButtonClicked() {
openbrowser("https://www.github.com/OpenDiablo2/OpenDiablo2")
2019-10-26 12:55:36 -04:00
}
2019-10-25 23:41:54 -04:00
func (v *MainMenu) onExitButtonClicked() {
os.Exit(0)
}
2019-10-26 00:26:48 -04:00
func (v *MainMenu) onCreditsButtonClicked() {
v.sceneProvider.SetNextScene(CreateCredits(v.fileProvider, v.sceneProvider, v.uiManager, v.soundManager))
}
// Unload unloads the data for the main menu
2019-10-24 09:31:59 -04:00
func (v *MainMenu) Unload() {
}
// Render renders the main menu
2019-10-24 09:31:59 -04:00
func (v *MainMenu) Render(screen *ebiten.Image) {
2019-10-26 00:55:59 -04:00
if v.ShowTrademarkScreen {
v.trademarkBackground.DrawSegments(screen, 4, 3, 0)
2019-10-24 09:31:59 -04:00
} else {
v.background.DrawSegments(screen, 4, 3, 0)
2019-10-24 09:31:59 -04:00
}
v.diabloLogoLeftBack.Draw(screen)
v.diabloLogoRightBack.Draw(screen)
v.diabloLogoLeft.Draw(screen)
v.diabloLogoRight.Draw(screen)
2019-10-24 09:31:59 -04:00
2019-10-26 00:55:59 -04:00
if v.ShowTrademarkScreen {
v.copyrightLabel.Draw(screen)
v.copyrightLabel2.Draw(screen)
2019-10-24 09:31:59 -04:00
} else {
2019-10-26 12:55:36 -04:00
v.openDiabloLabel.Draw(screen)
v.versionLabel.Draw(screen)
v.commitLabel.Draw(screen)
2019-10-24 09:31:59 -04:00
}
}
// Update runs the update logic on the main menu
func (v *MainMenu) Update(tickTime float64) {
2019-10-26 00:55:59 -04:00
if v.ShowTrademarkScreen {
2019-10-25 20:28:14 -04:00
if v.uiManager.CursorButtonPressed(UI.CursorButtonLeft) {
if v.leftButtonHeld {
return
}
v.uiManager.WaitForMouseRelease()
v.leftButtonHeld = true
2019-10-26 00:55:59 -04:00
v.ShowTrademarkScreen = false
v.exitDiabloButton.SetVisible(true)
2019-10-26 00:06:54 -04:00
v.creditsButton.SetVisible(true)
2019-10-26 12:55:36 -04:00
v.cinematicsButton.SetVisible(true)
v.singlePlayerButton.SetVisible(true)
v.githubButton.SetVisible(true)
v.mapTestButton.SetVisible(true)
} else {
v.leftButtonHeld = false
2019-10-25 15:06:47 -04:00
}
return
}
2019-10-24 09:31:59 -04:00
}