Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 97 additions & 15 deletions modules/corelib/ui/uispinbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ UISpinBox = extends(UITextEdit, 'UISpinBox')
function UISpinBox.create()
local spinbox = UISpinBox.internalCreate()
spinbox:setFocusable(false)
spinbox:setValidCharacters('0123456789')
spinbox:setValidCharacters('0123456789,')
spinbox.displayButtons = true
spinbox.minimum = 0
spinbox.maximum = 1
Expand Down Expand Up @@ -47,26 +47,84 @@ function UISpinBox:onKeyPress()
end

function UISpinBox:onTextChange(text, oldText)
if text:len() == 0 then
self:setValue(self.minimum)
return
end
if self.formattedMode then
local cursorPos = self:getCursorPos()
local cleanText = text:gsub(",", "")

local number = tonumber(text)
if not number then
self:setText(number)
return
if cleanText:len() == 0 then
self:setValue(self.minimum)
return
end

local number = tonumber(cleanText)
if not number then
self:setText(oldText or "")
self:setCursorPos(cursorPos)
return
else
if number < self.minimum then
local formattedText = comma_value(self.minimum)
self:setText(formattedText)
self.value = self.minimum
self:setCursorPos(formattedText:len())
return
elseif number > self.maximum then
local formattedText = comma_value(self.maximum)
self:setText(formattedText)
self.value = self.maximum
self:setCursorPos(formattedText:len())
return
end
end

local formattedText = comma_value(number)
self:setText(formattedText)
self.value = number
self.originalValue = number

local commasBefore = 0
local cleanPos = 0
for i = 1, cursorPos do
if string.sub(text, i, i) ~= "," then
cleanPos = cleanPos + 1
end
end

local formattedPos = 0
local cleanCount = 0
for i = 1, formattedText:len() do
if string.sub(formattedText, i, i) ~= "," then
cleanCount = cleanCount + 1
if cleanCount <= cleanPos then
formattedPos = i
end
end
end

self:setCursorPos(formattedPos)
signalcall(self.onValueChange, self, number)
else
if number < self.minimum then
self:setText(self.minimum)
if text:len() == 0 then
self:setValue(self.minimum)
return
elseif number > self.maximum then
self:setText(self.maximum)
end

local number = tonumber(text)
if not number then
self:setText(number)
return
else
if number < self.minimum then
self:setText(self.minimum)
return
elseif number > self.maximum then
self:setText(self.maximum)
return
end
end
end

self:setValue(number)
self:setValue(number)
end
end

function UISpinBox:onValueChange(value)
Expand Down Expand Up @@ -206,3 +264,27 @@ end
function UISpinBox:downSpin()
self:setValue(self.value - self.step)
end

function UISpinBox:setFormattedValue(value)
self.originalValue = value
self:setText(comma_value(value))
self.value = value
end

function UISpinBox:getFormattedValue()
if self.originalValue then
return self.originalValue
end
local text = self:getText() or ""
text = text:gsub(",", "")
return tonumber(text) or 0
end

function UISpinBox:setFormattedMode(enabled)
self.formattedMode = enabled
if enabled then
self:setValidCharacters('0123456789,')
else
self:setValidCharacters('0123456789')
end
end
65 changes: 34 additions & 31 deletions modules/game_market/market.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ local function addOffer(offer, offerType)
row = buyMyOfferTable:addRow({{
text = itemName .. " (Tier " .. tier .. ")"
}, {
text = price * amount
text = comma_value(price * amount)
}, {
text = price
text = comma_value(price)
}, {
text = amount
text = comma_value(amount)
}, {
text = string.gsub(os.date('%c', timestamp), ' ', ' '),
sortvalue = timestamp
Expand All @@ -238,11 +238,11 @@ local function addOffer(offer, offerType)
row = buyMyHistoryTable:addRow({{
text = itemName .. " (Tier " .. tier .. ")"
}, {
text = price * amount
text = comma_value(price * amount)
}, {
text = price
text = comma_value(price)
}, {
text = amount
text = comma_value(amount)
}, {
text = MarketOfferStateString[action],
sortvalue = timestamp
Expand All @@ -251,11 +251,11 @@ local function addOffer(offer, offerType)
row = buyOfferTable:addRow({{
text = player
}, {
text = amount
text = comma_value(amount)
}, {
text = price * amount
text = comma_value(price * amount)
}, {
text = price
text = comma_value(price)
}, {
text = string.gsub(os.date('%c', timestamp), ' ', ' ')
}})
Expand All @@ -276,11 +276,11 @@ local function addOffer(offer, offerType)
row = sellMyOfferTable:addRow({{
text = itemName .. " (Tier " .. tier .. ")"
}, {
text = price * amount
text = comma_value(price * amount)
}, {
text = price
text = comma_value(price)
}, {
text = amount
text = comma_value(amount)
}, {
text = string.gsub(os.date('%c', timestamp), ' ', ' '),
sortvalue = timestamp
Expand All @@ -289,11 +289,11 @@ local function addOffer(offer, offerType)
row = sellMyHistoryTable:addRow({{
text = itemName .. " (Tier " .. tier .. ")"
}, {
text = price * amount
text = comma_value(price * amount)
}, {
text = price
text = comma_value(price)
}, {
text = amount
text = comma_value(amount)
}, {
text = MarketOfferStateString[action],
sortvalue = timestamp
Expand All @@ -302,11 +302,11 @@ local function addOffer(offer, offerType)
row = sellOfferTable:addRow({{
text = player
}, {
text = amount
text = comma_value(amount)
}, {
text = price * amount
text = comma_value(price * amount)
}, {
text = price
text = comma_value(price)
}, {
text = string.gsub(os.date('%c', timestamp), ' ', ' '),
sortvalue = timestamp
Expand Down Expand Up @@ -596,7 +596,7 @@ local function updateBalance(balance)
end
information.balance = balance

balanceLabel:setText('Balance: ' .. balance .. ' gold')
balanceLabel:setText('Balance: ' .. comma_value(balance) .. ' gold')
balanceLabel:resizeToText()
end

Expand All @@ -607,7 +607,7 @@ local function updateFee(price, amount)
elseif fee > 1000 then
fee = 1000
end
feeLabel:setText('Fee: ' .. fee)
feeLabel:setText('Fee: ' .. comma_value(fee))
feeLabel:resizeToText()
end

Expand Down Expand Up @@ -658,7 +658,7 @@ local function openAmountWindow(callback, actionType, actionText)
scrollbar:setText(offer:getPrice() .. 'gp')

scrollbar.onValueChange = function(widget, value)
widget:setText((value * offer:getPrice()) .. 'gp')
widget:setText(comma_value(value * offer:getPrice()) .. 'gp')
itembox:setText(value)
end

Expand Down Expand Up @@ -807,32 +807,32 @@ end

local function onTotalPriceChange()
local amount = amountEdit:getValue()
local totalPrice = totalPriceEdit:getValue()
local totalPrice = totalPriceEdit:getFormattedValue()
local piecePrice = math.floor(totalPrice / amount)

piecePriceEdit:setValue(piecePrice, true)
piecePriceEdit:setFormattedValue(piecePrice)
if Market.isItemSelected() then
updateFee(piecePrice, amount)
end
end

local function onPiecePriceChange()
local amount = amountEdit:getValue()
local totalPrice = totalPriceEdit:getValue()
local piecePrice = piecePriceEdit:getValue()
local totalPrice = totalPriceEdit:getFormattedValue()
local piecePrice = piecePriceEdit:getFormattedValue()

totalPriceEdit:setValue(piecePrice * amount, true)
totalPriceEdit:setFormattedValue(piecePrice * amount)
if Market.isItemSelected() then
updateFee(piecePrice, amount)
end
end

local function onAmountChange()
local amount = amountEdit:getValue()
local piecePrice = piecePriceEdit:getValue()
local piecePrice = piecePriceEdit:getFormattedValue()
local totalPrice = piecePrice * amount

totalPriceEdit:setValue(piecePrice * amount, true)
totalPriceEdit:setFormattedValue(piecePrice * amount)
if Market.isItemSelected() then
updateFee(piecePrice, amount)
end
Expand Down Expand Up @@ -1013,6 +1013,9 @@ local function initInterface()
totalPriceEdit.onValueChange = onTotalPriceChange
piecePriceEdit.onValueChange = onPiecePriceChange
amountEdit.onValueChange = onAmountChange
totalPriceEdit:setFormattedMode(true)
piecePriceEdit:setFormattedMode(true)


offerTypeList = marketOffersPanel:getChildById('offerTypeComboBox')
offerTypeList.onOptionChange = onChangeOfferType
Expand Down Expand Up @@ -1273,8 +1276,8 @@ function Market.updateCurrentItems()
end

function Market.resetCreateOffer(resetFee)
piecePriceEdit:setValue(1)
totalPriceEdit:setValue(1)
piecePriceEdit:setFormattedValue(1)
totalPriceEdit:setFormattedValue(1)
amountEdit:setValue(1)
refreshTypeList()

Expand Down Expand Up @@ -1419,7 +1422,7 @@ function Market.createNewOffer()

local spriteId = selectedItem.item.marketData.tradeAs

local piecePrice = piecePriceEdit:getValue()
local piecePrice = piecePriceEdit:getFormattedValue()
local amount = amountEdit:getValue()
local anonymous = anonymous:isChecked() and 1 or 0

Expand Down