1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-07 18:57:18 -05:00
OpenDiablo2/d2game/d2gamescreen/main_menu.go

652 lines
21 KiB
Go
Raw Normal View History

// Package d2gamescreen contains the screens
package d2gamescreen
2019-10-24 09:31:59 -04:00
import (
2019-10-26 12:55:36 -04:00
"fmt"
"net"
2019-10-25 23:41:54 -04:00
"os"
2019-10-26 12:55:36 -04:00
"os/exec"
"runtime"
2020-11-26 06:25:47 -05:00
"strings"
Removing d2datadict singletons (#738) * Remove weapons, armor, misc, itemCommon, itemTyps datadict singletons - removed loader calls from d2app - removed the HeroObjects singleton from `d2core/d2inventory` - added an InventoryItemFactory in d2inventory - package-level functions that use data records are now methods of the InventoryItemFactory - renamed ItemGenerator in d2item to ItemFactory - package-level functions that use records are now methods of ItemFactory - d2map.MapEntityFactory now has an item factory instance for creating items - fixed a bug in unique item record loader where it loaded an empty record - added a PlayerStateFactory for creating a player state (uses the asset manager) - updated the test inventory/equipment code in d2player to handle errors from the ItemFactory - character select and character creation screens have a player state and inventory item factory - updated item tests to use the item factory * minor edit * Removed d2datadict.Experience singleton added a HeroStatsFactory, much like the other factories. The factory gets an asset manager reference in order to use data records. * removed d2datadict.AutoMagic singleton * removed d2datadict.AutoMap singleton * removed d2datadict.BodyLocations singleton * removed d2datadict.Books singleton * Removed singletons for level records - removed loader calls in d2app - changed type references from d2datadict to d2records - added a `MapGenerator` in d2mapgen which uses thew asset manager and map engine - package-level map generation functions are now MapGenerator methods - `d2datadict.GetLevelDetails(id int)` is now a method of the RecordManager * remove SkillCalc and MissileCalc singletons * Removed CharStats and ItemStatCost singletons - added an ItemStatFactory which uses the asset manager to create stats - package-level functions for stats in d2item are now StatFactory methods - changed type references from d2datadict to d2records - `d2player.GetAllPlayerStates` is now a method of the `PlayerStateFactory` * Removed DkillDesc and Skills singletons from d2datadict - removed loader calls from d2app - diablo2stats.Stat instances are given a reference to the factory for doing record lookups * update the stats test to use mock a asset manager and stat factory * fixed diablo2stats tests and diablo2item tests * removed CompCodes singleton from d2datadict * remove cubemain singleton from d2datadict * removed DifficultyLevels singleton from d2datadict * removed ElemTypes singleton from d2datadict * removed events.go loader from d2datadict (was unused) * removed Gems singleton from d2datadict * removed Hireling and Inventory singletons from d2datadict * removed MagicPrefix and MagicSuffix singletons from d2datadict * removed ItemRatios singleton from d2datadict * removed Missiles singleton from d2datadict * removed MonModes singleton * Removed all monster and npc singletons from d2datadict - MapStamp instances now get a reference to their factory for doing record lookups * removed SoundEntry and SoundEnviron singletons from d2datadict
2020-09-20 17:52:01 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
2020-02-08 21:02:37 -05:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
2020-02-08 21:02:37 -05:00
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2screen"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
"github.com/OpenDiablo2/OpenDiablo2/d2script"
2019-10-24 09:31:59 -04:00
)
type mainMenuScreenMode int
2020-06-18 14:11:04 -04:00
// mainMenuScreenMode types
2020-06-18 14:11:04 -04:00
const (
ScreenModeUnknown mainMenuScreenMode = iota
ScreenModeTrademark
ScreenModeMainMenu
ScreenModeMultiplayer
ScreenModeTCPIP
ScreenModeServerIP
2020-06-18 14:11:04 -04:00
)
const (
joinGameDialogX, joinGameDialogY = 318, 245
serverIPbackgroundX, serverIPbackgroundY = 270, 175
backgroundX, backgroundY = 0, 0
versionLabelX, versionLabelY = 795, -10
commitLabelX, commitLabelY = 2, 2
copyrightX, copyrightY = 400, 500
copyright2X, copyright2Y = 400, 525
od2LabelX, od2LabelY = 400, 580
tcpOptionsX, tcpOptionsY = 400, 23
joinGameX, joinGameY = 400, 190
diabloLogoX, diabloLogoY = 400, 120
exitDiabloBtnX, exitDiabloBtnY = 264, 535
creditBtnX, creditBtnY = 264, 505
cineBtnX, cineBtnY = 401, 505
singlePlayerBtnX, singlePlayerBtnY = 264, 290
githubBtnX, githubBtnY = 264, 400
mapTestBtnX, mapTestBtnY = 264, 440
tcpBtnX, tcpBtnY = 33, 543
srvCancelBtnX, srvCancelBtnY = 285, 305
srvOkBtnX, srvOkBtnY = 420, 305
multiplayerBtnX, multiplayerBtnY = 264, 330
tcpNetBtnX, tcpNetBtnY = 264, 280
networkCancelBtnX, networkCancelBtnY = 264, 540
tcpHostBtnX, tcpHostBtnY = 264, 200
tcpJoinBtnX, tcpJoinBtnY = 264, 240
errorLabelX, errorLabelY = 400, 250
machineIPX, machineIPY = 400, 90
)
const (
white = 0xffffffff
lightYellow = 0xffff8cff
gold = 0xd8c480ff
red = 0xff0000ff
)
const (
joinGameCharacterFilter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890._:"
)
const (
logPrefix = "Game Screen"
)
2020-07-23 12:56:50 -04:00
// BuildInfo contains information about the current build
type BuildInfo struct {
Branch, Commit string
}
// CreateMainMenu creates an instance of MainMenu
func CreateMainMenu(
navigator d2interface.Navigator,
asset *d2asset.AssetManager,
renderer d2interface.Renderer,
inputManager d2interface.InputManager,
audioProvider d2interface.AudioProvider,
ui *d2ui.UIManager,
buildInfo BuildInfo,
l d2util.LogLevel,
errorMessageOptional ...string,
) (*MainMenu, error) {
heroStateFactory, err := d2hero.NewHeroStateFactory(asset)
if err != nil {
return nil, err
}
mainMenu := &MainMenu{
asset: asset,
screenMode: ScreenModeUnknown,
leftButtonHeld: true,
renderer: renderer,
inputManager: inputManager,
audioProvider: audioProvider,
navigator: navigator,
buildInfo: buildInfo,
uiManager: ui,
heroState: heroStateFactory,
}
mainMenu.Logger = d2util.NewLogger()
mainMenu.Logger.SetPrefix(logPrefix)
mainMenu.Logger.SetLevel(l)
if len(errorMessageOptional) != 0 {
mainMenu.errorLabel = ui.NewLabel(d2resource.FontFormal12, d2resource.PaletteUnits)
mainMenu.errorLabel.SetText(errorMessageOptional[0])
}
return mainMenu, nil
}
// MainMenu represents the main menu
2019-10-24 09:31:59 -04:00
type MainMenu struct {
tcpIPBackground *d2ui.Sprite
trademarkBackground *d2ui.Sprite
background *d2ui.Sprite
diabloLogoLeft *d2ui.Sprite
diabloLogoRight *d2ui.Sprite
diabloLogoLeftBack *d2ui.Sprite
diabloLogoRightBack *d2ui.Sprite
serverIPBackground *d2ui.Sprite
singlePlayerButton *d2ui.Button
multiplayerButton *d2ui.Button
githubButton *d2ui.Button
exitDiabloButton *d2ui.Button
creditsButton *d2ui.Button
cinematicsButton *d2ui.Button
mapTestButton *d2ui.Button
networkTCPIPButton *d2ui.Button
networkCancelButton *d2ui.Button
btnTCPIPCancel *d2ui.Button
btnTCPIPHostGame *d2ui.Button
btnTCPIPJoinGame *d2ui.Button
btnServerIPCancel *d2ui.Button
btnServerIPOk *d2ui.Button
copyrightLabel *d2ui.Label
copyrightLabel2 *d2ui.Label
openDiabloLabel *d2ui.Label
versionLabel *d2ui.Label
commitLabel *d2ui.Label
tcpIPOptionsLabel *d2ui.Label
tcpJoinGameLabel *d2ui.Label
machineIP *d2ui.Label
errorLabel *d2ui.Label
tcpJoinGameEntry *d2ui.TextBox
screenMode mainMenuScreenMode
leftButtonHeld bool
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 16:51:30 -04:00
asset *d2asset.AssetManager
inputManager d2interface.InputManager
renderer d2interface.Renderer
audioProvider d2interface.AudioProvider
scriptEngine *d2script.ScriptEngine // nolint:structcheck,unused // it will be used...
navigator d2interface.Navigator
uiManager *d2ui.UIManager
Removing d2datadict singletons (#738) * Remove weapons, armor, misc, itemCommon, itemTyps datadict singletons - removed loader calls from d2app - removed the HeroObjects singleton from `d2core/d2inventory` - added an InventoryItemFactory in d2inventory - package-level functions that use data records are now methods of the InventoryItemFactory - renamed ItemGenerator in d2item to ItemFactory - package-level functions that use records are now methods of ItemFactory - d2map.MapEntityFactory now has an item factory instance for creating items - fixed a bug in unique item record loader where it loaded an empty record - added a PlayerStateFactory for creating a player state (uses the asset manager) - updated the test inventory/equipment code in d2player to handle errors from the ItemFactory - character select and character creation screens have a player state and inventory item factory - updated item tests to use the item factory * minor edit * Removed d2datadict.Experience singleton added a HeroStatsFactory, much like the other factories. The factory gets an asset manager reference in order to use data records. * removed d2datadict.AutoMagic singleton * removed d2datadict.AutoMap singleton * removed d2datadict.BodyLocations singleton * removed d2datadict.Books singleton * Removed singletons for level records - removed loader calls in d2app - changed type references from d2datadict to d2records - added a `MapGenerator` in d2mapgen which uses thew asset manager and map engine - package-level map generation functions are now MapGenerator methods - `d2datadict.GetLevelDetails(id int)` is now a method of the RecordManager * remove SkillCalc and MissileCalc singletons * Removed CharStats and ItemStatCost singletons - added an ItemStatFactory which uses the asset manager to create stats - package-level functions for stats in d2item are now StatFactory methods - changed type references from d2datadict to d2records - `d2player.GetAllPlayerStates` is now a method of the `PlayerStateFactory` * Removed DkillDesc and Skills singletons from d2datadict - removed loader calls from d2app - diablo2stats.Stat instances are given a reference to the factory for doing record lookups * update the stats test to use mock a asset manager and stat factory * fixed diablo2stats tests and diablo2item tests * removed CompCodes singleton from d2datadict * remove cubemain singleton from d2datadict * removed DifficultyLevels singleton from d2datadict * removed ElemTypes singleton from d2datadict * removed events.go loader from d2datadict (was unused) * removed Gems singleton from d2datadict * removed Hireling and Inventory singletons from d2datadict * removed MagicPrefix and MagicSuffix singletons from d2datadict * removed ItemRatios singleton from d2datadict * removed Missiles singleton from d2datadict * removed MonModes singleton * Removed all monster and npc singletons from d2datadict - MapStamp instances now get a reference to their factory for doing record lookups * removed SoundEntry and SoundEnviron singletons from d2datadict
2020-09-20 17:52:01 -04:00
heroState *d2hero.HeroStateFactory
2020-07-23 12:56:50 -04:00
buildInfo BuildInfo
Removing d2datadict singletons (#738) * Remove weapons, armor, misc, itemCommon, itemTyps datadict singletons - removed loader calls from d2app - removed the HeroObjects singleton from `d2core/d2inventory` - added an InventoryItemFactory in d2inventory - package-level functions that use data records are now methods of the InventoryItemFactory - renamed ItemGenerator in d2item to ItemFactory - package-level functions that use records are now methods of ItemFactory - d2map.MapEntityFactory now has an item factory instance for creating items - fixed a bug in unique item record loader where it loaded an empty record - added a PlayerStateFactory for creating a player state (uses the asset manager) - updated the test inventory/equipment code in d2player to handle errors from the ItemFactory - character select and character creation screens have a player state and inventory item factory - updated item tests to use the item factory * minor edit * Removed d2datadict.Experience singleton added a HeroStatsFactory, much like the other factories. The factory gets an asset manager reference in order to use data records. * removed d2datadict.AutoMagic singleton * removed d2datadict.AutoMap singleton * removed d2datadict.BodyLocations singleton * removed d2datadict.Books singleton * Removed singletons for level records - removed loader calls in d2app - changed type references from d2datadict to d2records - added a `MapGenerator` in d2mapgen which uses thew asset manager and map engine - package-level map generation functions are now MapGenerator methods - `d2datadict.GetLevelDetails(id int)` is now a method of the RecordManager * remove SkillCalc and MissileCalc singletons * Removed CharStats and ItemStatCost singletons - added an ItemStatFactory which uses the asset manager to create stats - package-level functions for stats in d2item are now StatFactory methods - changed type references from d2datadict to d2records - `d2player.GetAllPlayerStates` is now a method of the `PlayerStateFactory` * Removed DkillDesc and Skills singletons from d2datadict - removed loader calls from d2app - diablo2stats.Stat instances are given a reference to the factory for doing record lookups * update the stats test to use mock a asset manager and stat factory * fixed diablo2stats tests and diablo2item tests * removed CompCodes singleton from d2datadict * remove cubemain singleton from d2datadict * removed DifficultyLevels singleton from d2datadict * removed ElemTypes singleton from d2datadict * removed events.go loader from d2datadict (was unused) * removed Gems singleton from d2datadict * removed Hireling and Inventory singletons from d2datadict * removed MagicPrefix and MagicSuffix singletons from d2datadict * removed ItemRatios singleton from d2datadict * removed Missiles singleton from d2datadict * removed MonModes singleton * Removed all monster and npc singletons from d2datadict - MapStamp instances now get a reference to their factory for doing record lookups * removed SoundEntry and SoundEnviron singletons from d2datadict
2020-09-20 17:52:01 -04:00
*d2util.Logger
2019-10-24 09:31:59 -04:00
}
// OnLoad is called to load the resources for the main menu
func (v *MainMenu) OnLoad(loading d2screen.LoadingState) {
2020-06-28 19:31:10 -04:00
v.audioProvider.PlayBGM(d2resource.BGMTitle)
loading.Progress(twentyPercent)
2020-02-08 21:02:37 -05:00
v.createLabels(loading)
v.loadBackgroundSprites()
v.createLogos(loading)
v.createButtons(loading)
v.tcpJoinGameEntry = v.uiManager.NewTextbox()
v.tcpJoinGameEntry.SetPosition(joinGameDialogX, joinGameDialogY)
v.tcpJoinGameEntry.SetFilter(joinGameCharacterFilter)
loading.Progress(ninetyPercent)
if v.screenMode == ScreenModeUnknown {
v.SetScreenMode(ScreenModeTrademark)
} else {
v.SetScreenMode(ScreenModeMainMenu)
}
if err := v.inputManager.BindHandler(v); err != nil {
v.Error("failed to add main menu as event handler")
}
}
func (v *MainMenu) loadBackgroundSprites() {
var err error
v.background, err = v.uiManager.NewSprite(d2resource.GameSelectScreen, d2resource.PaletteSky)
if err != nil {
v.Error(err.Error())
}
v.background.SetPosition(backgroundX, backgroundY)
v.trademarkBackground, err = v.uiManager.NewSprite(d2resource.TrademarkScreen, d2resource.PaletteSky)
if err != nil {
v.Error(err.Error())
}
v.trademarkBackground.SetPosition(backgroundX, backgroundY)
v.tcpIPBackground, err = v.uiManager.NewSprite(d2resource.TCPIPBackground, d2resource.PaletteSky)
if err != nil {
v.Error(err.Error())
}
v.tcpIPBackground.SetPosition(backgroundX, backgroundY)
v.serverIPBackground, err = v.uiManager.NewSprite(d2resource.PopUpOkCancel, d2resource.PaletteFechar)
if err != nil {
v.Error(err.Error())
}
v.serverIPBackground.SetPosition(serverIPbackgroundX, serverIPbackgroundY)
}
func (v *MainMenu) createLabels(loading d2screen.LoadingState) {
v.versionLabel = v.uiManager.NewLabel(d2resource.FontFormal12, d2resource.PaletteStatic)
v.versionLabel.Alignment = d2ui.HorizontalAlignRight
2020-07-23 12:56:50 -04:00
v.versionLabel.SetText("OpenDiablo2 - " + v.buildInfo.Branch)
2020-12-14 05:25:37 -05:00
v.versionLabel.Color[0] = d2util.Color(white)
v.versionLabel.SetPosition(versionLabelX, versionLabelY)
2020-02-08 21:02:37 -05:00
v.commitLabel = v.uiManager.NewLabel(d2resource.FontFormal10, d2resource.PaletteStatic)
v.commitLabel.Alignment = d2ui.HorizontalAlignLeft
2020-07-23 12:56:50 -04:00
v.commitLabel.SetText(v.buildInfo.Commit)
2020-12-14 05:25:37 -05:00
v.commitLabel.Color[0] = d2util.Color(white)
v.commitLabel.SetPosition(commitLabelX, commitLabelY)
2020-02-08 21:02:37 -05:00
v.copyrightLabel = v.uiManager.NewLabel(d2resource.FontFormal12, d2resource.PaletteStatic)
v.copyrightLabel.Alignment = d2ui.HorizontalAlignCenter
v.copyrightLabel.SetText(v.asset.TranslateLabel(d2enum.CopyrightLabel))
2020-12-14 05:25:37 -05:00
v.copyrightLabel.Color[0] = d2util.Color(lightBrown)
v.copyrightLabel.SetPosition(copyrightX, copyrightY)
loading.Progress(thirtyPercent)
2020-02-08 21:02:37 -05:00
v.copyrightLabel2 = v.uiManager.NewLabel(d2resource.FontFormal12, d2resource.PaletteStatic)
v.copyrightLabel2.Alignment = d2ui.HorizontalAlignCenter
v.copyrightLabel2.SetText(v.asset.TranslateLabel(d2enum.AllRightsReservedLabel))
2020-12-14 05:25:37 -05:00
v.copyrightLabel2.Color[0] = d2util.Color(lightBrown)
v.copyrightLabel2.SetPosition(copyright2X, copyright2Y)
2020-02-08 21:02:37 -05:00
v.openDiabloLabel = v.uiManager.NewLabel(d2resource.FontFormal10, d2resource.PaletteStatic)
v.openDiabloLabel.Alignment = d2ui.HorizontalAlignCenter
2020-02-08 21:02:37 -05:00
v.openDiabloLabel.SetText("OpenDiablo2 is neither developed by, nor endorsed by Blizzard or its parent company Activision")
2020-12-14 05:25:37 -05:00
v.openDiabloLabel.Color[0] = d2util.Color(lightYellow)
v.openDiabloLabel.SetPosition(od2LabelX, od2LabelY)
loading.Progress(fiftyPercent)
2020-02-08 21:02:37 -05:00
v.tcpIPOptionsLabel = v.uiManager.NewLabel(d2resource.Font42, d2resource.PaletteUnits)
v.tcpIPOptionsLabel.SetPosition(tcpOptionsX, tcpOptionsY)
v.tcpIPOptionsLabel.Alignment = d2ui.HorizontalAlignCenter
v.tcpIPOptionsLabel.SetText(v.asset.TranslateLabel(d2enum.TCPIPOptionsLabel))
2020-02-08 21:02:37 -05:00
v.tcpJoinGameLabel = v.uiManager.NewLabel(d2resource.Font16, d2resource.PaletteUnits)
v.tcpJoinGameLabel.Alignment = d2ui.HorizontalAlignCenter
v.tcpJoinGameLabel.SetText(strings.Join(d2util.SplitIntoLinesWithMaxWidth(v.asset.TranslateLabel(d2enum.TCPIPEnterHostIPLabel), 27), "\n"))
2020-12-14 05:25:37 -05:00
v.tcpJoinGameLabel.Color[0] = d2util.Color(gold)
v.tcpJoinGameLabel.SetPosition(joinGameX, joinGameY)
v.machineIP = v.uiManager.NewLabel(d2resource.Font24, d2resource.PaletteUnits)
v.machineIP.Alignment = d2ui.HorizontalAlignCenter
v.machineIP.SetText(v.asset.TranslateLabel(d2enum.TCPIPYourIPLabel) + "\n" + v.getLocalIP())
2020-12-14 05:25:37 -05:00
v.machineIP.Color[0] = d2util.Color(lightYellow)
v.machineIP.SetPosition(machineIPX, machineIPY)
if v.errorLabel != nil {
v.errorLabel.SetPosition(errorLabelX, errorLabelY)
v.errorLabel.Alignment = d2ui.HorizontalAlignCenter
2020-12-14 05:25:37 -05:00
v.errorLabel.Color[0] = d2util.Color(red)
}
}
2020-06-18 14:11:04 -04:00
func (v *MainMenu) createLogos(loading d2screen.LoadingState) {
var err error
v.diabloLogoLeft, err = v.uiManager.NewSprite(d2resource.Diablo2LogoFireLeft, d2resource.PaletteUnits)
if err != nil {
v.Error(err.Error())
}
v.diabloLogoLeft.SetEffect(d2enum.DrawEffectModulate)
2020-02-08 21:02:37 -05:00
v.diabloLogoLeft.PlayForward()
v.diabloLogoLeft.SetPosition(diabloLogoX, diabloLogoY)
loading.Progress(sixtyPercent)
2020-02-08 21:02:37 -05:00
v.diabloLogoRight, err = v.uiManager.NewSprite(d2resource.Diablo2LogoFireRight, d2resource.PaletteUnits)
if err != nil {
v.Error(err.Error())
}
v.diabloLogoRight.SetEffect(d2enum.DrawEffectModulate)
2020-02-08 21:02:37 -05:00
v.diabloLogoRight.PlayForward()
v.diabloLogoRight.SetPosition(diabloLogoX, diabloLogoY)
2020-02-08 21:02:37 -05:00
v.diabloLogoLeftBack, err = v.uiManager.NewSprite(d2resource.Diablo2LogoBlackLeft, d2resource.PaletteUnits)
if err != nil {
v.Error(err.Error())
}
v.diabloLogoLeftBack.SetPosition(diabloLogoX, diabloLogoY)
2020-02-08 21:02:37 -05:00
v.diabloLogoRightBack, err = v.uiManager.NewSprite(d2resource.Diablo2LogoBlackRight, d2resource.PaletteUnits)
if err != nil {
v.Error(err.Error())
}
v.diabloLogoRightBack.SetPosition(diabloLogoX, diabloLogoY)
}
2020-02-08 21:02:37 -05:00
func (v *MainMenu) createButtons(loading d2screen.LoadingState) {
v.exitDiabloButton = v.uiManager.NewButton(d2ui.ButtonTypeWide, v.asset.TranslateLabel(d2enum.ExitGameLabel))
v.exitDiabloButton.SetPosition(exitDiabloBtnX, exitDiabloBtnY)
2020-02-08 21:02:37 -05:00
v.exitDiabloButton.OnActivated(func() { v.onExitButtonClicked() })
v.creditsButton = v.uiManager.NewButton(d2ui.ButtonTypeShort, v.asset.TranslateLabel(d2enum.CreditsLabel))
v.creditsButton.SetPosition(creditBtnX, creditBtnY)
2020-02-08 21:02:37 -05:00
v.creditsButton.OnActivated(func() { v.onCreditsButtonClicked() })
v.cinematicsButton = v.uiManager.NewButton(d2ui.ButtonTypeShort, v.asset.TranslateLabel(d2enum.CinematicsLabel))
v.cinematicsButton.SetPosition(cineBtnX, cineBtnY)
2020-11-09 12:51:38 -05:00
v.cinematicsButton.OnActivated(func() { v.onCinematicsButtonClicked() })
loading.Progress(seventyPercent)
2020-02-08 21:02:37 -05:00
v.singlePlayerButton = v.uiManager.NewButton(d2ui.ButtonTypeWide, v.asset.TranslateLabel(d2enum.SinglePlayerLabel))
v.singlePlayerButton.SetPosition(singlePlayerBtnX, singlePlayerBtnY)
2020-02-08 21:02:37 -05:00
v.singlePlayerButton.OnActivated(func() { v.onSinglePlayerClicked() })
v.githubButton = v.uiManager.NewButton(d2ui.ButtonTypeWide, "PROJECT WEBSITE")
v.githubButton.SetPosition(githubBtnX, githubBtnY)
2020-02-08 21:02:37 -05:00
v.githubButton.OnActivated(func() { v.onGithubButtonClicked() })
v.mapTestButton = v.uiManager.NewButton(d2ui.ButtonTypeWide, "MAP ENGINE TEST")
v.mapTestButton.SetPosition(mapTestBtnX, mapTestBtnY)
2020-02-08 21:02:37 -05:00
v.mapTestButton.OnActivated(func() { v.onMapTestClicked() })
2020-06-18 14:11:04 -04:00
v.btnTCPIPCancel = v.uiManager.NewButton(d2ui.ButtonTypeMedium,
v.asset.TranslateLabel(d2enum.CancelLabel))
v.btnTCPIPCancel.SetPosition(tcpBtnX, tcpBtnY)
v.btnTCPIPCancel.OnActivated(func() { v.onTCPIPCancelClicked() })
2020-06-18 14:11:04 -04:00
v.btnServerIPCancel = v.uiManager.NewButton(d2ui.ButtonTypeOkCancel, v.asset.TranslateLabel(d2enum.CancelLabel))
v.btnServerIPCancel.SetPosition(srvCancelBtnX, srvCancelBtnY)
v.btnServerIPCancel.OnActivated(func() { v.onBtnTCPIPCancelClicked() })
2020-06-18 14:11:04 -04:00
v.btnServerIPOk = v.uiManager.NewButton(d2ui.ButtonTypeOkCancel, v.asset.TranslateString(d2enum.OKLabel))
v.btnServerIPOk.SetPosition(srvOkBtnX, srvOkBtnY)
v.btnServerIPOk.OnActivated(func() { v.onBtnTCPIPOkClicked() })
2020-06-18 14:11:04 -04:00
v.createMultiplayerMenuButtons()
loading.Progress(eightyPercent)
}
2020-06-18 14:11:04 -04:00
func (v *MainMenu) createMultiplayerMenuButtons() {
2020-11-25 04:03:50 -05:00
v.multiplayerButton = v.uiManager.NewButton(d2ui.ButtonTypeWide,
v.asset.TranslateLabel(d2enum.OtherMultiplayerLabel))
v.multiplayerButton.SetPosition(multiplayerBtnX, multiplayerBtnY)
v.multiplayerButton.OnActivated(func() { v.onMultiplayerClicked() })
2020-06-18 14:11:04 -04:00
v.networkTCPIPButton = v.uiManager.NewButton(d2ui.ButtonTypeWide, v.asset.TranslateLabel(d2enum.TCPIPGameLabel))
v.networkTCPIPButton.SetPosition(tcpNetBtnX, tcpNetBtnY)
v.networkTCPIPButton.OnActivated(func() { v.onNetworkTCPIPClicked() })
2020-06-18 14:11:04 -04:00
v.networkCancelButton = v.uiManager.NewButton(d2ui.ButtonTypeWide,
v.asset.TranslateLabel(d2enum.CancelLabel))
v.networkCancelButton.SetPosition(networkCancelBtnX, networkCancelBtnY)
v.networkCancelButton.OnActivated(func() { v.onNetworkCancelClicked() })
2020-06-18 14:11:04 -04:00
v.btnTCPIPHostGame = v.uiManager.NewButton(d2ui.ButtonTypeWide, v.asset.TranslateLabel(d2enum.TCPIPHostGameLabel))
v.btnTCPIPHostGame.SetPosition(tcpHostBtnX, tcpHostBtnY)
v.btnTCPIPHostGame.OnActivated(func() { v.onTCPIPHostGameClicked() })
2020-06-18 14:11:04 -04:00
v.btnTCPIPJoinGame = v.uiManager.NewButton(d2ui.ButtonTypeWide, v.asset.TranslateLabel(d2enum.TCPIPJoinGameLabel))
v.btnTCPIPJoinGame.SetPosition(tcpJoinBtnX, tcpJoinBtnY)
v.btnTCPIPJoinGame.OnActivated(func() { v.onTCPIPJoinGameClicked() })
}
2020-06-18 14:11:04 -04:00
func (v *MainMenu) onMapTestClicked() {
v.navigator.ToMapEngineTest(0, 1)
}
2020-06-18 14:11:04 -04:00
func (v *MainMenu) onSinglePlayerClicked() {
v.SetScreenMode(ScreenModeUnknown)
Removing d2datadict singletons (#738) * Remove weapons, armor, misc, itemCommon, itemTyps datadict singletons - removed loader calls from d2app - removed the HeroObjects singleton from `d2core/d2inventory` - added an InventoryItemFactory in d2inventory - package-level functions that use data records are now methods of the InventoryItemFactory - renamed ItemGenerator in d2item to ItemFactory - package-level functions that use records are now methods of ItemFactory - d2map.MapEntityFactory now has an item factory instance for creating items - fixed a bug in unique item record loader where it loaded an empty record - added a PlayerStateFactory for creating a player state (uses the asset manager) - updated the test inventory/equipment code in d2player to handle errors from the ItemFactory - character select and character creation screens have a player state and inventory item factory - updated item tests to use the item factory * minor edit * Removed d2datadict.Experience singleton added a HeroStatsFactory, much like the other factories. The factory gets an asset manager reference in order to use data records. * removed d2datadict.AutoMagic singleton * removed d2datadict.AutoMap singleton * removed d2datadict.BodyLocations singleton * removed d2datadict.Books singleton * Removed singletons for level records - removed loader calls in d2app - changed type references from d2datadict to d2records - added a `MapGenerator` in d2mapgen which uses thew asset manager and map engine - package-level map generation functions are now MapGenerator methods - `d2datadict.GetLevelDetails(id int)` is now a method of the RecordManager * remove SkillCalc and MissileCalc singletons * Removed CharStats and ItemStatCost singletons - added an ItemStatFactory which uses the asset manager to create stats - package-level functions for stats in d2item are now StatFactory methods - changed type references from d2datadict to d2records - `d2player.GetAllPlayerStates` is now a method of the `PlayerStateFactory` * Removed DkillDesc and Skills singletons from d2datadict - removed loader calls from d2app - diablo2stats.Stat instances are given a reference to the factory for doing record lookups * update the stats test to use mock a asset manager and stat factory * fixed diablo2stats tests and diablo2item tests * removed CompCodes singleton from d2datadict * remove cubemain singleton from d2datadict * removed DifficultyLevels singleton from d2datadict * removed ElemTypes singleton from d2datadict * removed events.go loader from d2datadict (was unused) * removed Gems singleton from d2datadict * removed Hireling and Inventory singletons from d2datadict * removed MagicPrefix and MagicSuffix singletons from d2datadict * removed ItemRatios singleton from d2datadict * removed Missiles singleton from d2datadict * removed MonModes singleton * Removed all monster and npc singletons from d2datadict - MapStamp instances now get a reference to their factory for doing record lookups * removed SoundEntry and SoundEnviron singletons from d2datadict
2020-09-20 17:52:01 -04:00
if v.heroState.HasGameStates() {
// Go here only if existing characters are available to select
v.navigator.ToCharacterSelect(d2clientconnectiontype.Local, v.tcpJoinGameEntry.GetText())
} else {
v.navigator.ToSelectHero(d2clientconnectiontype.Local, v.tcpJoinGameEntry.GetText())
2020-06-18 14:11:04 -04:00
}
2019-10-24 09:31:59 -04:00
}
func (v *MainMenu) onGithubButtonClicked() {
url := "https://www.github.com/OpenDiablo2/OpenDiablo2"
2019-10-26 12:55:36 -04:00
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")
}
2019-10-26 12:55:36 -04:00
if err != nil {
v.Error(err.Error())
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.navigator.ToCredits()
2019-10-26 00:26:48 -04:00
}
2020-11-09 12:51:38 -05:00
func (v *MainMenu) onCinematicsButtonClicked() {
v.navigator.ToCinematics()
}
// Render renders the main menu
func (v *MainMenu) Render(screen d2interface.Surface) {
v.renderBackgrounds(screen)
v.renderLogos(screen)
v.renderLabels(screen)
}
func (v *MainMenu) renderBackgrounds(screen d2interface.Surface) {
2020-06-18 14:11:04 -04:00
switch v.screenMode {
case ScreenModeTrademark:
v.trademarkBackground.RenderSegmented(screen, 4, 3, 0)
case ScreenModeServerIP:
v.tcpIPBackground.RenderSegmented(screen, 4, 3, 0)
v.serverIPBackground.RenderSegmented(screen, 2, 1, 0)
case ScreenModeTCPIP:
v.tcpIPBackground.RenderSegmented(screen, 4, 3, 0)
2020-06-18 14:11:04 -04:00
default:
v.background.RenderSegmented(screen, 4, 3, 0)
2019-10-24 09:31:59 -04:00
}
}
func (v *MainMenu) renderLogos(screen d2interface.Surface) {
2020-06-18 14:11:04 -04:00
switch v.screenMode {
case ScreenModeTrademark, ScreenModeMainMenu, ScreenModeMultiplayer:
v.diabloLogoLeftBack.Render(screen)
v.diabloLogoRightBack.Render(screen)
v.diabloLogoLeft.Render(screen)
v.diabloLogoRight.Render(screen)
2020-06-18 14:11:04 -04:00
}
}
func (v *MainMenu) renderLabels(screen d2interface.Surface) {
2020-06-18 14:11:04 -04:00
switch v.screenMode {
case ScreenModeServerIP:
v.tcpIPOptionsLabel.Render(screen)
v.tcpJoinGameLabel.Render(screen)
v.machineIP.Render(screen)
case ScreenModeTCPIP:
v.tcpIPOptionsLabel.Render(screen)
v.machineIP.Render(screen)
case ScreenModeTrademark:
v.copyrightLabel.Render(screen)
v.copyrightLabel2.Render(screen)
if v.errorLabel != nil {
v.errorLabel.Render(screen)
}
case ScreenModeMainMenu:
v.openDiabloLabel.Render(screen)
v.versionLabel.Render(screen)
v.commitLabel.Render(screen)
2019-10-24 09:31:59 -04:00
}
}
// Advance runs the update logic on the main menu
2020-02-08 21:02:37 -05:00
func (v *MainMenu) Advance(tickTime float64) error {
2020-06-18 14:11:04 -04:00
switch v.screenMode {
case ScreenModeMainMenu, ScreenModeTrademark, ScreenModeMultiplayer:
if err := v.diabloLogoLeftBack.Advance(tickTime); err != nil {
return err
}
if err := v.diabloLogoRightBack.Advance(tickTime); err != nil {
return err
}
if err := v.diabloLogoLeft.Advance(tickTime); err != nil {
return err
}
if err := v.diabloLogoRight.Advance(tickTime); err != nil {
return err
}
2020-06-18 14:11:04 -04:00
}
2020-02-08 21:02:37 -05:00
return nil
2019-10-24 09:31:59 -04:00
}
2020-06-18 14:11:04 -04:00
// OnMouseButtonDown is called when a mouse button is clicked
func (v *MainMenu) OnMouseButtonDown(event d2interface.MouseEvent) bool {
if v.screenMode == ScreenModeTrademark && event.Button() == d2enum.MouseButtonLeft {
v.SetScreenMode(ScreenModeMainMenu)
return true
}
return false
}
func (v *MainMenu) onEscapePressed(event d2interface.KeyEvent, mode mainMenuScreenMode) {
if event.Key() == d2enum.KeyEscape {
v.SetScreenMode(mode)
}
}
// OnKeyUp is called when a key is released
func (v *MainMenu) OnKeyUp(event d2interface.KeyEvent) bool {
preventKeyEventPropagation := false
switch v.screenMode {
case ScreenModeTrademark: // On retail version of D2, some specific key events (Escape, Space and Enter) puts you onto the main menu.
switch event.Key() {
case d2enum.KeyEscape, d2enum.KeyEnter, d2enum.KeySpace:
v.SetScreenMode(ScreenModeMainMenu)
}
preventKeyEventPropagation = true
case ScreenModeMainMenu: // pressing escape in Main Menu close the game
if event.Key() == d2enum.KeyEscape {
v.onExitButtonClicked()
}
case ScreenModeMultiplayer: // back to previous menu
v.onEscapePressed(event, ScreenModeMainMenu)
preventKeyEventPropagation = true
case ScreenModeTCPIP: // back to previous menu
v.onEscapePressed(event, ScreenModeMultiplayer)
preventKeyEventPropagation = true
case ScreenModeServerIP: // back to previous menu
v.onEscapePressed(event, ScreenModeTCPIP)
preventKeyEventPropagation = true
}
return preventKeyEventPropagation
}
// SetScreenMode sets the screen mode (which sub-menu the screen is on)
func (v *MainMenu) SetScreenMode(screenMode mainMenuScreenMode) {
2020-06-18 14:11:04 -04:00
v.screenMode = screenMode
isMainMenu := screenMode == ScreenModeMainMenu
isMultiplayer := screenMode == ScreenModeMultiplayer
isTCPIP := screenMode == ScreenModeTCPIP
isServerIP := screenMode == ScreenModeServerIP
2020-06-18 14:11:04 -04:00
v.exitDiabloButton.SetVisible(isMainMenu)
v.creditsButton.SetVisible(isMainMenu)
v.cinematicsButton.SetVisible(isMainMenu)
v.singlePlayerButton.SetVisible(isMainMenu)
v.githubButton.SetVisible(isMainMenu)
v.mapTestButton.SetVisible(isMainMenu)
v.multiplayerButton.SetVisible(isMainMenu)
v.networkTCPIPButton.SetVisible(isMultiplayer)
2020-06-18 14:11:04 -04:00
v.networkCancelButton.SetVisible(isMultiplayer)
v.btnTCPIPCancel.SetVisible(isTCPIP)
v.btnTCPIPHostGame.SetVisible(isTCPIP)
v.btnTCPIPJoinGame.SetVisible(isTCPIP)
v.tcpJoinGameEntry.SetVisible(isServerIP)
if isServerIP {
v.tcpJoinGameEntry.Activate()
}
v.btnServerIPOk.SetVisible(isServerIP)
v.btnServerIPCancel.SetVisible(isServerIP)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onNetworkCancelClicked() {
v.SetScreenMode(ScreenModeMainMenu)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onMultiplayerClicked() {
v.SetScreenMode(ScreenModeMultiplayer)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onNetworkTCPIPClicked() {
v.SetScreenMode(ScreenModeTCPIP)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onTCPIPCancelClicked() {
v.SetScreenMode(ScreenModeMultiplayer)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onTCPIPHostGameClicked() {
v.navigator.ToCharacterSelect(d2clientconnectiontype.LANServer, "")
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onTCPIPJoinGameClicked() {
v.SetScreenMode(ScreenModeServerIP)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onBtnTCPIPCancelClicked() {
v.SetScreenMode(ScreenModeTCPIP)
2020-06-18 14:11:04 -04:00
}
func (v *MainMenu) onBtnTCPIPOkClicked() {
v.navigator.ToCharacterSelect(d2clientconnectiontype.LANClient, v.tcpJoinGameEntry.GetText())
2020-06-18 14:11:04 -04:00
}
// getLocalIP returns local machine IP address
func (v *MainMenu) getLocalIP() string {
// https://stackoverflow.com/a/28862477
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
return ipv4.String()
}
}
v.Warning("no IPv4 Address could be found")
return v.asset.TranslateLabel(d2enum.IPNotFoundLabel)
}