Refactor
This commit is contained in:
55
tools/editor/map/camera.py
Normal file
55
tools/editor/map/camera.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import math
|
||||
import time
|
||||
from OpenGL.GL import *
|
||||
from OpenGL.GLU import *
|
||||
from tools.dusk.defs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH, RPG_CAMERA_PIXELS_PER_UNIT, RPG_CAMERA_Z_OFFSET, RPG_CAMERA_FOV
|
||||
|
||||
class Camera:
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.pixelsPerUnit = RPG_CAMERA_PIXELS_PER_UNIT
|
||||
self.yOffset = RPG_CAMERA_Z_OFFSET
|
||||
self.fov = RPG_CAMERA_FOV
|
||||
self.scale = 8.0
|
||||
self.lastTime = time.time()
|
||||
self.lookAtTarget = [0.0, 0.0, 0.0]
|
||||
|
||||
def setup(self, vw, vh, duration=0.1):
|
||||
now = time.time()
|
||||
delta = now - self.lastTime
|
||||
self.lastTime = now
|
||||
# Calculate ease factor for exponential smoothing over 'duration' seconds
|
||||
ease = 1 - math.exp(-delta / duration)
|
||||
|
||||
z = (vh / 2.0) / (
|
||||
(self.pixelsPerUnit * self.scale) * math.tan(math.radians(self.fov / 2.0))
|
||||
)
|
||||
lookAt = [
|
||||
self.parent.map.position[0] * TILE_WIDTH,
|
||||
self.parent.map.position[1] * TILE_HEIGHT,
|
||||
self.parent.map.position[2] * TILE_DEPTH,
|
||||
]
|
||||
aspectRatio = vw / vh
|
||||
|
||||
# Ease the lookAt target
|
||||
for i in range(3):
|
||||
self.lookAtTarget[i] += (lookAt[i] - self.lookAtTarget[i]) * ease
|
||||
|
||||
# Camera position is now based on the eased lookAtTarget
|
||||
cameraPosition = (
|
||||
self.lookAtTarget[0],
|
||||
self.lookAtTarget[1] + self.yOffset,
|
||||
self.lookAtTarget[2] + z
|
||||
)
|
||||
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glLoadIdentity()
|
||||
gluPerspective(self.fov, aspectRatio, 0.1, 1000.0)
|
||||
glScalef(1.0, -1.0, 1.0) # Flip the projection matrix upside down
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
glLoadIdentity()
|
||||
gluLookAt(
|
||||
cameraPosition[0], cameraPosition[1], cameraPosition[2],
|
||||
self.lookAtTarget[0], self.lookAtTarget[1], self.lookAtTarget[2],
|
||||
0.0, 1.0, 0.0
|
||||
)
|
||||
80
tools/editor/map/chunkpanel.py
Normal file
80
tools/editor/map/chunkpanel.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QGridLayout, QTreeWidget, QTreeWidgetItem, QComboBox
|
||||
from tools.dusk.defs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, TILE_SHAPES
|
||||
|
||||
class ChunkPanel(QWidget):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent)
|
||||
self.parent = parent
|
||||
layout = QVBoxLayout(self)
|
||||
|
||||
# Tile shape dropdown
|
||||
self.tileShapeDropdown = QComboBox()
|
||||
self.tileShapeDropdown.addItems(TILE_SHAPES.keys())
|
||||
self.tileShapeDropdown.setToolTip("Tile Shape")
|
||||
layout.addWidget(self.tileShapeDropdown)
|
||||
|
||||
# Add expandable tree list
|
||||
self.tree = QTreeWidget()
|
||||
self.tree.setHeaderLabel("Chunks")
|
||||
self.tree.expandAll() # Expand by default, remove if you want collapsed
|
||||
layout.addWidget(self.tree) # Removed invalid stretch factor
|
||||
|
||||
# Add stretch so tree expands
|
||||
layout.setStretchFactor(self.tree, 1)
|
||||
|
||||
# Event subscriptions
|
||||
self.parent.map.onMapData.sub(self.onMapData)
|
||||
self.parent.map.onPositionChange.sub(self.onPositionChange)
|
||||
self.tileShapeDropdown.currentTextChanged.connect(self.onTileShapeChanged)
|
||||
|
||||
# For each chunk
|
||||
for chunk in self.parent.map.chunks.values():
|
||||
# Create tree element
|
||||
item = QTreeWidgetItem(self.tree, ["Chunk ({}, {}, {})".format(chunk.x, chunk.y, chunk.z)])
|
||||
chunk.chunkPanelTree = item
|
||||
chunk.chunkPanelTree.setExpanded(True)
|
||||
item.setData(0, 0, chunk) # Store chunk reference
|
||||
|
||||
chunk.onChunkData.sub(self.onChunkData)
|
||||
|
||||
def onMapData(self, data):
|
||||
pass
|
||||
|
||||
def onPositionChange(self, pos):
|
||||
self.updateChunkList()
|
||||
|
||||
tile = self.parent.map.getTileAtWorldPos(*self.parent.map.position)
|
||||
if tile is None:
|
||||
return
|
||||
|
||||
key = "TILE_SHAPE_NULL"
|
||||
for k, v in TILE_SHAPES.items():
|
||||
if v != tile.shape:
|
||||
continue
|
||||
key = k
|
||||
break
|
||||
self.tileShapeDropdown.setCurrentText(key)
|
||||
|
||||
def onTileShapeChanged(self, shape_key):
|
||||
tile = self.parent.map.getTileAtWorldPos(*self.parent.map.position)
|
||||
|
||||
if tile is None or shape_key not in TILE_SHAPES:
|
||||
return
|
||||
tile.setShape(TILE_SHAPES[shape_key])
|
||||
|
||||
def updateChunkList(self):
|
||||
# Clear existing items
|
||||
currentChunk = self.parent.map.getChunkAtWorldPos(*self.parent.map.position)
|
||||
|
||||
# Example tree items
|
||||
for chunk in self.parent.map.chunks.values():
|
||||
title = "Chunk ({}, {}, {})".format(chunk.x, chunk.y, chunk.z)
|
||||
if chunk == currentChunk:
|
||||
title += " [C]"
|
||||
if chunk.isDirty():
|
||||
title += " *"
|
||||
item = chunk.chunkPanelTree
|
||||
item.setText(0, title)
|
||||
|
||||
def onChunkData(self, chunk):
|
||||
self.updateChunkList()
|
||||
154
tools/editor/map/entitypanel.py
Normal file
154
tools/editor/map/entitypanel.py
Normal file
@@ -0,0 +1,154 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QComboBox, QHBoxLayout, QPushButton, QLineEdit, QListWidget, QListWidgetItem
|
||||
from PyQt5.QtCore import Qt
|
||||
from tools.dusk.entity import Entity
|
||||
from tools.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")
|
||||
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)
|
||||
41
tools/editor/map/glwidget.py
Normal file
41
tools/editor/map/glwidget.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from PyQt5.QtCore import QTimer
|
||||
from PyQt5.QtWidgets import QOpenGLWidget
|
||||
from OpenGL.GL import *
|
||||
from OpenGL.GLU import *
|
||||
|
||||
class GLWidget(QOpenGLWidget):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent)
|
||||
self.parent = parent
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self.update)
|
||||
self.timer.start(16) # ~60 FPS
|
||||
|
||||
def initializeGL(self):
|
||||
glClearColor(0.392, 0.584, 0.929, 1.0)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
glEnable(GL_BLEND)
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
|
||||
glEnable(GL_POLYGON_OFFSET_FILL)
|
||||
glPolygonOffset(1.0, 1.0)
|
||||
glDisable(GL_POLYGON_OFFSET_FILL)
|
||||
|
||||
def resizeGL(self, w, h):
|
||||
pass
|
||||
|
||||
def paintGL(self):
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||
glLoadIdentity()
|
||||
|
||||
w = self.width()
|
||||
h = self.height()
|
||||
if h <= 0:
|
||||
h = 1
|
||||
if w <= 0:
|
||||
w = 1
|
||||
|
||||
glViewport(0, 0, w, h)
|
||||
self.parent.camera.setup(w, h)
|
||||
self.parent.grid.draw()
|
||||
self.parent.map.draw()
|
||||
self.parent.selectBox.draw()
|
||||
46
tools/editor/map/grid.py
Normal file
46
tools/editor/map/grid.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from OpenGL.GL import *
|
||||
from tools.dusk.defs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH
|
||||
|
||||
class Grid:
|
||||
def __init__(self, lines=1000):
|
||||
self.cellWidth = TILE_WIDTH
|
||||
self.cellHeight = TILE_HEIGHT
|
||||
self.lines = lines
|
||||
self.enabled = True
|
||||
|
||||
def draw(self):
|
||||
if not self.enabled:
|
||||
return
|
||||
center = [0.01,0.01,0.01]
|
||||
halfWidth = self.cellWidth * self.lines // 2
|
||||
halfHeight = self.cellHeight * self.lines // 2
|
||||
# Draw origin axes
|
||||
glBegin(GL_LINES)
|
||||
|
||||
# X axis - RED
|
||||
glColor3f(1.0, 0.0, 0.0)
|
||||
glVertex3f(center[0] - halfWidth, center[1], center[2])
|
||||
glVertex3f(center[0] + halfWidth, center[1], center[2])
|
||||
|
||||
# Y axis - GREEN
|
||||
glColor3f(0.0, 1.0, 0.0)
|
||||
glVertex3f(center[0], center[1] - halfHeight, center[2])
|
||||
glVertex3f(center[0], center[1] + halfHeight, center[2])
|
||||
|
||||
# Z axis - BLUE
|
||||
glColor3f(0.0, 0.0, 1.0)
|
||||
glVertex3f(center[0], center[1], center[2] - halfWidth)
|
||||
glVertex3f(center[0], center[1], center[2] + halfWidth)
|
||||
glEnd()
|
||||
|
||||
# Draw grid
|
||||
glColor3f(0.8, 0.8, 0.8)
|
||||
glBegin(GL_LINES)
|
||||
for i in range(-self.lines // 2, self.lines // 2 + 1):
|
||||
# Vertical lines
|
||||
glVertex3f(center[0] + i * self.cellWidth, center[1] - halfHeight, center[2])
|
||||
glVertex3f(center[0] + i * self.cellWidth, center[1] + halfHeight, center[2])
|
||||
# Horizontal lines
|
||||
glVertex3f(center[0] - halfWidth, center[1] + i * self.cellHeight, center[2])
|
||||
glVertex3f(center[0] + halfWidth, center[1] + i * self.cellHeight, center[2])
|
||||
glEnd()
|
||||
83
tools/editor/map/mapinfopanel.py
Normal file
83
tools/editor/map/mapinfopanel.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QHBoxLayout, QMessageBox
|
||||
from tools.dusk.defs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH
|
||||
|
||||
class MapInfoPanel(QWidget):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent)
|
||||
self.parent = parent
|
||||
|
||||
# Components
|
||||
layout = QVBoxLayout()
|
||||
|
||||
mapTitleLabel = QLabel("Map Title")
|
||||
self.mapTitleInput = QLineEdit()
|
||||
layout.addWidget(mapTitleLabel)
|
||||
layout.addWidget(self.mapTitleInput)
|
||||
|
||||
tilePositionLabel = QLabel("Tile Position")
|
||||
layout.addWidget(tilePositionLabel)
|
||||
tilePositionRow = QHBoxLayout()
|
||||
self.tileXInput = QLineEdit()
|
||||
self.tileXInput.setPlaceholderText("X")
|
||||
tilePositionRow.addWidget(self.tileXInput)
|
||||
self.tileYInput = QLineEdit()
|
||||
self.tileYInput.setPlaceholderText("Y")
|
||||
tilePositionRow.addWidget(self.tileYInput)
|
||||
self.tileZInput = QLineEdit()
|
||||
self.tileZInput.setPlaceholderText("Z")
|
||||
tilePositionRow.addWidget(self.tileZInput)
|
||||
self.tileGo = QPushButton("Go")
|
||||
tilePositionRow.addWidget(self.tileGo)
|
||||
layout.addLayout(tilePositionRow)
|
||||
|
||||
self.chunkPosLabel = QLabel()
|
||||
layout.addWidget(self.chunkPosLabel)
|
||||
self.chunkLabel = QLabel()
|
||||
layout.addWidget(self.chunkLabel)
|
||||
|
||||
layout.addStretch()
|
||||
self.setLayout(layout)
|
||||
|
||||
# Events
|
||||
self.tileGo.clicked.connect(self.goToPosition)
|
||||
self.tileXInput.returnPressed.connect(self.goToPosition)
|
||||
self.tileYInput.returnPressed.connect(self.goToPosition)
|
||||
self.tileZInput.returnPressed.connect(self.goToPosition)
|
||||
self.parent.map.onPositionChange.sub(self.updatePositionLabels)
|
||||
self.parent.map.onMapData.sub(self.onMapData)
|
||||
self.mapTitleInput.textChanged.connect(self.onMapNameChange)
|
||||
|
||||
# Initial label setting
|
||||
self.updatePositionLabels(self.parent.map.position)
|
||||
|
||||
def goToPosition(self):
|
||||
try:
|
||||
x = int(self.tileXInput.text())
|
||||
y = int(self.tileYInput.text())
|
||||
z = int(self.tileZInput.text())
|
||||
self.parent.map.moveTo(x, y, z)
|
||||
except ValueError:
|
||||
QMessageBox.warning(self, "Invalid Input", "Please enter valid integer coordinates.")
|
||||
|
||||
def updatePositionLabels(self, pos):
|
||||
self.tileXInput.setText(str(pos[0]))
|
||||
self.tileYInput.setText(str(pos[1]))
|
||||
self.tileZInput.setText(str(pos[2]))
|
||||
|
||||
chunkTileX = pos[0] % CHUNK_WIDTH
|
||||
chunkTileY = pos[1] % CHUNK_HEIGHT
|
||||
chunkTileZ = pos[2] % CHUNK_DEPTH
|
||||
chunkTileIndex = chunkTileX + chunkTileY * CHUNK_WIDTH + chunkTileZ * CHUNK_WIDTH * CHUNK_HEIGHT
|
||||
self.chunkPosLabel.setText(f"Chunk Position: {chunkTileX}, {chunkTileY}, {chunkTileZ} ({chunkTileIndex})")
|
||||
|
||||
chunkX = pos[0] // CHUNK_WIDTH
|
||||
chunkY = pos[1] // CHUNK_HEIGHT
|
||||
chunkZ = pos[2] // CHUNK_DEPTH
|
||||
self.chunkLabel.setText(f"Chunk: {chunkX}, {chunkY}, {chunkZ}")
|
||||
|
||||
def onMapData(self, data):
|
||||
self.updatePositionLabels(self.parent.map.position)
|
||||
self.mapTitleInput.setText(data.get("mapName", ""))
|
||||
|
||||
def onMapNameChange(self, text):
|
||||
self.parent.map.data['mapName'] = text
|
||||
51
tools/editor/map/mapleftpanel.py
Normal file
51
tools/editor/map/mapleftpanel.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QGridLayout, QPushButton, QTabWidget, QLabel
|
||||
from tools.editor.map.chunkpanel import ChunkPanel
|
||||
from tools.editor.map.entitypanel import EntityPanel
|
||||
from tools.editor.map.regionpanel import RegionPanel
|
||||
|
||||
class MapLeftPanel(QWidget):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent)
|
||||
self.parent = parent
|
||||
layout = QVBoxLayout(self)
|
||||
self.setLayout(layout)
|
||||
|
||||
# Nav buttons
|
||||
self.chunkInfoLabel = QLabel("Tile Information")
|
||||
layout.addWidget(self.chunkInfoLabel)
|
||||
grid = QGridLayout()
|
||||
self.btnUp = QPushButton("U")
|
||||
self.btnN = QPushButton("N")
|
||||
self.btnDown = QPushButton("D")
|
||||
self.btnW = QPushButton("W")
|
||||
self.btnS = QPushButton("S")
|
||||
self.btnE = QPushButton("E")
|
||||
|
||||
grid.addWidget(self.btnUp, 0, 0)
|
||||
grid.addWidget(self.btnN, 0, 1)
|
||||
grid.addWidget(self.btnDown, 0, 2)
|
||||
grid.addWidget(self.btnW, 1, 0)
|
||||
grid.addWidget(self.btnS, 1, 1)
|
||||
grid.addWidget(self.btnE, 1, 2)
|
||||
layout.addLayout(grid)
|
||||
|
||||
# Panels
|
||||
self.chunkPanel = ChunkPanel(self.parent)
|
||||
self.entityPanel = EntityPanel(self.parent)
|
||||
self.regionPanel = RegionPanel(self.parent)
|
||||
|
||||
# Tabs
|
||||
self.tabs = QTabWidget()
|
||||
self.tabs.addTab(self.chunkPanel, "Tiles")
|
||||
self.tabs.addTab(self.entityPanel, "Entities")
|
||||
self.tabs.addTab(self.regionPanel, "Regions")
|
||||
self.tabs.addTab(None, "Triggers")
|
||||
layout.addWidget(self.tabs)
|
||||
|
||||
# Event subscriptions
|
||||
self.btnN.clicked.connect(lambda: self.parent.map.moveRelative(0, -1, 0))
|
||||
self.btnS.clicked.connect(lambda: self.parent.map.moveRelative(0, 1, 0))
|
||||
self.btnE.clicked.connect(lambda: self.parent.map.moveRelative(1, 0, 0))
|
||||
self.btnW.clicked.connect(lambda: self.parent.map.moveRelative(-1, 0, 0))
|
||||
self.btnUp.clicked.connect(lambda: self.parent.map.moveRelative(0, 0, 1))
|
||||
self.btnDown.clicked.connect(lambda: self.parent.map.moveRelative(0, 0, -1))
|
||||
46
tools/editor/map/menubar.py
Normal file
46
tools/editor/map/menubar.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
from PyQt5.QtWidgets import QAction, QMenuBar, QFileDialog
|
||||
from PyQt5.QtGui import QKeySequence
|
||||
from tools.dusk.map import MAP_DEFAULT_PATH
|
||||
|
||||
class MapMenubar:
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.menubar = QMenuBar(parent)
|
||||
parent.setMenuBar(self.menubar)
|
||||
|
||||
self.fileMenu = self.menubar.addMenu("File")
|
||||
self.actionNew = QAction("New", parent)
|
||||
self.actionOpen = QAction("Open", parent)
|
||||
self.actionSave = QAction("Save", parent)
|
||||
self.actionSaveAs = QAction("Save As", parent)
|
||||
|
||||
self.actionNew.setShortcut(QKeySequence("Ctrl+N"))
|
||||
self.actionOpen.setShortcut(QKeySequence("Ctrl+O"))
|
||||
self.actionSave.setShortcut(QKeySequence("Ctrl+S"))
|
||||
self.actionSaveAs.setShortcut(QKeySequence("Ctrl+Shift+S"))
|
||||
|
||||
self.actionNew.triggered.connect(self.newFile)
|
||||
self.actionOpen.triggered.connect(self.openFile)
|
||||
self.actionSave.triggered.connect(self.saveFile)
|
||||
self.actionSaveAs.triggered.connect(self.saveAsFile)
|
||||
self.fileMenu.addAction(self.actionNew)
|
||||
self.fileMenu.addAction(self.actionOpen)
|
||||
self.fileMenu.addAction(self.actionSave)
|
||||
self.fileMenu.addAction(self.actionSaveAs)
|
||||
|
||||
def newFile(self):
|
||||
self.parent.map.newFile()
|
||||
|
||||
def openFile(self):
|
||||
filePath, _ = QFileDialog.getOpenFileName(self.menubar, "Open Map File", MAP_DEFAULT_PATH, "Map Files (*.json)")
|
||||
if filePath:
|
||||
self.parent.map.load(filePath)
|
||||
|
||||
def saveFile(self):
|
||||
self.parent.map.save()
|
||||
|
||||
def saveAsFile(self):
|
||||
filePath, _ = QFileDialog.getSaveFileName(self.menubar, "Save Map File As", MAP_DEFAULT_PATH, "Map Files (*.json)")
|
||||
if filePath:
|
||||
self.parent.map.save(filePath)
|
||||
15
tools/editor/map/regionpanel.py
Normal file
15
tools/editor/map/regionpanel.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QComboBox, QHBoxLayout, QPushButton, QLineEdit, QListWidget, QListWidgetItem
|
||||
from PyQt5.QtCore import Qt
|
||||
from tools.dusk.entity import Entity
|
||||
from tools.dusk.defs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, ENTITY_TYPES, ENTITY_TYPE_NULL
|
||||
|
||||
class RegionPanel(QWidget):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent)
|
||||
self.parent = parent
|
||||
layout = QVBoxLayout(self)
|
||||
self.setLayout(layout)
|
||||
|
||||
# Top panel placeholder
|
||||
topWidget = QLabel("Region Editor")
|
||||
layout.addWidget(topWidget)
|
||||
44
tools/editor/map/selectbox.py
Normal file
44
tools/editor/map/selectbox.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import OpenGL.GL as gl
|
||||
from tools.dusk.defs import defs
|
||||
import colorsys
|
||||
from tools.dusk.defs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH
|
||||
|
||||
class SelectBox:
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.hue = 0.0
|
||||
|
||||
def draw(self):
|
||||
position = [
|
||||
self.parent.map.position[0] * TILE_WIDTH - (TILE_WIDTH / 2.0),
|
||||
self.parent.map.position[1] * TILE_HEIGHT - (TILE_HEIGHT / 2.0),
|
||||
self.parent.map.position[2] * TILE_DEPTH - (TILE_DEPTH / 2.0)
|
||||
]
|
||||
|
||||
vertices = [
|
||||
(position[0], position[1], position[2]),
|
||||
(position[0]+TILE_WIDTH, position[1], position[2]),
|
||||
(position[0]+TILE_WIDTH, position[1]+TILE_HEIGHT, position[2]),
|
||||
(position[0], position[1]+TILE_HEIGHT, position[2]),
|
||||
(position[0], position[1], position[2]+TILE_DEPTH),
|
||||
(position[0]+TILE_WIDTH, position[1], position[2]+TILE_DEPTH),
|
||||
(position[0]+TILE_WIDTH, position[1]+TILE_HEIGHT, position[2]+TILE_DEPTH),
|
||||
(position[0], position[1]+TILE_HEIGHT, position[2]+TILE_DEPTH)
|
||||
]
|
||||
edges = [
|
||||
(0, 1), (1, 2), (2, 3), (3, 0), # bottom face
|
||||
(4, 5), (5, 6), (6, 7), (7, 4), # top face
|
||||
(4, 5), (5, 6), (6, 7), (7, 4), # top face
|
||||
(0, 4), (1, 5), (2, 6), (3, 7) # vertical edges
|
||||
]
|
||||
|
||||
# Cycle hue
|
||||
self.hue = (self.hue + 0.01) % 1.0
|
||||
r, g, b = colorsys.hsv_to_rgb(self.hue, 1.0, 1.0)
|
||||
gl.glColor3f(r, g, b)
|
||||
gl.glLineWidth(2.0)
|
||||
gl.glBegin(gl.GL_LINES)
|
||||
for edge in edges:
|
||||
for vertex in edge:
|
||||
gl.glVertex3f(*vertices[vertex])
|
||||
gl.glEnd()
|
||||
18
tools/editor/map/statusbar.py
Normal file
18
tools/editor/map/statusbar.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from PyQt5.QtWidgets import QStatusBar, QLabel
|
||||
|
||||
class StatusBar(QStatusBar):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.parent = parent
|
||||
self.leftLabel = QLabel("")
|
||||
self.rightLabel = QLabel("")
|
||||
self.addWidget(self.leftLabel, 1)
|
||||
self.addPermanentWidget(self.rightLabel)
|
||||
|
||||
parent.map.onMapData.sub(self.onMapData)
|
||||
|
||||
def setStatus(self, message):
|
||||
self.leftLabel.setText(message)
|
||||
|
||||
def onMapData(self, data):
|
||||
self.rightLabel.setText(self.parent.map.mapFileName if self.parent.map.mapFileName else "Untitled.json")
|
||||
80
tools/editor/map/vertexbuffer.py
Normal file
80
tools/editor/map/vertexbuffer.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from OpenGL.GL import *
|
||||
import array
|
||||
|
||||
class VertexBuffer:
|
||||
def __init__(self, componentsPerVertex=3):
|
||||
self.componentsPerVertex = componentsPerVertex
|
||||
self.vertices = []
|
||||
self.colors = []
|
||||
self.uvs = []
|
||||
self.data = None
|
||||
self.colorData = None
|
||||
self.uvData = None
|
||||
|
||||
def buildData(self):
|
||||
hasColors = len(self.colors) > 0
|
||||
hasUvs = len(self.uvs) > 0
|
||||
|
||||
vertexCount = len(self.vertices) // self.componentsPerVertex
|
||||
|
||||
dataList = []
|
||||
colorList = []
|
||||
uvList = []
|
||||
|
||||
for i in range(vertexCount):
|
||||
vStart = i * self.componentsPerVertex
|
||||
dataList.extend(self.vertices[vStart:vStart+self.componentsPerVertex])
|
||||
|
||||
if hasColors:
|
||||
cStart = i * 4 # Assuming RGBA
|
||||
colorList.extend(self.colors[cStart:cStart+4])
|
||||
|
||||
if hasUvs:
|
||||
uStart = i * 2 # Assuming UV
|
||||
uvList.extend(self.uvs[uStart:uStart+2])
|
||||
|
||||
self.data = array.array('f', dataList)
|
||||
|
||||
if hasColors:
|
||||
self.colorData = array.array('f', colorList)
|
||||
else:
|
||||
self.colorData = None
|
||||
|
||||
if hasUvs:
|
||||
self.uvData = array.array('f', uvList)
|
||||
else:
|
||||
self.uvData = None
|
||||
|
||||
def draw(self, mode=GL_TRIANGLES, count=-1):
|
||||
if count == -1:
|
||||
count = len(self.data) // self.componentsPerVertex
|
||||
|
||||
if count == 0:
|
||||
return
|
||||
|
||||
glEnableClientState(GL_VERTEX_ARRAY)
|
||||
glVertexPointer(self.componentsPerVertex, GL_FLOAT, 0, self.data.tobytes())
|
||||
|
||||
if self.colorData:
|
||||
glEnableClientState(GL_COLOR_ARRAY)
|
||||
glColorPointer(4, GL_FLOAT, 0, self.colorData.tobytes())
|
||||
|
||||
if self.uvData:
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, self.uvData.tobytes())
|
||||
|
||||
glDrawArrays(mode, 0, count)
|
||||
|
||||
glDisableClientState(GL_VERTEX_ARRAY)
|
||||
if self.colorData:
|
||||
glDisableClientState(GL_COLOR_ARRAY)
|
||||
if self.uvData:
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
|
||||
|
||||
def clear(self):
|
||||
self.vertices = []
|
||||
self.colors = []
|
||||
self.uvs = []
|
||||
self.data = array.array('f')
|
||||
self.colorData = None
|
||||
self.uvData = None
|
||||
Reference in New Issue
Block a user