LuisMayo / objection_engine

Library that turns comment chains into ace attorney scenes, used in several bots
MIT License
105 stars 20 forks source link

Color chunks of text red, green, or blue + fixed RTL rendering #91

Closed Meorge closed 1 year ago

Meorge commented 1 year ago

This pull request adds the ability to use HTML-like formatting to color dialogue text the red, green, or blue colors from the games (#41). See example.py and the rendered video below for an example. RTL text rendering is also fixed: it originates from the right side of the textbox and moves left, instead of originating from the left.

https://user-images.githubusercontent.com/9957987/200093808-d65bda2b-c5c3-417d-b575-8b9d024dc9b6.mp4

LuisMayo commented 1 year ago

This is huge. I'll take a look in the following days. I'm looking forward to seeing this merged. Thanks!

Meorge commented 1 year ago

Welp, I was hoping nothing like this would pop up, but such is my luck...

It appears adding a line break with \n causes the text color indices to get misaligned:

https://user-images.githubusercontent.com/9957987/200228913-07e67f44-847e-4323-8694-5b619d794652.mp4

from objection_engine.renderer import render_comment_list
from objection_engine.beans.comment import Comment
from time import time

comments = [
    Comment(user_name = 'Phoenix', text_content="The <red>text up here works</red>. But\nI just <red>did a line break</red>"),
]

render_comment_list(comments, f'test-line-breaks-{str(int(time()))}.mp4')

I'll have to look and think over how to handle this sometime in the coming days, as it's pretty late for me right now and I doubt I'll have much success with it when I'm tired. Maybe stripping \n characters from the string while we're finding tag indices would be sufficient??

LuisMayo commented 1 year ago

The line breaks thing is unfortunate I agree :( I hope it's not hard to fix I don't think it should be worst case scenario we internally duplicate the color tag on line break

Meorge commented 1 year ago

How common/expected are user-inserted line breaks? I (think I) just implemented a quick fix for the issue: basically just replacing \n characters with a single space. This unfortunately means that the locations of line breaks would be entirely up to the wrapping algorithm. On the upside, this prevents strange behavior from a user inserting a bunch of newlines into a single box.

Using the same code as above, with the fix the rendered video looks like this:

https://user-images.githubusercontent.com/9957987/200348751-f6d9d595-b668-484e-8709-88db978861ce.mp4

Meorge commented 1 year ago

Also, something I might do in a follow-up pull request: I just found out that apparently Python has a built-in HTML parser that could simplify the tag-parsing code significantly, as well as add additional functionality (parameters for tags etc).

https://docs.python.org/3/library/html.parser.html

Don't know why this didn't come up in my research before I wrote my tag parser, but better late then never I suppose? 😅

LuisMayo commented 1 year ago

I was giving a thought to the "replace newline by spaces" idea. At first I didn't like tampering with the user input. But later I found that newlines were being rendered improperly anyway (vertical overflow) so substituting them by spaces would actually fix two of our problems in one commit!

I'll give it another couple tests to see how this goes.

Thanks!

LuisMayo commented 1 year ago

With(out) your permission I have moved all tests to a folder. That being said this is good to go. Merged!

Thanks tremendously for your help.

Fixes #41