147 lines
4.5 KiB
Python
147 lines
4.5 KiB
Python
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QComboBox, QHBoxLayout, QPushButton, QLineEdit, QListWidget, QListWidgetItem
|
|
from PyQt5.QtCore import Qt
|
|
from dusk.entity import Entity
|
|
from dusk.defs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, ENTITY_TYPES, ENTITY_TYPE_NULL
|
|
|
|
class EntityPanel(QWidget):
|
|
def __init__(self, parent):
|
|
super().__init__(parent)
|
|
self.parent = parent
|
|
layout = QVBoxLayout(self)
|
|
self.setLayout(layout)
|
|
|
|
# Top panel placeholder
|
|
topWidget = QLabel("Entity Editor (top)")
|
|
layout.addWidget(topWidget)
|
|
|
|
# Name input
|
|
nameLayout = QHBoxLayout()
|
|
nameLabel = QLabel("Name:")
|
|
self.nameInput = QLineEdit()
|
|
nameLayout.addWidget(nameLabel)
|
|
nameLayout.addWidget(self.nameInput)
|
|
layout.addLayout(nameLayout)
|
|
|
|
# Entity Type dropdown (single selection)
|
|
typeLayout = QHBoxLayout()
|
|
typeLabel = QLabel("Type:")
|
|
self.typeDropdown = QComboBox()
|
|
self.typeDropdown.addItems(ENTITY_TYPES)
|
|
typeLayout.addWidget(typeLabel)
|
|
typeLayout.addWidget(self.typeDropdown)
|
|
layout.addLayout(typeLayout)
|
|
|
|
# Entity list and buttons
|
|
self.entityList = QListWidget()
|
|
self.entityList.addItems([])
|
|
layout.addWidget(self.entityList, stretch=1)
|
|
|
|
btnLayout = QHBoxLayout()
|
|
self.btnAdd = QPushButton("Add")
|
|
self.btnRemove = QPushButton("Remove")
|
|
btnLayout.addWidget(self.btnAdd)
|
|
btnLayout.addWidget(self.btnRemove)
|
|
layout.addLayout(btnLayout)
|
|
|
|
# Events
|
|
self.btnAdd.clicked.connect(self.onAddEntity)
|
|
self.btnRemove.clicked.connect(self.onRemoveEntity)
|
|
self.parent.map.onEntityData.sub(self.onEntityData)
|
|
self.parent.map.onPositionChange.sub(self.onPositionChange)
|
|
self.entityList.itemClicked.connect(self.onEntityClicked)
|
|
self.entityList.itemDoubleClicked.connect(self.onEntityDoubleClicked)
|
|
self.typeDropdown.currentIndexChanged.connect(self.onTypeSelected)
|
|
self.nameInput.textChanged.connect(self.onNameChanged)
|
|
|
|
# Call once to populate
|
|
self.onEntityData()
|
|
self.onEntityUnselect()
|
|
|
|
def onEntityUnselect(self):
|
|
self.entityList.setCurrentItem(None)
|
|
self.nameInput.setText("")
|
|
self.typeDropdown.setCurrentIndex(ENTITY_TYPE_NULL)
|
|
|
|
def onEntitySelect(self, entity):
|
|
self.entityList.setCurrentItem(entity.item)
|
|
self.nameInput.setText(entity.name)
|
|
self.typeDropdown.setCurrentIndex(entity.type)
|
|
|
|
def onEntityDoubleClicked(self, item):
|
|
entity = item.data(Qt.UserRole)
|
|
chunk = entity.chunk
|
|
worldX = chunk.x + entity.localX
|
|
worldY = chunk.y + entity.localY
|
|
worldZ = chunk.z + entity.localZ
|
|
self.parent.map.moveTo(worldX, worldY, worldZ)
|
|
|
|
def onEntityClicked(self, item):
|
|
pass
|
|
|
|
def onAddEntity(self):
|
|
chunk = self.parent.map.getChunkAtWorldPos(*self.parent.map.position)
|
|
if chunk is None:
|
|
return
|
|
ent = chunk.addEntity(
|
|
self.parent.map.position[0] - chunk.x,
|
|
self.parent.map.position[1] - chunk.y,
|
|
self.parent.map.position[2] - chunk.z
|
|
)
|
|
|
|
def onRemoveEntity(self):
|
|
item = self.entityList.currentItem()
|
|
if item is None:
|
|
return
|
|
entity = item.data(Qt.UserRole)
|
|
if entity:
|
|
chunk = entity.chunk
|
|
chunk.removeEntity(entity)
|
|
pass
|
|
|
|
def onEntityData(self):
|
|
self.onEntityUnselect()
|
|
self.entityList.clear()
|
|
for chunk in self.parent.map.chunks.values():
|
|
for id, entity in chunk.entities.items():
|
|
item = QListWidgetItem(entity.name)
|
|
item.setData(Qt.UserRole, entity) # Store the entity object
|
|
entity.item = item
|
|
self.entityList.addItem(item)
|
|
|
|
# Select if there is something at current position
|
|
self.onPositionChange(self.parent.map.position)
|
|
|
|
def onPositionChange(self, position):
|
|
self.onEntityUnselect()
|
|
|
|
# Get Entity at..
|
|
chunk = self.parent.map.getChunkAtWorldPos(*position)
|
|
if chunk is None:
|
|
return
|
|
|
|
localX = (position[0] - chunk.x) % CHUNK_WIDTH
|
|
localY = (position[1] - chunk.y) % CHUNK_HEIGHT
|
|
localZ = (position[2] - chunk.z) % CHUNK_DEPTH
|
|
|
|
for ent in chunk.entities.values():
|
|
if ent.localX != localX or ent.localY != localY or ent.localZ != localZ:
|
|
continue
|
|
self.onEntitySelect(ent)
|
|
self.entityList.setCurrentItem(ent.item)
|
|
break
|
|
|
|
def onTypeSelected(self, index):
|
|
item = self.entityList.currentItem()
|
|
if item is None:
|
|
return
|
|
entity = item.data(Qt.UserRole)
|
|
if entity:
|
|
entity.setType(index)
|
|
|
|
def onNameChanged(self, text):
|
|
item = self.entityList.currentItem()
|
|
if item is None:
|
|
return
|
|
entity = item.data(Qt.UserRole)
|
|
if entity:
|
|
entity.setName(text) |