You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					74 lines
				
				2.3 KiB
			
		
		
			
		
	
	
					74 lines
				
				2.3 KiB
			| 
								 
											6 months ago
										 
									 | 
							
								import pyray as rl
							 | 
						||
| 
								 
											4 months ago
										 
									 | 
							
								from openpilot.system.ui.lib.application import MousePos
							 | 
						||
| 
								 
											4 months ago
										 
									 | 
							
								from openpilot.system.ui.widgets import Widget
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								ON_COLOR = rl.Color(51, 171, 76, 255)
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								OFF_COLOR = rl.Color(0x39, 0x39, 0x39, 255)
							 | 
						||
| 
								 | 
							
								KNOB_COLOR = rl.WHITE
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								DISABLED_ON_COLOR = rl.Color(0x22, 0x77, 0x22, 255)  # Dark green when disabled + on
							 | 
						||
| 
								 | 
							
								DISABLED_OFF_COLOR = rl.Color(0x39, 0x39, 0x39, 255)
							 | 
						||
| 
								 | 
							
								DISABLED_KNOB_COLOR = rl.Color(0x88, 0x88, 0x88, 255)
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								WIDTH, HEIGHT = 160, 80
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								BG_HEIGHT = 60
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								ANIMATION_SPEED = 8.0
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								class Toggle(Widget):
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								  def __init__(self, initial_state=False):
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    super().__init__()
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								    self._state = initial_state
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    self._enabled = True
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    self._progress = 1.0 if initial_state else 0.0
							 | 
						||
| 
								 | 
							
								    self._target = self._progress
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								  def set_rect(self, rect: rl.Rectangle):
							 | 
						||
| 
								 | 
							
								    self._rect = rl.Rectangle(rect.x, rect.y, WIDTH, HEIGHT)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 months ago
										 
									 | 
							
								  def _handle_mouse_release(self, mouse_pos: MousePos):
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    if not self._enabled:
							 | 
						||
| 
								 
											4 months ago
										 
									 | 
							
								      return
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 months ago
										 
									 | 
							
								    self._state = not self._state
							 | 
						||
| 
								 | 
							
								    self._target = 1.0 if self._state else 0.0
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  def get_state(self):
							 | 
						||
| 
								 | 
							
								    return self._state
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								  def set_state(self, state: bool):
							 | 
						||
| 
								 | 
							
								    self._state = state
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    self._target = 1.0 if state else 0.0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def set_enabled(self, enabled: bool):
							 | 
						||
| 
								 | 
							
								    self._enabled = enabled
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def is_enabled(self):
							 | 
						||
| 
								 | 
							
								    return self._enabled
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								  def update(self):
							 | 
						||
| 
								 | 
							
								    if abs(self._progress - self._target) > 0.01:
							 | 
						||
| 
								 | 
							
								      delta = rl.get_frame_time() * ANIMATION_SPEED
							 | 
						||
| 
								 | 
							
								      self._progress += delta if self._progress < self._target else -delta
							 | 
						||
| 
								 | 
							
								      self._progress = max(0.0, min(1.0, self._progress))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								  def _render(self, rect: rl.Rectangle):
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    self.update()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if self._enabled:
							 | 
						||
| 
								 | 
							
								      bg_color = self._blend_color(OFF_COLOR, ON_COLOR, self._progress)
							 | 
						||
| 
								 | 
							
								      knob_color = KNOB_COLOR
							 | 
						||
| 
								 | 
							
								    else:
							 | 
						||
| 
								 | 
							
								      bg_color = self._blend_color(DISABLED_OFF_COLOR, DISABLED_ON_COLOR, self._progress)
							 | 
						||
| 
								 | 
							
								      knob_color = DISABLED_KNOB_COLOR
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    # Draw background
							 | 
						||
| 
								 | 
							
								    bg_rect = rl.Rectangle(self._rect.x + 5, self._rect.y + 10, WIDTH - 10, BG_HEIGHT)
							 | 
						||
| 
								 | 
							
								    rl.draw_rectangle_rounded(bg_rect, 1.0, 10, bg_color)
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    # Draw knob
							 | 
						||
| 
								 | 
							
								    knob_x = self._rect.x + HEIGHT / 2 + (WIDTH - HEIGHT) * self._progress
							 | 
						||
| 
								 | 
							
								    knob_y = self._rect.y + HEIGHT / 2
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								    rl.draw_circle(int(knob_x), int(knob_y), HEIGHT / 2, knob_color)
							 | 
						||
| 
								 
											6 months ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 months ago
										 
									 | 
							
								  def _blend_color(self, c1, c2, t):
							 | 
						||
| 
								 | 
							
								    return rl.Color(int(c1.r + (c2.r - c1.r) * t), int(c1.g + (c2.g - c1.g) * t), int(c1.b + (c2.b - c1.b) * t), 255)
							 |