summaryrefslogtreecommitdiff
path: root/inputmethods/dasher/DasherModel.cpp
blob: f15fa4992a3bb2337fe1cf5b8e4f4b5327deaa39 (plain)
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
// DasherModel.h
//
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2001-2002 David Ward
//
/////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include "DasherModel.h"

using namespace Dasher;
using namespace std;

//////////////////////////////////////////////////////////////////////
// CDasherModel
//////////////////////////////////////////////////////////////////////

CDasherModel::CDasherModel(CDashEditbox* Editbox, CLanguageModel* LanguageModel, bool Dimensions)
  : m_Dimensions(Dimensions), m_editbox(Editbox), m_languagemodel(LanguageModel), m_Root(0)
{
	LearnContext = m_languagemodel->GetRootNodeContext();
	
	// various settings
	int iShift = 12;
	m_DasherY = 1<<iShift;
	m_DasherOY = m_DasherY/2;
	m_DasherOX = m_DasherY/2;
	m_dAddProb = 0.003;
}


CDasherModel::~CDasherModel()
{
	m_languagemodel->ReleaseNodeContext(LearnContext);
	delete m_Root;  // which will also delete all the whole structure
}


void CDasherModel::Make_root(int whichchild)
 // find a new root node 
{
	symbol t=m_Root->Symbol();
	if (t) {
		m_editbox->output(t);
		m_languagemodel->LearnNodeSymbol(LearnContext, t);
	}

	CDasherNode * oldroot=m_Root;
	
	CDasherNode **children=m_Root->Children();
	m_Root=children[whichchild];
	//	oldroot->Children()[whichchild]=0;  // null the pointer so we don't delete the whole tree
	//	delete oldroot;
	
	oldroots.push_back(oldroot);

	myint range=m_Rootmax-m_Rootmin;
	m_Rootmax=m_Rootmin+(range*m_Root->Hbnd())/Normalization();
	m_Rootmin+=(range*m_Root->Lbnd())/Normalization();
}

void CDasherModel::Reparent_root(int lower, int upper)
{
  /* Change the root node to the parent of the existing node
     We need to recalculate the coordinates for the "new" root as the 
     user may have moved around within the current root */

  /* Determine how zoomed in we are */
	float scalefactor=(m_Rootmax-m_Rootmin)/(upper-lower);

	m_Rootmax=int(m_Rootmax+((1024-upper)*scalefactor));
	m_Rootmin=int(m_Rootmin-(lower*scalefactor));

	m_editbox->deletetext();

	m_Root=oldroots.back();
	oldroots.pop_back();
}

/////////////////////////////////////////////////////////////////////////////

CDasherNode * CDasherModel::Get_node_under_crosshair()
{
	return m_Root->Get_node_under(Normalization(),m_Rootmin,m_Rootmax,m_DasherOX,m_DasherOY);
}

/////////////////////////////////////////////////////////////////////////////


CDasherNode * CDasherModel::Get_node_under_mouse(myint Mousex,myint Mousey)
{
	return m_Root->Get_node_under(Normalization(),m_Rootmin,m_Rootmax,Mousex,Mousey);
}

/////////////////////////////////////////////////////////////////////////////


void CDasherModel::Get_string_under_mouse(const myint Mousex,const myint Mousey, vector<symbol> &str)
{
	m_Root->Get_string_under(Normalization(),m_Rootmin,m_Rootmax,Mousex,Mousey,str);
	return;
}

/////////////////////////////////////////////////////////////////////////////


void CDasherModel::Flush(const myint ,const myint )
{
	vector<symbol> vtUnder;
	Get_string_under_mouse(m_DasherOX,m_DasherOY,vtUnder);
	unsigned int i;
	for (i=0;i<vtUnder.size();i++) {
	  if (vtUnder[i]==0)
	    continue;
	  m_editbox->flush(vtUnder[i]);
	}
}

/////////////////////////////////////////////////////////////////////////////

void CDasherModel::Update(CDasherNode *node,CDasherNode *under_mouse,int iSafe)
// go through the Dasher nodes, delete ones who have expired
// decrease the time left for nodes which arent safe
// safe nodes are those which are under the mouse or offspring of this node
{
//	if (node->pushme )
//		node->push_node();
	if (node==under_mouse)
		iSafe=1;
	if (!iSafe)
		node->Age();
//	dchar debug[256];
//	wsprintf(debug,TEXT("node->Age %d %f\n"),node->Age, fr.framerate());
//	OutputDebugString(debug);
	
	
	if (node->Age() > Framerate())
		node->Kill();
	
	
	if (node->Alive()) {
		CDasherNode **children=node->Children();
		if (children) {
			unsigned int i;
			for (i=1;i<node->Chars();i++)
				Update(children[i],under_mouse,iSafe);
		}
	}
	return;
}

/////////////////////////////////////////////////////////////////////////////

void CDasherModel::Start()
{
	m_Rootmin=0;
	m_Rootmax=m_DasherY;
	
	delete m_Root;
	CLanguageModel::CNodeContext* therootcontext=m_languagemodel->GetRootNodeContext();
	
	//Rootparent=new DasherNode(0,0,0,therootcontext,0,0,0,Normalization(),languagemodel);	
	if (m_editbox) {
		m_editbox->set_flushed(0);
		string ContextString;
		m_editbox->get_new_context(ContextString,5);
		if (ContextString.size() != 0) {
		  m_languagemodel->EnterText(therootcontext, ContextString);
		}
		m_languagemodel->ReleaseNodeContext(LearnContext);
		LearnContext = m_languagemodel->CloneNodeContext(therootcontext);
	}
	m_Root=new CDasherNode(0,0,0,0,Opts::Nodes1,0,Normalization(),m_languagemodel);
	m_Root->Push_Node(therootcontext);
	
	m_languagemodel->ReleaseNodeContext(therootcontext);
//	ppmmodel->dump();
//	dump();
	
}

/////////////////////////////////////////////////////////////////////////////

void CDasherModel::Get_new_root_coords(myint Mousex,myint Mousey)
{
	// int cappedrate=0;
	double dRx=1.0,dRxnew=1.0;
	double dRxnew2;

	int iSteps=m_fr.Steps();

	if (Mousex<m_DasherOX) {
	//	rx=1.0001*Ixmap[mx]/Ixmap[cx];
		if (Mousex<=0)
			Mousex=1;
		dRx=1.0*m_DasherOX/Mousex;
		dRxnew=pow(dRx,1.0/iSteps);  // or exp(log(rx)/steps) - i think the replacement is faster   
	
		dRxnew2=1+(dRx-1)/iSteps;
		//+(rx-1)*(rx-1)*(1.0/fr.steps()-1.0)/2/fr.steps();
		

		const double dRxmax=m_fr.Rxmax();
		if (dRxnew>dRxmax)
		 dRxnew=dRxmax;
		//		cappedrate=1;
	} else {
		if (Mousex==m_DasherOX)
			Mousex++;
	//		OutputDebugString(TEXT("zoom out\n"));
		dRx=1.0001*m_DasherOX/Mousex;
		dRxnew=exp(log(dRx)/iSteps);
	//	get_coords(root->lbnd,root->hbnd,&x1,&y1,&y2);
		//if (x1>0 || y1>0 || y2<CanvasY)
		//go_back_a_char();
		if (m_Rootmax<m_DasherY && m_Rootmin>0)
			return;
	} 
//	dchar debug[256];
//	_stprintf(debug,TEXT("rx %f rxnew %f approx %f\n"),rx,rxnew,rxnew2);
//	OutputDebugString(debug);
	//wsprintf(debug,TEXT("rx %f rxnew %f\n"),rx,rxnew);
	//OutputDebugString(debug);
	myint above=(Mousey-m_Rootmin);//*(1-rxnew)/(1-rx);
	myint below=(m_Rootmax-Mousey);//*(1-rxnew)/(1-rx);

//	wsprintf(debug,TEXT("above %I64d below %I64d \n"),above,below);
//	OutputDebugString(debug);

	myint miDistance=m_DasherY/2-Mousey;
	miDistance=myint(miDistance*(dRxnew-1)/(dRx-1));
	myint miNewrootzoom=Mousey+miDistance;

	myint newRootmax=miNewrootzoom+myint(below*dRxnew);
	myint newRootmin=miNewrootzoom-myint(above*dRxnew);
	if (newRootmin<m_DasherY/2 && newRootmax>m_DasherY/2 && newRootmax<LLONG_MAX && newRootmin>LLONG_MIN) {
		m_Rootmax=newRootmax;
		m_Rootmin=newRootmin;	
	}

}

/////////////////////////////////////////////////////////////////////////////

void CDasherModel::Tap_on_display(myint miMousex,myint miMousey, unsigned long ) 
	// work out the next viewpoint, opens some new nodes
{
	// works out next viewpoint
	Get_new_root_coords(miMousex,miMousey);

	// opens up new nodes

	// push node under mouse
	CDasherNode *under_mouse=Get_node_under_mouse(miMousex,miMousey);
	under_mouse->Push_Node();


	if (Framerate() > 4) {
		// push node under mouse but with x coord on RHS
		CDasherNode *right=Get_node_under_mouse(50,miMousey);
		right->Push_Node();
	}

	if (Framerate() > 8) {
		// push node under the crosshair
		CDasherNode *under_cross=Get_node_under_crosshair();
		under_cross->Push_Node();
	}

	unsigned int iRandom;
#if defined(_WIN32_WCE)
	iRandom=Random();
#else
	iRandom=rand();
#endif
	if (Framerate() > 8) {
		// add some noise and push another node
		CDasherNode *right=Get_node_under_mouse(50,miMousey+iRandom%500-250);
		right->Push_Node();
	}
#if defined(_WIN32_WCE)
	iRandom=Random();
#else
	iRandom=rand();
#endif
	if (Framerate() > 15) {
		// add some noise and push another node
		CDasherNode *right=Get_node_under_mouse(50,miMousey+iRandom%500-250);
		right->Push_Node();
	}

	// only do this is Dasher is flying
	if (Framerate() > 30) {
		for (int i=1;i<int(Framerate()-30)/3;i++) {
#if defined(_WIN32_WCE)	
		iRandom=Random();
#else
		iRandom=rand();
#endif
		// push at a random node on the RHS
		CDasherNode *right=Get_node_under_mouse(50,miMousey+iRandom%1000-500);
		right->Push_Node();
	
		}
	}
	Update(m_Root,under_mouse,0);


}

/////////////////////////////////////////////////////////////////////////////

void CDasherModel::Dump() const 
	// diagnostic dump
{
		// OutputDebugString(TEXT(" ptr   symbol   context Next  Child    pushme pushed cscheme   lbnd  hbnd \n"));
		m_Root->Dump_node();
}