layer_move_tool.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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
  17. from flexlay.workspace import Workspace
  18. from flexlay.color import Color
  19. from flexlay.layer import Layer
  20. from flexlay.gui.editor_map_component import EditorMapComponent
  21. from flexlay.math import Pointf
  22. from flexlay.tools.tool import Tool
  23. from flexlay.input_event import InputEvent
  24. from flexlay.graphic_context import GraphicContext
  25. class LayerMoveTool(Tool):
  26. def __init__(self) -> None:
  27. super().__init__()
  28. self.scrolling: bool = False
  29. self.click_pos: Pointf = Pointf(0, 0)
  30. self.old_trans_offset: Pointf = Pointf(0, 0)
  31. self.layer: Optional[Layer] = None
  32. def find_closed_layer(self, pos: Pointf) -> Layer:
  33. assert Workspace.current is not None
  34. layer: Optional[Layer] = None
  35. parent = Workspace.current.get_map()
  36. for i in range(0, parent.get_layer_count()):
  37. layer_i = parent.get_layer(i)
  38. assert layer_i is not None
  39. assert layer_i.get_bounding_rect() is not None
  40. if layer_i.get_bounding_rect().is_inside(pos):
  41. layer = layer_i
  42. assert layer is not None
  43. return layer
  44. def draw(self, gc: GraphicContext) -> None:
  45. assert Workspace.current is not None
  46. for i in range(0, Workspace.current.get_map().get_layer_count()):
  47. layer = Workspace.current.get_map().get_layer(i)
  48. assert layer is not None
  49. if layer.has_bounding_rect():
  50. rect = layer.get_bounding_rect()
  51. gc.draw_line(rect.left, rect.top, rect.right, rect.bottom,
  52. Color(0, 255, 255))
  53. gc.draw_line(rect.left, rect.bottom, rect.right, rect.top,
  54. Color(0, 255, 255))
  55. def on_mouse_up(self, event: InputEvent) -> None:
  56. if self.layer:
  57. self.scrolling = False
  58. self.update(event)
  59. assert EditorMapComponent.current is not None
  60. EditorMapComponent.current.release_mouse()
  61. self.layer = Layer()
  62. def on_mouse_down(self, event: InputEvent) -> None:
  63. assert EditorMapComponent.current is not None
  64. assert event.mouse_pos is not None
  65. parent = EditorMapComponent.current
  66. pos = parent.screen2world(event.mouse_pos.to_f())
  67. self.layer = self.find_closed_layer(pos)
  68. if self.layer:
  69. self.scrolling = True
  70. self.old_trans_offset = self.layer.get_pos()
  71. self.click_pos = pos
  72. EditorMapComponent.current.grab_mouse()
  73. def on_mouse_move(self, event: InputEvent) -> None:
  74. if self.layer and self.scrolling:
  75. self.update(event)
  76. def update(self, event: InputEvent) -> None:
  77. assert EditorMapComponent.current is not None
  78. assert event.mouse_pos is not None
  79. if self.layer:
  80. parent = EditorMapComponent.current
  81. pos = parent.screen2world(event.mouse_pos.to_f())
  82. self.layer.set_pos(self.old_trans_offset + (pos - self.click_pos))
  83. # EOF #