supertux.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import signal
  17. import argparse
  18. import os
  19. import logging
  20. from PyQt5.QtCore import QByteArray
  21. from PyQt5.QtWidgets import QMessageBox, QFileDialog
  22. from flexlay.flexlay import Flexlay
  23. from supertux.gui import SuperTuxGUI
  24. from supertux.tileset import SuperTuxTileset
  25. from supertux.config import make_config
  26. def main() -> None:
  27. signal.signal(signal.SIGINT, signal.SIG_DFL)
  28. # Parse Arguments
  29. parser = argparse.ArgumentParser(description="Flexlay - SuperTux Editor")
  30. parser.add_argument("LEVELFILE", action="store", type=str, nargs="?",
  31. help=".stl file to load")
  32. parser.add_argument("-d", "--datadir", metavar="DIR", action="store", type=str,
  33. help="SuperTux data directory directory")
  34. parser.add_argument("-b", "--binary", metavar="BIN", action="store", type=str,
  35. help="SuperTux binary path")
  36. args = parser.parse_args()
  37. # Create flexlay instance
  38. flexlay = Flexlay("SuperTux")
  39. # Load data directory path from config file, --datadir argument or open directory dialog
  40. config = make_config()
  41. if args.datadir is not None:
  42. config.datadir = args.datadir
  43. if not config.datadir or not os.path.isdir(config.datadir):
  44. QMessageBox.warning(None, "No Data Directory Found",
  45. "Press OK to select your Supertux directory")
  46. config.datadir = QFileDialog.getExistingDirectory(None, "Open Data Directory")
  47. if not config.datadir:
  48. raise RuntimeError("datadir missing, use --datadir DIR")
  49. logging.info("Datadir: %s", config.datadir)
  50. # Load supertux binary path from config file, --binary argument or open file dialog
  51. if args.binary and os.path.isfile(args.binary):
  52. config.binary = args.binary
  53. if not config.binary or not os.path.isfile(config.binary):
  54. QMessageBox.warning(None, "No Supertux Binary Found",
  55. "Press OK to select your Supertux binary")
  56. config.binary = QFileDialog.getOpenFileName(None, "Open Supertux Binary")[0]
  57. if not config.binary:
  58. raise RuntimeError("binary path missing, use --binary BIN")
  59. logging.info("Binary path: %s", config.binary)
  60. # Load tileset
  61. tileset = SuperTuxTileset(32)
  62. tileset.load(os.path.join(config.datadir, "images/tiles.strf"))
  63. tileset.create_ungrouped_tiles_group()
  64. gui = SuperTuxGUI(flexlay)
  65. if args.LEVELFILE is not None:
  66. gui.load_level(args.LEVELFILE)
  67. elif len(config.recent_files) > 0:
  68. # Check that at least one file is loaded
  69. any_valid = False
  70. for recent_file_path in reversed(config.recent_files):
  71. try:
  72. gui.load_level(recent_file_path)
  73. except Exception: # TODO: Except only reasonable errors.
  74. config.recent_files.remove(recent_file_path)
  75. continue
  76. else:
  77. any_valid = True
  78. break
  79. if not any_valid:
  80. logging.info("All recent files broken. Creating a new level...")
  81. gui.gui_level_new()
  82. else:
  83. gui.gui_level_new()
  84. # Init the GUI, so that button state is in sync with internal state
  85. gui.gui_toggle_minimap()
  86. gui.gui_toggle_minimap()
  87. gui.set_tilemap_paint_tool()
  88. if config.geometry:
  89. if not gui.gui.window.restoreGeometry(QByteArray.fromBase64(config.geometry.encode("ascii"))):
  90. logging.error("error: failed to restore window geometry")
  91. if config.window_state:
  92. if not gui.gui.window.restoreState(QByteArray.fromBase64(config.window_state.encode("ascii"))):
  93. logging.error("error: failed to restore window state")
  94. gui.run()
  95. config.save()
  96. # EOF #