Files
dusk/tools/editortool/map/entitypanel.py
Dominic Masters d068f0f2c3
Some checks failed
Build Dusk / build-linux (push) Failing after 1m8s
Build Dusk / build-psp (push) Has been cancelled
Fixed double clicking to ent
2025-11-25 08:23:27 -06:00

154 lines
5.0 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 * CHUNK_WIDTH) + entity.localX
worldY = (chunk.y * CHUNK_HEIGHT) + entity.localY
worldZ = (chunk.z * CHUNK_DEPTH) + 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
localX = (self.parent.map.position[0] - (chunk.x * CHUNK_WIDTH)) % CHUNK_WIDTH
localY = (self.parent.map.position[1] - (chunk.y * CHUNK_HEIGHT)) % CHUNK_HEIGHT
localZ = (self.parent.map.position[2] - (chunk.z * CHUNK_DEPTH)) % CHUNK_DEPTH
# Make sure there's not already an entity here
for ent in chunk.entities.values():
if ent.localX == localX and ent.localY == localY and ent.localZ == localZ:
print("Entity already exists at this location")
return
ent = chunk.addEntity(localX, localY, localZ)
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)) % CHUNK_WIDTH
localY = (position[1] - (chunk.y * CHUNK_HEIGHT)) % CHUNK_HEIGHT
localZ = (position[2] - (chunk.z * CHUNK_DEPTH)) % 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)