Refasctoring brush 'events' to be under data and named as such, moved cbindings to libs

This commit is contained in:
2025-09-17 16:22:04 -05:00
parent a5df20bf60
commit 5c72847864
21 changed files with 19 additions and 22 deletions

View File

@@ -0,0 +1,7 @@
from .brush_event import BrushEvent
from .grid import Grid
from .arrow import Arrow
from .line import Line
from .circle import Circle
from .square import Square
from .erase import Erase

View File

@@ -0,0 +1,44 @@
# Python imports
# Lib imports
import cairo
# Application imports
from data.point import Point
from data.points import Points
from . import BrushEvent
class Arrow(BrushEvent):
def __init__(self):
super(Arrow, self).__init__()
self.points: Points = Points()
def __copy__(self):
copy = type(self)()
copy.color = self.color
return copy
def update(self, eve = None):
if len(self.points) < 2:
self.points.append( Point(eve.x, eve.y) )
return
self.points[1] = Point(eve.x, eve.y)
self.is_valid = True
def process(self, brush: cairo.Context):
brush.set_line_width(self.size)
brush.set_line_cap(1) # 0 = BUTT, 1 = ROUND, 2 = SQUARE
brush.set_source_rgba(*self.color)
brush.set_operator(cairo.OPERATOR_OVER);
brush.move_to(self.points[0].x, self.points[0].y)
brush.line_to(self.points[1].x, self.points[1].y)
brush.stroke()

View File

@@ -0,0 +1,27 @@
# Python imports
# Lib imports
import cairo
# Application imports
from data.events.event import Event
class UnboundException(Exception):
...
class BrushEvent(Event):
def __init__(self):
super(BrushEvent, self).__init__()
self.is_valid: bool = False
self.size: int = 12
self.color: [] = [0.0, 0.0, 0.0, 1.0]
def update(self, eve):
raise UnboundException("Method hasn't been overriden...")
def process(self, brush: cairo.Context):
raise UnboundException("Method hasn't been overriden...")

View File

@@ -0,0 +1,37 @@
# Python imports
# Lib imports
import cairo
# Application imports
from data.point import Point
from data.points import Points
from . import BrushEvent
class Circle(BrushEvent):
def __init__(self):
super(Circle, self).__init__()
self.points: Points = Points()
def __copy__(self):
copy = type(self)()
copy.color = self.color
return copy
def update(self, eve):
self.points.append( Point(eve.x, eve.y) )
self.is_valid = True
def process(self, brush: cairo.Context):
brush.set_source_rgba(*self.color)
brush.set_operator(cairo.OPERATOR_OVER);
for point in self.points:
brush.move_to(point.x, point.y)
brush.arc(point.x, point.y, self.size, 0, 2 * 3.14)
brush.fill()

View File

@@ -0,0 +1,42 @@
# Python imports
# Lib imports
import cairo
# Application imports
from data.point import Point
from data.points import Points
from . import BrushEvent
class Erase(BrushEvent):
def __init__(self):
super(Erase, self).__init__()
self.points: Points = Points()
def __copy__(self):
copy = type(self)()
copy.color = self.color
return copy
def update(self, eve):
self.points.append( Point(eve.x, eve.y) )
self.is_valid = True
def process(self, brush: cairo.Context):
if len(self.points) < 2: return
brush.set_line_width(self.size)
brush.set_line_cap(1) # 0 = BUTT, 1 = ROUND, 2 = SQUARE
brush.set_source_rgba(*self.color)
brush.set_operator(cairo.OPERATOR_CLEAR);
brush.move_to(self.points[0].x, self.points[0].y)
for point in self.points[1 : -1]:
brush.line_to(point.x, point.y)
brush.stroke()
brush.move_to(point.x, point.y)

View File

@@ -0,0 +1,46 @@
# Python imports
# Lib imports
import cairo
# Application imports
from . import BrushEvent
class Grid(BrushEvent):
def __init__(self):
super(Grid, self).__init__()
self.width = 800
self.height = 800
self.grid_spacing = 12 # px
self.color = [0.2, 0.2, 0.2, 0.64] # light gray
def __copy__(self):
copy = type(self)()
copy.color = self.color
return copy
def update(self, eve):
self.is_valid = True
def process(self, brush: cairo.Context):
brush.set_source_rgba(*self.color)
# Draw vertical lines
x = 0
while x < self.width:
brush.move_to(x, 0)
brush.line_to(x, self.height)
x += self.grid_spacing
# Draw horizontal lines
y = 0
while y < self.height:
brush.move_to(0, y)
brush.line_to(self.width, y)
y += self.grid_spacing
brush.stroke()

View File

@@ -0,0 +1,42 @@
# Python imports
# Lib imports
import cairo
# Application imports
from data.point import Point
from data.points import Points
from . import BrushEvent
class Line(BrushEvent):
def __init__(self):
super(Line, self).__init__()
self.points: Points = Points()
def __copy__(self):
copy = type(self)()
copy.color = self.color
return copy
def update(self, eve = None):
self.points.append( Point(eve.x, eve.y) )
if len(self.points) < 2: return
self.is_valid = True
def process(self, brush: cairo.Context):
brush.set_line_width(self.size)
brush.set_line_cap(1) # 0 = BUTT, 1 = ROUND, 2 = SQUARE
brush.set_source_rgba(*self.color)
brush.set_operator(cairo.OPERATOR_OVER);
brush.move_to(self.points[0].x, self.points[0].y)
for point in self.points[1 : -1]:
brush.line_to(point.x, point.y)
brush.stroke()
brush.move_to(point.x, point.y)

View File

@@ -0,0 +1,43 @@
# Python imports
# Lib imports
import cairo
# Application imports
from data.point import Point
from data.points import Points
from . import BrushEvent
class Square(BrushEvent):
def __init__(self):
super(Square, self).__init__()
self.points: Points = Points()
def __copy__(self):
copy = type(self)()
copy.color = self.color
return copy
def update(self, eve):
if len(self.points) < 2:
self.points.append( Point(eve.x, eve.y) )
return
self.points[1] = Point(eve.x, eve.y)
self.is_valid = True
def process(self, brush: cairo.Context):
x1, y1 = self.points[0].x, self.points[0].y
x2, y2 = self.points[1].x, self.points[1].y
w = x2 - x1
h = y2 - y1
brush.set_source_rgba(*self.color)
brush.set_operator(cairo.OPERATOR_OVER);
brush.rectangle(x1, y1, w, h)
brush.fill()