2019-12-29 04:32:53 +00:00
|
|
|
package d2player
|
|
|
|
|
|
|
|
import (
|
2020-08-11 22:01:33 +00:00
|
|
|
"fmt"
|
2020-08-03 17:44:00 +00:00
|
|
|
"image/color"
|
|
|
|
|
2020-07-11 15:25:34 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
2020-06-23 18:12:30 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 04:41:58 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-26 05:39:13 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-02-01 23:55:56 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-08-03 17:44:00 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gui"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2item/diablo2item"
|
2020-02-01 23:55:56 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
2019-12-29 04:32:53 +00:00
|
|
|
)
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// Inventory represents the inventory
|
2019-12-29 04:32:53 +00:00
|
|
|
type Inventory struct {
|
2020-09-12 20:51:30 +00:00
|
|
|
asset *d2asset.AssetManager
|
2020-08-11 22:01:33 +00:00
|
|
|
uiManager *d2ui.UIManager
|
|
|
|
frame *d2ui.Sprite
|
|
|
|
panel *d2ui.Sprite
|
|
|
|
grid *ItemGrid
|
|
|
|
hoverLabel *d2ui.Label
|
|
|
|
hoverX int
|
|
|
|
hoverY int
|
|
|
|
originX int
|
|
|
|
originY int
|
|
|
|
lastMouseX int
|
|
|
|
lastMouseY int
|
|
|
|
hovering bool
|
|
|
|
isOpen bool
|
2019-12-29 04:32:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// NewInventory creates an inventory instance and returns a pointer to it
|
2020-09-12 20:51:30 +00:00
|
|
|
func NewInventory(asset *d2asset.AssetManager, ui *d2ui.UIManager,
|
|
|
|
record *d2datadict.InventoryRecord) *Inventory {
|
2020-08-06 14:30:23 +00:00
|
|
|
hoverLabel := ui.NewLabel(d2resource.FontFormal11, d2resource.PaletteStatic)
|
2020-08-03 17:44:00 +00:00
|
|
|
hoverLabel.Alignment = d2gui.HorizontalAlignCenter
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
return &Inventory{
|
2020-09-12 20:51:30 +00:00
|
|
|
asset: asset,
|
2020-08-06 14:30:23 +00:00
|
|
|
uiManager: ui,
|
2020-09-12 20:51:30 +00:00
|
|
|
grid: NewItemGrid(asset, ui, record),
|
2020-08-03 17:44:00 +00:00
|
|
|
originX: record.Panel.Left,
|
2020-08-06 14:30:23 +00:00
|
|
|
hoverLabel: hoverLabel,
|
2020-07-11 15:25:34 +00:00
|
|
|
// originY: record.Panel.Top,
|
|
|
|
originY: 0, // expansion data has these all offset by +60 ...
|
2019-12-29 04:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// IsOpen returns true if the inventory is open
|
2019-12-29 04:32:53 +00:00
|
|
|
func (g *Inventory) IsOpen() bool {
|
|
|
|
return g.isOpen
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// Toggle negates the open state of the inventory
|
2019-12-29 04:32:53 +00:00
|
|
|
func (g *Inventory) Toggle() {
|
|
|
|
g.isOpen = !g.isOpen
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// Open opens the inventory
|
2019-12-29 04:32:53 +00:00
|
|
|
func (g *Inventory) Open() {
|
|
|
|
g.isOpen = true
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// Close closes the inventory
|
2019-12-29 04:32:53 +00:00
|
|
|
func (g *Inventory) Close() {
|
|
|
|
g.isOpen = false
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:47:11 +00:00
|
|
|
// Load the resources required by the inventory
|
2019-12-29 04:32:53 +00:00
|
|
|
func (g *Inventory) Load() {
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 21:31:45 +00:00
|
|
|
g.frame, _ = g.uiManager.NewSprite(d2resource.Frame, d2resource.PaletteSky)
|
2020-02-01 04:18:11 +00:00
|
|
|
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 21:31:45 +00:00
|
|
|
g.panel, _ = g.uiManager.NewSprite(d2resource.InventoryCharacterPanel, d2resource.PaletteSky)
|
2019-12-29 04:32:53 +00:00
|
|
|
items := []InventoryItem{
|
2020-08-03 17:44:00 +00:00
|
|
|
diablo2item.NewItem("kit", "Crimson", "of the Bat", "of Frost").Identify(),
|
|
|
|
diablo2item.NewItem("rin", "Steel", "of Shock").Identify(),
|
|
|
|
diablo2item.NewItem("jav").Identify(),
|
|
|
|
diablo2item.NewItem("buc").Identify(),
|
2020-08-11 22:01:33 +00:00
|
|
|
// diablo2item.NewItem("Arctic Furs", "qui"),
|
2020-06-23 18:12:30 +00:00
|
|
|
// TODO: Load the player's actual items
|
2019-12-29 04:32:53 +00:00
|
|
|
}
|
2020-08-11 22:01:33 +00:00
|
|
|
|
2020-08-03 17:44:00 +00:00
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotLeftArm, diablo2item.NewItem("wnd"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotRightArm, diablo2item.NewItem("buc"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotHead, diablo2item.NewItem("crn"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotTorso, diablo2item.NewItem("plt"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotLegs, diablo2item.NewItem("vbt"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotBelt, diablo2item.NewItem("vbl"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotGloves, diablo2item.NewItem("lgl"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotLeftHand, diablo2item.NewItem("rin"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotRightHand, diablo2item.NewItem("rin"))
|
|
|
|
g.grid.ChangeEquippedSlot(d2enum.EquippedSlotNeck, diablo2item.NewItem("amu"))
|
2020-06-23 18:12:30 +00:00
|
|
|
// TODO: Load the player's actual items
|
2020-08-11 22:01:33 +00:00
|
|
|
|
|
|
|
_, err := g.grid.Add(items...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not add items to the inventory, err: %v\n", err)
|
|
|
|
}
|
2019-12-29 04:32:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
// Render draws the inventory onto the given surface
|
2020-07-26 18:52:54 +00:00
|
|
|
func (g *Inventory) Render(target d2interface.Surface) error {
|
2019-12-29 04:32:53 +00:00
|
|
|
if !g.isOpen {
|
2020-07-26 18:52:54 +00:00
|
|
|
return nil
|
2019-12-29 04:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x, y := g.originX, g.originY
|
|
|
|
|
|
|
|
// Frame
|
|
|
|
// Top left
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.frame.SetCurrentFrame(5); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h := g.frame.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.frame.SetPosition(x, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.frame.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
x += w
|
|
|
|
|
|
|
|
// Top right
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.frame.SetCurrentFrame(6); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h = g.frame.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.frame.SetPosition(x, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.frame.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
x += w
|
|
|
|
y += h
|
|
|
|
|
|
|
|
// Right
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.frame.SetCurrentFrame(7); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h = g.frame.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.frame.SetPosition(x-w, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.frame.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
y += h
|
|
|
|
|
|
|
|
// Bottom right
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.frame.SetCurrentFrame(8); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h = g.frame.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.frame.SetPosition(x-w, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.frame.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
x -= w
|
|
|
|
|
|
|
|
// Bottom left
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.frame.SetCurrentFrame(9); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h = g.frame.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.frame.SetPosition(x-w, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.frame.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 04:32:53 +00:00
|
|
|
|
2020-08-03 17:47:45 +00:00
|
|
|
x, y = g.originX+1, g.originY
|
2019-12-29 04:32:53 +00:00
|
|
|
y += 64
|
|
|
|
|
|
|
|
// Panel
|
|
|
|
// Top left
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.panel.SetCurrentFrame(4); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h = g.panel.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.panel.SetPosition(x, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.panel.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
x += w
|
|
|
|
|
|
|
|
// Top right
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.panel.SetCurrentFrame(5); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
_, h = g.panel.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.panel.SetPosition(x, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.panel.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
y += h
|
|
|
|
|
|
|
|
// Bottom right
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.panel.SetCurrentFrame(7); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:01:33 +00:00
|
|
|
_, h = g.panel.GetCurrentFrameSize()
|
2019-12-29 04:32:53 +00:00
|
|
|
g.panel.SetPosition(x, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.panel.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 04:32:53 +00:00
|
|
|
|
|
|
|
// Bottom left
|
2020-07-26 18:52:54 +00:00
|
|
|
if err := g.panel.SetCurrentFrame(6); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
w, h = g.panel.GetCurrentFrameSize()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2019-12-29 04:32:53 +00:00
|
|
|
g.panel.SetPosition(x-w, y+h)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
|
|
|
if err := g.panel.Render(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 04:32:53 +00:00
|
|
|
|
|
|
|
g.grid.Render(target)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2020-08-03 17:44:00 +00:00
|
|
|
hovering := false
|
|
|
|
|
|
|
|
for idx := range g.grid.items {
|
|
|
|
item := g.grid.items[idx]
|
|
|
|
ix, iy := g.grid.SlotToScreen(item.InventoryGridSlot())
|
|
|
|
iw, ih := g.grid.sprites[item.GetItemCode()].GetCurrentFrameSize()
|
|
|
|
mx, my := g.lastMouseX, g.lastMouseY
|
|
|
|
hovering = hovering || ((mx > ix) && (mx < ix+iw) && (my > iy) && (my < iy+ih))
|
|
|
|
|
|
|
|
if hovering {
|
|
|
|
if !g.hovering {
|
|
|
|
// set the initial hover coordinates
|
|
|
|
// this is so that moving mouse doesnt move the description
|
|
|
|
g.hoverX, g.hoverY = mx, my
|
|
|
|
}
|
|
|
|
|
|
|
|
g.renderItemDescription(target, item)
|
2020-08-06 14:30:23 +00:00
|
|
|
|
2020-08-03 17:44:00 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g.hovering = hovering
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
return nil
|
2019-12-29 04:32:53 +00:00
|
|
|
}
|
2020-08-03 17:44:00 +00:00
|
|
|
|
|
|
|
func (g *Inventory) renderItemDescription(target d2interface.Surface, i InventoryItem) {
|
|
|
|
lines := i.GetItemDescription()
|
|
|
|
|
|
|
|
maxW, maxH := 0, 0
|
|
|
|
_, iy := g.grid.SlotToScreen(i.InventoryGridSlot())
|
|
|
|
|
|
|
|
for idx := range lines {
|
|
|
|
w, h := g.hoverLabel.GetTextMetrics(lines[idx])
|
|
|
|
|
|
|
|
if maxW < w {
|
|
|
|
maxW = w
|
|
|
|
}
|
|
|
|
|
|
|
|
maxH += h
|
|
|
|
}
|
|
|
|
|
|
|
|
halfW, halfH := maxW/2, maxH/2
|
2020-08-06 14:30:23 +00:00
|
|
|
centerX, centerY := g.hoverX, iy-halfH
|
2020-08-03 17:44:00 +00:00
|
|
|
|
|
|
|
if (centerX + halfW) > 800 {
|
|
|
|
centerX = 800 - halfW
|
|
|
|
}
|
|
|
|
|
|
|
|
if (centerY + halfH) > 600 {
|
|
|
|
centerY = 600 - halfH
|
|
|
|
}
|
|
|
|
|
|
|
|
target.PushTranslation(centerX, centerY)
|
|
|
|
target.PushTranslation(-halfW, -halfH)
|
|
|
|
target.DrawRect(maxW, maxH, color.RGBA{0, 0, 0, uint8(200)})
|
|
|
|
target.PushTranslation(halfW, 0)
|
|
|
|
|
|
|
|
for idx := range lines {
|
|
|
|
g.hoverLabel.SetText(lines[idx])
|
|
|
|
_, h := g.hoverLabel.GetTextMetrics(lines[idx])
|
|
|
|
g.hoverLabel.Render(target)
|
|
|
|
target.PushTranslation(0, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
target.PopN(len(lines))
|
|
|
|
target.PopN(3)
|
|
|
|
}
|