How to color speech bubbles

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

How to color speech bubbles

#1 Post by jeffster »

A relatively new functionality in Ren'Py is "speech bubbles".
If we want to customize them, for example, to have
Different colour speech bubble per character,
how to do that?
Image

TL;DR:
Add this code to one of your .rpy files:

Code: Select all

init python:
    def bub_custom(tag):
        """ bubble.properties_callback to use custom properties for particular tags """
        possible = [
            '_'.join(("bottom_left", tag)),
            '_'.join(("bottom_right", tag)),
            '_'.join(("top_left", tag)),
            '_'.join(("top_right", tag)),
            '_'.join(("thought", tag))
            ]
        actual = []
        for i in possible:
            if i in bubble.properties:

                # If custom properties were set for this tag, use them
                actual.append(i)
        if actual:
            return actual

        # Otherwise use the default properties
        return ["bottom_left", "bottom_right", "top_left", "top_right", "thought"]

define bubble.properties_callback = bub_custom

init 1 python:
    # Loop through "tints" dictionary and for every tag there set custom properties
    for tag, tint in tints.items():
        mtrx = TintMatrix(tint)

        ckey = '_'.join(("bottom_left", tag))
        bubble.properties[ckey] = bubble.properties["bottom_left"].copy()
        bubble.properties[ckey]["window_background"] = Transform(
            bubble.frame, xzoom=1, yzoom=1, matrixcolor=mtrx)

        ckey = '_'.join(("bottom_right", tag))
        bubble.properties[ckey] = bubble.properties["bottom_right"].copy()
        bubble.properties[ckey]["window_background"] = Transform(
            bubble.frame, xzoom=-1, yzoom=1, matrixcolor=mtrx)

        ckey = '_'.join(("top_left", tag))
        bubble.properties[ckey] = bubble.properties["top_left"].copy()
        bubble.properties[ckey]["window_background"] = Transform(
            bubble.frame, xzoom=1, yzoom=-1, matrixcolor=mtrx)

        ckey = '_'.join(("top_right", tag))
        bubble.properties[ckey] = bubble.properties["top_right"].copy()
        bubble.properties[ckey]["window_background"] = Transform(
            bubble.frame, xzoom=-1, yzoom=-1, matrixcolor=mtrx)

        ckey = '_'.join(("thought", tag))
        bubble.properties[ckey] = {
            "window_background": Transform(bubble.thoughtframe, matrixcolor=mtrx)
            }
Usage:

PS. The second version (with different syntax) see in the next post.

For every character you want to color the speech bubble, add its
"image tag": "color code"
to dictionary "tints", like this:

Code: Select all

define e = Character(None, image="eileen",  kind=bubble)
define l = Character(None, image="lucy",    kind=bubble)
define a = Character(None, image="amanda",  kind=bubble)

define tints = {
    "eileen":   "#FCC",
    "lucy":     "#CFC",
    "amanda":   "#CCF",
    }
Then speech bubbles
for "eileen" will be pink ("#FCC"),
for "lucy" green(ish) ("#CFC"),
for "amanda" blue ("#CCF").

That's it.

Explanation:

In screens.rpy, there is a dict "bubble.properties".
https://renpy.org/doc/html/bubble.html

The code presented here (see "init 1 python" part) loops through "tints" dict and adds custom properties to "bubble.properties".

For every key in "tints" it adds "bottom_left_[key]", "bottom_right_[key]" and so on.

Those custom properties show the same default bubble.frame as usual, but transformed with "matrixcolor", with the tint that you have set in "tints".
https://renpy.org/doc/html/matrixcolor.html

And when a character speaks, bubble.properties_callback function makes the bubble use those custom properties.
https://renpy.org/doc/html/bubble.html# ... s_callback

Of course with a little modification of the code you can set other properties in a similar manner (e.g. quite different backgrounds for bubbles etc.).
Attachments
bubbles.jpg
(77.85 KiB) Not downloaded yet
Last edited by jeffster on Tue Mar 26, 2024 5:24 am, edited 1 time in total.

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: How to color speech bubbles

#2 Post by jeffster »

Here's the second version, with a bit simpler syntax.

Usage:

To define bubble speech characters with colored bubbles, add tint to definitions:

Code: Select all

define e = Character(None, image="eileen",  kind=bubble, tint="#FAC")
define l = Character(None, image="lucy",    kind=bubble, tint="#CEC")
define a = Character(None, image="amanda",  kind=bubble, tint="#BAD")
define n = Character(None, image="none",    kind=bubble) # <- this is not colored, default bubble
Then this code would search store for variables of bubble.BubbleCharacter type, look for "tint" value and if found, set bubble.properties like in the previous version. The code:

Code: Select all

init 1 python:
    bub_chars = [x for x in store.__dir__() if isinstance(
                getattr(store, x), bubble.BubbleCharacter)]

    for bub_char in bub_chars:

        bub_char_obj = getattr(store, bub_char)

        if 'tint' in bub_char_obj.who_args:
            mtrx = TintMatrix(bub_char_obj.who_args['tint'])
            tag = bub_char_obj.image_tag

            ckey = '_'.join(("bottom_left", tag))
            bubble.properties[ckey] = bubble.properties["bottom_left"].copy()
            bubble.properties[ckey]["window_background"] = Transform(
                bubble.frame, xzoom=1, yzoom=1, matrixcolor=mtrx)

            ckey = '_'.join(("bottom_right", tag))
            bubble.properties[ckey] = bubble.properties["bottom_right"].copy()
            bubble.properties[ckey]["window_background"] = Transform(
                bubble.frame, xzoom=-1, yzoom=1, matrixcolor=mtrx)

            ckey = '_'.join(("top_left", tag))
            bubble.properties[ckey] = bubble.properties["top_left"].copy()
            bubble.properties[ckey]["window_background"] = Transform(
                bubble.frame, xzoom=1, yzoom=-1, matrixcolor=mtrx)

            ckey = '_'.join(("top_right", tag))
            bubble.properties[ckey] = bubble.properties["top_right"].copy()
            bubble.properties[ckey]["window_background"] = Transform(
                bubble.frame, xzoom=-1, yzoom=-1, matrixcolor=mtrx)

            ckey = '_'.join(("thought", tag))
            bubble.properties[ckey] = {
                "window_background": Transform(bubble.thoughtframe, matrixcolor=mtrx)
                }

init python:

    def bub_custom(tag):
        possible = [
            '_'.join(("bottom_left", tag)),
            '_'.join(("bottom_right", tag)),
            '_'.join(("top_left", tag)),
            '_'.join(("top_right", tag)),
            '_'.join(("thought", tag))
            ]
        actual = []
        for i in possible:
            if i in bubble.properties:
                actual.append(i)
        if actual:
            return actual
        return ["bottom_left", "bottom_right", "top_left", "top_right", "thought"]

define bubble.properties_callback = bub_custom

RewindTheGame
Regular
Posts: 62
Joined: Thu Dec 31, 2020 3:37 pm
Contact:

Re: How to color speech bubbles

#3 Post by RewindTheGame »

Well... I've just got through writing my own multicolour bubble code so now I want to slap myself in the face for not waiting. Still, at least I exercised a few brain cells.

Out of interest, does this require the latest version of RenPy or will it work in 8.1.3? I know all the bubble functionality wasn't initially present. The reason I ask is because I'm currently having a few issues upgrading to the latest version as it throws a few errors I'm not sure how to correct atm.

RewindTheGame
Regular
Posts: 62
Joined: Thu Dec 31, 2020 3:37 pm
Contact:

Re: How to color speech bubbles

#4 Post by RewindTheGame »

Just answered my own question - it doesn't work. Just an excuse to work out what's changed in the latest version of RenPy that messes with my code and fix it so I can upgrade, I suppose. I also hate Visual Studio with the intensity of a thousand suns and it won't let me choose Atom which may be old but is what I'm used to. But that's my problem.

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: How to color speech bubbles

#5 Post by jeffster »

RewindTheGame wrote: Tue Mar 26, 2024 12:35 pm Just answered my own question - it doesn't work. Just an excuse to work out what's changed in the latest version of RenPy that messes with my code and fix it so I can upgrade, I suppose. I also hate Visual Studio with the intensity of a thousand suns and it won't let me choose Atom which may be old but is what I'm used to. But that's my problem.
You can choose "System editor" and associate any editor you want with .rpy files, and Ren'Py Launcher would use that one. Me, I just edit files in folders opening them from file manager, so Ren'Py settings don't even matter.

Post Reply

Who is online

Users browsing this forum: No registered users