blob: dc64dfaf6cb849b17a775a5367807b6fcfea3a74 [file] [log] [blame]
John Park91e69732019-03-03 13:12:43 -08001#!/usr/bin/python3
Ravago Jones6d460fe2021-07-03 16:59:55 -07002
3# matplotlib overrides fontconfig locations, so has to be imported before gtk
4import matplotlib
John Park91e69732019-03-03 13:12:43 -08005import gi
Ravago Jones6d460fe2021-07-03 16:59:55 -07006from path_edit import FieldWidget
7from basic_window import RunApp
8from constants import FIELDS, FIELD, SCREEN_SIZE
Ravago Jones5127ccc2022-07-31 16:32:45 -07009
John Park91e69732019-03-03 13:12:43 -080010gi.require_version('Gtk', '3.0')
11from gi.repository import Gdk, Gtk, GLib
12import cairo
vlad56329f42020-06-12 21:41:08 -070013import basic_window
Ravago Jones6d460fe2021-07-03 16:59:55 -070014import os
Ravago Jones26f7ad02021-02-05 15:45:59 -080015
Ravago Jones36c92f02021-07-24 16:35:33 -070016
John Park91e69732019-03-03 13:12:43 -080017class GridWindow(Gtk.Window):
Ravago Jones5127ccc2022-07-31 16:32:45 -070018
John Park91e69732019-03-03 13:12:43 -080019 def method_connect(self, event, cb):
Ravago Jones5127ccc2022-07-31 16:32:45 -070020
John Park91e69732019-03-03 13:12:43 -080021 def handler(obj, *args):
22 cb(*args)
23
John Park91e69732019-03-03 13:12:43 -080024 self.connect(event, handler)
25
Ryan Yin85f861f2021-09-16 17:55:11 -070026 def clear_clicked(self, button):
Ravago Jonesfa8da562022-07-02 18:10:22 -070027 self.field.clear()
Ryan Yin85f861f2021-09-16 17:55:11 -070028
John Park909c0392020-03-05 23:56:30 -080029 def output_json_clicked(self, button):
Ravago Jones6d460fe2021-07-03 16:59:55 -070030 self.field.export_json(self.file_name_box.get_text())
John Park909c0392020-03-05 23:56:30 -080031
32 def input_json_clicked(self, button):
Ravago Jones6d460fe2021-07-03 16:59:55 -070033 self.field.import_json(self.file_name_box.get_text())
kyle96e46e12021-02-10 21:47:55 -080034 self.long_input.set_value(
Ravago Jonesfa8da562022-07-02 18:10:22 -070035 self.field.active_multispline.getConstraint(
36 "LONGITUDINAL_ACCELERATION"))
kyle96e46e12021-02-10 21:47:55 -080037 self.lat_input.set_value(
Ravago Jonesfa8da562022-07-02 18:10:22 -070038 self.field.active_multispline.getConstraint(
39 "LATERAL_ACCELERATION"))
40 self.vol_input.set_value(
41 self.field.active_multispline.getConstraint("VOLTAGE"))
John Park91e69732019-03-03 13:12:43 -080042
Ryan Yind8be3882021-10-13 20:59:41 -070043 def undo_func(self, *args):
44 self.field.undo()
Ravago Jones8da89c42022-07-17 19:34:06 -070045
Nathan Leong295f7302022-07-23 16:09:31 -070046 def new_spline_clicked(self, *args):
47 self.field.new_spline()
48
49 def new_multispline_clicked(self, *args):
50 self.field.new_multispline()
51
kyle0e5a9f32021-01-23 15:02:19 -080052 def long_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080053 value = self.long_input.get_value()
Ravago Jonesfa8da562022-07-02 18:10:22 -070054 self.field.active_multispline.setConstraint(
55 "LONGITUDINAL_ACCELERATION", value)
56 self.field.graph.schedule_recalculate(self.field.multisplines)
kyle0e5a9f32021-01-23 15:02:19 -080057
58 def lat_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080059 value = self.lat_input.get_value()
Ravago Jonesfa8da562022-07-02 18:10:22 -070060 self.field.active_multispline.setConstraint("LATERAL_ACCELERATION",
61 value)
62 self.field.graph.schedule_recalculate(self.field.multisplines)
kyle0e5a9f32021-01-23 15:02:19 -080063
kyle96e46e12021-02-10 21:47:55 -080064 def vel_changed(self, button):
65 value = self.vel_input.get_value()
kyle0e5a9f32021-01-23 15:02:19 -080066
67 def vol_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080068 value = self.vol_input.get_value()
Ravago Jonesfa8da562022-07-02 18:10:22 -070069 self.field.active_multispline.setConstraint("VOLTAGE", value)
70 self.field.graph.schedule_recalculate(self.field.multisplines)
kyle0e5a9f32021-01-23 15:02:19 -080071
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -080072 def input_combobox_choice(self, combo):
73 text = combo.get_active_text()
74 if text is not None:
75 print("Combo Clicked on: " + text)
Ravago Jones086a8872021-08-07 15:49:40 -070076 self.field.set_field(FIELDS[text])
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -080077
John Park91e69732019-03-03 13:12:43 -080078 def __init__(self):
79 Gtk.Window.__init__(self)
80
81 self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE)
82
Ravago Jones6d460fe2021-07-03 16:59:55 -070083 container = Gtk.Grid()
84 container.set_vexpand(True)
85 self.add(container)
John Park91e69732019-03-03 13:12:43 -080086
Ravago Jones6d460fe2021-07-03 16:59:55 -070087 self.field = FieldWidget()
John Park91e69732019-03-03 13:12:43 -080088
vlad56329f42020-06-12 21:41:08 -070089 self.method_connect("delete-event", basic_window.quit_main_loop)
Ravago Jones76ecec82021-08-07 14:37:08 -070090 self.method_connect("key-release-event", self.field.do_key_press_event)
John Park91e69732019-03-03 13:12:43 -080091
92 self.file_name_box = Gtk.Entry()
Nathan Leong295f7302022-07-23 16:09:31 -070093 self.file_name_box.set_size_request(50, 40)
Ravago Jonesc26b9162021-06-30 20:12:48 -070094 self.file_name_box.set_text(FIELD.field_id + ".json")
John Park91e69732019-03-03 13:12:43 -080095 self.file_name_box.set_editable(True)
96
kyle0e5a9f32021-01-23 15:02:19 -080097 self.long_input = Gtk.SpinButton()
98 self.long_input.set_size_request(100, 20)
99 self.long_input.set_numeric(True)
Austin Schuhb9dc3f62021-03-31 20:06:49 -0700100 self.long_input.set_range(0, 20)
kyle96e46e12021-02-10 21:47:55 -0800101 self.long_input.set_digits(3)
102 self.long_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800103 self.long_input.connect("value-changed", self.long_changed)
104 self.long_label = Gtk.Label()
kyle96e46e12021-02-10 21:47:55 -0800105 self.long_label.set_text("Longitudinal Acceleration Restriction")
106 self.long_input.set_value(
Ravago Jonesfa8da562022-07-02 18:10:22 -0700107 self.field.active_multispline.getConstraint(
108 "LONGITUDINAL_ACCELERATION"))
kyle0e5a9f32021-01-23 15:02:19 -0800109
110 self.lat_input = Gtk.SpinButton()
111 self.lat_input.set_size_request(100, 20)
112 self.lat_input.set_numeric(True)
Austin Schuhb9dc3f62021-03-31 20:06:49 -0700113 self.lat_input.set_range(0, 20)
kyle96e46e12021-02-10 21:47:55 -0800114 self.lat_input.set_digits(3)
115 self.lat_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800116 self.lat_input.connect("value-changed", self.lat_changed)
117 self.lat_label = Gtk.Label()
kyle96e46e12021-02-10 21:47:55 -0800118 self.lat_label.set_text("Lateral Acceleration Restriction")
119 self.lat_input.set_value(
Ravago Jonesfa8da562022-07-02 18:10:22 -0700120 self.field.active_multispline.getConstraint(
121 "LATERAL_ACCELERATION"))
kyle0e5a9f32021-01-23 15:02:19 -0800122
kyle96e46e12021-02-10 21:47:55 -0800123 self.vel_input = Gtk.SpinButton()
124 self.vel_input.set_size_request(100, 20)
125 self.vel_input.set_numeric(True)
126 self.vel_input.set_range(0, 10)
127 self.vel_input.set_digits(3)
128 self.vel_input.set_increments(0.1, 100)
129 self.vel_input.connect("value-changed", self.vel_changed)
130 self.vel_label = Gtk.Label()
131 self.vel_label.set_text(
132 "Velocity Restriction"
133 ) #note: the velocity restrictions are not yet working, they need to be hooked up later
kyle0e5a9f32021-01-23 15:02:19 -0800134
135 self.vol_input = Gtk.SpinButton()
136 self.vol_input.set_size_request(100, 20)
137 self.vol_input.set_numeric(True)
kyle96e46e12021-02-10 21:47:55 -0800138 self.vol_input.set_range(0, 12)
139 self.vol_input.set_digits(3)
140 self.vol_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800141 self.vol_input.connect("value-changed", self.vol_changed)
kyle0e5a9f32021-01-23 15:02:19 -0800142 self.vol_label = Gtk.Label()
143 self.vol_label.set_text("Voltage Restriction")
Ravago Jonesfa8da562022-07-02 18:10:22 -0700144 self.vol_input.set_value(
145 self.field.active_multispline.getConstraint("VOLTAGE"))
kyle0e5a9f32021-01-23 15:02:19 -0800146
Ravago Jones6d460fe2021-07-03 16:59:55 -0700147 self.output_json = Gtk.Button.new_with_label("Export")
Nathan Leong295f7302022-07-23 16:09:31 -0700148 self.output_json.set_size_request(50, 40)
John Park909c0392020-03-05 23:56:30 -0800149 self.output_json.connect("clicked", self.output_json_clicked)
150
151 self.input_json = Gtk.Button.new_with_label("Import")
Nathan Leong295f7302022-07-23 16:09:31 -0700152 self.input_json.set_size_request(50, 40)
John Park909c0392020-03-05 23:56:30 -0800153 self.input_json.connect("clicked", self.input_json_clicked)
154
Ryan Yin85f861f2021-09-16 17:55:11 -0700155 self.clear = Gtk.Button.new_with_label("Clear")
Nathan Leong295f7302022-07-23 16:09:31 -0700156 self.clear.set_size_request(50, 40)
Ryan Yin85f861f2021-09-16 17:55:11 -0700157 self.clear.connect("clicked", self.clear_clicked)
Ryan Yind8be3882021-10-13 20:59:41 -0700158
159 self.undo = Gtk.Button.new_with_label("Undo (Ctrl + Z)")
Nathan Leong295f7302022-07-23 16:09:31 -0700160 self.undo.set_size_request(50, 40)
Ryan Yind8be3882021-10-13 20:59:41 -0700161 self.undo.connect("clicked", self.undo_func)
Nathan Leong295f7302022-07-23 16:09:31 -0700162
163 self.new_spline = Gtk.Button.new_with_label("Add Spline")
164 self.new_spline.set_size_request(50, 40)
165 self.new_spline.connect("clicked", self.new_spline_clicked)
166
167 self.new_multispline = Gtk.Button.new_with_label("Add Multispline")
168 self.new_multispline.set_size_request(50, 40)
169 self.new_multispline.connect("clicked", self.new_multispline_clicked)
170
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800171 #Dropdown feature
Ravago Jones6d460fe2021-07-03 16:59:55 -0700172 self.label = Gtk.Label()
173 self.label.set_text("Change Field:")
174 self.label.set_size_request(100, 40)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800175
176 game_store = Gtk.ListStore(str)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800177
178 self.game_combo = Gtk.ComboBoxText()
179 self.game_combo.set_entry_text_column(0)
180 self.game_combo.connect("changed", self.input_combobox_choice)
181
Ravago Jones6d460fe2021-07-03 16:59:55 -0700182 for game in FIELDS.keys():
183 self.game_combo.append_text(game)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800184
Ravago Jones76ecec82021-08-07 14:37:08 -0700185 if FIELD in FIELDS.values():
186 self.game_combo.set_active(list(FIELDS.values()).index(FIELD))
Ravago Jones6d460fe2021-07-03 16:59:55 -0700187 self.game_combo.set_size_request(100, 40)
188
189 limitControls = Gtk.FlowBox()
190 limitControls.set_min_children_per_line(1)
191 limitControls.set_max_children_per_line(2)
192 limitControls.add(self.long_label)
193 limitControls.add(self.long_input)
194
195 limitControls.add(self.lat_label)
196 limitControls.add(self.lat_input)
197
198 limitControls.add(self.vel_label)
199 limitControls.add(self.vel_input)
200
201 limitControls.add(self.vol_label)
202 limitControls.add(self.vol_input)
203
204 container.attach(limitControls, 5, 1, 1, 1)
205
206 jsonControls = Gtk.FlowBox()
Nathan Leong295f7302022-07-23 16:09:31 -0700207 jsonControls.set_min_children_per_line(7)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700208 jsonControls.add(self.file_name_box)
209 jsonControls.add(self.output_json)
210 jsonControls.add(self.input_json)
Ryan Yin85f861f2021-09-16 17:55:11 -0700211 jsonControls.add(self.clear)
Ryan Yind8be3882021-10-13 20:59:41 -0700212 jsonControls.add(self.undo)
Nathan Leong295f7302022-07-23 16:09:31 -0700213 jsonControls.add(self.new_spline)
214 jsonControls.add(self.new_multispline)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700215 container.attach(jsonControls, 1, 0, 1, 1)
216
217 container.attach(self.label, 4, 0, 1, 1)
218 container.attach(self.game_combo, 5, 0, 1, 1)
219
Ravago Jones76ecec82021-08-07 14:37:08 -0700220 container.attach(self.field, 1, 1, 4, 4)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700221
222 container.attach(self.field.graph, 0, 10, 10, 1)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800223
224 self.show_all()
John Park91e69732019-03-03 13:12:43 -0800225
Ravago Jones6d460fe2021-07-03 16:59:55 -0700226
John Park91e69732019-03-03 13:12:43 -0800227window = GridWindow()
228RunApp()