From 2a62159a61c0b916042c37f922643b4bb2d737ea Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 1 Feb 2012 13:20:13 -0200 Subject: [PATCH] rename item data to count, add function to get current class name in lua --- modules/core_widgets/uiitem.lua | 2 +- modules/game/game.lua | 4 ++-- modules/game/map.lua | 2 +- src/framework/luascript/luainterface.cpp | 5 +++-- src/framework/luascript/luaobject.h | 4 ++-- src/otclient/core/item.cpp | 20 ++++++++++---------- src/otclient/core/item.h | 6 +++--- src/otclient/core/map.cpp | 4 ++-- src/otclient/core/thing.h | 4 ++-- src/otclient/luafunctions.cpp | 2 +- src/otclient/net/protocolgameparse.cpp | 2 +- src/otclient/ui/uiitem.cpp | 15 ++++++++++----- src/otclient/ui/uiitem.h | 5 +++-- 13 files changed, 41 insertions(+), 34 deletions(-) diff --git a/modules/core_widgets/uiitem.lua b/modules/core_widgets/uiitem.lua index d6766d27..a9883839 100644 --- a/modules/core_widgets/uiitem.lua +++ b/modules/core_widgets/uiitem.lua @@ -30,7 +30,7 @@ function UIItem:onDrop(widget, mousePos) if not widget or not widget.currentDragThing then return true end local pos = self.position - local data = widget.currentDragThing:getData() + local data = widget.currentDragThing:getCount() if widget.currentDragThing:isStackable() and data > 1 then widget.parsed = true local moveWindow = displayUI('/game/movewindow.otui') diff --git a/modules/game/game.lua b/modules/game/game.lua index 6c2c334a..ec2e088b 100644 --- a/modules/game/game.lua +++ b/modules/game/game.lua @@ -21,12 +21,12 @@ local function onUseWithMouseRelease(self, mousePosition, mouseButton) if mouseButton == MouseLeftButton then local clickedWidget = Game.gameUi:recursiveGetChildByPos(mousePosition) if clickedWidget then - if clickedWidget.getTile then + if clickedWidget:getClassName() == 'Tile' then local tile = clickedWidget:getTile(mousePosition) if tile then Game.useWith(Game.selectedThing, tile:getTopMultiUseThing()) end - elseif clickedWidget.getItem and not clickedWidget:isVirtual() then + elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then Game.useWith(Game.selectedThing, clickedWidget:getItem()) end end diff --git a/modules/game/map.lua b/modules/game/map.lua index 25b6ee74..be7d9e64 100644 --- a/modules/game/map.lua +++ b/modules/game/map.lua @@ -26,7 +26,7 @@ function UIMap:onDrop(widget, mousePos) local tile = self:getTile(mousePos) if not tile then return false end - local data = widget.currentDragThing:getData() + local data = widget.currentDragThing:getCount() if widget.currentDragThing:isStackable() and data > 1 then widget.parsed = true local moveWindow = displayUI('/game/movewindow.otui') diff --git a/src/framework/luascript/luainterface.cpp b/src/framework/luascript/luainterface.cpp index 64ad66ac..77d57fda 100644 --- a/src/framework/luascript/luainterface.cpp +++ b/src/framework/luascript/luainterface.cpp @@ -48,7 +48,8 @@ void LuaInterface::init() // register LuaObject, the base of all other objects registerClass(); - bindClassMemberGetField("use_count", &LuaObject::getUseCount); + bindClassMemberFunction("getUseCount", &LuaObject::getUseCount); + bindClassMemberFunction("getClassName", &LuaObject::getClassName); } void LuaInterface::terminate() @@ -958,7 +959,7 @@ void LuaInterface::pushObject(const LuaObjectPtr& obj) new(newUserdata(sizeof(LuaObjectPtr))) LuaObjectPtr(obj); // set the userdata metatable - getGlobal(Fw::mkstr(obj->getLuaObjectName(), "_mt")); + getGlobal(Fw::mkstr(obj->getClassName(), "_mt")); assert(!isNil()); setMetatable(); } diff --git a/src/framework/luascript/luaobject.h b/src/framework/luascript/luaobject.h index e9f17276..a5de7922 100644 --- a/src/framework/luascript/luaobject.h +++ b/src/framework/luascript/luaobject.h @@ -61,8 +61,8 @@ public: /// @note each userdata of this object on lua counts as a reference int getUseCount(); - /// Returns the class name used in Lua - virtual std::string getLuaObjectName() const { + /// Returns the derived class name, its the same name used in Lua + virtual std::string getClassName() const { // TODO: this could be cached for more performance return Fw::demangleName(typeid(*this).name()); } diff --git a/src/otclient/core/item.cpp b/src/otclient/core/item.cpp index 807c6a5f..4bc14852 100644 --- a/src/otclient/core/item.cpp +++ b/src/otclient/core/item.cpp @@ -32,7 +32,7 @@ Item::Item() : Thing() { - m_data = 1; + m_count = 1; } ItemPtr Item::create(int id) @@ -77,26 +77,26 @@ void Item::setPosition(const Position& position) Thing::setPosition(position); } -void Item::setData(uint8 data) +void Item::setCount(uint8 count) { if(isStackable() && getNumPatternsX() == 4 && getNumPatternsY() == 2) { - if(data < 5) { - m_xPattern = data-1; + if(count < 5) { + m_xPattern = count-1; m_yPattern = 0; } - else if(data < 10) { + else if(count < 10) { m_xPattern = 0; m_yPattern = 1; } - else if(data < 25) { + else if(count < 25) { m_xPattern = 1; m_yPattern = 1; } - else if(data < 50) { + else if(count < 50) { m_xPattern = 2; m_yPattern = 1; } - else if(data <= 100) { + else if(count <= 100) { m_xPattern = 3; m_yPattern = 1; } @@ -111,7 +111,7 @@ void Item::setData(uint8 data) } else if(isFluid() || isFluidContainer()) { int color = Otc::FluidTransparent; - switch(data) { + switch(count) { case Otc::FluidNone: color = Otc::FluidTransparent; break; @@ -175,5 +175,5 @@ void Item::setData(uint8 data) m_yPattern = (color / 4) % getNumPatternsY(); } - m_data = data; + m_count = count; } diff --git a/src/otclient/core/item.h b/src/otclient/core/item.h index 0a01f027..f6bbfd3c 100644 --- a/src/otclient/core/item.h +++ b/src/otclient/core/item.h @@ -36,14 +36,14 @@ public: void draw(const Point& dest, float scaleFactor); void setPosition(const Position &position); - void setData(uint8 data); + void setCount(uint8 data); - uint8 getData() { return m_data; } + uint8 getCount() { return m_count; } ItemPtr asItem() { return std::static_pointer_cast(shared_from_this()); } private: - uint8 m_data; + uint8 m_count; }; #endif diff --git a/src/otclient/core/map.cpp b/src/otclient/core/map.cpp index d0d17a1d..b3839080 100644 --- a/src/otclient/core/map.cpp +++ b/src/otclient/core/map.cpp @@ -71,7 +71,7 @@ void Map::load() if(item->isStackable() || item->isFluidContainer() || item->isFluid()) { uint8 data; in.read((char*)&data, sizeof(data)); - item->setData(data); + item->setCount(data); } addThing(item, pos, 255); in.read((char*)&id, sizeof(id)); @@ -95,7 +95,7 @@ void Map::save() id = item->getId(); out.write((char*)&id, sizeof(id)); if(item->isStackable() || item->isFluidContainer() || item->isFluid()) { - uint8 data = item->getData(); + uint8 data = item->getCount(); out.write((char*)&data, sizeof(data)); } } diff --git a/src/otclient/core/thing.h b/src/otclient/core/thing.h index a173d001..ca0ed219 100644 --- a/src/otclient/core/thing.h +++ b/src/otclient/core/thing.h @@ -108,9 +108,9 @@ protected: void internalDraw(const Point& dest, float scaleFactor, int layer); void updateType(); - uint32 m_id; + uint32 m_id; //TODO: move to derived class to use less memory Position m_position; - uint8 m_xPattern, m_yPattern, m_zPattern, m_animation; + uint8 m_xPattern, m_yPattern, m_zPattern, m_animation; //TODO: remove this variables to use less memory private: ThingType *m_type; diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index e0201323..a1c38f92 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -129,7 +129,7 @@ void OTClient::registerLuaFunctions() g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &Item::create); - g_lua.bindClassMemberFunction("getData", &Item::getData); + g_lua.bindClassMemberFunction("getCount", &Item::getCount); g_lua.registerClass(); g_lua.registerClass(); diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index a4d464ac..72ca6635 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -1125,7 +1125,7 @@ ItemPtr ProtocolGame::internalGetItem(InputMessage& msg, int id) ItemPtr item = Item::create(id); if(item->isStackable() || item->isFluidContainer() || item->isFluid()) - item->setData(msg.getU8()); + item->setCount(msg.getU8()); return item; } diff --git a/src/otclient/ui/uiitem.cpp b/src/otclient/ui/uiitem.cpp index d0e51b5f..d41434dc 100644 --- a/src/otclient/ui/uiitem.cpp +++ b/src/otclient/ui/uiitem.cpp @@ -40,8 +40,8 @@ void UIItem::draw() g_painter.setColor(Fw::white); m_item->draw(topLeft, 1); - if(m_font && m_item->isStackable() && m_item->getData() > 1) { - std::string count = Fw::tostring(m_item->getData()); + if(m_font && m_item->isStackable() && m_item->getCount() > 1) { + std::string count = Fw::tostring(m_item->getCount()); m_font->renderText(count, Rect(m_rect.topLeft(), m_rect.bottomRight() - Point(3, 0)), Fw::AlignBottomRight, Color(231, 231, 231)); } @@ -56,14 +56,19 @@ void UIItem::setItemId(int id) { if(!m_item) m_item = Item::create(id); - else - m_item->setId(id); + else { + // remove item + if(id == 0) + m_item = nullptr; + else + m_item->setId(id); + } } void UIItem::setItemCount(int count) { if(m_item) - m_item->setData(count); + m_item->setCount(count); } void UIItem::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode) diff --git a/src/otclient/ui/uiitem.h b/src/otclient/ui/uiitem.h index 78ff6002..d1d148a2 100644 --- a/src/otclient/ui/uiitem.h +++ b/src/otclient/ui/uiitem.h @@ -37,9 +37,10 @@ public: void setItemCount(int count); void setItem(const ItemPtr& item) { m_item = item; } void setVirtual(bool virt) { m_virtual = virt; } + void clearItem() { setItemId(0); } - int getItemId() { return m_item->getId(); } - int getItemCount() { return m_item->getData(); } + int getItemId() { return m_item ? m_item->getId() : 0; } + int getItemCount() { return m_item ? m_item->getCount() : 0; } ItemPtr getItem() { return m_item; } bool isVirtual() { return m_virtual; }