Skip to content

Commit 875beea

Browse files
mainly feedback from last PR, plus improved clear context
1 parent 7dd058f commit 875beea

File tree

3 files changed

+63
-54
lines changed

3 files changed

+63
-54
lines changed

mpf/config_players/segment_display_player.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,23 @@ def _remove(self, instance_dict, key, display):
106106
del instance_dict[display][key]
107107

108108
def clear_context(self, context):
109-
"""Remove all texts."""
110-
instance_dict = self._get_instance_dict(context)
111-
for display, keys in instance_dict.items():
112-
for key in dict(keys).keys():
113-
self._remove(instance_dict=instance_dict,
114-
key=key, display=display)
109+
110+
##############
111+
# Remove all texts. Ignore what keys are available, that will be checked later in the segment display code.
112+
# Especially important for update_method replace since there are no keys.
113+
##############
114+
115+
instance_dict = self._get_instance_dict(context) # key of the dict is the display, the value is another dict
116+
117+
for display, keys_dict in instance_dict.items(): # keys_dict key is the show key, the value is a boolean (with yet unknown usage)
118+
if(keys_dict): #depending on the situation the keys_dict might be empty, still need to clear the display
119+
for key in dict(keys_dict).keys():
120+
display.clear_segment_display(key)
121+
if instance_dict[display][key] is not True:
122+
self.delay.remove(instance_dict[display][key])
123+
del instance_dict[display][key]
124+
else:
125+
display.clear_segment_display(None)
115126

116127
self._reset_instance_dict(context)
117128

mpf/devices/segment_display/segment_display.py

+37-33
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ async def _initialize(self):
8989

9090
self.size = self.config['size']
9191
self._default_color = [RGBColor(color) for color in self.config["default_color"][0:self.size]]
92-
if len(self._default_color) < self.size:
93-
self._default_color += [RGBColor("white")] * (self.size - len(self._default_color))
92+
93+
if (len(self._default_color)) == 1:
94+
self._default_color = self._default_color * self.size
95+
elif len(self._default_color) != self.size:
96+
self.warning_log("The amount of colors you specified for your text has to be either equal to "
97+
"the amount of digits in your display or equals 1. Your display has a size of %s and the "
98+
"amount of colors specified is %s. All display colors will be set to white.", self.size, len(self._default_color))
99+
self._default_color = [RGBColor("white")] * self.size
94100

95101
# configure hardware
96102
try:
@@ -148,11 +154,9 @@ def add_text_entry(self, text, color, flashing, flash_mask, transition, transiti
148154
149155
This will replace texts with the same key.
150156
"""
151-
152-
if len(color) == 0:
157+
if not color:
153158
color = self._current_state.text.get_colors()
154159

155-
156160
if self.config['update_method'] == "stack":
157161

158162

@@ -172,35 +176,19 @@ def add_text_entry(self, text, color, flashing, flash_mask, transition, transiti
172176
###############################
173177

174178
# Handle new and previous text
175-
if self._previous_text:
176-
previous_text = self._previous_text
177-
else:
178-
previous_text = ""
179+
previous_text = self._previous_text or ""
179180
self._previous_text = text # Save the new text as the next previous text
180181

181182
# Handle new and previous color
182-
if self._previous_color:
183-
previous_color = self._previous_color
184-
else:
185-
previous_color = self._default_color
183+
previous_color = self._previous_color or self._default_color
186184
self._previous_color = color # Save the new color as the next previous color
187185

188186
if transition or self._previous_transition_out:
189-
if transition: #if transition exists, then ignore transition_out of previous text/color
190-
transition_conf = TransitionManager.get_transition(self.size,
191-
self.config['integrated_dots'],
192-
self.config['integrated_commas'],
193-
self.config['use_dots_for_commas'],
194-
transition)
195-
elif self._previous_transition_out:
196-
transition_conf = TransitionManager.get_transition(self.size,
197-
self.config['integrated_dots'],
198-
self.config['integrated_commas'],
199-
self.config['use_dots_for_commas'],
200-
self._previous_transition_out)
201-
self._previous_transition_out = None # Once the transistion_out is played removed it that is not played in the next step again
202-
if transition_out: #in case transition_out is set we need to preserve it for the next step but only after the previous transition_out is in this step's config
203-
self._previous_transition_out = transition_out
187+
transition_conf = TransitionManager.get_transition(self.size,
188+
self.config['integrated_dots'],
189+
self.config['integrated_commas'],
190+
self.config['use_dots_for_commas'],
191+
transition or self._previous_transition_out)
204192

205193
#start transition
206194
self._start_transition(transition_conf, previous_text, text,
@@ -216,6 +204,12 @@ def add_text_entry(self, text, color, flashing, flash_mask, transition, transiti
216204
color)
217205
self._update_display(SegmentDisplayState(text, flashing, flash_mask))
218206

207+
###############################
208+
# Once the transistion_out is played, removed it that is not played in the next step again, but in case transition_out is set in the current step
209+
# then we need to preserve it for the next step but only after the previous step's transition_out is in this step's config (or the transition of the current step)
210+
###############################
211+
self._previous_transition_out = transition_out or None
212+
219213
def add_text(self, text: str, priority: int = 0, key: str = None) -> None:
220214
"""Add text to display stack.
221215
@@ -225,13 +219,23 @@ def add_text(self, text: str, priority: int = 0, key: str = None) -> None:
225219

226220
def remove_text_by_key(self, key: Optional[str]):
227221
"""Remove entry from text stack."""
228-
if self.config['update_method'] != "stack":
222+
if self.config['update_method'] == "stack":
223+
if key in self._text_stack:
224+
del self._text_stack[key]
225+
self._update_stack()
226+
else: # must be update_method replace, send empyt text since no key in that case
229227
self.add_text_entry("", self._previous_color, FlashingType.NO_FLASH, "", None, None, 100, key)
230-
return
231228

232-
if key in self._text_stack:
233-
del self._text_stack[key]
234-
self._update_stack()
229+
230+
def clear_segment_display(self, key: Optional[str]):
231+
"""Clear segment dispaly if context is removed from player."""
232+
233+
if self.config['update_method'] == "replace":
234+
self._stop_transition()
235+
self._previous_transition_out = None
236+
237+
self.remove_text_by_key(key)
238+
235239

236240
# pylint: disable=too-many-arguments
237241
def _start_transition(self, transition: TransitionBase, current_text: str, new_text: str,

mpf/devices/segment_display/segment_display_text.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,19 @@ def _embed_dots_and_commas(cls, text: str, collapse_dots: bool, collapse_commas:
7474
def _create_characters(cls, text: str, display_size: int, collapse_dots: bool, collapse_commas: bool,
7575
use_dots_for_commas: bool, colors: List[Optional[RGBColor]]) -> List[DisplayCharacter]:
7676
"""Create characters from text and color them.
77-
78-
- Colors are used from the left to the right (starting with the first character).
7977
- Dots and commas are embedded on the fly.
8078
- Text will be right aligned on the display, thus if text is shorter than display spaces will be padded before the text
81-
- Provided colors are assumed to be for the text itself, thus the list of colors is "right aligned" too
82-
- If list of colors is less than the display size the list will be extended with white as default color on the left endswith
83-
so that the provided colors are for the text and not being used for invisible spaces
79+
- If list of colors is less than the display size then all white will be used, if only one color is given that will be used for the full display
8480
"""
8581
char_list = []
8682
uncolored_chars = cls._embed_dots_and_commas(text, collapse_dots, collapse_commas, use_dots_for_commas)
8783

8884
# Adujust the color array if needed
89-
color_length = len(colors)
90-
if color_length > display_size:
91-
for _ in range(color_length - display_size):
92-
colors.pop(0) # remove very left color of array if too long
93-
elif color_length < display_size:
94-
for _ in range(display_size - color_length):
95-
colors.append(RGBColor("white")) # add default color on the left of the array if too few colors
85+
if (len(colors)) == 1:
86+
colors = colors * display_size
87+
elif len(colors) != display_size:
88+
#TODO: Log that colors were adjusted to white as default
89+
colors = [RGBColor("white")] * display_size
9690

9791
# ensure list is the same size as the segment display (cut off on left if too long or right justify characters if too short)
9892
current_length = len(uncolored_chars)
@@ -103,9 +97,9 @@ def _create_characters(cls, text: str, display_size: int, collapse_dots: bool, c
10397
for _ in range(display_size - current_length):
10498
uncolored_chars.insert(0, (SPACE_CODE, False, False))
10599

106-
for _ in range(len(uncolored_chars)):
107-
color = colors[_]
108-
char_list.append(DisplayCharacter(uncolored_chars[_][0], uncolored_chars[_][1], uncolored_chars[_][2], color))
100+
for i, char in enumerate(uncolored_chars):
101+
color = colors[i]
102+
char_list.append(DisplayCharacter(char[0], char[1], char[2], color)) #0: char code 1: char_has_dot 2: char_has_comma
109103

110104
return char_list
111105

0 commit comments

Comments
 (0)