blob: 52bd8d1fd933eceae93345608322eefa4f98141a [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
20LENGTH_OF_FIELD = 323.65
21PIXELS_ON_SCREEN = 300
22
Andrew Runke6842bf92019-01-26 15:38:25 -080023
Tabitha Jarvis1007a132018-12-12 21:47:54 -080024def pxToIn(p):
Andrew Runke6842bf92019-01-26 15:38:25 -080025 return p * LENGTH_OF_FIELD / PIXELS_ON_SCREEN
26
Tabitha Jarvis1007a132018-12-12 21:47:54 -080027
28def inToPx(i):
Andrew Runke6842bf92019-01-26 15:38:25 -080029 return (i * PIXELS_ON_SCREEN / LENGTH_OF_FIELD)
30
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):
158 self.selected_points.append([x, y])
159 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):
191 for index_points, i in enumerate(points.curve):
192 # pythagorean
193 distance = np.sqrt((cur_p[0] - i[0])**2 + (cur_p[1] - i[1])**2)
194 if distance < nearest:
195 nearest = distance
196 print("DISTANCE: ", distance, " | INDEX: ", index_points)
197 index_of_closest = index_points
198 self.index_of_edit = index_of_closest
199 self.spline_edit = index_splines
200 self.held_x = self.x
201 if self.startSet == False:
202 self.inStart = [self.index_of_edit, self.findDistance()]
203 self.startSet = True
204 else:
205 self.inEnd = [self.index_of_edit, self.findDistance()]
Andrew Runke6842bf92019-01-26 15:38:25 -0800206 self.startSet = False
207 self.mode = Mode.kEditing
208 self.spline_edit = -1
209 self.index_of_edit = -1
210
211 print("Nearest: " + str(nearest))
212 print("Spline: " + str(self.spline_edit))
213 print("Index: " + str(index_of_closest))
214
215 def findDistance(self):
216 """ findDistance goes through each point on the spline finding distance to that point from the point before.
217 It does this to find the the length of the spline to the point that is currently selected.
218 """
219 distance = 0
220 points = self.curves[self.spline_edit]
221 for index, point in enumerate(points):
222 if index > 0 and index <= self.index_of_edit:
223 distance += np.sqrt((points[index - 1][0] - point[0])**2 +
224 (points[index - 1][1] - point[1])**2)
225 return pxToIn(distance)
226
227 # Handle the expose-event by updating the Window and drawing
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800228 def handle_draw(self, cr):
Andrew Runke6842bf92019-01-26 15:38:25 -0800229 # print(self.new_point)
230 # print("SELF.POINT_SELECTED: " + str(self.point_selected))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800231
232 # begin drawing
233 # Fill the background color of the window with grey
234 set_color(cr, palette["GREY"])
235 cr.paint()
Andrew Runkea9c8de52019-01-26 19:54:29 -0800236 #Scale the field to fit within drawing area
Andrew Runke6696bb82019-02-02 14:33:59 -0800237 cr.scale(0.5, 0.5)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800238
239 # Draw a extents rectangle
240 set_color(cr, palette["WHITE"])
241 cr.rectangle(self.extents_x_min, self.extents_y_min,
242 (self.extents_x_max - self.extents_x_min),
243 self.extents_y_max - self.extents_y_min)
244 cr.fill()
245
246 #Drawing the switch and scale in the field
247 cr.move_to(0, 50)
248 cr.show_text('Press "e" to export')
249 cr.show_text('Press "i" to import')
250
251 set_color(cr, Color(0.3, 0.3, 0.3))
Andrew Runke6842bf92019-01-26 15:38:25 -0800252 cr.rectangle(-450, -150, 300, 300)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800253 cr.fill()
254 set_color(cr, palette["BLACK"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800255 cr.rectangle(-450, -150, 300, 300)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800256 cr.set_line_join(cairo.LINE_JOIN_ROUND)
257 cr.stroke()
Andrew Runke6842bf92019-01-26 15:38:25 -0800258 cr.rectangle((inToPx(140 - 161.825) - 300), inToPx(76.575), inToPx(56),
259 inToPx(-153.15))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800260 cr.set_line_join(cairo.LINE_JOIN_ROUND)
261 cr.stroke()
Andrew Runke6842bf92019-01-26 15:38:25 -0800262 cr.rectangle((inToPx(161.825 - 24) - 300), inToPx(90), inToPx(24),
263 inToPx(-180))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800264 cr.set_line_join(cairo.LINE_JOIN_ROUND)
265 cr.stroke()
266
267 set_color(cr, Color(0.2, 0.2, 0.2))
Andrew Runke6842bf92019-01-26 15:38:25 -0800268 cr.rectangle(
269 inToPx(140 - 161.825) - 300, inToPx(76.575), inToPx(56),
270 inToPx(-153.15))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800271 cr.fill()
Andrew Runke6842bf92019-01-26 15:38:25 -0800272 cr.rectangle(
273 inToPx(161.825 - 24) - 300, inToPx(90), inToPx(24), inToPx(-180))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800274 cr.fill()
275
Andrew Runke6842bf92019-01-26 15:38:25 -0800276 y = 0
Andrew Runke6842bf92019-01-26 15:38:25 -0800277
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800278 # update all the things
279
280 if self.mode == Mode.kViewing:
281 set_color(cr, palette["BLACK"])
282 cr.move_to(-300, 170)
283 cr.show_text("VIEWING")
284 set_color(cr, palette["GREY"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800285
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800286 if len(self.selected_points) > 0:
287 print("SELECTED_POINTS: " + str(len(self.selected_points)))
288 print("ITEMS:")
Andrew Runke6842bf92019-01-26 15:38:25 -0800289 # for item in self.selected_points:
290 # print(str(item))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800291 for i, point in enumerate(self.selected_points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800292 # print("I: " + str(i))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800293 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800294 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800295 display_text(cr, str(i), 0.5, 0.5, 2, 2)
296
Andrew Runke6842bf92019-01-26 15:38:25 -0800297 elif self.mode == Mode.kPlacing:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800298 set_color(cr, palette["BLACK"])
299 cr.move_to(-300, 170)
300 display_text(cr, "ADD", 1, 1, 1, 1)
301 set_color(cr, palette["GREY"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800302
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800303 if len(self.selected_points) > 0:
304 print("SELECTED_POINTS: " + str(len(self.selected_points)))
305 print("ITEMS:")
306 for item in self.selected_points:
307 print(str(item))
308 for i, point in enumerate(self.selected_points):
309 print("I: " + str(i))
310 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800311 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800312 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800313
314 elif self.mode == Mode.kEditing:
315 set_color(cr, palette["BLACK"])
316 cr.move_to(-300, 170)
317 display_text(cr, "EDITING", 1, 1, 1, 1)
Andrew Runke6842bf92019-01-26 15:38:25 -0800318 if len(self.splines) > 0:
319 # print("Splines: " + str(len(self.splines)))
320 # print("ITEMS:")
321 holder_spline = []
322 for i, points in enumerate(self.splines):
323 array = np.zeros(shape=(6, 2), dtype=float)
324 for j, point in enumerate(points):
325 array[j, 0] = point[0]
326 array[j, 1] = point[1]
327 spline = Spline(np.ascontiguousarray(np.transpose(array)))
328 for k in np.linspace(0.01, 1, 100):
329
330 cr.move_to(
331 spline.Point(k - 0.01)[0],
332 spline.Point(k - 0.01)[1])
333 cr.line_to(spline.Point(k)[0], spline.Point(k)[1])
334 cr.stroke()
335 holding = [
336 spline.Point(k - 0.01)[0],
337 spline.Point(k - 0.01)[1]
338 ]
339
340 holder_spline.append(holding)
341 self.curves.append(holder_spline)
342
343 for spline, points in enumerate(self.splines):
344 # for item in points:
345 # print(str(item))
346 for i, point in enumerate(points):
347 # print("I: " + str(i))
348 if spline == self.spline_edit and i == self.index_of_edit:
349 draw_px_x(cr, point[0], point[1], 15,
350 self.colors[spline])
351 elif (spline == 0 and not i == 5) or (not i == 0
352 and not i == 5):
353 draw_px_x(cr, point[0], point[1], 10,
354 self.colors[spline])
355 cr.move_to(point[0], point[1] - 15)
356 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800357
358 elif self.mode == Mode.kExporting:
359 set_color(cr, palette["BLACK"])
360 cr.move_to(-300, 170)
361 display_text(cr, "VIEWING", 1, 1, 1, 1)
362 set_color(cr, palette["GREY"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800363
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800364 if len(self.selected_points) > 0:
365 print("SELECTED_POINTS: " + str(len(self.selected_points)))
366 print("ITEMS:")
Andrew Runke6842bf92019-01-26 15:38:25 -0800367 # for item in self.selected_points:
368 # print(str(item))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800369 for i, point in enumerate(self.selected_points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800370 # print("I: " + str(i))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800371 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800372 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800373 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Andrew Runke6842bf92019-01-26 15:38:25 -0800374
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800375 elif self.mode == Mode.kImporting:
376 set_color(cr, palette["BLACK"])
377 cr.move_to(-300, 170)
378 display_text(cr, "VIEWING", 1, 1, 1, 1)
379 set_color(cr, palette["GREY"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800380
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800381 if len(self.selected_points) > 0:
382 print("SELECTED_POINTS: " + str(len(self.selected_points)))
383 print("ITEMS:")
384 for item in self.selected_points:
385 print(str(item))
386 for i, point in enumerate(self.selected_points):
387 print("I: " + str(i))
388 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800389 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800390 display_text(cr, str(i), 0.5, 0.5, 2, 2)
391
Andrew Runke6842bf92019-01-26 15:38:25 -0800392 elif self.mode == Mode.kConstraint:
393 print("Drawn")
394 set_color(cr, palette["BLACK"])
395 cr.move_to(-300, 170)
396 display_text(cr, "Adding Constraint", 1, 1, 1, 1)
397 if len(self.splines) > 0:
398 # print("Splines: " + str(len(self.splines)))
399 # print("ITEMS:")
400 for s, points in enumerate(self.splines):
401 # for item in points:
402 # print(str(item))
403 for i, point in enumerate(points):
404 # print("I: " + str(i))
405 draw_px_x(cr, point[0], point[1], 10, self.colors[s])
406 cr.move_to(point[0], point[1] - 15)
407 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800408
409 cr.paint_with_alpha(.65)
410
411 draw_px_cross(cr, self.x, self.y, 10)
412
413 def do_key_press(self, event):
414 keyval = Gdk.keyval_to_lower(event.keyval)
Andrew Runke6842bf92019-01-26 15:38:25 -0800415 # print("Gdk.KEY_" + Gdk.keyval_name(keyval))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800416 if keyval == Gdk.KEY_q:
417 print("Found q key and exiting.")
418 quit_main_loop()
419 if keyval == Gdk.KEY_e:
420 self.mode = Mode.kExporting
421 # Will export to csv file
422 with open('points_for_pathedit.csv', mode='w') as points_file:
Andrew Runke6842bf92019-01-26 15:38:25 -0800423 writer = csv.writer(
424 points_file,
425 delimiter=',',
426 quotechar='"',
427 quoting=csv.QUOTE_MINIMAL)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800428 for item in self.selected_points:
429 writer.writerow([str(item[0]), str(item[1])])
Andrew Runke6842bf92019-01-26 15:38:25 -0800430 print("Wrote: " + str(item[0]) + " " + str(item[1]))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800431 if keyval == Gdk.KEY_i:
432 self.mode = Mode.kImporting
433 # import from csv file
434 self.selected_points = []
435 with open('points_for_pathedit.csv') as points_file:
436 reader = csv.reader(points_file, delimiter=',')
437 for row in reader:
438 self.add_point(float(row[0]), float(row[1]))
Andrew Runke6842bf92019-01-26 15:38:25 -0800439 print("Added: " + row[0] + " " + row[1])
440 if keyval == Gdk.KEY_p:
441 self.mode = Mode.kPlacing
442 # F0 = A1
443 # B1 = 2F0 - E0
444 # C1= d0 + 4F0 - 4E0
445 spline_index = len(self.splines) - 1
446 self.selected_points = []
447 f = self.splines[spline_index][5]
448 e = self.splines[spline_index][4]
449 d = self.splines[spline_index][3]
450 self.selected_points.append(f)
451 self.selected_points.append(f * 2 + e * -1)
452 self.selected_points.append(d + f * 4 + e * -4)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800453
Andrew Runke6842bf92019-01-26 15:38:25 -0800454 if keyval == Gdk.KEY_c:
455 self.mode = Mode.kConstraint
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800456
457 def button_press_action(self):
Andrew Runke6842bf92019-01-26 15:38:25 -0800458 if self.mode == Mode.kPlacing:
459 #Check that the point clicked is on the field
460 if (self.x < -150 and self.x > -450 and self.y < 150
461 and self.y > -150):
462 self.add_point(self.x, self.y)
463 elif self.mode == Mode.kEditing:
464 # Now after index_of_edit is not -1, the point is selected, so
465 # user can click for new point
466 if self.index_of_edit > -1 and self.held_x != self.x:
467 print("INDEX OF EDIT: " + str(self.index_of_edit))
468 self.splines[self.spline_edit][self.index_of_edit] = [
469 self.x, self.y
470 ]
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800471
Andrew Runke6842bf92019-01-26 15:38:25 -0800472 if not self.spline_edit == len(self.splines) - 1:
473 spline_edit = self.spline_edit + 1
474 f = self.splines[self.spline_edit][5]
475 e = self.splines[self.spline_edit][4]
476 d = self.splines[self.spline_edit][3]
477 self.splines[spline_edit][0] = f
478 self.splines[spline_edit][1] = f * 2 + e * -1
479 self.splines[spline_edit][2] = d + f * 4 + e * -4
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800480
Andrew Runke6842bf92019-01-26 15:38:25 -0800481 if not self.spline_edit == 0:
482 spline_edit = self.spline_edit - 1
483 a = self.splines[self.spline_edit][0]
484 b = self.splines[self.spline_edit][1]
485 c = self.splines[self.spline_edit][2]
486 self.splines[spline_edit][5] = a
487 self.splines[spline_edit][4] = a * 2 + b * -1
488 self.splines[spline_edit][3] = c + a * 4 + b * -4
489
Andrew Runke6842bf92019-01-26 15:38:25 -0800490 self.index_of_edit = -1
491 self.spline_edit = -1
492 else:
493 print("mode == 2")
494 # Get clicked point
495 # Find nearest
496 # Move nearest to clicked
497 cur_p = [self.x, self.y]
498 print("CUR_P: " + str(self.x) + " " + str(self.y))
499 # Get the distance between each for x and y
500 # Save the index of the point closest
501 nearest = 50
502 index_of_closest = 0
503 for index_splines, points in enumerate(self.splines):
504 for index_points, val in enumerate(points):
505 # pythagorean
506 distance = np.sqrt((cur_p[0] - val[0])**2 +
507 (cur_p[1] - val[1])**2)
508 if distance < nearest:
509 nearest = distance
510 index_of_closest = index_points
511 print("Nearest: " + str(nearest))
512 print("Index: " + str(index_of_closest))
513 self.index_of_edit = index_of_closest
514 self.spline_edit = index_splines
515 self.held_x = self.x
516 elif self.mode == Mode.kConstraint:
517 print("RAN")
518 self.set_index_to_nearest_spline_point()
519 print("FINISHED")
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800520
521 def do_button_press(self, event):
522 print("button press activated")
Andrew Runkea9c8de52019-01-26 19:54:29 -0800523 #Be consistent with the scaling in the drawing_area
524 self.x = event.x * 2
525 self.y = event.y * 2
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800526 self.button_press_action()
527
528
Andrew Runke6842bf92019-01-26 15:38:25 -0800529class GridWindow(Gtk.Window):
530 def method_connect(self, event, cb):
531 def handler(obj, *args):
532 cb(*args)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800533
Andrew Runke6842bf92019-01-26 15:38:25 -0800534 print("Method_connect ran")
535 self.connect(event, handler)
536
537 def button_press(self, event):
538 print("button press activated")
539 o_x = event.x
540 o_y = event.y
541 x = event.x - self.drawing_area.window_shape[0] / 2
542 y = self.drawing_area.window_shape[1] / 2 - event.y
543 scale = self.drawing_area.get_current_scale()
544 event.x = x / scale + self.drawing_area.center[0]
545 event.y = y / scale + self.drawing_area.center[1]
546 self.drawing_area.do_button_press(event)
547 event.x = o_x
548 event.y = o_y
549
550 def key_press(self, event):
551 print("key press activated")
552 self.drawing_area.do_key_press(event)
553 self.queue_draw()
554
555 def configure(self, event):
556 print("configure activated")
557 self.drawing_area.window_shape = (event.width, event.height)
558
559 def on_submit_click(self, widget):
560 self.drawing_area.inConstraint = int(self.constraint_box.get_text())
561 self.drawing_area.inValue = int(self.value_box.get_text())
562
563 def __init__(self):
564 Gtk.Window.__init__(self)
565
566 self.set_default_size(1366, 738)
567
568 flowBox = Gtk.FlowBox()
569 flowBox.set_valign(Gtk.Align.START)
570 flowBox.set_selection_mode(Gtk.SelectionMode.NONE)
571
572 flowBox.set_valign(Gtk.Align.START)
573
574 self.add(flowBox)
575
576 container = Gtk.Fixed()
577 flowBox.add(container)
578
579 self.eventBox = Gtk.EventBox()
580 container.add(self.eventBox)
581
582 self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
583 | Gdk.EventMask.BUTTON_RELEASE_MASK
584 | Gdk.EventMask.POINTER_MOTION_MASK
585 | Gdk.EventMask.SCROLL_MASK
586 | Gdk.EventMask.KEY_PRESS_MASK)
587
588 self.drawing_area = GTK_Widget()
589 self.eventBox.add(self.drawing_area)
590
591 self.method_connect("key-release-event", self.key_press)
592 self.method_connect("button-release-event", self.button_press)
593 self.method_connect("configure-event", self.configure)
594
595 # Constraint Boxes
596
597 self.start_box = Gtk.Entry()
598 self.start_box.set_size_request(100, 20)
599
600 self.constraint_box = Gtk.Entry()
601 self.constraint_box.set_size_request(100, 20)
602
603 self.constraint_box.set_text("Constraint")
604 self.constraint_box.set_editable(True)
605
606 container.put(self.constraint_box, 700, 0)
607
608 self.value_box = Gtk.Entry()
609 self.value_box.set_size_request(100, 20)
610
611 self.value_box.set_text("Value")
612 self.value_box.set_editable(True)
613
614 container.put(self.value_box, 700, 40)
615
616 self.submit_button = Gtk.Button("Submit")
617 self.submit_button.connect('clicked', self.on_submit_click)
618
619 container.put(self.submit_button, 880, 0)
620
621 self.show_all()
622
623
624window = GridWindow()
Andrew Runke6696bb82019-02-02 14:33:59 -0800625basic_window.RunApp()