/traderous

To get this branch, use:
bzr branch http://9ix.org/bzr/traderous

« back to all changes in this revision

Viewing changes to trade_view.lua

  • Committer: Josh C
  • Date: 2013-06-26 01:33:26 UTC
  • Revision ID: josh@9ix.org-20130626013326-k5uawz75pw2ujunu
s/spacey/spacey-nova/ (still need a real name...)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
TradeView = Subview:extend {
2
2
   drawParent = true, --default?
 
3
   have = {},
3
4
   onNew = function (self)
4
5
              -- create all the interface bits from the planet
5
6
              -- and add them to the view
6
7
 
7
 
              local boxL = the.app.width / 2 - 200
8
 
              local boxT = the.app.height / 2 - 200
 
8
              local boxL = the.app.width / 2 - 225
 
9
              local boxT = the.app.height / 2 - 150
 
10
              local boxR = boxL + 450
 
11
              local boxB = boxT + 300
 
12
 
 
13
              local lh = 17 -- line height
9
14
 
10
15
              self:add(Fill:new{
11
16
                          fill = {255,255,255},
12
17
                          x = boxL - 1,
13
18
                          y = boxT - 1,
14
 
                          width = 402,
15
 
                          height = 402,
 
19
                          width = 452,
 
20
                          height = 302,
16
21
                       })
17
22
 
18
23
              self:add(Fill:new{
19
24
                          fill = {0,0,0},
20
25
                          x = boxL,
21
26
                          y = boxT,
22
 
                          width = 400,
23
 
                          height = 400,
 
27
                          width = 450,
 
28
                          height = 300,
 
29
                       })
 
30
 
 
31
              self:add(Text:new{
 
32
                          text = 'Name',
 
33
                          x = boxL + 10,
 
34
                          y = boxT + 10,
 
35
                          width = 380
 
36
                       })
 
37
 
 
38
              self:add(Text:new{
 
39
                          text = 'Price',
 
40
                          x = boxL + 10,
 
41
                          y = boxT + 10,
 
42
                          width = 300,
 
43
                          align = 'right'
 
44
                       })
 
45
 
 
46
              self:add(Text:new{
 
47
                          text = 'Have',
 
48
                          align = 'right',
 
49
                          y = boxT + 10,
 
50
                          x = boxR - 40
24
51
                       })
25
52
 
26
53
              for i, good in ipairs(self.planet.goods) do
27
54
                 local t = Text:new{
28
55
                    text = good[1],
29
56
                    x = boxL + 10,
 
57
                    y = boxT + 10 + i * lh,
30
58
                    width = 380
31
59
                 }
32
60
 
33
 
                 local tw, th = t:getSize()
34
 
                 t.y = boxT + 10 + (i-1) * th -- (th + 2) -- for padding?
35
 
 
36
 
                 self:add(t)
37
 
              end
 
61
                 self:add(t)
 
62
 
 
63
                 t = Text:new{
 
64
                    text = good[2],
 
65
                    x = boxL + 10,
 
66
                    y = boxT + 10 + i * lh,
 
67
                    width = 300,
 
68
                    align = 'right'
 
69
                 }
 
70
                 self:add(t)
 
71
 
 
72
                 local b = Button:new{
 
73
                    x = boxR - 130,
 
74
                    y = boxT + 10 + i * lh - 1,
 
75
                    label = Text:new{ text = 'BUY', x = 1 },
 
76
                    onMouseDown = function (self)
 
77
                                     the.player:buy(good[1], good[2])
 
78
                                  end
 
79
                 }
 
80
                 local tw, th = b.label:getSize()
 
81
                 b.background = Fill:new{
 
82
                    fill = {100,100,100},
 
83
                    width = tw + 3,
 
84
                    height = th
 
85
                 }
 
86
                 self:add(b)
 
87
 
 
88
                 b = Button:new{
 
89
                    x = boxR - 90,
 
90
                    y = boxT + 10 + i * lh - 1,
 
91
                    label = Text:new{ text = 'SELL', x = 1 },
 
92
                    onMouseDown = function (self)
 
93
                                     the.player:sell(good[1], good[2])
 
94
                                  end
 
95
                 }
 
96
                 local tw, th = b.label:getSize()
 
97
                 b.background = Fill:new{
 
98
                    fill = {100,100,100},
 
99
                    width = tw + 3,
 
100
                    height = th
 
101
                 }
 
102
                 self:add(b)
 
103
 
 
104
                 local have = Text:new{
 
105
                    x = boxR - 110,
 
106
                    y = boxT + 10 + i * lh,
 
107
                    width = 100,
 
108
                    align = 'right',
 
109
                 }
 
110
                 self.have[good[1]] = have
 
111
                 self:add(have)
 
112
 
 
113
              end
 
114
 
 
115
              local b = Button:new{
 
116
                 x = boxL, y = boxT,
 
117
                 label = Text:new{ text = 'Close', x = 3, y = 2 },
 
118
                 onMouseDown = function()
 
119
                                  self:close()
 
120
                               end
 
121
              }
 
122
 
 
123
              local tw, th = b.label:getSize()
 
124
              b.background = Fill:new{
 
125
                 fill = {100,100,100},
 
126
                 width = tw + 7,
 
127
                 height = th + 4
 
128
              }
 
129
              b.x = boxR - b.background.width - 10
 
130
              b.y = boxB - b.background.height - 10
 
131
 
 
132
              self:add(b)
 
133
 
 
134
              local sh = the.player.shield
 
135
              local cost = (sh.max - sh.strength) * sh.cost
 
136
              b = Button:new{
 
137
                 x = boxL, y = boxT,
 
138
                 width = 800,
 
139
                 -- WTF: trailing space makes it not wrap?
 
140
                 label = Text:new{ text = 'Repair shields (' .. cost .. ') ',
 
141
                                   x = 3, y = 2 },
 
142
                 onMouseDown = function(self)
 
143
                                  if the.player.money >= cost and sh.strength < sh.max then
 
144
                                     the.player.money = the.player.money - cost
 
145
                                     sh.strength = sh.max
 
146
                                     self.visible = false
 
147
                                  end
 
148
                               end
 
149
              }
 
150
 
 
151
              local tw, th = b.label:getSize()
 
152
              b.background = Fill:new{
 
153
                 fill = {100,100,100},
 
154
                 width = tw + 3,
 
155
                 height = th + 4
 
156
              }
 
157
              b.x = boxR - b.background.width - 60
 
158
              b.y = boxB - b.background.height - 10
 
159
 
 
160
              if sh.strength < sh.max then
 
161
                 self:add(b)
 
162
              end
 
163
 
 
164
              self.playerMoney = Text:new{
 
165
                 text = 'Your money: ',
 
166
                 width = 200,
 
167
                 x = boxL + 10,
 
168
                 y = boxB - th - 10
 
169
              }
 
170
              self:add(self.playerMoney)
 
171
 
 
172
              self.availSpace = Text:new{
 
173
                 text = 'Available cargo space: ',
 
174
                 width = 200,
 
175
                 x = boxL + 10,
 
176
                 y = boxB - 2 * th - 12
 
177
              }
 
178
              self:add(self.availSpace)
 
179
 
 
180
              -- give the buttons a cycle to get out of the T/L corner
 
181
              self:update(0)
38
182
           end,
39
183
   activate = function (self)
40
184
                 the.cursor.visible = false
41
185
                 love.mouse.setVisible(true)
42
186
                 love.mouse.setGrab(false)
 
187
                 love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
43
188
 
44
189
                 Subview.activate(self)
45
190
              end,
46
 
 
 
191
   close = function (self)
 
192
              the.cursor.visible = true
 
193
              love.mouse.setVisible(false)
 
194
              love.mouse.setGrab(true)
 
195
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
 
196
 
 
197
              self:deactivate()
 
198
           end,
 
199
   onUpdate = function (self)
 
200
                 if the.keys:justPressed('escape') then
 
201
                    self:close()
 
202
                 end
 
203
 
 
204
                 self.playerMoney.text = 'Your money: ' .. the.player.money
 
205
                 self.availSpace.text = 'Available cargo space: ' .. the.player:availableSpace()
 
206
 
 
207
                 for good, have in pairs(self.have) do
 
208
                    have.text = the.player.goods[good] or 0
 
209
                 end
 
210
              end
47
211
}
 
 
b'\\ No newline at end of file'