forked from ennorehling/csmapfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtradeinfos.cpp
More file actions
289 lines (231 loc) · 7.65 KB
/
Copy pathtradeinfos.cpp
File metadata and controls
289 lines (231 loc) · 7.65 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "main.h"
#include "fxhelper.h"
#include "tradeinfos.h"
#include "symbols.h"
// *********************************************************************************************************
// *** FXTradeInfos implementation
FXDEFMAP(FXTradeInfos) MessageMap[]=
{
//________Message_Type_____________________ID_______________Message_Handler_______
FXMAPFUNC(SEL_COMMAND, FXTradeInfos::ID_UPDATE, FXTradeInfos::onMapChange),
};
FXIMPLEMENT(FXTradeInfos,FXVerticalFrame,MessageMap, ARRAYNUMBER(MessageMap))
FXTradeInfos::FXTradeInfos(FXComposite* p, FXObject* tgt,FXSelector sel, FXuint opts, FXint x,FXint y,FXint w,FXint h)
: FXVerticalFrame(p, opts, x,y,w,h, 0,0,0,0, 0,0)
{
// set target etc.
setTarget(tgt);
setSelector(sel);
// set style
setBorderColor(getApp()->getShadowColor());
setFrameStyle(FRAME_LINE);
// init variables
files = NULL;
// create layout
tags.topmatrix = new FXMatrix(this,3,MATRIX_BY_COLUMNS|LAYOUT_FILL_X, 0,0,0,0, 2,2,2,2, 0,0);
tags.matrixsep = new FXHorizontalSeparator(this, LAYOUT_FILL_X|SEPARATOR_LINE, 0,0,0,0, 0,0,0,0);
tags.matrixsep->setBorderColor(getBorderColor());
tags.matrixsep->hide();
tags.matrixframe = new FXHorizontalFrame(this, LAYOUT_SIDE_TOP|LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0, 0,0);
tags.matrixframe->hide();
tags.leftmatrix = new FXMatrix(tags.matrixframe,3,MATRIX_BY_COLUMNS|LAYOUT_FILL_X, 0,0,0,0, 2,2,2,2, 0,0);
FXFrame *sep = new FXVerticalSeparator(tags.matrixframe,LAYOUT_FILL_Y|SEPARATOR_LINE, 0,0,0,0, 0,0,0,0);
sep->setBorderColor(getBorderColor());
tags.rightmatrix = new FXMatrix(tags.matrixframe,3,MATRIX_BY_COLUMNS|LAYOUT_FILL_X, 0,0,0,0, 2,2,2,2, 0,0);
}
void FXTradeInfos::create()
{
FXVerticalFrame::create();
createLabels("Kein Handel", "", "", -1); // -1 == topmatrix
}
FXTradeInfos::~FXTradeInfos()
{
}
void FXTradeInfos::mapfiles(std::list<datafile> *f)
{
files = f;
}
inline FXString thousandsPoints(FXint value, bool plusSign = false)
{
FXString str = FXStringVal((value<0)?-value:value);
int n = 0;
for (int i = str.length(); i > 0; i--,n++)
if (n && !(n%3))
str.insert(i, '.');
if (value<0)
str = "-" + str;
else if (value && plusSign)
str = "+" + str;
return str;
}
void FXTradeInfos::clearLabels()
{
for (std::vector<FXLabel*>::iterator itor = tags.entries.begin(); itor != tags.entries.end(); itor++)
delete *itor;
tags.entries.clear();
}
void FXTradeInfos::createLabels(const FXString& name, const FXString& first, const FXString& second, int column)
{
FXMatrix *matrix = NULL;
if (column == -1)
matrix = tags.topmatrix;
else if (column == 0)
matrix = tags.leftmatrix;
else if (column == 1)
matrix = tags.rightmatrix;
// create labels
FXLabel *lname = new FXLabel(matrix, name, NULL, JUSTIFY_LEFT|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
FXLabel *lfirst = new FXLabel(matrix, first, NULL, JUSTIFY_RIGHT|LAYOUT_RIGHT);
FXLabel *lsecond = new FXLabel(matrix, second, NULL, JUSTIFY_RIGHT|LAYOUT_RIGHT);
lsecond->disable();
lname->create(); lfirst->create(); lsecond->create();
// put into list
tags.entries.push_back(lname);
tags.entries.push_back(lfirst);
tags.entries.push_back(lsecond);
}
void FXTradeInfos::setInfo(const std::list<Info>& info)
{
int number = info.size();
int index = 0;
for (std::list<Info>::const_iterator itor = info.begin(); itor != info.end(); itor++, index++)
{
FXString value = thousandsPoints(itor->value);
FXString offset = "";
if (itor->offset)
offset = thousandsPoints(itor->offset, true);
if (itor->skill)
value += " (" + FXStringVal(itor->skill) + ")";
FXString infotip = "\t" + value;
if (offset.length())
infotip += " [" + offset + "]";
createLabels(itor->name+"\t"+itor->tip, value+infotip, offset+infotip, 2*index>=number);
}
if (number)
{
tags.matrixsep->show();
tags.matrixframe->show();
}
else
{
tags.matrixsep->hide();
tags.matrixframe->hide();
}
}
void FXTradeInfos::addEntry(std::list<Info>& info, FXString name, int value, int skill, int offset, FXString tip)
{
std::list<Info>::iterator itor;
for (itor = info.begin(); itor != info.end(); itor++)
if (itor->name == name)
{
itor->value += value;
itor->skill += skill;
itor->offset += offset;
break;
}
if (itor == info.end())
info.push_back(Info(name, tip, value, skill, offset));
}
void FXTradeInfos::collectData(std::list<Info>& info, datablock::itor region)
{
// search prices block of this region
datablock::itor end = files->front().blocks().end();
datablock::itor block = region;
for (block++; block != end && block->depth() > region->depth(); block++)
if (block->type() == datablock::TYPE_PRICES)
break; // found
// found PREISE block?
if (block != end && block->type() == datablock::TYPE_PRICES)
{
/* PREISE
96;Balsam // wird angekauft
-5;Seide // wird verkauft
*/
for (datakey::itor goods = block->data().begin(); goods != block->data().end(); goods++)
if (goods->key().length() && goods->value().length())
{
FXint price = atoi(goods->value().text());
FXint offset = 0;
if (price >= 0)
addEntry(info, goods->key(), price, 0, offset, "Verkaufspreis " + goods->key());
else
{
price = -price;
createLabels("Einkaufspreis "+goods->key(), thousandsPoints(price), "", -1); // -1 == topmatrix
}
}
}
}
void FXTradeInfos::updateData()
{
//if (files->empty())
//return;
if (selection.selected & selection.MULTIPLE_REGIONS)
{
tags.matrixsep->hide();
tags.matrixframe->hide();
clearLabels();
createLabels("Kein Handel", "", "", -1); // -1 == topmatrix
}
else if (selection.selected & selection.REGION)
{
clearLabels();
// show information of newly selected region
datablock ®ion = *selection.region;
FXString peasants = region.value("Bauern");
FXint goods_at_price = 0;
if (!peasants.empty())
goods_at_price = atoi(peasants.text()) / 100; // one unit of goods for every 100 peasants
// -1 == topmatrix
createLabels(FXString(L"Luxusg\u00fcter zum angegebenen Preis"), thousandsPoints(goods_at_price), "", -1);
// collect information about luxury goods
std::list<Info> info;
collectData(info, selection.region);
// apply information entries (Bauern, Silber, Pferde...)
setInfo(info);
}
else
{
tags.matrixsep->hide();
tags.matrixframe->hide();
clearLabels();
createLabels("Kein Handel", "", "", -1); // -1 == topmatrix
}
}
long FXTradeInfos::onMapChange(FXObject*, FXSelector, void* ptr)
{
datafile::SelectionState *state = (datafile::SelectionState*)ptr;
// connected to a datafile list?
if (!files)
return 0;
bool needUpdate = false;
// any data changed, so need to update list?
if (selection.fileChange != state->fileChange)
{
selection.fileChange = state->fileChange;
selection.map = state->map;
selection.activefaction = state->activefaction;
needUpdate = true;
}
if (selection.selChange != state->selChange)
{
selection.selChange = state->selChange;
if ((selection.selected & selection.REGION) != (state->selected & selection.REGION)
|| (selection.selected & selection.REGION && selection.region != state->region))
needUpdate = true; // ignore changes that don't change selected region
if ((selection.selected & selection.MULTIPLE_REGIONS) != (state->selected & selection.MULTIPLE_REGIONS)
|| (selection.selected & selection.MULTIPLE_REGIONS && selection.regionsSelected != state->regionsSelected))
needUpdate = true;
selection.selected = state->selected;
selection.region = state->region;
selection.faction = state->faction;
selection.building = state->building;
selection.ship = state->ship;
selection.unit = state->unit;
if (needUpdate) // expensive operation
selection.regionsSelected = state->regionsSelected;
}
if (needUpdate)
updateData();
return 1;
}