Directional pad with up to 4 buttons

Assumes that each pad is manipulated by at max a single pointer.

Introduced properties

init defaultinit(center: Point3d[Float], names: Array[String])

gamnit :: DPad :: defaultinit

fun names: Array[String]

gamnit :: DPad :: names

Event names for the keys, in order of top, left, down and right
protected fun names=(names: Array[String])

gamnit :: DPad :: names=

Event names for the keys, in order of top, left, down and right
fun show_down: Bool

gamnit :: DPad :: show_down

Show the down button
fun show_down=(show_down: Bool)

gamnit :: DPad :: show_down=

Show the down button
fun show_left: Bool

gamnit :: DPad :: show_left

Show the left button
fun show_left=(show_left: Bool)

gamnit :: DPad :: show_left=

Show the left button
fun show_right: Bool

gamnit :: DPad :: show_right

Show the right button
fun show_right=(show_right: Bool)

gamnit :: DPad :: show_right=

Show the right button
fun show_up: Bool

gamnit :: DPad :: show_up

Show the up button
fun show_up=(show_up: Bool)

gamnit :: DPad :: show_up=

Show the up button

Redefined properties

redef type SELF: DPad

gamnit $ DPad :: SELF

Type of this instance, automatically specialized in every class
redef fun radius: Float

gamnit $ DPad :: radius

Radius in UI units/pixels of the part of this control

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun center: Point3d[Float]

gamnit :: RoundControl :: center

Center position on the UI
protected fun center=(center: Point3d[Float])

gamnit :: RoundControl :: center=

Center position on the UI
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(center: Point3d[Float], names: Array[String])

gamnit :: DPad :: defaultinit

fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun names: Array[String]

gamnit :: DPad :: names

Event names for the keys, in order of top, left, down and right
protected fun names=(names: Array[String])

gamnit :: DPad :: names=

Event names for the keys, in order of top, left, down and right
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
abstract fun radius: Float

gamnit :: RoundControl :: radius

Radius in UI units/pixels of the part of this control
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun show_down: Bool

gamnit :: DPad :: show_down

Show the down button
fun show_down=(show_down: Bool)

gamnit :: DPad :: show_down=

Show the down button
fun show_left: Bool

gamnit :: DPad :: show_left

Show the left button
fun show_left=(show_left: Bool)

gamnit :: DPad :: show_left=

Show the left button
fun show_right: Bool

gamnit :: DPad :: show_right

Show the right button
fun show_right=(show_right: Bool)

gamnit :: DPad :: show_right=

Show the right button
fun show_up: Bool

gamnit :: DPad :: show_up

Show the up button
fun show_up=(show_up: Bool)

gamnit :: DPad :: show_up=

Show the up button
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram gamnit::DPad DPad gamnit::RoundControl RoundControl gamnit::DPad->gamnit::RoundControl core::Object Object gamnit::RoundControl->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class RoundControl

gamnit :: RoundControl

Control composing a VirtualGamepad

Class definitions

gamnit $ DPad
# Directional pad with up to 4 buttons
#
# Assumes that each pad is manipulated by at max a single pointer.
class DPad
	super RoundControl

	# Event names for the keys, in order of top, left, down and right
	var names: Array[String]

	# Show the up button
	var show_up = true is writable

	# Show the down button
	var show_down = true is writable

	# Show the left button
	var show_left = true is writable

	# Show the right button
	var show_right = true is writable

	redef fun radius do return 200.0

	redef fun contains(ui_pos)
	do
		if show_down then return super(new Point[Float](ui_pos.x+0.0, ui_pos.y-100.0))
		return super
	end

	redef fun hit(event, ui_pos)
	do
		var display = app.display
		if display == null then return false

		var dx = ui_pos.x - center.x
		var dy = ui_pos.y - center.y
		if show_down then dy -= 100.0

		# Angle (with 0.0 on the right) to index in WASD (0 -> W/up)
		var indexes = new Set[Int]
		var ao = atan2(dy, dx)
		ao -= pi/4.0

		# Look for 2 angles so 2 buttons can be pressed at the same time
		for da in once [-pi/8.0, pi/8.0] do
			var a = ao+da
			while a < 0.0 do a += pi*2.0
			while a > 2.0*pi do a -= pi*2.0
			var index = (a * 2.0 / pi).floor.to_i
			if index < 0 then index += 4
			indexes.add index
		end

		var shows = [show_up, show_left, show_down, show_right]
		var new_down_names = new Set[String]
		for index in indexes do
			# Don't trigger events for hidden buttons/directions
			if not shows[index] then continue

			var name = names[index]
			# Simulate event
			var e = new VirtualGamepadEvent(name)
			e.is_down = event.pressed
			app.accept_event e

			if event.pressed then new_down_names.add name
		end

		# Depress released directions
		for name in down_names do
			if not new_down_names.has(name) then
				var e = new VirtualGamepadEvent(name)
				e.is_down = false
				app.accept_event e
			end
		end

		down_names = new_down_names

		return true
	end

	redef fun sprites
	do
		var dy = 0.0
		if show_down then dy = 100.0

		var sprites = new Array[Sprite]

		# Interactive buttons
		if show_up then sprites.add new Sprite(app.gamepad_spritesheet.dpad_up,
			center.offset(   0.0, 100.0+dy, 0.0))
		if show_left then sprites.add new Sprite(app.gamepad_spritesheet.dpad_left,
			center.offset(-100.0,   0.0+dy, 0.0))
		if show_right then sprites.add new Sprite(app.gamepad_spritesheet.dpad_right,
			center.offset( 100.0,   0.0+dy, 0.0))
		if show_down then sprites.add new Sprite(app.gamepad_spritesheet.dpad_down,
			center.offset(   0.0,-100.0+dy, 0.0))

		# Non-interactive joystick background
		var back = new Sprite(app.gamepad_spritesheet.joystick_back,
			center.offset(0.0, 0.0+dy, -1.0)) # In the back
		back.draw_order = -1
		sprites.add back

		# Non-interactive handle in the bottom
		if not show_down then sprites.add new Sprite(app.gamepad_spritesheet.joystick_down,
			center.offset(0.0, -100.0+dy,  0.0))

		return sprites
	end
end
lib/gamnit/virtual_gamepad/virtual_gamepad.nit:314,1--425,3