summaryrefslogtreecommitdiff
path: root/src/ui.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui.py')
-rw-r--r--src/ui.py178
1 files changed, 164 insertions, 14 deletions
diff --git a/src/ui.py b/src/ui.py
index 247938e..825a713 100644
--- a/src/ui.py
+++ b/src/ui.py
@@ -2,6 +2,7 @@
from common import *
from editorui import *
+import os
class KPLayerList(QtGui.QWidget):
@@ -28,7 +29,8 @@ class KPLayerList(QtGui.QWidget):
def setupToolbar(self, tb):
- self.actAdd = tb.addAction(QtGui.QIcon(), 'Add', self.addLayer)
+ self.actAddTile = tb.addAction(QtGui.QIcon(), 'T+', self.addTileLayer)
+ self.actAddDoodad = tb.addAction(QtGui.QIcon(), 'D+', self.addDoodadLayer)
self.actRemove = tb.addAction(QtGui.QIcon(), 'Remove', self.removeLayer)
self.actMoveUp = tb.addAction(QtGui.QIcon(), 'Move Up', self.moveUp)
self.actMoveDown = tb.addAction(QtGui.QIcon(), 'Move Down', self.moveDown)
@@ -55,22 +57,26 @@ class KPLayerList(QtGui.QWidget):
self.setButtonStates()
- def addLayer(self):
+ def addTileLayer(self):
from dialogs import KPTilesetChooserDialog
- special = ['Doodads (Special)']
- tilesetName = KPTilesetChooserDialog.run('Choose a tileset for the new layer', special)
+ tilesetName = KPTilesetChooserDialog.run('Choose a tileset for the new layer')
if tilesetName is None:
return
- if tilesetName == 0:
- layer = KP.map.createNewDoodadLayer()
- else:
- layer = KP.map.createNewTileLayer(tilesetName)
+ layer = KP.map.createNewTileLayer(tilesetName)
+
+ KP.map.appendLayer(layer)
+ self.setButtonStates()
+
+
+ def addDoodadLayer(self):
+ layer = KP.map.createNewDoodadLayer()
KP.map.appendLayer(layer)
self.setButtonStates()
+
def removeLayer(self):
layer = self.selectedLayer()
scene = KP.mainWindow.scene
@@ -96,6 +102,135 @@ class KPLayerList(QtGui.QWidget):
+class KPDoodadSelector(QtGui.QWidget):
+ objChanged = QtCore.pyqtSignal(int, QtGui.QListWidgetItem)
+
+
+ def __init__(self):
+ """Initialises the widget."""
+
+ QtGui.QWidget.__init__(self)
+
+ self.layout = QtGui.QVBoxLayout()
+ self.layout.setSpacing(0)
+
+ self.doodadList = QtGui.QListWidget()
+ self.doodadList.setViewMode(1)
+ self.doodadList.setWrapping(True)
+ self.doodadList.setDragDropMode(1)
+ self.doodadList.setSelectionMode(1)
+ self.doodadList.setResizeMode(1)
+ self.doodadList.setGridSize(QtCore.QSize(128, 128))
+ self.doodadList.setIconSize(QtCore.QSize(100, 100))
+ self.doodadList.setSpacing(4)
+
+
+ self.toolbar = QtGui.QToolBar()
+ self.layout.addWidget(self.toolbar)
+
+ self.addDoodadButton = self.toolbar.addAction(QtGui.QIcon(), 'Add', self.addDoodadfromFile)
+ self.removeDoodadButton = self.toolbar.addAction(QtGui.QIcon(), 'Remove', self.removeDoodad)
+
+
+ self.layout.addWidget(self.doodadList)
+
+ self.setLayout(self.layout)
+
+ self.nextIndex = 0
+
+ self.doodadList.currentItemChanged.connect(self.handleRowChanged)
+
+
+ def keyPressEvent(self, event):
+
+ self.doodadList.keyPressEvent(event)
+
+ if event.key() == 0x1000003:
+
+ msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Warning,
+ "Delete Doodad?", "Are you sure you want to delete this doodad? This action cannot be undone.",
+ QtGui.QMessageBox.NoButton, self)
+ msgBox.addButton("Delete", QtGui.QMessageBox.AcceptRole)
+ msgBox.addButton("Cancel", QtGui.QMessageBox.RejectRole)
+ if msgBox.exec_() == QtGui.QMessageBox.AcceptRole:
+ self.removeDoodad()
+
+
+ def addDoodad(self, image, name):
+ """Takes a name and a QPixmap and turns it into an icon, then goes ahead and makes a doodad.
+ Doodads are QListWidget items with an index number as Qt.UserRole #32."""
+
+
+ doodie = QtGui.QListWidgetItem(QtGui.QIcon(image), name)
+ doodie.setSizeHint(QtCore.QSize(128,128))
+ doodie.setData(32, self.nextIndex)
+ doodie.setToolTip(name)
+ doodie.setTextAlignment(Qt.AlignBottom | Qt.AlignHCenter)
+
+ self.doodadList.addItem(doodie)
+ self.nextIndex += 1
+
+
+ def addDoodadfromFile(self):
+ """Asks the user for files to load in as doodads."""
+
+ log = QtGui.QFileDialog()
+ log.setFileMode(3);
+
+ files = QtGui.QFileDialog.getOpenFileNames(self,
+ "Choose an image or several image files.", "",
+ "Images (*.png *.jpeg *.jpg *.bmp)")
+ if files:
+ for image in files:
+ name = os.path.basename(unicode(image)).split('.')[0]
+
+ pix = QtGui.QPixmap()
+ pix.load(image)
+
+ self.addDoodad(pix, name)
+
+
+ def removeDoodad(self):
+ """Removes selected doodad"""
+
+ item = self.doodadList.currentRow()
+
+ if item != -1:
+ self.doodadList.takeItem(item)
+
+
+ def getDoodad(self, index):
+ """Retrieves a doodad by index"""
+
+ for item in self.doodadList.items():
+ if item.data(32) == index:
+ return item
+
+
+ def getDoodadImage(self, index, width, height):
+ """Retrieves a doodad pixmap by index"""
+
+ for item in self.doodadList.items():
+ if item.data(32) == index:
+ return item.icon().pixmap(width, height)
+
+ def getDoodads(self):
+ """Returns a list of all doodads.
+
+ Name can be retrieved with doodad.text()
+ Image with doodad.icon().pixmap(doodad.icon().availableSizes()[0])
+ Index with doodad.data(32)"""
+
+ return self.doodadList.items()
+
+
+ @QtCore.pyqtSlot(QtGui.QListWidgetItem)
+ def handleRowChanged(self, current):
+ """Throws a signal emitting the current object when changed"""
+
+ self.objChanged.emit(current.data(32), current)
+
+
class KPObjectSelector(QtGui.QWidget):
@@ -246,11 +381,6 @@ class KPObjectSelector(QtGui.QWidget):
-
-
-
-
-
class KPMainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
@@ -285,16 +415,31 @@ class KPMainWindow(QtGui.QMainWindow):
self.objectSelectorDock = QtGui.QDockWidget('Objects')
self.objectSelectorDock.setWidget(self.objectSelector)
+ self.doodadSelector = KPDoodadSelector()
+ self.doodadSelector.objChanged.connect(self.handleSelectedDoodadChanged)
+
+ self.doodadSelectorDock = QtGui.QDockWidget('Doodads')
+ self.doodadSelectorDock.setWidget(self.doodadSelector)
+ self.doodadSelectorDock.hide()
self.addDockWidget(Qt.RightDockWidgetArea, self.layerListDock)
self.addDockWidget(Qt.RightDockWidgetArea, self.objectSelectorDock)
+ self.addDockWidget(Qt.RightDockWidgetArea, self.doodadSelectorDock)
@QtCore.pyqtSlot(KPLayer)
def handleSelectedLayerChanged(self, layer):
self.scene.setCurrentLayer(layer)
- self.objectSelector.setModel(KP.map.loadedTilesets[layer.tileset].getModel())
+ if isinstance(layer, KPDoodadLayer):
+ self.objectSelectorDock.hide()
+ self.doodadSelectorDock.show()
+
+ else:
+ self.doodadSelectorDock.hide()
+ self.objectSelectorDock.show()
+
+ self.objectSelector.setModel(KP.map.loadedTilesets[layer.tileset].getModel())
@QtCore.pyqtSlot(int, KPTileObject)
@@ -303,5 +448,10 @@ class KPMainWindow(QtGui.QMainWindow):
self.editor.paintNextID = index
+ @QtCore.pyqtSlot(int, QtGui.QListWidgetItem)
+ def handleSelectedDoodadChanged(self, index, obj):
+ self.editor.paintNext = obj
+ self.editor.paintNextID = index
+