/traderous

To get this branch, use:
bzr branch /bzr/traderous
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
TradeView = Subview:extend {
   drawParent = true, --default?
   have = {},
   onNew = function (self)
              -- create all the interface bits from the planet
              -- and add them to the view

              local boxL = the.app.width / 2 - 225
              local boxT = the.app.height / 2 - 150
              local boxR = boxL + 450
              local boxB = boxT + 300

              local lh = 17 -- line height

              self:add(Fill:new{
                          fill = {255,255,255},
                          x = boxL - 1,
                          y = boxT - 1,
                          width = 452,
                          height = 302,
                       })

              self:add(Fill:new{
                          fill = {0,0,0},
                          x = boxL,
                          y = boxT,
                          width = 450,
                          height = 300,
                       })

              self:add(Text:new{
                          text = 'Name',
                          x = boxL + 10,
                          y = boxT + 10,
                          width = 380
                       })

              self:add(Text:new{
                          text = 'Price',
                          x = boxL + 10,
                          y = boxT + 10,
                          width = 300,
                          align = 'right'
                       })

              self:add(Text:new{
                          text = 'Have',
                          align = 'right',
                          y = boxT + 10,
                          x = boxR - 40
                       })

              for i, good in ipairs(self.planet.goods) do
                 local t = Text:new{
                    text = good[1],
                    x = boxL + 10,
                    y = boxT + 10 + i * lh,
                    width = 380
                 }

                 self:add(t)

                 t = Text:new{
                    text = good[2],
                    x = boxL + 10,
                    y = boxT + 10 + i * lh,
                    width = 300,
                    align = 'right'
                 }
                 self:add(t)

                 local b = Button:new{
                    x = boxR - 130,
                    y = boxT + 10 + i * lh - 1,
                    label = Text:new{ text = 'BUY', x = 1 },
                    onMouseDown = function (self)
                                     the.player:buy(good[1], good[2])
                                  end
                 }
                 local tw, th = b.label:getSize()
                 b.background = Fill:new{
                    fill = {100,100,100},
                    width = tw + 3,
                    height = th
                 }
                 self:add(b)

                 b = Button:new{
                    x = boxR - 90,
                    y = boxT + 10 + i * lh - 1,
                    label = Text:new{ text = 'SELL', x = 1 },
                    onMouseDown = function (self)
                                     the.player:sell(good[1], good[2])
                                  end
                 }
                 local tw, th = b.label:getSize()
                 b.background = Fill:new{
                    fill = {100,100,100},
                    width = tw + 3,
                    height = th
                 }
                 self:add(b)

                 local have = Text:new{
                    x = boxR - 110,
                    y = boxT + 10 + i * lh,
                    width = 100,
                    align = 'right',
                 }
                 self.have[good[1]] = have
                 self:add(have)

              end

              local b = Button:new{
                 x = boxL, y = boxT,
                 label = Text:new{ text = 'Close', x = 3, y = 2 },
                 onMouseDown = function()
                                  self:close()
                               end
              }

              local tw, th = b.label:getSize()
              b.background = Fill:new{
                 fill = {100,100,100},
                 width = tw + 7,
                 height = th + 4
              }
              b.x = boxR - b.background.width - 10
              b.y = boxB - b.background.height - 10

              self:add(b)

              local sh = the.player.shield
              local cost = (sh.max - sh.strength) * sh.cost
              b = Button:new{
                 x = boxL, y = boxT,
                 width = 800,
                 -- WTF: trailing space makes it not wrap?
                 label = Text:new{ text = 'Repair shields (' .. cost .. ') ',
                                   x = 3, y = 2 },
                 onMouseDown = function(self)
                                  if the.player.money >= cost and sh.strength < sh.max then
                                     the.player.money = the.player.money - cost
                                     sh.strength = sh.max
                                     self.visible = false
                                  end
                               end
              }

              local tw, th = b.label:getSize()
              b.background = Fill:new{
                 fill = {100,100,100},
                 width = tw + 3,
                 height = th + 4
              }
              b.x = boxR - b.background.width - 60
              b.y = boxB - b.background.height - 10

              if sh.strength < sh.max then
                 self:add(b)
              end

              self.playerMoney = Text:new{
                 text = 'Your money: ',
                 width = 200,
                 x = boxL + 10,
                 y = boxB - th - 10
              }
              self:add(self.playerMoney)

              self.availSpace = Text:new{
                 text = 'Available cargo space: ',
                 width = 200,
                 x = boxL + 10,
                 y = boxB - 2 * th - 12
              }
              self:add(self.availSpace)

              -- give the buttons a cycle to get out of the T/L corner
              self:update(0)
           end,
   activate = function (self)
                 the.cursor.visible = false
                 love.mouse.setVisible(true)
                 love.mouse.setGrab(false)
                 love.mouse.setPosition(the.app.width / 2, the.app.height / 2)

                 Subview.activate(self)
              end,
   close = function (self)
              the.cursor.visible = true
              love.mouse.setVisible(false)
              love.mouse.setGrab(true)
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)

              self:deactivate()
           end,
   onUpdate = function (self)
                 if the.keys:justPressed('escape') then
                    self:close()
                 end

                 self.playerMoney.text = 'Your money: ' .. the.player.money
                 self.availSpace.text = 'Available cargo space: ' .. the.player:availableSpace()

                 for good, have in pairs(self.have) do
                    have.text = the.player.goods[good] or 0
                 end
              end
}