1
2
3
4
5
6
7 package org.sourceforge.jvb3d.Control;
8
9 import java.awt.event.KeyEvent;
10 import java.awt.event.KeyListener;
11 import java.awt.event.MouseEvent;
12
13 import javax.swing.event.MouseInputListener;
14 import org.sourceforge.jvb3d.Model.IModelInput;
15 import org.sourceforge.jvb3d.Network.INetworkModel;
16 import java.lang.*;
17
18 /***
19 * @author spootnick
20 *
21 * Klasa odpowiedzialna za obsługę sterowania. Steruje lokalną postacią w modelu
22 */
23 public class InputAdapter implements MouseInputListener, KeyListener {
24
25 private IModelInput model;
26
27 private int lastKey = 0;
28
29 private boolean isText = false;
30 private int screenHeight;
31 private int screenWidth;
32 private int lastX,lastY;
33 private float rotationAngle=(float)Math.PI;
34
35 public INetworkModel network;
36
37 public String localPlayerID;
38
39 public String localPlayerName = "";
40
41 /***
42 * Konstruktor pobierający referencję do modelu
43 * @param model
44 */
45 public InputAdapter(IModelInput model) {
46 this.model = model;
47 }
48
49 /***
50 * Ustawia wymiar ekranu użuwana przy sterowaniu myszą.
51 * Na razie nie jest ważne
52 * @param width wysokość w pikselach
53 * @param height szerokość
54 */
55 public void setScreen(int width,int height){
56
57 screenHeight=height;
58 screenWidth=width;
59 }
60
61 /***
62 * Konstruktor pobierający referencję do modelu i części sieciowej
63 * @param model część ligiki
64 * @param network część sieciowa
65 */
66 public InputAdapter(IModelInput model, INetworkModel network) {
67 this.model = model;
68 this.network = network;
69
70
71 }
72
73 private void triggerUpdate() {
74 if (network != null && localPlayerID != null) {
75 try {
76 network.sendUpdate(localPlayerID);
77 } catch (Exception ex) {
78 System.err.println(ex);
79 System.exit(0);
80 }
81
82 }
83 }
84
85 /***
86 * Używana gdy obiekt klasy jest w trybie przyjmowania teksu
87 * Powoduje doklejanie kolejnych znaków do aktualnie wprowadzanej linii
88 */
89 public void keyTyped(KeyEvent arg0) {
90
91 int key=arg0.getKeyChar();
92
93 if(isText && key!=8)
94 model.append(String.valueOf(arg0.getKeyChar()));
95 }
96
97
98 /***
99 * Używana gdy obiekt jest w trybie sterowania postacią
100 * Powoduje zmiany: prędkości i zmiane trybu sterowanie postacią/wprowadzanie tekstu
101 */
102 public void keyPressed(KeyEvent arg0) {
103
104
105
106 int key = arg0.getKeyCode();
107 if (!isText) {
108 if (key == KeyEvent.VK_LEFT)
109 model.turnLeftRight((float) Math.PI / 16);
110 else if(key==KeyEvent.VK_ENTER){
111 isText=!isText;
112 model.beginEdit(">");
113 }
114 else if (key == KeyEvent.VK_RIGHT)
115 model.turnLeftRight(-(float) Math.PI / 16);
116 else if (key == KeyEvent.VK_W)
117 model.moveForwardBackward(-0.2f);
118 else if (key == KeyEvent.VK_S)
119 model.moveForwardBackward(0.2f);
120 else if (key == KeyEvent.VK_A)
121 model.moveLeftRight(0.2f);
122 else if (key == KeyEvent.VK_D)
123 model.moveLeftRight(-0.2f);
124
125
126
127
128 if (lastKey != key) {
129 lastKey = key;
130 this.triggerUpdate();
131 }
132 }else{
133 if(key==KeyEvent.VK_ENTER)
134 {
135 isText=!isText;
136 model.submitLine(localPlayerName);
137 try{
138 network.sendUpdate("chat");
139
140 }catch(Exception ex){
141 System.err.println(ex);
142 System.exit(0);
143 }
144 }
145 else if(key==KeyEvent.VK_BACK_SPACE)
146 {
147 model.delete(1);
148
149 }
150
151 }
152 }
153
154 /***
155 * Używana gdy obiekt jest w trybie sterowania postacią
156 */
157 public void keyReleased(KeyEvent arg0) {
158
159 int key = arg0.getKeyCode();
160 lastKey = 0;
161 if (key == KeyEvent.VK_W)
162 model.moveForwardBackward(0);
163 else if (key == KeyEvent.VK_S)
164 model.moveForwardBackward(0);
165 else if (key == KeyEvent.VK_A)
166 model.moveLeftRight(0);
167 else if (key == KeyEvent.VK_D)
168 model.moveLeftRight(0);
169
170 this.triggerUpdate();
171
172 }
173
174 /***
175 * Nie używana
176 */
177 public void mouseClicked(MouseEvent arg0) {
178
179
180 }
181
182 /***
183 * Powoduje zaznaczenie początkowej pozycji przy obracaniu postaci myszą
184 */
185 public void mousePressed(MouseEvent arg0) {
186
187 lastX=arg0.getX();
188 lastY=arg0.getY();
189 }
190
191 /***
192 * Nie używana
193 */
194 public void mouseReleased(MouseEvent arg0) {
195
196
197 }
198
199 /***
200 * Nie używana
201 */
202 public void mouseEntered(MouseEvent arg0) {
203
204
205 }
206
207 /***
208 * Nie używana
209 */
210 public void mouseExited(MouseEvent arg0) {
211
212
213 }
214
215 /***
216 * Na podstawie zmiany położenia myszy powoduje odpowiedni obrót gracza
217 */
218 public void mouseDragged(MouseEvent arg0) {
219
220 int x=arg0.getX();
221 int y=arg0.getY();
222 model.turnLeftRight(-(float)(x-lastX)/screenWidth*rotationAngle);
223 model.turnUpDown(-(float)(y-lastY)/screenHeight*rotationAngle);
224 lastX=x;
225 lastY=y;
226 }
227
228 /***
229 * Nie używana
230 */
231 public void mouseMoved(MouseEvent arg0) {
232
233
234 }
235
236 }