rename item data to count, add function to get current class name in lua

This commit is contained in:
Eduardo Bart 2012-02-01 13:20:13 -02:00
parent 266917cc3c
commit 2a62159a61
13 changed files with 41 additions and 34 deletions

View File

@ -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')

View File

@ -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

View File

@ -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')

View File

@ -48,7 +48,8 @@ void LuaInterface::init()
// register LuaObject, the base of all other objects
registerClass<LuaObject>();
bindClassMemberGetField<LuaObject>("use_count", &LuaObject::getUseCount);
bindClassMemberFunction<LuaObject>("getUseCount", &LuaObject::getUseCount);
bindClassMemberFunction<LuaObject>("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();
}

View File

@ -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());
}

View File

@ -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;
}

View File

@ -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<Item>(shared_from_this()); }
private:
uint8 m_data;
uint8 m_count;
};
#endif

View File

@ -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));
}
}

View File

@ -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;

View File

@ -129,7 +129,7 @@ void OTClient::registerLuaFunctions()
g_lua.registerClass<Item, Thing>();
g_lua.bindClassStaticFunction<Item>("create", &Item::create);
g_lua.bindClassMemberFunction<Item>("getData", &Item::getData);
g_lua.bindClassMemberFunction<Item>("getCount", &Item::getCount);
g_lua.registerClass<Effect, Thing>();
g_lua.registerClass<Missile, Thing>();

View File

@ -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;
}

View File

@ -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)

View File

@ -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; }