File: Events.cc

package info (click to toggle)
hatman 0.5.0.1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,420 kB
  • ctags: 1,972
  • sloc: cpp: 7,873; sh: 330; yacc: 322; ansic: 260; lex: 210; makefile: 109; awk: 80
file content (172 lines) | stat: -rw-r--r-- 4,799 bytes parent folder | download | duplicates (2)
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
/*C*
 *
 * Hatman - The Game of Kings
 * Copyright (C) 1997 James Pharaoh & Timothy Fisken
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *C*/

#include "CtrlEvent.h"
#include "Events.h"
#include "../gl/Console.h"
#include "../gl/Keyboard.h"
#include "../gl/Mouse.h"
#include "../gl/Sprite.h"
#include "../gl/VgaBlur.h"

//--------------------------------------------------------------------------------------------------------------------------------

Events::Events(Collection<Control>* _controls)
 : Collection<CtrlEvent>()
{
 controls = _controls;
}

//--------------------------------------------------------------------------------------------------------------------------------

int Events::operator () ()
{
 assert(mouse->sprite);

 // set up a few things (well, one)

 screen->clipping = true;

 // before we start, draw all controls

 foreach(*controls, i)
  {
   Control* control = controls->at(i);
   screen->clippingRect = control->rect();
   control->draw(control->rect());
  }
 screen->keepAndUpdate();

 Control* mouseOwner = NULL; // set when you press the left mouse button
 quitFlag = false;
 while(1)
  {
   
   // grab mouse events

   MouseEvent* me;
   while((me = mouse->getEvent()))
    {
     // cast the event
     MouseLBUpEvent* mlue = dynamic_cast<MouseLBUpEvent*>(me);
     MouseMoveEvent* mme = dynamic_cast<MouseMoveEvent*>(me);

     // proccess each control, from last (highest) to first
     for(int i = controls->length()-1; i >= 0; i--)
      {
       Control* control = controls->at(i);
       if(mouseOwner? mouseOwner != control : !control->rect().contains(me->p)) continue;

       if(MouseLBDownEvent* mlbde = dynamic_cast<MouseLBDownEvent*>(me))
	{
	 add(new LBDownCtrlEvent(control, mlbde->p));
	 mouseOwner = control; // this will finally be set to the highest control
	}

       if(mlue) // left button released
	{
	 add(new LBUpCtrlEvent(control, mlue->p));
	 mouseOwner = NULL;
	}

       if(MouseRBDownEvent* mrbde = dynamic_cast<MouseRBDownEvent*>(me))
	add(new RBDownCtrlEvent(control, mrbde->p));

       if(mme) // mouse moved
	add(new PtrMoveCtrlEvent(control, mme->p));

       MouseDbClickEvent* mdce = dynamic_cast<MouseDbClickEvent*>(me);
       if(mdce) control->evDbClick(mdce->p);

       break;
      }

     delete me;
     if(quitFlag) goto quit;
    }
   
   // grab keyboard events

   KeyboardEvent* ke;
   while((ke = keyboard->getEvent()))
    {
     KeyDownEvent* kde = dynamic_cast<KeyDownEvent*>(ke);
     if(kde) foreach(*controls, i) controls->at(i)->evKeyDown(kde->key);

     KeyUpEvent* kue = dynamic_cast<KeyUpEvent*>(ke);
     if(kue) foreach(*controls, i) controls->at(i)->evKeyUp(kue->key);

     KeyRepEvent* kre = dynamic_cast<KeyRepEvent*>(ke);
     if(kre) foreach(*controls, i) controls->at(i)->evKeyRep(kre->key);
    }

   // process events till none are left
   
   int loops = 0;
   while(length())
    {
     foreach(*this, i)
      {
       (*at(i))();
       del(i);
       if(quitFlag) goto quit;
      }
     pack();
     if(++loops == 100) fatal("events looped 100 times, now that can't be good\n");
    }
   
   screen->clippingRect = screenRect;
   screen->blur2(mouse->sprite->draw(screen, mouse->position()));
   screen->copyBlurs();
  }

quit:
 VPRINTF("<control> events finished ret=%d\n", ret);
 return ret;
}

//--------------------------------------------------------------------------------------------------------------------------------

void Events::quit(int _ret)
{
 VPRINTF("<control> quit requested ret=%d\n", _ret);
 quitFlag = true;
 ret = _ret;
}

//--------------------------------------------------------------------------------------------------------------------------------

void Events::redraw(Rect r)
{
 foreach(*controls, i)
  {
   Control* control = controls->at(i);
   if(control->rect().meets(r))
    {
     screen->clippingRect = control->rect();
     screen->clippingRect.clipTo(r);
     control->draw(screen->clippingRect);
    }
  }
 screen->keepAndBlur1(r);
}

//--------------------------------------------------------------------------------------------------------------------------------