PageRenderTime 47ms CodeModel.GetById 33ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/cui/application.d

http://github.com/wilkie/djehuty
D | 271 lines | 241 code | 28 blank | 2 comment | 65 complexity | 26194353b787019d378e8ac2e2453141 MD5 | raw file
  1module cui.application;
  2
  3import cui.window;
  4
  5import core.application;
  6import core.string;
  7import core.event;
  8import core.main;
  9import core.definitions;
 10
 11import scaffold.cui;
 12
 13import platform.vars.cui;
 14
 15import io.console;
 16
 17// Description: This class represents a Text User Interface application (TUI).
 18class CuiApplication : Application {
 19public:
 20
 21	this() {
 22		CuiStart(&_pfvars);
 23		super();
 24	}
 25
 26	this(string appName) {
 27		CuiStart(&_pfvars);
 28		super(appName);
 29	}
 30
 31	override void push(Dispatcher dsp) {
 32		super.push(dsp);
 33
 34		if (cast(CuiWindow)dsp !is null) {
 35			setWindow(cast(CuiWindow)dsp);
 36		}
 37	}
 38
 39	CuiWindow window() {
 40		return _curConsoleWindow;
 41	}
 42
 43	override bool isZombie() {
 44		return (_curConsoleWindow is null);
 45	}
 46
 47protected:
 48
 49	CuiWindow _curConsoleWindow;
 50
 51    CuiPlatformVars _pfvars;
 52
 53	override void shutdown() {
 54		CuiEnd(&_pfvars);
 55	}
 56
 57	override void start() {
 58		eventLoop();
 59	}
 60
 61	override void end(uint exitCode) {
 62		_running = false;
 63	}
 64
 65private:
 66
 67	void setWindow(CuiWindow window) {
 68		_curConsoleWindow = window;
 69
 70		// Draw Window
 71		window.onInitialize();
 72	}
 73
 74	bool _running = true;
 75
 76	void eventLoop() {
 77		while(_running) {
 78			CuiEvent evt;
 79			if (_curConsoleWindow is null) {
 80				continue;
 81			}
 82
 83			CuiNextEvent(&evt, &_pfvars);
 84
 85			switch(evt.type) {
 86				case CuiEvent.Type.KeyDown:
 87					_curConsoleWindow.onKeyDown(evt.info.key);
 88					dchar chr;
 89					if (isPrintable(evt.info.key, chr)) {
 90						_curConsoleWindow.onKeyChar(chr);
 91					}
 92					break;
 93				case CuiEvent.Type.MouseDown:
 94					break;
 95				case CuiEvent.Type.MouseUp:
 96					break;
 97				case CuiEvent.Type.MouseMove:
 98					break;
 99				case CuiEvent.Type.Close:
100					this.exit(evt.aux);
101					break;
102				case CuiEvent.Type.Size:
103					_curConsoleWindow._onResize();
104					break;
105				default:
106					break;
107			}
108		}
109	}
110
111	bool _inited;
112
113	bool isPrintable(Key key, out dchar chr) {
114				if (key.ctrl || key.alt) {
115			return false;
116		}
117
118		if (key.code >= Key.A && key.code <= Key.Z) {
119			if (key.shift) {
120				chr = (key.code - Key.A) + 'A';
121			}
122			else {
123				chr = (key.code - Key.A) + 'a';
124			}
125		}
126		else if (key.code >= Key.Zero && key.code <= Key.Nine) {
127			if (key.shift) {
128				switch (key.code) {
129					case Key.Zero:
130						chr = ')';
131						break;
132					case Key.One:
133						chr = '!';
134						break;
135					case Key.Two:
136						chr = '@';
137						break;
138					case Key.Three:
139						chr = '#';
140						break;
141					case Key.Four:
142						chr = '$';
143						break;
144					case Key.Five:
145						chr = '%';
146						break;
147					case Key.Six:
148						chr = '^';
149						break;
150					case Key.Seven:
151						chr = '&';
152						break;
153					case Key.Eight:
154						chr = '*';
155						break;
156					case Key.Nine:
157						chr = '(';
158						break;
159					default:
160						return false;
161				}
162			}
163			else {
164				chr = (key.code - Key.Zero) + '0';
165			}
166		}
167		else if (key.code == Key.SingleQuote) {
168			if (key.shift) {
169				chr = '~';
170			}
171			else {
172				chr = '`';
173			}
174		}
175		else if (key.code == Key.Minus) {
176			if (key.shift) {
177				chr = '_';
178			}
179			else {
180				chr = '-';
181			}
182		}
183		else if (key.code == Key.Equals) {
184			if (key.shift) {
185				chr = '+';
186			}
187			else {
188				chr = '=';
189			}
190		}
191		else if (key.code == Key.LeftBracket) {
192			if (key.shift) {
193				chr = '{';
194			}
195			else {
196				chr = '[';
197			}
198		}
199		else if (key.code == Key.RightBracket) {
200			if (key.shift) {
201				chr = '}';
202			}
203			else {
204				chr = '}';
205			}
206		}
207		else if (key.code == Key.Semicolon) {
208			if (key.shift) {
209				chr = ':';
210			}
211			else {
212				chr = ';';
213			}
214		}
215		else if (key.code == Key.Comma) {
216			if (key.shift) {
217				chr = '<';
218			}
219			else {
220				chr = ',';
221			}
222		}
223		else if (key.code == Key.Period) {
224			if (key.shift) {
225				chr = '>';
226			}
227			else {
228				chr = '.';
229			}
230		}
231		else if (key.code == Key.Foreslash) {
232			if (key.shift) {
233				chr = '?';
234			}
235			else {
236				chr = '/';
237			}
238		}
239		else if (key.code == Key.Backslash) {
240			if (key.shift) {
241				chr = '|';
242			}
243			else {
244				chr = '\\';
245			}
246		}
247		else if (key.code == Key.Quote) {
248			if (key.shift) {
249				chr = '"';
250			}
251			else {
252				chr = '\'';
253			}
254		}
255		else if (key.code == Key.Tab && !key.shift) {
256			chr = '\t';
257		}
258		else if (key.code == Key.Space) {
259			chr = ' ';
260		}
261		else if (key.code == Key.Return && !key.shift) {
262			chr = '\r';
263		}
264		else {
265			return false;
266		}
267
268		return true;
269
270	}
271}