# -*- coding: utf-8 -*- u"""KonamiCommand""" from __future__ import absolute_import, division, print_function from Qt import QtGui, QtWidgets from maya.app.general.mayaMixin import MayaQWidgetBaseMixin import time class KonamiCommandWindow(MayaQWidgetBaseMixin, QtWidgets.QMainWindow): _UI_WINDOW_NAME = "KonamiCommandWindow" _UI_ID = "tm8r_konami_command" _VALID_SEQUENCE = [u"↑", u"↑", u"↓", u"↓", u"←", u"→", u"←", u"→", "B", "A"] _VALID_SEQUENCE_LEN = len(_VALID_SEQUENCE) def __init__(self, parent=None): super(KonamiCommandWindow, self).__init__(parent=parent) self.setWindowTitle(self._UI_WINDOW_NAME) self.setObjectName(self._UI_ID) self.command_field = QtWidgets.QLineEdit() self.result_label = QtWidgets.QLabel("") self.prev_time = time.time() self.reset_threshold = 2 self.current_command = [] @staticmethod def show_ui(*args): u"""UIを表示""" win = KonamiCommandWindow() win._create_ui() def _create_ui(self): rootWidget = QtWidgets.QWidget(self) rootLayout = QtWidgets.QVBoxLayout(self) rootWidget.setLayout(rootLayout) label = QtWidgets.QLabel(u"Please enter the command.") rootLayout.addWidget(label) command_layout = QtWidgets.QVBoxLayout() self.command_field.setEnabled(False) command_layout.addWidget(self.command_field) command_layout.addWidget(self.result_label) rootLayout.addLayout(command_layout) self.setCentralWidget(rootWidget) self.show() def keyPressEvent(self, event): if event.isAutoRepeat(): return pressed = QtGui.QKeySequence(event.key()).toString(QtGui.QKeySequence.NativeText) if self.current_command and time.time() - self.prev_time > self.reset_threshold: self._reset_command() self.current_command.append(pressed) self._reflect_keys(self.current_command) return self.current_command.append(pressed) self.prev_time = time.time() if len(self.current_command) < self._VALID_SEQUENCE_LEN: self._reflect_keys(self.current_command) self.result_label.setText("") return target_command = self.current_command[-self._VALID_SEQUENCE_LEN:] self._reflect_keys(target_command) if target_command != self._VALID_SEQUENCE: self.result_label.setText("Not Match") self.result_label.setStyleSheet("color:#ff0000;") return self.result_label.setText("Match") self.result_label.setStyleSheet("color:#00ff00;") self._reset_command(False) def _reflect_keys(self, keys): self.command_field.setText(",".join(keys)) def _reset_command(self, update_result=True): self.current_command = [] self.prev_time = time.time() if update_result: self.result_label.setText("") self.result_label.setStyleSheet("color:#ffffff;") KonamiCommandWindow.show_ui()