Checking in half finished context stuff. Probably not the best time to
[dasher.git] / Src / DasherCore / DasherInterfaceBase.h
blob6cc89ab398b52022be08a2c6fb50c15920980809
1 // DasherInterfaceBase.h
2 //
3 // Copyright (c) 2002 Iain Murray
5 #ifndef __DasherInterfaceBase_h__
6 #define __DasherInterfaceBase_h__
8 // TODO - there is a list of things to be configurable in my notes
9 // Check that everything that is not self-contained within the GUI is covered.
11 #include "../Common/NoClones.h"
12 #include "../Common/ModuleSettings.h"
13 #include "Alphabet/Alphabet.h"
14 #include "Alphabet/AlphIO.h"
15 #include "ColourIO.h"
16 #include "ModuleManager.h"
17 #include "ActionButton.h"
19 #include "AutoSpeedControl.h"
21 #include "InputFilter.h"
23 namespace Dasher {
24 class CDasherScreen;
25 class CDasherView;
26 class CDasherInput;
27 class CDasherModel;
28 class CEventHandler;
29 class CEvent;
32 class Dasher::CDasherScreen;
33 class Dasher::CDasherView;
34 class Dasher::CDasherInput;
35 class Dasher::CDasherModel;
36 class Dasher::CEventHandler;
37 class Dasher::CEvent;
38 class CSettingsStore;
39 class CUserLogBase;
40 class CDasherButtons;
41 class CNodeCreationManager;
43 #include <map>
44 #include <algorithm>
46 namespace Dasher {
47 class CDasherInterfaceBase;
49 /// The central class in the core of Dasher. Ties together the rest of
50 /// the platform independent stuff and provides a single interface for
51 /// the UI to use.
53 class Dasher::CDasherInterfaceBase:private NoClones
55 public:
56 CDasherInterfaceBase();
57 virtual ~ CDasherInterfaceBase();
59 /// @name Access to internal member classes
60 /// Access various classes contained within the interface. These
61 /// should be considered dangerous and use minimised. Eventually to
62 /// be replaced by properly encapsulated equivalents.
63 /// @{
65 ///
66 /// Return a pointer to the current EventHandler (the one
67 /// which the CSettingsStore is using to notify parameter
68 /// changes)
69 ///
71 virtual CEventHandler *GetEventHandler() {
72 return m_pEventHandler;
75 ///
76 /// \deprecated In situ alphabet editing is no longer supported
77 /// \todo Document this
78 ///
80 const CAlphIO::AlphInfo & GetInfo(const std::string & AlphID);
82 /// \todo Document this
84 void SetInfo(const CAlphIO::AlphInfo & NewInfo);
86 /// \todo Document this
88 void DeleteAlphabet(const std::string & AlphID);
90 /// Get a pointer to the current alphabet object
92 CAlphabet *GetAlphabet() {
93 return m_Alphabet;
96 /// Gets a pointer to the object doing user logging
98 CUserLogBase* GetUserLogPtr();
100 // @}
103 /// @name Parameter manipulation
104 /// Members for manipulating the parameters of the core Dasher object.
107 //@{
110 /// Set a boolean parameter.
111 /// \param iParameter The parameter to set.
112 /// \param bValue The new value.
115 void SetBoolParameter(int iParameter, bool bValue);
118 /// Set a long integer parameter.
119 /// \param iParameter The parameter to set.
120 /// \param lValue The new value.
123 void SetLongParameter(int iParameter, long lValue);
126 /// Set a string parameter.
127 /// \param iParameter The parameter to set.
128 /// \param sValue The new value.
131 void SetStringParameter(int iParameter, const std::string & sValue);
133 /// Get a boolean parameter
134 /// \param iParameter The parameter to get.
135 /// \retval The current value.
137 bool GetBoolParameter(int iParameter);
139 /// Get a long integer parameter
140 /// \param iParameter The parameter to get.
141 /// \retval The current value.
143 long GetLongParameter(int iParameter);
145 /// Get a string parameter
146 /// \param iParameter The parameter to get.
147 /// \retval The current value.
149 std::string GetStringParameter(int iParameter);
152 /// Reset a parameter to the default value
155 void ResetParameter(int iParmater);
157 /// \todo Document this
159 void GetFontSizes(std::vector < int >*FontSizes) const;
162 /// Obtain the permitted values for a string parameter - used to
163 /// geneate preferences dialogues etc.
166 void GetPermittedValues(int iParameter, std::vector<std::string> &vList);
168 ///
169 /// Get a list of settings which apply to a particular module
170 ///
172 bool GetModuleSettings(const std::string &strName, SModuleSettings **pSettings, int *iCount);
175 //@}
177 /// Forward events to listeners in the SettingsUI and Editbox.
178 /// \param pEvent The event to forward.
179 /// \todo Should be protected.
181 virtual void ExternalEventHandler(Dasher::CEvent * pEvent) {};
183 /// Interface level event handler. For example, responsible for
184 /// restarting the Dasher model whenever parameter changes make it
185 /// invalid.
186 /// \param pEvent The event.
187 /// \todo Should be protected.
189 void InterfaceEventHandler(Dasher::CEvent * pEvent);
192 void PreSetNotify(int iParameter, const std::string &sValue);
195 /// Draw a new Dasher frame, regardless of whether we're paused etc.
196 /// \param iTime Current time in ms.
197 /// \todo See comments in cpp file for some functionality which needs to be re-implemented
199 void NewFrame(unsigned long iTime, bool bForceRedraw);
202 /// @name Starting and stopping
203 /// Methods used to instruct dynamic motion of Dasher to start or stop
204 /// @{
206 /// Resets the Dasher model. Doesn't actually unpause Dasher.
207 /// \deprecated Use InvalidateContext() instead
209 // void Start();
211 /// Pause Dasher
212 /// \todo Parameters are ignored (?) - remove from definition.
214 void PauseAt(int MouseX, int MouseY); // are required to make
216 /// Halt Dasher. This simply freezes Dasher but does not emit a stop event, so does not result in speech etc.
218 void Halt();
220 /// Unpause Dasher
221 /// \param Time Time in ms, used to keep a constant frame rate
223 void Unpause(unsigned long Time); // Dasher run at the
225 /// @}
228 // App Interface
229 // -----------------------------------------------------
231 // std::map<int, std::string>& GetAlphabets(); // map<key, value> int is a UID string can change. Store UID in preferences. Display string to user.
232 // std::vector<std::string>& GetAlphabets();
233 // std::vector<std::string>& GetLangModels();
234 // std::vector<std::string>& GetViews();
236 /// Supply a new CDasherScreen object to do the rendering.
237 /// \param NewScreen Pointer to the new CDasherScreen.
239 void ChangeScreen(CDasherScreen * NewScreen); // We may change the widgets Dasher uses
241 /// Train Dasher from a file
242 /// \param Filename File to load.
244 int TrainFile(std::string Filename, int iTotalBytes, int iOffset); // all training data must be in UTF-8.
246 /// Set the context in which Dasher makes predictions
247 /// \param strNewContext The new context (UTF-8)
249 void SetContext(std::string strNewContext);
251 /// New control mechanisms:
253 void SetBuffer(int iOffset);
255 void UnsetBuffer();
257 void SetOffset(int iOffset);
259 /// @name Status reporting
260 /// Get information about the runtime status of Dasher which might
261 /// be of interest for debugging purposes etc.
262 /// @{
264 /// Get the current rate of text entry.
265 /// \retval The rate in characters per minute.
266 /// TODO: Check that this is still used
268 double GetCurCPM();
270 /// Get current refresh rate.
271 /// \retval The rate in frames per second
272 /// TODO: Check that this is still used
274 double GetCurFPS();
276 /// Get the total number of nats (base-e bits) entered.
277 /// \retval The current total
278 /// \todo Obsolete since new logging code?
280 double GetNats() const;
282 /// Reset the count of nats entered.
283 /// \todo Obsolete since new logging code?
285 void ResetNats();
287 double GetFramerate();
289 int GetRenderCount();
291 /// @}
293 /// @name Control hierarchy and action buttons
294 /// Manipulate the hierarchy of commands presented in control mode etc
295 /// @{
297 void RegisterNode( int iID, const std::string &strLabel, int iColour );
299 void ConnectNode(int iChild, int iParent, int iAfter);
301 void DisconnectNode(int iChild, int iParent);
303 void ExecuteCommand(const std::string &strName);
305 void AddActionButton(const std::string &strName);
307 /// @}
309 /// @name User input
310 /// Deals with forwarding user input to the core
311 /// @{
313 void KeyDown(int iTime, int iId);
315 void KeyUp(int iTime, int iId);
317 void HandleClickUp(int iTime, int iX, int iY);
319 void HandleClickDown(int iTime, int iX, int iY);
321 /// @}
323 // Module management functions
324 void RegisterFactory(CModuleFactory *pFactory);
326 void StartShutdown();
328 void AddGameModeString(const std::string &strText) {
329 m_deGameModeStrings.push_back(strText);
330 PauseAt(0,0);
331 // CreateDasherModel();
332 CreateNCManager();
333 // Start();
336 void ScheduleRedraw() {
337 m_bRedrawScheduled = true;
340 void CheckRedraw();
342 std::string GetContext(int iStart, int iLength) {
343 return "a";
347 protected:
349 /// @name Startup
350 /// Interaction with the derived class during core startup
351 /// @{
354 /// Allocate resources, create alphabets etc. This is a separate
355 /// routine to the constructor to give us a chance to set up
356 /// parameters before things are created.
359 void Realize();
362 /// Notify the core that the UI has been realised. At this point drawing etc. is expected to work
365 void OnUIRealised();
367 /// @}
369 enum ETransition {
370 TR_MODEL_INIT = 0,
371 TR_UI_INIT,
372 TR_LOCK,
373 TR_UNLOCK,
374 TR_SHUTDOWN,
375 TR_NUM
378 enum EState {
379 ST_START = 0,
380 ST_MODEL,
381 ST_UI,
382 ST_NORMAL,
383 ST_LOCKED,
384 ST_SHUTDOWN,
385 ST_NUM,
386 ST_FORBIDDEN,
387 ST_DELAY
390 /// @name State machine functions
391 /// ...
392 /// @{
394 void ChangeState(ETransition iTransition);
396 /// @}
398 CDasherModule *GetModule(long long int iID);
399 CDasherModule *GetModuleByName(const std::string &strName);
401 CEventHandler *m_pEventHandler;
402 CSettingsStore *m_pSettingsStore;
404 private:
406 /// @name Platform dependent utility functions
407 /// These functions provide various platform dependent functions
408 /// required by the core. A derived class is created for each
409 /// supported platform which implements these.
410 // @{
412 ///
413 /// Initialise the SP_SYSTEM_LOC and SP_USER_LOC paths - the exact
414 /// method of doing this will be OS dependent
417 virtual void SetupPaths() = 0;
419 ///
420 /// Produce a list of filenames for alphabet files
423 virtual void ScanAlphabetFiles(std::vector<std::string> &vFileList) = 0;
426 /// Produce a list of filenames for colour files
429 virtual void ScanColourFiles(std::vector<std::string> &vFileList) = 0;
432 /// Set up the platform dependent UI for the widget (not the wider
433 /// app). Note that the constructor of the derived class will
434 /// probably want to return details of what was created - this will
435 /// have to happen separately, but we'll need to be careful with the
436 /// semantics.
439 virtual void SetupUI() = 0;
442 /// Create any module factories which are specific to the platform
443 /// (eg input device drivers)
446 virtual void CreateLocalFactories() = 0;
449 /// Create settings store object, which will be platform dependent
450 /// TODO: Can this not be done just by selecting which settings
451 /// store implementation to instantiate?
454 virtual void CreateSettingsStore() = 0;
457 /// Obtain the size in bytes of a file - the way to do this is
458 /// dependent on the OS (TODO: Check this - any posix on Windows?)
461 virtual int GetFileSize(const std::string &strFileName) = 0;
464 /// Start the callback timer
467 virtual void StartTimer() = 0;
470 /// Shutdown the callback timer (permenantly - this is called once
471 /// Dasher is committed to closing).
474 virtual void ShutdownTimer() = 0;
477 /// Append text to the trainign file - used to store state between
478 /// sessions
479 /// @todo Pass file path to the fuction rather than having implementations work it out themselves
482 virtual void WriteTrainFile(const std::string &strNewText) {
485 /// @}
488 /// Provide a new CDasherInput input device object.
490 void CreateInput();
492 void CreateFactories();
493 void CreateInputFilter();
495 void CreateModel(int iOffset);
496 void CreateNCManager();
498 void ChangeAlphabet();
499 void ChangeColours();
500 void ChangeView();
501 void Redraw(bool bRedrawNodes);
502 void SetupActionButtons();
503 void DestroyActionButtons();
504 void PositionActionButtons();
505 bool DrawActionButtons();
507 void LeaveState(EState iState);
508 void EnterState(EState iState);
510 void WriteTrainFileFull();
511 void WriteTrainFilePartial();
514 /// @name Lock Management
515 /// Functions for locking/unlocking the core. Note that the lock
516 /// flags parameter is currently ignored, but will be used to give
517 /// finer granularity
518 /// @{
521 /// Add a lock
524 void AddLock(int iLockFlags);
527 /// Release an existing lock. Note that these functions have minimal
528 /// error checking at the moment, so be careful.
531 void ReleaseLock(int iLockFlags);
533 /// @}
535 std::deque<std::string> m_deGameModeStrings;
537 std::vector<CActionButton *> m_vLeftButtons;
538 std::vector<CActionButton *> m_vRightButtons;
541 /// @name Child components
542 /// Various objects which are 'owned' by the core.
543 /// @{
544 CAlphabet *m_Alphabet;
545 CDasherModel *m_pDasherModel;
546 CDasherScreen *m_DasherScreen;
547 CDasherView *m_pDasherView;
548 CDasherInput *m_pInput;
549 CAlphIO *m_AlphIO;
550 CColourIO *m_ColourIO;
551 CNodeCreationManager *m_pNCManager;
552 CUserLogBase *m_pUserLog;
553 CInputFilter* m_pInputFilter;
554 CModuleManager m_oModuleManager;
555 /// @}
557 std::string strTrainfileBuffer;
558 std::string strCurrentContext;
560 /// @name State variables
561 /// Represent the current overall state of the core
562 /// @{
563 bool m_bGlobalLock; // The big lock
564 bool m_bRedrawScheduled;
565 int m_iLockCount;
566 EState m_iCurrentState;
567 bool m_bOldVisible;
568 /// @}
570 bool m_bLastChanged;
573 #endif /* #ifndef __DasherInterfaceBase_h__ */