source: ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButton.js@ 23072

Last change on this file since 23072 was 23072, checked in by elexis, 5 years ago

Refactor trade dialog and barter panel buttons to use object orientation, refs #23, #2311, #4366, #5387, rP10588, rP14417, rP17553, rP19354.

Differential Revision: https://code.wildfiregames.com/D2369

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1/**
2 * This class manages a buy and sell button for one resource.
3 * The sell button selects the resource to be sold.
4 * The buy button selects the resource to be bought and performs the sale.
5 */
6class BarterButton
7{
8 constructor(barterButtonManager, resourceCode, i, panel)
9 {
10 this.barterButtonManager = barterButtonManager;
11 this.resourceCode = resourceCode;
12 this.amountToSell = 0;
13
14 this.sellButton = panel.children[i].children[0].children[0];
15 this.sellIcon = this.sellButton.children[0];
16 this.sellAmount = this.sellButton.children[1];
17 this.sellSelection = this.sellButton.children[2];
18
19 this.buyButton = panel.children[i].children[0].children[1];
20 this.buyIcon= this.buyButton.children[0];
21 this.buyAmount = this.buyButton.children[1];
22
23 let resourceName = { "resource": resourceNameWithinSentence(resourceCode) };
24
25 this.sellButton.tooltip = sprintf(this.SellTooltip, resourceName);
26 this.sellButton.onPress = () => { barterButtonManager.setSelectedResource(this.resourceCode); };
27 this.sellButton.hidden = false;
28
29 this.buyButton.tooltip = sprintf(this.BuyTooltip, resourceName);
30 this.buyButton.onPress = () => { this.buy(); };
31
32 this.iconPath = this.ResourceIconPath + resourceCode + ".png";
33
34 setPanelObjectPosition(panel.children[i], i, Infinity);
35 }
36
37 buy()
38 {
39 Engine.PostNetworkCommand({
40 "type": "barter",
41 "sell": this.barterButtonManager.selectedResource,
42 "buy": this.resourceCode,
43 "amount": this.amountToSell
44 });
45 }
46
47 /**
48 * The viewed player might be the owner of the selected market.
49 */
50 update(viewedPlayer)
51 {
52 this.amountToSell = this.BarterResourceSellQuantity;
53
54 if (Engine.HotkeyIsPressed("session.massbarter"))
55 this.amountToSell *= this.Multiplier;
56
57 let neededResourcesSell = Engine.GuiInterfaceCall("GetNeededResources", {
58 "cost": {
59 [this.resourceCode]: this.amountToSell
60 },
61 "player": viewedPlayer
62 });
63
64 let neededResourcesBuy = Engine.GuiInterfaceCall("GetNeededResources", {
65 "cost": {
66 [this.barterButtonManager.selectedResource]: this.amountToSell
67 },
68 "player": viewedPlayer
69 });
70
71 let isSelected = this.resourceCode == this.barterButtonManager.selectedResource;
72 let icon = "stretched:" + (isSelected ? this.SelectedModifier : "") + this.iconPath;
73 this.sellIcon.sprite = (neededResourcesSell ? this.DisabledModifier : "") + icon;
74 this.buyIcon.sprite = (neededResourcesBuy ? this.DisabledModifier : "") + icon;
75
76 this.sellAmount.caption = sprintf(translateWithContext("sell action", this.SellCaption), {
77 "amount": this.amountToSell
78 });
79
80 let prices = GetSimState().players[viewedPlayer].barterPrices;
81
82 this.buyAmount.caption = sprintf(translateWithContext("buy action", this.BuyCaption), {
83 "amount": Math.round(
84 prices.sell[this.barterButtonManager.selectedResource] /
85 prices.buy[this.resourceCode] * this.amountToSell)
86 });
87
88 this.buyButton.hidden = isSelected;
89 this.buyButton.enabled = controlsPlayer(viewedPlayer);
90 this.sellSelection.hidden = !isSelected;
91 }
92}
93
94BarterButton.getWidth = function(panel)
95{
96 let size = panel.children[0].size;
97 return size.right - size.left;
98};
99
100BarterButton.prototype.BuyTooltip = markForTranslation("Buy %(resource)s");
101BarterButton.prototype.SellTooltip = markForTranslation("Sell %(resource)s");
102BarterButton.prototype.BuyCaption = markForTranslationWithContext("buy action", "+%(amount)s");
103BarterButton.prototype.SellCaption = markForTranslationWithContext("sell action", "-%(amount)s");
104
105/**
106 * The barter constants should match with the simulation Quantity of goods to sell per click.
107 */
108BarterButton.prototype.BarterResourceSellQuantity = 100;
109
110/**
111 * Multiplier to be applied when holding the massbarter hotkey.
112 */
113BarterButton.prototype.Multiplier = 5;
114
115BarterButton.prototype.SelectedModifier = "color:0 0 0 100:grayscale:";
116
117BarterButton.prototype.DisabledModifier = "color:255 0 0 80:";
118
119BarterButton.prototype.ResourceIconPath = "session/icons/resources/";
Note: See TracBrowser for help on using the repository browser.