3 // Copyright (c) 2001-2005 David Ward
5 #ifndef __DasherModel_h__
6 #define __DasherModel_h__
8 #include "../Common/NoClones.h"
10 #include "LanguageModelling/LanguageModel.h"
12 #include "DasherNode.h"
13 #include "DasherComponent.h"
14 #include "Alphabet/Alphabet.h"
15 #include "AlphabetManagerFactory.h"
16 #include "ControlManagerFactory.h"
18 #include "DasherTypes.h"
19 #include "FrameRate.h"
22 #include "DasherGameMode.h"
27 class CDasherInterfaceBase
;
30 class Dasher::CDasherInterfaceBase
;
32 /// \brief Dasher 'world' data structures and dynamics.
34 /// The DasherModel represents the current state of Dasher
35 /// It contains a tree of DasherNodes
36 /// knows the current viewpoint
37 /// knows how to evolve the viewpoint
39 class Dasher::CDasherModel
:public Dasher::CDasherComponent
, private NoClones
45 CTrainer(CDasherModel
& DasherModel
);
47 void Train(const std::vector
< symbol
> &vSymbols
);
52 CLanguageModel::Context m_Context
;
53 CDasherModel
& m_DasherModel
;
65 CDasherModel(CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
, CDasherInterfaceBase
* pDashIface
);
68 bool WriteLMToFile(const std::string
&strFilename
) {
70 return m_pLanguageModel
->WriteToFile(strFilename
);
75 bool ReadLMFromFile(const std::string
&strFilename
) {
77 return m_pLanguageModel
->ReadFromFile(strFilename
);
82 void HandleEvent(Dasher::CEvent
* pEvent
);
84 CTrainer
*GetTrainer();
86 // framerate functions
87 void NewFrame(unsigned long Time
) {
89 } // called everytime we render a new frame
90 double Framerate() const {
91 return m_fr
.Framerate();
92 } // return the framerate
93 void Reset_framerate(unsigned long Time
) {
101 void RecursiveOutput(CDasherNode
*pNode
, Dasher::VECTOR_SYMBOL_PROB
* pAdded
);
103 // User control of speed
104 void SetBitrate(double TargetRate
) {
105 m_fr
.SetBitrate(TargetRate
);
106 } // Use or start at this bitrate
107 //void SetMaxBitrate(double MaxRate) {m_dMaxRate=MaxRate;m_fr.SetMaxBitrate(MaxRate);} // Cap any adaption at this rate
109 std::string
GroupLabel(int group
) const {
110 return m_pcAlphabet
->GetGroupLabel(group
);
112 int GroupColour(int group
) const {
113 return m_pcAlphabet
->GetGroupColour(group
);
115 void SetContext(std::string
& sNewContext
);
117 // functions returning private data (read only access)
118 myint
Rootmin() const {
121 myint
Rootmax() const {
124 myint
DasherOX() const {
127 myint
DasherOY() const {
130 CDasherNode
*Root() const {
133 myint
DasherY() const {
136 void OutputCharacters(CDasherNode
* node
);
137 bool DeleteCharacters(CDasherNode
* newnode
, CDasherNode
* oldnode
, int* pNumDeleted
= NULL
);
139 void Trace() const; // diagnostics
140 //void Learn_symbol(symbol Symbol) {m_languagemodel->learn_symbol(Symbol);} // feed character to language model
142 void Tap_on_display(myint
, myint
, unsigned long Time
, Dasher::VECTOR_SYMBOL_PROB
* pAdded
= NULL
, int* pNumDeleted
= NULL
); // evolves the current viewpoint
144 void GoTo(double, myint
); // jumps to a new viewpoint
145 void NewGoTo(myint n1
, myint n2
, int style
);
146 double CDasherModel::Plan_new_goto_coords(int iRxnew
, myint mousey
, int *iSteps
, myint
*o1
, myint
*o2
, myint
*n1
, myint
*n2
);
148 void Start(); // initializes the data structure
149 void Make_root(CDasherNode
*whichchild
); // find a new root node
150 void Reparent_root(int lower
, int upper
); // change back to the previous root
155 if(GetBoolParameter(BP_NUMBER_DIMENSIONS
))
157 else if(GetBoolParameter(BP_EYETRACKER_MODE
))
173 myint
PlotGoTo(myint MouseX
, myint MouseY
);
175 void NewControlTree(ControlTree
* tree
) {
176 m_pControltree
= tree
;
178 ControlTree
*GetControlTree() const {
179 return m_pControltree
;
182 CRange(myint _iMin
, myint _iMax
):iMin(_iMin
), iMax(_iMax
) {
190 void SetActive(const CRange
& range
) {
194 void EnterText(CLanguageModel::Context Context
, std::string TheText
) const;
195 void LearnText(CLanguageModel::Context Context
, std::string
* TheText
, bool IsMore
);
197 CLanguageModel::Context
CreateEmptyContext()const;
199 // Alphabet pass-through functions for widely needed information
200 symbol
GetSpaceSymbol() const {
201 return m_pcAlphabet
->GetSpaceSymbol();
203 symbol
GetControlSymbol() const {
204 return m_pcAlphabet
->GetControlSymbol();
206 const std::string
& GetDisplayText(int iSymbol
) const {
207 return m_pcAlphabet
->GetDisplayText(iSymbol
);
209 const CAlphabet
& GetAlphabet() const {
210 return *m_pcAlphabet
;
212 CDasherNode
*Get_node_under_crosshair(); // Needed for Game Mode
213 myint
GetGameModePointerLoc() {
214 return m_pGameMode
->GetDasherCoordOfTarget();
218 CDasherNode
*GetRoot( int iType
, CDasherNode
*pParent
, int iLower
, int iUpper
, void *pUserData
) {
220 return m_pAlphabetManagerFactory
->GetRoot(pParent
, iLower
, iUpper
, pUserData
);
222 return m_pControlManagerFactory
->GetRoot(pParent
, iLower
, iUpper
, pUserData
);
225 // FIXME - only public temporarily
226 void GetProbs(CLanguageModel::Context context
, std::vector
< symbol
> &NewSymbols
, std::vector
< unsigned int >&Probs
, int iNorm
) const;
227 int GetColour(symbol s
) const;
230 // Control mode stuff
232 void RegisterNode( int iID
, const std::string
&strLabel
, int iColour
) {
233 m_pControlManagerFactory
->RegisterNode(iID
, strLabel
, iColour
);
236 void ConnectNode(int iChild
, int iParent
, int iAfter
) {
237 m_pControlManagerFactory
->ConnectNode(iChild
, iParent
, iAfter
);
240 void Push_Node(CDasherNode
* pNode
); // give birth to children
242 bool m_bContextSensitive
;
244 std::string m_strContextBuffer
;
248 CDasherInterfaceBase
* m_pDasherInterface
;
250 /////////////////////////////////////////////////////////////////////////////
254 CLanguageModel
*m_pLanguageModel
; // pointer to the language model
256 const CAlphabet
*m_pcAlphabet
; // pointer to the alphabet
258 CLanguageModel::Context LearnContext
; // Used to add data to model as it is entered
260 /////////////////////////////////////////////////////////////////////////////
262 CDasherGameMode
*m_pGameMode
;
267 std::deque
< CDasherNode
* >oldroots
;
269 // Rootmin and Rootmax specify the position of the root node in Dasher coords
270 myint m_Rootmin
, m_Rootmax
;
272 myint m_Rootmin_min
, m_Rootmax_max
;
274 // Size of Dasher's arithmetic coding interval - it defines the Dasher coordinate system
277 // x position of crosshair in Dasher coords - distance from RHS is square Dasher
280 // y position of crosshair in Dasher coords - distance from top in square Dasher
283 // The active interval over which Dasher nodes are maintained - this is most likely bigger than (0,DasherY)
286 CFrameRate m_fr
; // keep track of framerate
288 double total_nats
; // Information entered so far
290 // the probability that gets added to every symbol
297 CDasherNode
*Get_node_under_mouse(myint smousex
, myint smousey
);
299 double Get_new_root_coords(myint mousex
, myint mousey
);
301 void DoZoom(myint iTargetMin
, myint iTargetMax
);
303 void Get_new_goto_coords(double zoomfactor
, myint mousey
);
305 void Get_string_under_mouse(const myint smousex
, const myint smousey
, std::vector
< symbol
> &str
);
310 void Recursive_Push_Node(CDasherNode
* pNode
, int depth
);
313 ControlTree
*m_pControltree
;
315 CAlphabetManagerFactory
*m_pAlphabetManagerFactory
;
316 CControlManagerFactory
*m_pControlManagerFactory
;
318 friend class CDasherGameMode
;
319 friend class CTrainer
;
320 friend class CDasherNode
;
324 /////////////////////////////////////////////////////////////////////////////
326 inline CLanguageModel::Context
CDasherModel::CreateEmptyContext() const {
327 return m_pLanguageModel
->CreateEmptyContext();
330 inline int CDasherModel::GetColour(symbol s
) const {
331 return m_pcAlphabet
->GetColour(s
);
334 #endif /* #ifndef __DasherModel_h__ */