210 likes | 308 Views
User Interaction. capturing and responding to input events. Event handling in java. JRE. user action. input device. OS. AWT event queue. component + event. component event listeners. Events – message passing. AWTEvent ... ComponentEvent ... InputEvent KeyEvent MouseEvent ...
E N D
User Interaction capturing and responding to input events COSC 4126 User Interaction
Event handling in java JRE user action input device OS AWT event queue component + event component event listeners COSC 4126 User Interaction
Events – message passing AWTEvent • ... • ComponentEvent • ... • InputEvent • KeyEvent • MouseEvent • ... • MouseWheelEvent COSC 4126 User Interaction
Listening for events - interfaces EventListener • ... • AWTEventListener all AWT events • WindowListener window state • KeyListener keyboard • MouseListener click, press, enter • MouseMotionListener move, drag • MouseWheelListener COSC 4126 User Interaction
constructing a listener • create component object to capture events (window, button, etc) • create object that implements listener interface • code for reacting to event • attach listener to component COSC 4126 User Interaction
Keyboard control KeyListener methods public void keyTyped(KeyEvent e) Invoked when a key has been typed. See the class description for KeyEvent for a definition of a key typed event. (not often used in a real-time situation – includes multi-key combinations as single events) public void keyPressed(KeyEvent e) Invoked when a key has been pressed. See the class description for KeyEvent for a definition of a key pressed event. public void keyReleased(KeyEvent e) Invoked when a key has been released. See the class description for KeyEvent for a definition of a key released event. COSC 4126 User Interaction
Keyboard control KeyEvent contains information about the event what character, key location (eg left/right shift) multiple formats, string, unicode, ... for games, typically a small set of key actions are used for game control; other key events are ignored; note details in Brackeen (e.g., disabling tabs and alts) example: KeyTest.java in chapter 3 COSC 4126 User Interaction
Mouse control • MouseListener void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component. void mouseEntered(MouseEvent e) Invoked when the mouse enters a component. void mouseExited(MouseEvent e) Invoked when the mouse exits a component. for game interaction void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component. void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component. COSC 4126 User Interaction
Mouse control • MouseEvent • which button is pressed, released • mouse location COSC 4126 User Interaction
Mouse control • MouseMotionListener voidmouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged. All dragged events go to component where button was pushed. voidmouseMoved(MouseEvent e) Invoked when the mouse button has been moved on a component (with no buttons down). COSC 4126 User Interaction
Mouse control • MouseWheelListener void mouseWheelMoved(MouseWheelEvent e) Invoked when the mouse wheel is rotated. MouseWheelEvent extends MouseEvent • number of clicks (pos or neg) • number of scroll units • scroll type ( by line or by page ) example: MouseTest.java COSC 4126 User Interaction
The user input “language” • keyReleased(key) • mousePressed(button), mouseReleased(button) • keyPressed(key) • mouseMoved, mouseDragged • mouseWheelMoved change of state single action possible multiple or continuous action COSC 4126 User Interaction
User controlattaching action to inputs interface program - game input action point of view update point of view: display on/off, tooltips, pan/zoom COSC 4126 User Interaction
Mouselook – panning the view draw fullscreen image four times at: (x,y) (x-w,y) (x,y-h) (x-w,y-h) (x,y) COSC 4126 User Interaction
Mouselook – Panning with mouse • mouseMoved event • calculate change in mouse position • apply change to image display point (x,y) • reset mouse to screen centre example: MouselookTest.java COSC 4126 User Interaction
Robot classcontrolling mouse and keys void keyPress(int keycode) Presses a given key. void keyRelease(int keycode) Releases a given key. void mouseMove(int x, int y) Moves mouse pointer to given screen coordinates. void mousePress(int buttons) Presses one or more mouse buttons. void mouseRelease(int buttons) Releases one or more mouse buttons. void mouseWheel(int wheelAmt) Rotates the scroll wheel on wheel-equipped mice. ... other methods COSC 4126 User Interaction
Brackeen’s Input Manager • capture input Events • manage Event interpretation • manage connection of Events to GameActions • control when GameActions are made COSC 4126 User Interaction
InputManager class keyActions gameActions listeners 1 - 1 update game state mouseActions remapped COSC 4126 User Interaction
InputManagerTest example keys space A D P <esc> mouse MOUSEBUTTON1 MOUSEMOVELEFT MOUSEMOVERIGHT game actions moveLeft moveRight jump pause exit events keyPressed keyReleased mousePressed mouseReleased mouseMoved COSC 4126 User Interaction
Player class • moveLeft, moveRight, jump effect sprite • Player extends Sprite class • state: jumping or normal • floor: y-coordinate of normal path vertical velocity set by jump ‘falls’ over time 0 jump event COSC 4126 User Interaction
Assignments: • first individual assignment – one week • two interfaces • second group assignment – Thursday • topic • document – team plus topic COSC 4126 User Interaction