-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCDockScreenStack.cpp
333 lines (247 loc) · 6.39 KB
/
CDockScreenStack.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// CDockScreenStack.cpp
//
// CDockScreenStack class
// Copyright (c) 2018 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
const SDockFrame CDockScreenStack::m_NullFrame;
void CDockScreenStack::DeleteAll (void)
// DeleteAll
//
// Delete all frames
{
while (!IsEmpty())
Pop();
}
const SDockFrame &CDockScreenStack::GetCurrent (void) const
// GetCurrent
//
// Returns the top of the stack
{
if (IsEmpty())
return m_NullFrame;
int iTop = m_Stack.GetCount() - 1;
return m_Stack[iTop];
}
ICCItem *CDockScreenStack::GetData (const CString &sAttrib)
// GetData
//
// Returns data for the given attribute. The caller is responsible for
// discarding this data.
{
CCodeChain &CC = g_pUniverse->GetCC();
if (IsEmpty())
return CC.CreateNil();
ICCItem *pResult = NULL;
ICCItem *pKey = CC.CreateString(sAttrib);
SDockFrame &Frame = m_Stack[m_Stack.GetCount() - 1];
if (Frame.pStoredData)
{
pResult = Frame.pStoredData->Lookup(&CC, pKey);
if (pResult->IsError())
{
pResult->Discard(&CC);
pResult = NULL;
}
}
if (pResult == NULL && Frame.pInitialData)
{
pResult = Frame.pInitialData->Lookup(&CC, pKey);
if (pResult->IsError())
{
pResult->Discard(&CC);
pResult = NULL;
}
}
pKey->Discard(&CC);
if (pResult == NULL)
pResult = CC.CreateNil();
return pResult;
}
const CString &CDockScreenStack::GetDisplayData (const CString &sID)
// GetDisplayData
//
// Returns opaque data stored by displays
{
if (IsEmpty())
return NULL_STR;
SDockFrame &Frame = m_Stack[m_Stack.GetCount() - 1];
CString *pValue = Frame.DisplayData.GetAt(sID);
if (pValue == NULL)
return NULL_STR;
return *pValue;
}
ICCItem *CDockScreenStack::GetReturnData (const CString &sAttrib)
// GetReturnData
//
// Returns data for the given attribute. The caller is responsible for
// discarding this data.
{
CCodeChain &CC = g_pUniverse->GetCC();
if (IsEmpty())
return CC.CreateNil();
SDockFrame &Frame = m_Stack[m_Stack.GetCount() - 1];
if (Frame.pReturnData)
{
ICCItem *pResult = Frame.pReturnData->GetElement(sAttrib);
if (pResult)
return pResult->Reference();
}
return CC.CreateNil();
}
void CDockScreenStack::Push (const SDockFrame &Frame)
// Push
//
// Push a frame
{
// Whenever we add a frame, we clear the return data block of the top frame
// so that we can get return data.
if (!IsEmpty())
m_Stack[m_Stack.GetCount() - 1].pReturnData.Delete();
// Add
m_Stack.Insert(Frame);
}
void CDockScreenStack::Pop (void)
// Pop
//
// Pop a frame
{
if (!IsEmpty())
{
int iTop = m_Stack.GetCount() - 1;
m_Stack.Delete(iTop);
}
}
void CDockScreenStack::ResolveCurrent (const SDockFrame &ResolvedFrame)
// SetCurrent
//
// Sets the current frame (and optionally returns the old frame).
//
// NOTE: Caller is responsible for discarding pInitialData if it get the previous
// frame.
{
if (!IsEmpty())
{
int iTop = m_Stack.GetCount() - 1;
m_Stack[iTop].pResolvedRoot = ResolvedFrame.pResolvedRoot;
m_Stack[iTop].sResolvedScreen = ResolvedFrame.sResolvedScreen;
}
}
void CDockScreenStack::SetCurrent (const SDockFrame &NewFrame, SDockFrame *retPrevFrame)
// SetCurrent
//
// Sets the current frame (and optionally returns the old frame).
//
// NOTE: Caller is responsible for discarding pInitialData if it get the previous
// frame.
{
if (!IsEmpty())
{
int iTop = m_Stack.GetCount() - 1;
if (retPrevFrame)
*retPrevFrame = m_Stack[iTop];
m_Stack[iTop] = NewFrame;
}
}
void CDockScreenStack::SetCurrentPane (const CString &sPane)
// SetCurrentPane
//
// Sets the current pane.
{
if (!IsEmpty())
{
int iTop = m_Stack.GetCount() - 1;
m_Stack[iTop].sPane = sPane;
}
}
void CDockScreenStack::IncData (const CString &sAttrib, ICCItem *pValue, ICCItem **retpResult)
// IncData
//
// Increments data
{
CCodeChain &CC = g_pUniverse->GetCC();
if (IsEmpty())
{
if (retpResult) *retpResult = CC.CreateNil();
return;
}
// If pValue is NULL, we default to 1. We add ref no matter what so that
// we can discard unconditionally.
if (pValue == NULL)
pValue = CC.CreateInteger(1);
else
pValue->Reference();
// If the entry is currently blank, then we just take the increment.
ICCItem *pOriginal = GetData(sAttrib);
ICCItem *pResult = NULL;
if (pOriginal->IsNil())
pResult = pValue->Reference();
// Otherwise, we need to get the data value
else
{
if (pOriginal->IsDouble() || pValue->IsDouble())
pResult = CC.CreateDouble(pOriginal->GetDoubleValue() + pValue->GetDoubleValue());
else
pResult = CC.CreateInteger(pOriginal->GetIntegerValue() + pValue->GetIntegerValue());
}
pOriginal->Discard(&CC);
// Store
SetData(sAttrib, pResult);
// Done
if (retpResult)
*retpResult = pResult;
else
pResult->Discard(&CC);
pValue->Discard(&CC);
}
void CDockScreenStack::SetData (const CString &sAttrib, ICCItem *pData)
// SetData
//
// Sets data associated with the current frame
{
CCodeChain &CC = g_pUniverse->GetCC();
if (IsEmpty())
return;
// If necessary, create the stored data block
SDockFrame &Frame = m_Stack[m_Stack.GetCount() - 1];
if (!Frame.pStoredData)
Frame.pStoredData = ICCItemPtr(CC.CreateSymbolTable());
// Add the entry
ICCItem *pKey = CC.CreateString(sAttrib);
ICCItem *pResult = Frame.pStoredData->AddEntry(&CC, pKey, pData);
pResult->Discard(&CC);
pKey->Discard(&CC);
}
void CDockScreenStack::SetDisplayData (const CString &sID, const CString &sData)
// SetDisplayData
//
// Sets opaque data for a display
{
if (IsEmpty())
return;
SDockFrame &Frame = m_Stack[m_Stack.GetCount() - 1];
Frame.DisplayData.SetAt(sID, sData);
}
void CDockScreenStack::SetLocation (CSpaceObject *pLocation)
// SetLocation
//
// Sets the location for all frames. This is called, e.g., when we change ships.
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
m_Stack[i].pLocation = pLocation;
}
void CDockScreenStack::SetReturnData (const CString &sAttrib, ICCItem *pData)
// SetReturnData
//
// Sets data associated with previous frame.
{
CCodeChain &CC = g_pUniverse->GetCC();
if (m_Stack.GetCount() < 2)
return;
// If necessary, create the stored data block
SDockFrame &Frame = m_Stack[m_Stack.GetCount() - 2];
if (!Frame.pReturnData)
Frame.pReturnData = ICCItemPtr(CC.CreateSymbolTable());
// Add the entry
Frame.pReturnData->SetAt(CC, sAttrib, pData);
}