trade working

* use only mouse left release in checkboxes
* remvoe duplicated code in radiogroup
* implement player trade
This commit is contained in:
Eduardo Bart 2012-05-09 09:24:35 -03:00
parent 2afa80b1cd
commit fea34a41ea
7 changed files with 127 additions and 41 deletions

View File

@ -8,31 +8,24 @@ end
function RadioGroup:destroy()
for k,widget in pairs(self.widgets) do
widget.onMousePress = nil
widget.onClick = nil
end
self.widgets = {}
end
function RadioGroup:addWidget(widget)
table.insert(self.widgets, widget)
widget.onMousePress = function(widget) self:selectWidget(widget) end
widget.onClick = function(widget) self:selectWidget(widget) end
end
function RadioGroup:removeWidget(widget)
if self.selectedWidget == widget then
self:selectWidget(nil)
end
widget.onMousePress = nil
widget.onClick = nil
table.removevalue(self.widgets, widget)
end
function RadioGroup:destroy()
for k,widget in pairs(self.widgets) do
widget.onMousePress = nil
end
self.widgets = {}
end
function RadioGroup:selectWidget(selectedWidget)
if selectedWidget == self.selectedWidget then return end

View File

@ -7,7 +7,6 @@ function UICheckBox.create()
return checkbox
end
function UICheckBox:onMousePress(mousePos, mouseButton)
function UICheckBox:onClick()
self:setChecked(not self:isChecked())
return true
end

View File

@ -7,3 +7,7 @@ ScrollablePanel < UIScrollArea
FlatPanel < Panel
image-source: /core_styles/styles/images/panel_flat.png
image-border: 1
ScrollableFlatPanel < ScrollablePanel
image-source: /core_styles/styles/images/panel_flat.png
image-border: 1

View File

@ -4,3 +4,10 @@ HorizontalSeparator < UIWidget
height: 2
phantom: true
focusable: false
VerticalSeparator < UIWidget
image-source: /core_styles/styles/images/horizontal_separator.png
image-border-left: 2
width: 2
phantom: true
focusable: false

View File

@ -7,26 +7,55 @@ PlayerTrade = {}
local tradeWindow
local function createTrade()
if tradeWindow then
tradeWindow:destroy()
tradeWindow = nil
tradeWindow = createWidget('TradeWindow', GameInterface.getRightPanel())
tradeWindow.onClose = function()
g_game.rejectTrade()
tradeWindow:hide()
end
end
tradeWindow = createWidget('TradeWindow', rootWidget)
local function fillTrade(name, items, counter)
if not tradeWindow then
createTrade()
end
local function onOwnTrade(name, items)
local firstItem = items[1]
local tradeItemWidget = tradeWindow:getChildById('tradeItem')
tradeItemWidget:setItem(firstItem)
tradeItemWidget:setItemId(items[1]:getId())
local tradeContainer
local label
if counter then
tradeContainer = tradeWindow:recursiveGetChildById('counterTradeContainer')
label = tradeWindow:recursiveGetChildById('counterTradeLabel')
tradeWindow:recursiveGetChildById('acceptButton'):enable()
else
tradeContainer = tradeWindow:recursiveGetChildById('ownTradeContainer')
label = tradeWindow:recursiveGetChildById('ownTradeLabel')
end
label:setText(name)
for index,item in ipairs(items) do
local itemWidget = createWidget('Item', tradeContainer)
itemWidget:setItem(item)
itemWidget:setVirtual(true)
itemWidget:setMargin(1)
itemWidget.onClick = function()
g_game.inspectTrade(counter, index-1)
end
end
end
local function onCounterTrade(name, items)
local function onGameOwnTrade(name, items)
fillTrade(name, items, false)
end
local function onCloseTrade()
local function onGameCounterTrade(name, items)
fillTrade(name, items, true)
end
local function onGameCloseTrade()
if not tradeWindow then return end
tradeWindow:destroy()
tradeWindow = nil
end
@ -36,11 +65,18 @@ function PlayerTrade.init()
connect(g_game, { onOwnTrade = onGameOwnTrade,
onCounterTrade = onGameCounterTrade,
onCloseTrade = onGameCloseTrade })
onCloseTrade = onGameCloseTrade,
onGameEnd = onGameCloseTrade })
end
function PlayerTrade.terminate()
disconnect(g_game, { onOwnTrade = onGameOwnTrade,
onCounterTrade = onGameCounterTrade,
onCloseTrade = onGameCloseTrade })
onCloseTrade = onGameCloseTrade,
onGameEnd = onGameCloseTrade })
if tradeWindow then
tradeWindow:destroy()
tradeWindow = nil
end
end

View File

@ -15,24 +15,70 @@ TradeWindow < MiniWindow
MiniWindowContents
padding: 6
FlatPanel
ScrollableFlatPanel
id: ownTradeContainer
anchors.fill: parent
anchors.right: parent.horizontalCenter
margin-right: 2
anchors.left: parent.left
anchors.top: tradeScrollBar.top
anchors.right: tradeScrollBar.left
anchors.bottom: tradeScrollBar.bottom
layout:
type: grid
cell-size: 40 40
cell-size: 36 36
flow: true
cell-spacing: 0
vertical-scrollbar: tradeScrollBar
ScrollableFlatPanel
id: counterTradeContainer
anchors.right: parent.right
anchors.top: tradeScrollBar.top
anchors.left: tradeScrollBar.right
anchors.bottom: tradeScrollBar.bottom
layout:
type: grid
cell-size: 36 36
flow: true
cell-spacing: 0
FlatPanel
id: counterTradeContainer
anchors.fill: parent
VerticalScrollBar
id: tradeScrollBar
anchors.top: parent.top
anchors.bottom: acceptButton.top
anchors.horizontalCenter: parent.horizontalCenter
margin-top: 14
margin-bottom: 4
step: 14
pixels-scroll: true
Label
id: ownTradeLabel
anchors.bottom: ownTradeContainer.top
anchors.left: ownTradeContainer.left
anchors.right: ownTradeContainer.right
margin-bottom: 2
Label
id: counterTradeLabel
anchors.bottom: counterTradeContainer.top
anchors.left: counterTradeContainer.left
anchors.right: counterTradeContainer.right
margin-bottom: 2
Button
!text: tr('Accept')
id: acceptButton
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.horizontalCenter
margin-right: 2
enabled: false
@onClick: g_game.acceptTrade(); self:disable()
Button
!text: tr('Reject')
id: rejectButton
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.left: parent.horizontalCenter
margin-left: 2
layout:
type: grid
cell-size: 40 40
flow: true
cell-spacing: 0
@onClick: g_game.rejectTrade()

View File

@ -45,6 +45,7 @@ void Application::registerLuaFunctions()
g_lua.bindGlobalFunction("pointtostring", [](const Point& v) { return Fw::tostring(v); });
g_lua.bindGlobalFunction("colortostring", [](const Color& v) { return Fw::tostring(v); });
g_lua.bindGlobalFunction("sizetostring", [](const Size& v) { return Fw::tostring(v); });
g_lua.bindGlobalFunction("getticks", []() { return g_clock.asyncTicks(); });
// Event
g_lua.registerClass<Event>();
@ -334,7 +335,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UILayout>("asUIGridLayout", &UILayout::asUIGridLayout);
// UIBoxLayout
g_lua.registerClass<UIBoxLayout>();
g_lua.registerClass<UIBoxLayout, UILayout>();
g_lua.bindClassMemberFunction<UIBoxLayout>("setSpacing", &UIBoxLayout::setSpacing);
g_lua.bindClassMemberFunction<UIBoxLayout>("setFitChildren", &UIBoxLayout::setFitChildren);