some changes to npctrade. still needs another rev

This commit is contained in:
Henrique Santiago 2012-04-24 20:04:37 -03:00
parent 9afe4e012b
commit c89665848d
2 changed files with 38 additions and 28 deletions

View File

@ -26,6 +26,9 @@ local ignoreCapacity = false
local ignoreEquipped = true local ignoreEquipped = true
local showOnlyHolding = false local showOnlyHolding = false
local CURRENCY = 'gold'
local WEIGHT_UNIT = 'oz'
-- public functions -- public functions
function NPCTrade.init() function NPCTrade.init()
cacheItems = {} cacheItems = {}
@ -85,7 +88,8 @@ function NPCTrade.terminate()
cacheItems = nil cacheItems = nil
cacheGoods = nil cacheGoods = nil
disconnect(g_game, { onOpenNpcTrade = NPCTrade.onOpenNpcTrade, disconnect(g_game, { onGameEnd = NPCTrade.hide,
onOpenNpcTrade = NPCTrade.onOpenNpcTrade,
onPlayerGoods = NPCTrade.onPlayerGoods, onPlayerGoods = NPCTrade.onPlayerGoods,
onCloseNpcTrade = NPCTrade.onCloseNpcTrade } ) onCloseNpcTrade = NPCTrade.onCloseNpcTrade } )
end end
@ -129,10 +133,14 @@ function NPCTrade.updateSetup()
if offerSelected then if offerSelected then
if radioItems.selectedWidget:isEnabled() then if radioItems.selectedWidget:isEnabled() then
if setupButton:getText() == 'Buy' then if setupButton:getText() == 'Buy' then
quantityScroll:setMaximum(math.max(0, math.min(100, math.floor(moneyGoods/NPCTrade.getOfferPrice(offerSelected))))) local capacityMaxCount = math.floor(100*g_game.getLocalPlayer():getFreeCapacity()/offerSelected[3])
local priceMaxCount = math.floor(moneyGoods/NPCTrade.getOfferPrice(offerSelected))
quantityScroll:setMaximum(math.max(0, math.min(100, math.min(priceMaxCount, capacityMaxCount))))
else else
if cacheGoods[offerSelected[1]:getId()] then -- list might be empty.
quantityScroll:setMaximum(math.max(0, math.min(100, cacheGoods[offerSelected[1]:getId()]))) quantityScroll:setMaximum(math.max(0, math.min(100, cacheGoods[offerSelected[1]:getId()])))
end end
end
else else
NPCTrade.resetSetup() NPCTrade.resetSetup()
end end
@ -163,9 +171,9 @@ function NPCTrade.setItem(widget)
local price = NPCTrade.getOfferPrice(widget.offer) local price = NPCTrade.getOfferPrice(widget.offer)
local freeCapacity = g_game.getLocalPlayer():getFreeCapacity() local freeCapacity = g_game.getLocalPlayer():getFreeCapacity()
nameLabel:setText(offer[2]) nameLabel:setText(offer[2])
weightLabel:setText(string.format("%.2f", offer[3]/100) .. " oz") weightLabel:setText(string.format('%.2f', offer[3]/100) .. ' ' .. WEIGHT_UNIT)
priceLabel:setText(price .. " gold") priceLabel:setText(price .. ' ' .. CURRENCY)
capacityLabel:setText(string.format("%.2f", freeCapacity) .. " oz") capacityLabel:setText(string.format('%.2f', freeCapacity) .. ' ' .. WEIGHT_UNIT)
quantityLabel:setText(1) quantityLabel:setText(1)
quantityScroll:setValue(1) quantityScroll:setValue(1)
@ -178,8 +186,8 @@ function NPCTrade.setQuantity(quantity)
if quantityLabel and offerSelected then if quantityLabel and offerSelected then
local price = NPCTrade.getOfferPrice(offerSelected) local price = NPCTrade.getOfferPrice(offerSelected)
quantityLabel:setText(quantity) quantityLabel:setText(quantity)
weightLabel:setText(string.format("%.2f", offerSelected[3]*quantity/100) .. " oz") weightLabel:setText(string.format('%.2f', offerSelected[3]*quantity/100) .. ' ' .. WEIGHT_UNIT)
priceLabel:setText(price .. " gold") priceLabel:setText(price .. ' ' .. CURRENCY)
end end
end end
@ -209,7 +217,7 @@ end
function NPCTrade.switchBuyWithBackpack() function NPCTrade.switchBuyWithBackpack()
buyWithBackpack = not buyWithBackpack buyWithBackpack = not buyWithBackpack
if offerSelected then if offerSelected then
priceLabel:setText(NPCTrade.getOfferPrice(offerSelected) .. " gold") priceLabel:setText(NPCTrade.getOfferPrice(offerSelected) .. ' ' .. CURRENCY)
end end
return true return true
end end
@ -251,8 +259,8 @@ function NPCTrade.createItemsOnPanel()
itemBox.offer = v itemBox.offer = v
itemBox:getChildById('item'):setItem(v[1]) itemBox:getChildById('item'):setItem(v[1])
itemBox:getChildById('nameLabel'):setText(v[2]) itemBox:getChildById('nameLabel'):setText(v[2])
itemBox:getChildById('weightLabel'):setText(string.format("%.2f", v[3]/100) .. " oz") itemBox:getChildById('weightLabel'):setText(string.format('%.2f', v[3]/100) .. ' ' .. WEIGHT_UNIT)
itemBox:getChildById('priceLabel'):setText(price .. " gold") itemBox:getChildById('priceLabel'):setText(price .. ' ' .. CURRENCY)
itemBox.onMouseRelease = NPCTrade.itemPopup itemBox.onMouseRelease = NPCTrade.itemPopup
itemBox:getChildById('item').onMouseRelease = function (self, mousePosition, mouseButton) NPCTrade.itemPopup(itemBox, mousePosition, mouseButton) end itemBox:getChildById('item').onMouseRelease = function (self, mousePosition, mouseButton) NPCTrade.itemPopup(itemBox, mousePosition, mouseButton) end
@ -282,7 +290,7 @@ function NPCTrade.refreshFilters()
local items = itemsPanel:getChildCount() local items = itemsPanel:getChildCount()
for i = 1, items do for i = 1, items do
local itemWidget = itemsPanel:getChildByIndex(i) local itemWidget = itemsPanel:getChildByIndex(i)
if filter ~= "" then if filter ~= '' then
if string.find(itemWidget.offer[2], filter) and NPCTrade.extraFilters(itemWidget, showOnlyHolding) then if string.find(itemWidget.offer[2], filter) and NPCTrade.extraFilters(itemWidget, showOnlyHolding) then
itemWidget:show() itemWidget:show()
else else
@ -325,9 +333,9 @@ end
function NPCTrade.onPlayerGoods(money, goods) function NPCTrade.onPlayerGoods(money, goods)
moneyGoods = money moneyGoods = money
moneyLabel:setText(money .. " gold") moneyLabel:setText(money .. ' ' .. CURRENCY)
local freeCapacity = g_game.getLocalPlayer():getFreeCapacity() local freeCapacity = g_game.getLocalPlayer():getFreeCapacity()
capacityLabel:setText(string.format("%.2f", freeCapacity) .. " oz") capacityLabel:setText(string.format('%.2f', freeCapacity) .. ' ' .. WEIGHT_UNIT)
cacheGoods = {} cacheGoods = {}
for i,v in pairs(goods) do for i,v in pairs(goods) do

View File

@ -85,7 +85,7 @@ MainWindow
anchors.left: parent.left anchors.left: parent.left
anchors.top: prev.bottom anchors.top: prev.bottom
text-auto-resize: true text-auto-resize: true
margin-top: 5 margin-top: 7
margin-left: 2 margin-left: 2
TextEdit TextEdit
@ -93,6 +93,7 @@ MainWindow
width: 200 width: 200
anchors.left: prev.right anchors.left: prev.right
anchors.top: prev.top anchors.top: prev.top
margin-top: -2
margin-left: 5 margin-left: 5
@onTextChange: NPCTrade.refreshFilters() @onTextChange: NPCTrade.refreshFilters()
@ -136,7 +137,7 @@ MainWindow
Label Label
id: quantityLabel id: quantityLabel
text: Quantity text: Quantity:
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
text-auto-resize: true text-auto-resize: true
@ -160,60 +161,60 @@ MainWindow
Label Label
id: nameLabel id: nameLabel
text: Name text: Name:
anchors.left: parent.left anchors.left: parent.left
anchors.top: prev.bottom anchors.top: prev.bottom
text-auto-resize: true
margin-top: 5 margin-top: 5
margin-left: 2 margin-left: 2
width: 64
NPCOfferLabel NPCOfferLabel
id: name id: name
Label Label
id: priceLabel id: priceLabel
text: Price text: Price:
anchors.left: parent.left anchors.left: parent.left
anchors.top: prev.bottom anchors.top: prev.bottom
text-auto-resize: true
margin-top: 5 margin-top: 5
margin-left: 2 margin-left: 2
width: 64
NPCOfferLabel NPCOfferLabel
id: price id: price
Label Label
id: moneyLabel id: moneyLabel
text: Money text: Money:
anchors.left: parent.left anchors.left: parent.left
anchors.top: prev.bottom anchors.top: prev.bottom
text-auto-resize: true
margin-top: 5 margin-top: 5
margin-left: 2 margin-left: 2
width: 64
NPCOfferLabel NPCOfferLabel
id: money id: money
Label Label
id: weightLabel id: weightLabel
text: Weight text: Weight:
anchors.left: parent.left anchors.left: parent.left
anchors.top: prev.bottom anchors.top: prev.bottom
text-auto-resize: true
margin-top: 5 margin-top: 5
margin-left: 2 margin-left: 2
width: 64
NPCOfferLabel NPCOfferLabel
id: weight id: weight
Label Label
id: capacityLabel id: capacityLabel
text: Capacity text: Capacity:
anchors.left: parent.left anchors.left: parent.left
anchors.top: prev.bottom anchors.top: prev.bottom
text-auto-resize: true
margin-top: 5 margin-top: 5
margin-left: 2 margin-left: 2
width: 64
NPCOfferLabel NPCOfferLabel
id: capacity id: capacity
@ -232,3 +233,4 @@ MainWindow
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
@onClick: NPCTrade.hide() @onClick: NPCTrade.hide()