CS 134 Alternate Inputs.

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

Cosc 5/4730 Input Keyboard, touch, and Accelerometer.
3.3. G AME I NPUT Handling input within games. In lecture exploration of answers to frequently asked student questions.
Donald Heer 2/14/2011. Overview  Input Types Keyboard Mouse  Examples  The Arcade What inputs does the arcade have?
Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead February 12, 2008 Creative Commons Attribution.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Inheritance & Interfaces. The Plan ● Motivate Inheritance ● Motivate Interfaces ● Examples ● Continue Practice on Observer/Observable.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
1Computer Graphics Input and Interaction Lecture 8 John Shearer Culture Lab – space 2
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
Processing Lecture.2 Mouse and Keyboard
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
For (int i = 1; i
CIS 3.5 Lecture 2.2 More programming with "Processing"
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
COSC 4126 User Interaction User Interaction capturing and responding to input events.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
CS Lecture 04 Mice Lynda Thomas
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
CompSci Inheritance & Interfaces. CompSci Inheritance & Interfaces The Plan  Motivate Inheritance  Motivate Interfaces  Examples  Continue.
Lecture Input Devices Keyboard. Mouse Microphone Digital Camera Scanner.
Task 1 and Task 2. Variables in Java Programs Variables start with lower case letter Variables are descriptive of what they store Variables are one word.
Events and Event Handling
Game Engine Architecture
OpModes in FTC Eric Golde.
Standard Methods of Input.
Lecture 2 Richard Gesick
Lecture 8 Object Oriented Programming Using Java
Handling User Events with Swing
Android Bluetooth Game Controllers
Keyboard Input.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Computer Science 209 Graphics and GUIs.
Frame Update, Level Representation & Graphics
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Programming III: Events
More on Graphical User Interfaces
CS 106A, Lecture 15 Events and Memory
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Computation as an Expressive Medium
CS 106A, Lecture 14 Events and Instance Variables
Introduction to Computing Using Java
UNITY TEAM PROJECT TOPICS: [1]. Unity Collaborate
Mouse Inputs in Processing
More programming with "Processing"
Game Loop Update & Draw.
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Events, Event Handlers, and Threads
LCC 6310 Computation as an Expressive Medium
Game Programming Algorithms and Techniques
05 | Capturing User Input Michael “Mickey” MacDonald | Indie Game Developer Bryan Griffiths | Software Engineer/Game Developer.
Presentation transcript:

CS 134 Alternate Inputs

Today in Video Games

Send me requests for the Flexi-Lectures Updates Send me requests for the Flexi-Lectures

Alternate Inputs Other input methods are similar via polling Mouse XInput Joystick Other inputs that must be done with events Touchscreen

Alternate Inputs Gesture recognition is much more important with other inputs Mouse Click vs Drag Joystick Light vs Heavy press

Alternate Inputs Each input has a different way of getting the actual input state May require setup / cleanup code But we usually don't care about cleanup, the OS will do that for us!

Keyboard

Keyboard No setup or cleanup code C API – Polling SDL_GetKeyboardState() Returns the state of each key on the keyboard Java API – Events (converted to Polling) KeyListener.keyPressed, .keyReleased Store current state in kbState Gestures Keep the last state of the keyboard around to know if it was just pressed or just released

Mouse

Mouse No setup or cleanup code C API – Polling SDL_GetMouseState() SDL_GetRelativeMouseState() SDL_SetRelativeMouseMode() Java API – Events (converted to polling) MouseListener.mouseMoved, .mouseClicked, .mouseDragged, etc. Gestures Click, double click, drag

Mouse SDL_GetMouseState(int* x, int* y) SDL_GetRelativeMouseState(int* x, int* y) Fill out X, Y with the pixel position / change of the mouse. Returns the current state of the mouse buttons Bitand (&) with each button to see its state SDL_BUTTON_LMASK for left SDL_BUTTON_MMASK for middle SDL_BUTTON_RMASK for right

Mouse // globals int mouseX, mouseY, mouseDeltaX, mouseDeltaY; Uint32 mouseButtons; // game loop while (!shouldExit) { lastFrameMs = currentFrameMs; memcpy(kbPrevState, kbState, sizeof(kbPrevState)); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: shouldExit = 1; } currentFrameMs = SDL_GetTicks(); float deltaTime = (currentFrameMs - lastFrameMs) / 1000.0f; mouseButtons = SDL_GetMouseState(&mouseX, &mouseY); SDL_GetRelativeMouseState(&mouseDeltaX, &mouseDeltaY); /* Rest of the game loop goes here */ SDL_GL_SwapWindow(window);

Mouse MouseListener.mouseMoved(MouseEvent e) Also mouseDragged, mousePressed, etc. e.getX(), e.getY() Get X, Y of mouse when event happened. e.getButton() Get button event happened for. No way to get only relative motion.

Mouse // fields public static int mouseX, mouseY; public static boolean mouseButton[] = new boolean[3]; // initializiation window.addKeyListener(new KeyListener() {...}); window.addMouseListener(new MouseListener() { private void updateMousePos(MouseEvent ev) { mouseX = ev.getX(); mouseY = ev.getY(); } private void updateMouseButtons(MouseEvent ev) { int idx = mouseEvent.getButton() - 1; if (idx >= 0 && idx < mouseButtons.length) { mouseButtons[idx] = true; public void mouseEntered(MouseEvent ev) { updateMousePos(ev); } public void mouseMoved(MouseEvent ev) { updateMousePos(ev); } public void mouseDragged(MouseEvent ev) { updateMousePos(ev); } public void mouseExited(MouseEvent ev) { updateMousePos(ev); } public void mousePressed(MouseEvent ev) { updateMouseButtons(ev); } public void mouseReleased(MouseEvent ev) { updateMouseButtons(ev); } public void mouseClicked(MouseEvent ev) {} public void mouseWheelMoved(MouseEvent ev){} });

Mouse SDL_SetRelativeMouseMode(bool) If set, the mouse will disappear and freeze. Relative motion will still work Useful for cameras! For RTS: Enable when dragging starts, disable when dragging ends For FPS: Enable when in gameplay, disable leaving gameplay No corresponding method for Java.

Mouse Using the mouse, you may want to distinguish between different gestures: Click / Double Click / Drag All you can detect is the low level operations Mouse Button Is Down / Mouse Button Is Up Unlike the keyboard, these gestures have a time component to them

Mouse What is a click? What is a double click? What is a drag?

Mouse What is a click? A down followed by an up within .1 sec Minimal mouse movement What is a double click? Two clicks within .3 seconds Note that this will ALSO include the clicks! What is a drag? Mouse held down for more than .1 sec -OR- significant movement with mouse down

Mouse To detect these you will need to keep track of when and where the mouse went down last! int lastMouseDownX, lastMouseDownY int lastMouseDownTime Then a click is when all of the following are true Left button is just down currentFrameTime - lastMouseDownTime < .1 sec abs(mouseX - lastMouseDownX) < 5 abs(mouseY - lastMouseDownY) < 5

Mouse Similar checks for Drag, Double click:

Mouse Questions?

Joysticks

Joysticks APIs for Joysticks XInput JInput XInput is significantly easier to use, but only works for Xbox 360-style controllers And only on Windows With Java bindings too! https://github.com/StrikerX3/JXInput

Joysticks No setup or cleanup code C API – Polling XInputGetState() struct XINPUT_STATE Java API – Polling XInputDevice.poll(), .getComponents().getAxes(), .getComponents().getButtons() Gestures Deadzone for sticks

Joysticks XInputGetState(int i, XINPUT_STATE* state) Returns ERROR_SUCCESS if a gamepad is connected i is 0 – 3, for each of the gamepads Fills out state with all the different parts of a gamepad

Joysticks struct XINPUT_STATE .Gamepad.wButtons Bitand with values to see the state of the DPAD, face buttons, bumpers, and click sticks XINPUT_GAMEPAD_DPAD_UP, etc. .Gamepad.bLeftTrigger, bRightTrigger 0 – 255 values for each trigger .Gamepad.sThumbLX, sThumbLY -32768 – 32767 values for the axis sThumbRX, sThumbRY for right stick

Joysticks XInputDevice.isAvailable() Returns true if XInput is available and library is loaded XInputDevice.getAllDevices() Returns an array of four XInputDevice objects, one for each player. Player 1’s controller is in arr[0], Player 2’s in arr[1], etc.

Joysticks XInputDevice.poll() Updates the state of all buttons and axes based on the current state of the controller. Returns true if that controller is plugged in. Call this every frame on every device! XInputDevice.getComponents().getButtons() Get the current state of all the buttons as an XinputButtons object. Has public fields .a, .b, .x, .y, .up, .down, etc.

Joysticks XInputDevice.getComponents().getAxes() Get the current state of all the axes as an XinputAxes object. Has public fields .lx, .ly, .rx, .ry, .lt, .rt, and raw copies of each Axes range from -1 to +1 for left and right sticks and 0 to 1 for triggers Raw values have the underlying integer values, which range from -32768 to 32767 for sticks and 0 to 255 for triggers.

Joysticks XInputDevice.getLastComponents() Gets the previous state for axes and buttons. Think “prevKb” but for the input device!

Joysticks Players are very bad at holding the sticks perfectly stationary. Need to create a “dead zone” where even if the stick is moving, it is treated as being at zero I suggest values from -500 to +500 (-0.02 to +0.02) Not an issue with triggers!

Joysticks Questions?