John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 2 | |
| 3 | # matplotlib overrides fontconfig locations, so has to be imported before gtk |
| 4 | import matplotlib |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 5 | import gi |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 6 | from path_edit import FieldWidget |
| 7 | from basic_window import RunApp |
| 8 | from constants import FIELDS, FIELD, SCREEN_SIZE |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 9 | gi.require_version('Gtk', '3.0') |
| 10 | from gi.repository import Gdk, Gtk, GLib |
| 11 | import cairo |
vlad | 56329f4 | 2020-06-12 21:41:08 -0700 | [diff] [blame] | 12 | import basic_window |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 13 | import os |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 14 | |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 15 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 16 | class GridWindow(Gtk.Window): |
| 17 | def method_connect(self, event, cb): |
| 18 | def handler(obj, *args): |
| 19 | cb(*args) |
| 20 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 21 | self.connect(event, handler) |
| 22 | |
Ryan Yin | 85f861f | 2021-09-16 17:55:11 -0700 | [diff] [blame] | 23 | def clear_clicked(self, button): |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 24 | self.field.clear() |
Ryan Yin | 85f861f | 2021-09-16 17:55:11 -0700 | [diff] [blame] | 25 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 26 | def output_json_clicked(self, button): |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 27 | self.field.export_json(self.file_name_box.get_text()) |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 28 | |
| 29 | def input_json_clicked(self, button): |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 30 | self.field.import_json(self.file_name_box.get_text()) |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 31 | self.long_input.set_value( |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 32 | self.field.active_multispline.getConstraint( |
| 33 | "LONGITUDINAL_ACCELERATION")) |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 34 | self.lat_input.set_value( |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 35 | self.field.active_multispline.getConstraint( |
| 36 | "LATERAL_ACCELERATION")) |
| 37 | self.vol_input.set_value( |
| 38 | self.field.active_multispline.getConstraint("VOLTAGE")) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 39 | |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 40 | def undo_func(self, *args): |
| 41 | self.field.undo() |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 42 | |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 43 | def long_changed(self, button): |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 44 | value = self.long_input.get_value() |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 45 | self.field.active_multispline.setConstraint( |
| 46 | "LONGITUDINAL_ACCELERATION", value) |
| 47 | self.field.graph.schedule_recalculate(self.field.multisplines) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 48 | |
| 49 | def lat_changed(self, button): |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 50 | value = self.lat_input.get_value() |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 51 | self.field.active_multispline.setConstraint("LATERAL_ACCELERATION", |
| 52 | value) |
| 53 | self.field.graph.schedule_recalculate(self.field.multisplines) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 54 | |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 55 | def vel_changed(self, button): |
| 56 | value = self.vel_input.get_value() |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 57 | |
| 58 | def vol_changed(self, button): |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 59 | value = self.vol_input.get_value() |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 60 | self.field.active_multispline.setConstraint("VOLTAGE", value) |
| 61 | self.field.graph.schedule_recalculate(self.field.multisplines) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 62 | |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 63 | def input_combobox_choice(self, combo): |
| 64 | text = combo.get_active_text() |
| 65 | if text is not None: |
| 66 | print("Combo Clicked on: " + text) |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 67 | self.field.set_field(FIELDS[text]) |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 68 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 69 | def __init__(self): |
| 70 | Gtk.Window.__init__(self) |
| 71 | |
| 72 | self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE) |
| 73 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 74 | container = Gtk.Grid() |
| 75 | container.set_vexpand(True) |
| 76 | self.add(container) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 77 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 78 | self.field = FieldWidget() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 79 | |
vlad | 56329f4 | 2020-06-12 21:41:08 -0700 | [diff] [blame] | 80 | self.method_connect("delete-event", basic_window.quit_main_loop) |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 81 | self.method_connect("key-release-event", self.field.do_key_press_event) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 82 | |
| 83 | self.file_name_box = Gtk.Entry() |
| 84 | self.file_name_box.set_size_request(200, 40) |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 85 | self.file_name_box.set_text(FIELD.field_id + ".json") |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 86 | self.file_name_box.set_editable(True) |
| 87 | |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 88 | self.long_input = Gtk.SpinButton() |
| 89 | self.long_input.set_size_request(100, 20) |
| 90 | self.long_input.set_numeric(True) |
Austin Schuh | b9dc3f6 | 2021-03-31 20:06:49 -0700 | [diff] [blame] | 91 | self.long_input.set_range(0, 20) |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 92 | self.long_input.set_digits(3) |
| 93 | self.long_input.set_increments(0.1, 100) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 94 | self.long_input.connect("value-changed", self.long_changed) |
| 95 | self.long_label = Gtk.Label() |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 96 | self.long_label.set_text("Longitudinal Acceleration Restriction") |
| 97 | self.long_input.set_value( |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 98 | self.field.active_multispline.getConstraint( |
| 99 | "LONGITUDINAL_ACCELERATION")) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 100 | |
| 101 | self.lat_input = Gtk.SpinButton() |
| 102 | self.lat_input.set_size_request(100, 20) |
| 103 | self.lat_input.set_numeric(True) |
Austin Schuh | b9dc3f6 | 2021-03-31 20:06:49 -0700 | [diff] [blame] | 104 | self.lat_input.set_range(0, 20) |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 105 | self.lat_input.set_digits(3) |
| 106 | self.lat_input.set_increments(0.1, 100) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 107 | self.lat_input.connect("value-changed", self.lat_changed) |
| 108 | self.lat_label = Gtk.Label() |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 109 | self.lat_label.set_text("Lateral Acceleration Restriction") |
| 110 | self.lat_input.set_value( |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 111 | self.field.active_multispline.getConstraint( |
| 112 | "LATERAL_ACCELERATION")) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 113 | |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 114 | self.vel_input = Gtk.SpinButton() |
| 115 | self.vel_input.set_size_request(100, 20) |
| 116 | self.vel_input.set_numeric(True) |
| 117 | self.vel_input.set_range(0, 10) |
| 118 | self.vel_input.set_digits(3) |
| 119 | self.vel_input.set_increments(0.1, 100) |
| 120 | self.vel_input.connect("value-changed", self.vel_changed) |
| 121 | self.vel_label = Gtk.Label() |
| 122 | self.vel_label.set_text( |
| 123 | "Velocity Restriction" |
| 124 | ) #note: the velocity restrictions are not yet working, they need to be hooked up later |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 125 | |
| 126 | self.vol_input = Gtk.SpinButton() |
| 127 | self.vol_input.set_size_request(100, 20) |
| 128 | self.vol_input.set_numeric(True) |
kyle | 96e46e1 | 2021-02-10 21:47:55 -0800 | [diff] [blame] | 129 | self.vol_input.set_range(0, 12) |
| 130 | self.vol_input.set_digits(3) |
| 131 | self.vol_input.set_increments(0.1, 100) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 132 | self.vol_input.connect("value-changed", self.vol_changed) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 133 | self.vol_label = Gtk.Label() |
| 134 | self.vol_label.set_text("Voltage Restriction") |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 135 | self.vol_input.set_value( |
| 136 | self.field.active_multispline.getConstraint("VOLTAGE")) |
kyle | 0e5a9f3 | 2021-01-23 15:02:19 -0800 | [diff] [blame] | 137 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 138 | self.output_json = Gtk.Button.new_with_label("Export") |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 139 | self.output_json.set_size_request(100, 40) |
| 140 | self.output_json.connect("clicked", self.output_json_clicked) |
| 141 | |
| 142 | self.input_json = Gtk.Button.new_with_label("Import") |
| 143 | self.input_json.set_size_request(100, 40) |
| 144 | self.input_json.connect("clicked", self.input_json_clicked) |
| 145 | |
Ryan Yin | 85f861f | 2021-09-16 17:55:11 -0700 | [diff] [blame] | 146 | self.clear = Gtk.Button.new_with_label("Clear") |
| 147 | self.clear.set_size_request(100, 40) |
| 148 | self.clear.connect("clicked", self.clear_clicked) |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 149 | |
| 150 | self.undo = Gtk.Button.new_with_label("Undo (Ctrl + Z)") |
| 151 | self.undo.set_size_request(100, 40) |
| 152 | self.undo.connect("clicked", self.undo_func) |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 153 | #Dropdown feature |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 154 | self.label = Gtk.Label() |
| 155 | self.label.set_text("Change Field:") |
| 156 | self.label.set_size_request(100, 40) |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 157 | |
| 158 | game_store = Gtk.ListStore(str) |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 159 | |
| 160 | self.game_combo = Gtk.ComboBoxText() |
| 161 | self.game_combo.set_entry_text_column(0) |
| 162 | self.game_combo.connect("changed", self.input_combobox_choice) |
| 163 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 164 | for game in FIELDS.keys(): |
| 165 | self.game_combo.append_text(game) |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 166 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 167 | if FIELD in FIELDS.values(): |
| 168 | self.game_combo.set_active(list(FIELDS.values()).index(FIELD)) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 169 | self.game_combo.set_size_request(100, 40) |
| 170 | |
| 171 | limitControls = Gtk.FlowBox() |
| 172 | limitControls.set_min_children_per_line(1) |
| 173 | limitControls.set_max_children_per_line(2) |
| 174 | limitControls.add(self.long_label) |
| 175 | limitControls.add(self.long_input) |
| 176 | |
| 177 | limitControls.add(self.lat_label) |
| 178 | limitControls.add(self.lat_input) |
| 179 | |
| 180 | limitControls.add(self.vel_label) |
| 181 | limitControls.add(self.vel_input) |
| 182 | |
| 183 | limitControls.add(self.vol_label) |
| 184 | limitControls.add(self.vol_input) |
| 185 | |
| 186 | container.attach(limitControls, 5, 1, 1, 1) |
| 187 | |
| 188 | jsonControls = Gtk.FlowBox() |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 189 | jsonControls.set_min_children_per_line(5) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 190 | jsonControls.add(self.file_name_box) |
| 191 | jsonControls.add(self.output_json) |
| 192 | jsonControls.add(self.input_json) |
Ryan Yin | 85f861f | 2021-09-16 17:55:11 -0700 | [diff] [blame] | 193 | jsonControls.add(self.clear) |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 194 | jsonControls.add(self.undo) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 195 | container.attach(jsonControls, 1, 0, 1, 1) |
| 196 | |
| 197 | container.attach(self.label, 4, 0, 1, 1) |
| 198 | container.attach(self.game_combo, 5, 0, 1, 1) |
| 199 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 200 | container.attach(self.field, 1, 1, 4, 4) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 201 | |
| 202 | container.attach(self.field.graph, 0, 10, 10, 1) |
Kenneth Nishiyama | b45b7ba | 2021-01-27 21:45:34 -0800 | [diff] [blame] | 203 | |
| 204 | self.show_all() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 205 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 206 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 207 | window = GridWindow() |
| 208 | RunApp() |