blob: 9748a41cad7f43a7bca364f2282a3ff4b0d63889 [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 Runke6842bf92019-01-26 15:38:25 -08004import copy
Tabitha Jarvis1007a132018-12-12 21:47:54 -08005import basic_window
6from color import Color, palette
7import random
8import gi
9import numpy as np
Andrew Runke6842bf92019-01-26 15:38:25 -080010from libspline import Spline
Tabitha Jarvis1007a132018-12-12 21:47:54 -080011import scipy.spatial.distance
12gi.require_version('Gtk', '3.0')
Andrew Runke6842bf92019-01-26 15:38:25 -080013from gi.repository import Gdk, Gtk, GLib
Tabitha Jarvis1007a132018-12-12 21:47:54 -080014import cairo
Tabitha Jarvis1007a132018-12-12 21:47:54 -080015import enum
Andrew Runke6842bf92019-01-26 15:38:25 -080016import csv # For writing to csv files
Tabitha Jarvis1007a132018-12-12 21:47:54 -080017
18from basic_window import OverrideMatrix, identity, quit_main_loop, set_color
19
John Park13d3e282019-01-26 20:16:48 -080020WIDTH_OF_FIELD_IN_METERS = 8.258302
Tabitha Jarvis1007a132018-12-12 21:47:54 -080021PIXELS_ON_SCREEN = 300
22
Andrew Runke6842bf92019-01-26 15:38:25 -080023
John Park13d3e282019-01-26 20:16:48 -080024def pxToM(p):
25 return p * WIDTH_OF_FIELD_IN_METERS / PIXELS_ON_SCREEN
Andrew Runke6842bf92019-01-26 15:38:25 -080026
Tabitha Jarvis1007a132018-12-12 21:47:54 -080027
John Park13d3e282019-01-26 20:16:48 -080028def mToPx(i):
29 return (i * PIXELS_ON_SCREEN / WIDTH_OF_FIELD_IN_METERS)
Andrew Runke6842bf92019-01-26 15:38:25 -080030
Tabitha Jarvis1007a132018-12-12 21:47:54 -080031
32def px(cr):
33 return OverrideMatrix(cr, identity)
34
Andrew Runke6842bf92019-01-26 15:38:25 -080035
Tabitha Jarvis1007a132018-12-12 21:47:54 -080036def draw_px_cross(cr, x, y, length_px, color=palette["RED"]):
37 """Draws a cross with fixed dimensions in pixel space."""
38 set_color(cr, color)
39 cr.move_to(x, y - length_px)
40 cr.line_to(x, y + length_px)
41 cr.stroke()
42
43 cr.move_to(x - length_px, y)
44 cr.line_to(x + length_px, y)
45 cr.stroke()
46 set_color(cr, palette["LIGHT_GREY"])
47
Andrew Runke6842bf92019-01-26 15:38:25 -080048
Tabitha Jarvis1007a132018-12-12 21:47:54 -080049def draw_px_x(cr, x, y, length_px1, color=palette["BLACK"]):
50 """Draws a x with fixed dimensions in pixel space."""
51 length_px = length_px1 / np.sqrt(2)
52 set_color(cr, color)
53 cr.move_to(x - length_px, y - length_px)
54 cr.line_to(x + length_px, y + length_px)
55 cr.stroke()
56
57 cr.move_to(x - length_px, y + length_px)
58 cr.line_to(x + length_px, y - length_px)
59 cr.stroke()
60 set_color(cr, palette["LIGHT_GREY"])
61
Andrew Runke6842bf92019-01-26 15:38:25 -080062
Tabitha Jarvis1007a132018-12-12 21:47:54 -080063def draw_points(cr, p, size):
64 for i in range(0, len(p)):
Andrew Runke6842bf92019-01-26 15:38:25 -080065 draw_px_cross(cr, p[i][0], p[i][1], size, Color(
66 0, np.sqrt(0.2 * i), 0))
67
Tabitha Jarvis1007a132018-12-12 21:47:54 -080068
69class Mode(enum.Enum):
70 kViewing = 0
71 kPlacing = 1
72 kEditing = 2
73 kExporting = 3
74 kImporting = 4
Andrew Runke6842bf92019-01-26 15:38:25 -080075 kConstraint = 5
76
77
78class ConstraintType(enum.Enum):
79 kMaxSpeed = 0
80 kMaxAcceleration = 1
81
Tabitha Jarvis1007a132018-12-12 21:47:54 -080082
83def display_text(cr, text, widtha, heighta, widthb, heightb):
84 cr.scale(widtha, -heighta)
85 cr.show_text(text)
86 cr.scale(widthb, -heightb)
87
Andrew Runke6842bf92019-01-26 15:38:25 -080088
89def redraw(needs_redraw, window):
90 print("Redrew")
91 if not needs_redraw:
92 window.queue_draw()
93
94
95class Constraint():
96 def __init__(self, start, end, constraint, value):
97 self.start = start #Array with index and distance from start of spline
98 self.end = end #Array with index and distance from start of spline
99 self.constraint = constraint #INT
100 self.value = value #INT
101 if self.constraint == 0:
102 self.conName = "kMaxSpeed"
103 else:
104 self.conName = "kMaxAcceleration"
105
106 def toString(self):
107
108 return "START: " + str(self.start[0]) + ", " + str(
109 self.start[1]) + " | END: " + str(self.end[0]) + ", " + str(
110 self.end[1]) + " | " + str(self.conName) + ": " + str(
111 self.value)
112
113
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800114class GTK_Widget(basic_window.BaseWindow):
Andrew Runke6842bf92019-01-26 15:38:25 -0800115 """Create a GTK+ widget on which we will draw using Cairo"""
116
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800117 def __init__(self):
118 super(GTK_Widget, self).__init__()
119
120 # init field drawing
121 # add default spline for testing purposes
122 # init editing / viewing modes and pointer location
123 self.mode = Mode.kPlacing
124 self.x = 0
125 self.y = 0
126
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800127 # update list of control points
128 self.point_selected = False
129 # self.adding_spline = False
130 self.index_of_selected = -1
131 self.new_point = []
132
133 # For the editing mode
Andrew Runke6842bf92019-01-26 15:38:25 -0800134 self.index_of_edit = -1 # Can't be zero beause array starts at 0
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800135 self.held_x = 0
Andrew Runke6842bf92019-01-26 15:38:25 -0800136 self.spline_edit = -1
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800137
Andrew Runke6842bf92019-01-26 15:38:25 -0800138 self.curves = []
139
140 self.colors = []
141
142 for c in palette:
143 self.colors.append(palette[c])
144
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800145 self.selected_points = []
Andrew Runke6842bf92019-01-26 15:38:25 -0800146 self.splines = []
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800147 self.reinit_extents()
148
Andrew Runke6842bf92019-01-26 15:38:25 -0800149 self.inStart = None
150 self.inEnd = None
151 self.inConstraint = None
152 self.inValue = None
153 self.startSet = False
154
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800155 #John also wrote this
156 def add_point(self, x, y):
Andrew Runke6842bf92019-01-26 15:38:25 -0800157 if (len(self.selected_points) < 6):
John Park13d3e282019-01-26 20:16:48 -0800158 self.selected_points.append([pxToM(x), pxToM(y)])
Andrew Runke6842bf92019-01-26 15:38:25 -0800159 if (len(self.selected_points) == 6):
160 self.mode = Mode.kEditing
161 self.splines.append(np.array(self.selected_points))
162 self.selected_points = []
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800163
Andrew Runke6842bf92019-01-26 15:38:25 -0800164 """set extents on images"""
165
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800166 def reinit_extents(self):
167 self.extents_x_min = -800
168 self.extents_x_max = 800
169 self.extents_y_min = -800
170 self.extents_y_max = 800
171
172 # this needs to be rewritten with numpy, i dont think this ought to have
173 # SciPy as a dependecy
174 def get_index_of_nearest_point(self):
175 cur_p = [[self.x, self.y]]
176 distances = scipy.spatial.distance.cdist(cur_p, self.all_controls)
177
178 return np.argmin(distances)
179
180 # return the closest point to the loc of the click event
181 def get_nearest_point(self):
182 return self.all_controls[self.get_index_of_nearest_point()]
183
Andrew Runke6842bf92019-01-26 15:38:25 -0800184 def set_index_to_nearest_spline_point(self):
185 nearest = 50
186 index_of_closest = 0
187 self.spline_edit = 0
188 cur_p = [self.x, self.y]
189
190 for index_splines, points in enumerate(self.spline):
John Park13d3e282019-01-26 20:16:48 -0800191 for index_points, point in enumerate(points.curve):
Andrew Runke6842bf92019-01-26 15:38:25 -0800192 # pythagorean
John Park13d3e282019-01-26 20:16:48 -0800193 distance = np.sqrt((cur_p[0] - mToPx(point[0]))**2 +
194 (cur_p[1] - mToPx(point[1])**2))
Andrew Runke6842bf92019-01-26 15:38:25 -0800195 if distance < nearest:
196 nearest = distance
197 print("DISTANCE: ", distance, " | INDEX: ", index_points)
198 index_of_closest = index_points
199 self.index_of_edit = index_of_closest
200 self.spline_edit = index_splines
201 self.held_x = self.x
202 if self.startSet == False:
203 self.inStart = [self.index_of_edit, self.findDistance()]
204 self.startSet = True
205 else:
206 self.inEnd = [self.index_of_edit, self.findDistance()]
Andrew Runke6842bf92019-01-26 15:38:25 -0800207 self.startSet = False
208 self.mode = Mode.kEditing
209 self.spline_edit = -1
210 self.index_of_edit = -1
211
212 print("Nearest: " + str(nearest))
213 print("Spline: " + str(self.spline_edit))
214 print("Index: " + str(index_of_closest))
215
216 def findDistance(self):
217 """ findDistance goes through each point on the spline finding distance to that point from the point before.
218 It does this to find the the length of the spline to the point that is currently selected.
219 """
220 distance = 0
221 points = self.curves[self.spline_edit]
222 for index, point in enumerate(points):
223 if index > 0 and index <= self.index_of_edit:
224 distance += np.sqrt((points[index - 1][0] - point[0])**2 +
225 (points[index - 1][1] - point[1])**2)
John Park13d3e282019-01-26 20:16:48 -0800226 return distance
Andrew Runke6842bf92019-01-26 15:38:25 -0800227
228 # Handle the expose-event by updating the Window and drawing
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800229 def handle_draw(self, cr):
Andrew Runke6842bf92019-01-26 15:38:25 -0800230 # print(self.new_point)
231 # print("SELF.POINT_SELECTED: " + str(self.point_selected))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800232
233 # begin drawing
234 # Fill the background color of the window with grey
235 set_color(cr, palette["GREY"])
236 cr.paint()
Andrew Runkea9c8de52019-01-26 19:54:29 -0800237 #Scale the field to fit within drawing area
Andrew Runke6696bb82019-02-02 14:33:59 -0800238 cr.scale(0.5, 0.5)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800239
240 # Draw a extents rectangle
241 set_color(cr, palette["WHITE"])
242 cr.rectangle(self.extents_x_min, self.extents_y_min,
243 (self.extents_x_max - self.extents_x_min),
244 self.extents_y_max - self.extents_y_min)
245 cr.fill()
246
247 #Drawing the switch and scale in the field
248 cr.move_to(0, 50)
249 cr.show_text('Press "e" to export')
250 cr.show_text('Press "i" to import')
251
252 set_color(cr, Color(0.3, 0.3, 0.3))
Andrew Runke6842bf92019-01-26 15:38:25 -0800253 cr.rectangle(-450, -150, 300, 300)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800254 cr.fill()
255 set_color(cr, palette["BLACK"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800256 cr.rectangle(-450, -150, 300, 300)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800257 cr.set_line_join(cairo.LINE_JOIN_ROUND)
258 cr.stroke()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800259
Andrew Runke6842bf92019-01-26 15:38:25 -0800260 y = 0
Andrew Runke6842bf92019-01-26 15:38:25 -0800261
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800262 # update all the things
263
264 if self.mode == Mode.kViewing:
265 set_color(cr, palette["BLACK"])
266 cr.move_to(-300, 170)
267 cr.show_text("VIEWING")
268 set_color(cr, palette["GREY"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800269
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800270 if len(self.selected_points) > 0:
271 print("SELECTED_POINTS: " + str(len(self.selected_points)))
272 print("ITEMS:")
Andrew Runke6842bf92019-01-26 15:38:25 -0800273 # for item in self.selected_points:
274 # print(str(item))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800275 for i, point in enumerate(self.selected_points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800276 # print("I: " + str(i))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800277 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800278 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800279 display_text(cr, str(i), 0.5, 0.5, 2, 2)
280
Andrew Runke6842bf92019-01-26 15:38:25 -0800281 elif self.mode == Mode.kPlacing:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800282 set_color(cr, palette["BLACK"])
283 cr.move_to(-300, 170)
284 display_text(cr, "ADD", 1, 1, 1, 1)
285 set_color(cr, palette["GREY"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800286
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800287 if len(self.selected_points) > 0:
288 print("SELECTED_POINTS: " + str(len(self.selected_points)))
289 print("ITEMS:")
290 for item in self.selected_points:
291 print(str(item))
292 for i, point in enumerate(self.selected_points):
293 print("I: " + str(i))
John Park13d3e282019-01-26 20:16:48 -0800294 draw_px_x(cr, mToPx(point[0]), mToPx(point[1]), 10)
295 cr.move_to(mToPx(point[0]), mToPx(point[1]) - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800296 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800297
298 elif self.mode == Mode.kEditing:
299 set_color(cr, palette["BLACK"])
300 cr.move_to(-300, 170)
301 display_text(cr, "EDITING", 1, 1, 1, 1)
Andrew Runke6842bf92019-01-26 15:38:25 -0800302 if len(self.splines) > 0:
Andrew Runke6842bf92019-01-26 15:38:25 -0800303 holder_spline = []
304 for i, points in enumerate(self.splines):
305 array = np.zeros(shape=(6, 2), dtype=float)
306 for j, point in enumerate(points):
John Park13d3e282019-01-26 20:16:48 -0800307 array[j, 0] = mToPx(point[0])
308 array[j, 1] = mToPx(point[1])
Andrew Runke6842bf92019-01-26 15:38:25 -0800309 spline = Spline(np.ascontiguousarray(np.transpose(array)))
310 for k in np.linspace(0.01, 1, 100):
Andrew Runke6842bf92019-01-26 15:38:25 -0800311 cr.move_to(
312 spline.Point(k - 0.01)[0],
313 spline.Point(k - 0.01)[1])
314 cr.line_to(spline.Point(k)[0], spline.Point(k)[1])
315 cr.stroke()
316 holding = [
317 spline.Point(k - 0.01)[0],
318 spline.Point(k - 0.01)[1]
319 ]
Andrew Runke6842bf92019-01-26 15:38:25 -0800320 holder_spline.append(holding)
321 self.curves.append(holder_spline)
322
323 for spline, points in enumerate(self.splines):
324 # for item in points:
325 # print(str(item))
326 for i, point in enumerate(points):
327 # print("I: " + str(i))
328 if spline == self.spline_edit and i == self.index_of_edit:
John Park13d3e282019-01-26 20:16:48 -0800329 draw_px_x(cr, mToPx(point[0]), mToPx(point[1]), 15,
Andrew Runke6842bf92019-01-26 15:38:25 -0800330 self.colors[spline])
331 elif (spline == 0 and not i == 5) or (not i == 0
332 and not i == 5):
John Park13d3e282019-01-26 20:16:48 -0800333 draw_px_x(cr, mToPx(point[0]), mToPx(point[1]), 10,
Andrew Runke6842bf92019-01-26 15:38:25 -0800334 self.colors[spline])
John Park13d3e282019-01-26 20:16:48 -0800335 cr.move_to(mToPx(point[0]), mToPx(point[1]) - 15)
Andrew Runke6842bf92019-01-26 15:38:25 -0800336 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800337
338 elif self.mode == Mode.kExporting:
339 set_color(cr, palette["BLACK"])
340 cr.move_to(-300, 170)
341 display_text(cr, "VIEWING", 1, 1, 1, 1)
342 set_color(cr, palette["GREY"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800343
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800344 if len(self.selected_points) > 0:
345 print("SELECTED_POINTS: " + str(len(self.selected_points)))
346 print("ITEMS:")
Andrew Runke6842bf92019-01-26 15:38:25 -0800347 # for item in self.selected_points:
348 # print(str(item))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800349 for i, point in enumerate(self.selected_points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800350 # print("I: " + str(i))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800351 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800352 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800353 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Andrew Runke6842bf92019-01-26 15:38:25 -0800354
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800355 elif self.mode == Mode.kImporting:
356 set_color(cr, palette["BLACK"])
357 cr.move_to(-300, 170)
358 display_text(cr, "VIEWING", 1, 1, 1, 1)
359 set_color(cr, palette["GREY"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800360
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800361 if len(self.selected_points) > 0:
362 print("SELECTED_POINTS: " + str(len(self.selected_points)))
363 print("ITEMS:")
364 for item in self.selected_points:
365 print(str(item))
366 for i, point in enumerate(self.selected_points):
367 print("I: " + str(i))
368 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800369 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800370 display_text(cr, str(i), 0.5, 0.5, 2, 2)
371
Andrew Runke6842bf92019-01-26 15:38:25 -0800372 elif self.mode == Mode.kConstraint:
373 print("Drawn")
374 set_color(cr, palette["BLACK"])
375 cr.move_to(-300, 170)
376 display_text(cr, "Adding Constraint", 1, 1, 1, 1)
377 if len(self.splines) > 0:
378 # print("Splines: " + str(len(self.splines)))
379 # print("ITEMS:")
380 for s, points in enumerate(self.splines):
381 # for item in points:
382 # print(str(item))
383 for i, point in enumerate(points):
384 # print("I: " + str(i))
385 draw_px_x(cr, point[0], point[1], 10, self.colors[s])
386 cr.move_to(point[0], point[1] - 15)
387 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800388
389 cr.paint_with_alpha(.65)
390
391 draw_px_cross(cr, self.x, self.y, 10)
392
393 def do_key_press(self, event):
394 keyval = Gdk.keyval_to_lower(event.keyval)
Andrew Runke6842bf92019-01-26 15:38:25 -0800395 # print("Gdk.KEY_" + Gdk.keyval_name(keyval))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800396 if keyval == Gdk.KEY_q:
397 print("Found q key and exiting.")
398 quit_main_loop()
399 if keyval == Gdk.KEY_e:
400 self.mode = Mode.kExporting
401 # Will export to csv file
402 with open('points_for_pathedit.csv', mode='w') as points_file:
Andrew Runke6842bf92019-01-26 15:38:25 -0800403 writer = csv.writer(
404 points_file,
405 delimiter=',',
406 quotechar='"',
407 quoting=csv.QUOTE_MINIMAL)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800408 for item in self.selected_points:
409 writer.writerow([str(item[0]), str(item[1])])
Andrew Runke6842bf92019-01-26 15:38:25 -0800410 print("Wrote: " + str(item[0]) + " " + str(item[1]))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800411 if keyval == Gdk.KEY_i:
412 self.mode = Mode.kImporting
413 # import from csv file
414 self.selected_points = []
415 with open('points_for_pathedit.csv') as points_file:
416 reader = csv.reader(points_file, delimiter=',')
417 for row in reader:
418 self.add_point(float(row[0]), float(row[1]))
Andrew Runke6842bf92019-01-26 15:38:25 -0800419 print("Added: " + row[0] + " " + row[1])
420 if keyval == Gdk.KEY_p:
421 self.mode = Mode.kPlacing
422 # F0 = A1
423 # B1 = 2F0 - E0
424 # C1= d0 + 4F0 - 4E0
425 spline_index = len(self.splines) - 1
426 self.selected_points = []
427 f = self.splines[spline_index][5]
428 e = self.splines[spline_index][4]
429 d = self.splines[spline_index][3]
430 self.selected_points.append(f)
431 self.selected_points.append(f * 2 + e * -1)
432 self.selected_points.append(d + f * 4 + e * -4)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800433
Andrew Runke6842bf92019-01-26 15:38:25 -0800434 if keyval == Gdk.KEY_c:
435 self.mode = Mode.kConstraint
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800436
437 def button_press_action(self):
Andrew Runke6842bf92019-01-26 15:38:25 -0800438 if self.mode == Mode.kPlacing:
439 #Check that the point clicked is on the field
440 if (self.x < -150 and self.x > -450 and self.y < 150
441 and self.y > -150):
442 self.add_point(self.x, self.y)
443 elif self.mode == Mode.kEditing:
444 # Now after index_of_edit is not -1, the point is selected, so
445 # user can click for new point
446 if self.index_of_edit > -1 and self.held_x != self.x:
447 print("INDEX OF EDIT: " + str(self.index_of_edit))
448 self.splines[self.spline_edit][self.index_of_edit] = [
John Park13d3e282019-01-26 20:16:48 -0800449 pxToM(self.x), pxToM(self.y)
Andrew Runke6842bf92019-01-26 15:38:25 -0800450 ]
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800451
Andrew Runke6842bf92019-01-26 15:38:25 -0800452 if not self.spline_edit == len(self.splines) - 1:
453 spline_edit = self.spline_edit + 1
454 f = self.splines[self.spline_edit][5]
455 e = self.splines[self.spline_edit][4]
456 d = self.splines[self.spline_edit][3]
457 self.splines[spline_edit][0] = f
458 self.splines[spline_edit][1] = f * 2 + e * -1
459 self.splines[spline_edit][2] = d + f * 4 + e * -4
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800460
Andrew Runke6842bf92019-01-26 15:38:25 -0800461 if not self.spline_edit == 0:
462 spline_edit = self.spline_edit - 1
463 a = self.splines[self.spline_edit][0]
464 b = self.splines[self.spline_edit][1]
465 c = self.splines[self.spline_edit][2]
466 self.splines[spline_edit][5] = a
467 self.splines[spline_edit][4] = a * 2 + b * -1
468 self.splines[spline_edit][3] = c + a * 4 + b * -4
469
Andrew Runke6842bf92019-01-26 15:38:25 -0800470 self.index_of_edit = -1
471 self.spline_edit = -1
472 else:
473 print("mode == 2")
474 # Get clicked point
475 # Find nearest
476 # Move nearest to clicked
John Park13d3e282019-01-26 20:16:48 -0800477 cur_p = [pxToM(self.x), pxToM(self.y)]
Andrew Runke6842bf92019-01-26 15:38:25 -0800478 print("CUR_P: " + str(self.x) + " " + str(self.y))
479 # Get the distance between each for x and y
480 # Save the index of the point closest
John Park13d3e282019-01-26 20:16:48 -0800481 nearest = 1 # Max distance away a the selected point can be in meters
Andrew Runke6842bf92019-01-26 15:38:25 -0800482 index_of_closest = 0
483 for index_splines, points in enumerate(self.splines):
484 for index_points, val in enumerate(points):
485 # pythagorean
486 distance = np.sqrt((cur_p[0] - val[0])**2 +
487 (cur_p[1] - val[1])**2)
488 if distance < nearest:
489 nearest = distance
490 index_of_closest = index_points
491 print("Nearest: " + str(nearest))
492 print("Index: " + str(index_of_closest))
493 self.index_of_edit = index_of_closest
494 self.spline_edit = index_splines
495 self.held_x = self.x
496 elif self.mode == Mode.kConstraint:
497 print("RAN")
498 self.set_index_to_nearest_spline_point()
499 print("FINISHED")
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800500
501 def do_button_press(self, event):
502 print("button press activated")
John Park13d3e282019-01-26 20:16:48 -0800503 # Be consistent with the scaling in the drawing_area
Andrew Runkea9c8de52019-01-26 19:54:29 -0800504 self.x = event.x * 2
505 self.y = event.y * 2
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800506 self.button_press_action()
507
508
Andrew Runke6842bf92019-01-26 15:38:25 -0800509class GridWindow(Gtk.Window):
510 def method_connect(self, event, cb):
511 def handler(obj, *args):
512 cb(*args)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800513
Andrew Runke6842bf92019-01-26 15:38:25 -0800514 print("Method_connect ran")
515 self.connect(event, handler)
516
517 def button_press(self, event):
518 print("button press activated")
519 o_x = event.x
520 o_y = event.y
521 x = event.x - self.drawing_area.window_shape[0] / 2
522 y = self.drawing_area.window_shape[1] / 2 - event.y
523 scale = self.drawing_area.get_current_scale()
524 event.x = x / scale + self.drawing_area.center[0]
525 event.y = y / scale + self.drawing_area.center[1]
526 self.drawing_area.do_button_press(event)
527 event.x = o_x
528 event.y = o_y
529
530 def key_press(self, event):
531 print("key press activated")
532 self.drawing_area.do_key_press(event)
533 self.queue_draw()
534
535 def configure(self, event):
536 print("configure activated")
537 self.drawing_area.window_shape = (event.width, event.height)
538
539 def on_submit_click(self, widget):
540 self.drawing_area.inConstraint = int(self.constraint_box.get_text())
541 self.drawing_area.inValue = int(self.value_box.get_text())
542
543 def __init__(self):
544 Gtk.Window.__init__(self)
545
546 self.set_default_size(1366, 738)
547
548 flowBox = Gtk.FlowBox()
549 flowBox.set_valign(Gtk.Align.START)
550 flowBox.set_selection_mode(Gtk.SelectionMode.NONE)
551
552 flowBox.set_valign(Gtk.Align.START)
553
554 self.add(flowBox)
555
556 container = Gtk.Fixed()
557 flowBox.add(container)
558
559 self.eventBox = Gtk.EventBox()
560 container.add(self.eventBox)
561
562 self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
563 | Gdk.EventMask.BUTTON_RELEASE_MASK
564 | Gdk.EventMask.POINTER_MOTION_MASK
565 | Gdk.EventMask.SCROLL_MASK
566 | Gdk.EventMask.KEY_PRESS_MASK)
567
568 self.drawing_area = GTK_Widget()
569 self.eventBox.add(self.drawing_area)
570
571 self.method_connect("key-release-event", self.key_press)
572 self.method_connect("button-release-event", self.button_press)
573 self.method_connect("configure-event", self.configure)
574
575 # Constraint Boxes
576
577 self.start_box = Gtk.Entry()
578 self.start_box.set_size_request(100, 20)
579
580 self.constraint_box = Gtk.Entry()
581 self.constraint_box.set_size_request(100, 20)
582
583 self.constraint_box.set_text("Constraint")
584 self.constraint_box.set_editable(True)
585
586 container.put(self.constraint_box, 700, 0)
587
588 self.value_box = Gtk.Entry()
589 self.value_box.set_size_request(100, 20)
590
591 self.value_box.set_text("Value")
592 self.value_box.set_editable(True)
593
594 container.put(self.value_box, 700, 40)
595
596 self.submit_button = Gtk.Button("Submit")
597 self.submit_button.connect('clicked', self.on_submit_click)
598
599 container.put(self.submit_button, 880, 0)
600
601 self.show_all()
602
603
604window = GridWindow()
John Park13d3e282019-01-26 20:16:48 -0800605basic_window.RunApp()