5 // Copyright (c) 2001-2005 David Ward
7 #ifndef __DasherModel_h__
8 #define __DasherModel_h__
14 #include "../Common/NoClones.h"
16 #include "LanguageModelling/LanguageModel.h"
17 #include "DasherNode.h"
18 #include "DasherComponent.h"
19 #include "Alphabet/Alphabet.h"
20 #include "Alphabet/AlphIO.h"
21 #include "AlphabetManagerFactory.h"
22 #include "ControlManagerFactory.h"
25 #include "ConversionManagerFactory.h"
29 #include "DasherTypes.h"
30 #include "FrameRate.h"
38 class CDasherInterfaceBase
;
43 /// \brief Dasher 'world' data structures and dynamics.
45 /// The DasherModel represents the current state of Dasher
46 /// It contains a tree of DasherNodes
47 /// knows the current viewpoint
48 /// knows how to evolve the viewpoint
50 class Dasher::CDasherModel
:public Dasher::CDasherComponent
, private NoClones
56 CTrainer(CDasherModel
& DasherModel
);
58 void Train(const std::vector
< symbol
> &vSymbols
);
63 CLanguageModel::Context m_Context
;
64 CDasherModel
& m_DasherModel
;
76 CDasherModel(CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
, CDasherInterfaceBase
* pDashIface
, CAlphIO
*pAlphIO
, bool bGameMode
= false, const std::string
&strGameModeText
= "");
79 bool WriteLMToFile(const std::string
&strFilename
) {
81 return m_pLanguageModel
->WriteToFile(strFilename
);
86 bool ReadLMFromFile(const std::string
&strFilename
) {
88 return m_pLanguageModel
->ReadFromFile(strFilename
);
93 void HandleEvent(Dasher::CEvent
* pEvent
);
95 CTrainer
*GetTrainer();
97 // framerate functions
98 void NewFrame(unsigned long Time
) {
102 // called everytime we render a new frame
103 double Framerate() const {
104 return m_fr
.Framerate();
107 void Reset_framerate(unsigned long Time
) {
115 void RecursiveOutput(CDasherNode
*pNode
, Dasher::VECTOR_SYMBOL_PROB
* pAdded
);
117 // User control of speed
118 void SetBitrate(double TargetRate
) {
119 m_fr
.SetBitrate(TargetRate
);
120 } // Use or start at this bitrate
121 //void SetMaxBitrate(double MaxRate) {m_dMaxRate=MaxRate;m_fr.SetMaxBitrate(MaxRate);} // Cap any adaption at this rate
123 /* std::string GroupLabel(int group) const { */
124 /* return m_pcAlphabet->GetGroupLabel(group); */
126 /* int GroupColour(int group) const { */
127 /* return m_pcAlphabet->GetGroupColour(group); */
130 void SetContext(std::string
& sNewContext
);
132 // functions returning private data (read only access)
133 myint
Rootmin() const {
136 myint
Rootmax() const {
139 CDasherNode
*Root() const {
143 void OutputCharacters(CDasherNode
* node
);
144 bool DeleteCharacters(CDasherNode
* newnode
, CDasherNode
* oldnode
, int* pNumDeleted
= NULL
);
146 void Trace() const; // diagnostics
147 //void Learn_symbol(symbol Symbol) {m_languagemodel->learn_symbol(Symbol);} // feed character to language model
149 bool Tap_on_display(myint
, myint
, unsigned long iTime
, Dasher::VECTOR_SYMBOL_PROB
* pAdded
= NULL
, int* pNumDeleted
= NULL
); // evolves the current viewpoint
151 /* void GoTo(double, myint); // jumps to a new viewpoint */
152 void NewGoTo(myint n1
, myint n2
, Dasher::VECTOR_SYMBOL_PROB
* pAdded
, int* pNumDeleted
);
153 void OldPush(myint iMousex
, myint iMousey
);
154 double Plan_new_goto_coords(int iRxnew
, myint mousey
, int *iSteps
, myint
*o1
, myint
*o2
, myint
*n1
, myint
*n2
);
156 void Start(); // initializes the data structure
159 /// Make a child of the root into a new root
162 void Make_root(CDasherNode
*whichchild
);
165 /// Clear the queue of old roots - used when those nodes become
166 /// invalid, eg during changes to conrol mode
169 void ClearRootQueue();
172 /// A version of Make_root which is suitable for arbitrary
173 /// descendents of the root, not just immediate children.
176 void RecursiveMakeRoot(CDasherNode
*pNewRoot
);
179 /// Rebuild the data structure such that a given node is guaranteed
180 /// to be in the same place on the screen. This would usually be the
181 /// node under the crosshair
184 void RebuildAroundNode(CDasherNode
*pNode
);
186 void Reparent_root(int lower
, int upper
); // change back to the previous root
197 // myint PlotGoTo(myint MouseX, myint MouseY);
199 void EnterText(CLanguageModel::Context Context
, std::string TheText
) const;
200 void LearnText(CLanguageModel::Context Context
, std::string
* TheText
, bool IsMore
);
202 CLanguageModel::Context
CreateEmptyContext()const;
204 // Alphabet pass-through functions for widely needed information
205 symbol
GetSpaceSymbol() const {
206 return m_pcAlphabet
->GetSpaceSymbol();
209 symbol
GetControlSymbol() const {
210 return m_pcAlphabet
->GetControlSymbol();
213 symbol
GetStartConversionSymbol() const {
214 return m_pcAlphabet
->GetStartConversionSymbol();
218 const std::string
& GetDisplayText(int iSymbol
) const {
219 return m_pcAlphabet
->GetDisplayText(iSymbol
);
222 const CAlphabet
& GetAlphabet() const {
223 return *m_pcAlphabet
;
226 CAlphabet
*GetAlphabetNew() const {
230 CDasherNode
*Get_node_under_crosshair(); // Needed for Game Mode
232 CDasherNode
*GetRoot( int iType
, CDasherNode
*pParent
, int iLower
, int iUpper
, void *pUserData
) {
235 return m_pAlphabetManagerFactory
->GetRoot(pParent
, iLower
, iUpper
, pUserData
);
237 return m_pControlManagerFactory
->GetRoot(pParent
, iLower
, iUpper
, pUserData
);
240 return m_pConversionManagerFactory
->GetRoot(pParent
, iLower
, iUpper
, pUserData
);
245 // FIXME - only public temporarily
246 void GetProbs(CLanguageModel::Context context
, std::vector
< symbol
> &NewSymbols
, std::vector
< unsigned int >&Probs
, int iNorm
) const;
247 int GetColour(symbol s
) const;
250 // Control mode stuff
252 void RegisterNode( int iID
, const std::string
&strLabel
, int iColour
) {
253 m_pControlManagerFactory
->RegisterNode(iID
, strLabel
, iColour
);
256 void ConnectNode(int iChild
, int iParent
, int iAfter
) {
257 m_pControlManagerFactory
->ConnectNode(iChild
, iParent
, iAfter
);
260 void DisconnectNode(int iChild
, int iParent
) {
261 m_pControlManagerFactory
->DisconnectNode(iChild
, iParent
);
264 void Push_Node(CDasherNode
* pNode
); // give birth to children
266 bool m_bContextSensitive
;
268 std::string m_strContextBuffer
;
270 bool RenderToView(CDasherView
*pView
, bool bRedrawDisplay
);
272 bool CheckForNewRoot(CDasherView
*pView
);
274 void ScheduleZoom(int dasherx
, int dashery
);
276 void HandleOutput(CDasherNode
*pNewNode
, CDasherNode
*pOldNode
, Dasher::VECTOR_SYMBOL_PROB
* pAdded
, int* pNumDeleted
);
278 int ScheduledSteps() {
279 return m_deGotoQueue
.size();
282 // Apply an offset to the 'target' coordinates - implements the jumps in
283 // two button dynamic mode.
284 void Offset(int iOffset
);
286 // Reset the 'target' root coordinates to match those currently visible.
287 // Appropriate for abrubt changes in behaviour (such as backing off in
289 void MatchTarget(bool bReverse
);
291 // This is pretty horrible - a rethink of the start/reset mechanism
292 // is definitely in order
293 void LimitRoot(int iMaxWidth
);
298 CDasherInterfaceBase
* m_pDasherInterface
;
300 /////////////////////////////////////////////////////////////////////////////
304 CLanguageModel
*m_pLanguageModel
; // pointer to the language model
306 CAlphabet
*m_pcAlphabet
; // pointer to the alphabet
308 CLanguageModel::Context LearnContext
; // Used to add data to model as it is entered
310 /////////////////////////////////////////////////////////////////////////////
315 std::deque
< CDasherNode
* >oldroots
;
317 // Rootmin and Rootmax specify the position of the root node in Dasher coords
318 myint m_Rootmin
, m_Rootmax
;
323 myint m_iTargetOffset
; // Displayed rootmin/max - actual rootmin/rootmax
325 myint m_Rootmin_min
, m_Rootmax_max
;
327 // The active interval over which Dasher nodes are maintained - this is most likely bigger than (0,DasherY)
330 CFrameRate m_fr
; // keep track of framerate
332 double m_dTotalNats
; // Information entered so far
334 // the probability that gets added to every symbol
341 CDasherNode
*Get_node_under_mouse(myint smousex
, myint smousey
);
343 void Get_new_root_coords(myint mousex
, myint mousey
, myint
&iNewMin
, myint
&iNewMax
, unsigned long iTime
);
345 // void Get_new_goto_coords(double zoomfactor, myint mousey);
347 void Get_string_under_mouse(const myint smousex
, const myint smousey
, std::vector
< symbol
> &str
);
349 double CorrectionFactor(int dasherx
, int dashery
);
351 void Recursive_Push_Node(CDasherNode
* pNode
, int depth
);
353 // ControlTree *m_pControltree;
355 CAlphabetManagerFactory
*m_pAlphabetManagerFactory
;
356 CControlManagerFactory
*m_pControlManagerFactory
;
360 unsigned long m_iStartTime
;
363 CConversionManagerFactory
*m_pConversionManagerFactory
;
372 std::deque
<SGotoItem
> m_deGotoQueue
;
374 // Whether characters entered by alphabet manager are expected to
375 // require conversion.
376 // TODO: Need to rethink this at some point.
377 bool m_bRequireConversion
;
379 friend class CTrainer
;
380 friend class CDasherNode
;
384 /////////////////////////////////////////////////////////////////////////////
386 inline CLanguageModel::Context
CDasherModel::CreateEmptyContext() const {
387 return m_pLanguageModel
->CreateEmptyContext();
390 inline int CDasherModel::GetColour(symbol s
) const {
391 return m_pcAlphabet
->GetColour(s
);
394 #endif /* #ifndef __DasherModel_h__ */