Skip to content

Adjusted sizing to add support for PyPortal Titano #16

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 2 commits into from
Dec 20, 2019
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
69 changes: 47 additions & 22 deletions adafruit_pyoa.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
from digitalio import DigitalInOut
import displayio
import adafruit_touchscreen
from adafruit_cursorcontrol.cursorcontrol import Cursor
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
try: # No need for Cursor Control on the PyPortal
from adafruit_cursorcontrol.cursorcontrol import Cursor
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
except ImportError:
pass
import audioio
from adafruit_display_text.label import Label
from adafruit_button import Button
Expand All @@ -66,14 +69,18 @@ class PYOA_Graphics():

def __init__(self):
self.root_group = displayio.Group(max_size=15)

self._display = board.DISPLAY
self._background_group = displayio.Group(max_size=1)
self.root_group.append(self._background_group)
self._text_group = displayio.Group(max_size=1)
self.root_group.append(self._text_group)
self._button_group = displayio.Group(max_size=2)
self.root_group.append(self._button_group)

if self._display.height > 250:
self._text_group.scale = 2
self._button_group.scale = 2

self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
self._speaker_enable.switch_to_output(False)
if hasattr(board, 'AUDIO_OUT'):
Expand All @@ -86,19 +93,20 @@ def __init__(self):
self._background_file = None
self._wavfile = None

board.DISPLAY.auto_brightness = False
self._display.auto_brightness = False
self.backlight_fade(0)
board.DISPLAY.show(self.root_group)
self._display.show(self.root_group)
self.touchscreen = None
self.mouse_cursor = None
if hasattr(board, 'TOUCH_XL'):
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
board.TOUCH_YD, board.TOUCH_YU,
calibration=((5200, 59000),
(5800, 57000)),
size=(320, 240))
size=(self._display.width,
self._display.height))
elif hasattr(board, 'BUTTON_CLOCK'):
self.mouse_cursor = Cursor(board.DISPLAY, display_group=self.root_group, cursor_speed=8)
self.mouse_cursor = Cursor(self._display, display_group=self.root_group, cursor_speed=8)
self.cursor = CursorManager(self.mouse_cursor)
else:
raise AttributeError('PYOA requires a touchscreen or cursor.')
Expand All @@ -120,20 +128,33 @@ def load_game(self, game_directory):
"""
self._gamedirectory = game_directory
self._text_font = terminalio.FONT
# Possible Screen Sizes are:
# 320x240 PyPortal and PyPortal Pynt
# 160x128 PyBadge and PyGamer
# 480x320 PyPortal Titano
# 240x240 if we wanted to use HalloWing M4

# Button Attributes
btn_left = 10
btn_right = btn_left+180
btn_mid = btn_left+90
button_y = 195
button_width = 120
button_height = 40
if board.DISPLAY.height < 200:
if self._display.height < 200:
button_y /= 2
button_y += 10
button_width /= 2
button_height /= 2
btn_right /= 2
btn_mid /= 2
elif self._display.height > 250:
button_y *= .75
button_y -= 20
button_width *= .75
button_height *= .75
btn_right *= .75
btn_mid *= .75
self._left_button = Button(x=int(btn_left), y=int(button_y), width=int(button_width), height=int(button_height),
label="Left", label_font=self._text_font,
style=Button.SHADOWROUNDRECT)
Expand Down Expand Up @@ -232,6 +253,8 @@ def _wait_for_press(self, card):
if self.cursor.is_clicked is True:
point_touched = self.mouse_cursor.x, self.mouse_cursor.y
if point_touched is not None:
point_touched = (point_touched[0] // self._button_group.scale,
point_touched[1] // self._button_group.scale)
print("touch: ", point_touched)
if button01_text and not button02_text:
# showing only middle button
Expand Down Expand Up @@ -264,10 +287,10 @@ def display_card(self, card_num):
self.backlight_fade(1.0)
self._display_text_for(card)
try:
board.DISPLAY.refresh(target_frames_per_second=60)
self._display.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
self._display.refresh_soon()
self._display.wait_for_frame()

self._play_sound_for(card)

Expand Down Expand Up @@ -305,9 +328,9 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
filename = self._gamedirectory+"/"+filename
print("Playing sound", filename)
try:
board.DISPLAY.refresh(target_frames_per_second=60)
self._display.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.wait_for_frame()
self._display.wait_for_frame()
try:
self._wavfile = open(filename, "rb")
except OSError:
Expand Down Expand Up @@ -336,16 +359,18 @@ def set_text(self, text, color):
if not text or not color:
return # nothing to do!
text_wrap = 37
if board.DISPLAY.height < 130:
if self._display.height < 130:
text_wrap = 25
text = self.wrap_nicely(text, text_wrap)
text = '\n'.join(text)
print("Set text to", text, "with color", hex(color))
text_x = 8
text_y = 95
if board.DISPLAY.height < 130:
text_y = 38
if self._display.height < 130:
text_x = 3
text_y = 38
elif self._display.height > 250:
text_y = 50
if text:
self._text = Label(self._text_font, text=str(text))
self._text.x = text_x
Expand Down Expand Up @@ -375,26 +400,26 @@ def set_background(self, filename, *, with_fade=True):
self._background_group.append(self._background_sprite)
if with_fade:
try:
board.DISPLAY.refresh(target_frames_per_second=60)
self._display.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
self._display.refresh_soon()
self._display.wait_for_frame()
self.backlight_fade(1.0)

def backlight_fade(self, to_light):
"""Adjust the TFT backlight. Fade from one value to another
"""
from_light = board.DISPLAY.brightness
from_light = self._display.brightness
from_light = int(from_light*100)
to_light = max(0, min(1.0, to_light))
to_light = int(to_light*100)
delta = 1
if from_light > to_light:
delta = -1
for val in range(from_light, to_light, delta):
board.DISPLAY.brightness = val/100
self._display.brightness = val/100
time.sleep(0.003)
board.DISPLAY.brightness = to_light/100
self._display.brightness = to_light/100


# return a list of lines with wordwrapping
Expand Down
Binary file added examples/cyoa_titano/Mystery.wav
Binary file not shown.
74 changes: 74 additions & 0 deletions examples/cyoa_titano/cyoa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[
{
"card_id": "startup",
"background_image": "startup.bmp",
"sound": "startup.wav",
"auto_advance": "5"
},
{
"card_id": "home",
"background_image": "home.bmp",
"sound": "home.wav",
"sound_repeat": "True",
"button01_text": "Help",
"button01_goto_card_id": "help",
"button02_text": "Start",
"button02_goto_card_id": "want to build?"
},

{
"card_id": "want to build?",
"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",
"sound": "sound_01.wav",
"button01_text": "Yes",
"button01_goto_card_id": "continue?",
"button02_text": "No",
"button02_goto_card_id": "lazy"
},
{
"card_id": "continue?",
"background_image": "page02.bmp",
"text": "You spend all day, then all week, then all month building a robot, everyone stops talking to you, however a lot of progress has been made. Do you want to keep making the robots?",
"text_color": "0xFFFFFF",
"button01_text": "Yes",
"button01_goto_card_id": "robot friend",
"button02_text": "No",
"button02_goto_card_id": "lazy"
},
{
"card_id": "robot friend",
"background_image": "page03.bmp",
"text": "The robot is now you're friend, everyone else wishes they had a robot, this is the best thing ever. Good work!",
"text_color": "0xFFFFFF",
"sound": "Mystery.wav",
"button01_text": "Next",
"button01_goto_card_id": "happy ending"
},
{
"card_id": "lazy",
"background_image": "page04.bmp",
"sound": "sound_04.wav",
"text": "Welp, not only will you not have any friends, you are lazy. What's the point of playing? Try again.",
"text_color": "0xFFFFFF",
"button01_text": "Start Over",
"button01_goto_card_id": "home"
},
{
"card_id": "help",
"background_image": "help.bmp",
"text": "All you need to do is click the buttons, that's it.\nThis is a new line.",
"text_color": "0xFFFFFF",
"button01_text": "Home",
"button01_goto_card_id": "home"
},
{
"card_id": "happy ending",
"background_image": "happyending.bmp",
"sound": "happy_ending.wav",
"sound_repeat": "True",
"button01_text": "Home",
"button01_goto_card_id": "home"
}
]
Loading