1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkHitTest.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if SK_USE_CONDENSED_INFO == 0
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkMemberInfo SkHitTest::fInfo[] = {
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_ARRAY(bullets, Displayable),
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_ARRAY(hits, Int),
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_ARRAY(targets, Displayable),
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(value, Boolean)
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comDEFINE_GET_MEMBER(SkHitTest);
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkHitTest::SkHitTest() : value(false) {
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2893c7ee34dc5c8f6bfad65809f4b39f8d00d7f0d4sugoi@google.combool SkHitTest::draw(SkAnimateMaker&) {
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    hits.setCount(bullets.count());
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    value = false;
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int bulletCount = bullets.count();
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int targetCount = targets.count();
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int bIndex = 0; bIndex < bulletCount; bIndex++) {
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDisplayable* bullet = bullets[bIndex];
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect bBounds;
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bullet->getBounds(&bBounds);
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hits[bIndex] = -1;
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (bBounds.fLeft == (int16_t)0x8000U)
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            continue;
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (int tIndex = 0; tIndex < targetCount; tIndex++) {
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkDisplayable* target = targets[tIndex];
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRect tBounds;
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            target->getBounds(&tBounds);
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (bBounds.intersect(tBounds)) {
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                hits[bIndex] = tIndex;
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                value = true;
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5493c7ee34dc5c8f6bfad65809f4b39f8d00d7f0d4sugoi@google.combool SkHitTest::enable(SkAnimateMaker&) {
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int bIndex = 0; bIndex < bullets.count(); bIndex++) {
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDisplayable* bullet = bullets[bIndex];
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bullet->enableBounder();
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int tIndex = 0; tIndex < targets.count(); tIndex++) {
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDisplayable* target = targets[tIndex];
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        target->enableBounder();
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkHitTest::hasEnable() const {
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7093c7ee34dc5c8f6bfad65809f4b39f8d00d7f0d4sugoi@google.comconst SkMemberInfo* SkHitTest::preferredChild(SkDisplayTypes) {
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bullets.count() == 0)
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return getMember("bullets");
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return getMember("targets"); // !!! cwap! need to refer to member through enum like kScope instead
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
75