Class: Selector

THING.Selector()

Selector The selector to find objects with some conditions.

Constructor

new Selector()

Source:

Members

isSelector :Boolean

Check class type.
Type:
  • Boolean
Source:
Example
let selector = new THING.Selector();
// @expect(selector.isSelector == true);

length :Number

Get/Set the length of objects.
Type:
  • Number
Source:
Example
let length = selector.length;

pickable :Boolean

Set all objects and its children pickable state.
Type:
  • Boolean
Source:
Example
// Set pickable attribute of all objects and its children in selector to true
selector.pickable = true;

visible :Boolean

Set all objects and its children visible state.
Type:
  • Boolean
Source:
Example
// Set visible attribute of all objects and its children in selector to true
selector.visible = true;

Methods

add(selector) → {THING.Selector}

Add objects by selector and return new one with results.
Parameters:
Name Type Description
selector THING.Selector | Array.<THING.BaseObject> | THING.BaseObject The selector or object(s) to add.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.Entity');
let selector = new THING.Selector(objects);
let count1 = selector.length;
let object1 = new THING.BaseObject();
let object2 = new THING.BaseObject();
selector.add([object1,object2]);
let count2 = selector.length;
let ret = count2 - count1 == 2;
// @expect(ret == true)

clear()

Clear objects.
Source:
Example
let objects = app.query('.Entity');
let selector = new THING.Selector(objects);
selector.clear();
let ret = count == 0;
// @expect(ret == true)

concat(selector) → {THING.Selector}

Combine array.
Parameters:
Name Type Description
selector THING.Selector | Array.<THING.BaseObject> The objects array or selector.
Source:
Returns:
Type
THING.Selector
Example
// Concat other objects/selector and create new selector
let newSelector = selector.concat([obj1, obj2]);

destroy()

Destroy all.
Source:
Example
selector.destroy();

equals(objects) → {Boolean}

Check whether it's the same selector.
Parameters:
Name Type Description
objects Array.<Object> | THING.Selector The objects array or selector.
Source:
Returns:
Type
Boolean
Example
let isSame = selector.equals([obj1, obj2]);

filter(callback) → {THING.Selector}

Filter.
Parameters:
Name Type Description
callback OnFilterObjectsInSelector The callback function.
Source:
Returns:
Type
THING.Selector
Example
// Filter objects with name
let objects = selector.filter((object) => {
	return !!object.name;
});

find(condition, objects) → {BaseObject}

Find objects with some conditions and returns the first one.
Parameters:
Name Type Description
condition String The conditions.
objects Array.<Object> The objects what to be queried.
Source:
Returns:
Type
BaseObject
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let entity = selector.find('.Entity')
let ret = entity instanceof THING.Entity;;
// @expect(ret == true)

forEach(callback)

Traverse objects.
Parameters:
Name Type Description
callback OnTraverseObjectsInSelector The callback function(returns false indicates break it, otherwise continue to process it).
Source:
Example
// Traverse all objects in selector
selector.forEach((object) => {
	console.log(object);
});

forEachAsync(callback)

Traverse objects in async mode.
Parameters:
Name Type Description
callback OnTraverseObjectsInSelector The callback function(returns false indicates break it, otherwise continue to process it).
Source:
Example
// Objects fly one by one(it would wait for previous object fly to complete before start)
selector.forEachAsync(object => {
	return THING.App.current.camera.flyToAsync({
		target: object,
		duration: 1 * 1000,
		distance: 10,
		delayTime: THING.Math.randomFloat(0, 1000),
		complete: function (ev) {
		}
	});
});

getBoundingBox() → {THING.Box3}

Get the axis-aligned bounding box(AABB).
Source:
Returns:
Type
THING.Box3
Example
let selector = new THING.Selector();
let box = selector.getBoundingBox();
let ret = box.size[0] == 0;
// @expect(ret == true);

getInstancedDrawingObjects() → {Array.<THING.BaseObject>}

Get the instanced drawing objects.
Source:
Returns:
Type
Array.<THING.BaseObject>
Example
// Get the objects what has enabled instanced drawing
let instancedDrawingObjects = selector.getInstancedDrawingObjects();

has(object) → {Boolean}

Check whether has object or not.
Parameters:
Name Type Description
object Object The object.
Source:
Returns:
Type
Boolean
Example
// Check whether has specified object
let exists = selector.has(object);

includes(object) → {Boolean}

Check whether has/includes element.
Parameters:
Name Type Description
object Object The object.
Source:
Returns:
Type
Boolean
Example
// Check whether includes specified object
let exists = selector.includes(object);

indexOf(object) → {Number}

Get the index of element in objects.
Parameters:
Name Type Description
object Object The object.
Source:
Returns:
-1 indicates that specified object does not exist.
Type
Number
Example
// Get the index of object in selector
let index = selector.indexOf(object);

insert(index, object) → {THING.Selector}

Insert object by index.
Parameters:
Name Type Description
index Number The index to insert.
object THING.BaseObject The object to insert.
Source:
Returns:
Type
THING.Selector
Example
// Insert objects at the front of selector
selector.insert(0, [obj1, obj2]);

loadResource(options)

Load resource.
Parameters:
Name Type Description
options Object The load options.
Source:
Example
selector.loadResource();

makeInstancedDrawing(value, options)

Make objects in instanced drawing mode.
Parameters:
Name Type Description
value Boolean True indicates enable instanced drawing mode.
options Object The options.
Source:
Example
// Enable objects instanced drawing
selector.makeInstancedDrawing(true);

// Disable objects instanced drawing
selector.makeInstancedDrawing(false);

map(callback) → {THING.Selector}

Traverse objects and return an new selector.
Parameters:
Name Type Description
callback OnMapObjectsInSelector The callback function.
Source:
Returns:
Type
THING.Selector
Example
// Returns all first child object of each objects
let objects = selector.map((object) => {
	return object.children[0];
});

not(condition) → {THING.Selector}

Remove objects with some conditions and return new selector with results.
Parameters:
Name Type Description
condition String The condition.
Source:
Returns:
Type
THING.Selector
Example
// Return new selector by removing selector/objects
let objects = selector.not([obj1, obj2, obj3]);
console.log(objects);

off(type, condition, tag)

Unregister all objects event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
tag String The event tag.
Source:
Example
// Unregister all entities with 'click' event listener by tag name
let mark = 0;
let entity = new THING.Entity();
entity.on('test', ()=>{
   mark = 1;
})
let selector = new THING.Selector([entity]);
let markOff = 0;
selector.on('testOff', function(ev) {
		markOff = 1;
});
selector.trigger('testOff');
// @expect(markOff == 1 && mark == 1);
markOff = 0;
mark = 0;
selector.off('testOff');
selector.trigger('testOff');
// @expect(markOff == 0 && mak == 0);

on(type, condition, callback, tag, priority, options)

Register all objects event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
callback function The callback function.
tag String The event tag.
priority Number The priority value(default is 0, higher value will be processed first)
options ObjectEventOptions The options.
Source:
Example
// Register all entities with 'click' event listener
let mark = 0;
let entity = new THING.Entity();
entity.on('test', ()=>{
   mark = 1;
})
let selector = new THING.Selector([entity]);
let markOn = 0;
selector.on('testOn', function(ev) {
		markOn = 1;
});
selector.trigger('testOn');
// @expect(markOn == 1 && mark == 1);

once(type, condition, callback, tag, priority, options)

Register all objects event what just trigger once time.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
callback function The callback function.
tag String The event tag.
priority Number The priority value(default is 0, higher value will be processed first)
options ObjectEventOptions The options.
Source:
Example
// Register all entities with 'click' event listener in one time
let mark = 0;
let entity = new THING.Entity();
entity.on('test', ()=>{
   mark = 1;
})
let selector = new THING.Selector([entity]);
let markOnce = 0;
selector.once('testOnce', function(ev) {
		markOnce = 1;
});
selector.trigger('testOnce');
// @expect(markOnce == 1 && mark == 1);
markOnce = 0;
mark = 0;
selector.trigger('testOnce');
// @expect(markOnce == 0 && mark == 0);

pauseEvent(type, condition, tag)

Pause all objects event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
tag String The event tag.
Source:
Example
// Pause all entities with 'click' event listener by tag name
let mark = 0;
let entity = new THING.Entity();
entity.on('test', ()=>{
   mark = 1;
})
let selector = new THING.Selector([entity]);
let markPause = 0;
selector.on('testPause', function(ev) {
		markPause = 1;
});
selector.trigger('testPause');
// @expect(markPause == 1 && mark == 1);
markPause = 0;
mark = 0;
selector.PauseEvent('testPause');
selector.trigger('testPause');
// @expect(markPause == 0 && mark == 0);

push(object) → {Number}

Push object into it.
Parameters:
Name Type Description
object THING.BaseObject | Array.<THING.BaseObject> | THING.Selector The object what you want to push.
Source:
Returns:
The length of objects after push.
Type
Number
Example
let objects = app.query('.Entity');
let selector = new THING.Selector(objects);
let count1 = selector.length;
let object = new THING.BaseObject();
let count2 = selector.push(object);
let ret = count2 - count1 == 1;
// @expect(ret == true)

query(condition) → {THING.Selector}

Find objects with some conditions and store it in result.
Parameters:
Name Type Description
condition String The conditions.
Source:
Returns:
The reference of target or new selector.
Type
THING.Selector
Example
// Query/select objects by specified condition
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let entitys = selector.query('.Entity');
let entity = entitys[0];
// @expect(entity.isEntity == true);

queryById(condition, options) → {THING.Selector}

Query children by id.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let car = selector.queryById('car01');
let ret = car.length != 0 && car[0].id == 'car01';
// @expect(ret == true)

queryByName(condition, options) → {THING.Selector}

Query children by name.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let car = selector.queryByName('car1');
let ret = car.length != 0 && car[0].name == 'car1';
// @expect(ret == true)

queryByRegExp(condition, options) → {THING.Selector}

Query children by reg exp.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let car = selector.queryByRegExp(/car/);
let ret = car.length != 0;
// @expect(ret == true)

queryByTags(condition, options) → {THING.Selector}

Query children by tag.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let car = selector.queryByTags('Entity')
let ret = car.length != 0;
// @expect(ret == true)

queryByType(condition, options) → {THING.Selector}

Query children by type.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let entitys = selector.queryByType('Entity');
let ret = entitys.length != 0 && entitys[0].type == 'Entity';
// @expect(ret == true)

queryByUUID(condition, options) → {THING.Selector}

Query children by uuid.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let car = selector.queryByUUID('1605');
let ret = car.length != 0 && car[0].uuid == '1605';
// @expect(ret == true)

queryByUserData(condition, options) → {THING.Selector}

Query children by userData.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let car = selector.queryByUserData('id="666"')
let ret = car.length != 0 && car[0].userData.id == '666';
// @expect(ret == true)

reduce(callback, initialValue) → {Number}

Executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
Parameters:
Name Type Default Description
callback OnReduceObjectsInSelector The callback function.
initialValue Number 0 The initial number.
Source:
Returns:
Type
Number
Example
// Get avg height of entities
let entities = app.query('.Entity');
let height = entities.reduce((value, entity) => {
	return value + entity.position[1];
}, 0) / entities.length;

remove(condition) → {THING.Selector}

Remove objects with some conditions and return new selector with results.
Parameters:
Name Type Description
condition String The condition.
Source:
Returns:
Type
THING.Selector
Example
let objects = app.query('.Entity');
let selector = new THING.Selector(objects);
selector.remove('.Entity');
let count = selector.length;
let ret = count == 0;
// @expect(ret == true)

removeAt(index) → {THING.Selector}

Remove object at index.
Parameters:
Name Type Description
index Number The index of object to remove.
Source:
Returns:
Type
THING.Selector
Example
// Remove the first object in selector.
selector.remoteAt(0);

resumeEvent(type, condition, tag)

Resume all objects event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
tag String The event tag.
Source:
Example
// Resume all entities with 'click' event listener by tag name
let mark = 0;
let entity = new THING.Entity();
entity.on('test', ()=>{
   mark = 1;
})
let selector = new THING.Selector([entity]);
let markResume = 0;
selector.on('testPause', function(ev) {
		markResume = 1;
});
selector.trigger('testPause');
// @expect(markResume == 1 && mark == 1);
markResume = 0;
mark = 0;
selector.PauseEvent('testPause');
selector.trigger('testPause');
// @expect(markResume == 0 && mark == 0);
selector.resumeEvent('testPause');
selector.trigger('testPause');
// @expect(markResume == 1 && mark == 1);

reverse() → {THING.Selector}

Reverse the objects.
Source:
Returns:
Type
THING.Selector
Example
// Reverse objects in selector
selector.reverse();

select(condition, objects) → {Array.<Object>}

Select objects by condition with object's queryable state.
Parameters:
Name Type Description
condition String The conditions.
objects Array.<Object> The objects what to be queried.
Source:
Returns:
Type
Array.<Object>
Example
// Select entities from objects
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let entities = selector.select('.Entity', objects);
let entity = entities[0];
let ret = entity.isEntity;
// @expect(ret == true)

setVisible(value, recursiveopt)

Set visible state.
Parameters:
Name Type Attributes Default Description
value Boolean True indicates show it, otherwise hide it.
recursive Boolean <optional>
false True indicates process it with all children.
Source:
Example
// Set visible attribute of all objects in selector to true
selector.setVisible(true);

// Set visible attribute of all objects and its children in selector to true
selector.setVisible(true, true);

slice(start, end?) → {THING.Selector}

Return elements in range.
Parameters:
Name Type Description
start Number The start index.
end? Number The end index.
Source:
Returns:
Type
THING.Selector
Example
// Returns [1, 4] range of objects
let objects1 = selector.slice(1, 4);

// Returns [1, ...] range of objects
let objects2 = selector.slice(1);

sort(callback) → {THING.Selector}

Sort objects from low to high by the result from callback function.
Parameters:
Name Type Description
callback OnSortObjectsInSelector The callback function to sort.
Source:
Returns:
Type
THING.Selector
Example
// Sort objects by name
selector.sort((obj1, obj2) => {
	return obj1.name.localeCompare(obj2.name);
})

splice(index, number) → {THING.Selector}

Remove objects.
Parameters:
Name Type Description
index Number The start index.
number Number The number of objects what to remove.
Source:
Returns:
The removed objects.
Type
THING.Selector
Example
// Remove [1, 4] range of objects
let removeObjects = selector.splice(1, 4);

swap(index0, index1) → {THING.Selector}

Swap objects by index.
Parameters:
Name Type Description
index0 Number The first index of objects.
index1 Number The second index of objects.
Source:
Returns:
Type
THING.Selector
Example
// Swap (index: 0) and (index:3) objects in selector
selector.swap(0, 3);

test(condition, object) → {Boolean}

Test the single object with some conditions (without object's queryable state).
Parameters:
Name Type Description
condition String The conditions.
object THING.BaseObject The object what to be queried.
Source:
Returns:
Type
Boolean
Example
// Test whether object fit specified condition
let objects = app.query('.BaseObject');
let selector = new THING.Selector(objects);
let object = new THING.Entity();
let ret = selector.test('.Entity', object);
// @expect(ret == true);
let box = new THING.Box();
ret = selector.test('.Entity', box);
// @expect(ret == false);

toArray() → {Array.<THING.BaseObject>}

Convert to array (Return a new array)
Source:
Returns:
Type
Array.<THING.BaseObject>
Example
// Get/Convert objects in array mode
let objects = selector.objects;
console.log(objects);

trigger(type, ev, options)

Trigger all objects event.
Parameters:
Name Type Description
type String The event type.
ev Object The event info.
options Object The options.
Properties
Name Type Description
tag String The tag name.
Source:
Example
// Trigger all entities with 'click' event listener by tag name
let mark = 0;
let entity = new THING.Entity();
entity.on('test', ()=>{
  mark = 1;
})
let selector = new THING.Selector([entity]);
selector.trigger('test');
// @expect(mark == 1)

unloadResource()

Unload resource.
Source:
Example
selector.unloadResource();

waitForComplete() → {Promise.<any>}

Wait for all objects load completed.
Source:
Returns:
Type
Promise.<any>
Example
// Wait all objects load finished then print their name
selector.waitForComplete().then((objects) => {
	objects.forEach(object => {
		console.log(object.name);
	});
});