File: ui_cmn.cpp

package info (click to toggle)
xevil 2.02r2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,964 kB
  • ctags: 8,002
  • sloc: cpp: 47,780; makefile: 220; csh: 4
file content (166 lines) | stat: -rw-r--r-- 5,102 bytes parent folder | download | duplicates (10)
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
/* 
 * XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
 * http://www.xevil.com
 * satan@xevil.com
 *
 * 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, the file "gpl.txt"; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA, or visit http://www.gnu.org.
 */

// "ui_cmn.cpp"

#if X11
#ifndef NO_PRAGMAS
#pragma implementation "ui_cmn.h"
#endif
#endif

#include "stdafx.h"
#include "utils.h"
#include "ui_cmn.h"



KeyDispatcher::KeyDispatcher() {
  weaponKeyDown = False;
  weaponCommandDefault = IT_CENTER;
  dispatchCalled = False;
}



void KeyDispatcher::clock(IKeyState* keyState,IDispatcher* dispatcher,
                          void* closure) {
  ITcommand command = IT_NO_COMMAND;
  
  // Weirdness for firing weapons.
  // If user holds down WEAPON_CENTER and presses a direction, 
  // a shot is fired in that direction.    
  // If user presses and releases WEAPON_CENTER, a shot is fired
  // in the direction the user most recently moved or fired.
  if (keyState->key_down(IT_WEAPON_CENTER,closure)) {
    if (!weaponKeyDown) {
      weaponKeyDown = True;
      dispatchCalled = False;
    }
  }
  if (!keyState->key_down(IT_WEAPON_CENTER,closure) &&
      weaponKeyDown == True) {
    // Fire weapon in last direction if weapon_center was pressed and 
    // released without sending any other command.
    if (!dispatchCalled) {
      command = weaponCommandDefault;
    }
    weaponKeyDown = False;
  }
  
  
  // Movement
  if (keyState->key_down(IT_R,closure)) {
    command = weaponKeyDown ? IT_WEAPON_R : IT_R;
    weaponCommandDefault = IT_WEAPON_R;
  }
  if (keyState->key_down(IT_L,closure)) {
    if (command == IT_WEAPON_R || command == IT_R) {
      // If left and right pressed, consider it to be center.
      command = IT_CENTER;
      // Does not effect weaponCommandDefault.
    }
    else {
      command = weaponKeyDown ? IT_WEAPON_L : IT_L;
      weaponCommandDefault = IT_WEAPON_L;
    }
  }
  if (keyState->key_down(IT_DN,closure)) {
    if (keyState->key_down(IT_UP,closure)) {
      // If up and down pressed, do nothing
    }
    if ((command == IT_R)||(command == IT_WEAPON_R)) {
      command = weaponKeyDown ? IT_WEAPON_DN_R : IT_DN_R;
      weaponCommandDefault = IT_WEAPON_DN_R;
    }
    else if ((command == IT_L)||(command == IT_WEAPON_L)) {
      command = weaponKeyDown ? IT_WEAPON_DN_L : IT_DN_L;
      weaponCommandDefault = IT_WEAPON_DN_L;
    }
    else {
      command = weaponKeyDown ? IT_WEAPON_DN : IT_DN;
      weaponCommandDefault = IT_WEAPON_DN;
    }
  }
  else if (keyState->key_down(IT_UP,closure)) {
    if ((command == IT_R)||(command == IT_WEAPON_R)) {
      command = weaponKeyDown ? IT_WEAPON_UP_R : IT_UP_R;
      weaponCommandDefault = IT_WEAPON_UP_R;
    }
    else if ((command == IT_L)||(command == IT_WEAPON_L)) {
      command = weaponKeyDown ? IT_WEAPON_UP_L : IT_UP_L;
      weaponCommandDefault = IT_WEAPON_UP_L;
    }
    else {
      command = weaponKeyDown ? IT_WEAPON_UP : IT_UP;
      weaponCommandDefault = IT_WEAPON_UP;
    }
  }
  
  if (keyState->key_down(IT_CENTER,closure)) {  
    command = IT_CENTER;
    // Does not effect weaponCommandDefault.
  }
  if (keyState->key_down(IT_UP_R,closure)) {
    command = weaponKeyDown ? IT_WEAPON_UP_R : IT_UP_R;
    weaponCommandDefault = IT_WEAPON_UP_R;
  }
  if (keyState->key_down(IT_UP_L,closure)) {
    command = weaponKeyDown ? IT_WEAPON_UP_L : IT_UP_L;
    weaponCommandDefault = IT_WEAPON_UP_L;
  }
  if (keyState->key_down(IT_DN_L,closure)) {
    command = weaponKeyDown ? IT_WEAPON_DN_L : IT_DN_L;
    weaponCommandDefault = IT_WEAPON_DN_L;
  }
  if (keyState->key_down(IT_DN_R,closure)) {
    command = weaponKeyDown ? IT_WEAPON_DN_R : IT_DN_R;
    weaponCommandDefault = IT_WEAPON_DN_R;
  }
  
  
  // Rest of weapons and items.
  if (keyState->key_down(IT_WEAPON_CHANGE,closure)) {
    command = IT_WEAPON_CHANGE;
  }
  else if (keyState->key_down(IT_WEAPON_DROP,closure)) {
    command = IT_WEAPON_DROP;
  }
  else if (keyState->key_down(IT_ITEM_USE,closure)) {
    command = IT_ITEM_USE;
  }
  else if (keyState->key_down(IT_ITEM_CHANGE,closure)) {
    command = IT_ITEM_CHANGE;
  }
  else if (keyState->key_down(IT_ITEM_DROP,closure)) {
    command = IT_ITEM_DROP;
  }
  else if (keyState->key_down(IT_CHAT,closure)) {
    command = IT_CHAT;
  }
  
  
  // Only call dispatch() if we have an interesting command.
  if (command != IT_NO_COMMAND) {
    dispatcher->dispatch(command,closure);
    dispatchCalled = True;
  }	  
}