2019-12-28 23:32:53 -05:00
|
|
|
package d2player
|
|
|
|
|
|
|
|
import (
|
2020-11-22 17:53:33 -05:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
|
|
|
|
|
2020-06-23 14:12:30 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-11-18 16:02:49 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-08-03 13:44:00 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2item/diablo2item"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
2019-12-28 23:32:53 -05:00
|
|
|
)
|
|
|
|
|
2020-10-25 03:42:31 -04:00
|
|
|
const (
|
|
|
|
frameInventoryTopLeft = 4
|
|
|
|
frameInventoryTopRight = 5
|
|
|
|
frameInventoryBottomLeft = 6
|
|
|
|
frameInventoryBottomRight = 7
|
|
|
|
)
|
|
|
|
|
2020-10-25 10:54:39 -04:00
|
|
|
const (
|
|
|
|
invCloseButtonX, invCloseButtonY = 419, 449
|
2020-11-22 17:53:33 -05:00
|
|
|
invGoldButtonX, invGoldButtonY = 485, 455
|
|
|
|
invGoldLabelX, invGoldLabelY = 510, 455
|
2020-10-25 10:54:39 -04:00
|
|
|
)
|
|
|
|
|
2020-11-18 16:02:49 -05:00
|
|
|
// NewInventory creates an inventory instance and returns a pointer to it
|
|
|
|
func NewInventory(asset *d2asset.AssetManager,
|
|
|
|
ui *d2ui.UIManager,
|
|
|
|
l d2util.LogLevel,
|
2020-11-22 17:53:33 -05:00
|
|
|
gold int,
|
2020-12-10 07:15:18 -05:00
|
|
|
record *d2records.InventoryRecord) (*Inventory, error) {
|
2020-11-18 16:02:49 -05:00
|
|
|
itemTooltip := ui.NewTooltip(d2resource.FontFormal11, d2resource.PaletteStatic, d2ui.TooltipXCenter, d2ui.TooltipYBottom)
|
|
|
|
|
2020-12-10 07:15:18 -05:00
|
|
|
itemFactory, err := diablo2item.NewItemFactory(asset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("during creating new item factory: %s", err)
|
|
|
|
}
|
2020-11-18 16:02:49 -05:00
|
|
|
|
2020-12-12 13:29:12 -05:00
|
|
|
mgp := NewMoveGoldPanel(asset, ui, gold, l)
|
2020-11-18 16:02:49 -05:00
|
|
|
|
|
|
|
inventory := &Inventory{
|
|
|
|
asset: asset,
|
|
|
|
uiManager: ui,
|
|
|
|
item: itemFactory,
|
|
|
|
grid: NewItemGrid(asset, ui, l, record),
|
|
|
|
originX: record.Panel.Left,
|
|
|
|
itemTooltip: itemTooltip,
|
|
|
|
// originY: record.Panel.Top,
|
2020-12-12 04:39:26 -05:00
|
|
|
originY: 0, // expansion data has these all offset by +60 ...
|
|
|
|
gold: gold,
|
|
|
|
moveGoldPanel: mgp,
|
2020-11-18 16:02:49 -05:00
|
|
|
}
|
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
inventory.moveGoldPanel.SetOnCloseCb(func() { inventory.onCloseGoldPanel() })
|
|
|
|
|
2020-11-22 00:36:59 -05:00
|
|
|
inventory.Logger = d2util.NewLogger()
|
|
|
|
inventory.Logger.SetLevel(l)
|
|
|
|
inventory.Logger.SetPrefix(logPrefix)
|
2020-11-18 16:02:49 -05:00
|
|
|
|
2020-12-10 07:15:18 -05:00
|
|
|
return inventory, nil
|
2020-11-18 16:02:49 -05:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:01:33 -04:00
|
|
|
// Inventory represents the inventory
|
2019-12-28 23:32:53 -05:00
|
|
|
type Inventory struct {
|
2020-11-22 17:53:33 -05:00
|
|
|
asset *d2asset.AssetManager
|
|
|
|
item *diablo2item.ItemFactory
|
|
|
|
uiManager *d2ui.UIManager
|
|
|
|
panel *d2ui.Sprite
|
2020-12-12 04:39:26 -05:00
|
|
|
goldLabel *d2ui.Label
|
2020-11-22 17:53:33 -05:00
|
|
|
grid *ItemGrid
|
|
|
|
itemTooltip *d2ui.Tooltip
|
|
|
|
panelGroup *d2ui.WidgetGroup
|
|
|
|
hoverX int
|
|
|
|
hoverY int
|
|
|
|
originX int
|
|
|
|
originY int
|
|
|
|
lastMouseX int
|
|
|
|
lastMouseY int
|
|
|
|
hovering bool
|
|
|
|
isOpen bool
|
|
|
|
onCloseCb func()
|
|
|
|
gold int
|
2020-12-12 04:39:26 -05:00
|
|
|
moveGoldPanel *MoveGoldPanel
|
2020-09-20 17:52:01 -04:00
|
|
|
|
2020-11-22 00:36:59 -05:00
|
|
|
*d2util.Logger
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:01:33 -04:00
|
|
|
// Toggle negates the open state of the inventory
|
2019-12-28 23:32:53 -05:00
|
|
|
func (g *Inventory) Toggle() {
|
2020-10-25 10:54:39 -04:00
|
|
|
if g.isOpen {
|
|
|
|
g.Close()
|
|
|
|
} else {
|
|
|
|
g.Open()
|
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
// Load the resources required by the inventory
|
2019-12-28 23:32:53 -05:00
|
|
|
func (g *Inventory) Load() {
|
2020-11-22 17:53:33 -05:00
|
|
|
var err error
|
|
|
|
|
2020-12-02 02:19:15 -05:00
|
|
|
g.panelGroup = g.uiManager.NewWidgetGroup(d2ui.RenderPriorityInventory)
|
2020-11-22 17:53:33 -05:00
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
frame := d2ui.NewUIFrame(g.asset, g.uiManager, d2ui.FrameRight)
|
|
|
|
g.panelGroup.AddWidget(frame)
|
|
|
|
|
|
|
|
g.panel, err = g.uiManager.NewSprite(d2resource.InventoryCharacterPanel, d2resource.PaletteSky)
|
|
|
|
if err != nil {
|
|
|
|
g.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
closeButton := g.uiManager.NewButton(d2ui.ButtonTypeSquareClose, "")
|
|
|
|
closeButton.SetVisible(false)
|
|
|
|
closeButton.SetPosition(invCloseButtonX, invCloseButtonY)
|
|
|
|
closeButton.OnActivated(func() { g.Close() })
|
|
|
|
g.panelGroup.AddWidget(closeButton)
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
goldButton := g.uiManager.NewButton(d2ui.ButtonTypeGoldCoin, "")
|
|
|
|
goldButton.SetVisible(false)
|
|
|
|
goldButton.SetPosition(invGoldButtonX, invGoldButtonY)
|
|
|
|
goldButton.OnActivated(func() { g.onGoldClicked() })
|
2020-11-22 17:53:33 -05:00
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
// nolint:gocritic // this variable will be used in future
|
|
|
|
// deposite := g.asset.TranslateString("strGoldDeposit")
|
|
|
|
drop := g.asset.TranslateString("strGoldDrop")
|
|
|
|
// nolint:gocritic // this variable will be used in future
|
|
|
|
// withdraw := g.asset.TranslateString("strGoldWithdraw")
|
|
|
|
|
|
|
|
tooltip := g.uiManager.NewTooltip(d2resource.Font16, d2resource.PaletteSky, d2ui.TooltipXCenter, d2ui.TooltipYBottom)
|
|
|
|
// here should be switch-case statement for each of move-gold button descr
|
|
|
|
tooltip.SetText(drop)
|
|
|
|
tooltip.SetPosition(invGoldButtonX, invGoldButtonY)
|
|
|
|
goldButton.SetTooltip(tooltip)
|
|
|
|
|
|
|
|
g.panelGroup.AddWidget(goldButton)
|
2020-11-22 17:53:33 -05:00
|
|
|
|
|
|
|
g.goldLabel = g.uiManager.NewLabel(d2resource.Font16, d2resource.PaletteStatic)
|
|
|
|
g.goldLabel.Alignment = d2ui.HorizontalAlignLeft
|
2020-12-12 04:39:26 -05:00
|
|
|
g.goldLabel.SetText(fmt.Sprintln(g.moveGoldPanel.gold))
|
2020-11-22 17:53:33 -05:00
|
|
|
g.goldLabel.SetPosition(invGoldLabelX, invGoldLabelY)
|
|
|
|
g.panelGroup.AddWidget(g.goldLabel)
|
|
|
|
|
2020-10-25 10:21:14 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/795
|
2020-09-20 17:52:01 -04:00
|
|
|
testInventoryCodes := [][]string{
|
|
|
|
{"kit", "Crimson", "of the Bat", "of Frost"},
|
|
|
|
{"rin", "Steel", "of Shock"},
|
|
|
|
{"jav"},
|
|
|
|
{"buc"},
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryItems := make([]InventoryItem, 0)
|
|
|
|
|
|
|
|
for idx := range testInventoryCodes {
|
2020-11-22 17:53:33 -05:00
|
|
|
item, itemErr := g.item.NewItem(testInventoryCodes[idx]...)
|
|
|
|
if itemErr != nil {
|
2020-09-20 17:52:01 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
item.Identify()
|
|
|
|
inventoryItems = append(inventoryItems, item)
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:21:14 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/795
|
2020-09-20 17:52:01 -04:00
|
|
|
testEquippedItemCodes := map[d2enum.EquippedSlot][]string{
|
|
|
|
d2enum.EquippedSlotLeftArm: {"wnd"},
|
|
|
|
d2enum.EquippedSlotRightArm: {"buc"},
|
|
|
|
d2enum.EquippedSlotHead: {"crn"},
|
|
|
|
d2enum.EquippedSlotTorso: {"plt"},
|
|
|
|
d2enum.EquippedSlotLegs: {"vbt"},
|
|
|
|
d2enum.EquippedSlotBelt: {"vbl"},
|
|
|
|
d2enum.EquippedSlotGloves: {"lgl"},
|
|
|
|
d2enum.EquippedSlotLeftHand: {"rin"},
|
|
|
|
d2enum.EquippedSlotRightHand: {"rin"},
|
|
|
|
d2enum.EquippedSlotNeck: {"amu"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for slot := range testEquippedItemCodes {
|
2020-11-22 17:53:33 -05:00
|
|
|
item, itemErr := g.item.NewItem(testEquippedItemCodes[slot]...)
|
|
|
|
if itemErr != nil {
|
2020-09-20 17:52:01 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
g.grid.ChangeEquippedSlot(slot, item)
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
2020-08-11 18:01:33 -04:00
|
|
|
|
2020-11-22 17:53:33 -05:00
|
|
|
_, err = g.grid.Add(inventoryItems...)
|
2020-08-11 18:01:33 -04:00
|
|
|
if err != nil {
|
2020-11-22 00:36:59 -05:00
|
|
|
g.Errorf("could not add items to the inventory, err: %v", err.Error())
|
2020-08-11 18:01:33 -04:00
|
|
|
}
|
2020-12-12 04:39:26 -05:00
|
|
|
|
|
|
|
g.moveGoldPanel.Load()
|
|
|
|
|
|
|
|
g.panelGroup.SetVisible(false)
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
|
2020-11-22 17:53:33 -05:00
|
|
|
// Open opens the inventory
|
|
|
|
func (g *Inventory) Open() {
|
|
|
|
g.isOpen = true
|
|
|
|
g.panelGroup.SetVisible(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the inventory
|
|
|
|
func (g *Inventory) Close() {
|
|
|
|
g.isOpen = false
|
2020-12-12 04:39:26 -05:00
|
|
|
g.moveGoldPanel.Close()
|
2020-11-22 17:53:33 -05:00
|
|
|
g.panelGroup.SetVisible(false)
|
|
|
|
g.onCloseCb()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOnCloseCb the callback run on closing the inventory
|
|
|
|
func (g *Inventory) SetOnCloseCb(cb func()) {
|
|
|
|
g.onCloseCb = cb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Inventory) onGoldClicked() {
|
2020-12-12 04:39:26 -05:00
|
|
|
g.Info("Move gold action clicked")
|
|
|
|
g.toggleMoveGoldPanel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Inventory) toggleMoveGoldPanel() {
|
|
|
|
g.moveGoldPanel.Toggle()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Inventory) onCloseGoldPanel() {
|
|
|
|
|
2020-11-22 17:53:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsOpen returns true if the inventory is open
|
|
|
|
func (g *Inventory) IsOpen() bool {
|
|
|
|
return g.isOpen
|
|
|
|
}
|
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
// Advance advances the state of the Inventory
|
|
|
|
func (g *Inventory) Advance(_ float64) {
|
|
|
|
if !g.IsOpen() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g.goldLabel.SetText(fmt.Sprintln(g.moveGoldPanel.gold))
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:01:33 -04:00
|
|
|
// Render draws the inventory onto the given surface
|
2020-10-28 14:17:42 -04:00
|
|
|
func (g *Inventory) Render(target d2interface.Surface) {
|
2019-12-28 23:32:53 -05:00
|
|
|
if !g.isOpen {
|
2020-10-28 14:17:42 -04:00
|
|
|
return
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
g.renderFrame(target)
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
g.grid.Render(target)
|
|
|
|
g.renderItemHover(target)
|
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
func (g *Inventory) renderFrame(target d2interface.Surface) {
|
2020-10-26 05:24:04 -04:00
|
|
|
frames := []int{
|
|
|
|
frameInventoryTopLeft,
|
|
|
|
frameInventoryTopRight,
|
|
|
|
frameInventoryBottomRight,
|
|
|
|
frameInventoryBottomLeft,
|
2020-07-26 14:52:54 -04:00
|
|
|
}
|
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
x, y := g.originX+1, g.originY
|
|
|
|
y += 64
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
for _, frame := range frames {
|
|
|
|
if err := g.panel.SetCurrentFrame(frame); err != nil {
|
2020-12-12 04:39:26 -05:00
|
|
|
g.Error(err.Error())
|
|
|
|
return
|
2020-10-26 05:24:04 -04:00
|
|
|
}
|
2019-12-28 23:32:53 -05:00
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
w, h := g.panel.GetCurrentFrameSize()
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
g.panel.SetPosition(x, y+h)
|
2020-11-11 09:05:04 -05:00
|
|
|
g.panel.Render(target)
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
switch frame {
|
|
|
|
case frameInventoryTopLeft:
|
|
|
|
x += w
|
|
|
|
case frameInventoryTopRight:
|
|
|
|
y += h
|
|
|
|
case frameInventoryBottomRight:
|
2020-10-26 08:00:30 -04:00
|
|
|
x = g.originX + 1
|
2020-10-26 05:24:04 -04:00
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
}
|
2020-10-26 05:24:04 -04:00
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-10-26 05:24:04 -04:00
|
|
|
func (g *Inventory) renderItemHover(target d2interface.Surface) {
|
2020-08-03 13:44:00 -04: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 10:30:23 -04:00
|
|
|
|
2020-08-03 13:44:00 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g.hovering = hovering
|
2019-12-28 23:32:53 -05:00
|
|
|
}
|
2020-08-03 13:44:00 -04:00
|
|
|
|
|
|
|
func (g *Inventory) renderItemDescription(target d2interface.Surface, i InventoryItem) {
|
2020-12-12 04:39:26 -05:00
|
|
|
if !g.moveGoldPanel.IsOpen() {
|
|
|
|
lines := i.GetItemDescription()
|
|
|
|
g.itemTooltip.SetTextLines(lines)
|
|
|
|
_, y := g.grid.SlotToScreen(i.InventoryGridSlot())
|
2020-11-09 09:46:08 -05:00
|
|
|
|
2020-12-12 04:39:26 -05:00
|
|
|
g.itemTooltip.SetPosition(g.hoverX, y)
|
|
|
|
g.itemTooltip.Render(target)
|
|
|
|
}
|
2020-08-03 13:44:00 -04:00
|
|
|
}
|