1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-03 01:37:18 -04:00
OpenDiablo2/d2game/d2player/mini_panel.go

287 lines
6.1 KiB
Go
Raw Normal View History

package d2player
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
)
const (
miniPanelX = 325
miniPanelY = 526
panelOffsetLeft = 130
panelOffsetRight = 130
)
const (
containerOffsetX = -75
containerOffsetY = -49
buttonOffsetX = -72
buttonOffsetY = -52
)
type miniPanelContent struct {
buttonType d2ui.ButtonType
onActivate func()
tooltip string
}
type miniPanelActions struct {
characterToggle func()
inventoryToggle func()
skilltreeToggle func()
partyToggle func()
automapToggle func()
messageToggle func()
questToggle func()
menuToggle func()
}
type miniPanel struct {
ui *d2ui.UIManager
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
container *d2ui.Sprite
sprite *d2ui.Sprite
isOpen bool
isSinglePlayer bool
movedLeft bool
movedRight bool
panelGroup *d2ui.WidgetGroup
tooltipGroup *d2ui.WidgetGroup
}
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
func newMiniPanel(asset *d2asset.AssetManager, uiManager *d2ui.UIManager, isSinglePlayer bool) *miniPanel {
return &miniPanel{
ui: uiManager,
asset: asset,
isOpen: false,
isSinglePlayer: isSinglePlayer,
}
}
func (m *miniPanel) load(actions *miniPanelActions) {
var err error
m.sprite, err = m.ui.NewSprite(d2resource.MinipanelButton, d2resource.PaletteSky)
if err != nil {
log.Print(err)
return
}
m.createWidgets(actions)
}
func (m *miniPanel) createWidgets(actions *miniPanelActions) {
var err error
m.panelGroup = m.ui.NewWidgetGroup(d2ui.RenderPriorityMinipanel)
m.panelGroup.SetPosition(miniPanelX, miniPanelY)
m.tooltipGroup = m.ui.NewWidgetGroup(d2ui.RenderPriorityForeground)
miniPanelContainerPath := d2resource.Minipanel
if m.isSinglePlayer {
miniPanelContainerPath = d2resource.MinipanelSmall
}
2020-09-12 16:25:09 -04:00
m.container, err = m.ui.NewSprite(miniPanelContainerPath, d2resource.PaletteSky)
if err != nil {
log.Print(err)
return
}
if err = m.container.SetCurrentFrame(0); err != nil {
log.Print(err)
return
}
// nolint:golint,gomnd // divide by 2 does not need a magic number
x, y := screenWidth/2+containerOffsetX, screenHeight+containerOffsetY
m.container.SetPosition(x, y)
m.panelGroup.AddWidget(m.container)
buttonWidth, buttonHeight, err := m.sprite.GetFrameSize(0)
if err != nil {
log.Print(err)
return
}
buttonWidth++
// nolint:golint,gomnd // divide by 2 does not need a magic number
x, y = screenWidth/2+buttonOffsetX, screenHeight+buttonOffsetY-buttonHeight
buttonsFirst := []miniPanelContent{
{d2ui.ButtonTypeMinipanelCharacter,
actions.characterToggle,
m.asset.TranslateString("minipanelchar"),
},
{d2ui.ButtonTypeMinipanelInventory,
actions.inventoryToggle,
m.asset.TranslateString("minipanelinv"),
},
{d2ui.ButtonTypeMinipanelSkill,
actions.skilltreeToggle,
m.asset.TranslateString("minipaneltree"),
},
}
for i := range buttonsFirst {
btn := m.createButton(buttonsFirst[i], x+(i*buttonWidth), y, buttonHeight)
m.panelGroup.AddWidget(btn)
}
idxOffset := len(buttonsFirst)
if !m.isSinglePlayer {
partyContent := miniPanelContent{d2ui.ButtonTypeMinipanelParty,
actions.partyToggle,
m.asset.TranslateString("minipanelparty"),
}
btn := m.createButton(partyContent, x+(3*buttonWidth), y, buttonHeight)
m.panelGroup.AddWidget(btn)
idxOffset++
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
}
buttonsLast := []miniPanelContent{
{d2ui.ButtonTypeMinipanelAutomap,
actions.automapToggle,
m.asset.TranslateString("minipanelautomap"),
},
{d2ui.ButtonTypeMinipanelMessage,
actions.messageToggle,
m.asset.TranslateString("minipanelmessage"),
},
{d2ui.ButtonTypeMinipanelQuest,
actions.questToggle,
m.asset.TranslateString("minipanelquest"),
},
{d2ui.ButtonTypeMinipanelMen,
actions.menuToggle,
m.asset.TranslateString("minipanelmenubtn"),
},
}
for i := range buttonsLast {
idx := i + idxOffset
btn := m.createButton(buttonsLast[i], x+(idx*buttonWidth), y, buttonHeight)
m.panelGroup.AddWidget(btn)
}
m.panelGroup.SetVisible(false)
}
func (m *miniPanel) createButton(content miniPanelContent, x, y, buttonHeight int) *d2ui.Button {
// Tooltip
tt := m.ui.NewTooltip(d2resource.Font16, d2resource.PaletteSky, d2ui.TooltipXCenter, d2ui.TooltipYTop)
tt.SetPosition(x, y-buttonHeight)
tt.SetText(content.tooltip)
tt.SetVisible(false)
m.tooltipGroup.AddWidget(tt)
// Button
btn := m.ui.NewButton(content.buttonType, "")
btn.SetPosition(x, y)
btn.OnActivated(content.onActivate)
btn.SetTooltip(tt)
return btn
}
func (m *miniPanel) IsOpen() bool {
return m.isOpen
}
func (m *miniPanel) Toggle() {
if m.isOpen {
m.Close()
} else {
m.Open()
}
}
func (m *miniPanel) Open() {
m.panelGroup.SetVisible(true)
m.isOpen = true
}
func (m *miniPanel) Close() {
m.panelGroup.SetVisible(false)
m.isOpen = false
}
func (m *miniPanel) IsInRect(px, py int) bool {
return m.panelGroup.Contains(px, py)
}
func (m *miniPanel) moveRight() {
m.panelGroup.OffsetPosition(panelOffsetRight, 0)
m.tooltipGroup.OffsetPosition(panelOffsetRight, 0)
}
func (m *miniPanel) undoMoveRight() {
m.panelGroup.OffsetPosition(-panelOffsetRight, 0)
m.tooltipGroup.OffsetPosition(-panelOffsetRight, 0)
}
func (m *miniPanel) moveLeft() {
m.panelGroup.OffsetPosition(-panelOffsetLeft, 0)
m.tooltipGroup.OffsetPosition(-panelOffsetLeft, 0)
}
func (m *miniPanel) undoMoveLeft() {
m.panelGroup.OffsetPosition(panelOffsetLeft, 0)
m.tooltipGroup.OffsetPosition(panelOffsetLeft, 0)
}
func (m *miniPanel) SetMovedLeft(moveLeft bool) {
if m.movedLeft == moveLeft {
return
}
if m.movedRight {
if moveLeft {
m.undoMoveRight()
m.panelGroup.SetVisible(false)
} else {
m.moveRight()
m.panelGroup.SetVisible(true)
}
} else {
if moveLeft {
m.moveLeft()
} else {
m.undoMoveLeft()
}
}
m.movedLeft = moveLeft
}
func (m *miniPanel) SetMovedRight(moveRight bool) {
if m.movedRight == moveRight {
return
}
if m.movedLeft {
if moveRight {
m.undoMoveLeft()
m.panelGroup.SetVisible(false)
} else {
m.moveLeft()
m.panelGroup.SetVisible(true)
}
} else {
if moveRight {
m.moveRight()
} else {
m.undoMoveRight()
}
}
m.movedRight = moveRight
}