Skip to content

support optional text_background_color for cards #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions adafruit_pyoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,23 @@ def _display_text_for(self, card):
"""
text = card.get("text", None)
text_color = card.get("text_color", 0x0) # default to black
text_background_color = card.get("text_background_color", None)
if text:
try:
text_color = int(text_color) # parse the JSON string to hex int
except ValueError:
text_color = 0x0
self.set_text(text, text_color)

try:
text_background_color = int(
text_background_color
) # parse the JSON string to hex int
except ValueError:
text_background_color = None
except TypeError:
text_background_color = None

self.set_text(text, text_color, background_color=text_background_color)

def _play_sound_for(self, card):
"""If there's a sound, start playing it.
Expand Down Expand Up @@ -356,7 +367,7 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
self._wavfile = None
self._speaker_enable.value = False

def set_text(self, text, color):
def set_text(self, text, color, background_color=None):
"""Display the test for a card.

:param Union(None,str) text: the text to display
Expand Down Expand Up @@ -385,6 +396,8 @@ def set_text(self, text, color):
self._text.x = text_x
self._text.y = text_y
self._text.color = color
if background_color:
self._text.background_color = background_color
self._text_group.append(self._text)

def set_background(self, filename, *, with_fade=True):
Expand Down
1 change: 1 addition & 0 deletions examples/cyoa/cyoa.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"background_image": "page01.bmp",
"text": "You do not have any friends so you decide that it might be a good idea to build a robot friend. You're unsure if you want to do this, so now is the time to decide. Do you want to build a robot friend?",
"text_color": "0x000001",
"text_background_color": "0xeeeeee",
"sound": "sound_01.wav",
"button01_text": "Yes",
"button01_goto_card_id": "continue?",
Expand Down