init move items

This commit is contained in:
Henrique Santiago 2012-01-19 23:12:26 -02:00
parent 168f03125c
commit 733039e50e
11 changed files with 112 additions and 2 deletions

View File

@ -1,3 +1,29 @@
function UIItem:onDragEnter(mousePos)
local item = self:getItem()
if not item then return false end
self.currentDragThing = item
setTargetCursor()
return true
end
function UIItem:onDragLeave(widget, mousePos)
self.currentDragThing = nil
restoreCursor()
return true
end
function UIItem:onDrop(widget, mousePos)
if not widget or not widget.currentDragThing then return false end
local pos = self.position
local count = 1 -- todo make a window for it
Game.move(widget.currentDragThing, pos, count)
return true
end
function UIItem:onMouseRelease(mousePosition, mouseButton) function UIItem:onMouseRelease(mousePosition, mouseButton)
local item = self:getItem() local item = self:getItem()
if not item or not self:containsPoint(mousePosition) then return false end if not item or not self:containsPoint(mousePosition) then return false end

View File

@ -1,5 +1,34 @@
function UIMap:onDragEnter(mousePos)
local tile = self:getTile(mousePosition)
if not tile then return false end
local thing = tile:getTopMoveThing()
if not thing then return false end
self.currentDragThing = thing
setTargetCursor()
return true
end
function UIMap:onDragLeave(widget, mousePos)
self.currentDragThing = nil
restoreCursor()
return true
end
function UIMap:onDrop(widget, mousePos)
if not widget or not widget.currentDragThing then return false end
local pos = self:getPosition(mousePos)
local count = 1 -- todo make a window for it
Game.move(widget.currentDragThing, pos, count)
return true
end
function UIMap:onMouseRelease(mousePosition, mouseButton) function UIMap:onMouseRelease(mousePosition, mouseButton)
local tile = self:getTile(mousePosition) local tile = self:getTile(mousePosition)
if tile and Game.processMouseAction(mousePosition, mouseButton, nil, tile:getTopLookThing(), tile:getTopUseThing(), tile:getTopCreature(), tile:getTopMultiUseThing()) then return true end if tile and Game.processMouseAction(mousePosition, mouseButton, nil, tile:getTopLookThing(), tile:getTopUseThing(), tile:getTopCreature(), tile:getTopMultiUseThing()) then return true end
return false return false
end end

View File

@ -42,6 +42,7 @@ function Containers.onContainerOpen(containerId, itemId, name, capacity, hasPare
local item = UIItem.create() local item = UIItem.create()
item:setStyle('Item') item:setStyle('Item')
container:addChild(item) container:addChild(item)
item.position = {x=65535, y=containerId+64, z=i-1}
end end
m_containers[containerId] = container m_containers[containerId] = container

View File

@ -11,6 +11,7 @@ UIWindow
id: slot1 id: slot1
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
&position: {x=65535, y=1, z=0}
Item Item
// armor // armor
@ -18,6 +19,7 @@ UIWindow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5 margin-top: 5
&position: {x=65535, y=4, z=0}
Item Item
// legs // legs
@ -25,6 +27,7 @@ UIWindow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5 margin-top: 5
&position: {x=65535, y=7, z=0}
Item Item
// feet // feet
@ -32,6 +35,7 @@ UIWindow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5 margin-top: 5
&position: {x=65535, y=8, z=0}
Item Item
// necklace // necklace
@ -40,6 +44,7 @@ UIWindow
anchors.right: slot1.left anchors.right: slot1.left
margin-top: 10 margin-top: 10
margin-right: 5 margin-right: 5
&position: {x=65535, y=2, z=0}
Item Item
// left // left
@ -47,6 +52,7 @@ UIWindow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5 margin-top: 5
&position: {x=65535, y=6, z=0}
Item Item
// ring // ring
@ -54,6 +60,7 @@ UIWindow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5 margin-top: 5
&position: {x=65535, y=9, z=0}
Item Item
// backpack // backpack
@ -62,6 +69,7 @@ UIWindow
anchors.left: slot1.right anchors.left: slot1.right
margin-top: 10 margin-top: 10
margin-left: 5 margin-left: 5
&position: {x=65535, y=3, z=0}
Item Item
// right // right
@ -76,6 +84,7 @@ UIWindow
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5 margin-top: 5
&position: {x=65535, y=10, z=0}
GameLabel GameLabel
id: soul id: soul

View File

@ -299,6 +299,20 @@ void Game::useInventoryItem(int itemId, const ThingPtr& toThing)
} }
} }
void Game::move(const ThingPtr& thing, const Position& toPos, int count)
{
if(!isOnline() || !thing || !checkBotProtection() || thing->getPos() == toPos || count <= 0)
return;
m_localPlayer->lockWalk();
int stackpos = getThingStackpos(thing);
if(stackpos == -1)
return;
m_protocolGame->sendThrow(thing->getPos(), thing->getId(), stackpos, toPos, count);
}
void Game::attack(const CreaturePtr& creature) void Game::attack(const CreaturePtr& creature)
{ {
if(!isOnline() || !creature || !checkBotProtection()) if(!isOnline() || !creature || !checkBotProtection())

View File

@ -65,6 +65,7 @@ public:
void use(const ThingPtr& thing); void use(const ThingPtr& thing);
void useWith(const ThingPtr& fromThing, const ThingPtr& toThing); void useWith(const ThingPtr& fromThing, const ThingPtr& toThing);
void useInventoryItem(int itemId, const ThingPtr& toThing); void useInventoryItem(int itemId, const ThingPtr& toThing);
void move(const ThingPtr &thing, const Position& toPos, int count);
// attack/follow related // attack/follow related
void attack(const CreaturePtr& creature); void attack(const CreaturePtr& creature);

View File

@ -247,6 +247,23 @@ CreaturePtr Tile::getTopCreature()
return creature; return creature;
} }
ThingPtr Tile::getTopMoveThing()
{
if(isEmpty())
return nullptr;
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->asCreature()) {
if(i > 0 && thing->isNotMoveable())
return m_things[i-1];
return thing;
}
}
return m_things[0];
}
ThingPtr Tile::getTopMultiUseThing() ThingPtr Tile::getTopMultiUseThing()
{ {
// this is related to classic controls, getting top item, forceuse or creature // this is related to classic controls, getting top item, forceuse or creature

View File

@ -48,6 +48,7 @@ public:
ThingPtr getTopLookThing(); ThingPtr getTopLookThing();
ThingPtr getTopUseThing(); ThingPtr getTopUseThing();
CreaturePtr getTopCreature(); CreaturePtr getTopCreature();
ThingPtr getTopMoveThing();
ThingPtr getTopMultiUseThing(); ThingPtr getTopMultiUseThing();
const Position& getPos() { return m_pos; } const Position& getPos() { return m_pos; }

View File

@ -169,6 +169,7 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassMemberFunction<Tile>("getTopLookThing", &Tile::getTopLookThing); g_lua.bindClassMemberFunction<Tile>("getTopLookThing", &Tile::getTopLookThing);
g_lua.bindClassMemberFunction<Tile>("getTopUseThing", &Tile::getTopUseThing); g_lua.bindClassMemberFunction<Tile>("getTopUseThing", &Tile::getTopUseThing);
g_lua.bindClassMemberFunction<Tile>("getTopCreature", &Tile::getTopCreature); g_lua.bindClassMemberFunction<Tile>("getTopCreature", &Tile::getTopCreature);
g_lua.bindClassMemberFunction<Tile>("getTopMoveThing", &Tile::getTopMoveThing);
g_lua.bindClassMemberFunction<Tile>("getTopMultiUseThing", &Tile::getTopMultiUseThing); g_lua.bindClassMemberFunction<Tile>("getTopMultiUseThing", &Tile::getTopMultiUseThing);
g_lua.bindClassMemberFunction<Tile>("getPos", &Tile::getPos); g_lua.bindClassMemberFunction<Tile>("getPos", &Tile::getPos);
g_lua.bindClassMemberFunction<Tile>("getDrawElevation", &Tile::getDrawElevation); g_lua.bindClassMemberFunction<Tile>("getDrawElevation", &Tile::getDrawElevation);
@ -237,6 +238,7 @@ void OTClient::registerLuaFunctions()
g_lua.registerClass<UIMap, UIWidget>(); g_lua.registerClass<UIMap, UIWidget>();
g_lua.bindClassStaticFunction<UIMap>("create", []{ return UIMapPtr(new UIMap); } ); g_lua.bindClassStaticFunction<UIMap>("create", []{ return UIMapPtr(new UIMap); } );
g_lua.bindClassMemberFunction<UIMap>("getTile", &UIMap::getTile); g_lua.bindClassMemberFunction<UIMap>("getTile", &UIMap::getTile);
g_lua.bindClassMemberFunction<UIMap>("getPosition", &UIMap::getPosition);
g_lua.registerClass<UIGame, UIWidget>(); g_lua.registerClass<UIGame, UIWidget>();
g_lua.bindClassStaticFunction<UIGame>("create", []{ return UIGamePtr(new UIGame); } ); g_lua.bindClassStaticFunction<UIGame>("create", []{ return UIGamePtr(new UIGame); } );

View File

@ -38,10 +38,10 @@ void UIMap::draw()
drawChildren(); drawChildren();
} }
TilePtr UIMap::getTile(const Point& mousePos) Position UIMap::getPosition(const Point& mousePos)
{ {
if(!m_mapRect.contains(mousePos)) if(!m_mapRect.contains(mousePos))
return nullptr; return Position();
// Get tile position // Get tile position
Point relativeStretchMousePos = mousePos - m_mapRect.topLeft(); Point relativeStretchMousePos = mousePos - m_mapRect.topLeft();
@ -58,6 +58,15 @@ TilePtr UIMap::getTile(const Point& mousePos)
PointF tilePosF = relativeMousePos / Map::NUM_TILE_PIXELS; PointF tilePosF = relativeMousePos / Map::NUM_TILE_PIXELS;
Position tilePos = Position(1 + (int)tilePosF.x - g_map.getCentralOffset().x, 1 + (int)tilePosF.y - g_map.getCentralOffset().y, 0) + g_map.getCentralPosition(); Position tilePos = Position(1 + (int)tilePosF.x - g_map.getCentralOffset().x, 1 + (int)tilePosF.y - g_map.getCentralOffset().y, 0) + g_map.getCentralPosition();
return tilePos;
}
TilePtr UIMap::getTile(const Point& mousePos)
{
Position tilePos = getPosition(mousePos);
if(!tilePos.isValid())
return nullptr;
// Get tile // Get tile
TilePtr tile = nullptr; TilePtr tile = nullptr;

View File

@ -32,6 +32,7 @@ class UIMap : public UIWidget
public: public:
void draw(); void draw();
Position getPosition(const Point& mousePos);
TilePtr getTile(const Point& mousePos); TilePtr getTile(const Point& mousePos);
protected: protected: