new_level.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2015 Karkus476 <karkus476@yahoo.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. from typing import Any, Optional
  17. import os
  18. import random
  19. from PyQt5.QtWidgets import QWidget, QWizardPage, QTextEdit, QVBoxLayout
  20. from flexlay.util.config import Config
  21. from flexlay.gui.properties_widget import PropertiesWidget
  22. from flexlay.gui.generic_wizard import GenericWizard
  23. from supertux.level import Level
  24. class NewLevelWizard(GenericWizard):
  25. def __init__(self, parent: QWidget) -> None:
  26. super().__init__(parent, "New Level Wizard")
  27. # Modal means you cannot touch the parent until this
  28. # closes
  29. self.setModal(True)
  30. assert Config.current is not None
  31. self.level: Optional[Level] = Level()
  32. self.level.name = "Unnamed Level"
  33. self.level.author = Config.current.name
  34. self.level.music = ""
  35. self.level.background = ""
  36. self.level.license = "GPL 2+ / CC-by-sa 3.0"
  37. self.level.width = 300
  38. self.level.height = 35
  39. # self.level.spawn = (5, 25)
  40. # Each function returns a QWizardPage, which is then added
  41. self.add_page("Create A New Level", self.create_intro_page())
  42. self.add_page("General Info", self.create_main_page())
  43. self.addPage(self.create_license_page())
  44. # When 'Finish' pressed run finish()
  45. self.finish_callback.connect(self.finish)
  46. # When 'Cancel' pressed run cancel()
  47. self.rejected.connect(self.cancel)
  48. def finish(self, *pages: Any) -> None:
  49. """Executed when "Finish" button clicked"""
  50. main_page_data = pages[1]
  51. assert Config.current is not None
  52. # FIXME: Don't create game data in GUI code
  53. self.level = Level.from_size(main_page_data[2], main_page_data[3])
  54. # self.level.current_sector.music = self.level.music
  55. # self.level.current_sector.background = self.level.image
  56. self.level.name = main_page_data[0]
  57. self.level.author = main_page_data[1]
  58. Config.current.name = self.level.author
  59. assert self.level.current_sector is not None
  60. self.level.current_sector.music = main_page_data[4]
  61. # self.level.spawn = ceil(main_page_data[2] / 10), int(main_page_data[3] / 2)
  62. self.level.tileset_path = os.path.join("images", "tiles.strf")
  63. # Not Implemented
  64. def cancel(self) -> None:
  65. self.level = None
  66. def create_intro_page(self) -> PropertiesWidget:
  67. """Creates the intro page containing a bit of text
  68. :return: PropertiesWidget Introduction Page
  69. """
  70. page_widget = PropertiesWidget(self)
  71. page_widget.add_label("Click 'Next' to continue.\n" +
  72. "In the future, this page will show some " +
  73. "checkboxes to allow experienced users " +
  74. "to view extra pages in this wizard.")
  75. return page_widget
  76. def create_main_page(self) -> PropertiesWidget:
  77. """Creates the main wizard page
  78. :return: PropertiesWidget to add to GenericWidget
  79. """
  80. page_widget = PropertiesWidget(self)
  81. # A list of names randomly selected as the 'Example
  82. # Level name.
  83. # Feel free to add more
  84. fun_names = ("Fire Meets Ice",
  85. "Flipper Hell",
  86. "Level 4",
  87. "Into the Deep",
  88. "Unknown Flying Penguin")
  89. # choice is random.choice
  90. assert self.level is not None
  91. self.level.name = "Example: " + random.choice(fun_names)
  92. assert Config.current is not None
  93. page_widget.add_string("Level Name:", self.level.name, None)
  94. page_widget.add_string("Level Author:", self.level.author, None)
  95. page_widget.add_int("Level Width:", self.level.width, None)
  96. page_widget.add_int("Level Height:", self.level.height, None)
  97. page_widget.add_file("Level Music", "",
  98. ret_rel_to=Config.current.datadir,
  99. show_rel_to=Config.current.datadir,
  100. open_in=os.path.join(Config.current.datadir, "music"))
  101. page_widget.add_file("Level Background", "",
  102. ret_rel_to=Config.current.datadir,
  103. show_rel_to=Config.current.datadir,
  104. open_in=os.path.join(Config.current.datadir, "images", "background"))
  105. return page_widget
  106. def create_license_page(self) -> QWizardPage:
  107. """Creates the license page
  108. @return QWizardPage The License Page of the wizard
  109. """
  110. page = QWizardPage()
  111. page.setTitle("Level License")
  112. page.setSubTitle("You must set a license for your level, which " +
  113. "defines how others may use and share your level. " +
  114. "In the spirit of 'free and open source' " +
  115. "we ask that you make your level free " +
  116. "(as in 'free speech' not 'free WiFi')")
  117. self.license_input = QTextEdit("GPL 2+ / CC-by-sa 3.0")
  118. self.set_license()
  119. self.license_input.textChanged.connect(self.set_license)
  120. vbox = QVBoxLayout()
  121. vbox.addWidget(self.license_input)
  122. page.setLayout(vbox)
  123. return page
  124. def set_license(self) -> None: # Connected to signal
  125. assert self.level is not None
  126. self.level.license = self.license_input.toPlainText()
  127. # EOF #