diff --git a/CMakeLists.txt b/CMakeLists.txt index 29ff9feb..4f947a82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/modules/core_styles/core_styles.otmod b/modules/core_styles/core_styles.otmod index cf03de56..57fcf37c 100644 --- a/modules/core_styles/core_styles.otmod +++ b/modules/core_styles/core_styles.otmod @@ -13,5 +13,6 @@ Module importStyles 'styles/lineedits.otui' importStyles 'styles/windows.otui' importStyles 'styles/listboxes.otui' + importStyles 'styles/items.otui' return true diff --git a/modules/core_styles/styles/items.otui b/modules/core_styles/styles/items.otui new file mode 100644 index 00000000..3ba2a364 --- /dev/null +++ b/modules/core_styles/styles/items.otui @@ -0,0 +1,6 @@ +Item < UIItem + size: 34 34 + item margin: 1 + border-image: + source: /core_styles/images/panel_flat.png + border: 4 diff --git a/modules/core_styles/styles/windows.otui b/modules/core_styles/styles/windows.otui index 4f7337c0..80b6c3ba 100644 --- a/modules/core_styles/styles/windows.otui +++ b/modules/core_styles/styles/windows.otui @@ -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 @@ -29,4 +29,4 @@ MiniWindow < UIWindow border.top: 25 MainWindow < Window - anchors.centerIn: parent \ No newline at end of file + anchors.centerIn: parent diff --git a/modules/health_mana/health_mana.otui b/modules/health_mana/health_mana.otui index 1c76692c..e653d36a 100644 --- a/modules/health_mana/health_mana.otui +++ b/modules/health_mana/health_mana.otui @@ -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 diff --git a/modules/inventory/inventory.lua b/modules/inventory/inventory.lua new file mode 100644 index 00000000..3b236fe1 --- /dev/null +++ b/modules/inventory/inventory.lua @@ -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 }) diff --git a/modules/inventory/inventory.otmod b/modules/inventory/inventory.otmod new file mode 100644 index 00000000..f9d1ab13 --- /dev/null +++ b/modules/inventory/inventory.otmod @@ -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 + + diff --git a/modules/inventory/inventory.otui b/modules/inventory/inventory.otui new file mode 100644 index 00000000..71826014 --- /dev/null +++ b/modules/inventory/inventory.otui @@ -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 + + + diff --git a/src/otclient/const.h b/src/otclient/const.h index 92b8fb51..7f2f0d27 100644 --- a/src/otclient/const.h +++ b/src/otclient/const.h @@ -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, diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 56ff20f6..9b96adef 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -23,6 +23,7 @@ #include "game.h" #include "localplayer.h" #include +#include 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) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index be49cd2f..c0f6ae25 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -25,6 +25,7 @@ #include "declarations.h" #include +#include 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); diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index f1b1c569..ce626f0b 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -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) diff --git a/src/otclient/otclientluafunctions.cpp b/src/otclient/otclientluafunctions.cpp index f2c83238..515a6904 100644 --- a/src/otclient/otclientluafunctions.cpp +++ b/src/otclient/otclientluafunctions.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include void OTClient::registerLuaFunctions() @@ -65,6 +66,11 @@ void OTClient::registerLuaFunctions() g_lua.bindClassStaticFunction("isOnline", std::bind(&Game::isOnline, &g_game)); g_lua.bindClassStaticFunction("isOnline", std::bind(&Game::isOnline, &g_game)); + g_lua.registerClass(); + g_lua.bindClassStaticFunction("create", &UIItem::create); + g_lua.bindClassMemberFunction("getItem", &UIItem::getItem); + g_lua.bindClassMemberFunction("setItem", &UIItem::setItem); + g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &UIWidget::create); diff --git a/src/otclient/ui/uiitem.cpp b/src/otclient/ui/uiitem.cpp new file mode 100644 index 00000000..278f6b01 --- /dev/null +++ b/src/otclient/ui/uiitem.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2010-2011 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 +#include + +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(); + } + + UIWidget::onStyleApply(styleNode); +} diff --git a/src/otclient/ui/uiitem.h b/src/otclient/ui/uiitem.h new file mode 100644 index 00000000..f5d50452 --- /dev/null +++ b/src/otclient/ui/uiitem.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010-2011 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 +#include + +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