c70cbc518884a214da1b42b83d7bee75b9550b6c
[sdk] / samples / net / eirc / console.ec
1 /****************************************************************************
2    ECERE Runtime Library
3
4    Copyright (c) 2001-2008 Jerome Jacovella-St-Louis
5    All Rights Reserved.
6
7    console.ec - Console Dialog
8 ****************************************************************************/
9 import "ecere"
10
11 #include <stdarg.h>
12
13 class Console : Window
14 {
15    EditBox log
16    {
17       this,
18       readOnly = true;
19       multiLine = true;
20       textHorzScroll = true;
21       textVertScroll = true;
22       autoEmpty = true;
23       maxNumLines = 256;
24    };
25    EditBox commandBox
26    {
27       this, textHorzScroll = true; size.h = editHeight, anchor = { left = 0, right = 0, bottom = 0 };
28    };
29    virtual bool ProcessCommand(char * command);
30
31    Bitmap bitmap;
32    OldList commands;
33    Item command;
34    bool doneLooping;
35    char currentCommand[256];
36    int movement;
37    bool moving;
38    int actualHeight;
39    double pos;
40    double lastTime;
41    int currentAlpha;
42    bool shown;
43
44    int separatorHeight;
45    int editHeight;
46    int referenceHeight;
47    float speed;
48    int alpha;
49    char * version;
50    Point verLROffset;
51    Color logTextColor;
52    char * bmpFile;
53    Color editBackColor;
54    Color editTextColor;
55    alpha = 255;
56
57    Timer timer
58    {
59       this, delay = 0.05;
60
61       bool DelayExpired()
62       {
63          double realTime, diffTime;
64
65          realTime = GetTime();
66          diffTime = realTime - lastTime;
67
68          /* CONSOLE MOVEMENT */
69          if(shown && moving)
70          {
71             int movement = (int)(diffTime * (movement * speed));
72
73             if(movement)
74             {
75                pos += movement;
76                pos = Max(pos, -size.h);
77                pos = Min(pos, 0);
78                if(pos == -size.h && movement == -1)
79                {
80                   // eWindow_Hide(window, true, false);
81                   moving = false;
82                   shown = false;
83                }
84                else if(moving)
85                   Move(position.x, (int)pos, clientSize.w, clientSize.h);
86                if(!pos && movement == 1)
87                   moving = false;
88
89                // Set up transparency color
90                if(size.h)
91                   currentAlpha = (((int)pos + size.h) * alpha / size.h);
92
93                log.foreground = (logTextColor & 0xFFFFFF) | (currentAlpha << 24);
94                if(alpha)
95                {
96                   commandBox.foreground = (editTextColor & 0xFFFFFF) | ((currentAlpha * 255 / alpha) << 24);
97                }
98                commandBox.background = (editBackColor & 0xFFFFFF) | (currentAlpha << 24);
99                lastTime = realTime;
100             }
101          }
102          return true;
103       }
104    };
105    bool OnCreate()
106    {
107       int editHeight;
108       if(speed)
109          timer.Start();
110       commands.Clear();
111       commands.circ = true;
112       movement = 1;
113       doneLooping = true;
114       if(!referenceHeight)
115          referenceHeight = 768;
116       if(referenceHeight)
117       {
118          // h = height * 768 / referenceHeight;
119
120          // Set up transparency color
121          currentAlpha = (((int)pos + size.h) * alpha / size.h);
122       }
123       else
124          referenceHeight = 768;
125
126       editHeight = this.editHeight * 768 / referenceHeight;
127
128       log.anchor = { left = 0, top = 0, right = 0, bottom = editHeight + separatorHeight* 768/referenceHeight };
129       return true;
130    }
131
132    bool OnPostCreate()
133    {
134       log.background = 0;
135        log.foreground = (logTextColor & 0xFFFFFF) | (currentAlpha << 24);
136       if(alpha)
137          commandBox.foreground = (editTextColor & 0xFFFFFF) | ((currentAlpha * 255 / alpha) << 24);
138       commandBox.background = (editBackColor & 0xFFFFFF) | (currentAlpha << 24);
139       return true;
140    }
141
142    void OnDestroy()
143    {
144       commands.Free(null);
145    }
146
147    bool OnLoadGraphics()
148    {
149       if(bmpFile)
150          bitmap.Load(bmpFile, null, displaySystem);
151       return true;
152    }
153
154    void OnUnloadGraphics()
155    {
156       bitmap.Free();
157    }
158
159    void OnRedraw(Surface surface)
160    {
161       surface.SetForeground(white);
162       if(bitmap)
163          surface.Stretch(bitmap, 0, 0, 0, 0, clientSize.w, clientSize.h, bitmap.width, bitmap.height);
164       if(referenceHeight)
165       {
166          surface.SetForeground((logTextColor & 0xFFFFFF) | (currentAlpha << 24));
167          if(version)
168             surface.WriteTextf(
169                clientSize.w - verLROffset.x * 768 / referenceHeight,
170                clientSize.h - verLROffset.y * 768 / referenceHeight,
171                version);
172       }
173    }
174
175    bool OnKeyDown(Key key, unichar ch)
176    {
177       switch((SmartKey)key)
178       {
179          case tilde:
180             Toggle();
181             return false;
182          case enter:
183          {
184             Item string;
185             char * lineBuffer;
186             char * buffer;
187
188             // Process Command in command edit box here
189             lineBuffer = commandBox.line.text;
190
191             if(lineBuffer && lineBuffer[0])
192             {
193                string = (Item)new0 byte[sizeof(class Item) + strlen(lineBuffer) + 1];
194                if(string)
195                {
196                   commands.Add(string);
197                   buffer = (char *) ((byte *) string + sizeof(class Item));
198                   strcpy(buffer, lineBuffer);
199                   command = commands.first;
200                   doneLooping = true;
201                }
202                log.End();
203                if(log.numLines > 1 || log.line.count)
204                   log.PutCh('\n');
205                log.PutS(" > ");
206                log.PutS(lineBuffer);
207                if(ProcessCommand)
208                {
209                   if(ProcessCommand(lineBuffer))
210                      return false;
211                }
212                if(commandBox)
213                   commandBox.Clear();
214             }
215             break;
216          }
217       }
218       return true;
219    }
220
221    bool OnKeyHit(Key key, unichar ch)
222    {
223       char * lineBuffer = commandBox.line.text;
224       char * buffer;
225
226       switch((SmartKey)key)
227       {
228          case pageDown:
229             log.PageDown();
230             return true;
231          case pageUp:
232             log.PageUp();
233             return false;
234          case up:
235             if(command)
236             {
237                buffer = (char *) ((byte *) command + sizeof(class Item));
238                if(strcmp(buffer, lineBuffer))
239                {
240                   strcpy(currentCommand, lineBuffer);
241                }
242                if(command == commands.first && !doneLooping)
243                {
244                   doneLooping = true;
245                   commandBox.Clear();
246                   commandBox.PutS(currentCommand);
247                }
248                else
249                {
250                   doneLooping = false;
251                   command = command.prev;
252                   commandBox.Clear();
253                   commandBox.PutS(((byte *) command) + sizeof(class Item));
254                }
255             }
256             return false;
257          case down:
258             if(command)
259             {
260                buffer = (char *) ((byte *) command + sizeof(class Item));
261                if(strcmp(buffer, lineBuffer))
262                {
263                   strcpy(currentCommand, lineBuffer);
264                }
265                if(command == commands.last && !doneLooping)
266                {
267                   doneLooping = true;
268                   commandBox.Clear();
269                   commandBox.PutS(currentCommand);
270                }
271                else
272                {
273                   doneLooping = false;
274                   command = command.next;
275                   commandBox.Clear();
276                   commandBox.PutS(((byte *) command) + sizeof(class Item));
277                }
278             }
279             return false;
280          case tilde:
281             return false;
282    /*      case F6:
283             return false;*/
284       }
285       return true;
286    }
287
288    void Toggle()
289    {
290       if(this)
291       {
292          movement = -movement;
293          moving = true;
294          shown = true;
295          SetState(normal, true, 0);
296          commandBox.Activate();
297          lastTime = GetTime();
298       }
299    }
300
301    void Log(char * format, ...)
302    {
303       if(this)
304       {
305          char text[MAX_F_STRING];
306          va_list args;
307
308          va_start(args, format);
309          vsprintf(text, format, args);
310          log.End();
311          log.PutS(text);
312          va_end(args);
313       }
314    }
315 };