add scrolling text and change ui layout
This commit is contained in:
78
scrolling_text.py
Normal file
78
scrolling_text.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import pyray as pr
|
||||
|
||||
|
||||
class ScrollingText:
|
||||
def __init__(
|
||||
self,
|
||||
text: str,
|
||||
font_size: int,
|
||||
speed: float = 40.0, # pixels per second
|
||||
pause_time: float = 1.0, # seconds before scrolling
|
||||
color=pr.WHITE,
|
||||
):
|
||||
self.font_size = font_size
|
||||
self.speed = speed
|
||||
self.pause_time = pause_time
|
||||
self.color = color
|
||||
self.text = None
|
||||
self.set_text(text)
|
||||
|
||||
|
||||
def set_text(self, text: str):
|
||||
if text == self.text:
|
||||
return
|
||||
self.text = text
|
||||
self.text_width = pr.measure_text(self.text, self.font_size)
|
||||
self.reset()
|
||||
|
||||
def reset(self):
|
||||
self.offset = 0.0
|
||||
self.timer = 0.0
|
||||
self.scrolling = False
|
||||
|
||||
def update(self, dt: float, size: pr.Vector2):
|
||||
if self.text_width <= size.x:
|
||||
return
|
||||
|
||||
self.timer += dt
|
||||
|
||||
if not self.scrolling:
|
||||
if self.timer >= self.pause_time:
|
||||
self.scrolling = True
|
||||
self.timer = 0.0
|
||||
else:
|
||||
self.offset += self.speed * dt
|
||||
|
||||
if self.offset >= self.text_width + 20:
|
||||
self.reset()
|
||||
|
||||
def draw(self, pos: pr.Vector2, size: pr.Vector2):
|
||||
clip = pr.Rectangle(pos.x, pos.y, size.x, size.y)
|
||||
pr.begin_scissor_mode(
|
||||
int(clip.x),
|
||||
int(clip.y),
|
||||
int(clip.width),
|
||||
int(clip.height),
|
||||
)
|
||||
|
||||
y = pos.y + (size.y - self.font_size) / 2
|
||||
|
||||
pr.draw_text(
|
||||
self.text,
|
||||
int(pos.x - self.offset),
|
||||
int(y),
|
||||
self.font_size,
|
||||
self.color,
|
||||
)
|
||||
|
||||
# Second copy for seamless loop
|
||||
if self.text_width > size.x:
|
||||
pr.draw_text(
|
||||
self.text,
|
||||
int(pos.x - self.offset + self.text_width + 20),
|
||||
int(y),
|
||||
self.font_size,
|
||||
self.color,
|
||||
)
|
||||
|
||||
pr.end_scissor_mode()
|
||||
Reference in New Issue
Block a user