inventory module

This commit is contained in:
Henrique 2011-11-10 03:29:25 -02:00
parent 9c55f18186
commit 7487931b87
15 changed files with 289 additions and 9 deletions

View File

@ -72,6 +72,7 @@ SET(SOURCES
src/otclient/core/localplayer.cpp
# otclient ui
src/otclient/ui/uiitem.cpp
src/otclient/ui/uimap.cpp
# otclient net

View File

@ -13,5 +13,6 @@ Module
importStyles 'styles/lineedits.otui'
importStyles 'styles/windows.otui'
importStyles 'styles/listboxes.otui'
importStyles 'styles/items.otui'
return true

View File

@ -0,0 +1,6 @@
Item < UIItem
size: 34 34
item margin: 1
border-image:
source: /core_styles/images/panel_flat.png
border: 4

View File

@ -19,7 +19,7 @@ MiniWindow < UIWindow
size: 192 200
head height: 25
head text align: center
margin.top: 6
margin.top: 10
margin.left: 6
margin.right: 6
move policy: free updated

View File

@ -21,7 +21,7 @@ ManaLabel < Label
UIWindow
id: healthManaPanel
width: 192
margin.top: 6
margin.top: 10
margin.left: 6
margin.right: 6
move policy: free updated

View File

@ -0,0 +1,61 @@
Inventory = {}
-- private variables
local window = nil
local InventorySlotHead = 1
local InventorySlotNecklace = 2
local InventorySlotBackpack = 3
local InventorySlotArmor = 4
local InventorySlotRight = 5
local InventorySlotLeft = 6
local InventorySlotLegs = 7
local InventorySlotFeet = 8
local InventorySlotRing = 9
local InventorySlotAmmo = 10
-- public functions
function Inventory.create()
window = loadUI("/inventory/inventory.otui", Game.gameRightPanel)
local itemWidget = window:getChildById('feet')
window:setHeight(itemWidget:getPosition().y + itemWidget:getHeight() - window:getPosition().y)
end
function Inventory.destroy()
window:destroy()
window = nil
end
-- hooked events
function Game.onInventoryChange(slot, item)
local slotId
if slot == InventorySlotHead then
slotId = 'head'
elseif slot == InventorySlotNecklace then
slotId = 'necklace'
elseif slot == InventorySlotBackpack then
slotId = 'backpack'
elseif slot == InventorySlotArmor then
slotId = 'armor'
elseif slot == InventorySlotRight then
slotId = 'right'
elseif slot == InventorySlotLeft then
slotId = 'left'
elseif slot == InventorySlotLegs then
slotId = 'legs'
elseif slot == InventorySlotFeet then
slotId = 'feet'
elseif slot == InventorySlotRing then
slotId = 'ring'
elseif slot == InventorySlotAmmo then
slotId = 'ammo'
end
local itemWidget = window:getChildById(slotId)
itemWidget:setItem(item)
end
connect(Game, { onLogin = Inventory.create,
onLogout = Inventory.destroy })

View File

@ -0,0 +1,14 @@
Module
name: equipments
description: View local player equipments window
author: OTClient team
website: https://github.com/edubart/otclient
autoLoad: true
dependencies:
- game
onLoad: |
require 'inventory'
return true

View File

@ -0,0 +1,70 @@
UIWindow
width: 192
margin.top: 10
margin.left: 6
margin.right: 6
move policy: free updated
Item
id: head
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
Item
id: armor
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10
Item
id: legs
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10
Item
id: feet
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10
Item
id: necklace
anchors.top: parent.top
anchors.right: head.left
margin.top: 15
margin.right: 10
Item
id: left
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10
Item
id: ring
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10
Item
id: backpack
anchors.top: parent.top
anchors.left: head.right
margin.top: 15
margin.left: 10
Item
id: right
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10
Item
id: ammo
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin.top: 10

View File

@ -259,6 +259,19 @@ namespace Otc
SpriteNoMask = 255
};
enum InventorySlots {
InventorySlotHead = 1,
InventorySlotNecklace,
InventorySlotBackpack,
InventorySlotArmor,
InventorySlotRight,
InventorySlotLeft,
InventorySlotLegs,
InventorySlotFeet,
InventorySlotRing,
InventorySlotAmmo
};
enum Statistic {
Health,
MaxHealth,

View File

@ -23,6 +23,7 @@
#include "game.h"
#include "localplayer.h"
#include <otclient/net/protocolgame.h>
#include <framework/core/eventdispatcher.h>
Game g_game;
@ -102,6 +103,13 @@ void Game::processTextMessage(int type, const std::string& message)
g_lua.callGlobalField("Game","onTextMessage", type, message);
}
void Game::processInventoryChange(int slot, const ItemPtr& item)
{
g_dispatcher.addEvent([=] {
g_lua.callGlobalField("Game","onInventoryChange", slot, item);
});
}
void Game::walk(Otc::Direction direction)
{
if(!m_online || !m_localPlayer->canWalk(direction))
@ -158,9 +166,6 @@ void Game::turn(Otc::Direction direction)
}
}
// Game.talkChannel(1, 0, "lalala")
// TODO: MAKE SURE IT WAS AN USER EVENT AND NOT DIRECTLY FROM SCRIPT.
void Game::talkChannel(int channelType, int channelId, const std::string& message)
{
if(!m_online)

View File

@ -25,6 +25,7 @@
#include "declarations.h"
#include <otclient/net/declarations.h>
#include <otclient/core/item.h>
class Game
{
@ -45,6 +46,7 @@ public:
void processLogout();
void processTextMessage(int type, const std::string& message);
void processInventoryChange(int slot, const ItemPtr& item);
void walk(Otc::Direction direction);
void turn(Otc::Direction direction);

View File

@ -452,13 +452,15 @@ void ProtocolGame::parseContainerRemoveItem(InputMessage& msg)
void ProtocolGame::parseAddInventoryItem(InputMessage& msg)
{
msg.getU8(); // slot
internalGetItem(msg, 0xFFFF);
uint8 slot = msg.getU8();
ItemPtr item = internalGetItem(msg, 0xFFFF);
g_game.processInventoryChange(slot, item);
}
void ProtocolGame::parseRemoveInventoryItem(InputMessage& msg)
{
msg.getU8(); // slot
uint8 slot = msg.getU8();
g_game.processInventoryChange(slot, ItemPtr(nullptr));
}
void ProtocolGame::parseOpenShopWindow(InputMessage& msg)

View File

@ -34,6 +34,7 @@
#include <otclient/core/spritemanager.h>
#include <otclient/net/protocollogin.h>
#include <otclient/net/protocolgame.h>
#include <otclient/ui/uiitem.h>
#include <otclient/ui/uimap.h>
void OTClient::registerLuaFunctions()
@ -65,6 +66,11 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassStaticFunction<Game>("isOnline", std::bind(&Game::isOnline, &g_game));
g_lua.bindClassStaticFunction<Game>("isOnline", std::bind(&Game::isOnline, &g_game));
g_lua.registerClass<UIItem, UIWidget>();
g_lua.bindClassStaticFunction<UIItem>("create", &UIItem::create<UIItem>);
g_lua.bindClassMemberFunction<UIItem>("getItem", &UIItem::getItem);
g_lua.bindClassMemberFunction<UIItem>("setItem", &UIItem::setItem);
g_lua.registerClass<UIMap, UIWidget>();
g_lua.bindClassStaticFunction<UIMap>("create", &UIWidget::create<UIMap>);

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "uiitem.h"
#include <framework/otml/otml.h>
#include <framework/graphics/graphics.h>
void UIItem::setup()
{
UIWidget::setup();
}
void UIItem::render()
{
renderSelf();
if(m_item) {
g_graphics.bindColor(Fw::white);
m_item->draw(m_rect.topLeft() + m_itemMargin);
}
renderChildren();
}
void UIItem::onStyleApply(const OTMLNodePtr& styleNode)
{
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "item margin")
m_itemMargin = node->value<int>();
}
UIWidget::onStyleApply(styleNode);
}

47
src/otclient/ui/uiitem.h Normal file
View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef UIITEM_H
#define UIITEM_H
#include "declarations.h"
#include <framework/ui/uiwidget.h>
#include <otclient/core/item.h>
class UIItem : public UIWidget
{
public:
void setup();
void render();
void setItem(const ItemPtr& item) { m_item = item; }
ItemPtr getItem() { return m_item; }
protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode);
private:
ItemPtr m_item;
int m_itemMargin;
};
#endif