bool MouseHelper::IsMouseInRect(RectangleWH rectangle)
{
    return
        previousMousePosition.GetX() >= rectangle.GetX() &&
        previousMousePosition.GetX() <= rectangle.GetX() + rectangle.GetWidth() &&
        previousMousePosition.GetY() >= rectangle.GetY() &&
        previousMousePosition.GetY() <= rectangle.GetY() + rectangle.GetHeight();
}
Esempio n. 2
0
Vector2 Crowd::GetCenterPoint()
{
    if (IsInteractionPointExact())
    {
        return interactionLocation;
    }

    RectangleWH clickPolygonBoundingBox = clickPolygon.GetBoundingBox();

    return Vector2(
        clickPolygonBoundingBox.GetX() + clickPolygonBoundingBox.GetWidth() / 2,
        clickPolygonBoundingBox.GetY() + clickPolygonBoundingBox.GetHeight() / 2);
}
RectangleWH FieldCharacter::GetBoundsForInteraction()
{
    if (IsInteractionPointExact())
    {
        return RectangleWH(interactionLocation.GetX(), interactionLocation.GetY(), 1, 1);
    }

    RectangleWH hitboxBoundingBox = GetHitBox()->GetBoundingBox();

    return RectangleWH(
        GetVectorAnchorPosition().GetX() + hitboxBoundingBox.GetX(),
        GetVectorAnchorPosition().GetY() + hitboxBoundingBox.GetY(),
        hitboxBoundingBox.GetWidth(),
        hitboxBoundingBox.GetHeight());
}
void Location::CutsceneView::CameraManipulatable::SetWindowRect(RectangleWH windowRect)
{
    RectangleWH adjustedWindowRect =
        RectangleWH(
                min(max(windowRect.GetX(), 0.0), backgroundSize.GetX() - gameWindowSize.GetX()),
                min(max(windowRect.GetY(), 0.0), backgroundSize.GetY() - gameWindowSize.GetY()),
                gameWindowSize.GetX(),
                gameWindowSize.GetY());

    if (adjustedWindowRect != this->windowRect)
    {
        this->windowRect = adjustedWindowRect;
        Update();

        emit WindowRectChanged(adjustedWindowRect);
    }
}
void Sprite::DrawClipped(Vector2 position, RectangleWH clipRect, bool flipHorizontally, Color color)
{
    if (GetSpriteSheetImage() == NULL)
    {
        return;
    }

    Vector2 pixelSnappedPosition = Vector2((int)position.GetX(), (int)position.GetY());

    GetSpriteSheetImage()->Draw(
        pixelSnappedPosition,
        RectangleWH(
            spriteClipRect.GetX() + clipRect.GetX(),
            spriteClipRect.GetY() + clipRect.GetY(),
            clipRect.GetWidth(),
            clipRect.GetHeight()),
        flipHorizontally,
        false /* flipVertically */,
        1.0,
        color);
}
SharedUtilsStringType ParseRawDialog(IDialogEventsOwner *pDialogEventsOwner, const SharedUtilsStringType &rawDialog, RectangleWH textAreaRect, double desiredPadding, SharedUtilsFontType dialogFont)
{
    double allowedWidth = textAreaRect.GetWidth() - desiredPadding * 2;
    SharedUtilsStringType fullString = "";
    SharedUtilsStringListType wordList = split(rawDialog, ' ');

    while (!wordList.empty())
    {
        SharedUtilsStringType curstring = "";
        double curTextWidth = 0;
        bool lineDone = false;
        bool addSpace = false;

        if (fullString.length() > 0)
        {
            fullString += "\n";
        }

        while (!lineDone)
        {
            SharedUtilsStringType stringToTest = (addSpace ? " " : "") + wordList.front();
            double curStringWidth = GetStringWidth(StripDialogEvents(stringToTest), dialogFont);

            // If we've got a single word that takes up more than the entire length of the screen,
            // then we need to split it up.
            if (curTextWidth == 0 && curStringWidth > allowedWidth)
            {
                SharedUtilsStringType testString = "";
                SharedUtilsStringType lastTestString = "";
                curStringWidth = 0;

                while (curStringWidth <= allowedWidth)
                {
                    if (stringToTest[0] == '{')
                    {
                        testString += stringToTest[0];
                        stringToTest = GetSubstring(stringToTest, 1);

                        while (stringToTest[0] != '}')
                        {
                            testString += stringToTest[0];
                            stringToTest = GetSubstring(stringToTest, 1);
                        }

                        testString += stringToTest[0];
                        stringToTest = GetSubstring(stringToTest, 1);
                    }

                    lastTestString = testString;
                    testString += stringToTest[0];
                    double testCurStringWidth = GetStringWidth(StripDialogEvents(testString), dialogFont);

                    if (testCurStringWidth > allowedWidth)
                    {
                        break;
                    }

                    curStringWidth = testCurStringWidth;
                    stringToTest = GetSubstring(stringToTest, 1);
                }

                wordList.insert(wordList.begin() + 1, stringToTest);
                stringToTest = lastTestString;
            }

            if (curTextWidth + curStringWidth <= allowedWidth)
            {
                SharedUtilsStringType stringToPrependOnNext;
                stringToTest = ParseDialogEvents(pDialogEventsOwner, static_cast<int>(fullString.length() + curstring.length()), stringToTest, &stringToPrependOnNext);
                curstring += stringToTest;
                curTextWidth += curStringWidth;
                wordList.pop_front();
                addSpace = true;

                if (wordList.empty())
                {
                    lineDone = true;
                }
                else
                {
                    wordList[0] = stringToPrependOnNext + wordList.front();
                }
            }
            else
            {
                lineDone = true;
            }
        }

        fullString += curstring;
    }

    return fullString;
}
void Sprite::DrawClipped(Vector2 position, RectangleWH clipRect, bool flipHorizontally, Color color)
{
    if (GetSpriteSheetImage() == NULL)
    {
        return;
    }

    // Adjust the clip rect to account for the fact that we've eliminated blank space
    // that was around the source image.
    if (spriteDrawOffset.GetX() > 0 || spriteDrawOffset.GetY() > 0)
    {
        Vector2 oldClipRectPosition(clipRect.GetX(), clipRect.GetY());

        clipRect.SetX(clipRect.GetX() - spriteDrawOffset.GetX());
        clipRect.SetY(clipRect.GetY() - spriteDrawOffset.GetY());

        position += Vector2(clipRect.GetX(), clipRect.GetY()) - oldClipRectPosition;

        if (clipRect.GetX() + clipRect.GetWidth() > GetWidth())
        {
            clipRect.SetWidth(GetWidth() - clipRect.GetX());
        }

        if (clipRect.GetY() + clipRect.GetHeight() > GetHeight())
        {
            clipRect.SetHeight(GetHeight() - clipRect.GetY());
        }
    }

    Vector2 pixelSnappedPosition =
        Vector2(
            (int)(position.GetX() + (originalSize.GetX() > 0 && flipHorizontally ? originalSize.GetX() - GetWidth() - spriteDrawOffset.GetX() : spriteDrawOffset.GetX())),
            (int)(position.GetY() + spriteDrawOffset.GetY()));

    GetSpriteSheetImage()->Draw(
        pixelSnappedPosition,
        RectangleWH(
            spriteClipRect.GetX() + clipRect.GetX(),
            spriteClipRect.GetY() + clipRect.GetY(),
            clipRect.GetWidth(),
            clipRect.GetHeight()),
        flipHorizontally,
        false /* flipVertically */,
        1.0,
        color);
}
void MLIFont::DrawInternal(const string &s, Vector2 position, Color color, double scale, RectangleWH clipRect)
{
    EnsureUIThread();

    // If we're trying to draw an empty string, we can just return -
    // we're not gonna draw anything anyhow.
    if (s.length() == 0)
    {
        return;
    }

    CheckScale();

    double x = position.GetX();
    double y = position.GetY();

    for (string::const_iterator it = s.begin(); it < s.end();)
    {
        uint32_t c = 0;
        if (!GetNextFromStringIterator(it, s.end(), &c))
        {
            break;
        }

        Image *pGlyphImage = cache[c];
        if (pGlyphImage == NULL)
        {
            continue;
        }

        RectangleWH characterClipRect(0, 0, pGlyphImage->width / GetFontScale(), pGlyphImage->height / GetFontScale());
        RectangleWH originalCharacterClipRect = characterClipRect;

        if (clipRect.GetWidth() < 0 || clipRect.GetX() < originalCharacterClipRect.GetWidth())
        {
            if (clipRect.GetWidth() >= 0)
            {
                if (clipRect.GetX() > 0)
                {
                    characterClipRect.SetX(originalCharacterClipRect.GetX() + clipRect.GetX());
                    characterClipRect.SetWidth(originalCharacterClipRect.GetWidth() - clipRect.GetX());
                }

                if (clipRect.GetWidth() < characterClipRect.GetWidth())
                {
                    characterClipRect.SetWidth(clipRect.GetWidth());
                }

                if (clipRect.GetY() > 0)
                {
                    characterClipRect.SetY(min(originalCharacterClipRect.GetY() + clipRect.GetY(), originalCharacterClipRect.GetY() + originalCharacterClipRect.GetHeight()));
                    characterClipRect.SetHeight(originalCharacterClipRect.GetHeight() - (characterClipRect.GetY() - originalCharacterClipRect.GetY()));
                }

                if (clipRect.GetHeight() < characterClipRect.GetHeight())
                {
                    characterClipRect.SetHeight(clipRect.GetHeight());
                }
            }

            if (characterClipRect.GetWidth() > 0 && characterClipRect.GetHeight() > 0)
            {
                characterClipRect.SetHeight(characterClipRect.GetHeight() * GetFontScale());
                characterClipRect.SetWidth(characterClipRect.GetWidth() * GetFontScale());
                pGlyphImage->Draw(Vector2(x, y), characterClipRect, false, false, scale, scale, color);
            }
        }

        double deltaX = pGlyphImage->width;

        if (it < s.end())
        {
            uint32_t c2 = 0;
            if (PeekNextFromStringIterator(it, s.end(), &c2))
            {
                deltaX = GetKernedWidth(c, c2);
            }
        }

        x += deltaX / GetFontScale();
    }
}