workspace_move_tool.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 math
  17. from flexlay.gui.editor_map_component import EditorMapComponent
  18. from flexlay.math import Point, Pointf
  19. from flexlay.input_event import InputEvent
  20. from flexlay.graphic_context import GraphicContext
  21. from flexlay.tools.tool import Tool
  22. class WorkspaceMoveTool(Tool):
  23. def __init__(self) -> None:
  24. self.scrolling: bool = False
  25. self.click_pos: Point = Point(0, 0)
  26. self.old_trans_offset: Pointf = Pointf(0, 0)
  27. def on_mouse_down(self, event: InputEvent) -> None:
  28. assert EditorMapComponent.current is not None
  29. assert event.mouse_pos is not None
  30. self.scrolling = True
  31. self.old_trans_offset = EditorMapComponent.current.get_gc_state().get_pos()
  32. self.click_pos = event.mouse_pos
  33. EditorMapComponent.current.grab_mouse()
  34. def on_mouse_up(self, event: InputEvent) -> None:
  35. assert event.mouse_pos is not None
  36. assert EditorMapComponent.current is not None
  37. self.scrolling = False
  38. self.update(event)
  39. self.old_trans_offset = EditorMapComponent.current.get_gc_state().get_pos()
  40. EditorMapComponent.current.release_mouse()
  41. def on_mouse_move(self, event: InputEvent) -> None:
  42. if self.scrolling:
  43. self.update(event)
  44. def draw(self, gc: GraphicContext) -> None:
  45. pass
  46. def update(self, event: InputEvent) -> None:
  47. assert EditorMapComponent.current is not None
  48. gc_state = EditorMapComponent.current.get_gc_state()
  49. sa = math.sin(-gc_state.get_rotation() / 180.0 * math.pi)
  50. ca = math.cos(-gc_state.get_rotation() / 180.0 * math.pi)
  51. assert event.mouse_pos is not None
  52. dx = ca * (self.click_pos.x - event.mouse_pos.x) - sa * (self.click_pos.y - event.mouse_pos.y)
  53. dy = sa * (self.click_pos.x - event.mouse_pos.x) + ca * (self.click_pos.y - event.mouse_pos.y)
  54. gc_state.set_pos(Pointf(self.old_trans_offset.x + dx / EditorMapComponent.current.get_gc_state().get_zoom(),
  55. self.old_trans_offset.y + dy / EditorMapComponent.current.get_gc_state().get_zoom()))
  56. # EOF #