Skip to content

Commit

Permalink
fixed line bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nokse22 committed Jan 11, 2024
1 parent 0ba5e20 commit 60c1580
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/tools/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def __init__(self, _canvas, *args, **kwargs):
self.line_direction = [0,0]

self.prev_char = ""
self.prev_char_pos = []
self.prev_pos = []
self.prev_prev_pos = [0,0]
self.prev_pos = [0,0]

@GObject.Property(type=bool, default=False)
def active(self):
Expand Down Expand Up @@ -103,6 +103,12 @@ def on_drag_begin(self, gesture, start_x, start_y):
if not self._active: return
self.start_x = start_x
self.start_y = start_y
if self._line_type == 2:
self.canvas.add_undo_action("Freehand Line")
start_x_char = start_x // self.x_mul
start_y_char = start_y // self.y_mul
self.prev_prev_pos = [start_x_char, start_y_char]
self.prev_pos = [start_x_char, start_y_char]

def on_drag_follow(self, gesture, end_x, end_y):
if not self._active: return
Expand All @@ -124,8 +130,14 @@ def on_drag_follow(self, gesture, end_x, end_y):

if self._line_type == 0:
self.draw_line(start_x_char, start_y_char, width, height, self.line_direction, False)
else:
elif self._line_type == 1:
self.draw_step_line(start_x_char, start_y_char, width, height, False)
elif self._line_type == 2:
if [int(start_x_char + width), int(start_y_char + height)] != self.prev_pos:
self.draw_free_line(start_x_char + width, start_y_char + height, self.prev_pos[0], self.prev_pos[1], self.prev_prev_pos[0], self.prev_prev_pos[1], True)
self.prev_prev_pos = [self.prev_pos[0], self.prev_pos[1]]
self.prev_pos = [start_x_char + width, start_y_char + height]
self.canvas.update()

self.prev_line_pos = [end_x, end_y]

Expand All @@ -144,16 +156,16 @@ def on_drag_end(self, gesture, delta_x, delta_y):
self.prev_x = 0
self.prev_y = 0

self.canvas.add_undo_action("Line")

if self._line_type == 0:
self.canvas.add_undo_action("Cartesian Line")
self.draw_line(start_x_char, start_y_char, width, height, self.line_direction, True)
elif self._line_type == 1:
self.canvas.add_undo_action("Step Line")
self.draw_step_line(start_x_char, start_y_char, width, height, True)
elif self._line_type == 2:
self.prev_char = ""
self.prev_char_pos = []
self.prev_pos = []
self.prev_prev_pos = [0,0]
self.prev_pos = [0,0]

self.canvas.update()

Expand Down Expand Up @@ -295,16 +307,24 @@ def draw_line(self, start_x_char, start_y_char, width, height, direction: list[i
self.canvas.set_char_at(start_x_char + width, start_y_char, end_vertical, draw)
elif width == 0:
if height < 0:
lenght = height - 1
self.canvas.vertical_line(start_x_char, start_y_char + 1, height - 1, end_vertical, draw)
if self._arrow:
self.canvas.set_char_at(start_x_char, start_y_char + height, self.canvas.up_arrow(), draw)
else:
lenght = height + 1
self.canvas.vertical_line(start_x_char, start_y_char, lenght, end_vertical, draw)
self.canvas.vertical_line(start_x_char, start_y_char, height + 1, end_vertical, draw)
if self._arrow:
self.canvas.set_char_at(start_x_char, start_y_char + height, self.canvas.down_arrow(), draw)

elif height == 0:
if width < 0:
lenght = width - 1
self.canvas.horizontal_line(start_y_char, start_x_char + 1, width - 1, start_horizontal, draw)
if self._arrow:
self.canvas.set_char_at(start_x_char + width, start_y_char, self.canvas.left_arrow(), draw)
else:
lenght = width + 1
self.canvas.horizontal_line(start_y_char, start_x_char, lenght, start_horizontal, draw)
self.canvas.horizontal_line(start_y_char, start_x_char, width + 1, start_horizontal, draw)
if self._arrow:
self.canvas.set_char_at(start_x_char + width, start_y_char, self.canvas.right_arrow(), draw)


def normalize_vector(self, vector):
magnitude = math.sqrt(vector[0]**2 + vector[1]**2)
Expand All @@ -319,9 +339,7 @@ def draw_free_line(self, new_x, new_y, old_x, old_y, old_old_x, old_old_y, draw)
prev_prev_pos = [old_old_x, old_old_y]

if prev_pos == [] or pos == prev_pos:
prev_pos = [new_x, new_y]
return
pos = [new_x, new_y]
direction = [int(pos[0] - prev_pos[0]), int(pos[1] - prev_pos[1])]
dir2 = direction
direction = self.normalize_vector(direction)
Expand All @@ -333,8 +351,6 @@ def draw_free_line(self, new_x, new_y, old_x, old_y, old_old_x, old_old_y, draw)
elif direction == [0, 1] or direction == [0, -1]:
self.canvas.set_char_at(new_x, new_y, self.canvas.right_vertical(), draw)

# ["─", "─", "│", "│", "┌", "┐", "┘","└", "┼", "├", "┤", "┴","┬", "▲", "▼", "►", "◄"],

if direction == [1, 0]:
if dir2 != direction:
self.canvas.horizontal_line(new_y, new_x - dir2[0], dir2[0], self.canvas.bottom_horizontal(), draw)
Expand All @@ -351,8 +367,7 @@ def draw_free_line(self, new_x, new_y, old_x, old_y, old_old_x, old_old_y, draw)
self.canvas.set_char_at(prev_pos[0], prev_pos[1], self.canvas.bottom_right(), draw)
else:
self.canvas.set_char_at(prev_pos[0], prev_pos[1], self.canvas.bottom_horizontal(), draw)

if direction == [0, -1]:
elif direction == [0, -1]:
if prev_direction == [1, 0]:
self.canvas.set_char_at(prev_pos[0], prev_pos[1], self.canvas.bottom_right(), draw)
elif prev_direction == [-1, 0]:
Expand All @@ -366,10 +381,3 @@ def draw_free_line(self, new_x, new_y, old_x, old_y, old_old_x, old_old_y, draw)
self.canvas.set_char_at(prev_pos[0], prev_pos[1], self.canvas.top_left(), draw)
else:
self.canvas.set_char_at(prev_pos[0], prev_pos[1], self.canvas.right_vertical(), draw)

# def drawing_function(self, area, cr, width, height, data):
# cr.set_source_rgb(0.208, 0.518, 0.894)
# cr.move_to (self.start_x, self.start_y)
# cr.rel_line_to (self.end_x, self.end_y)
# cr.set_source_rgb(1, 0, 0)
# cr.stroke()

0 comments on commit 60c1580

Please sign in to comment.