John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | import gi |
| 3 | from path_edit import * |
| 4 | import numpy as np |
| 5 | gi.require_version('Gtk', '3.0') |
| 6 | from gi.repository import Gdk, Gtk, GLib |
| 7 | import cairo |
vlad | 56329f4 | 2020-06-12 21:41:08 -0700 | [diff] [blame] | 8 | import basic_window |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 9 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 10 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 11 | class GridWindow(Gtk.Window): |
| 12 | def method_connect(self, event, cb): |
| 13 | def handler(obj, *args): |
| 14 | cb(*args) |
| 15 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 16 | self.connect(event, handler) |
| 17 | |
| 18 | def mouse_move(self, event): |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 19 | # Changes event.x and event.y to be relative to the center. |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 20 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 21 | y = self.drawing_area.window_shape[1] / 2 - event.y |
| 22 | scale = self.drawing_area.get_current_scale() |
| 23 | event.x = x / scale + self.drawing_area.center[0] |
| 24 | event.y = y / scale + self.drawing_area.center[1] |
| 25 | self.drawing_area.mouse_move(event) |
| 26 | self.queue_draw() |
| 27 | |
| 28 | def button_press(self, event): |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 29 | original_x = event.x |
| 30 | original_y = event.y |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 31 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 32 | y = self.drawing_area.window_shape[1] / 2 - event.y |
| 33 | scale = 2 * self.drawing_area.get_current_scale() |
| 34 | event.x = x / scale + self.drawing_area.center[0] |
| 35 | event.y = y / scale + self.drawing_area.center[1] |
| 36 | self.drawing_area.do_button_press(event) |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 37 | event.x = original_x |
| 38 | event.y = original_y |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 39 | |
| 40 | def key_press(self, event): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 41 | self.drawing_area.do_key_press(event, self.file_name_box.get_text()) |
| 42 | self.queue_draw() |
| 43 | |
| 44 | def configure(self, event): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 45 | self.drawing_area.window_shape = (event.width, event.height) |
| 46 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 47 | def output_json_clicked(self, button): |
| 48 | print("OUTPUT JSON CLICKED") |
| 49 | self.drawing_area.export_json(self.file_name_box.get_text()) |
| 50 | |
| 51 | def input_json_clicked(self, button): |
| 52 | print("INPUT JSON CLICKED") |
| 53 | self.drawing_area.import_json(self.file_name_box.get_text()) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 54 | |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 55 | def long_changed(self, button): |
| 56 | print("LONGITUDAL VELOCITY CHANGED") |
| 57 | |
| 58 | def lat_changed(self, button): |
| 59 | print("LATITUDAL VELOCITY CHANGED") |
| 60 | |
| 61 | def accel_changed(self, button): |
| 62 | print("ACCELERATION CHANGED") |
| 63 | |
| 64 | def vol_changed(self, button): |
| 65 | print("VOLTAGE CHANGED") |
| 66 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 67 | def __init__(self): |
| 68 | Gtk.Window.__init__(self) |
| 69 | |
| 70 | self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE) |
| 71 | |
| 72 | flowBox = Gtk.FlowBox() |
| 73 | flowBox.set_valign(Gtk.Align.START) |
| 74 | flowBox.set_selection_mode(Gtk.SelectionMode.NONE) |
| 75 | |
| 76 | flowBox.set_valign(Gtk.Align.START) |
| 77 | |
| 78 | self.add(flowBox) |
| 79 | |
| 80 | container = Gtk.Fixed() |
| 81 | flowBox.add(container) |
| 82 | |
| 83 | self.eventBox = Gtk.EventBox() |
| 84 | container.add(self.eventBox) |
| 85 | |
| 86 | self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
| 87 | | Gdk.EventMask.BUTTON_RELEASE_MASK |
| 88 | | Gdk.EventMask.POINTER_MOTION_MASK |
| 89 | | Gdk.EventMask.SCROLL_MASK |
| 90 | | Gdk.EventMask.KEY_PRESS_MASK) |
| 91 | |
| 92 | #add the graph box |
| 93 | self.drawing_area = GTK_Widget() |
| 94 | self.eventBox.add(self.drawing_area) |
| 95 | |
vlad | 56329f4 | 2020-06-12 21:41:08 -0700 | [diff] [blame] | 96 | self.method_connect("delete-event", basic_window.quit_main_loop) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 97 | self.method_connect("key-release-event", self.key_press) |
| 98 | self.method_connect("button-release-event", self.button_press) |
| 99 | self.method_connect("configure-event", self.configure) |
| 100 | self.method_connect("motion_notify_event", self.mouse_move) |
| 101 | |
| 102 | self.file_name_box = Gtk.Entry() |
| 103 | self.file_name_box.set_size_request(200, 40) |
| 104 | |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 105 | self.file_name_box.set_text("output_file_name.json") |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 106 | self.file_name_box.set_editable(True) |
| 107 | |
| 108 | container.put(self.file_name_box, 0, 0) |
| 109 | |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 110 | self.long_input = Gtk.SpinButton() |
| 111 | self.long_input.set_size_request(100, 20) |
| 112 | self.long_input.set_numeric(True) |
| 113 | self.long_input.set_range(-100, 100) |
| 114 | self.long_input.connect("value-changed", self.long_changed) |
| 115 | self.long_label = Gtk.Label() |
| 116 | self.long_label.set_text("Longitudal Velocity Restriction") |
| 117 | |
| 118 | self.lat_input = Gtk.SpinButton() |
| 119 | self.lat_input.set_size_request(100, 20) |
| 120 | self.lat_input.set_numeric(True) |
| 121 | self.lat_input.set_range(-100, 100) |
| 122 | self.lat_input.connect("value-changed", self.lat_changed) |
| 123 | self.lat_label = Gtk.Label() |
| 124 | self.lat_label.set_text("Latitudal Velocity Restriction") |
| 125 | |
| 126 | self.accel_input = Gtk.SpinButton() |
| 127 | self.accel_input.set_size_request(100, 20) |
| 128 | self.accel_input.set_numeric(True) |
| 129 | self.accel_input.set_range(-100, 100) |
| 130 | self.accel_input.connect("value-changed", self.accel_changed) |
| 131 | |
| 132 | self.accel_label = Gtk.Label() |
| 133 | self.accel_label.set_text("Acceleration Restriction") |
| 134 | |
| 135 | self.vol_input = Gtk.SpinButton() |
| 136 | self.vol_input.set_size_request(100, 20) |
| 137 | self.vol_input.set_numeric(True) |
| 138 | self.vol_input.set_range(-12, 12) |
| 139 | self.vol_input.connect("value-changed", self.vol_changed) |
| 140 | |
| 141 | self.vol_label = Gtk.Label() |
| 142 | self.vol_label.set_text("Voltage Restriction") |
| 143 | |
| 144 | container.put(self.long_input, 0, 60) |
| 145 | container.put(self.lat_input, 0, 110) |
| 146 | container.put(self.accel_input, 0, 160) |
| 147 | container.put(self.vol_input, 0, 210) |
| 148 | container.put(self.long_label, 0, 40) |
| 149 | container.put(self.lat_label, 0, 90) |
| 150 | container.put(self.accel_label, 0, 140) |
| 151 | container.put(self.vol_label, 0, 190) |
| 152 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 153 | self.output_json = Gtk.Button.new_with_label("Output") |
| 154 | self.output_json.set_size_request(100, 40) |
| 155 | self.output_json.connect("clicked", self.output_json_clicked) |
| 156 | |
| 157 | self.input_json = Gtk.Button.new_with_label("Import") |
| 158 | self.input_json.set_size_request(100, 40) |
| 159 | self.input_json.connect("clicked", self.input_json_clicked) |
| 160 | |
| 161 | container.put(self.output_json, 210, 0) |
| 162 | container.put(self.input_json, 320, 0) |
| 163 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 164 | self.show_all() |
| 165 | |
| 166 | |
| 167 | window = GridWindow() |
| 168 | RunApp() |