Item reference system deprecated in favor of smart pointer system. Converted all items to use pointers instead, removed all reference wrappers for item system. Fixed buggy last item sorting when removing items from InventoryScrollableWindowComponents. Implemented proper inventory removal checks when items are completely removed from an inventory. Sorted inventories now copy the pointer as a shared pointer from the main inventory code instead of needlessly creating extra copies of the same item.

This commit is contained in:
2023-12-22 06:14:37 -06:00
parent d801a41900
commit 70423be9d4
20 changed files with 259 additions and 224 deletions
+24 -21
View File
@@ -50,30 +50,30 @@ INCLUDE_ITEM_DATA
class MenuItemItemButton:public MenuIconButton{
private:
std::reference_wrapper<const Item>itemRef;
std::weak_ptr<Item>itemRef;
std::string itemNameLabelName;
std::string itemDescriptionLabelName;
bool hideQty=false;
CompactText compact=COMPACT;
public:
inline MenuItemItemButton(geom2d::rect<float>rect,const Item&itemRef,MenuType menuDest,MenuFunc onClick,std::string itemNameLabelName,std::string itemDescriptionLabelName,IconButtonAttr attributes=IconButtonAttr::SELECTABLE)
:MenuIconButton(rect,(!itemRef.IsBlank())?const_cast<Decal*>(itemRef.Decal()):nullptr,menuDest,onClick,attributes),itemRef(itemRef),itemNameLabelName(itemNameLabelName),itemDescriptionLabelName(itemDescriptionLabelName){
inline MenuItemItemButton(geom2d::rect<float>rect,const std::weak_ptr<Item>itemRef,MenuType menuDest,MenuFunc onClick,std::string itemNameLabelName,std::string itemDescriptionLabelName,IconButtonAttr attributes=IconButtonAttr::SELECTABLE)
:MenuIconButton(rect,!ISBLANK(itemRef)?const_cast<Decal*>(itemRef.lock()->Decal()):nullptr,menuDest,onClick,attributes),itemRef(itemRef),itemNameLabelName(itemNameLabelName),itemDescriptionLabelName(itemDescriptionLabelName){
draggable=false;
valid=!itemRef.IsBlank();
valid=!ISBLANK(itemRef);
}
inline MenuItemItemButton(geom2d::rect<float>rect,const Item&itemRef,MenuType menuDest,MenuFunc onClick,MenuFunc onHover,MenuFunc onMouseOut,std::string itemNameLabelName="",std::string itemDescriptionLabelName="",IconButtonAttr attributes=IconButtonAttr::SELECTABLE)
:MenuIconButton(rect,(!itemRef.IsBlank())?const_cast<Decal*>(itemRef.Decal()):nullptr,menuDest,onClick,attributes),itemRef(itemRef),itemNameLabelName(itemNameLabelName),itemDescriptionLabelName(itemDescriptionLabelName){
inline MenuItemItemButton(geom2d::rect<float>rect,const std::weak_ptr<Item>itemRef,MenuType menuDest,MenuFunc onClick,MenuFunc onHover,MenuFunc onMouseOut,std::string itemNameLabelName="",std::string itemDescriptionLabelName="",IconButtonAttr attributes=IconButtonAttr::SELECTABLE)
:MenuIconButton(rect,!ISBLANK(itemRef)?const_cast<Decal*>(itemRef.lock()->Decal()):nullptr,menuDest,onClick,attributes),itemRef(itemRef),itemNameLabelName(itemNameLabelName),itemDescriptionLabelName(itemDescriptionLabelName){
runHoverFunctions=true;
draggable=false;
valid=!itemRef.IsBlank();
valid=!ISBLANK(itemRef);
SetHoverFunc(onHover);
SetMouseOutFunc(onMouseOut);
}
inline const Item&GetItem(){
return itemRef.get();
inline const std::weak_ptr<Item>GetItem(){
return itemRef;
}
inline const Item&SetItem(const Item&newItem){
return itemRef=std::reference_wrapper<const Item>(newItem);
inline const std::weak_ptr<Item>SetItem(const std::weak_ptr<Item>newItem){
return itemRef=newItem;
}
inline void SetShowQuantity(bool show){
this->hideQty=!show;
@@ -83,11 +83,11 @@ public:
else this->compact=NON_COMPACT;
}
inline void UpdateIcon(){
if(itemRef.get().IsBlank()){
if(ISBLANK(itemRef)){
icon=nullptr;
return;
}
icon=const_cast<Decal*>(itemRef.get().Decal());
icon=const_cast<Decal*>(itemRef.lock()->Decal());
}
protected:
virtual inline void OnMouseOut()override{
@@ -105,16 +105,16 @@ protected:
std::string labelNameText;
std::string labelDescriptionText;
if(itemRef.get().IsBlank()){
if(ISBLANK(itemRef)){
icon=nullptr;
labelNameText="";
labelDescriptionText="";
return;
}
icon=const_cast<Decal*>(itemRef.get().Decal());
labelNameText=itemRef.get().DisplayName();
labelDescriptionText=itemRef.get().Description(compact);
icon=const_cast<Decal*>(itemRef.lock()->Decal());
labelNameText=itemRef.lock()->DisplayName();
labelDescriptionText=itemRef.lock()->Description(compact);
if(itemNameLabelName!=""){
Component<MenuLabel>(parentMenu,itemNameLabelName)->label=labelNameText;
Component<MenuLabel>(parentMenu,itemNameLabelName)->Enable(true);
@@ -126,8 +126,11 @@ protected:
}
virtual inline void Update(Crawler*game)override{
MenuIconButton::Update(game);
valid=!itemRef.get().IsBlank();
valid=!ISBLANK(itemRef);
if(!valid){
icon=nullptr;
}
if(hovered){
UpdateLabel();
}
@@ -135,11 +138,11 @@ protected:
virtual inline void DrawDecal(ViewPort&window,bool focused)override{
MenuIconButton::DrawDecal(window,focused);
if(valid&&!hideQty){
std::string quantityText="x"+std::to_string(itemRef.get().Amt());
std::string quantityText="x"+std::to_string(itemRef.lock()->Amt());
vf2d quantityTextScale=rect.size/48.f;
vf2d textSize=vf2d(game->GetTextSizeProp(quantityText))*quantityTextScale;
vf2d drawPos=rect.pos+rect.size-textSize;
if(itemRef.get().Amt()!=INFINITE){
if(itemRef.lock()->Amt()!=INFINITE){
window.DrawShadowStringDecal(drawPos,quantityText,WHITE,BLACK,quantityTextScale);
}
}