KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > UserManager


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer;
17
18 import java.awt.event.ItemListener JavaDoc;
19 import javax.swing.event.EventListenerList JavaDoc;
20 import javax.vecmath.*;
21
22 import com.buchuki.ensmer.input.*;
23 import com.buchuki.ensmer.input.event.*;
24 import com.buchuki.ensmer.input.event.semantic.UserMoveEvent;
25 import com.buchuki.ensmer.input.event.semantic.UserMoveListener;
26
27 /**
28  * Class to manage the users and interaction. Mostly it just acts as a
29  * wrapper for the <code>InputManager</code>. It also maintains code specific
30  * to the user, including selection information, inventory, position.
31  *
32  * @author Dusty Phillips [dusty@buchuki.com]
33  */

34 public class UserManager {
35
36     /**
37      * Add a UserMoveListener to the list of objects listening for
38      * UserMoveEvents
39      *
40      * @param listener the UserMoveListener to be added
41      */

42     public void addUserMoveListener(UserMoveListener listener) {
43         listenerList.add(UserMoveListener.class, listener);
44     }
45
46     /**
47      * Remove a UserMoveListener from the list of objects listening for
48      * UserMoveEvents
49      *
50      * @param listener the UserMoveListener to be removed
51      */

52     public void removeItemListener(UserMoveListener listener) {
53         listenerList.remove(UserMoveListener.class, listener);
54     }
55
56     /**
57      * Retrieve the InputManager for the current user.
58      *
59      * @return Ensmer's InputManager
60      */

61     public InputManager getInputManager() {
62         return inputManager;
63     }
64
65     /**
66      * Move the user by a relative amount in the current area. Allow the area to
67      * serialize the new position and instruct the Interface manager to move the
68      * actual view.
69      *
70      * @param relativePos A matrix4f containing a transformation relative to
71      * the current transform. This new transform is applied to the existing
72      * transform, not in place of it.
73      */

74     public void setUserPosition(Matrix4f relativePos) {
75         Matrix4f currentPos = EnsmerManager.instance().getInterfaceManager()
76             .getViewPosition();
77         currentPos.mul(relativePos);
78         setAbsoluteUserPosition(currentPos);
79         fireUserMoveEvent(new UserMoveEvent(this, relativePos));
80     }
81
82     /**
83      * Move the user to an absolute position in the current area. Allow the area
84      * to serialize the new position and instruct the interface manager to move
85      * the actual view. No UserMoveEvent is fired when the view is reset
86      * absolutely.
87      *
88      * @param absolutePos A matrix4f containing a transformation relative to
89      * the origin. This new transform is applied in place of the existing
90      * transform
91      */

92     public void setAbsoluteUserPosition(Matrix4f absolutePos) {
93         //broken apart to help trap intermittent nullpointerexceptions
94
InterfaceManager intf = EnsmerManager.instance().getInterfaceManager();
95         AreaManager area = EnsmerManager.instance().getAreaManager();
96         Area current = area.getCurrentArea();
97         intf.setViewPosition(absolutePos);
98         current.setUserPosition(absolutePos);
99     }
100
101     /**
102      * Fire an UserMoveEvent to all listening objects
103      *
104      * @param event The Event to fire
105      */

106     protected void fireUserMoveEvent(UserMoveEvent event) {
107         Object JavaDoc[] listeners = listenerList.getListenerList();
108         for (int i = listeners.length - 2; i >= 0; i -= 2) {
109             if (listeners[i] == UserMoveListener.class) {
110                 ((UserMoveListener) listeners[i + 1]).userMoved(event);
111             }
112         }
113     }
114
115     /**
116      * List of event listeners listening for high level UserMoved events.
117      */

118     EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
119
120     /**
121      * Reference to the InputManager that manages user input.
122      */

123     private InputManager inputManager = new InputManager();
124 }
125
126
Popular Tags