Index: ps/trunk/binaries/data/mods/public/gui/session/hotkeys/misc.xml =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/hotkeys/misc.xml +++ ps/trunk/binaries/data/mods/public/gui/session/hotkeys/misc.xml @@ -72,18 +72,6 @@ updateSelectionDetails(); - - - updateSelectionDetails(); - updateBarterButtons(); - - - - updateSelectionDetails(); - updateBarterButtons(); - - - findIdleUnit(g_MilitaryTypes); Index: ps/trunk/binaries/data/mods/public/gui/session/menu.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/menu.js +++ ps/trunk/binaries/data/mods/public/gui/session/menu.js @@ -19,12 +19,6 @@ // Number of pixels per millisecond to move var MENU_SPEED = 1.2; -// Trade menu: step for probability changes -var STEP = 5; - -// Shown in the trade dialog. -var g_IdleTraderTextColor = "orange"; - /** * Store civilization code and page (structree or history) opened in civilization info. */ @@ -33,30 +27,8 @@ "page": "page_structree.xml" }; -/** - * The barter constants should match with the simulation - * Quantity of goods to sell per click. - */ -const g_BarterResourceSellQuantity = 100; - -/** - * Multiplier to be applied when holding the massbarter hotkey. - */ -const g_BarterMultiplier = 5; - -/** - * Barter actions, as mapped to the names of GUI Buttons. - */ -const g_BarterActions = ["Buy", "Sell"]; - -/** - * Currently selected resource type to sell in the barter GUI. - */ -var g_BarterSell; - var g_IsMenuOpen = false; -var g_IsTradeOpen = false; var g_IsObjectivesOpen = false; /** @@ -237,407 +209,6 @@ }); } -function resizeTradeDialog() -{ - let dialog = Engine.GetGUIObjectByName("tradeDialogPanel"); - let size = dialog.size; - let width = size.right - size.left; - - let resTradCodesLength = g_ResourceData.GetTradableCodes().length; - Engine.GetGUIObjectByName("tradeDialogPanelTrade").hidden = !resTradCodesLength; - - let resBarterCodesLength = g_ResourceData.GetBarterableCodes().length; - Engine.GetGUIObjectByName("tradeDialogPanelBarter").hidden = !resBarterCodesLength; - - let tradeSize = Engine.GetGUIObjectByName("tradeResource[0]").size; - let length = Math.max(resTradCodesLength, resBarterCodesLength); - width += length * (tradeSize.right - tradeSize.left); - - size.left = -width / 2; - size.right = width / 2; - dialog.size = size; -} - -function openTrade() -{ - closeOpenDialogs(); - - if (g_ViewedPlayer < 1) - return; - - g_IsTradeOpen = true; - - let proba = Engine.GuiInterfaceCall("GetTradingGoods", g_ViewedPlayer); - let button = {}; - let resTradeCodes = g_ResourceData.GetTradableCodes(); - let resBarterCodes = g_ResourceData.GetBarterableCodes(); - let currTradeSelection = resTradeCodes[0]; - - let updateTradeButtons = function() - { - for (let res in button) - { - button[res].label.caption = proba[res] + "%"; - - button[res].sel.hidden = !controlsPlayer(g_ViewedPlayer) || res != currTradeSelection; - button[res].up.hidden = !controlsPlayer(g_ViewedPlayer) || res == currTradeSelection || proba[res] == 100 || proba[currTradeSelection] == 0; - button[res].dn.hidden = !controlsPlayer(g_ViewedPlayer) || res == currTradeSelection || proba[res] == 0 || proba[currTradeSelection] == 100; - } - }; - - hideRemaining("tradeResources", resTradeCodes.length); - Engine.GetGUIObjectByName("tradeHelp").hidden = false; - - - for (let i = 0; i < resBarterCodes.length; ++i) - { - let resBarterCode = resBarterCodes[i]; - - let barterResource = Engine.GetGUIObjectByName("barterResource[" + i + "]"); - if (!barterResource) - { - warn("Current GUI limits prevent displaying more than " + i + " resources in the barter dialog!"); - break; - } - - barterOpenCommon(resBarterCode, i, "barter"); - setPanelObjectPosition(barterResource, i, i + 1); - } - - for (let i = 0; i < resTradeCodes.length; ++i) - { - let resTradeCode = resTradeCodes[i]; - - let tradeResource = Engine.GetGUIObjectByName("tradeResource[" + i + "]"); - if (!tradeResource) - { - warn("Current GUI limits prevent displaying more than " + i + " resources in the trading goods selection dialog!"); - break; - } - - setPanelObjectPosition(tradeResource, i, i + 1); - - let icon = Engine.GetGUIObjectByName("tradeResourceIcon[" + i + "]"); - icon.sprite = "stretched:session/icons/resources/" + resTradeCode + ".png"; - - let buttonUp = Engine.GetGUIObjectByName("tradeArrowUp[" + i + "]"); - let buttonDn = Engine.GetGUIObjectByName("tradeArrowDn[" + i + "]"); - - button[resTradeCode] = { - "up": buttonUp, - "dn": buttonDn, - "label": Engine.GetGUIObjectByName("tradeResourceText[" + i + "]"), - "sel": Engine.GetGUIObjectByName("tradeResourceSelection[" + i + "]") - }; - - proba[resTradeCode] = proba[resTradeCode] || 0; - - let buttonResource = Engine.GetGUIObjectByName("tradeResourceButton[" + i + "]"); - buttonResource.enabled = controlsPlayer(g_ViewedPlayer); - buttonResource.onPress = (resource => { - return () => { - if (Engine.HotkeyIsPressed("session.fulltradeswap")) - { - for (let res of resTradeCodes) - proba[res] = 0; - proba[resource] = 100; - Engine.PostNetworkCommand({ "type": "set-trading-goods", "tradingGoods": proba }); - } - currTradeSelection = resource; - updateTradeButtons(); - }; - })(resTradeCode); - - buttonUp.enabled = controlsPlayer(g_ViewedPlayer); - buttonUp.onPress = (resource => { - return () => { - proba[resource] += Math.min(STEP, proba[currTradeSelection]); - proba[currTradeSelection] -= Math.min(STEP, proba[currTradeSelection]); - Engine.PostNetworkCommand({ "type": "set-trading-goods", "tradingGoods": proba }); - updateTradeButtons(); - }; - })(resTradeCode); - - buttonDn.enabled = controlsPlayer(g_ViewedPlayer); - buttonDn.onPress = (resource => { - return () => { - proba[currTradeSelection] += Math.min(STEP, proba[resource]); - proba[resource] -= Math.min(STEP, proba[resource]); - Engine.PostNetworkCommand({ "type": "set-trading-goods", "tradingGoods": proba }); - updateTradeButtons(); - }; - })(resTradeCode); - } - - updateTradeButtons(); - updateTraderTexts(); - - Engine.GetGUIObjectByName("tradeDialogPanel").hidden = false; -} - -function updateTraderTexts() -{ - let traderNumber = Engine.GuiInterfaceCall("GetTraderNumber", g_ViewedPlayer); - Engine.GetGUIObjectByName("traderCountText").caption = getIdleLandTradersText(traderNumber) + "\n\n" + getIdleShipTradersText(traderNumber); -} - -function initBarterButtons() -{ - let resBartCodes = g_ResourceData.GetBarterableCodes(); - g_BarterSell = resBartCodes.length ? resBartCodes[0] : undefined; -} - -/** - * Code common to both the Barter Panel and the Trade/Barter Dialog, that - * only needs to be run when the panel or dialog is opened by the player. - * - * @param {string} resourceCode - * @param {number} idx - Element index within its set - * @param {string} prefix - Common prefix of the gui elements to be worked upon - */ -function barterOpenCommon(resourceCode, idx, prefix) -{ - let barterButton = {}; - for (let action of g_BarterActions) - barterButton[action] = Engine.GetGUIObjectByName(prefix + action + "Button[" + idx + "]"); - - let resource = resourceNameWithinSentence(resourceCode); - barterButton.Buy.tooltip = sprintf(translate("Buy %(resource)s"), { "resource": resource }); - barterButton.Sell.tooltip = sprintf(translate("Sell %(resource)s"), { "resource": resource }); - - barterButton.Sell.onPress = function() { - g_BarterSell = resourceCode; - updateSelectionDetails(); - updateBarterButtons(); - }; -} - -/** - * Code common to both the Barter Panel and the Trade/Barter Dialog, that - * needs to be run on simulation update and when relevant hotkeys - * (i.e. massbarter) are pressed. - * - * @param {string} resourceCode - * @param {number} idx - Element index within its set - * @param {string} prefix - Common prefix of the gui elements to be worked upon - * @param {number} player - */ -function barterUpdateCommon(resourceCode, idx, prefix, player) -{ - let barterButton = {}; - let barterIcon = {}; - let barterAmount = {}; - for (let action of g_BarterActions) - { - barterButton[action] = Engine.GetGUIObjectByName(prefix + action + "Button[" + idx + "]"); - barterIcon[action] = Engine.GetGUIObjectByName(prefix + action + "Icon[" + idx + "]"); - barterAmount[action] = Engine.GetGUIObjectByName(prefix + action + "Amount[" + idx + "]"); - } - let selectionIcon = Engine.GetGUIObjectByName(prefix + "SellSelection[" + idx + "]"); - - let amountToSell = g_BarterResourceSellQuantity; - if (Engine.HotkeyIsPressed("session.massbarter")) - amountToSell *= g_BarterMultiplier; - - let isSelected = resourceCode == g_BarterSell; - let grayscale = isSelected ? "color:0 0 0 100:grayscale:" : ""; - - // Select color of the sell button - let neededRes = {}; - neededRes[resourceCode] = amountToSell; - let canSellCurrent = Engine.GuiInterfaceCall("GetNeededResources", { - "cost": neededRes, - "player": player - }) ? "color:255 0 0 80:" : ""; - - // Select color of the buy button - neededRes = {}; - neededRes[g_BarterSell] = amountToSell; - let canBuyAny = Engine.GuiInterfaceCall("GetNeededResources", { - "cost": neededRes, - "player": player - }) ? "color:255 0 0 80:" : ""; - - barterIcon.Sell.sprite = canSellCurrent + "stretched:" + grayscale + "session/icons/resources/" + resourceCode + ".png"; - barterIcon.Buy.sprite = canBuyAny + "stretched:" + grayscale + "session/icons/resources/" + resourceCode + ".png"; - - barterAmount.Sell.caption = "-" + amountToSell; - let prices = GetSimState().players[player].barterPrices; - barterAmount.Buy.caption = "+" + Math.round(prices.sell[g_BarterSell] / prices.buy[resourceCode] * amountToSell); - - barterButton.Buy.onPress = function() { - Engine.PostNetworkCommand({ - "type": "barter", - "sell": g_BarterSell, - "buy": resourceCode, - "amount": amountToSell - }); - }; - - barterButton.Buy.hidden = isSelected; - barterButton.Buy.enabled = controlsPlayer(player); - barterButton.Sell.hidden = false; - selectionIcon.hidden = !isSelected; -} - -function updateBarterButtons() -{ - let playerState = GetSimState().players[g_ViewedPlayer]; - if (!playerState) - return; - - let canBarter = playerState.canBarter; - Engine.GetGUIObjectByName("barterNoMarketsMessage").hidden = canBarter; - Engine.GetGUIObjectByName("barterResources").hidden = !canBarter; - Engine.GetGUIObjectByName("barterHelp").hidden = !canBarter; - - if (canBarter) - g_ResourceData.GetBarterableCodes().forEach((resCode, i) => { - barterUpdateCommon(resCode, i, "barter", g_ViewedPlayer); - }); -} - -function getIdleLandTradersText(traderNumber) -{ - let active = traderNumber.landTrader.trading; - let garrisoned = traderNumber.landTrader.garrisoned; - let inactive = traderNumber.landTrader.total - active - garrisoned; - - let messageTypes = { - "active": { - "garrisoned": { - "no-inactive": translate("%(openingTradingString)s, and %(garrisonedString)s."), - "inactive": translate("%(openingTradingString)s, %(garrisonedString)s, and %(inactiveString)s.") - }, - "no-garrisoned": { - "no-inactive": translate("%(openingTradingString)s."), - "inactive": translate("%(openingTradingString)s, and %(inactiveString)s.") - } - }, - "no-active": { - "garrisoned": { - "no-inactive": translate("%(openingGarrisonedString)s."), - "inactive": translate("%(openingGarrisonedString)s, and %(inactiveString)s.") - }, - "no-garrisoned": { - "inactive": translatePlural("There is %(inactiveString)s.", "There are %(inactiveString)s.", inactive), - "no-inactive": translate("There are no land traders.") - } - } - }; - - let message = messageTypes[active ? "active" : "no-active"][garrisoned ? "garrisoned" : "no-garrisoned"][inactive ? "inactive" : "no-inactive"]; - - let activeString = sprintf( - translatePlural( - "There is %(numberTrading)s land trader trading", - "There are %(numberTrading)s land traders trading", - active - ), - { "numberTrading": active } - ); - - let inactiveString = sprintf( - active || garrisoned ? - translatePlural( - "%(numberOfLandTraders)s inactive", - "%(numberOfLandTraders)s inactive", - inactive - ) : - translatePlural( - "%(numberOfLandTraders)s land trader inactive", - "%(numberOfLandTraders)s land traders inactive", - inactive - ), - { "numberOfLandTraders": inactive } - ); - - let garrisonedString = sprintf( - active || inactive ? - translatePlural( - "%(numberGarrisoned)s garrisoned on a trading merchant ship", - "%(numberGarrisoned)s garrisoned on a trading merchant ship", - garrisoned - ) : - translatePlural( - "There is %(numberGarrisoned)s land trader garrisoned on a trading merchant ship", - "There are %(numberGarrisoned)s land traders garrisoned on a trading merchant ship", - garrisoned - ), - { "numberGarrisoned": garrisoned } - ); - - return sprintf(message, { - "openingTradingString": activeString, - "openingGarrisonedString": garrisonedString, - "garrisonedString": garrisonedString, - "inactiveString": coloredText(inactiveString, g_IdleTraderTextColor) - }); -} - -function getIdleShipTradersText(traderNumber) -{ - let active = traderNumber.shipTrader.trading; - let inactive = traderNumber.shipTrader.total - active; - - let messageTypes = { - "active": { - "inactive": translate("%(openingTradingString)s, and %(inactiveString)s."), - "no-inactive": translate("%(openingTradingString)s.") - }, - "no-active": { - "inactive": translatePlural("There is %(inactiveString)s.", "There are %(inactiveString)s.", inactive), - "no-inactive": translate("There are no merchant ships.") - } - }; - - let message = messageTypes[active ? "active" : "no-active"][inactive ? "inactive" : "no-inactive"]; - - let activeString = sprintf( - translatePlural( - "There is %(numberTrading)s merchant ship trading", - "There are %(numberTrading)s merchant ships trading", - active - ), - { "numberTrading": active } - ); - - let inactiveString = sprintf( - active ? - translatePlural( - "%(numberOfShipTraders)s inactive", - "%(numberOfShipTraders)s inactive", - inactive - ) : - translatePlural( - "%(numberOfShipTraders)s merchant ship inactive", - "%(numberOfShipTraders)s merchant ships inactive", - inactive - ), - { "numberOfShipTraders": inactive } - ); - - return sprintf(message, { - "openingTradingString": activeString, - "inactiveString": coloredText(inactiveString, g_IdleTraderTextColor) - }); -} - -function closeTrade() -{ - g_IsTradeOpen = false; - Engine.GetGUIObjectByName("tradeDialogPanel").hidden = true; -} - -function toggleTrade() -{ - let open = g_IsTradeOpen; - closeOpenDialogs(); - - if (!open) - openTrade(); -} - function toggleTutorial() { let tutorialPanel = Engine.GetGUIObjectByName("tutorialPanel"); @@ -863,9 +434,9 @@ function closeOpenDialogs() { closeMenu(); - closeTrade(); closeObjectives(); g_Chat.closePage(); g_DiplomacyDialog.close(); + g_TradeDialog.close(); } Index: ps/trunk/binaries/data/mods/public/gui/session/selection_panels.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/selection_panels.js +++ ps/trunk/binaries/data/mods/public/gui/session/selection_panels.js @@ -28,10 +28,12 @@ // Cache some formation info // Available formations per player -let g_AvailableFormations = new Map(); -let g_FormationsInfo = new Map(); +var g_AvailableFormations = new Map(); +var g_FormationsInfo = new Map(); -let g_SelectionPanels = {}; +var g_SelectionPanels = {}; + +var g_SelectionPanelBarterButtonManager; g_SelectionPanels.Alert = { "getMaxNumberOfItems": function() @@ -90,15 +92,11 @@ }, "setupButton": function(data) { - barterOpenCommon(data.item, data.i, "unitBarter"); - barterUpdateCommon(data.item, data.i, "unitBarter", data.player); - - let button = {}; - for (let action of g_BarterActions) - button[action] = Engine.GetGUIObjectByName("unitBarter" + action + "Button[" + data.i + "]"); - - setPanelObjectPosition(button.Sell, data.i, data.rowLength); - setPanelObjectPosition(button.Buy, data.i + data.rowLength, data.rowLength); + if (g_SelectionPanelBarterButtonManager) + { + g_SelectionPanelBarterButtonManager.setViewedPlayer(data.player); + g_SelectionPanelBarterButtonManager.update(); + } return true; } }; @@ -1131,6 +1129,14 @@ } }; +function initSelectionPanels() +{ + + let unitBarterPanel = Engine.GetGUIObjectByName("unitBarterPanel"); + if (BarterButtonManager.IsAvailable(unitBarterPanel)) + g_SelectionPanelBarterButtonManager = new BarterButtonManager(unitBarterPanel); +} + /** * Pauses game and opens the template details viewer for a selected entity or technology. * Index: ps/trunk/binaries/data/mods/public/gui/session/selection_panels_left/barter_panel.xml =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/selection_panels_left/barter_panel.xml +++ ps/trunk/binaries/data/mods/public/gui/session/selection_panels_left/barter_panel.xml @@ -1,23 +1,10 @@ -