sketch_stroke_tool.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 flexlay.wip.stroke import Stroke
  17. from flexlay.wip.sprite_stroke_drawer import SpriteStrokeDrawer
  18. from flexlay.wip.bitmap_layer import BitmapLayer
  19. from flexlay.gui.editor_map_component import EditorMapComponent
  20. from flexlay.math import Point
  21. class Mouse:
  22. pass
  23. class DrawerProperties:
  24. pass
  25. class InputEvent:
  26. pass
  27. class Dab:
  28. pass
  29. class SketchStrokeTool:
  30. def __init__(self) -> None:
  31. self.drawing = False
  32. self.stroke = Stroke()
  33. self.drawer = SpriteStrokeDrawer()
  34. def draw(self, gc):
  35. if self.drawing:
  36. # FIXME: This translation is a bit ugly, layer position should be handled somewhat different
  37. gc.push_modelview()
  38. gc.add_translate(BitmapLayer.current.get_pos().x,
  39. BitmapLayer.current.get_pos().y)
  40. self.stroke.draw(0)
  41. gc.pop_modelview()
  42. else:
  43. parent = EditorMapComponent.current
  44. p = parent.screen2world(Point(Mouse.get_x(), Mouse.get_y()))
  45. s = DrawerProperties.current.get_brush().get_sprite()
  46. s.set_color(DrawerProperties.current.get_color())
  47. # FIXME: when using mouse 1.0, when tablet 0.5
  48. s.set_scale(DrawerProperties.current.get_size() * 0.5,
  49. DrawerProperties.current.get_size() * 0.5)
  50. s.set_alpha(0.5)
  51. s.draw(p.x, p.y)
  52. def on_mouse_up(self, event):
  53. if event.kind == InputEvent.MOUSE_LEFT and self.drawing:
  54. self.drawing = False
  55. parent = EditorMapComponent.current
  56. parent.release_mouse()
  57. self.add_dab(event)
  58. BitmapLayer.current.add_stroke(self.stroke)
  59. def on_mouse_down(self, event):
  60. if event.kind == InputEvent.MOUSE_LEFT:
  61. self.drawing = True
  62. parent = EditorMapComponent.current
  63. parent.grab_mouse()
  64. self.stroke = Stroke()
  65. self.stroke.set_drawer(self.drawer.copy())
  66. self.add_dab(event)
  67. def add_dab(self, event):
  68. parent = EditorMapComponent.current
  69. p = parent.screen2world(event.mouse_pos.to_f())
  70. # FIXME: This is ugly, events relative to the layer should be handled somewhat differently
  71. dab = Dab(p.x - BitmapLayer.current.get_pos().x,
  72. p.y - BitmapLayer.current.get_pos().y)
  73. # FIXME: Make tablet configurable
  74. # if (CL_Display.get_current_window().get_ic().get_mouse_count() >= 4)
  75. # {
  76. # CL_InputDevice tablet = CL_Display.get_current_window().get_ic().get_mouse(5)
  77. # print("Mouse Count: " << CL_Display.get_current_window().get_ic().get_mouse_count() << std.endl
  78. # std.cout << tablet.get_name() << ": "
  79. # for(int i = 0 i < tablet.get_axis_count() ++i)
  80. # std.cout << tablet.get_axis(i) << " "
  81. # std.cout << std.endl
  82. # dab.pressure = tablet.get_axis(2)
  83. # dab.tilt.x = tablet.get_axis(3)
  84. # dab.tilt.y = tablet.get_axis(4)
  85. # }
  86. # std.cout << dab.pressure << " " << dab.tilt.x << " " << dab.tilt.y << std.endl
  87. if dab.pressure == 0: # most likly we are using the mouse
  88. dab.pressure = 1.0
  89. self.stroke.add_dab(dab)
  90. def on_mouse_move(self, event):
  91. if self.drawing:
  92. self.add_dab(event)
  93. def get_drawer(self):
  94. return self.drawer
  95. # EOF #