Skip to content

Commit

Permalink
Fix buttons not glowing in config for some users
Browse files Browse the repository at this point in the history
  • Loading branch information
roxgib committed Dec 8, 2023
1 parent 4053615 commit 09c1a83
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 69 deletions.
Binary file removed contanki/buttons/Other/Glow.png
Binary file not shown.
Binary file added contanki/buttons/Other/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions contanki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,5 +1081,4 @@ def update_control(self):
layout.replaceWidget(self.icon, new_icon)
new_icon.setFixedHeight(60)
self.icon.deleteLater()
self.icon = new_icon
self.icon.refresh()
self.icon = new_icon
5 changes: 3 additions & 2 deletions contanki/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ function mock_controller() {
return;
}
bridgeCommand(`contanki::on_connect::10::4::mock_controller`);
polling = setInterval(mock_poll, 20);
mock_index = 0;
polling = setInterval(mock_poll, 50);
}

function mock_poll() {
if ((mock_index = 100)) {
if (mock_index > 80) {
mock_index = 0;
}
if (mock_index < 20) {
Expand Down
117 changes: 55 additions & 62 deletions contanki/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,68 @@
from weakref import WeakSet

from aqt import mw
from aqt import (
QComboBox,
from aqt.qt import (
QFont,
QHBoxLayout,
QLabel,
QSizePolicy,
QWidget,
Qt,
QPixmap,
QPainter,
QColor,
QIcon,
QGraphicsColorizeEffect,
)
from aqt.utils import tooltip

from .controller import Controller


def get_button_icon(
controller: Controller | str, button: str, glow: bool = False
) -> QPixmap:
directions = [
"Left",
"Right",
"Up",
"Down",
"Horizontal",
"Vertical",
"Diagonal",
"UpLeft",
"UpRight",
"DownLeft",
"DownRight",
"HorizontalVertical",
]


def icon_path(folder, file):
return join(dirname(abspath(__file__)), "buttons", folder, file)


def get_button_icon(controller: Controller, button: str) -> QPixmap:
"""Fetches the icon for a button, and applies glow effect."""
controller = str(controller)
if "(" in controller:
controller = controller.split(" (")[0]
directions = [
"Left",
"Right",
"Up",
"Down",
"Horizontal",
"Vertical",
"Diagonal",
"UpLeft",
"UpRight",
"DownLeft",
"DownRight",
"HorizontalVertical",
]

def path(folder, file):
return join(dirname(abspath(__file__)), "buttons", folder, file)

pixmap = QPixmap(path(controller, button))
if (
pixmap.isNull()
and button
and button != "Not Assigned"
and button.split(" ")[-1] in directions
):
direction = button.split(" ")[-1]
button = " ".join(button.split(" ")[:-1])
pixmap = QPixmap(path(controller, button))
if not pixmap.isNull():
dpixmap = QPixmap(path("Arrows", direction))
with QPainter(pixmap) as painter:
painter.drawPixmap(pixmap.rect(), dpixmap, dpixmap.rect())
if pixmap.isNull():
pixmap = QPixmap(100, 100)
# not sure how or why, but the last icon will still be there, so we clear it
pixmap.fill(QColor(255, 255, 255, 255))
with QPainter(pixmap) as painter:
controller_name = str(controller.parent)
if "(" in controller_name:
controller_name = controller_name.split(" (")[0]
pixmap = QPixmap(icon_path("Other", "background.png"))
with QPainter(pixmap) as painter:
if not (icon := QPixmap(icon_path(controller_name, button))).isNull():
painter.drawPixmap(pixmap.rect(), icon, icon.rect())
elif (
button
and button != "Not Assigned"
and (direction := button.split(" ")[-1]) in directions
and (button := " ".join(button.split(" ")[:-1]))
and not (icon := QPixmap(icon_path(controller_name, button))).isNull()
and not (dpixmap := QPixmap(icon_path("Arrows", direction))).isNull()
):
painter.drawPixmap(pixmap.rect(), icon, icon.rect())
painter.drawPixmap(pixmap.rect(), dpixmap, dpixmap.rect())
else:
painter.setFont(QFont("Arial", 20))
painter.drawText(
pixmap.rect(), Qt.AlignmentFlag.AlignCenter, button.replace(" ", "\n")
)
if button and button != "Not Assigned":
tooltip(f"Error: Couldn't load {button} icon for {controller}.")

if glow:
gpixmap = QPixmap(path("Other", "glow"))
with QPainter(pixmap) as painter:
painter.drawPixmap(pixmap.rect(), gpixmap, gpixmap.rect())

if button and button != "Not Assigned":
tooltip(f"Error: Couldn't load {button} icon for {controller_name}.")
return pixmap


Expand All @@ -89,7 +77,7 @@ def __init__(
self,
parent: QWidget | None,
button: str,
controller: Controller | str,
controller: Controller,
index: int | None = None,
is_large=False,
) -> None:
Expand All @@ -99,16 +87,21 @@ def __init__(
self.setMaximumHeight(120 if is_large else 60)
self.setContentsMargins(0, 0, 0, 0)
self._pixmap = get_button_icon(controller, button)
self._pixmap_glow = get_button_icon(controller, button, glow=True)
if index is not None:
IconHighlighter.register_icon(index, self)
self.refresh()
self.colorize_effect = QGraphicsColorizeEffect()
self.colorize_effect.setColor(QColor(255, 255, 255, 128))
self.setGraphicsEffect(self.colorize_effect)
self.glow(False)
self.resizeEvent(None)

def refresh(self, glow=False):
def glow(self, glow=False):
"""Updates the icon for size and glow."""
pixmap = self._pixmap_glow if glow else self._pixmap
self.colorize_effect.setEnabled(glow)

def resizeEvent(self, event):
self.setPixmap(
pixmap.scaled(
self._pixmap.scaled(
self.height(),
self.height(),
Qt.AspectRatioMode.KeepAspectRatio,
Expand All @@ -131,4 +124,4 @@ def set_highlight(self, index: int, highlight: bool) -> None:
"""Sets the highlight for an icon."""
if index in self.icons:
for icon in self.icons[index]:
icon.refresh(highlight)
icon.glow(highlight)
1 change: 0 additions & 1 deletion contanki/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,5 @@ def appear(self, state: State):
if self.width() - 300 < len(text) * 8:
text = text.replace(" ", "\n")
self.action.setText(text)
self.icon.refresh()
self.update()
self.show()
4 changes: 2 additions & 2 deletions contanki/tests/test_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
def test_can_get_all_button_icons():
for controller in get_controller_list():
for button in Controller(controller).buttons.values():
get_button_icon(controller, button)
ButtonIcon(None, button, controller)
get_button_icon(Controller(controller), button)
ButtonIcon(None, button, Controller(controller))

0 comments on commit 09c1a83

Please sign in to comment.