iwmlib/lib/pixi/scatter.js

480 lines
14 KiB
JavaScript
Raw Normal View History

2019-08-08 12:06:16 +02:00
/* eslint-disable no-unused-vars */
2019-03-21 09:57:27 +01:00
import Events from '../events.js'
import { AbstractScatter } from '../scatter.js'
import { Angle, Points, Polygon } from '../utils.js'
import { InteractionMapper } from '../interaction.js'
/** A container for scatter objects, which uses a single InteractionMapper
* for all children. This reduces the number of registered event handlers
* and covers the common use case that multiple objects are scattered
* on the same level.
*/
export class ScatterContainer extends PIXI.Graphics {
/**
2019-07-18 12:26:39 +02:00
* @constructor
* @param {PIXI.Renderer} renderer - PIXI renderer, needed for hit testing
* @param {Bool} stopEvents - Whether events should be stopped or propagated
* @param {Bool} claimEvents - Whether events should be marked as claimed
* if findTarget return as non-null value.
* @param {PIXI.Container} container - A container for the scatter
* @param {Bool} showBounds - Show bounds for debugging purposes.
* @param {Bool} showTouches - Show touches and pointer for debugging purposes.
* @param {Color} backgroundColor - Set background color if specified.
* @param {PIXIApp} app - Needed if showBounds is true to register
* update handler.
*/
constructor(
renderer,
{
stopEvents = true,
claimEvents = true,
container = null,
showBounds = false,
showPolygon = false,
showTouches = false,
backgroundColor = null,
app = window.app
} = {}
) {
2019-03-21 09:57:27 +01:00
super()
this.container = container
if (this.container)
this.containerDimensions = {
x: this.container.width,
y: this.container.height
}
this.backgroundWidth = null
this.backgroundHeight = null
this.app = app
this.renderer = renderer
this.stopEvents = stopEvents
this.claimEvents = claimEvents
this.delegate = new InteractionMapper(this.eventReceiver, this)
this.showBounds = showBounds
this.showTouches = showTouches
this.showPolygon = showPolygon
this.backgroundColor = backgroundColor
if (showBounds || showTouches || showPolygon) {
//console.log("Show TOUCHES!!!")
2019-07-18 12:26:39 +02:00
this.app.ticker.add(delta => this.update(delta), this)
2019-03-21 09:57:27 +01:00
}
if (backgroundColor) {
this.updateBackground()
}
}
updateBackground() {
this.clear()
let rect = this.bounds
this.beginFill(this.backgroundColor, 1)
this.drawRect(0, 0, rect.width, rect.height)
this.endFill()
}
get eventReceiver() {
return this.renderer.plugins.interaction.interactionDOMElement
}
get bounds() {
let x = 0
let y = 0
// @container: We need to call the constant values, as the container
// gets resized, when a child moves outside the original boundaries.
2019-07-30 16:56:29 +02:00
let w = this.container ? this.containerDimensions.x : this.backgroundWidth || this.app.width
let h = this.container ? this.containerDimensions.y : this.backgroundHeight || this.app.height
2019-03-21 09:57:27 +01:00
if (this.app.fullscreen && this.app.monkeyPatchMapping) {
let fixed = this.mapPositionToPoint({ x: w, y: 0 })
if (fixed.x < w) {
w = fixed.x
}
if (fixed.y > 0) {
y += fixed.y
h -= fixed.y
}
}
return new PIXI.Rectangle(x, y, w, h)
}
get center() {
let r = this.bounds
return { x: r.width / 2, y: r.height / 2 }
}
get polygon() {
let r = this.bounds
let w2 = r.width / 2
let h2 = r.height / 2
let center = { x: w2, y: h2 }
let polygon = new Polygon(center)
polygon.addPoint({ x: -w2, y: -h2 })
polygon.addPoint({ x: w2, y: -h2 })
polygon.addPoint({ x: w2, y: h2 })
polygon.addPoint({ x: -w2, y: h2 })
return polygon
}
update(dt) {
this.clear()
2019-07-18 12:26:39 +02:00
this.lineStyle(1, 0x0000ff)
2019-03-21 09:57:27 +01:00
if (this.showBounds) {
for (let child of this.children) {
if (child.scatter) {
//let {x, y, width, height} = child.scatter.throwBounds()
// new PIXI.Rectangle(x, y, width, height)
this.drawShape(child.scatter.bounds)
let center = child.scatter.center
this.drawCircle(center.x, center.y, 4)
this.drawCircle(child.scatter.x, child.scatter.y, 4)
}
}
2019-07-18 12:26:39 +02:00
this.lineStyle(2, 0x0000ff)
2019-03-21 09:57:27 +01:00
this.drawShape(this.bounds)
}
if (this.showPolygon) {
2019-07-18 12:26:39 +02:00
this.lineStyle(2, 0xff0000)
2019-03-21 09:57:27 +01:00
for (let child of this.children) {
if (child.scatter) {
let polygon = child.scatter.polygon
let shape = new PIXI.Polygon(polygon.flatAbsolutePoints())
2019-09-10 16:34:59 +02:00
//shape.close() not possible in PixiJS v5
2019-03-21 09:57:27 +01:00
this.drawShape(shape)
}
}
}
if (this.showTouches) {
let current = this.delegate.interaction.current
for (let [key, point] of current.entries()) {
let local = this.mapPositionToPoint(point)
this.drawCircle(local.x, local.y, 12)
}
}
}
capture(event) {
2019-07-18 12:26:39 +02:00
if (this.stopEvents) Events.stop(event)
2019-03-21 09:57:27 +01:00
return true
}
fakeInteractionEvent(point, key) {
return { data: { global: point, key: key } }
}
findHitScatter(data, displayObject, hit) {
// if (hit) {
// console.log("findHitScatter", displayObject)
// }
2019-07-30 16:56:29 +02:00
if (hit && this.hitScatter === null && typeof displayObject != undefined) {
this.hitScatter = displayObject.scatter ? displayObject.scatter : null
2019-03-21 09:57:27 +01:00
}
}
mapPositionToPoint(point, element = null) {
// In case of nested scatters we get an additional parameter that
// contains the found scatter
let local = new PIXI.Point()
let interactionManager = this.renderer.plugins.interaction
interactionManager.mapPositionToPoint(local, point.x, point.y)
2019-07-30 16:56:29 +02:00
if (element instanceof DisplayObjectScatter && element.displayObject.parent != null) {
2019-03-21 09:57:27 +01:00
return element.displayObject.parent.toLocal(local)
}
return local
}
/**
* New method hitTest implemented (in InteractionManager, since 4.5.0).
* See https://github.com/pixijs/pixi.js/pull/3878
*/
findTarget(event, local, global) {
if (event.claimedByScatter) {
return null
}
this.hitScatter = null
let interactionManager = this.renderer.plugins.interaction
let fakeEvent = this.fakeInteractionEvent(local)
2019-07-30 16:56:29 +02:00
interactionManager.processInteractive(fakeEvent, this, this.findHitScatter.bind(this), true)
2019-07-18 12:26:39 +02:00
if (this.claimEvents) event.claimedByScatter = this.hitScatter
2019-03-21 09:57:27 +01:00
return this.hitScatter
}
findTargetNew(event, local, global) {
// UO: still problematic. Does not find non interactive elements
// which are needed for some stylus applications
if (event.claimedByScatter) {
return null
}
this.hitScatter = null
let interactionManager = this.renderer.plugins.interaction
let displayObject = interactionManager.hitTest(local, this)
2019-07-30 16:56:29 +02:00
if (displayObject != null && displayObject.scatter != null) this.hitScatter = displayObject.scatter
2019-07-18 12:26:39 +02:00
if (this.claimEvents) event.claimedByScatter = this.hitScatter
2019-03-21 09:57:27 +01:00
return this.hitScatter
}
2019-07-18 12:26:39 +02:00
onStart(event, interaction) {}
2019-03-21 09:57:27 +01:00
2019-07-18 12:26:39 +02:00
onMove(event, interaction) {}
2019-03-21 09:57:27 +01:00
onEnd(event, interaction) {
for (let key of interaction.ended.keys()) {
let point = interaction.ended.get(key)
if (interaction.isLongPress(key)) {
this.onLongPress(key, point)
}
if (interaction.isTap(key)) {
this.onTap(key, point)
}
}
}
onTap(key, point) {
console.info('ScatterContainer.onTap')
}
onLongPress(key, point) {
console.info('ScatterContainer.onLongPress')
}
bringToFront(displayObject) {
this.addChild(displayObject)
}
layout(width, height) {
this.backgroundWidth = width
this.backgroundHeight = height
if (this.backgroundColor) {
this.updateBackground()
}
}
}
/** A wrapper for child elements of a ScatterContainer. Can be used
* to combine scattered objects with non-scattered objects. Any
* PIXI.DisplayObject can be wrapped.
*/
export class DisplayObjectScatter extends AbstractScatter {
2019-07-18 12:26:39 +02:00
constructor(
displayObject,
renderer,
{
x = null,
y = null,
2019-03-21 09:57:27 +01:00
minScale = 0.1,
maxScale = 1.0,
startScale = 1.0,
autoBringToFront = true,
2019-07-18 12:26:39 +02:00
translatable = true,
scalable = true,
rotatable = true,
resizable = false,
2019-03-21 09:57:27 +01:00
movableX = true,
movableY = true,
throwVisibility = 44,
throwDamping = 0.95,
autoThrow = true,
rotationDegrees = null,
rotation = null,
overdoScaling = 1.5,
onTransform = null,
2019-07-18 12:26:39 +02:00
onThrowFinished = null
} = {}
) {
2019-03-21 09:57:27 +01:00
// For the simulation of named parameters,
// see: http://exploringjs.com/es6/ch_parameter-handling.html
super({
overdoScaling,
2019-07-18 12:26:39 +02:00
minScale,
maxScale,
2019-03-21 09:57:27 +01:00
startScale,
autoBringToFront,
2019-07-18 12:26:39 +02:00
translatable,
scalable,
rotatable,
resizable,
movableX,
movableY,
throwVisibility,
throwDamping,
2019-03-21 09:57:27 +01:00
autoThrow,
onThrowFinished,
2019-07-18 12:26:39 +02:00
rotationDegrees,
rotation,
2019-03-21 09:57:27 +01:00
onTransform
})
this.displayObject = displayObject
this.displayObject.scatter = this
this.renderer = renderer
this.scale = startScale
this.rotationDegrees = this.startRotationDegrees
// Only set x and y if they are specified.
// Otherwise the displayobject gets corrupted.
if (x != null) this.x = x
if (y != null) this.y = y
}
getWorldScatter() {
return this
}
/** Returns geometry data as object. **/
getState() {
return {
scale: this.scale,
x: this.x,
y: this.y,
rotation: this.rotation
}
}
setup() {
this.setupMouseWheelInteraction()
}
roundPixel(value) {
// UO: Should be obsolete because Renderer supports roundPixels by default
return value
}
get container() {
// return this.displayObject.parent
let obj = this.displayObject
2019-07-30 16:56:29 +02:00
while (obj.parent != null && !(obj.parent instanceof ScatterContainer)) obj = obj.parent
2019-03-21 09:57:27 +01:00
return obj.parent
}
get x() {
return this.position.x
}
set x(value) {
this.position.x = value
}
get y() {
return this.position.y
}
set y(value) {
this.position.y = value
}
get polygon() {
let polygon = new Polygon(this.center)
let w2 = this.width / 2
let h2 = this.height / 2
polygon.addPoint({ x: -w2, y: -h2 })
polygon.addPoint({ x: w2, y: -h2 })
polygon.addPoint({ x: w2, y: h2 })
polygon.addPoint({ x: -w2, y: h2 })
polygon.rotate(this.rotation)
return polygon
}
get containerBounds() {
return this.container.bounds
}
get containerPolygon() {
let container = this.container
if (container == null) return null
return container.polygon
}
get position() {
return this.displayObject.position
}
set position(value) {
this.displayObject.position = value
}
get scale() {
return this.displayObject.scale.x
}
set scale(value) {
this.displayObject.scale.x = value
this.displayObject.scale.y = value
}
get width() {
return this.displayObject.width
}
get height() {
return this.displayObject.height
}
get bounds() {
return this.displayObject.getBounds()
}
get pivot() {
return this.displayObject.pivot
}
get rotation() {
return this.displayObject.rotation
}
set rotation(value) {
this.displayObject.rotation = value
}
get rotationDegrees() {
return Angle.radian2degree(this.displayObject.rotation)
}
set rotationDegrees(value) {
this.displayObject.rotation = Angle.degree2radian(value)
}
get center() {
let w2 = this.width / 2
let h2 = this.height / 2
let dist = Math.sqrt(w2 * w2 + h2 * h2)
let angle = Points.angle({ x: w2, y: h2 }, { x: 0, y: 0 })
let p = this.displayObject.x
let c = Points.arc(this.position, this.rotation + angle, dist)
return c // Points.subtract(c, this.pivot)
}
get rotationOrigin() {
// In PIXI the default rotation and scale origin is the position
return this.position // Points.add(this.position, this.pivot)
}
mapPositionToContainerPoint(point) {
// UO: We need the coordinates related to this scatter in case
// of nested scatters
2019-07-30 16:56:29 +02:00
if (this.container != null) return this.container.mapPositionToPoint(point, this)
2019-03-21 09:57:27 +01:00
return point
}
capture(event) {
return true
}
bringToFront() {
if (this.autoBringToFront) {
if (this.displayObject.parent instanceof ScatterContainer) {
let scatterContainer = this.displayObject.parent
scatterContainer.bringToFront(this.displayObject)
2019-07-30 16:56:29 +02:00
} else if (this.displayObject.parent != null && this.displayObject.parent.scatter) {
2019-03-21 09:57:27 +01:00
this.displayObject.parent.scatter.toFront(this.displayObject)
}
}
}
toFront(displayObject) {
this.displayObject.addChild(displayObject)
}
validScale(scale) {
scale = Math.max(scale, this.minScale)
scale = Math.min(scale, this.maxScale)
return scale
}
}