-
Notifications
You must be signed in to change notification settings - Fork 9.9k
raylib: label class #35571
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
Draft
sshane
wants to merge
8
commits into
master
Choose a base branch
from
label-class
base: master
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.
Draft
raylib: label class #35571
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b36e09
Text
sshane ba52603
label widget instead of function
sshane a4df4be
some
sshane fd18d2e
stop random changes
sshane a8747f0
use new label class
sshane 74bc204
keyboard
sshane e00ee88
draft for now, but move over rest
sshane 7971988
rm
sshane 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
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
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
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
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 |
---|---|---|
@@ -1,54 +1,114 @@ | ||
import pyray as rl | ||
from openpilot.system.ui.lib.application import gui_app, FontWeight, DEFAULT_TEXT_SIZE, DEFAULT_TEXT_COLOR | ||
from openpilot.system.ui.lib.widget import Widget | ||
from openpilot.system.ui.lib.text_measure import measure_text_cached | ||
from openpilot.system.ui.lib.utils import GuiStyleContext | ||
|
||
|
||
def gui_label( | ||
rect: rl.Rectangle, | ||
text: str, | ||
font_size: int = DEFAULT_TEXT_SIZE, | ||
color: rl.Color = DEFAULT_TEXT_COLOR, | ||
font_weight: FontWeight = FontWeight.NORMAL, | ||
alignment: int = rl.GuiTextAlignment.TEXT_ALIGN_LEFT, | ||
alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE, | ||
elide_right: bool = True | ||
): | ||
font = gui_app.font(font_weight) | ||
text_size = measure_text_cached(font, text, font_size) | ||
display_text = text | ||
class Label(Widget): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @deanlee what do you think instead of a function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. want to open a PR to make the button and other functions widget classes? |
||
def __init__(self, | ||
text: str, | ||
font_size: int = DEFAULT_TEXT_SIZE, | ||
color: rl.Color = DEFAULT_TEXT_COLOR, | ||
font_weight: FontWeight = FontWeight.NORMAL, | ||
alignment: int = rl.GuiTextAlignment.TEXT_ALIGN_LEFT, | ||
alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE, | ||
elide_right: bool = True): | ||
super().__init__() | ||
self.text = text | ||
self.font_size = font_size | ||
self.color = color | ||
self.font_weight = font_weight | ||
self.alignment = alignment | ||
self.alignment_vertical = alignment_vertical | ||
self.elide_right = elide_right | ||
|
||
def set_text(self, txt: str): | ||
self.text = txt | ||
|
||
def _render(self, rect: rl.Rectangle): | ||
font = gui_app.font(self.font_weight) | ||
text_size = measure_text_cached(font, self.text, self.font_size) | ||
display_text = self.text | ||
|
||
# Elide text to fit within the rectangle | ||
if self.elide_right and text_size.x > rect.width: | ||
ellipsis = "..." | ||
left, right = 0, len(self.text) | ||
while left < right: | ||
mid = (left + right) // 2 | ||
candidate = self.text[:mid] + ellipsis | ||
candidate_size = measure_text_cached(font, candidate, self.font_size) | ||
if candidate_size.x <= rect.width: | ||
left = mid + 1 | ||
else: | ||
right = mid | ||
display_text = self.text[: left - 1] + ellipsis if left > 0 else ellipsis | ||
text_size = measure_text_cached(font, display_text, self.font_size) | ||
|
||
# Calculate horizontal position based on alignment | ||
text_x = rect.x + { | ||
rl.GuiTextAlignment.TEXT_ALIGN_LEFT: 0, | ||
rl.GuiTextAlignment.TEXT_ALIGN_CENTER: (rect.width - text_size.x) / 2, | ||
rl.GuiTextAlignment.TEXT_ALIGN_RIGHT: rect.width - text_size.x, | ||
}.get(self.alignment, 0) | ||
|
||
# Elide text to fit within the rectangle | ||
if elide_right and text_size.x > rect.width: | ||
ellipsis = "..." | ||
left, right = 0, len(text) | ||
while left < right: | ||
mid = (left + right) // 2 | ||
candidate = text[:mid] + ellipsis | ||
candidate_size = measure_text_cached(font, candidate, font_size) | ||
if candidate_size.x <= rect.width: | ||
left = mid + 1 | ||
else: | ||
right = mid | ||
display_text = text[: left - 1] + ellipsis if left > 0 else ellipsis | ||
text_size = measure_text_cached(font, display_text, font_size) | ||
# Calculate vertical position based on alignment | ||
text_y = rect.y + { | ||
rl.GuiTextAlignmentVertical.TEXT_ALIGN_TOP: 0, | ||
rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE: (rect.height - text_size.y) / 2, | ||
rl.GuiTextAlignmentVertical.TEXT_ALIGN_BOTTOM: rect.height - text_size.y, | ||
}.get(self.alignment_vertical, 0) | ||
|
||
# Calculate horizontal position based on alignment | ||
text_x = rect.x + { | ||
rl.GuiTextAlignment.TEXT_ALIGN_LEFT: 0, | ||
rl.GuiTextAlignment.TEXT_ALIGN_CENTER: (rect.width - text_size.x) / 2, | ||
rl.GuiTextAlignment.TEXT_ALIGN_RIGHT: rect.width - text_size.x, | ||
}.get(alignment, 0) | ||
# Draw the text in the specified rectangle | ||
rl.draw_text_ex(font, display_text, rl.Vector2(text_x, text_y), self.font_size, 0, self.color) | ||
|
||
# Calculate vertical position based on alignment | ||
text_y = rect.y + { | ||
rl.GuiTextAlignmentVertical.TEXT_ALIGN_TOP: 0, | ||
rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE: (rect.height - text_size.y) / 2, | ||
rl.GuiTextAlignmentVertical.TEXT_ALIGN_BOTTOM: rect.height - text_size.y, | ||
}.get(alignment_vertical, 0) | ||
|
||
# Draw the text in the specified rectangle | ||
rl.draw_text_ex(font, display_text, rl.Vector2(text_x, text_y), font_size, 0, color) | ||
# def gui_label( | ||
# rect: rl.Rectangle, | ||
# text: str, | ||
# font_size: int = DEFAULT_TEXT_SIZE, | ||
# color: rl.Color = DEFAULT_TEXT_COLOR, | ||
# font_weight: FontWeight = FontWeight.NORMAL, | ||
# alignment: int = rl.GuiTextAlignment.TEXT_ALIGN_LEFT, | ||
# alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE, | ||
# elide_right: bool = True | ||
# ): | ||
# font = gui_app.font(font_weight) | ||
# text_size = measure_text_cached(font, text, font_size) | ||
# display_text = text | ||
# | ||
# # Elide text to fit within the rectangle | ||
# if elide_right and text_size.x > rect.width: | ||
# ellipsis = "..." | ||
# left, right = 0, len(text) | ||
# while left < right: | ||
# mid = (left + right) // 2 | ||
# candidate = text[:mid] + ellipsis | ||
# candidate_size = measure_text_cached(font, candidate, font_size) | ||
# if candidate_size.x <= rect.width: | ||
# left = mid + 1 | ||
# else: | ||
# right = mid | ||
# display_text = text[: left - 1] + ellipsis if left > 0 else ellipsis | ||
# text_size = measure_text_cached(font, display_text, font_size) | ||
# | ||
# # Calculate horizontal position based on alignment | ||
# text_x = rect.x + { | ||
# rl.GuiTextAlignment.TEXT_ALIGN_LEFT: 0, | ||
# rl.GuiTextAlignment.TEXT_ALIGN_CENTER: (rect.width - text_size.x) / 2, | ||
# rl.GuiTextAlignment.TEXT_ALIGN_RIGHT: rect.width - text_size.x, | ||
# }.get(alignment, 0) | ||
# | ||
# # Calculate vertical position based on alignment | ||
# text_y = rect.y + { | ||
# rl.GuiTextAlignmentVertical.TEXT_ALIGN_TOP: 0, | ||
# rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE: (rect.height - text_size.y) / 2, | ||
# rl.GuiTextAlignmentVertical.TEXT_ALIGN_BOTTOM: rect.height - text_size.y, | ||
# }.get(alignment_vertical, 0) | ||
# | ||
# # Draw the text in the specified rectangle | ||
# rl.draw_text_ex(font, display_text, rl.Vector2(text_x, text_y), font_size, 0, color) | ||
|
||
|
||
def gui_text_box( | ||
|
Oops, something went wrong.
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.
ignore