Debian Codegraph @

apps/demos/drop/TsEvents.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 _______________________________________________________________________
39 ______________ S I L I C O N G R A P H I C S I N C . ____________
40 |
41 | $Revision: 1.1.1.1 $
42 |
43 | Methods : TsField::handleKeyboard
44 |
45 | Author : Dave Immel
46 |
47 ______________ S I L I C O N G R A P H I C S I N C . ____________
48 _______________________________________________________________________
49 */
50
51 //--------------------------- Include ----------------------------------
52 #include <Inventor/events/SoKeyboardEvent.h>
53 #include <Inventor/events/SoSpaceballButtonEvent.h>
54 #include <Inventor/nodes/SoEventCallback.h>
55 #include <Inventor/nodes/SoSeparator.h>
56 #include "TsField.h"
57
58 ////////////////////////////////////////////////////////////////////////
59 //
60 // Description:
61 // This routine handles all keyboard events that move the pieces as
62 // they fall.
63 //
64 // Use: private, static
65
66 void
67 TsField::handleKeyboard( void *data, SoEventCallback *node )
68
69 //
70 ////////////////////////////////////////////////////////////////////////
71 {
72 TsField *field = (TsField *)data;
73
74 if (SO_KEY_PRESS_EVENT(node->getEvent(), RETURN)) {
75 field->gameStarted = TRUE;
76 field->handleReturnKey();
77 node->setHandled();
78 return;
79 }
80
81 // If the game has been paused, don't accept any input. Just return.
82 // (Also, don't accept input if the game has not started.)
83 if (field->gamePaused || !field->gameStarted) {
84 node->setHandled();
85 return;
86 }
87
88 if (SO_KEY_PRESS_EVENT(node->getEvent(), LEFT_ARROW))
89 field->translation (TS_X_AXIS, -1);
90 else if (SO_KEY_PRESS_EVENT(node->getEvent(), RIGHT_ARROW))
91 field->translation (TS_X_AXIS, 1);
92 else if (SO_KEY_PRESS_EVENT(node->getEvent(), UP_ARROW))
93 field->translation (TS_Z_AXIS, 1);
94 else if (SO_KEY_PRESS_EVENT(node->getEvent(), DOWN_ARROW))
95 field->translation (TS_Z_AXIS, -1);
96 else if (SO_KEY_PRESS_EVENT(node->getEvent(), F))
97 field->rotation (TS_Z_AXIS, 1);
98 else if (SO_KEY_PRESS_EVENT(node->getEvent(), S))
99 field->rotation (TS_Z_AXIS, -1);
100 else if (SO_KEY_PRESS_EVENT(node->getEvent(), D))
101 field->rotation (TS_Y_AXIS, 1);
102 else if (SO_KEY_PRESS_EVENT(node->getEvent(), C))
103 field->rotation (TS_X_AXIS, -1);
104 else if (SO_KEY_PRESS_EVENT(node->getEvent(), E))
105 field->rotation (TS_X_AXIS, 1);
106 else if (SO_KEY_PRESS_EVENT(node->getEvent(), SPACE))
107 field->dropPiece();
108 else if (SO_KEY_PRESS_EVENT(node->getEvent(), H)) {
109 field->handleHelpKey();
110 }
111
112 node->setHandled();
113 }
114
115 ////////////////////////////////////////////////////////////////////////
116 //
117 // Description:
118 // This routine handles the return key press.
119 //
120 // Use: public
121
122 void
123 TsField::handleReturnKey()
124
125 //
126 ////////////////////////////////////////////////////////////////////////
127 {
128 // If help had been displayed, either start the first game
129 // or resume a game in progress.
130 if (helpDisplayed) {
131 helpDisplayed = FALSE;
132 helpRoot->removeChild(helpGroup); //old:overRoot->removeChild(helpRoot)
133 if (isGameOver)
134 startNewGame();
135 else
136 resumeGame();
137 }
138 else {
139 // Start a new game
140 startNewGame();
141 }
142 }
143
144 ////////////////////////////////////////////////////////////////////////
145 //
146 // Description:
147 // This routine handles the H key press.
148 //
149 // Use: public
150
151 void
152 TsField::handleHelpKey()
153
154 //
155 ////////////////////////////////////////////////////////////////////////
156 {
157 // If the help message is not displayed, add the help message geometry
158 // to the scene graph and pause the game.
159 if (!helpDisplayed) {
160 helpRoot->addChild(helpGroup); //old: overRoot->addChild(helpRoot);
161 helpDisplayed = TRUE;
162 pauseGame();
163 }
164 }