blob: 3a0a32389de96583e1205c90c0867c738a204613 [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 = []
147 self.spline = []
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800148 self.reinit_extents()
149
Andrew Runke6842bf92019-01-26 15:38:25 -0800150 self.inStart = None
151 self.inEnd = None
152 self.inConstraint = None
153 self.inValue = None
154 self.startSet = False
155
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800156 #John also wrote this
157 def add_point(self, x, y):
Andrew Runke6842bf92019-01-26 15:38:25 -0800158 if (len(self.selected_points) < 6):
159 self.selected_points.append([x, y])
160 if (len(self.selected_points) == 6):
161 self.mode = Mode.kEditing
162 self.splines.append(np.array(self.selected_points))
163 self.selected_points = []
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800164
Andrew Runke6842bf92019-01-26 15:38:25 -0800165 """set extents on images"""
166
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800167 def reinit_extents(self):
168 self.extents_x_min = -800
169 self.extents_x_max = 800
170 self.extents_y_min = -800
171 self.extents_y_max = 800
172
173 # this needs to be rewritten with numpy, i dont think this ought to have
174 # SciPy as a dependecy
175 def get_index_of_nearest_point(self):
176 cur_p = [[self.x, self.y]]
177 distances = scipy.spatial.distance.cdist(cur_p, self.all_controls)
178
179 return np.argmin(distances)
180
181 # return the closest point to the loc of the click event
182 def get_nearest_point(self):
183 return self.all_controls[self.get_index_of_nearest_point()]
184
Andrew Runke6842bf92019-01-26 15:38:25 -0800185 def set_index_to_nearest_spline_point(self):
186 nearest = 50
187 index_of_closest = 0
188 self.spline_edit = 0
189 cur_p = [self.x, self.y]
190
191 for index_splines, points in enumerate(self.spline):
192 for index_points, i in enumerate(points.curve):
193 # pythagorean
194 distance = np.sqrt((cur_p[0] - i[0])**2 + (cur_p[1] - i[1])**2)
195 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()]
207 self.spline[self.spline_edit].addConstraint(
208 self.inStart, self.inEnd, self.inConstraint, self.inValue)
209 self.startSet = False
210 self.mode = Mode.kEditing
211 self.spline_edit = -1
212 self.index_of_edit = -1
213
214 print("Nearest: " + str(nearest))
215 print("Spline: " + str(self.spline_edit))
216 print("Index: " + str(index_of_closest))
217
218 def findDistance(self):
219 """ findDistance goes through each point on the spline finding distance to that point from the point before.
220 It does this to find the the length of the spline to the point that is currently selected.
221 """
222 distance = 0
223 points = self.curves[self.spline_edit]
224 for index, point in enumerate(points):
225 if index > 0 and index <= self.index_of_edit:
226 distance += np.sqrt((points[index - 1][0] - point[0])**2 +
227 (points[index - 1][1] - point[1])**2)
228 return pxToIn(distance)
229
230 # Handle the expose-event by updating the Window and drawing
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800231 def handle_draw(self, cr):
Andrew Runke6842bf92019-01-26 15:38:25 -0800232 # print(self.new_point)
233 # print("SELF.POINT_SELECTED: " + str(self.point_selected))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800234
235 # begin drawing
236 # Fill the background color of the window with grey
237 set_color(cr, palette["GREY"])
238 cr.paint()
239
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()
Andrew Runke6842bf92019-01-26 15:38:25 -0800259 cr.rectangle((inToPx(140 - 161.825) - 300), inToPx(76.575), inToPx(56),
260 inToPx(-153.15))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800261 cr.set_line_join(cairo.LINE_JOIN_ROUND)
262 cr.stroke()
Andrew Runke6842bf92019-01-26 15:38:25 -0800263 cr.rectangle((inToPx(161.825 - 24) - 300), inToPx(90), inToPx(24),
264 inToPx(-180))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800265 cr.set_line_join(cairo.LINE_JOIN_ROUND)
266 cr.stroke()
267
268 set_color(cr, Color(0.2, 0.2, 0.2))
Andrew Runke6842bf92019-01-26 15:38:25 -0800269 cr.rectangle(
270 inToPx(140 - 161.825) - 300, inToPx(76.575), inToPx(56),
271 inToPx(-153.15))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800272 cr.fill()
Andrew Runke6842bf92019-01-26 15:38:25 -0800273 cr.rectangle(
274 inToPx(161.825 - 24) - 300, inToPx(90), inToPx(24), inToPx(-180))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800275 cr.fill()
276
Andrew Runke6842bf92019-01-26 15:38:25 -0800277 y = 0
278 for x, i in enumerate(self.spline):
279 for j in i.constraints:
280 cr.move_to(-650, -y * 10 + 320)
281 set_color(cr, palette["BLACK"])
282 display_text(
283 cr, str("Spline " + str(x) + ": " + str(j.toString())),
284 0.5, 0.5, 2, 2)
285 y += 1
286
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800287 # update all the things
288
289 if self.mode == Mode.kViewing:
290 set_color(cr, palette["BLACK"])
291 cr.move_to(-300, 170)
292 cr.show_text("VIEWING")
293 set_color(cr, palette["GREY"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800294
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800295 if len(self.selected_points) > 0:
296 print("SELECTED_POINTS: " + str(len(self.selected_points)))
297 print("ITEMS:")
Andrew Runke6842bf92019-01-26 15:38:25 -0800298 # for item in self.selected_points:
299 # print(str(item))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800300 for i, point in enumerate(self.selected_points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800301 # print("I: " + str(i))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800302 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800303 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800304 display_text(cr, str(i), 0.5, 0.5, 2, 2)
305
Andrew Runke6842bf92019-01-26 15:38:25 -0800306 elif self.mode == Mode.kPlacing:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800307 set_color(cr, palette["BLACK"])
308 cr.move_to(-300, 170)
309 display_text(cr, "ADD", 1, 1, 1, 1)
310 set_color(cr, palette["GREY"])
Andrew Runke6842bf92019-01-26 15:38:25 -0800311
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800312 if len(self.selected_points) > 0:
313 print("SELECTED_POINTS: " + str(len(self.selected_points)))
314 print("ITEMS:")
315 for item in self.selected_points:
316 print(str(item))
317 for i, point in enumerate(self.selected_points):
318 print("I: " + str(i))
319 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800320 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800321 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800322
323 elif self.mode == Mode.kEditing:
324 set_color(cr, palette["BLACK"])
325 cr.move_to(-300, 170)
326 display_text(cr, "EDITING", 1, 1, 1, 1)
Andrew Runke6842bf92019-01-26 15:38:25 -0800327 if len(self.splines) > 0:
328 # print("Splines: " + str(len(self.splines)))
329 # print("ITEMS:")
330 holder_spline = []
331 for i, points in enumerate(self.splines):
332 array = np.zeros(shape=(6, 2), dtype=float)
333 for j, point in enumerate(points):
334 array[j, 0] = point[0]
335 array[j, 1] = point[1]
336 spline = Spline(np.ascontiguousarray(np.transpose(array)))
337 for k in np.linspace(0.01, 1, 100):
338
339 cr.move_to(
340 spline.Point(k - 0.01)[0],
341 spline.Point(k - 0.01)[1])
342 cr.line_to(spline.Point(k)[0], spline.Point(k)[1])
343 cr.stroke()
344 holding = [
345 spline.Point(k - 0.01)[0],
346 spline.Point(k - 0.01)[1]
347 ]
348
349 holder_spline.append(holding)
350 self.curves.append(holder_spline)
351
352 for spline, points in enumerate(self.splines):
353 # for item in points:
354 # print(str(item))
355 for i, point in enumerate(points):
356 # print("I: " + str(i))
357 if spline == self.spline_edit and i == self.index_of_edit:
358 draw_px_x(cr, point[0], point[1], 15,
359 self.colors[spline])
360 elif (spline == 0 and not i == 5) or (not i == 0
361 and not i == 5):
362 draw_px_x(cr, point[0], point[1], 10,
363 self.colors[spline])
364 cr.move_to(point[0], point[1] - 15)
365 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800366
367 elif self.mode == Mode.kExporting:
368 set_color(cr, palette["BLACK"])
369 cr.move_to(-300, 170)
370 display_text(cr, "VIEWING", 1, 1, 1, 1)
371 set_color(cr, palette["GREY"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800372
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800373 if len(self.selected_points) > 0:
374 print("SELECTED_POINTS: " + str(len(self.selected_points)))
375 print("ITEMS:")
Andrew Runke6842bf92019-01-26 15:38:25 -0800376 # for item in self.selected_points:
377 # print(str(item))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800378 for i, point in enumerate(self.selected_points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800379 # print("I: " + str(i))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800380 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800381 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800382 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Andrew Runke6842bf92019-01-26 15:38:25 -0800383
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800384 elif self.mode == Mode.kImporting:
385 set_color(cr, palette["BLACK"])
386 cr.move_to(-300, 170)
387 display_text(cr, "VIEWING", 1, 1, 1, 1)
388 set_color(cr, palette["GREY"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800389
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800390 if len(self.selected_points) > 0:
391 print("SELECTED_POINTS: " + str(len(self.selected_points)))
392 print("ITEMS:")
393 for item in self.selected_points:
394 print(str(item))
395 for i, point in enumerate(self.selected_points):
396 print("I: " + str(i))
397 draw_px_x(cr, point[0], point[1], 10)
Andrew Runke6842bf92019-01-26 15:38:25 -0800398 cr.move_to(point[0], point[1] - 15)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800399 display_text(cr, str(i), 0.5, 0.5, 2, 2)
400
Andrew Runke6842bf92019-01-26 15:38:25 -0800401 elif self.mode == Mode.kConstraint:
402 print("Drawn")
403 set_color(cr, palette["BLACK"])
404 cr.move_to(-300, 170)
405 display_text(cr, "Adding Constraint", 1, 1, 1, 1)
406 if len(self.splines) > 0:
407 # print("Splines: " + str(len(self.splines)))
408 # print("ITEMS:")
409 for s, points in enumerate(self.splines):
410 # for item in points:
411 # print(str(item))
412 for i, point in enumerate(points):
413 # print("I: " + str(i))
414 draw_px_x(cr, point[0], point[1], 10, self.colors[s])
415 cr.move_to(point[0], point[1] - 15)
416 display_text(cr, str(i), 0.5, 0.5, 2, 2)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800417
418 cr.paint_with_alpha(.65)
419
420 draw_px_cross(cr, self.x, self.y, 10)
421
422 def do_key_press(self, event):
423 keyval = Gdk.keyval_to_lower(event.keyval)
Andrew Runke6842bf92019-01-26 15:38:25 -0800424 # print("Gdk.KEY_" + Gdk.keyval_name(keyval))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800425 if keyval == Gdk.KEY_q:
426 print("Found q key and exiting.")
427 quit_main_loop()
428 if keyval == Gdk.KEY_e:
429 self.mode = Mode.kExporting
430 # Will export to csv file
431 with open('points_for_pathedit.csv', mode='w') as points_file:
Andrew Runke6842bf92019-01-26 15:38:25 -0800432 writer = csv.writer(
433 points_file,
434 delimiter=',',
435 quotechar='"',
436 quoting=csv.QUOTE_MINIMAL)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800437 for item in self.selected_points:
438 writer.writerow([str(item[0]), str(item[1])])
Andrew Runke6842bf92019-01-26 15:38:25 -0800439 print("Wrote: " + str(item[0]) + " " + str(item[1]))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800440 if keyval == Gdk.KEY_i:
441 self.mode = Mode.kImporting
442 # import from csv file
443 self.selected_points = []
444 with open('points_for_pathedit.csv') as points_file:
445 reader = csv.reader(points_file, delimiter=',')
446 for row in reader:
447 self.add_point(float(row[0]), float(row[1]))
Andrew Runke6842bf92019-01-26 15:38:25 -0800448 print("Added: " + row[0] + " " + row[1])
449 if keyval == Gdk.KEY_p:
450 self.mode = Mode.kPlacing
451 # F0 = A1
452 # B1 = 2F0 - E0
453 # C1= d0 + 4F0 - 4E0
454 spline_index = len(self.splines) - 1
455 self.selected_points = []
456 f = self.splines[spline_index][5]
457 e = self.splines[spline_index][4]
458 d = self.splines[spline_index][3]
459 self.selected_points.append(f)
460 self.selected_points.append(f * 2 + e * -1)
461 self.selected_points.append(d + f * 4 + e * -4)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800462
Andrew Runke6842bf92019-01-26 15:38:25 -0800463 if keyval == Gdk.KEY_c:
464 self.mode = Mode.kConstraint
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800465
466 def button_press_action(self):
Andrew Runke6842bf92019-01-26 15:38:25 -0800467 if self.mode == Mode.kPlacing:
468 #Check that the point clicked is on the field
469 if (self.x < -150 and self.x > -450 and self.y < 150
470 and self.y > -150):
471 self.add_point(self.x, self.y)
472 elif self.mode == Mode.kEditing:
473 # Now after index_of_edit is not -1, the point is selected, so
474 # user can click for new point
475 if self.index_of_edit > -1 and self.held_x != self.x:
476 print("INDEX OF EDIT: " + str(self.index_of_edit))
477 self.splines[self.spline_edit][self.index_of_edit] = [
478 self.x, self.y
479 ]
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800480
Andrew Runke6842bf92019-01-26 15:38:25 -0800481 if not self.spline_edit == len(self.splines) - 1:
482 spline_edit = self.spline_edit + 1
483 f = self.splines[self.spline_edit][5]
484 e = self.splines[self.spline_edit][4]
485 d = self.splines[self.spline_edit][3]
486 self.splines[spline_edit][0] = f
487 self.splines[spline_edit][1] = f * 2 + e * -1
488 self.splines[spline_edit][2] = d + f * 4 + e * -4
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800489
Andrew Runke6842bf92019-01-26 15:38:25 -0800490 self.spline[spline_edit].point = self.splines[spline_edit]
491 self.spline[spline_edit].math()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800492
Andrew Runke6842bf92019-01-26 15:38:25 -0800493 if not self.spline_edit == 0:
494 spline_edit = self.spline_edit - 1
495 a = self.splines[self.spline_edit][0]
496 b = self.splines[self.spline_edit][1]
497 c = self.splines[self.spline_edit][2]
498 self.splines[spline_edit][5] = a
499 self.splines[spline_edit][4] = a * 2 + b * -1
500 self.splines[spline_edit][3] = c + a * 4 + b * -4
501
502 self.spline[spline_edit].point = self.splines[spline_edit]
503 self.spline[spline_edit].math()
504
505 self.spline[self.spline_edit].edit(self.index_of_edit,
506 [self.x, self.y])
507 self.index_of_edit = -1
508 self.spline_edit = -1
509 else:
510 print("mode == 2")
511 # Get clicked point
512 # Find nearest
513 # Move nearest to clicked
514 cur_p = [self.x, self.y]
515 print("CUR_P: " + str(self.x) + " " + str(self.y))
516 # Get the distance between each for x and y
517 # Save the index of the point closest
518 nearest = 50
519 index_of_closest = 0
520 for index_splines, points in enumerate(self.splines):
521 for index_points, val in enumerate(points):
522 # pythagorean
523 distance = np.sqrt((cur_p[0] - val[0])**2 +
524 (cur_p[1] - val[1])**2)
525 if distance < nearest:
526 nearest = distance
527 index_of_closest = index_points
528 print("Nearest: " + str(nearest))
529 print("Index: " + str(index_of_closest))
530 self.index_of_edit = index_of_closest
531 self.spline_edit = index_splines
532 self.held_x = self.x
533 elif self.mode == Mode.kConstraint:
534 print("RAN")
535 self.set_index_to_nearest_spline_point()
536 print("FINISHED")
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800537
538 def do_button_press(self, event):
539 print("button press activated")
540 self.x = event.x
541 self.y = event.y
542 self.button_press_action()
543
544
Andrew Runke6842bf92019-01-26 15:38:25 -0800545class GridWindow(Gtk.Window):
546 def method_connect(self, event, cb):
547 def handler(obj, *args):
548 cb(*args)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800549
Andrew Runke6842bf92019-01-26 15:38:25 -0800550 print("Method_connect ran")
551 self.connect(event, handler)
552
553 def button_press(self, event):
554 print("button press activated")
555 o_x = event.x
556 o_y = event.y
557 x = event.x - self.drawing_area.window_shape[0] / 2
558 y = self.drawing_area.window_shape[1] / 2 - event.y
559 scale = self.drawing_area.get_current_scale()
560 event.x = x / scale + self.drawing_area.center[0]
561 event.y = y / scale + self.drawing_area.center[1]
562 self.drawing_area.do_button_press(event)
563 event.x = o_x
564 event.y = o_y
565
566 def key_press(self, event):
567 print("key press activated")
568 self.drawing_area.do_key_press(event)
569 self.queue_draw()
570
571 def configure(self, event):
572 print("configure activated")
573 self.drawing_area.window_shape = (event.width, event.height)
574
575 def on_submit_click(self, widget):
576 self.drawing_area.inConstraint = int(self.constraint_box.get_text())
577 self.drawing_area.inValue = int(self.value_box.get_text())
578
579 def __init__(self):
580 Gtk.Window.__init__(self)
581
582 self.set_default_size(1366, 738)
583
584 flowBox = Gtk.FlowBox()
585 flowBox.set_valign(Gtk.Align.START)
586 flowBox.set_selection_mode(Gtk.SelectionMode.NONE)
587
588 flowBox.set_valign(Gtk.Align.START)
589
590 self.add(flowBox)
591
592 container = Gtk.Fixed()
593 flowBox.add(container)
594
595 self.eventBox = Gtk.EventBox()
596 container.add(self.eventBox)
597
598 self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
599 | Gdk.EventMask.BUTTON_RELEASE_MASK
600 | Gdk.EventMask.POINTER_MOTION_MASK
601 | Gdk.EventMask.SCROLL_MASK
602 | Gdk.EventMask.KEY_PRESS_MASK)
603
604 self.drawing_area = GTK_Widget()
605 self.eventBox.add(self.drawing_area)
606
607 self.method_connect("key-release-event", self.key_press)
608 self.method_connect("button-release-event", self.button_press)
609 self.method_connect("configure-event", self.configure)
610
611 # Constraint Boxes
612
613 self.start_box = Gtk.Entry()
614 self.start_box.set_size_request(100, 20)
615
616 self.constraint_box = Gtk.Entry()
617 self.constraint_box.set_size_request(100, 20)
618
619 self.constraint_box.set_text("Constraint")
620 self.constraint_box.set_editable(True)
621
622 container.put(self.constraint_box, 700, 0)
623
624 self.value_box = Gtk.Entry()
625 self.value_box.set_size_request(100, 20)
626
627 self.value_box.set_text("Value")
628 self.value_box.set_editable(True)
629
630 container.put(self.value_box, 700, 40)
631
632 self.submit_button = Gtk.Button("Submit")
633 self.submit_button.connect('clicked', self.on_submit_click)
634
635 container.put(self.submit_button, 880, 0)
636
637 self.show_all()
638
639
640window = GridWindow()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800641basic_window.RunApp()