Two fingers camera manipulation, pinch to zoom and slide to scroll

Redefined classes

redef class EulerCamera

gamnit :: camera_control_android $ EulerCamera

Simple camera with perspective oriented with Euler angles (pitch, yaw, roll)
redef class Point[N: Numeric]

gamnit :: camera_control_android $ Point

2D point with x and z

All class definitions

redef class EulerCamera

gamnit :: camera_control_android $ EulerCamera

Simple camera with perspective oriented with Euler angles (pitch, yaw, roll)
redef class Point[N: Numeric]

gamnit :: camera_control_android $ Point

2D point with x and z
package_diagram gamnit::camera_control_android camera_control_android gamnit::camera_control camera_control gamnit::camera_control_android->gamnit::camera_control gamnit::gamnit_android gamnit_android gamnit::camera_control_android->gamnit::gamnit_android gamnit gamnit gamnit::camera_control->gamnit gamnit::cameras cameras gamnit::camera_control->gamnit::cameras gamnit::gamnit_android->gamnit android android gamnit::gamnit_android->android android::input_events input_events gamnit::gamnit_android->android::input_events realtime realtime gamnit::gamnit_android->realtime gamnit::display_android display_android gamnit::gamnit_android->gamnit::display_android ...gamnit ... ...gamnit->gamnit ...gamnit::cameras ... ...gamnit::cameras->gamnit::cameras ...android ... ...android->android ...android::input_events ... ...android::input_events->android::input_events ...realtime ... ...realtime->realtime ...gamnit::display_android ... ...gamnit::display_android->gamnit::display_android a_star-m a_star-m a_star-m->gamnit::camera_control_android

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module activities

android :: activities

Android Activities wrapper
module android

android :: android

Android services and implementation of app.nit
module angles

geometry :: angles

Angle related service using Float to represent an angle in radians
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

android :: assets

Implementation of app::assets
module assets

app :: assets

Portable services to load resources from the assets folder
module assets_and_resources

android :: assets_and_resources

Android Assets and Resources Management
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module boxes

geometry :: boxes

Provides interfaces and classes to represent basic geometry needs.
module bytes

core :: bytes

Services for byte streams and arrays
module c

c :: c

Structures and services for compatibility with the C language
module caching

serialization :: caching

Services for caching serialization engines
module cameras

gamnit :: cameras

Camera services producing Model-View-Projection matrices
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module collections

java :: collections

Basic Java collections
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module dalvik

android :: dalvik

Java related services specific to Android and its Dalvik VM
module display

gamnit :: display

Abstract display services
module display_android

gamnit :: display_android

Gamnit display implementation for Android
module egl

gamnit :: egl

Use of EGL to implement Gamnit on GNU/Linux and Android
module egl

egl :: egl

Interface between rendering APIs (OpenGL, OpenGL ES, etc.) and the native windowing system.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module ffi_support

java :: ffi_support

Core supporting services for the FFI with Java
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module game

android :: game

Android services and implementation of app.nit for gamnit and mnit
module gamnit

gamnit :: gamnit

Game and multimedia framework for Nit
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module geometry

geometry :: geometry

Provides interfaces and classes to represent basic geometry needs.
module glesv2

glesv2 :: glesv2

OpenGL graphics rendering library for embedded systems, version 2.0
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module input

mnit :: input

Defines abstract classes for user and general inputs to the application.
module input_events

android :: input_events

Pointer and hardware key events
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module io

java :: io

Services from the java.io package
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module java

java :: java

Supporting services for the FFI with Java and to access Java libraries
module jvm

jvm :: jvm

Java Virtual Machine invocation API and others services from the JNI C API
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module load_image

android :: load_image

Low-level services to load pixel data from the assets
module log

android :: log

Advanced Android logging services
module math

core :: math

Mathematical operations
module matrix

matrix :: matrix

Services for matrices of Float values
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module native

core :: native

Native structures for text and bytes
module native_app_glue

android :: native_app_glue

Wrapper of the Android native_app_glue framework to implement app.nit
module numeric

core :: numeric

Advanced services for Numeric types
module platform

android :: platform

Triggers compilation for the android platform
module points_and_lines

geometry :: points_and_lines

Interfaces and classes to represent basic geometry needs.
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module programs

gamnit :: programs

Services for graphical programs with shaders, attributes and uniforms
module projection

matrix :: projection

Services on Matrix to transform and project 3D coordinates
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module realtime

realtime :: realtime

Services to keep time of the wall clock time
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module textures

gamnit :: textures

Load textures, create subtextures and manage their life-cycle
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module camera_control

gamnit :: camera_control

Simple camera control for user, as the method accept_scroll_and_zoom
module gamnit_android

gamnit :: gamnit_android

Support services for Gamnit on Android

Children

module a_star-m

a_star-m

# Two fingers camera manipulation, pinch to zoom and slide to scroll
module camera_control_android

import android
import camera_control

redef class EulerCamera
	# Smoothened history of pointers in the current motion event
	private var last_motion_pointers = new HashMap[Int, Point[Float]] is lazy

	# Start time of the current motion event
	private var last_motion_start: Int = -1

	redef fun accept_scroll_and_zoom(event)
	do
		if not event isa AndroidMotionEvent then return false

		if event.pointers.length < 2 then
			# Intercept leftovers of the last motion
			return event.down_time == last_motion_start
		end

		# Collect active pointer and their world position
		var new_motion_pointers = new HashMap[Int, Point[Float]]
		var ids = new Array[Int]
		for pointer in event.pointers do
			var id = pointer.pointer_id
			ids.add id
			new_motion_pointers[id] = camera_to_world(pointer.x, pointer.y)
		end

		var last_motion_pointers = last_motion_pointers
		if last_motion_start == event.down_time and
		   last_motion_pointers.keys.has(ids[0]) and last_motion_pointers.keys.has(ids[1]) then
			# Continued motion event

			# Get new and old position for 2 fingers
			var new_motion_a = new_motion_pointers[ids[0]]
			var new_motion_b = new_motion_pointers[ids[1]]
			var prev_pos_a = last_motion_pointers[ids[0]]
			var prev_pos_b = last_motion_pointers[ids[1]]

			# Move camera
			var prev_middle_pos = prev_pos_a.lerp(prev_pos_b, 0.5)
			var new_middle_pos = new_motion_a.lerp(new_motion_b, 0.5)
			position.x -= new_middle_pos.x - prev_middle_pos.x
			position.y -= new_middle_pos.y - prev_middle_pos.y

			# Zoom camera
			var prev_dist = prev_pos_a.dist(prev_pos_b)
			var new_dist = new_motion_a.dist(new_motion_b)

			position.z = prev_dist * position.z / new_dist
		else
			# Prepare for a new motion event
			last_motion_pointers.clear
			last_motion_start = event.down_time
		end

		# Keep a smooth history
		for i in [0..1] do
			if last_motion_pointers.keys.has(ids[i]) then
				last_motion_pointers[ids[i]] = last_motion_pointers[ids[i]]*0.5 +
				                                new_motion_pointers[ids[i]]*0.5
			else last_motion_pointers[ids[i]] = new_motion_pointers[ids[i]]
		end

		return true
	end
end

redef class Point[N]
	private fun *(scalar: Numeric): Point[N]
	do return new Point[N](x.mul(scalar), y.mul(scalar))

	private fun +(other: Point[N]): Point[N]
	do return new Point[N](x.add(other.x), y.add(other.y))
end
lib/gamnit/camera_control_android.nit:15,1--92,3