onion_skin_layer.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 PyQt5.QtGui import QColor
  17. from flexlay.blend_func import BlendFunc
  18. from flexlay.gui import EditorMapComponent
  19. from flexlay.pixel_buffer import PixelBuffer
  20. from flexlay.wip.canvas import Canvas
  21. class Surface:
  22. pass
  23. class OnionSkinLayer:
  24. def __init__(self, width, height) -> None:
  25. self.SCALE = 1
  26. self.surface = Surface(PixelBuffer(width / self.SCALE, height / self.SCALE))
  27. self.surface2 = Surface(PixelBuffer(width / self.SCALE, height / self.SCALE))
  28. self.editormaps = []
  29. self.color = []
  30. self.canvas = Canvas(self.surface)
  31. self.canvas.get_gc().clear(QColor(0, 0, 0, 0))
  32. self.canvas.get_gc().flush()
  33. self.canvas.sync_surface()
  34. self.canvas2 = Canvas(self.surface2)
  35. self.canvas2.get_gc().clear(QColor(0, 0, 0, 0))
  36. self.canvas2.get_gc().flush()
  37. self.canvas2.sync_surface()
  38. def draw(self, gc):
  39. # FIXME: We need to stop onion layer to draw onto itself
  40. self.surface.set_blend_func(BlendFunc.one, BlendFunc.one_minus_src_alpha)
  41. self.surface.set_scale(self.SCALE, self.SCALE)
  42. self.surface.draw(0, 0)
  43. def has_bounding_rect(self):
  44. return False
  45. def clear(self):
  46. self.canvas.get_gc().clear(QColor(0, 0, 0, 0))
  47. self.canvas.sync_surface()
  48. def add_map(self, editor_map, color):
  49. self.editormaps.append(editor_map)
  50. self.color.append(color)
  51. def update(self):
  52. self.canvas.get_gc().clear(QColor(0, 0, 0, 0))
  53. for i, e in enumerate(self, self.editormaps):
  54. self.canvas2.get_gc().clear(QColor(0, 0, 0, 0))
  55. self.canvas2.get_gc().push_modelview()
  56. self.canvas2.get_gc().add_scale(1.0 / self.SCALE, 1.0 / self.SCALE)
  57. self.editormaps[i].draw(EditorMapComponent.current.get_gc_state(), self.canvas2.get_gc())
  58. self.canvas2.get_gc().pop_modelview()
  59. self.canvas2.sync_surface()
  60. self.surface2.set_color(self.color[i])
  61. self.surface2.draw(0, 0, self.canvas.get_gc())
  62. self.canvas.sync_surface()
  63. # EOF #