Exemplo n.º 1
0
 def _finish_stopwatch(self):
     '''
     Finish the current stopwatch
     '''
     self._flash_count = 0
     self.ids.speed_rect.rect_color = ColorScheme.get_dark_background_translucent()
     self.exit_speed_color = ColorScheme.get_dark_background_translucent()
     self._set_exit_speed_frame_visible(False)
     Clock.schedule_once(self._flash_pit_stop_time)
     self._stop_stopwatch()
Exemplo n.º 2
0
 def _update_speed_color(self):
     '''
     Selects the appropriate color for the exit speed indicator based on 
     configured speed threshold
     '''
     if self.current_speed > self._pitstop_alert_speed:
         self.ids.speed_rect.rect_color = ColorScheme.get_error()
         self._toggle_exit_speed_alert(ColorScheme.get_error())
     elif self.current_speed > self._pitstop_alert_speed * self._SPEED_ALERT_THRESHOLD:
         self.ids.speed_rect.rect_color = ColorScheme.get_alert()
         self.exit_speed_color = ColorScheme.get_dark_background_translucent()
     else:
         self.ids.speed_rect.rect_color = ColorScheme.get_happy()
         self.exit_speed_color = ColorScheme.get_dark_background_translucent()
 def on_current_channel(self, instance, value):
     icon = ''
     if value == True:
         icon = '\357\200\224'
         self.highlight_color = ColorScheme.get_primary()
     else:
         self.highlight_color = ColorScheme.get_dark_background_translucent()
     self.ids.action_button.text = icon
Exemplo n.º 4
0
 def _toggle_exit_speed_alert(self, alert_color):
     '''
     Toggles the background color for the exit speed frame
     '''
     if self.exit_speed_color == alert_color:
         self.exit_speed_color = ColorScheme.get_dark_background_translucent()
     else:
         self.exit_speed_color = alert_color
Exemplo n.º 5
0
 def on_current_channel(self, instance, value):
     icon = ''
     if value == True:
         icon = '\357\200\224'
         self.highlight_color = ColorScheme.get_primary()
     else:
         self.highlight_color = ColorScheme.get_dark_background_translucent(
         )
     self.ids.action_button.text = icon
Exemplo n.º 6
0
class ChannelSelection(BoxLayout):
    highlight_color = ListProperty(
        ColorScheme.get_dark_background_translucent())
    current_channel = BooleanProperty(False)

    def __init__(self, **kwargs):
        super(ChannelSelection, self).__init__(**kwargs)
        self.channel = kwargs.get('channel')
        self.register_event_type('on_delete_channel')
        self.register_event_type('on_add_channel')
        self.ids.name.text = self.channel

    def on_label_add(self, *args):
        if self.current_channel == False:
            self.dispatch('on_add_channel', self.channel)

    def on_delete_channel(self, *args):
        pass

    def on_add_channel(self, *args):
        pass

    def on_current_channel(self, instance, value):
        icon = ''
        if value == True:
            icon = '\357\200\224'
            self.highlight_color = ColorScheme.get_primary()
        else:
            self.highlight_color = ColorScheme.get_dark_background_translucent(
            )
        self.ids.action_button.text = icon

    def on_action(self, *args):
        if self.current_channel == True:
            self.dispatch('on_delete_channel', self.channel)
        else:
            self.dispatch('on_add_channel', self.channel)
Exemplo n.º 7
0
class PitstopTimerView(BoxLayout, SettingsListener):
    '''
    Provides a pit stop timer that automatically activates when certain speed
    thresholds are met. 
    '''
    _STOPWATCH_TICK = 0.1
    _FLASH_INTERVAL = 0.25
    _FLASH_COUNT = 10
    _EXIT_SPEED_CONTAINER_SIZE_HINT = 0.3
    _TIMER_WIDGET_SIZE_HINT = 0.4
    _SPEED_ALERT_THRESHOLD = 0.85
    _POPUP_SIZE_HINT = (0.75, 0.8)

    exit_speed_color = ListProperty(ColorScheme.get_dark_background_translucent())
    title = StringProperty('')
    current_time = StringProperty(STOPWATCH_NULL_TIME)
    exit_speed = StringProperty('')
    current_speed = NumericProperty(None)
    current_lap = NumericProperty(None)

    Builder.load_string(STOPWATCH_LAYOUT)

    def __init__(self, databus, title='Pit Stop', **kwargs):
        super(PitstopTimerView, self).__init__(**kwargs)
        self.title = title
        self.databus = databus
        self._popup = None
        self._flash_count = 0
        self._start_time = 0.0
        self._currently_racing = False
        self._pitstop_trigger_speed = 0
        self._pitstop_exit_speed = 0
        self._pitstop_alert_speed = 0
        databus.addChannelListener(STOPWATCH_SPEED_CHANNEL, self.set_speed)
        databus.addChannelListener(STOPWATCH_CURRENT_LAP, self.set_current_lap)

    def _set_exit_speed_frame_visible(self, visible):
        '''
        Shows or hides the frame containing the exit speed indicator
        :param visible true if exit speed frame should be visible
        :type visible
        '''
        container = self.ids.exit_speed_frame_container
        frame_visible = self.exit_speed_frame in container.children
        if visible:
            if not frame_visible:
                container.add_widget(self.exit_speed_frame)
        else:
            if frame_visible:
                container.remove_widget(self.exit_speed_frame)

    def _format_stopwatch_time(self):
        '''
        Format current laptime for display
        '''
        return format_laptime(self._current_time / 60.0)

    def _toggle_exit_speed_alert(self, alert_color):
        '''
        Toggles the background color for the exit speed frame
        '''
        if self.exit_speed_color == alert_color:
            self.exit_speed_color = ColorScheme.get_dark_background_translucent()
        else:
            self.exit_speed_color = alert_color

    def _update_speed_color(self):
        '''
        Selects the appropriate color for the exit speed indicator based on 
        configured speed threshold
        '''
        if self.current_speed > self._pitstop_alert_speed:
            self.ids.speed_rect.rect_color = ColorScheme.get_error()
            self._toggle_exit_speed_alert(ColorScheme.get_error())
        elif self.current_speed > self._pitstop_alert_speed * self._SPEED_ALERT_THRESHOLD:
            self.ids.speed_rect.rect_color = ColorScheme.get_alert()
            self.exit_speed_color = ColorScheme.get_dark_background_translucent()
        else:
            self.ids.speed_rect.rect_color = ColorScheme.get_happy()
            self.exit_speed_color = ColorScheme.get_dark_background_translucent()

    def _tick_stopwatch(self, *args):
        '''
        Increment the current stopwatch time
        '''
        self._current_time = time() - self._start_time
        self.current_time = self._format_stopwatch_time()
        self.exit_speed = '{}'.format(int(self.current_speed))
        if self.current_speed > self._pitstop_trigger_speed:
            self._set_exit_speed_frame_visible(True)
        self._update_speed_color()

    def _stop_stopwatch(self):
        '''
        Stops the stopwatch timer
        '''
        Clock.unschedule(self._tick_stopwatch)
        self.current_time = STOPWATCH_NULL_TIME

    def _in_race_mode(self):
        '''
        Return True if we think we're in 'racing mode'
        '''
        return self.current_speed > self._pitstop_exit_speed and\
            self.current_lap > 0

    def user_preferences_updated(self, user_preferences):
        '''
        Update runtime values from user preferences
        '''
        self._pitstop_timer_enabled = user_preferences.get_pref_bool('dashboard_preferences', 'pitstoptimer_enabled')
        self._pitstop_trigger_speed = user_preferences.get_pref_int('dashboard_preferences', 'pitstoptimer_trigger_speed')
        self._pitstop_exit_speed = user_preferences.get_pref_int('dashboard_preferences', 'pitstoptimer_exit_speed')
        self._pitstop_alert_speed = user_preferences.get_pref_int('dashboard_preferences', 'pitstoptimer_alert_speed')

    def _start_stopwatch(self):
        '''
        Starts the stopwatch timer
        '''

        if not self._popup:
            self._popup = ModalView(size_hint=self._POPUP_SIZE_HINT, auto_dismiss=False)
            self._popup.add_widget(self)
        self._set_exit_speed_frame_visible(False)
        self._popup.open()
        self._current_time = 0.0
        self._start_time = time()
        self._flash_count = 0
        self._currently_racing = False
        Clock.schedule_interval(self._tick_stopwatch, self._STOPWATCH_TICK)

    def _is_popup_open(self):
        '''
        Indicates if current popup is open
        :return True if popup is currently open
        '''
        return self._popup and self._popup._window is not None

    def _flash_pit_stop_time(self, *args):
        '''
        Flashes the final pit stop time when complete
        '''
        self.current_time = self._format_stopwatch_time() if self._flash_count % 2 == 0 else ''
        self._flash_count += 1
        if self._flash_count < self._FLASH_COUNT * 2:
            Clock.schedule_once(self._flash_pit_stop_time, self._FLASH_INTERVAL)
        else:
            self._popup.dismiss()

    def _finish_stopwatch(self):
        '''
        Finish the current stopwatch
        '''
        self._flash_count = 0
        self.ids.speed_rect.rect_color = ColorScheme.get_dark_background_translucent()
        self.exit_speed_color = ColorScheme.get_dark_background_translucent()
        self._set_exit_speed_frame_visible(False)
        Clock.schedule_once(self._flash_pit_stop_time)
        self._stop_stopwatch()

    def check_popup(self):
        '''
        Check if we should pop-up the timer
        '''
        if not self._pitstop_timer_enabled:
            return

        if self._in_race_mode():
            self._currently_racing = True
        if self.current_speed < self._pitstop_trigger_speed and\
                self._currently_racing and\
                not self._is_popup_open():
            self._start_stopwatch()

        if self.current_speed > self._pitstop_exit_speed and\
                self._is_popup_open() and\
                self._flash_count == 0:
            self._finish_stopwatch()

    def on_current_speed(self, instance, value):
        self.check_popup()

    def on_current_lap(self, instance, value):
        self.check_popup()

    def set_speed(self, value):
        self.current_speed = value

    def set_current_lap(self, value):
        self.current_lap = value

    def dismiss(self):
        self._popup.dismiss()
        self._stop_stopwatch()

    def change_font_size(self):
        '''
        Auto resizes the timer widget if it spills over the edge of the bounding box
        '''
        widget = self.ids.timer
        try:
            if widget.texture_size[0] > widget.width:
                widget.font_size -= 1
        except Exception as e:
            Logger.warn('[PitstopTimerView] Failed to change font size: {}'.format(e))