~alanbell/dasher/ircfix-lp579181

« back to all changes in this revision

Viewing changes to Src/DasherCore/AlphabetManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-02-13 08:08:15 UTC
  • mfrom: (1.2.17 upstream)
  • Revision ID: james.westby@ubuntu.com-20070213080815-bpx0yjnzkwfamvi8
Tags: 4.3.4-0ubuntu1
* New upstream release:
  - Updated rendering routines
  - General tidying of gobject implemenatations
  - Translation updates
* debian/rules: 
  - build with --enable-joystick.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include "../Common/Common.h"
3
3
 
4
4
#include "AlphabetManager.h"
5
 
//#include "DasherModel.h"
 
5
#include "DasherInterfaceBase.h"
6
6
#include "DasherNode.h"
7
7
#include "Event.h"
8
8
#include "EventHandler.h"
23
23
#endif
24
24
#endif
25
25
 
26
 
CAlphabetManager::CAlphabetManager( CNodeCreationManager *pNCManager, CLanguageModel *pLanguageModel, CLanguageModel::Context iLearnContext, bool bGameMode, const std::string &strGameModeText ) 
 
26
CAlphabetManager::CAlphabetManager(CDasherInterfaceBase *pInterface, CNodeCreationManager *pNCManager, CLanguageModel *pLanguageModel, CLanguageModel::Context iLearnContext, bool bGameMode, const std::string &strGameModeText ) 
27
27
  : CNodeManager(0), m_pLanguageModel(pLanguageModel), m_pNCManager(pNCManager) {
28
28
 
 
29
  m_pInterface = pInterface;
 
30
 
29
31
  m_iLearnContext = iLearnContext;
30
32
  m_bGameMode = bGameMode;
31
33
  m_strGameString = strGameModeText;
34
36
CDasherNode *CAlphabetManager::GetRoot(CDasherNode *pParent, int iLower, int iUpper, void *pUserData) {
35
37
  CDasherNode *pNewNode;
36
38
 
 
39
  // TODO: iOffset has gotten a bit hacky here
 
40
 
37
41
  int iSymbol;
38
 
 
39
 
  if(pUserData)
40
 
    iSymbol = *((int *)pUserData);
41
 
  else
42
 
    iSymbol = 0;
43
 
 
44
 
  // FIXME - Make this a CDasherComponent
45
 
 
 
42
  int iOffset;
46
43
  int iColour;
47
44
  
48
 
  if(iSymbol == 0)
 
45
  std::string strContext;
 
46
 
 
47
  CLanguageModel::Context iContext;
 
48
 
 
49
  if(pUserData && static_cast<SRootData *>(pUserData)->szContext) {
 
50
    std::string strRootText(static_cast<SRootData *>(pUserData)->szContext);
 
51
 
 
52
    int iMaxContextLength = m_pLanguageModel->GetContextLength() + 1;
 
53
 
 
54
    // TODO: No need to explicitly pass context
 
55
    // TODO: Utility function for looking up symbolic context
 
56
 
 
57
    int iStart = static_cast<SRootData *>(pUserData)->iOffset - iMaxContextLength;
 
58
    if(iStart < 0)
 
59
      iStart = 0;
 
60
 
 
61
    strContext = m_pInterface->GetContext(iStart, static_cast<SRootData *>(pUserData)->iOffset - iStart);
 
62
    BuildContext(strContext, false, iContext, iSymbol);
 
63
 
 
64
    iOffset = static_cast<SRootData *>(pUserData)->iOffset - 1;
 
65
    iColour = m_pNCManager->GetColour(iSymbol);
 
66
  }
 
67
  else {
 
68
    // Create a root node
 
69
    
 
70
    if(pUserData)
 
71
      iOffset = static_cast<SRootData *>(pUserData)->iOffset;
 
72
    else
 
73
      iOffset = -1;
49
74
    iColour = 7;
50
 
  else
51
 
    iColour = m_pNCManager->GetColour(iSymbol);
 
75
 
 
76
    strContext = m_pNCManager->GetAlphabet()->GetDefaultContext();
 
77
    BuildContext(strContext, true, iContext, iSymbol);
 
78
  }
 
79
 
 
80
  // FIXME - Make this a CDasherComponent
52
81
 
53
82
  pNewNode = new CDasherNode(pParent, iSymbol, 0, Opts::Nodes1, iLower, iUpper, m_pLanguageModel, iColour);
54
 
  
55
 
  //  pNewNode->SetContext(m_pLanguageModel->CreateEmptyContext()); // FIXME - handle context properly
56
83
  pNewNode->m_pNodeManager = this;
57
 
  pNewNode->m_bShove = true;
 
84
  pNewNode->SetShove(true);
58
85
  pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup;
59
86
  pNewNode->m_strDisplayText = m_pNCManager->GetAlphabet()->GetDisplayText(iSymbol);
60
 
  pNewNode->Seen(true);
 
87
  pNewNode->SetFlag(NF_SEEN, true);
61
88
 
62
89
  SAlphabetData *pNodeUserData = new SAlphabetData;
63
90
  pNewNode->m_pUserData = pNodeUserData;
64
91
 
 
92
  pNodeUserData->iOffset = iOffset;
65
93
  pNodeUserData->iPhase = 0;
66
94
  pNodeUserData->iSymbol = iSymbol;
67
95
  pNodeUserData->pLanguageModel = m_pLanguageModel;
68
96
 
69
 
  // Hopefully this will obsolete any need to handle contexts outside
70
 
  // of the alphabet manager - check this and remove resulting
71
 
  // redundant code
72
 
  CLanguageModel::Context iContext;
73
 
  iContext = m_pLanguageModel->CreateEmptyContext();
74
 
 
75
 
  std::string strContext = m_pNCManager->GetAlphabet()->GetDefaultContext();
76
 
 
77
 
  std::vector<symbol> vContextSymbols;
78
 
  m_pNCManager->GetAlphabet()->GetSymbols(&vContextSymbols, &strContext, false);
79
 
 
80
 
  for(std::vector<symbol>::iterator it(vContextSymbols.begin()); it != vContextSymbols.end(); ++it)
81
 
    m_pLanguageModel->EnterSymbol(iContext, *it);
82
 
 
83
97
  pNodeUserData->iContext = iContext;
84
98
 
85
99
  if(m_bGameMode) {
86
100
    pNodeUserData->iGameOffset = -1;
87
 
    pNewNode->SetGame(true);
 
101
    pNewNode->SetFlag(NF_GAME, true);
88
102
  }
89
103
 
90
104
  return pNewNode;
121
135
  for(int j = 0; j < iChildCount; j++) {
122
136
    CDasherNode *pNewNode;
123
137
    
124
 
    if(newchars[j] == m_pNCManager->GetControlSymbol())
125
 
      pNewNode = m_pNCManager->GetRoot(1, pNode, iLbnd, cum[j], NULL);
 
138
    if(newchars[j] == m_pNCManager->GetControlSymbol()) {
 
139
      pNewNode = m_pNCManager->GetRoot(1, pNode, iLbnd, cum[j], &(pParentUserData->iOffset));
 
140
    }
126
141
    else if(newchars[j] == m_pNCManager->GetStartConversionSymbol()) {
127
142
      // TODO: Need to consider the case where there is no compile-time support for this
128
 
      pNewNode = m_pNCManager->GetRoot(2, pNode, iLbnd, cum[j], NULL);
 
143
      pNewNode = m_pNCManager->GetRoot(2, pNode, iLbnd, cum[j], &(pParentUserData->iOffset));
129
144
    }
130
145
    else if( newchars[j] == iExistingSymbol) {
131
146
      pNewNode = pExistingChild;
 
147
 
 
148
      //      std::cout << "Marker" << std::endl;
 
149
      
132
150
      pNewNode->SetRange(iLbnd, cum[j]);
133
151
    }
134
152
    else {
154
172
      
155
173
      pNewNode = new CDasherNode(pNode, newchars[j], 0, Nodes1, iLbnd, cum[j], pParentUserData->pLanguageModel, iColour);
156
174
      pNewNode->m_pNodeManager = this;
157
 
      pNewNode->m_bShove = true;
 
175
      pNewNode->SetShove(true);
158
176
      pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup;
159
177
      
160
178
      SAlphabetData *pNodeUserData = new SAlphabetData;
161
179
      pNewNode->m_pUserData = pNodeUserData;
162
180
      
 
181
      pNodeUserData->iOffset = pParentUserData->iOffset + 1;
163
182
      pNodeUserData->iPhase = iNewPhase;
164
183
      pNodeUserData->iSymbol = newchars[j];
165
184
      
169
188
        if((iCurrentGameOffset != -2) && ((iCurrentGameOffset + 1) < static_cast<int>(m_strGameString.size())) 
170
189
           && ((m_pNCManager->GetAlphabet()->GetText(newchars[j]))[0] == m_strGameString[iCurrentGameOffset + 1])) {
171
190
          static_cast<SAlphabetData *>(pNewNode->m_pUserData)->iGameOffset = iCurrentGameOffset + 1;
172
 
          pNewNode->SetGame(true);
 
191
          pNewNode->SetFlag(NF_GAME, true);
173
192
        }
174
193
        else
175
194
          static_cast<SAlphabetData *>(pNewNode->m_pUserData)->iGameOffset = -2;
191
210
    iLbnd = cum[j];
192
211
  }
193
212
  
194
 
  pNode->SetHasAllChildren(true);
 
213
  pNode->SetFlag(NF_ALLCHILDREN, true);
195
214
}
196
215
 
197
216
void CAlphabetManager::ClearNode( CDasherNode *pNode ) {
202
221
}
203
222
 
204
223
void CAlphabetManager::Output( CDasherNode *pNode, Dasher::VECTOR_SYMBOL_PROB* pAdded, int iNormalization) {
205
 
  //  m_pNCManager->m_bContextSensitive = true;
206
224
  symbol t = static_cast<SAlphabetData *>(pNode->m_pUserData)->iSymbol;
207
225
 
208
226
  if(t) { // Ignore symbol 0 (root node)
230
248
 
231
249
CDasherNode *CAlphabetManager::RebuildParent(CDasherNode *pNode, int iGeneration) {
232
250
 
233
 
  // TODO: Reimplement -----
234
 
 
235
 
  // Ask for context information
236
 
  //  m_pNCManager->m_strContextBuffer = "";
237
 
  //  Dasher::CEditContextEvent oEvent(10);
238
 
  //  m_pNCManager->InsertEvent(&oEvent);
239
 
 
240
 
  //  std::string strContext(m_pNCManager->m_strContextBuffer);
241
 
  std::vector<symbol> vSymbols;
242
 
  //  m_pLanguageModel->SymbolAlphabet().GetAlphabetPointer()->GetSymbols(&vSymbols, &strContext, false);
243
 
 
244
 
  // -----
 
251
  // TODO: iGeneration obsolete?
 
252
 
 
253
  int iOffset(static_cast<SAlphabetData *>(pNode->m_pUserData)->iOffset);
 
254
  int iNewOffset = iOffset - 1;
245
255
 
246
256
  CDasherNode *pNewNode;
247
257
 
250
260
 
251
261
  symbol iNewSymbol;
252
262
 
253
 
  CLanguageModel::Context oContext(m_pLanguageModel->CreateEmptyContext());
254
 
 
255
 
  if(static_cast<int>(vSymbols.size()) <= iGeneration + 1) {
256
 
    // Create a root node if there isn't any more context
 
263
  std::string strContext;
 
264
  CLanguageModel::Context iContext;
 
265
   
 
266
  if(iOffset == -1) {
 
267
    // pNode is already a root
 
268
    return NULL;
 
269
  }
 
270
  else if(iOffset == 0) {
 
271
    // TODO: Creating a root node, Shouldn't be a special case
257
272
    iNewPhase = 0;
258
273
    iNewSymbol = 0;
 
274
    strContext = m_pNCManager->GetAlphabet()->GetDefaultContext();
 
275
    BuildContext(strContext, true, iContext, iNewSymbol);
259
276
 
260
277
    pNewNode = new CDasherNode(0, 0, 0,  Opts::Nodes1, 0, 0, m_pLanguageModel, 7);
261
 
    
262
 
    // TODO: Horrible hard coded default context
263
 
    m_pNCManager->EnterText(oContext, ". ");
264
278
  }
265
279
  else {
266
 
    
 
280
    int iMaxContextLength = m_pLanguageModel->GetContextLength() + 1;
 
281
 
 
282
    int iStart = iOffset - iMaxContextLength;
 
283
    if(iStart < 0)
 
284
      iStart = 0;
 
285
 
 
286
    strContext = m_pInterface->GetContext(iStart, iOffset - iStart);
 
287
    BuildContext(strContext, false, iContext, iNewSymbol);
 
288
 
267
289
    iNewPhase = ((iOldPhase + 2 - 1) % 2);
268
 
    iNewSymbol = vSymbols[vSymbols.size() - iGeneration - 2];
269
290
 
270
 
    int iColour(m_pNCManager->GetColour(vSymbols[vSymbols.size() - iGeneration - 2]));
 
291
    int iColour(m_pNCManager->GetColour(iNewSymbol));
271
292
 
272
293
    // Loop colours if necessary for the colour scheme
273
294
    if(iNewPhase == 1)
274
295
      iColour += 130;
275
296
            
276
297
    pNewNode = new CDasherNode(0, iNewSymbol, 0, Nodes1, 0, 0, m_pLanguageModel, iColour);
277
 
 
278
 
    for(unsigned int i(0); i < vSymbols.size() - iGeneration -1; ++i)
279
 
      m_pLanguageModel->EnterSymbol(oContext, vSymbols[i]);
280
298
  }
281
 
  
 
299
 
 
300
  // TODO: Some of this context stuff could be consolidated
 
301
 
282
302
  pNewNode->m_pNodeManager = this;
283
 
  pNewNode->m_bShove = true;
284
 
  pNewNode->Seen(true);
 
303
  pNewNode->SetShove(true);
 
304
  pNewNode->SetFlag(NF_SEEN, true);
285
305
  pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup;
286
306
 
287
307
  SAlphabetData *pNodeUserData = new SAlphabetData;
288
308
  pNewNode->m_pUserData = pNodeUserData;
289
309
 
 
310
  pNodeUserData->iOffset = iNewOffset;
290
311
  pNodeUserData->iPhase = iNewPhase;
291
312
  pNodeUserData->iSymbol = iNewSymbol;
292
313
  pNodeUserData->pLanguageModel = m_pLanguageModel;
293
 
  pNodeUserData->iContext = oContext;
 
314
  pNodeUserData->iContext = iContext;
294
315
 
295
316
  PopulateChildrenWithSymbol(pNewNode, static_cast<SAlphabetData *>(pNode->m_pUserData)->iSymbol, pNode);
296
317
 
308
329
    break;
309
330
  }
310
331
}
 
332
 
 
333
void CAlphabetManager::BuildContext(std::string strContext, bool bRoot, CLanguageModel::Context &oContext, symbol &iSymbol) {
 
334
  // Hopefully this will obsolete any need to handle contexts outside
 
335
  // of the alphabet manager - check this and remove resulting
 
336
  // redundant code
 
337
 
 
338
  std::vector<symbol> vContextSymbols;
 
339
  m_pNCManager->GetAlphabet()->GetSymbols(&vContextSymbols, &strContext, false);
 
340
 
 
341
  oContext = m_pLanguageModel->CreateEmptyContext();
 
342
  
 
343
  for(std::vector<symbol>::iterator it(vContextSymbols.begin()); it != vContextSymbols.end(); ++it)
 
344
    m_pLanguageModel->EnterSymbol(oContext, *it);
 
345
 
 
346
  if((vContextSymbols.size() == 0) || bRoot)
 
347
    iSymbol = 0;
 
348
  else
 
349
    iSymbol = vContextSymbols[vContextSymbols.size() - 1];
 
350
}