Files
dusk/tools/editortool/maptool.py
Dominic Masters 7c194ab4b4
All checks were successful
Build Dusk / build-linux (push) Successful in 1m5s
Build Dusk / build-psp (push) Successful in 1m5s
About to draw chunk
2025-11-16 09:11:58 -06:00

178 lines
5.3 KiB
Python

import sys
import os
import json
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget,
QHBoxLayout, QVBoxLayout, QPushButton,
QLabel, QSlider, QDialog, QRadioButton, QDialogButtonBox,
QAction, QFileDialog, QLineEdit, QMessageBox
)
from editortool.map.glwidget import GLWidget
class MapWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Dusk Map Editor")
self.resize(1280, 720)
# File menu setup
menubar = self.menuBar()
file_menu = menubar.addMenu("File")
action_new = QAction("New", self)
action_open = QAction("Open", self)
action_save = QAction("Save", self)
action_save_as = QAction("Save As", self)
file_menu.addAction(action_new)
file_menu.addAction(action_open)
file_menu.addAction(action_save)
file_menu.addAction(action_save_as)
action_new.triggered.connect(self.newFile)
action_open.triggered.connect(self.openFile)
action_save.triggered.connect(self.saveFile)
action_save_as.triggered.connect(self.saveFileAs)
self.current_file = None
self.dirty = False
self.highlighted_pos = [0, 0, 0] # x, y, z
central = QWidget()
self.setCentralWidget(central)
main_layout = QHBoxLayout(central)
# Left panel
left_panel = QVBoxLayout()
chunk_info_label = QLabel("CHUNK INFO")
left_panel.addWidget(chunk_info_label)
# Movement controls
move_label = QLabel("Move Selection")
left_panel.addWidget(move_label)
btn_n = QPushButton("N")
btn_s = QPushButton("S")
btn_e = QPushButton("E")
btn_w = QPushButton("W")
btn_up = QPushButton("Up")
btn_down = QPushButton("Down")
left_panel.addWidget(btn_n)
left_panel.addWidget(btn_s)
left_panel.addWidget(btn_e)
left_panel.addWidget(btn_w)
left_panel.addWidget(btn_up)
left_panel.addWidget(btn_down)
left_panel.addStretch()
# Right panel
right_panel = QVBoxLayout()
map_info_label = QLabel("Map Information")
right_panel.addWidget(map_info_label)
map_title_label = QLabel("Map Title")
self.map_title_input = QLineEdit()
right_panel.addWidget(map_title_label)
right_panel.addWidget(self.map_title_input)
right_panel.addStretch()
# Add panels to main layout
left_widget = QWidget()
left_widget.setLayout(left_panel)
left_widget.setFixedWidth(200)
main_layout.addWidget(left_widget)
# Center panel (GLWidget + controls)
self.gl_widget = GLWidget(self)
main_layout.addWidget(self.gl_widget, stretch=3)
right_widget = QWidget()
right_widget.setLayout(right_panel)
right_widget.setFixedWidth(250)
main_layout.addWidget(right_widget)
self.map_title_input.textChanged.connect(self._on_map_title_changed)
# Connect movement buttons
btn_n.clicked.connect(lambda: self.move_highlight(0, 1, 0))
btn_s.clicked.connect(lambda: self.move_highlight(0, -1, 0))
btn_e.clicked.connect(lambda: self.move_highlight(1, 0, 0))
btn_w.clicked.connect(lambda: self.move_highlight(-1, 0, 0))
btn_up.clicked.connect(lambda: self.move_highlight(0, 0, 1))
btn_down.clicked.connect(lambda: self.move_highlight(0, 0, -1))
def _on_map_title_changed(self):
self.dirty = True
def newFile(self):
self.current_file = None
self.map_title_input.setText("")
self.dirty = False
# ...clear relevant data...
def openFile(self):
default_dir = os.path.join(os.path.dirname(__file__), '../../assets/map/')
fname, _ = QFileDialog.getOpenFileName(self, "Open JSON File", default_dir, "JSON Files (*.json)")
if fname:
self.current_file = fname
try:
with open(fname, "r", encoding="utf-8") as f:
data = json.load(f)
self.map_title_input.setText(data.get("mapName", ""))
self.dirty = False
except Exception as e:
self.map_title_input.setText("")
# Optionally show error dialog
def saveFile(self):
if self.current_file:
self._save_to_file(self.current_file)
else:
self.saveFileAs()
def saveFileAs(self):
default_dir = os.path.join(os.path.dirname(__file__), '../../assets/map/')
fname, _ = QFileDialog.getSaveFileName(self, "Save JSON File As", default_dir, "JSON Files (*.json)")
if fname:
self.current_file = fname
self._save_to_file(fname)
def _save_to_file(self, fname):
data = {
"mapName": self.map_title_input.text()
}
try:
with open(fname, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
self.dirty = False
except Exception as e:
# Optionally show error dialog
pass
def move_highlight(self, dx, dy, dz):
self.highlighted_pos[0] += dx
self.highlighted_pos[1] += dy
self.highlighted_pos[2] += dz
self.gl_widget.set_highlighted_pos(self.highlighted_pos)
self.dirty = True
def closeEvent(self, event):
if self.dirty:
reply = QMessageBox.question(
self,
"Unsaved Changes",
"You have unsaved changes. Do you want to save before closing?",
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel,
QMessageBox.Save
)
if reply == QMessageBox.Save:
self.saveFile()
if self.dirty:
event.ignore()
return
elif reply == QMessageBox.Cancel:
event.ignore()
return
# Discard: continue closing
event.accept()