view mde/input/eventstream.d.old @ 4:9a990644948c

Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 06 Jan 2008 17:38:51 +0000
parents
children
line wrap: on
line source

/** This module contains functions called on an event, which may modify the event (adjuster
 * functions), and finally output to one (or more) of the state tables (the event stream).
 *
 * Adjuster and other event functions should have a format to fit the ES_X_Func types, for X is B
 * (button event), A (axis event) or M (mouse relative motion event or joystick ball event).
 * Adjusters should call one of the xEventOut() functions with their output and the remainder of
 * the readOutQueue.
 *
 * To control which adjusters get called and pass parameters, a stack is used.
 */
module mde.input.eventstream;

// package imports
import mde.input.core;

/// Module constructor fills es_*_fcts tables and rehashes them.
static this () {
    es_b_fcts = [ ES_B_OUT : &es_b_out ];
}

/// These aliases are for pointers to the event functions.
alias void function (bool, readOutQueue) ES_B_Func;
alias void function (short, readOutQueue) ES_A_Func;			/// ditto
alias void function (short, short, readOutQueue) ES_M_Func;	/// ditto

/// These are the tables for looking up which event function to call.
static ES_B_Func[uint] es_b_fcts;
static ES_A_Func[uint] es_a_fcts;	/// ditto
static ES_M_Func[uint] es_m_fcts;	/// ditto

/// These are the codes allowing the config to specify event functions:
enum : uint {
    ES_B_OUT	= 0x0000_0100u,
    ES_A_OUT	= 0x0000_0200u,
    ES_M_OUT	= 0x0000_0300u,
}

/** These functions pass an event to the appropriate event function (adjuster or output func). */
void bEventOut (bool b, readOutQueue s)
{
	ES_B_Func* func = (s.next() in es_b_fcts);
	if (func != null) (*func)(b,s);
	else throw new InputException ("Input: Invalid configuration: bad event function code");
}
void aEventOut (short x, readOutQueue s)	/// ditto
{
	ES_A_Func* func = (s.next() in es_a_fcts);
	if (func != null) (*func)(x,s);
	else throw new InputException ("Input: Invalid configuration: bad event function code");
}
void mEventOut (short x, short y, readOutQueue s)	/// ditto
{
	ES_M_Func* func = (s.next() in es_m_fcts);
	if (func != null) (*func)(x,y,s);
	else throw new InputException ("Input: Invalid configuration: bad event function code");
}

/// Simple output function
void es_b_out (bool b, readOutQueue s) {
	current.button[cast(inputID) s.next()] = b;
}
/// Adjuster to check modifier keys
void es_b_modifier (bool b, readOutQueue s);

/** Simple output function

Adds 1-2 items on the stack.
*/
void es_a_out (short x, readOutQueue s) {
	real y = x;
	uint conf = s.next();
        enum : uint {
            HALF_RANGE	= 0x8000_0000u,
            SENSITIVITY	= 0x0080_0000u,
        }
        // Convert ranges into standard intervals (with or without reverse values)
	if (conf & HALF_RANGE) y = (y + 32767.0) * 1.5259254737998596e-05;	// range  0.0 - 1.0
	else y *= 3.0518509475997192e-05;					// range -1.0 - 1.0
	real a;
	if (conf & SENSITIVITY) a = s.next();
        /+ When a global sensitivity is available (possibly only use if it's enabled)...
        else a = axis.sensitivity;
	y = sign(y) * pow(abs(y), a);		// sensitivity adjustment by a +/
	current.axis[cast(inputID) s.next()] = y;
}

/// Simple output function
void es_m_out (short x, short y, readOutQueue s) {
	current.axis_rel[cast(inputID) s.next()] = RelPair(x,y);
}