Search in sources :

Example 1 with SpriteCharacterKey

use of com.glitchcog.fontificator.sprite.SpriteCharacterKey in project ChatGameFontificator by GlitchCog.

the class Message method processEmoji.

/**
     * Convert the content of the message into the appropriate emoji. Add those emoji and the remaining characters
     * between them to the specified keyList array.
     * 
     * @param content
     * @param privmsg
     * @param keyList
     *            The array to add the emoji and remaining characters to
     * @param emojiManager
     * @param emojiConfig
     * @param isManualMessage
     */
private static void processEmoji(String content, TwitchPrivmsg privmsg, List<SpriteCharacterKey> keyList, EmojiManager emojiManager, ConfigEmoji emojiConfig, boolean isManualMessage, MessageCasing casing) {
    Map<Integer, EmoteAndIndices> emotes = privmsg.getEmotes();
    String[] words = content.split(SPACE_BOUNDARY_REGEX);
    int codeIndex = 0;
    LazyLoadEmoji emoji = null;
    for (int w = 0; w < words.length; w++) {
        EmoteAndIndices eai = emotes.get(codeIndex);
        if (eai != null && emojiConfig.isTwitchEnabled()) {
            // This catches subscriber emotes and any non-global emotes
            emoji = emojiManager.getEmojiById(eai.getEmoteId(), words[w], emojiConfig);
            if (emoji == null) {
                // The already loaded Twitch V1 emoji map doesn't this emote ID yet, so add it
                try {
                    emoji = emojiManager.putEmojiById(eai.getEmoteId(), words[w], emojiConfig);
                } catch (MalformedURLException e) {
                    logger.error("Unable to load emote for emote ID " + eai.getEmoteId(), e);
                }
            }
        } else // At this point, only 3rd party emoji should be a possibility for this word (with the exception of
        // manual messages)
        {
            // This is the manual message exception
            if (isManualMessage) {
                // As a known bug here, all manual messages will have access to all Twitch emotes, regardless of
                // subscriber status
                emoji = emojiManager.getEmoji(EmojiType.MANUAL_EMOJI_TYPES, words[w], emojiConfig);
            } else // Only check 3rd party emotes
            {
                emoji = emojiManager.getEmoji(EmojiType.THIRD_PARTY_EMOJI_TYPES, words[w], emojiConfig);
            }
        }
        if (emoji == null) {
            keyList.addAll(toSpriteArray(applyCasing(words[w], casing)));
        } else {
            keyList.add(new SpriteCharacterKey(emoji, false));
        }
        // Increment the codeIndex by the current word's code point count
        // Emoji indexes are in code points, but java is counting in fixed 16-bit units
        codeIndex += words[w].codePointCount(0, words[w].length());
    }
}
Also used : LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) MalformedURLException(java.net.MalformedURLException) SpriteCharacterKey(com.glitchcog.fontificator.sprite.SpriteCharacterKey)

Example 2 with SpriteCharacterKey

use of com.glitchcog.fontificator.sprite.SpriteCharacterKey in project ChatGameFontificator by GlitchCog.

the class Message method toSpriteArray.

/**
     * Convert the string to a list of SpriteCharacterKeys by codepoint.
     *
     * @param str
     * @returns A List of SpriteCharacterKeys
     */
private static List<SpriteCharacterKey> toSpriteArray(String str) {
    // TODO java 1.8: We could use CharSequence and call codePoints method
    List<SpriteCharacterKey> keyList = new ArrayList<SpriteCharacterKey>();
    int i = 0;
    while (i < str.length()) {
        final int codepoint = str.codePointAt(i);
        keyList.add(new SpriteCharacterKey(codepoint));
        i += Character.charCount(codepoint);
    }
    return keyList;
}
Also used : ArrayList(java.util.ArrayList) SpriteCharacterKey(com.glitchcog.fontificator.sprite.SpriteCharacterKey)

Example 3 with SpriteCharacterKey

use of com.glitchcog.fontificator.sprite.SpriteCharacterKey in project ChatGameFontificator by GlitchCog.

the class Message method parseIntoText.

/**
     * Compile the array of SpriteCharacterKeys using the specified configuration. This can be a bit memory intensive
     * since each character is a new albeit small object, so this should not be done many times a second, rather only if
     * something has changed in the configuration to warrant a re-translation.
     * 
     * @param emojiManager
     * @param messageConfig
     * @param emojiConfig
     * @return spriteCharacterKeys
     */
private SpriteCharacterKey[] parseIntoText(EmojiManager emojiManager, ConfigMessage messageConfig, ConfigEmoji emojiConfig) {
    List<SpriteCharacterKey> keyList = new ArrayList<SpriteCharacterKey>();
    if (messageConfig.showTimestamps()) {
        String timeStampStr = messageConfig.getTimerFormatter().format(timestamp);
        timeStampStr = applyCasing(timeStampStr, messageConfig.getMessageCasing());
        keyList.addAll(toSpriteArray(timeStampStr));
    }
    // Add badges to be placed right before the username
    if (emojiConfig.isAnyBadgesEnabled()) {
        // LinkedHashMap to preserve original insert order
        badges = new LinkedHashMap<String, LazyLoadEmoji>();
        final boolean userIsModerator = privmsg.getUserType() == UserType.MOD;
        // Bank to pull Twitch badges from
        TypedEmojiMap twitchBadgeBank = emojiManager.getEmojiByType(EmojiType.TWITCH_BADGE);
        // Bank to pull FrankerFaceZ badges from
        TypedEmojiMap ffzBadgeBank = emojiManager.getEmojiByType(EmojiType.FRANKERFACEZ_BADGE);
        // Get the badge for the type of user, if the usertype has a badge (but not if it's an ffzBot)
        if (privmsg.getUserType() != null && privmsg.getUserType() != UserType.NONE) {
            LazyLoadEmoji testBadge;
            // FFZ badges are enabled, the user is a moderator, and the custom FFZ moderator badge exists
            if (emojiConfig.isFfzBadgesEnabled() && userIsModerator && ffzBadgeBank.getEmoji(UserType.MOD.getKey()) != null) {
                badges.put(UserType.MOD.getKey(), ffzBadgeBank.getEmoji(UserType.MOD.getKey()));
            } else if (emojiConfig.isTwitchBadgesEnabled() && (testBadge = twitchBadgeBank.getEmoji(privmsg.getUserType().getKey())) != null) {
                badges.put(privmsg.getUserType().getKey(), testBadge);
            }
        }
        LazyLoadEmoji replacementBadge = null;
        if (emojiConfig.isFfzBadgesEnabled()) {
            Map<Integer, Set<String>> ffzBadgeUsers = emojiManager.getFfzBadgeUsers();
            for (Integer ffzBadgeType : ffzBadgeUsers.keySet()) {
                final String ffzBadgeKey = ffzBadgeType == null ? null : Integer.toString(ffzBadgeType);
                Set<String> users = ffzBadgeUsers.get(ffzBadgeType);
                if (users.contains(username.toLowerCase())) {
                    LazyLoadEmoji ffzBadge = ffzBadgeBank.getEmoji(ffzBadgeType);
                    if (ffzBadge.isReplacement() && badges.containsKey(ffzBadge.getReplaces())) {
                        badges.put(ffzBadge.getReplaces(), ffzBadge);
                        if (userIsModerator && true) {
                            replacementBadge = ffzBadge;
                        }
                    } else {
                        badges.put(ffzBadgeKey, ffzBadge);
                    }
                }
            }
        }
        // Optional subscriber badge
        if (emojiConfig.isTwitchBadgesEnabled()) {
            final String subStr = "subscriber";
            if (privmsg.isSubscriber() && twitchBadgeBank.getEmoji(subStr) != null) {
                badges.put(subStr, twitchBadgeBank.getEmoji(subStr));
            }
        }
        // Optional turbo badge
        final String turboStr = "turbo";
        if (emojiConfig.isTwitchBadgesEnabled() && privmsg.isTurbo() && twitchBadgeBank.getEmoji(turboStr) != null) {
            badges.put(turboStr, twitchBadgeBank.getEmoji(turboStr));
        }
        final String primeStr = "prime";
        if (emojiConfig.isTwitchBadgesEnabled() && privmsg.isPrime() && twitchBadgeBank.getEmoji(primeStr) != null) {
            badges.put(primeStr, twitchBadgeBank.getEmoji(primeStr));
        }
        // Add each badges map item onto the sprite character key list
        for (LazyLoadEmoji lle : badges.values()) {
            SpriteCharacterKey sck = new SpriteCharacterKey(lle, true);
            if (userIsModerator && lle == replacementBadge) {
                sck.setEmojiBgColorOverride(ConfigEmoji.MOD_BADGE_COLOR);
            }
            keyList.add(sck);
        }
    }
    if (messageConfig.showUsernames()) {
        if (messageConfig.showTimestamps()) {
            keyList.addAll(toSpriteArray(TIMESTAMP_USERNAME_SPACER));
        }
        String casedUsername = applyCasing(username, messageConfig.getMessageCasing());
        keyList.addAll(toSpriteArray(casedUsername));
    }
    if (messageConfig.showUsernames() || messageConfig.showTimestamps() || (emojiConfig.isAnyBadgesEnabled() && badges != null && !badges.isEmpty())) {
        keyList.addAll(toSpriteArray(type.getContentBreaker()));
    }
    // Parse out the emoji, if enabled
    if (emojiConfig.isEmojiEnabled()) {
        processEmoji(content, privmsg, keyList, emojiManager, emojiConfig, MessageType.MANUAL.equals(type), messageConfig.getMessageCasing());
    } else // Configured for no emoji, so just chars
    {
        keyList.addAll(toSpriteArray(applyCasing(content, messageConfig.getMessageCasing())));
    }
    // Return the list as an array, to be kept until configuration is modified requiring a reprocessing
    return keyList.toArray(new SpriteCharacterKey[keyList.size()]);
}
Also used : LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) TypedEmojiMap(com.glitchcog.fontificator.emoji.TypedEmojiMap) Set(java.util.Set) ArrayList(java.util.ArrayList) SpriteCharacterKey(com.glitchcog.fontificator.sprite.SpriteCharacterKey)

Aggregations

SpriteCharacterKey (com.glitchcog.fontificator.sprite.SpriteCharacterKey)3 LazyLoadEmoji (com.glitchcog.fontificator.emoji.LazyLoadEmoji)2 ArrayList (java.util.ArrayList)2 TypedEmojiMap (com.glitchcog.fontificator.emoji.TypedEmojiMap)1 MalformedURLException (java.net.MalformedURLException)1 Set (java.util.Set)1