/**
 * clip this view so that outside of the visible bounds can be hidden.
 */
void ScrollView::onBeforeDraw()
{
    if (_clippingToBounds)
    {
		_scissorRestored = false;
        Rect frame = getViewRect();
        auto glview = Director::getInstance()->getOpenGLView();

        if (glview->isScissorEnabled()) {
            _scissorRestored = true;
            _parentScissorRect = glview->getScissorRect();
            //set the intersection of _parentScissorRect and frame as the new scissor rect
            if (frame.intersectsRect(_parentScissorRect)) {
                float x = MAX(frame.origin.x, _parentScissorRect.origin.x);
                float y = MAX(frame.origin.y, _parentScissorRect.origin.y);
                float xx = MIN(frame.origin.x+frame.size.width, _parentScissorRect.origin.x+_parentScissorRect.size.width);
                float yy = MIN(frame.origin.y+frame.size.height, _parentScissorRect.origin.y+_parentScissorRect.size.height);
                glview->setScissorInPoints(x, y, xx-x, yy-y);
            }
        }
        else {
            glEnable(GL_SCISSOR_TEST);
            glview->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
        }
    }
}
// init
//---------------------------------------------------------------------------
void MiniMapView::init()
{
    moveTo(iXY(0, 0));

    Surface *miniMap;
    miniMap = MiniMapInterface::getMiniMap();

    //iXY size = miniMap->getPix();
    iXY size(196, 196);

    resize(size);

    mapDrawType = MAP_SOLID;

    //int xOffset = size.x;
    //int yOffset = 0;

    MiniMapInterface::setMapScale(getViewRect().getSize());

    minMapSize =  64;
    maxMapSize = 480;

    // Get the original version of the minimap.
    miniMapSurface.copy(*miniMap);

    scaleGroupWait  = 0.0f;
    needScale       = true;
    selectionAnchor = false;

    selectionAnchorDownPos.zero();
    selectionAnchorCurPos.zero();

} // end MiniMapView::init
Exemple #3
0
bool ScrollTableView::onTouchBegan(CCTouch* touch, CCEvent* event){
    if(touchArea==NULL || !isTouchInside(touchArea, touch)) return  false;
    if (!this->isVisible())
    {
        return false;
    }
    m_startPoint = touch->getLocation();
    m_touchEnd = false;
    m_canClickMove = false;
    m_moveUp = false;
    unsigned long currentTime = clock()/1000;
    unsigned long gapTime = currentTime-m_clickTime;
    if(gapTime<300){
        return false;
    }
    m_clickTime = currentTime;
    if(clickArea!=NULL && !isTouchInside(clickArea, touch)){
         m_canClickMove = true;
    }
    
    m_tTouchTime = m_clickTime;
    CCRect frame = getViewRect();
    
    //dispatcher does not know about clipping. reject touches outside visible bounds.
    if (_touches.size() > 2 ||
        _touchMoved||
        !frame.containsPoint(_container->convertToWorldSpace(_container->convertTouchToNodeSpace(touch))))
    {
        return false;
    }
    
    if (std::find(_touches.begin(), _touches.end(), touch) == _touches.end())
    {
        _touches.push_back(touch);
    }
    
    if (_touches.size() == 1)
    { // scrolling
        _touchPoint     = this->convertTouchToNodeSpace(touch);
        _touchMoved     = false;
        _dragging     = true; //dragging started
        _scrollDistance = Vec2(0.0f, 0.0f);
        _touchLength    = 0.0f;
    }
    else if (_touches.size()  == 2)
    {
        _touchPoint = (this->convertTouchToNodeSpace(_touches[0]).getMidpoint(
                                                                              this->convertTouchToNodeSpace(_touches[1])));
        
        _touchLength = _container->convertTouchToNodeSpace(_touches[0]).getDistance(
                                                                                    _container->convertTouchToNodeSpace(_touches[1]));
        _dragging  = false;
    }
    m_touchBeganPoint = this->getContentOffset();
    m_aniStatus = false;
    return true;
}
// setViewWindow
//--------------------------------------------------------------------------
// Purpose: Sets the viewable window on the screen to the position clicked
//          on the map.  pos (position) corresponds to the grid(x,y) position clicked
//          ont he map.  The pos is converted to world coords and the
//          viewable window is repositioned.
//--------------------------------------------------------------------------
void MiniMapView::setViewWindow(const iXY &pos)
{
    assert(this != 0);

    iXY size(getViewRect().getSize());

    if ((pos.x >= 0) && (pos.x < size.x) && (pos.y >= 0) && (pos.y < size.y)) {
        MiniMapInterface::setWorldWindowPosition( iXY( pos.x, pos.y ) );
    }

} // end MiniMapView::setViewWindow
// doDecreaseSize
//--------------------------------------------------------------------------
void MiniMapView::doDecreaseSize(int value)
{
    iXY destSize(getViewRect().getSize());

    if (value == -1) {
        float dt = TimerInterface::getTimeSlice();

        destSize -= scaleDelta * dt;
    } else {
        destSize -= value;
    }

    resize(destSize);

    if (destSize < minMapSize) {
        resize(iXY(minMapSize, minMapSize));
    }

    MiniMapInterface::setMapScale(getViewRect().getSize());

    needScale      = true;
    scaleGroupWait = 0.0f;

} // end MiniMapView::doDecreaseSize
bool ScrollView::onTouchBegan(Touch* touch, Event* event)
{
    if (!this->isVisible() || !this->hasVisibleParents())
    {
        return false;
    }
    
    _beginOffset = getContentOffset();
    
    Rect frame = getViewRect();

    //dispatcher does not know about clipping. reject touches outside visible bounds.
    if (_touches.size() > 2 ||
        _touchMoved          ||
        !frame.containsPoint(touch->getLocation()))
    {
        return false;
    }

    if (std::find(_touches.begin(), _touches.end(), touch) == _touches.end())
    {
        _touches.push_back(touch);
    }

    if (_touches.size() == 1)
    { // scrolling
        _touchPoint     = this->convertTouchToNodeSpace(touch);
        _touchMoved     = false;
        _dragging     = true; //dragging started
        _scrollDistance.setZero();
        _touchLength    = 0.0f;
    }
    else if (_touches.size() == 2)
    {
        _touchPoint = (this->convertTouchToNodeSpace(_touches[0]).getMidpoint(
                        this->convertTouchToNodeSpace(_touches[1])));
        
        _touchLength = _container->convertTouchToNodeSpace(_touches[0]).getDistance(
                       _container->convertTouchToNodeSpace(_touches[1]));
        
        _dragging  = false;
    } 
    return true;
}
void ScrollView::onTouchMoved(Touch* touch, Event* event)
{
    if (!this->isVisible())
    {
        return;
    }

    if (std::find(_touches.begin(), _touches.end(), touch) != _touches.end())
    {
        if (_touches.size() == 1 && _dragging)
        { // scrolling
            Vec2 moveDistance, newPoint;
            Rect  frame;
            float newX, newY;
            
            frame = getViewRect();

            newPoint     = this->convertTouchToNodeSpace(_touches[0]);
            moveDistance = newPoint - _touchPoint;
            
            float dis = 0.0f;
            if (_direction == Direction::VERTICAL)
            {
                dis = moveDistance.y;
                float pos = _container->getPosition().y;
                if (!(minContainerOffset().y <= pos && pos <= maxContainerOffset().y)) {
                    moveDistance.y *= BOUNCE_BACK_FACTOR;
                }
            }
            else if (_direction == Direction::HORIZONTAL)
            {
                dis = moveDistance.x;
                float pos = _container->getPosition().x;
                if (!(minContainerOffset().x <= pos && pos <= maxContainerOffset().x)) {
                    moveDistance.x *= BOUNCE_BACK_FACTOR;
                }
            }
            else
            {
                dis = sqrtf(moveDistance.x*moveDistance.x + moveDistance.y*moveDistance.y);
                
                float pos = _container->getPosition().y;
                if (!(minContainerOffset().y <= pos && pos <= maxContainerOffset().y)) {
                    moveDistance.y *= BOUNCE_BACK_FACTOR;
                }
                
                pos = _container->getPosition().x;
                if (!(minContainerOffset().x <= pos && pos <= maxContainerOffset().x)) {
                    moveDistance.x *= BOUNCE_BACK_FACTOR;
                }
            }

            if (!_touchMoved && fabs(convertDistanceFromPointToInch(dis)) < MOVE_INCH )
            {
                //CCLOG("Invalid movement, distance = [%f, %f], disInch = %f", moveDistance.x, moveDistance.y);
                return;
            }
            
            if (!_touchMoved)
            {
                moveDistance = Vec2::ZERO;
            }
            
            _touchPoint = newPoint;
            _touchMoved = true;
            
            if (_dragging)
            {
                switch (_direction)
                {
                    case Direction::VERTICAL:
                        moveDistance = Vec2(0.0f, moveDistance.y);
                        break;
                    case Direction::HORIZONTAL:
                        moveDistance = Vec2(moveDistance.x, 0.0f);
                        break;
                    default:
                        break;
                }

                newX     = _container->getPosition().x + moveDistance.x;
                newY     = _container->getPosition().y + moveDistance.y;

                _scrollDistance = moveDistance;
                this->setContentOffset(Vec2(newX, newY));
            }
        }
        else if (_touches.size() == 2 && !_dragging)
        {
            const float len = _container->convertTouchToNodeSpace(_touches[0]).getDistance(
                                            _container->convertTouchToNodeSpace(_touches[1]));
            this->setZoomScale(this->getZoomScale()*len/_touchLength);
        }
    }
}
// doDraw
//---------------------------------------------------------------------------
void MiniMapView::doDraw(const Surface &viewArea, const Surface &clientArea)
{
    assert(this != 0);
    assert(viewArea.getDoesExist());
    assert(clientArea.getDoesExist());

    if (decreaseSize != 0) {
        doDecreaseSize(decreaseSize);
        decreaseSize = 0;
    }
    if (increaseSize != 0) {
        doIncreaseSize(increaseSize);
        increaseSize = 0;
    }

    float dt = TimerInterface::getTimeSlice();

    Surface *miniMap;
    miniMap = MiniMapInterface::getMiniMap();

    if (needScale) {
        scaleGroupWait += dt;

        if (scaleGroupWait > 1.0f) {
            miniMapSurface.create(getViewRect().getSize(), getViewRect().getSize().x , 1);

            //miniMapSurface.scale(getViewRect().getSize());
            iRect r(iXY(0, 0), getViewRect().getSize());

            miniMapSurface.bltScale(*miniMap, r);

            needScale      = false;
            scaleGroupWait = 0.0f;
        }
    }

    iRect r(getViewRect().min, getViewRect().max);

    if (needScale) {
        // Draw the slow on the fly scaled map.
        if (mapDrawType == MAP_SOLID) {
            clientArea.bltScale(*miniMap, r);
        } else if (mapDrawType == MAP_2080) {
            clientArea.bltBlendScale(*miniMap, r, Palette::colorTable2080);
        } else if (mapDrawType == MAP_4060) {
            clientArea.bltBlendScale(*miniMap, r, Palette::colorTable4060);
        }
        //else if (mapDrawType == MAP_BLEND_GREEN)
        //{
        //clientArea.bltLookup(iRect(iXY(0, 0), getSize()), Palette::green256.getColorArray());
        //}
        else if (mapDrawType == MAP_BLEND_GRAY) {
            clientArea.bltLookup(iRect(iXY(0, 0), getSize()), Palette::gray256.getColorArray());
        } else if (mapDrawType == MAP_BLEND_DARK_GRAY) {
            clientArea.bltLookup(iRect(iXY(0, 0), getSize()), Palette::darkGray256.getColorArray());
        } else if (mapDrawType == MAP_BLACK) {
            clientArea.fill(Color::black);
        } else if (mapDrawType == MAP_TRANSPARENT) {}
    }
    else {
        // Draw the fast not on the fly scaled map.
        if (mapDrawType == MAP_SOLID) {
            miniMapSurface.blt(clientArea, 0, 0);
        } else if (mapDrawType == MAP_2080) {
            clientArea.blendIn(miniMapSurface, iXY(0, 0), Palette::colorTable2080);
        } else if (mapDrawType == MAP_4060) {
            clientArea.blendIn(miniMapSurface, iXY(0, 0), Palette::colorTable4060);
        }
        //else if (mapDrawType == MAP_BLEND_GREEN)
        //{
        //clientArea.bltLookup(iRect(iXY(0, 0), getSize()), Palette::green256.getColorArray());
        //}
        else if (mapDrawType == MAP_BLEND_GRAY) {
            clientArea.bltLookup(iRect(iXY(0, 0), getSize()), Palette::gray256.getColorArray());
        } else if (mapDrawType == MAP_BLEND_DARK_GRAY) {
            clientArea.bltLookup(iRect(iXY(0, 0), getSize()), Palette::darkGray256.getColorArray());
        } else if (mapDrawType == MAP_BLACK) {
            clientArea.fill(Color::black);
        } else if (mapDrawType == MAP_TRANSPARENT) {}
    }

    // Draw a hairline border.
    //viewArea.drawRect(Color::white);
    viewArea.drawLookupBorder(Palette::darkGray256.getColorArray());

    // Draw the world view box.
    clientArea.bltLookup(MiniMapInterface::getWorldWindow(), Palette::darkGray256.getColorArray());

    // Draw the units and such on the minimap.
    MiniMapInterface::annotateMiniMap((Surface &) clientArea);

    // Draw the world view box corners.
    clientArea.drawBoxCorners(MiniMapInterface::getWorldWindow(), 5, Color::white);

    // Draw an inner black rect inside the outer white rect, for visibility reasons.
    //iRect innerRect(MiniMapInterface::getWorldWindow());
    //
    //innerRect.min += 1;
    //innerRect.max -= 1;
    //
    //clientArea.drawBoxCorners(innerRect, 4, Color::black);

    // If the mouse is over the client area, then change the cursor.
    if (getClientRect().contains(getScreenToClientPos(mouse.getScreenPos()))) {
        if (selectionAnchor) {
            // Since we are selecting units, draw the selection box.
            clientArea.drawRect(selectionAnchorDownPos, selectionAnchorCurPos, Color::white);
        } else {
            // Draw a box which show where the area which you click will be located.
            drawMouseBox(clientArea);
        }
    }

    GameTemplateView::doDraw(viewArea, clientArea);

} // end doDraw
// doIncreaseSize
//--------------------------------------------------------------------------
void MiniMapView::doIncreaseSize(int value)
{
    iXY destSize(getViewRect().getSize());

    if (value == -1) {
        float dt = TimerInterface::getTimeSlice();

        destSize += scaleDelta * dt;
    } else {
        destSize += value;
    }

    //resize(destSize);
    //deltaSize += deltaAmount;
    if (destSize > maxMapSize) {
        destSize = maxMapSize;
    }

    // Check the validity of the X dimension.
    if ((min.x + destSize.x) >= SCREEN_XPIX) {
        int xOffset = min.x + destSize.x - SCREEN_XPIX;

        int destXPos = min.x - xOffset;

        if (destXPos < 0) {
            moveTo(0, min.y);

        } else {
            moveTo(destXPos, min.y);
        }
    }

    // Check the validity of the Y dimension.
    if ((min.y + destSize.y) >= SCREEN_YPIX) {
        int yOffset = min.y + destSize.y - SCREEN_YPIX;

        int destYPos = min.y - yOffset;

        if (destYPos < 0) {
            moveTo(min.x, 0);

        } else {
            moveTo(min.x, destYPos);
        }
    }

    // Resize the x dimension.
    if (destSize.x > getViewRect().getSize().x) {
        if (destSize.x > maxMapSize) {
            resize(iXY(maxMapSize, getViewRect().getSize().y));

        } else {
            resize(iXY(destSize.x, getViewRect().getSize().y));
        }
    }

    // Resize the y dimension.
    if (destSize.y > getViewRect().getSize().y) {
        if (destSize.x > maxMapSize) {
            resize(iXY(getViewRect().getSize().x, maxMapSize));

        } else {
            resize(iXY(getViewRect().getSize().x, destSize.x));
        }
    }

    MiniMapInterface::setMapScale(getViewRect().getSize());

    needScale      = true;
    scaleGroupWait = 0.0f;

} // end MiniMapView::doIncreaseSize