feat: 자막 배경 여백 · 위치 조정 모드 · CPU 추천 · 마이크 입력
Some checks failed
Windows / verify (push) Has been cancelled
Some checks failed
Windows / verify (push) Has been cancelled
요청 네 가지를 넣는다. 1) 자막 배경 여백(padding) 배경 상자를 창 전체가 아니라 **글자를 감싸는 크기**로 그리도록 바꿨다. 창 전체를 칠하던 동안에는 여백을 키워도 글자만 안으로 밀릴 뿐 배경 크기는 그대로여서, 설정 항목을 만들어도 의미가 없었다. 2) 평소 잠금 + '자막 위치 조정' locked 기본값을 True 로 바꾼다. 게임 중에 자막을 실수로 끌고 다니는 사고를 막는 쪽이 기본이어야 한다. 자막 화면의 버튼으로 조정 모드를 열면 그때만 이동·네 모서리 크기 조절·휠이 열린다. 조정 중에는 클릭 통과를 강제로 끈다 — 켜둔 채로는 마우스가 창을 지나가 잡을 수 없다. 크기를 바꾸면 글자 크기가 높이 비율을 그대로 따라간다. 상자만 커지고 글자가 그대로면 여백만 늘어나 '자막을 키우는' 조작이 되지 않는다. 조정 모드는 저장하지 않는다. 다음 실행은 다시 잠긴 상태로 시작한다. 3) GPU 가 없을 때 CPU 추천 티어 표의 recommended 는 GPU 가 넉넉할 때를 전제한 값이라, 그대로 쓰면 GPU 없는 사람에게 10GB 짜리를 권하게 된다. 환경을 보고 고르는 recommended_tier() 를 만들고 배지도 그 결과로 붙인다. GPU 가 없으면 CTranslate2 계열만 후보에 두고 가장 가벼운 쪽을 권한다 — LLM 계열은 CPU 에서 한 문장에 수십 초가 걸려 실시간 자막이 되지 않는다. '추천대로 맞추기' 버튼과, 실제로는 CPU 로 도는데 화면은 GPU 라고 표시하던 것도 같이 고친다. nvidia-smi 실패 문구가 명령줄을 통째로 토해내던 것도 사람이 읽을 문장으로 바꿨다. 4) 마이크 등 입력 장치 선택 InputDeviceCapture 를 추가하고 홈 화면에 목록 전환 버튼을 둔다. 두 목록을 한 번에 섞지 않는다 — 다 합치면 정작 찾으려던 게임이 라인입력들 사이에 파묻힌다. 버튼 글자는 '지금 상태'가 아니라 '누르면 가는 곳'을 적는다. 고른 쪽은 설정에 기억한다.
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
게임/영상 위에 얹는 무테두리 반투명 창. 기본 동작은 유튜브 자막과 같은
|
||||
'외곽선 있는 큰 흰 글씨'로, 어떤 배경 위에서도 읽히는 것을 최우선으로 한다.
|
||||
|
||||
조작:
|
||||
* 드래그 — 위치 이동
|
||||
* 우하단 모서리 — 크기 조절
|
||||
* 휠 — 글자 크기 조절
|
||||
* 우클릭 — 잠금 / 클릭 통과 / 숨기기 메뉴
|
||||
평소에는 **잠겨 있다.** 게임 중에 자막을 실수로 끌고 다니는 일이 없어야 하기
|
||||
때문이다. 자막 화면의 '자막 위치 조정'을 켜야 아래 조작이 열린다.
|
||||
|
||||
위치 조정 모드에서:
|
||||
* 드래그 — 위치 이동
|
||||
* 네 모서리 — 크기 조절 (글자 크기도 같은 비율로 따라온다)
|
||||
* 휠 — 글자 크기 조절
|
||||
* 우클릭 — 잠금 / 클릭 통과 / 숨기기 메뉴
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -38,17 +41,16 @@ def paint_subtitle(
|
||||
source: str = "",
|
||||
partial: str = "",
|
||||
) -> None:
|
||||
"""자막 한 화면을 그린다. 오버레이와 설정 미리보기가 이 함수를 공유한다."""
|
||||
"""자막 한 화면을 그린다. 오버레이와 설정 미리보기가 이 함수를 공유한다.
|
||||
|
||||
배경 상자는 창 전체가 아니라 **글자를 감싸는 크기**로 그린다. 창 전체를
|
||||
칠하면 자막이 짧아도 커다란 띠가 남고, 그 상태에서는 여백을 키워도 글자만
|
||||
안쪽으로 밀릴 뿐 '배경 크기'는 그대로다. 감싸도록 그려야 여백이 곧 배경
|
||||
크기가 된다.
|
||||
"""
|
||||
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
|
||||
painter.setRenderHint(QPainter.RenderHint.TextAntialiasing, True)
|
||||
|
||||
if st.background_opacity > 0:
|
||||
bg = QColor(st.background_color)
|
||||
bg.setAlpha(int(255 * st.background_opacity / 100))
|
||||
painter.setBrush(bg)
|
||||
painter.setPen(Qt.PenStyle.NoPen)
|
||||
painter.drawRoundedRect(rect, 12, 12)
|
||||
|
||||
rows: list[tuple[str, QColor, int]] = []
|
||||
if st.show_source and source and translated:
|
||||
rows.append((source, QColor(st.source_text_color), int(st.font_size * 0.62)))
|
||||
@@ -61,33 +63,60 @@ def paint_subtitle(
|
||||
if not rows:
|
||||
return
|
||||
|
||||
margin = 18
|
||||
available = rect.width() - margin * 2
|
||||
pad = max(0, st.padding)
|
||||
available = rect.width() - pad * 2
|
||||
blocks: list[tuple[list[str], QColor, QFont]] = []
|
||||
total_h = 0
|
||||
text_w = 0
|
||||
for text, color, size in rows:
|
||||
font = QFont(st.font_family, size)
|
||||
font.setBold(st.bold)
|
||||
lines = _wrap(text, QFontMetrics(font), available, st.max_lines)
|
||||
fm = QFontMetrics(font)
|
||||
lines = _wrap(text, fm, available, st.max_lines)
|
||||
blocks.append((lines, color, font))
|
||||
total_h += len(lines) * (QFontMetrics(font).height() + st.line_spacing)
|
||||
total_h += len(lines) * (fm.height() + st.line_spacing)
|
||||
for line in lines:
|
||||
text_w = max(text_w, fm.horizontalAdvance(line))
|
||||
|
||||
y = rect.y() + max(margin, (rect.height() - total_h) // 2)
|
||||
box = _content_box(rect, text_w, total_h, pad, st.align)
|
||||
|
||||
if st.background_opacity > 0:
|
||||
bg = QColor(st.background_color)
|
||||
bg.setAlpha(int(255 * st.background_opacity / 100))
|
||||
painter.setBrush(bg)
|
||||
painter.setPen(Qt.PenStyle.NoPen)
|
||||
painter.drawRoundedRect(box, 12, 12)
|
||||
|
||||
y = box.y() + pad
|
||||
for lines, color, font in blocks:
|
||||
painter.setFont(font)
|
||||
fm = QFontMetrics(font)
|
||||
for line in lines:
|
||||
width = fm.horizontalAdvance(line)
|
||||
if st.align == "left":
|
||||
x = rect.x() + margin
|
||||
x = box.x() + pad
|
||||
elif st.align == "right":
|
||||
x = rect.right() - margin - width
|
||||
x = box.right() - pad - width
|
||||
else:
|
||||
x = rect.x() + (rect.width() - width) // 2
|
||||
x = box.x() + (box.width() - width) // 2
|
||||
_draw_outlined_text(painter, x, y + fm.ascent(), line, color, font, st)
|
||||
y += fm.height() + st.line_spacing
|
||||
|
||||
|
||||
def _content_box(rect: QRect, text_w: int, text_h: int, pad: int, align: str) -> QRect:
|
||||
"""글자를 감싸는 배경 상자. 창 밖으로는 넘지 않는다."""
|
||||
width = min(rect.width(), text_w + pad * 2)
|
||||
height = min(rect.height(), text_h + pad * 2)
|
||||
if align == "left":
|
||||
x = rect.x()
|
||||
elif align == "right":
|
||||
x = rect.right() - width + 1
|
||||
else:
|
||||
x = rect.x() + (rect.width() - width) // 2
|
||||
y = rect.y() + (rect.height() - height) // 2
|
||||
return QRect(x, y, width, height)
|
||||
|
||||
|
||||
def _draw_outlined_text(painter, x, y, text, color, font, st: SubtitleStyle) -> None:
|
||||
"""외곽선 있는 글자.
|
||||
|
||||
@@ -145,6 +174,10 @@ class SubtitleOverlay(QWidget):
|
||||
geometry_changed = Signal(int, int, int, int)
|
||||
#: 자막 on/off 가 바뀔 때 (단축키로도 바뀌므로 UI가 따라와야 한다)
|
||||
visibility_changed = Signal(bool)
|
||||
#: 크기 조정으로 글자 크기가 같이 바뀌었을 때 (설정 화면이 따라와야 한다)
|
||||
font_size_changed = Signal(int)
|
||||
#: 위치 조정 모드가 켜지고 꺼질 때
|
||||
edit_mode_changed = Signal(bool)
|
||||
closed = Signal()
|
||||
|
||||
def __init__(self, style: SubtitleStyle, overlay: OverlayConfig) -> None:
|
||||
@@ -156,7 +189,13 @@ class SubtitleOverlay(QWidget):
|
||||
self._partial_text = ""
|
||||
self._source_text = ""
|
||||
self._drag_from: QPoint | None = None
|
||||
self._resize_from: QRect | None = None
|
||||
#: 위치 조정 모드. 설정에 저장하지 않는다 — 다음 실행 때는 다시 잠긴
|
||||
#: 상태로 시작해야 게임 중에 자막을 실수로 끌고 다니지 않는다.
|
||||
self._edit_mode = False
|
||||
self._resize_corner = ""
|
||||
self._resize_geo = QRect()
|
||||
self._resize_mouse = QPoint()
|
||||
self._resize_font = 0
|
||||
|
||||
self._fade = QTimer(self)
|
||||
self._fade.setSingleShot(True)
|
||||
@@ -181,8 +220,11 @@ class SubtitleOverlay(QWidget):
|
||||
else flags & ~Qt.WindowType.WindowStaysOnTopHint
|
||||
)
|
||||
self.setWindowFlags(flags)
|
||||
# 위치 조정 중에는 클릭 통과를 반드시 꺼야 한다. 켜둔 채로는 마우스가
|
||||
# 창을 그냥 지나가서 잡을 수가 없다.
|
||||
self.setAttribute(
|
||||
Qt.WidgetAttribute.WA_TransparentForMouseEvents, cfg.click_through
|
||||
Qt.WidgetAttribute.WA_TransparentForMouseEvents,
|
||||
cfg.click_through and not self._edit_mode,
|
||||
)
|
||||
|
||||
# 앵커가 지정돼 있거나 아직 배치된 적이 없으면 다시 계산한다.
|
||||
@@ -192,6 +234,50 @@ class SubtitleOverlay(QWidget):
|
||||
self.setVisible(cfg.visible)
|
||||
self.update()
|
||||
|
||||
# --- 위치 조정 모드 --------------------------------------------------
|
||||
@property
|
||||
def edit_mode(self) -> bool:
|
||||
return self._edit_mode
|
||||
|
||||
def set_edit_mode(self, on: bool) -> None:
|
||||
"""자막을 옮기고 크기를 바꿀 수 있는 상태로 전환한다.
|
||||
|
||||
평소에는 잠겨 있다. 게임 중에 자막을 잘못 건드려 끌고 다니는 일이
|
||||
없어야 하기 때문이다. 이 모드에서만 드래그·모서리 크기 조절·휠이
|
||||
동작하고, 켜는 동안에는 자막이 안 보여도 잡을 수 있게 창을 띄운다.
|
||||
"""
|
||||
if on == self._edit_mode:
|
||||
return
|
||||
self._edit_mode = on
|
||||
if on and not self.isVisible():
|
||||
self.setVisible(True)
|
||||
self.setAttribute(
|
||||
Qt.WidgetAttribute.WA_TransparentForMouseEvents,
|
||||
self.overlay_cfg.click_through and not on,
|
||||
)
|
||||
self.setCursor(
|
||||
Qt.CursorShape.SizeAllCursor if on else Qt.CursorShape.ArrowCursor
|
||||
)
|
||||
if not on:
|
||||
self._drag_from = None
|
||||
self._resize_corner = ""
|
||||
self.update()
|
||||
self.edit_mode_changed.emit(on)
|
||||
|
||||
@property
|
||||
def _interactive(self) -> bool:
|
||||
return self._edit_mode or not self.overlay_cfg.locked
|
||||
|
||||
def _corner_at(self, pos: QPoint) -> str:
|
||||
"""모서리 손잡이를 집었는지. 집었으면 어느 모서리인지."""
|
||||
left = pos.x() <= GRIP
|
||||
right = pos.x() >= self.width() - GRIP
|
||||
top = pos.y() <= GRIP
|
||||
bottom = pos.y() >= self.height() - GRIP
|
||||
if (left or right) and (top or bottom):
|
||||
return ("top" if top else "bottom") + ("left" if left else "right")
|
||||
return ""
|
||||
|
||||
def snap_to_anchor(self, anchor: str | None = None, save_only: bool = False) -> None:
|
||||
"""선택한 모니터의 9분할 위치로 자막 창을 붙인다 (디스코드 오버레이 방식)."""
|
||||
cfg = self.overlay_cfg
|
||||
@@ -248,53 +334,94 @@ class SubtitleOverlay(QWidget):
|
||||
self._source_text,
|
||||
self._partial_text,
|
||||
)
|
||||
if not (self._final_text or self._partial_text) and not self.overlay_cfg.locked:
|
||||
self._draw_placeholder(painter)
|
||||
self._draw_grip(painter)
|
||||
if self._edit_mode:
|
||||
self._draw_edit_chrome(painter)
|
||||
|
||||
def _draw_placeholder(self, painter) -> None:
|
||||
painter.setPen(QColor(255, 255, 255, 90))
|
||||
font = QFont(self.style_cfg.font_family, 13)
|
||||
painter.setFont(font)
|
||||
painter.drawText(
|
||||
self.rect(),
|
||||
Qt.AlignmentFlag.AlignCenter,
|
||||
"자막이 여기에 표시됩니다\n드래그로 이동 · 휠로 크기 · 우클릭으로 메뉴",
|
||||
)
|
||||
def _draw_edit_chrome(self, painter) -> None:
|
||||
"""위치 조정 중이라는 것과 어디를 잡으면 되는지 보여준다."""
|
||||
painter.setBrush(Qt.BrushStyle.NoBrush)
|
||||
painter.setPen(QPen(QColor(90, 170, 255, 220), 2, Qt.PenStyle.DashLine))
|
||||
painter.drawRect(self.rect().adjusted(1, 1, -2, -2))
|
||||
|
||||
def _draw_grip(self, painter) -> None:
|
||||
if self.overlay_cfg.locked or self.overlay_cfg.click_through:
|
||||
return
|
||||
painter.setPen(QPen(QColor(255, 255, 255, 110), 2))
|
||||
painter.setPen(Qt.PenStyle.NoPen)
|
||||
painter.setBrush(QColor(90, 170, 255, 235))
|
||||
for rect in self._corner_handles():
|
||||
painter.drawRect(rect)
|
||||
|
||||
if not (self._final_text or self._partial_text):
|
||||
painter.setPen(QColor(255, 255, 255, 170))
|
||||
font = QFont(self.style_cfg.font_family, 13)
|
||||
painter.setFont(font)
|
||||
painter.drawText(
|
||||
self.rect(),
|
||||
Qt.AlignmentFlag.AlignCenter,
|
||||
"끌어서 이동 · 모서리를 잡아 크기 조절 (글자 크기도 같이 바뀝니다)",
|
||||
)
|
||||
|
||||
def _corner_handles(self) -> list[QRect]:
|
||||
w, h = self.width(), self.height()
|
||||
for offset in (5, 10, 15):
|
||||
painter.drawLine(w - offset, h - 4, w - 4, h - offset)
|
||||
return [
|
||||
QRect(0, 0, GRIP, GRIP),
|
||||
QRect(w - GRIP, 0, GRIP, GRIP),
|
||||
QRect(0, h - GRIP, GRIP, GRIP),
|
||||
QRect(w - GRIP, h - GRIP, GRIP, GRIP),
|
||||
]
|
||||
|
||||
# --- 마우스 ---------------------------------------------------------
|
||||
def mousePressEvent(self, event) -> None: # noqa: N802
|
||||
if self.overlay_cfg.locked or event.button() != Qt.MouseButton.LeftButton:
|
||||
if not self._interactive or event.button() != Qt.MouseButton.LeftButton:
|
||||
return
|
||||
pos = event.position().toPoint()
|
||||
if pos.x() > self.width() - GRIP and pos.y() > self.height() - GRIP:
|
||||
self._resize_from = QRect(event.globalPosition().toPoint(), self.size())
|
||||
corner = self._corner_at(event.position().toPoint())
|
||||
if corner:
|
||||
self._resize_corner = corner
|
||||
self._resize_geo = QRect(self.geometry())
|
||||
self._resize_mouse = event.globalPosition().toPoint()
|
||||
self._resize_font = self.style_cfg.font_size
|
||||
else:
|
||||
self._drag_from = event.globalPosition().toPoint() - self.pos()
|
||||
|
||||
def mouseMoveEvent(self, event) -> None: # noqa: N802
|
||||
if self._resize_from is not None:
|
||||
delta = event.globalPosition().toPoint() - self._resize_from.topLeft()
|
||||
self.resize(
|
||||
max(self.minimumWidth(), self._resize_from.width() + delta.x()),
|
||||
max(self.minimumHeight(), self._resize_from.height() + delta.y()),
|
||||
)
|
||||
if self._resize_corner:
|
||||
self._resize_to(event.globalPosition().toPoint())
|
||||
elif self._drag_from is not None:
|
||||
self.move(event.globalPosition().toPoint() - self._drag_from)
|
||||
|
||||
def _resize_to(self, mouse: QPoint) -> None:
|
||||
"""잡은 모서리만 움직이고 반대쪽 모서리는 제자리에 둔다.
|
||||
|
||||
글자 크기는 높이 비율을 그대로 따라간다. 상자만 커지고 글자는 그대로면
|
||||
여백만 늘어나므로, 크기 조절이 '자막을 키우는' 조작이 되지 않는다.
|
||||
"""
|
||||
start = self._resize_geo
|
||||
dx = mouse.x() - self._resize_mouse.x()
|
||||
dy = mouse.y() - self._resize_mouse.y()
|
||||
left, top = start.left(), start.top()
|
||||
right, bottom = start.right(), start.bottom()
|
||||
|
||||
if "left" in self._resize_corner:
|
||||
left = min(left + dx, right - self.minimumWidth())
|
||||
else:
|
||||
right = max(right + dx, left + self.minimumWidth())
|
||||
if "top" in self._resize_corner:
|
||||
top = min(top + dy, bottom - self.minimumHeight())
|
||||
else:
|
||||
bottom = max(bottom + dy, top + self.minimumHeight())
|
||||
|
||||
rect = QRect(QPoint(left, top), QPoint(right, bottom))
|
||||
self.setGeometry(rect)
|
||||
|
||||
scaled = round(self._resize_font * rect.height() / max(1, start.height()))
|
||||
size = max(12, min(120, scaled))
|
||||
if size != self.style_cfg.font_size:
|
||||
self.style_cfg.font_size = size
|
||||
self.font_size_changed.emit(size)
|
||||
self.update()
|
||||
|
||||
def mouseReleaseEvent(self, event) -> None: # noqa: N802
|
||||
if self._drag_from is None and self._resize_from is None:
|
||||
if self._drag_from is None and not self._resize_corner:
|
||||
return
|
||||
self._drag_from = None
|
||||
self._resize_from = None
|
||||
self._resize_corner = ""
|
||||
|
||||
cfg = self.overlay_cfg
|
||||
g = self.geometry()
|
||||
@@ -309,10 +436,13 @@ class SubtitleOverlay(QWidget):
|
||||
self.geometry_changed.emit(g.x(), g.y(), g.width(), g.height())
|
||||
|
||||
def wheelEvent(self, event) -> None: # noqa: N802
|
||||
if self.overlay_cfg.locked:
|
||||
if not self._interactive:
|
||||
return
|
||||
step = 2 if event.angleDelta().y() > 0 else -2
|
||||
self.style_cfg.font_size = max(12, min(120, self.style_cfg.font_size + step))
|
||||
size = max(12, min(120, self.style_cfg.font_size + step))
|
||||
if size != self.style_cfg.font_size:
|
||||
self.style_cfg.font_size = size
|
||||
self.font_size_changed.emit(size)
|
||||
self.update()
|
||||
|
||||
def contextMenuEvent(self, event) -> None: # noqa: N802
|
||||
@@ -343,7 +473,11 @@ class SubtitleOverlay(QWidget):
|
||||
|
||||
def _set_click_through(self, value: bool) -> None:
|
||||
self.overlay_cfg.click_through = value
|
||||
self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, value)
|
||||
# 위치 조정 중이면 통과를 켜지 않는다. 켜는 순간 창을 못 잡게 된다.
|
||||
self.setAttribute(
|
||||
Qt.WidgetAttribute.WA_TransparentForMouseEvents,
|
||||
value and not self._edit_mode,
|
||||
)
|
||||
self.update()
|
||||
|
||||
def _set_always_on_top(self, value: bool) -> None:
|
||||
|
||||
Reference in New Issue
Block a user