-
Notifications
You must be signed in to change notification settings - Fork 144
Add badge hardware frame overlay to simulator #94
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
Open
keith-oak
wants to merge
1
commit into
badger:main
Choose a base branch
from
keith-oak:simulator-badge-frame
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -806,14 +806,50 @@ def transformed_point(self, x: float, y: float): | |||||||||
|
|
||||||||||
|
|
||||||||||
| class Screen(_SurfaceTarget): | ||||||||||
| def __init__(self, width: int = 160, height: int = 120, scale: int = 4, screenshot_dir: str = None) -> None: | ||||||||||
| # Badge frame LCD coordinates (in badge_frame.png at 856x1280) | ||||||||||
| _FRAME_LCD_X = 130 | ||||||||||
| _FRAME_LCD_Y = 715 | ||||||||||
| _FRAME_LCD_W = 560 | ||||||||||
| _FRAME_LCD_H = 430 | ||||||||||
| _FRAME_BUTTONS = [ | ||||||||||
| (240, 1145, 62, 55, "A"), | ||||||||||
| (384, 1145, 62, 55, "B"), | ||||||||||
| (528, 1145, 62, 55, "C"), | ||||||||||
| (688, 858, 44, 54, "^"), | ||||||||||
| (688, 1002, 44, 54, "v"), | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| def __init__(self, width: int = 160, height: int = 120, scale: int = 4, screenshot_dir: str = None, frame: bool = False) -> None: | ||||||||||
| self.width = width | ||||||||||
| self.height = height | ||||||||||
| self.scale = scale | ||||||||||
| self.screenshot_dir = screenshot_dir | ||||||||||
| self._screenshot_counter = 0 | ||||||||||
| # Add space below for keyboard hints (30 pixels) | ||||||||||
| self._window = pygame.display.set_mode((width * scale, height * scale + 30)) | ||||||||||
| self._badge_frame = None | ||||||||||
|
|
||||||||||
| if frame: | ||||||||||
| frame_path = os.path.join(os.path.dirname(__file__), "badge_frame.png") | ||||||||||
| if os.path.exists(frame_path): | ||||||||||
| try: | ||||||||||
| raw = pygame.image.load(frame_path) | ||||||||||
| max_h = 800 | ||||||||||
| frame_scale = max_h / 1280 | ||||||||||
| fw = int(856 * frame_scale) | ||||||||||
| fh = int(1280 * frame_scale) | ||||||||||
| self._badge_frame = pygame.transform.smoothscale(raw, (fw, fh)) | ||||||||||
| self._frame_lcd_x = int(self._FRAME_LCD_X * frame_scale) | ||||||||||
| self._frame_lcd_y = int(self._FRAME_LCD_Y * frame_scale) | ||||||||||
| self._frame_lcd_w = int(self._FRAME_LCD_W * frame_scale) | ||||||||||
| self._frame_lcd_h = int(self._FRAME_LCD_H * frame_scale) | ||||||||||
| self._frame_scale = frame_scale | ||||||||||
| self._window = pygame.display.set_mode((fw, fh)) | ||||||||||
| except Exception as e: | ||||||||||
| print(f"Failed to load badge frame: {e}") | ||||||||||
| self._badge_frame = None | ||||||||||
|
|
||||||||||
| if self._badge_frame is None: | ||||||||||
| self._window = pygame.display.set_mode((width * scale, height * scale + 30)) | ||||||||||
|
|
||||||||||
| pygame.display.set_caption("Badge Local Simulator") | ||||||||||
| surface = pygame.Surface((width, height), pygame.SRCALPHA) | ||||||||||
| super().__init__(surface) | ||||||||||
|
|
@@ -862,36 +898,47 @@ def take_screenshot(self) -> None: | |||||||||
| print(f"Screenshot saved: {filepath}") | ||||||||||
|
|
||||||||||
| def present(self) -> None: | ||||||||||
| # Scale and blit the game screen to a temporary surface | ||||||||||
| scaled_game = pygame.transform.scale( | ||||||||||
| self._surface, (self.width * self.scale, self.height * self.scale) | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Blit the scaled game to the window | ||||||||||
| self._window.blit(scaled_game, (0, 0)) | ||||||||||
|
|
||||||||||
| # Draw keyboard hints below the screen | ||||||||||
| y_offset = self.height * self.scale | ||||||||||
| hint_bg = (40, 40, 40) | ||||||||||
| hint_text = (200, 200, 200) | ||||||||||
|
|
||||||||||
| # Fill the hint area with dark background | ||||||||||
| hint_rect = pygame.Rect(0, y_offset, self.width * self.scale, 30) | ||||||||||
| pygame.draw.rect(self._window, hint_bg, hint_rect) | ||||||||||
|
|
||||||||||
| # Draw the hint text | ||||||||||
| hints = [ | ||||||||||
| ("Z/A: A", 10), | ||||||||||
| ("X/B: B", 100), | ||||||||||
| ("Space/C: C", 180), | ||||||||||
| ("Arrows: D-pad", 300), | ||||||||||
| ("H/Esc: Home", 450) | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| for hint, x_pos in hints: | ||||||||||
| text_surf = self._hint_font.render(hint, True, hint_text) | ||||||||||
| self._window.blit(text_surf, (x_pos, y_offset + 8)) | ||||||||||
|
|
||||||||||
| if self._badge_frame is not None: | ||||||||||
| # Badge frame mode: render LCD content inside the frame | ||||||||||
| self._window.blit(self._badge_frame, (0, 0)) | ||||||||||
| pygame.draw.rect(self._window, (0, 0, 0), | ||||||||||
| (self._frame_lcd_x, self._frame_lcd_y, | ||||||||||
| self._frame_lcd_w, self._frame_lcd_h)) | ||||||||||
| scaled_game = pygame.transform.scale( | ||||||||||
| self._surface, (self._frame_lcd_w, self._frame_lcd_h)) | ||||||||||
| self._window.blit(scaled_game, | ||||||||||
| (self._frame_lcd_x, self._frame_lcd_y)) | ||||||||||
| # Draw button labels over white button squares | ||||||||||
| btn_font = pygame.font.Font(None, int(24 * self._frame_scale)) | ||||||||||
|
||||||||||
| btn_font = pygame.font.Font(None, int(24 * self._frame_scale)) | |
| if not hasattr(self, "_btn_font"): | |
| self._btn_font = pygame.font.Font(None, int(24 * self._frame_scale)) | |
| btn_font = self._btn_font |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The LCD target rectangle is 560×430, but the simulator framebuffer is 160×120 (4:3). Scaling 160×120 into a 560×430 rect will always distort the image (~2.3% aspect error). Consider either adjusting
_FRAME_LCD_Hto preserve 4:3 for the cutout, or letterboxing/pillarboxing by scaling to fit while preserving aspect and centering within the cutout rect.