tileset.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. from typing import Optional, Any
  17. import os
  18. import logging
  19. from flexlay.tile import Tile
  20. from flexlay.tileset import Tileset
  21. from flexlay.pixel_buffer import PixelBuffer
  22. from flexlay.util.config import Config
  23. from flexlay.util.sexpr_reader import get_value_from_tree
  24. from supertux.util import load_lisp
  25. class TileGroup:
  26. def __init__(self, name: str, tiles: list[int]) -> None:
  27. self.name: str = name
  28. self.tiles: list[int] = tiles
  29. # Load game tiles from filename into tileset
  30. class SuperTuxTileset(Tileset):
  31. current: Optional['SuperTuxTileset'] = None
  32. def __init__(self, *params: Any) -> None:
  33. super().__init__(*params)
  34. SuperTuxTileset.current = self
  35. self.tilegroups: list[TileGroup] = []
  36. self.filename: str = ""
  37. def create_ungrouped_tiles_group(self) -> None:
  38. self.tilegroups.append(TileGroup("Ungrouped Tiles", self.get_ungrouped_tiles()))
  39. def get_ungrouped_tiles(self) -> list[int]:
  40. # Searches for tiles which are not yet grouped and creates a group
  41. # for them
  42. ungrouped_tiles = []
  43. for tile in self.get_tiles():
  44. ungrouped = True
  45. for group in self.tilegroups:
  46. if tile not in group.tiles:
  47. ungrouped = False
  48. break
  49. if ungrouped:
  50. ungrouped_tiles.append(tile)
  51. return ungrouped_tiles
  52. def load(self, filename: str) -> None:
  53. logging.info("Loading Tileset: %s" % filename)
  54. self.filename = filename
  55. tree: Any = load_lisp(filename, "supertux-tiles")
  56. tree = tree[1:]
  57. pixelbuffer: Optional[PixelBuffer] = None
  58. for i in tree:
  59. if i[0] == "tiles":
  60. data = i[1:]
  61. width = get_value_from_tree(['width', '_'], data, 1)
  62. # height = get_value_from_tree(['height', '_'], data, 1)
  63. ids = get_value_from_tree(['ids'], data, [])
  64. # attributes = get_value_from_tree(['attributes'], data, [])
  65. image = get_value_from_tree(['image', '_'], data, None)
  66. if not image:
  67. image = get_value_from_tree(['images', '_'], data, None)
  68. if not image:
  69. image = get_value_from_tree(['editor-images', '_'], data, "tiles/auxiliary/notile.png")
  70. assert Config.current is not None
  71. x = 0
  72. y = 0
  73. for tile_id in ids:
  74. pixelbuffer = PixelBuffer.subregion_from_file(os.path.join(Config.current.datadir, "images", image),
  75. x * 32, y * 32, 32, 32)
  76. self.add_tile(tile_id, Tile(pixelbuffer))
  77. x += 1
  78. if x == width:
  79. x = 0
  80. y += 1
  81. elif i[0] == "tile":
  82. data = i[1:]
  83. tile_id = get_value_from_tree(['id', '_'], data, -1)
  84. image = get_value_from_tree(['editor-images', '_'], data, False)
  85. hidden = get_value_from_tree(['hidden', '_'], data, False)
  86. if not image:
  87. image = get_value_from_tree(['images', '_'], data, "tiles/auxiliary/notile.png")
  88. assert Config.current is not None
  89. if isinstance(image, str):
  90. pixelbuffer = PixelBuffer.from_file(
  91. os.path.join(Config.current.datadir, "images", image))
  92. elif isinstance(image, list):
  93. if image[0] == "region":
  94. pixelbuffer = PixelBuffer.subregion_from_file(
  95. os.path.join(Config.current.datadir, "images", image[1]),
  96. image[2], image[3], image[4], image[5])
  97. if not hidden:
  98. if tile_id == 0 or not pixelbuffer:
  99. self.add_tile(tile_id, None)
  100. else:
  101. self.add_tile(tile_id, Tile(pixelbuffer))
  102. elif i[0] == "tilegroup":
  103. data = i[1:]
  104. name = get_value_from_tree(['name', '_'], data, "Unnamed")
  105. tiles = get_value_from_tree(['tiles'], data, [])
  106. if not self.tilegroups:
  107. self.tilegroups = []
  108. self.tilegroups.append(TileGroup(name, tiles))
  109. # EOF #