blob: 0a944e1bda573bc3a1f21df1a30221877d0551cb [file] [log] [blame]
Tabitha Jarvis1007a132018-12-12 21:47:54 -08001#!/usr/bin/python3
Tabitha Jarvis1007a132018-12-12 21:47:54 -08002from __future__ import print_function
3import os
Andrew Runke0f945fd2019-01-27 21:10:37 -08004import sys
Ravago Jones6d460fe2021-07-03 16:59:55 -07005from color import palette
6from graph import Graph
Tabitha Jarvis1007a132018-12-12 21:47:54 -08007import gi
8import numpy as np
Ravago Jones5127ccc2022-07-31 16:32:45 -07009
Tabitha Jarvis1007a132018-12-12 21:47:54 -080010gi.require_version('Gtk', '3.0')
Andrew Runke6842bf92019-01-26 15:38:25 -080011from gi.repository import Gdk, Gtk, GLib
Tabitha Jarvis1007a132018-12-12 21:47:54 -080012import cairo
Ravago Jonesb170ed32022-06-01 21:16:15 -070013from libspline import Spline, DistanceSpline
Tabitha Jarvis1007a132018-12-12 21:47:54 -080014import enum
John Park91e69732019-03-03 13:12:43 -080015import json
Ravago Jonesfa8da562022-07-02 18:10:22 -070016import copy
Ravago Jones797c49c2021-07-31 14:51:59 -070017from constants import FIELD
18from constants import get_json_folder
19from constants import ROBOT_SIDE_TO_BALL_CENTER, ROBOT_SIDE_TO_HATCH_PANEL, HATCH_PANEL_WIDTH, BALL_RADIUS
Ravago Jones6d460fe2021-07-03 16:59:55 -070020from drawing_constants import set_color, draw_px_cross, draw_px_x, display_text, draw_control_points
Ravago Jonesfa8da562022-07-02 18:10:22 -070021from multispline import Multispline, ControlPointIndex
Ravago Jones6d460fe2021-07-03 16:59:55 -070022import time
Andrew Runke6842bf92019-01-26 15:38:25 -080023
Tabitha Jarvis1007a132018-12-12 21:47:54 -080024
25class Mode(enum.Enum):
26 kViewing = 0
27 kPlacing = 1
28 kEditing = 2
Andrew Runke6842bf92019-01-26 15:38:25 -080029
30
Ravago Jones6d460fe2021-07-03 16:59:55 -070031class FieldWidget(Gtk.DrawingArea):
Andrew Runke6842bf92019-01-26 15:38:25 -080032 """Create a GTK+ widget on which we will draw using Cairo"""
Ravago Jones5127ccc2022-07-31 16:32:45 -070033
Tabitha Jarvis1007a132018-12-12 21:47:54 -080034 def __init__(self):
Ravago Jones6d460fe2021-07-03 16:59:55 -070035 super(FieldWidget, self).__init__()
Ravago Jones086a8872021-08-07 15:49:40 -070036 self.set_field(FIELD)
Ravago Jones8da89c42022-07-17 19:34:06 -070037 self.set_size_request(self.mToPx(self.field.width),
38 self.mToPx(self.field.length))
Tabitha Jarvis1007a132018-12-12 21:47:54 -080039
Ravago Jonesfa8da562022-07-02 18:10:22 -070040 self.multisplines = []
Ravago Jones6d460fe2021-07-03 16:59:55 -070041 self.graph = Graph()
Ravago Jones0a1d4092022-06-03 12:47:32 -070042 self.graph.cursor_watcher = self
Ravago Jones6d460fe2021-07-03 16:59:55 -070043 self.set_vexpand(True)
44 self.set_hexpand(True)
Ravago Jonesfa8da562022-07-02 18:10:22 -070045 self.undo_history = []
Tabitha Jarvis1007a132018-12-12 21:47:54 -080046 # init field drawing
47 # add default spline for testing purposes
48 # init editing / viewing modes and pointer location
49 self.mode = Mode.kPlacing
Jason Anderson-Youngb3378152022-10-26 20:07:37 -070050 self.previous_mode = Mode.kPlacing
Ravago Jones6d460fe2021-07-03 16:59:55 -070051 self.mousex = 0
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -070052 self.lastx = 0
Ravago Jones6d460fe2021-07-03 16:59:55 -070053 self.mousey = 0
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -070054 self.lasty = 0
55 self.drag_start = None
Ravago Jones6d460fe2021-07-03 16:59:55 -070056 self.module_path = os.path.dirname(os.path.realpath(sys.argv[0]))
57 self.path_to_export = os.path.join(self.module_path,
John Park91e69732019-03-03 13:12:43 -080058 'points_for_pathedit.json')
59
Tabitha Jarvis1007a132018-12-12 21:47:54 -080060 # For the editing mode
Ravago Jonesfa8da562022-07-02 18:10:22 -070061 self.control_point_index = None
Ravago Jones359bbd22022-07-07 01:07:02 -070062 self.active_multispline_index = 0
Tabitha Jarvis1007a132018-12-12 21:47:54 -080063
Ravago Jones54dafeb2022-03-02 20:41:47 -080064 self.zoom_transform = cairo.Matrix()
Ravago Jones797c49c2021-07-31 14:51:59 -070065
Ravago Jones76ecec82021-08-07 14:37:08 -070066 self.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
67 | Gdk.EventMask.BUTTON_PRESS_MASK
68 | Gdk.EventMask.BUTTON_RELEASE_MASK
69 | Gdk.EventMask.POINTER_MOTION_MASK
70 | Gdk.EventMask.SCROLL_MASK)
71
Ravago Jonesfa8da562022-07-02 18:10:22 -070072 @property
73 def active_multispline(self):
74 """Get the current active multispline or create a new one"""
75 if not self.multisplines:
76 self.multisplines.append(Multispline())
Ravago Jones359bbd22022-07-07 01:07:02 -070077 self.active_multispline_index = len(self.multisplines) - 1
Ravago Jonesfa8da562022-07-02 18:10:22 -070078
79 return self.multisplines[self.active_multispline_index]
80
Ravago Jones086a8872021-08-07 15:49:40 -070081 def set_field(self, field):
82 self.field = field
Ravago Jonesc26b9162021-06-30 20:12:48 -070083 try:
Ravago Jones6d460fe2021-07-03 16:59:55 -070084 self.field_png = cairo.ImageSurface.create_from_png(
Ravago Jones086a8872021-08-07 15:49:40 -070085 "frc971/control_loops/python/field_images/" +
86 self.field.field_id + ".png")
Ravago Jonesc26b9162021-06-30 20:12:48 -070087 except cairo.Error:
88 self.field_png = None
Ravago Jones54dafeb2022-03-02 20:41:47 -080089
Ravago Jones086a8872021-08-07 15:49:40 -070090 self.queue_draw()
Ravago Jonesc26b9162021-06-30 20:12:48 -070091
Ravago Jones54dafeb2022-03-02 20:41:47 -080092 def invert(self, transform):
93 xx, yx, xy, yy, x0, y0 = transform
94 matrix = cairo.Matrix(xx, yx, xy, yy, x0, y0)
95 matrix.invert()
96 return matrix
97
Ravago Jones797c49c2021-07-31 14:51:59 -070098 # returns the transform from widget space to field space
99 @property
100 def input_transform(self):
Ravago Jones797c49c2021-07-31 14:51:59 -0700101 # the transform for input needs to be the opposite of the transform for drawing
Ravago Jones54dafeb2022-03-02 20:41:47 -0800102 return self.invert(self.field_transform.multiply(self.zoom_transform))
103
104 @property
105 def field_transform(self):
106 field_transform = cairo.Matrix()
Ravago Jones8da89c42022-07-17 19:34:06 -0700107 field_transform.scale(1, -1) # flipped y-axis
Ravago Jones54dafeb2022-03-02 20:41:47 -0800108 field_transform.scale(1 / self.pxToM_scale(), 1 / self.pxToM_scale())
Ravago Jones8da89c42022-07-17 19:34:06 -0700109 field_transform.translate(self.field.width / 2,
110 -1 * self.field.length / 2)
Ravago Jones54dafeb2022-03-02 20:41:47 -0800111 return field_transform
Ravago Jones797c49c2021-07-31 14:51:59 -0700112
113 # returns the scale from pixels in field space to meters in field space
114 def pxToM_scale(self):
115 available_space = self.get_allocation()
Ravago Jones086a8872021-08-07 15:49:40 -0700116 return np.maximum(self.field.width / available_space.width,
117 self.field.length / available_space.height)
Ravago Jones797c49c2021-07-31 14:51:59 -0700118
119 def pxToM(self, p):
120 return p * self.pxToM_scale()
121
122 def mToPx(self, m):
123 return m / self.pxToM_scale()
124
Ravago Jonesb170ed32022-06-01 21:16:15 -0700125 def draw_robot_at_point(self, cr, spline, t):
126 """Draws the robot at a point along a Spline or DistanceSpline"""
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800127
Ravago Jonesb170ed32022-06-01 21:16:15 -0700128 # we accept both Spline and DistanceSpline
129 if type(spline) is Spline:
130 point = spline.Point(t)
131 theta = spline.Theta(t)
132 elif type(spline) is DistanceSpline:
133 point = spline.XY(t)
134 theta = spline.Theta(t)
135 else:
136 raise TypeError(
137 f"expected Spline or DistanceSpline (got {type(spline)})")
John Park91e69732019-03-03 13:12:43 -0800138
Ravago Jonesb170ed32022-06-01 21:16:15 -0700139 # Transform so that +y is forward along the spline
140 transform = cairo.Matrix()
141 transform.translate(*point)
142 transform.rotate(theta - np.pi / 2)
John Park91e69732019-03-03 13:12:43 -0800143
Ravago Jonesb170ed32022-06-01 21:16:15 -0700144 cr.save()
145 cr.set_matrix(transform.multiply(cr.get_matrix()))
John Park91e69732019-03-03 13:12:43 -0800146
Ravago Jonesb170ed32022-06-01 21:16:15 -0700147 # Draw Robot
148 set_color(cr, palette["BLACK"])
149 cr.rectangle(-self.field.robot.width / 2, -self.field.robot.length / 2,
150 self.field.robot.width, self.field.robot.length)
John Park91e69732019-03-03 13:12:43 -0800151 cr.stroke()
152
153 #Draw Ball
154 set_color(cr, palette["ORANGE"], 0.5)
Ravago Jonesb170ed32022-06-01 21:16:15 -0700155 cr.arc(0, self.field.robot.length / 2 + BALL_RADIUS, BALL_RADIUS, 0,
156 2 * np.pi)
John Park91e69732019-03-03 13:12:43 -0800157 cr.stroke()
158
Ravago Jonesb170ed32022-06-01 21:16:15 -0700159 # undo the transform
160 cr.restore()
John Park91e69732019-03-03 13:12:43 -0800161
Ravago Jones6d460fe2021-07-03 16:59:55 -0700162 def do_draw(self, cr): # main
Ravago Jones8da89c42022-07-17 19:34:06 -0700163 cr.set_matrix(
164 self.field_transform.multiply(self.zoom_transform).multiply(
165 cr.get_matrix()))
Ravago Jones797c49c2021-07-31 14:51:59 -0700166
James Kuszmaul1c933e02020-03-07 16:17:51 -0800167 cr.save()
Ravago Jones797c49c2021-07-31 14:51:59 -0700168
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800169 set_color(cr, palette["BLACK"])
Ravago Jones5f787df2021-01-23 16:26:27 -0800170
Henry Speiser51be5c62022-03-13 23:14:36 -0700171 cr.set_line_width(self.pxToM(1))
Ravago Jones8da89c42022-07-17 19:34:06 -0700172 cr.rectangle(-0.5 * self.field.width, -0.5 * self.field.length,
173 self.field.width, self.field.length)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800174 cr.set_line_join(cairo.LINE_JOIN_ROUND)
175 cr.stroke()
Ravago Jones5f787df2021-01-23 16:26:27 -0800176
Ravago Jonesc26b9162021-06-30 20:12:48 -0700177 if self.field_png:
178 cr.save()
Ravago Jones54dafeb2022-03-02 20:41:47 -0800179 cr.translate(-0.5 * self.field.width, 0.5 * self.field.length)
Ravago Jonesc26b9162021-06-30 20:12:48 -0700180 cr.scale(
Ravago Jones54dafeb2022-03-02 20:41:47 -0800181 self.field.width / self.field_png.get_width(),
182 -self.field.length / self.field_png.get_height(),
Ravago Jones6d460fe2021-07-03 16:59:55 -0700183 )
Ravago Jonesc26b9162021-06-30 20:12:48 -0700184 cr.set_source_surface(self.field_png)
185 cr.paint()
186 cr.restore()
Andrew Runke6842bf92019-01-26 15:38:25 -0800187
John Park91e69732019-03-03 13:12:43 -0800188 # update everything
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800189
Henry Speiser51be5c62022-03-13 23:14:36 -0700190 cr.set_line_width(self.pxToM(1))
John Park91e69732019-03-03 13:12:43 -0800191 if self.mode == Mode.kPlacing or self.mode == Mode.kViewing:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800192 set_color(cr, palette["BLACK"])
Ravago Jonesfa8da562022-07-02 18:10:22 -0700193 for multispline in self.multisplines:
194 for i, point in enumerate(multispline.staged_points):
195 draw_px_x(cr, point[0], point[1], self.pxToM(2))
Jason Anderson-Youngb3378152022-10-26 20:07:37 -0700196 if len(self.multisplines) != 0 and self.multisplines[0].getSplines(
197 ): #still in testing
198 self.draw_splines(cr)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800199 elif self.mode == Mode.kEditing:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700200 if len(self.multisplines) != 0 and self.multisplines[0].getSplines(
201 ):
John Park91e69732019-03-03 13:12:43 -0800202 self.draw_splines(cr)
Ravago Jonesfa8da562022-07-02 18:10:22 -0700203
Ravago Jones5127ccc2022-07-31 16:32:45 -0700204 for i, points in enumerate(
205 self.active_multispline.getSplines()):
Ravago Jones8da89c42022-07-17 19:34:06 -0700206 points = [np.array([x, y]) for (x, y) in points]
207 draw_control_points(cr,
208 points,
209 width=self.pxToM(5),
210 radius=self.pxToM(2))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800211
Ravago Jones36c92f02021-07-24 16:35:33 -0700212 p0, p1, p2, p3, p4, p5 = points
John Park91e69732019-03-03 13:12:43 -0800213 first_tangent = p0 + 2.0 * (p1 - p0)
214 second_tangent = p5 + 2.0 * (p4 - p5)
215 cr.set_source_rgb(0, 0.5, 0)
Ravago Jonesb170ed32022-06-01 21:16:15 -0700216 cr.move_to(*p0)
Ravago Jones54dafeb2022-03-02 20:41:47 -0800217 cr.set_line_width(self.pxToM(1.0))
Ravago Jonesb170ed32022-06-01 21:16:15 -0700218 cr.line_to(*first_tangent)
219 cr.move_to(*first_tangent)
220 cr.line_to(*p2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800221
Ravago Jonesb170ed32022-06-01 21:16:15 -0700222 cr.move_to(*p5)
223 cr.line_to(*second_tangent)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800224
Ravago Jonesb170ed32022-06-01 21:16:15 -0700225 cr.move_to(*second_tangent)
226 cr.line_to(*p3)
John Park91e69732019-03-03 13:12:43 -0800227
228 cr.stroke()
Ravago Jones54dafeb2022-03-02 20:41:47 -0800229 cr.set_line_width(self.pxToM(2))
John Park91e69732019-03-03 13:12:43 -0800230
Ravago Jones7db1c502022-07-09 02:13:08 -0700231 set_color(cr, palette["WHITE"])
John Park91e69732019-03-03 13:12:43 -0800232 cr.paint_with_alpha(0.2)
233
Henry Speiser51be5c62022-03-13 23:14:36 -0700234 draw_px_cross(cr, self.mousex, self.mousey, self.pxToM(2))
James Kuszmaul1c933e02020-03-07 16:17:51 -0800235 cr.restore()
Ravago Jones6d460fe2021-07-03 16:59:55 -0700236
John Park91e69732019-03-03 13:12:43 -0800237 def draw_splines(self, cr):
Ravago Jonesb170ed32022-06-01 21:16:15 -0700238 mouse = np.array((self.mousex, self.mousey))
239
Ravago Jonesfa8da562022-07-02 18:10:22 -0700240 multispline, result = Multispline.nearest_distance(
241 self.multisplines, mouse)
Ravago Jonesb170ed32022-06-01 21:16:15 -0700242
Ravago Jonesac952dd2022-07-29 21:44:12 -0700243 if self.graph.cursor is not None and self.graph.data is not None:
Ravago Jones291f5592022-07-07 20:40:37 -0700244 multispline_index, x = self.graph.find_cursor()
Ravago Jonesfa8da562022-07-02 18:10:22 -0700245 distance_spline = DistanceSpline(
Ravago Jones291f5592022-07-07 20:40:37 -0700246 self.multisplines[multispline_index].getLibsplines())
Ravago Jonesfa8da562022-07-02 18:10:22 -0700247
248 self.draw_robot_at_point(cr, distance_spline, x)
249
250 # clear the cursor each draw so it doesn't persist
Ravago Jones0a1d4092022-06-03 12:47:32 -0700251 # after you move off the spline
252 self.graph.cursor = None
Ravago Jonesac952dd2022-07-29 21:44:12 -0700253 elif result is not None and result.fun < 2:
254 distance_spline = DistanceSpline(multispline.getLibsplines())
255 x = result.x[0]
256
257 # draw the robot to show its width
258 self.draw_robot_at_point(cr, distance_spline, x)
259
260 multispline_index = self.multisplines.index(multispline)
261 self.graph.place_cursor(multispline_index, distance=result.x[0])
John Park91e69732019-03-03 13:12:43 -0800262
Ravago Jones7db1c502022-07-09 02:13:08 -0700263 for multispline in self.multisplines:
264 for i, spline in enumerate(multispline.getLibsplines()):
265 alpha = 1 if multispline == self.active_multispline else 0.2
266 set_color(cr, palette["BLACK"], alpha)
267
268 # draw lots of really small line segments to
269 # approximate the shape of the spline
270 for k in np.linspace(0.005, 1, 200):
271 cr.move_to(*spline.Point(k - 0.008))
272 cr.line_to(*spline.Point(k))
273 cr.stroke()
274
275 if i == 0:
276 self.draw_robot_at_point(cr, spline, 0)
277 self.draw_robot_at_point(cr, spline, 1)
278
John Park909c0392020-03-05 23:56:30 -0800279 def export_json(self, file_name):
Ravago Jones09f59722021-03-03 21:11:41 -0800280 self.path_to_export = os.path.join(
281 self.module_path, # position of the python
282 "../../..", # root of the repository
Ravago Jones086a8872021-08-07 15:49:40 -0700283 get_json_folder(self.field), # path from the root
Ravago Jones09f59722021-03-03 21:11:41 -0800284 file_name # selected file
285 )
Ravago Jones3b92afa2021-02-05 14:27:32 -0800286
Ravago Jones6d460fe2021-07-03 16:59:55 -0700287 # Will export to json file
Ravago Jonesfa8da562022-07-02 18:10:22 -0700288 multisplines_object = [
289 multispline.toJsonObject() for multispline in self.multisplines
290 ]
291 print(multisplines_object)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700292 with open(self.path_to_export, mode='w') as points_file:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700293 json.dump(multisplines_object, points_file)
John Park909c0392020-03-05 23:56:30 -0800294
295 def import_json(self, file_name):
Ravago Jones09f59722021-03-03 21:11:41 -0800296 self.path_to_export = os.path.join(
297 self.module_path, # position of the python
298 "../../..", # root of the repository
Ravago Jones086a8872021-08-07 15:49:40 -0700299 get_json_folder(self.field), # path from the root
Ravago Jones09f59722021-03-03 21:11:41 -0800300 file_name # selected file
301 )
302
Ravago Jones6d460fe2021-07-03 16:59:55 -0700303 # import from json file
304 print("LOADING LOAD FROM " + file_name) # Load takes a few seconds
305 with open(self.path_to_export) as points_file:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700306 multisplines_object = json.load(points_file)
307
308 self.attempt_append_multisplines()
309
310 # TODO: Export multisplines in different files
311 if type(multisplines_object) is dict:
312 multisplines_object = [multisplines_object]
313 else:
314 self.multisplines = []
John Park909c0392020-03-05 23:56:30 -0800315
Ravago Jones6d460fe2021-07-03 16:59:55 -0700316 # if people messed with the spline json,
317 # it might not be the right length
318 # so give them a nice error message
Ravago Jonesfa8da562022-07-02 18:10:22 -0700319 for multispline_object in multisplines_object:
320 print(multispline_object)
321 try: # try to salvage as many segments of the spline as possible
322 self.multisplines.append(
323 Multispline.fromJsonObject(multispline_object))
324 except IndexError:
325 # check if they're both 6+5*(k-1) long
326 expected_length = 6 + 5 * (multispline_object["spline_count"] -
327 1)
328 x_len = len(multispline_object["spline_x"])
329 y_len = len(multispline_object["spline_x"])
330 if x_len is not expected_length:
331 print(
332 "Error: spline x values were not the expected length; expected {} got {}"
333 .format(expected_length, x_len))
334 elif y_len is not expected_length:
335 print(
336 "Error: spline y values were not the expected length; expected {} got {}"
337 .format(expected_length, y_len))
Ravago Jones3b92afa2021-02-05 14:27:32 -0800338
Ravago Jones6d460fe2021-07-03 16:59:55 -0700339 print("SPLINES LOADED")
340 self.mode = Mode.kEditing
Ravago Jones76ecec82021-08-07 14:37:08 -0700341 self.queue_draw()
Ravago Jonesfa8da562022-07-02 18:10:22 -0700342 self.graph.schedule_recalculate(self.multisplines)
John Park909c0392020-03-05 23:56:30 -0800343
Ravago Jonesfa8da562022-07-02 18:10:22 -0700344 def attempt_append_multisplines(self):
345 if len(self.undo_history
346 ) == 0 or self.multisplines != self.undo_history[-1]:
347 self.undo_history.append(copy.deepcopy(self.multisplines))
Ryan Yind8be3882021-10-13 20:59:41 -0700348
Ravago Jonesfa8da562022-07-02 18:10:22 -0700349 def clear(self, should_attempt_append=True):
Ryan Yind8be3882021-10-13 20:59:41 -0700350 if should_attempt_append:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700351 self.attempt_append_multisplines()
352 self.multisplines = []
Ravago Jones7db1c502022-07-09 02:13:08 -0700353 self.active_multispline_index = 0
354 self.control_point_index = None
Ryan Yin85f861f2021-09-16 17:55:11 -0700355 #recalulate graph using new points
356 self.graph.axis.clear()
357 self.graph.queue_draw()
358 #allow placing again
359 self.mode = Mode.kPlacing
360 #redraw entire graph
361 self.queue_draw()
Ryan Yind8be3882021-10-13 20:59:41 -0700362
Ryan Yind8be3882021-10-13 20:59:41 -0700363 def undo(self):
364 try:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700365 self.undo_history.pop()
Ryan Yind8be3882021-10-13 20:59:41 -0700366 except IndexError:
367 return
Ravago Jonesfa8da562022-07-02 18:10:22 -0700368 if len(self.undo_history) == 0:
369 self.clear(should_attempt_append=False) #clear, don't do anything
Ryan Yind8be3882021-10-13 20:59:41 -0700370 return
Ravago Jonesfa8da562022-07-02 18:10:22 -0700371 if len(self.multisplines) > 0 and not any(
372 multispline.staged_points
373 for multispline in self.multisplines):
Ravago Jones8da89c42022-07-17 19:34:06 -0700374 self.mode = Mode.kEditing
Ryan Yind8be3882021-10-13 20:59:41 -0700375 else:
376 self.mode = Mode.kPlacing
Ravago Jonesfa8da562022-07-02 18:10:22 -0700377 self.clear(should_attempt_append=False)
378 self.multisplines = copy.deepcopy(self.undo_history[-1])
Ryan Yind8be3882021-10-13 20:59:41 -0700379 self.queue_draw()
380
Ravago Jones76ecec82021-08-07 14:37:08 -0700381 def do_key_press_event(self, event):
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800382 keyval = Gdk.keyval_to_lower(event.keyval)
Ryan Yind8be3882021-10-13 20:59:41 -0700383 if keyval == Gdk.KEY_z and event.state & Gdk.ModifierType.CONTROL_MASK:
384 self.undo()
Nathan Leong295f7302022-07-23 16:09:31 -0700385
Andrew Runke6842bf92019-01-26 15:38:25 -0800386 if keyval == Gdk.KEY_p:
Nathan Leong295f7302022-07-23 16:09:31 -0700387 self.new_spline()
Ravago Jonesfa8da562022-07-02 18:10:22 -0700388 elif keyval == Gdk.KEY_m:
Nathan Leong295f7302022-07-23 16:09:31 -0700389 self.new_multispline()
390
391 def new_spline(self):
392 self.mode = Mode.kPlacing
393 # F0 = A1
394 # B1 = 2F0 - E0
395 # C1= d0 + 4F0 - 4E0
396 multispline = self.active_multispline
397 if len(multispline.getSplines()) != 0:
398 multispline.extrapolate(multispline.getSplines()[-1])
399 self.queue_draw()
400
401 def new_multispline(self):
402 if len(self.active_multispline.getSplines()) != 0:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700403 self.mode = Mode.kPlacing
Ravago Jones359bbd22022-07-07 01:07:02 -0700404 self.active_multispline_index += 1
405 self.multisplines.insert(self.active_multispline_index,
406 Multispline())
Ravago Jonesfa8da562022-07-02 18:10:22 -0700407
Ravago Jones359bbd22022-07-07 01:07:02 -0700408 prev_multispline = self.multisplines[self.active_multispline_index
409 - 1]
Nathan Leong295f7302022-07-23 16:09:31 -0700410 if len(prev_multispline.getSplines()) != 0:
Ravago Jones359bbd22022-07-07 01:07:02 -0700411 self.active_multispline.extrapolate(
412 prev_multispline.getSplines()[-1])
Ravago Jones128fb992021-07-31 13:56:58 -0700413 self.queue_draw()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800414
Ravago Jones76ecec82021-08-07 14:37:08 -0700415 def do_button_release_event(self, event):
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700416 self.drag_start = None
417
Ravago Jonesfa8da562022-07-02 18:10:22 -0700418 self.attempt_append_multisplines()
Ravago Jones76ecec82021-08-07 14:37:08 -0700419 self.mousex, self.mousey = self.input_transform.transform_point(
420 event.x, event.y)
421 if self.mode == Mode.kEditing:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700422 if self.control_point_index != None:
423 multispline = self.multisplines[
424 self.control_point_index.multispline_index]
Ravago Jones76ecec82021-08-07 14:37:08 -0700425
Ravago Jonesfa8da562022-07-02 18:10:22 -0700426 multispline.setControlPoint(self.control_point_index,
427 self.mousex, self.mousey)
Ravago Jones76ecec82021-08-07 14:37:08 -0700428
Ravago Jones359bbd22022-07-07 01:07:02 -0700429 Multispline.splineExtrapolate(self.multisplines,
430 self.control_point_index)
Ravago Jones76ecec82021-08-07 14:37:08 -0700431
Ravago Jonesfa8da562022-07-02 18:10:22 -0700432 multispline.update_lib_spline()
433 self.graph.schedule_recalculate(self.multisplines)
Ravago Jones359bbd22022-07-07 01:07:02 -0700434 self.queue_draw()
Ravago Jonesfa8da562022-07-02 18:10:22 -0700435
436 self.control_point_index = None
Ravago Jones76ecec82021-08-07 14:37:08 -0700437
438 def do_button_press_event(self, event):
Ravago Jones797c49c2021-07-31 14:51:59 -0700439 self.mousex, self.mousey = self.input_transform.transform_point(
440 event.x, event.y)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700441
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700442 self.lastx = event.x
443 self.lasty = event.y
444
Andrew Runke6842bf92019-01-26 15:38:25 -0800445 if self.mode == Mode.kPlacing:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700446 if self.active_multispline.addPoint(self.mousex, self.mousey):
John Park91e69732019-03-03 13:12:43 -0800447 self.mode = Mode.kEditing
Ravago Jonesfa8da562022-07-02 18:10:22 -0700448 self.graph.schedule_recalculate(self.multisplines)
Andrew Runke6842bf92019-01-26 15:38:25 -0800449 elif self.mode == Mode.kEditing:
Ravago Jonesfa8da562022-07-02 18:10:22 -0700450 # Now after we have no control point index,
451 # the user can click for new point
452 if self.control_point_index == None:
Andrew Runke6842bf92019-01-26 15:38:25 -0800453 # Get clicked point
454 # Find nearest
455 # Move nearest to clicked
Ravago Jones54dafeb2022-03-02 20:41:47 -0800456 cur_p = [self.mousex, self.mousey]
Ravago Jones7db1c502022-07-09 02:13:08 -0700457
Andrew Runke6842bf92019-01-26 15:38:25 -0800458 # Get the distance between each for x and y
459 # Save the index of the point closest
Ravago Jones7db1c502022-07-09 02:13:08 -0700460 nearest = 0.4 # Max distance away a the selected point can be in meters
Andrew Runke6842bf92019-01-26 15:38:25 -0800461 index_of_closest = 0
Ravago Jones7db1c502022-07-09 02:13:08 -0700462 index_multisplines = self.active_multispline_index
463 multispline = self.active_multispline
464
465 for index_splines, points in enumerate(
466 multispline.getSplines()):
467 for index_points, val in enumerate(points):
468 distance = np.sqrt((cur_p[0] - val[0])**2 +
469 (cur_p[1] - val[1])**2)
470 if distance < nearest:
471 nearest = distance
472 index_of_closest = index_points
473 self.control_point_index = ControlPointIndex(
474 index_multisplines, index_splines,
475 index_points)
476
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700477 if self.control_point_index == None:
478 self.drag_start = (event.x, event.y)
479
Ravago Jones5127ccc2022-07-31 16:32:45 -0700480 multispline, result = Multispline.nearest_distance(
481 self.multisplines, cur_p)
Ravago Jones7db1c502022-07-09 02:13:08 -0700482 if result and result.fun < 0.1:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700483 self.active_multispline_index = self.multisplines.index(
484 multispline)
Ravago Jones7db1c502022-07-09 02:13:08 -0700485
Jason Anderson-Youngb3378152022-10-26 20:07:37 -0700486 elif self.mode == Mode.kViewing:
487
488 if self.control_point_index == None:
489 self.drag_start = (event.x, event.y)
490
Ravago Jones128fb992021-07-31 13:56:58 -0700491 self.queue_draw()
492
Ravago Jones76ecec82021-08-07 14:37:08 -0700493 def do_motion_notify_event(self, event):
Ravago Jones797c49c2021-07-31 14:51:59 -0700494 self.mousex, self.mousey = self.input_transform.transform_point(
495 event.x, event.y)
Ravago Jones359bbd22022-07-07 01:07:02 -0700496 mouse = np.array([self.mousex, self.mousey])
Ravago Jones128fb992021-07-31 13:56:58 -0700497
Ravago Jonesfa8da562022-07-02 18:10:22 -0700498 if self.mode == Mode.kEditing and self.control_point_index != None:
499 multispline = self.multisplines[
500 self.control_point_index.multispline_index]
Ravago Jones359bbd22022-07-07 01:07:02 -0700501
502 multispline.updates_for_mouse_move(self.multisplines,
Ravago Jones5127ccc2022-07-31 16:32:45 -0700503 self.control_point_index, mouse)
Ravago Jones128fb992021-07-31 13:56:58 -0700504
Ravago Jonesfa8da562022-07-02 18:10:22 -0700505 multispline.update_lib_spline()
506 self.graph.schedule_recalculate(self.multisplines)
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700507
Jason Anderson-Youngb3378152022-10-26 20:07:37 -0700508 if self.drag_start != None and self.control_point_index == None:
509 if self.mode == Mode.kEditing or self.mode == Mode.kViewing:
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700510
Jason Anderson-Youngb3378152022-10-26 20:07:37 -0700511 self.zoom_transform.translate(event.x - self.lastx,
512 event.y - self.lasty)
513 self.lastx = event.x
514 self.lasty = event.y
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700515
Ravago Jones76ecec82021-08-07 14:37:08 -0700516 self.queue_draw()
Ravago Jones128fb992021-07-31 13:56:58 -0700517
Ravago Jones76ecec82021-08-07 14:37:08 -0700518 def do_scroll_event(self, event):
Ravago Jones54dafeb2022-03-02 20:41:47 -0800519
Ravago Jones01781202021-08-01 15:25:11 -0700520 self.mousex, self.mousey = self.input_transform.transform_point(
521 event.x, event.y)
522
Ravago Jones54dafeb2022-03-02 20:41:47 -0800523 step_size = self.pxToM(20) # px
Ravago Jones01781202021-08-01 15:25:11 -0700524
525 if event.direction == Gdk.ScrollDirection.UP:
526 # zoom out
527 scale_by = step_size
528 elif event.direction == Gdk.ScrollDirection.DOWN:
529 # zoom in
530 scale_by = -step_size
531 else:
532 return
533
Ravago Jones54dafeb2022-03-02 20:41:47 -0800534 scale = (self.field.width + scale_by) / self.field.width
Ravago Jones01781202021-08-01 15:25:11 -0700535
Ribhav Kaulfa68ba82021-09-11 16:26:57 -0700536 # This restricts the amount it can be scaled.
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700537 if self.zoom_transform.xx <= 0.05:
Ribhav Kaulfa68ba82021-09-11 16:26:57 -0700538 scale = max(scale, 1)
Jason Anderson-Young589e8fe2022-09-28 20:38:49 -0700539 elif self.zoom_transform.xx >= 32:
Ribhav Kaulfa68ba82021-09-11 16:26:57 -0700540 scale = min(scale, 1)
541
Ravago Jonesde18dfe2022-03-16 20:57:19 -0700542 # undo the scaled translation that the old zoom transform did
Ravago Jones8da89c42022-07-17 19:34:06 -0700543 x, y = self.invert(self.zoom_transform).transform_point(
544 event.x, event.y)
Ravago Jonesde18dfe2022-03-16 20:57:19 -0700545
Ravago Jones01781202021-08-01 15:25:11 -0700546 # move the origin to point
Ravago Jonesde18dfe2022-03-16 20:57:19 -0700547 self.zoom_transform.translate(x, y)
Ravago Jones01781202021-08-01 15:25:11 -0700548
549 # scale from new origin
Ravago Jones54dafeb2022-03-02 20:41:47 -0800550 self.zoom_transform.scale(scale, scale)
Ravago Jones01781202021-08-01 15:25:11 -0700551
552 # move back
Ravago Jonesde18dfe2022-03-16 20:57:19 -0700553 self.zoom_transform.translate(-x, -y)
Ravago Jones01781202021-08-01 15:25:11 -0700554
555 self.queue_draw()