from PyQt4 import QtGui, QtCore import socket, thread, time from os.path import exists from functools import partial from buttons import PixmapButton from constants import * def get_option(section, value, default=""): tempini = ConfigParser() tempini.read("AO2XP.ini") return ini.read_ini(tempini, section, value, default) class CharIcon(QtGui.QLabel): def __init__(self, parent, ind): super(CharIcon, self).__init__(parent) self.parent = parent self.ind = ind self.charid = 0 def setCharIndex(self, charid): self.charid = charid def mousePressEvent(self, event): self.parent.onCharClicked(self.charid) class charselect(QtGui.QWidget): setBtnImage = QtCore.pyqtSignal(str, int, int) def __init__(self, parent): super(charselect, self).__init__(parent) self.parent = parent self.page = 0 self.image = QtGui.QLabel(self) self.image.setPixmap(QtGui.QPixmap(AO2XPpath+"themes/default/charselect_background.png")) self.image.show() self.charsearch = QtGui.QLineEdit(self) self.charsearch.move(255, 7) self.charsearch.resize(200, self.charsearch.sizeHint().height()) self.charsearch.textChanged.connect(self.onSearch) self.charsearch.setPlaceholderText('Search...') self.quittolobby = QtGui.QPushButton(self) self.quittolobby.setText("Disconnect") self.quittolobby.resize(self.quittolobby.sizeHint()) self.quittolobby.clicked.connect(self.quitToLobby) self.quittolobby.move(714 - 25 - self.quittolobby.size().width(), 6) self.cancelbtn = QtGui.QPushButton(self) self.cancelbtn.setText("Cancel") self.cancelbtn.resize(self.cancelbtn.sizeHint()) self.cancelbtn.clicked.connect(self.onCancel) self.cancelbtn.move(25, self.quittolobby.y()) self.buttons = [] self.chartaken = [] self.prevpage = PixmapButton(self, QtGui.QPixmap(AO2XPpath+"themes/default/arrow_left.png")) self.nextpage = PixmapButton(self, QtGui.QPixmap(AO2XPpath+"themes/default/arrow_right.png")) self.setButtons() self.setBtnImage.connect(self.setButtonImage) def setButtons(self): #directly stolen from ao2 source code and default theme because i was stuck (i'm so sorry) btn_width, btn_height = 60, 60 x_spacing, y_spacing = 7, 7 x_mod_count, y_mod_count = 0, 0 left, top = 25, 36 width, height = 663, 596 columns = (width - btn_width) / (x_spacing + btn_width) +1 rows = (height - btn_height) / (y_spacing + btn_height) +1 self.max_chars_on_page = columns * rows for i in range(self.max_chars_on_page): self.buttons.append(CharIcon(self, i)) self.chartaken.append(QtGui.QLabel(self)) self.chartaken[i].setPixmap(QtGui.QPixmap(AO2XPpath+"themes/default/char_taken.png")) self.chartaken[i].hide() x_pos = (btn_width + x_spacing) * x_mod_count y_pos = (btn_height + y_spacing) * y_mod_count self.buttons[i].setGeometry(left+x_pos, top+y_pos, btn_width, btn_height) self.chartaken[i].move(left+x_pos, top+y_pos) self.buttons[i].show() x_mod_count += 1 if x_mod_count == columns: y_mod_count += 1 x_mod_count = 0 self.prevpage.move(left - self.prevpage.pixmap.size().width(), top + height) self.prevpage.clicked.connect(self.prevPageButton) self.prevpage.show() self.nextpage.move(left + width, top + height) self.nextpage.clicked.connect(self.nextPageButton) self.nextpage.show() def showCharPage(self, charlist=None): if not charlist: filtered = False charlist = self.charlist else: filtered = True for ind in range(self.page * self.max_chars_on_page, self.max_chars_on_page * (self.page + 1)): i = ind - (self.page * self.max_chars_on_page) if ind >= len(charlist): self.chartaken[i].hide() self.buttons[i].hide() self.nextpage.hide() continue else: self.nextpage.show() if self.page > 0: self.prevpage.show() else: self.prevpage.hide() char = charlist[ind][0].lower() charid = charlist[ind][2] if filtered else ind if exists(AOpath+"characters/"+ char +"/char_icon.png"): # AO2 self.setBtnImage.emit(AOpath+"characters/"+ char +"/char_icon.png", i, charid) elif exists(AOpath+"misc/demothings/"+ char +"_char_icon.png"): # AO 1.7.5/1.8 self.setBtnImage.emit(AOpath+"misc/demothings/"+ char +"_char_icon.png", i, charid) else: self.setBtnImage.emit("placeholder.png", i, charid) if charlist[ind][1] == 0: # free slot self.chartaken[i].hide() else: self.chartaken[i].setAttribute(QtCore.Qt.WA_TransparentForMouseEvents) self.chartaken[i].show() self.buttons[i].show() self.buttons[i].setToolTip(charlist[ind][0]) def showCharPageFiltered(self, name): charlist = [] for i, char in enumerate(self.charlist): if name.lower() in char[0].lower(): charlist.append([char[0], char[1], i]) self.showCharPage(charlist) def quitToLobby(self): self.parent.gamewindow.returnToMenu() def setCharList(self, charlist): self.charlist = charlist self.showCharPage() def nextPageButton(self): self.page += 1 self.showCharPage() def prevPageButton(self): self.page -= 1 self.showCharPage() def showCharSelect(self, can_cancel = True): self.show() self.cancelbtn.setEnabled(can_cancel) self.charsearch.setText("") # Automatically calls showCharPage def setButtonImage(self, filename, i, charid): self.buttons[i].setPixmap(QtGui.QPixmap(filename)) self.buttons[i].setCharIndex(charid) def onCharClicked(self, charid): char = self.charlist[charid] if self.parent.charname.lower() == char[0].lower(): self.onCancel() return self.selectChar(charid) def onSearch(self): self.page = 0 text = self.charsearch.text() if text: self.showCharPageFiltered(unicode(text)) else: self.showCharPage() def selectChar(self, charIndex): self.parent.tcp.send("CC#0#"+str(charIndex)+"#ur mom gay#%") def onCancel(self): self.hide() self.parent.gamewindow.setFixedSize(self.parent.width, self.parent.height) self.parent.gamewindow.center() def show(self): super(charselect, self).show() self.parent.gamewindow.setFixedSize(714, 668) self.parent.gamewindow.center()