Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 2 | from __future__ import print_function |
| 3 | import os |
Andrew Runke | 0f945fd | 2019-01-27 21:10:37 -0800 | [diff] [blame] | 4 | import sys |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 5 | import copy |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 6 | from color import Color, palette |
| 7 | import random |
| 8 | import gi |
| 9 | import numpy as np |
| 10 | import scipy.spatial.distance |
| 11 | gi.require_version('Gtk', '3.0') |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 12 | gi.require_version('Gdk', '3.0') |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 13 | from gi.repository import Gdk, Gtk, GLib |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 14 | import cairo |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 15 | from libspline import Spline, DistanceSpline, Trajectory |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 16 | import enum |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 17 | import json |
| 18 | from basic_window import * |
| 19 | from constants import * |
| 20 | from drawing_constants import * |
| 21 | from points import Points |
| 22 | from graph import Graph |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 23 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 24 | |
| 25 | class Mode(enum.Enum): |
| 26 | kViewing = 0 |
| 27 | kPlacing = 1 |
| 28 | kEditing = 2 |
| 29 | kExporting = 3 |
| 30 | kImporting = 4 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 31 | |
| 32 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 33 | class GTK_Widget(BaseWindow): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 34 | """Create a GTK+ widget on which we will draw using Cairo""" |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 35 | def __init__(self): |
| 36 | super(GTK_Widget, self).__init__() |
| 37 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 38 | self.points = Points() |
| 39 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 40 | # init field drawing |
| 41 | # add default spline for testing purposes |
| 42 | # init editing / viewing modes and pointer location |
| 43 | self.mode = Mode.kPlacing |
| 44 | self.x = 0 |
| 45 | self.y = 0 |
Andrew Runke | 0f945fd | 2019-01-27 21:10:37 -0800 | [diff] [blame] | 46 | module_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 47 | self.path_to_export = os.path.join(module_path, |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 48 | 'points_for_pathedit.json') |
| 49 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 50 | # update list of control points |
| 51 | self.point_selected = False |
| 52 | # self.adding_spline = False |
| 53 | self.index_of_selected = -1 |
| 54 | self.new_point = [] |
| 55 | |
| 56 | # For the editing mode |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 57 | self.index_of_edit = -1 # Can't be zero beause array starts at 0 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 58 | self.held_x = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 59 | self.spline_edit = -1 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 60 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 61 | self.curves = [] |
| 62 | |
| 63 | self.colors = [] |
| 64 | |
| 65 | for c in palette: |
| 66 | self.colors.append(palette[c]) |
| 67 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 68 | self.reinit_extents() |
| 69 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 70 | self.inStart = None |
| 71 | self.inEnd = None |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 72 | self.inValue = None |
| 73 | self.startSet = False |
| 74 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 75 | """set extents on images""" |
| 76 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 77 | def reinit_extents(self): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 78 | self.extents_x_min = -1.0 * SCREEN_SIZE |
| 79 | self.extents_x_max = SCREEN_SIZE |
| 80 | self.extents_y_min = -1.0 * SCREEN_SIZE |
| 81 | self.extents_y_max = SCREEN_SIZE |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 82 | |
| 83 | # this needs to be rewritten with numpy, i dont think this ought to have |
| 84 | # SciPy as a dependecy |
| 85 | def get_index_of_nearest_point(self): |
| 86 | cur_p = [[self.x, self.y]] |
| 87 | distances = scipy.spatial.distance.cdist(cur_p, self.all_controls) |
| 88 | |
| 89 | return np.argmin(distances) |
| 90 | |
| 91 | # return the closest point to the loc of the click event |
| 92 | def get_nearest_point(self): |
| 93 | return self.all_controls[self.get_index_of_nearest_point()] |
| 94 | |
John Park | a30a778 | 2019-02-01 18:47:26 -0800 | [diff] [blame] | 95 | def draw_field_elements(self, cr): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 96 | draw_HAB(cr) |
| 97 | draw_rockets(cr) |
| 98 | draw_cargo_ship(cr) |
John Park | a30a778 | 2019-02-01 18:47:26 -0800 | [diff] [blame] | 99 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 100 | def draw_robot_at_point(self, cr, i, p, spline): |
| 101 | p1 = [mToPx(spline.Point(i)[0]), mToPx(spline.Point(i)[1])] |
| 102 | p2 = [mToPx(spline.Point(i + p)[0]), mToPx(spline.Point(i + p)[1])] |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 103 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 104 | #Calculate Robot |
| 105 | distance = np.sqrt((p2[1] - p1[1])**2 + (p2[0] - p1[0])**2) |
| 106 | x_difference_o = p2[0] - p1[0] |
| 107 | y_difference_o = p2[1] - p1[1] |
| 108 | x_difference = x_difference_o * mToPx(LENGTH_OF_ROBOT / 2) / distance |
| 109 | y_difference = y_difference_o * mToPx(LENGTH_OF_ROBOT / 2) / distance |
| 110 | |
| 111 | front_middle = [] |
| 112 | front_middle.append(p1[0] + x_difference) |
| 113 | front_middle.append(p1[1] + y_difference) |
| 114 | |
| 115 | back_middle = [] |
| 116 | back_middle.append(p1[0] - x_difference) |
| 117 | back_middle.append(p1[1] - y_difference) |
| 118 | |
| 119 | slope = [-(1 / x_difference_o) / (1 / y_difference_o)] |
| 120 | angle = np.arctan(slope) |
| 121 | |
| 122 | x_difference = np.sin(angle[0]) * mToPx(WIDTH_OF_ROBOT / 2) |
| 123 | y_difference = np.cos(angle[0]) * mToPx(WIDTH_OF_ROBOT / 2) |
| 124 | |
| 125 | front_1 = [] |
| 126 | front_1.append(front_middle[0] - x_difference) |
| 127 | front_1.append(front_middle[1] - y_difference) |
| 128 | |
| 129 | front_2 = [] |
| 130 | front_2.append(front_middle[0] + x_difference) |
| 131 | front_2.append(front_middle[1] + y_difference) |
| 132 | |
| 133 | back_1 = [] |
| 134 | back_1.append(back_middle[0] - x_difference) |
| 135 | back_1.append(back_middle[1] - y_difference) |
| 136 | |
| 137 | back_2 = [] |
| 138 | back_2.append(back_middle[0] + x_difference) |
| 139 | back_2.append(back_middle[1] + y_difference) |
| 140 | |
| 141 | x_difference = x_difference_o * mToPx( |
| 142 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance |
| 143 | y_difference = y_difference_o * mToPx( |
| 144 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance |
| 145 | |
| 146 | #Calculate Ball |
| 147 | ball_center = [] |
| 148 | ball_center.append(p1[0] + x_difference) |
| 149 | ball_center.append(p1[1] + y_difference) |
| 150 | |
| 151 | x_difference = x_difference_o * mToPx( |
| 152 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance |
| 153 | y_difference = y_difference_o * mToPx( |
| 154 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance |
| 155 | |
| 156 | #Calculate Panel |
| 157 | panel_center = [] |
| 158 | panel_center.append(p1[0] + x_difference) |
| 159 | panel_center.append(p1[1] + y_difference) |
| 160 | |
| 161 | x_difference = np.sin(angle[0]) * mToPx(HATCH_PANEL_WIDTH / 2) |
| 162 | y_difference = np.cos(angle[0]) * mToPx(HATCH_PANEL_WIDTH / 2) |
| 163 | |
| 164 | panel_1 = [] |
| 165 | panel_1.append(panel_center[0] + x_difference) |
| 166 | panel_1.append(panel_center[1] + y_difference) |
| 167 | |
| 168 | panel_2 = [] |
| 169 | panel_2.append(panel_center[0] - x_difference) |
| 170 | panel_2.append(panel_center[1] - y_difference) |
| 171 | |
| 172 | #Draw Robot |
| 173 | cr.move_to(front_1[0], front_1[1]) |
| 174 | cr.line_to(back_1[0], back_1[1]) |
| 175 | cr.line_to(back_2[0], back_2[1]) |
| 176 | cr.line_to(front_2[0], front_2[1]) |
| 177 | cr.line_to(front_1[0], front_1[1]) |
| 178 | |
| 179 | cr.stroke() |
| 180 | |
| 181 | #Draw Ball |
| 182 | set_color(cr, palette["ORANGE"], 0.5) |
| 183 | cr.move_to(back_middle[0], back_middle[1]) |
| 184 | cr.line_to(ball_center[0], ball_center[1]) |
| 185 | cr.arc(ball_center[0], ball_center[1], mToPx(BALL_RADIUS), 0, |
| 186 | 2 * np.pi) |
| 187 | cr.stroke() |
| 188 | |
| 189 | #Draw Panel |
| 190 | set_color(cr, palette["YELLOW"], 0.5) |
| 191 | cr.move_to(panel_1[0], panel_1[1]) |
| 192 | cr.line_to(panel_2[0], panel_2[1]) |
| 193 | |
| 194 | cr.stroke() |
| 195 | cr.set_source_rgba(0, 0, 0, 1) |
| 196 | |
| 197 | def handle_draw(self, cr): # main |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 198 | # Fill the background color of the window with grey |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 199 | set_color(cr, palette["WHITE"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 200 | cr.paint() |
| 201 | |
| 202 | # Draw a extents rectangle |
| 203 | set_color(cr, palette["WHITE"]) |
| 204 | cr.rectangle(self.extents_x_min, self.extents_y_min, |
| 205 | (self.extents_x_max - self.extents_x_min), |
| 206 | self.extents_y_max - self.extents_y_min) |
| 207 | cr.fill() |
| 208 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 209 | #Drawing the switch and scale in the field |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 210 | cr.move_to(0, 50) |
| 211 | cr.show_text('Press "e" to export') |
| 212 | cr.show_text('Press "i" to import') |
| 213 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 214 | set_color(cr, palette["WHITE"]) |
| 215 | cr.rectangle(0, -mToPx(8.2296 / 2.0), SCREEN_SIZE, SCREEN_SIZE) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 216 | cr.fill() |
| 217 | set_color(cr, palette["BLACK"]) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 218 | cr.rectangle(0, -mToPx(8.2296 / 2.0), SCREEN_SIZE, SCREEN_SIZE) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 219 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 220 | cr.stroke() |
John Park | a30a778 | 2019-02-01 18:47:26 -0800 | [diff] [blame] | 221 | self.draw_field_elements(cr) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 222 | y = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 223 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 224 | # update everything |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 225 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 226 | if self.mode == Mode.kPlacing or self.mode == Mode.kViewing: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 227 | set_color(cr, palette["BLACK"]) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 228 | cr.move_to(-SCREEN_SIZE, 170) |
| 229 | plotPoints = self.points.getPoints() |
| 230 | if plotPoints: |
| 231 | for i, point in enumerate(plotPoints): |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 232 | draw_px_x(cr, mToPx(point[0]), mToPx(point[1]), 10) |
| 233 | cr.move_to(mToPx(point[0]), mToPx(point[1]) - 15) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 234 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 235 | set_color(cr, palette["WHITE"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 236 | |
| 237 | elif self.mode == Mode.kEditing: |
| 238 | set_color(cr, palette["BLACK"]) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 239 | cr.move_to(-SCREEN_SIZE, 170) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 240 | display_text(cr, "EDITING", 1, 1, 1, 1) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 241 | if self.points.getSplines(): |
| 242 | self.draw_splines(cr) |
| 243 | for i, points in enumerate(self.points.getSplines()): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 244 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 245 | p0 = np.array([mToPx(points[0][0]), mToPx(points[0][1])]) |
| 246 | p1 = np.array([mToPx(points[1][0]), mToPx(points[1][1])]) |
| 247 | p2 = np.array([mToPx(points[2][0]), mToPx(points[2][1])]) |
| 248 | p3 = np.array([mToPx(points[3][0]), mToPx(points[3][1])]) |
| 249 | p4 = np.array([mToPx(points[4][0]), mToPx(points[4][1])]) |
| 250 | p5 = np.array([mToPx(points[5][0]), mToPx(points[5][1])]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 251 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 252 | draw_control_points(cr, [p0, p1, p2, p3, p4, p5]) |
| 253 | first_tangent = p0 + 2.0 * (p1 - p0) |
| 254 | second_tangent = p5 + 2.0 * (p4 - p5) |
| 255 | cr.set_source_rgb(0, 0.5, 0) |
| 256 | cr.move_to(p0[0], p0[1]) |
| 257 | cr.set_line_width(1.0) |
| 258 | cr.line_to(first_tangent[0], first_tangent[1]) |
| 259 | cr.move_to(first_tangent[0], first_tangent[1]) |
| 260 | cr.line_to(p2[0], p2[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 261 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 262 | cr.move_to(p5[0], p5[1]) |
| 263 | cr.line_to(second_tangent[0], second_tangent[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 264 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 265 | cr.move_to(second_tangent[0], second_tangent[1]) |
| 266 | cr.line_to(p3[0], p3[1]) |
| 267 | |
| 268 | cr.stroke() |
| 269 | cr.set_line_width(2.0) |
| 270 | self.points.update_lib_spline() |
| 271 | set_color(cr, palette["WHITE"]) |
| 272 | |
| 273 | cr.paint_with_alpha(0.2) |
| 274 | |
| 275 | mygraph = Graph(cr, self.points) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 276 | draw_px_cross(cr, self.x, self.y, 10) |
| 277 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 278 | def draw_splines(self, cr): |
| 279 | holder_spline = [] |
| 280 | for i, points in enumerate(self.points.getSplines()): |
| 281 | array = np.zeros(shape=(6, 2), dtype=float) |
| 282 | for j, point in enumerate(points): |
| 283 | array[j, 0] = point[0] |
| 284 | array[j, 1] = point[1] |
| 285 | spline = Spline(np.ascontiguousarray(np.transpose(array))) |
| 286 | for k in np.linspace(0.01, 1, 100): |
| 287 | cr.move_to(mToPx(spline.Point(k - 0.01)[0]), |
| 288 | mToPx(spline.Point(k - 0.01)[1])) |
| 289 | cr.line_to(mToPx(spline.Point(k)[0]), |
| 290 | mToPx(spline.Point(k)[1])) |
| 291 | cr.stroke() |
| 292 | holding = [ |
| 293 | spline.Point(k - 0.01)[0], |
| 294 | spline.Point(k - 0.01)[1] |
| 295 | ] |
| 296 | holder_spline.append(holding) |
| 297 | if i == 0: |
| 298 | self.draw_robot_at_point(cr, 0.00, 0.01, spline) |
| 299 | self.draw_robot_at_point(cr, 1, 0.01, spline) |
| 300 | self.curves.append(holder_spline) |
| 301 | |
| 302 | def mouse_move(self, event): |
| 303 | old_x = self.x |
| 304 | old_y = self.y |
| 305 | self.x = event.x |
| 306 | self.y = event.y |
| 307 | dif_x = event.x - old_x |
| 308 | dif_y = event.y - old_y |
| 309 | difs = np.array([pxToM(dif_x), pxToM(dif_y)]) |
| 310 | |
| 311 | if self.mode == Mode.kEditing: |
| 312 | self.spline_edit = self.points.updates_for_mouse_move( |
| 313 | self.index_of_edit, self.spline_edit, self.x, self.y, difs) |
| 314 | |
| 315 | def do_key_press(self, event, file_name): |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 316 | keyval = Gdk.keyval_to_lower(event.keyval) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 317 | module_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 318 | self.path_to_export = os.path.join(module_path, |
| 319 | "spline_jsons/" + file_name) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 320 | if keyval == Gdk.KEY_q: |
| 321 | print("Found q key and exiting.") |
| 322 | quit_main_loop() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 323 | file_name_end = file_name[-5:] |
| 324 | if file_name_end != ".json": |
| 325 | print("Error: Filename doesn't end in .json") |
| 326 | else: |
| 327 | if keyval == Gdk.KEY_e: |
| 328 | # Will export to json file |
| 329 | self.mode = Mode.kEditing |
| 330 | print('out to: ', self.path_to_export) |
| 331 | exportList = [l.tolist() for l in self.points.getSplines()] |
| 332 | with open(self.path_to_export, mode='w') as points_file: |
| 333 | json.dump(exportList, points_file) |
| 334 | |
| 335 | if keyval == Gdk.KEY_i: |
| 336 | # import from json file |
| 337 | self.mode = Mode.kEditing |
| 338 | self.points.resetPoints() |
| 339 | self.points.resetSplines() |
| 340 | with open(self.path_to_export) as points_file: |
| 341 | self.points.setUpSplines(json.load(points_file)) |
| 342 | |
| 343 | self.points.update_lib_spline() |
| 344 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 345 | if keyval == Gdk.KEY_p: |
| 346 | self.mode = Mode.kPlacing |
| 347 | # F0 = A1 |
| 348 | # B1 = 2F0 - E0 |
| 349 | # C1= d0 + 4F0 - 4E0 |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 350 | spline_index = len(self.points.getSplines()) - 1 |
| 351 | self.points.resetPoints() |
| 352 | self.points.extrapolate( |
| 353 | self.points.getSplines()[len(self.points.getSplines()) - 1][5], |
| 354 | self.points.getSplines()[len(self.points.getSplines()) - 1][4], |
| 355 | self.points.getSplines()[len(self.points.getSplines()) - 1][3]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 356 | |
| 357 | def button_press_action(self): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 358 | if self.mode == Mode.kPlacing: |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 359 | if self.points.add_point(self.x, self.y): |
| 360 | self.mode = Mode.kEditing |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 361 | elif self.mode == Mode.kEditing: |
| 362 | # Now after index_of_edit is not -1, the point is selected, so |
| 363 | # user can click for new point |
| 364 | if self.index_of_edit > -1 and self.held_x != self.x: |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 365 | self.points.setSplines(self.spline_edit, self.index_of_edit, |
| 366 | pxToM(self.x), pxToM(self.y)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 367 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 368 | self.spline_edit = self.points.splineExtrapolate( |
| 369 | self.spline_edit) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 370 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 371 | self.index_of_edit = -1 |
| 372 | self.spline_edit = -1 |
| 373 | else: |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 374 | # Get clicked point |
| 375 | # Find nearest |
| 376 | # Move nearest to clicked |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 377 | cur_p = [pxToM(self.x), pxToM(self.y)] |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 378 | # Get the distance between each for x and y |
| 379 | # Save the index of the point closest |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 380 | nearest = 1 # Max distance away a the selected point can be in meters |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 381 | index_of_closest = 0 |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 382 | for index_splines, points in enumerate(self.points.getSplines()): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 383 | for index_points, val in enumerate(points): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 384 | distance = np.sqrt((cur_p[0] - val[0])**2 + |
| 385 | (cur_p[1] - val[1])**2) |
| 386 | if distance < nearest: |
| 387 | nearest = distance |
| 388 | index_of_closest = index_points |
| 389 | print("Nearest: " + str(nearest)) |
| 390 | print("Index: " + str(index_of_closest)) |
| 391 | self.index_of_edit = index_of_closest |
| 392 | self.spline_edit = index_splines |
| 393 | self.held_x = self.x |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 394 | |
| 395 | def do_button_press(self, event): |
| 396 | print("button press activated") |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 397 | # Be consistent with the scaling in the drawing_area |
Andrew Runke | a9c8de5 | 2019-01-26 19:54:29 -0800 | [diff] [blame] | 398 | self.x = event.x * 2 |
| 399 | self.y = event.y * 2 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 400 | self.button_press_action() |
| 401 | |
| 402 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 403 | class GridWindow(Gtk.Window): |
| 404 | def method_connect(self, event, cb): |
| 405 | def handler(obj, *args): |
| 406 | cb(*args) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 407 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 408 | print("Method_connect ran") |
| 409 | self.connect(event, handler) |
| 410 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 411 | def mouse_move(self, event): |
| 412 | #Changes event.x and event.y to be relative to the center. |
| 413 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 414 | y = self.drawing_area.window_shape[1] / 2 - event.y |
| 415 | scale = self.drawing_area.get_current_scale() |
| 416 | event.x = x / scale + self.drawing_area.center[0] |
| 417 | event.y = y / scale + self.drawing_area.center[1] |
| 418 | self.drawing_area.mouse_move(event) |
| 419 | self.queue_draw() |
| 420 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 421 | def button_press(self, event): |
| 422 | print("button press activated") |
| 423 | o_x = event.x |
| 424 | o_y = event.y |
| 425 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 426 | y = self.drawing_area.window_shape[1] / 2 - event.y |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 427 | scale = 2 * self.drawing_area.get_current_scale() |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 428 | event.x = x / scale + self.drawing_area.center[0] |
| 429 | event.y = y / scale + self.drawing_area.center[1] |
| 430 | self.drawing_area.do_button_press(event) |
| 431 | event.x = o_x |
| 432 | event.y = o_y |
| 433 | |
| 434 | def key_press(self, event): |
| 435 | print("key press activated") |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 436 | self.drawing_area.do_key_press(event, self.file_name_box.get_text()) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 437 | self.queue_draw() |
| 438 | |
| 439 | def configure(self, event): |
| 440 | print("configure activated") |
| 441 | self.drawing_area.window_shape = (event.width, event.height) |
| 442 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 443 | def __init__(self): |
| 444 | Gtk.Window.__init__(self) |
| 445 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 446 | self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 447 | |
| 448 | flowBox = Gtk.FlowBox() |
| 449 | flowBox.set_valign(Gtk.Align.START) |
| 450 | flowBox.set_selection_mode(Gtk.SelectionMode.NONE) |
| 451 | |
| 452 | flowBox.set_valign(Gtk.Align.START) |
| 453 | |
| 454 | self.add(flowBox) |
| 455 | |
| 456 | container = Gtk.Fixed() |
| 457 | flowBox.add(container) |
| 458 | |
| 459 | self.eventBox = Gtk.EventBox() |
| 460 | container.add(self.eventBox) |
| 461 | |
| 462 | self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
| 463 | | Gdk.EventMask.BUTTON_RELEASE_MASK |
| 464 | | Gdk.EventMask.POINTER_MOTION_MASK |
| 465 | | Gdk.EventMask.SCROLL_MASK |
| 466 | | Gdk.EventMask.KEY_PRESS_MASK) |
| 467 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 468 | #add the graph box |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 469 | self.drawing_area = GTK_Widget() |
| 470 | self.eventBox.add(self.drawing_area) |
| 471 | |
| 472 | self.method_connect("key-release-event", self.key_press) |
| 473 | self.method_connect("button-release-event", self.button_press) |
| 474 | self.method_connect("configure-event", self.configure) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 475 | self.method_connect("motion_notify_event", self.mouse_move) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 476 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 477 | self.file_name_box = Gtk.Entry() |
| 478 | self.file_name_box.set_size_request(200, 40) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 479 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 480 | self.file_name_box.set_text("File") |
| 481 | self.file_name_box.set_editable(True) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 482 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 483 | container.put(self.file_name_box, 0, 0) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 484 | |
| 485 | self.show_all() |
| 486 | |
| 487 | |
| 488 | window = GridWindow() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 489 | RunApp() |