blob: b7bc5ab283e21f4cdcf6196b726e6062e4210d75 [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
kyle0e5a9f32021-01-23 15:02:19 -080046 def long_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080047 value = self.long_input.get_value()
Ravago Jonesfa8da562022-07-02 18:10:22 -070048 self.field.active_multispline.setConstraint(
49 "LONGITUDINAL_ACCELERATION", value)
50 self.field.graph.schedule_recalculate(self.field.multisplines)
kyle0e5a9f32021-01-23 15:02:19 -080051
52 def lat_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080053 value = self.lat_input.get_value()
Ravago Jonesfa8da562022-07-02 18:10:22 -070054 self.field.active_multispline.setConstraint("LATERAL_ACCELERATION",
55 value)
56 self.field.graph.schedule_recalculate(self.field.multisplines)
kyle0e5a9f32021-01-23 15:02:19 -080057
kyle96e46e12021-02-10 21:47:55 -080058 def vel_changed(self, button):
59 value = self.vel_input.get_value()
kyle0e5a9f32021-01-23 15:02:19 -080060
61 def vol_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080062 value = self.vol_input.get_value()
Ravago Jonesfa8da562022-07-02 18:10:22 -070063 self.field.active_multispline.setConstraint("VOLTAGE", value)
64 self.field.graph.schedule_recalculate(self.field.multisplines)
kyle0e5a9f32021-01-23 15:02:19 -080065
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -080066 def input_combobox_choice(self, combo):
67 text = combo.get_active_text()
68 if text is not None:
69 print("Combo Clicked on: " + text)
Ravago Jones086a8872021-08-07 15:49:40 -070070 self.field.set_field(FIELDS[text])
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -080071
John Park91e69732019-03-03 13:12:43 -080072 def __init__(self):
73 Gtk.Window.__init__(self)
74
75 self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE)
76
Ravago Jones6d460fe2021-07-03 16:59:55 -070077 container = Gtk.Grid()
78 container.set_vexpand(True)
79 self.add(container)
John Park91e69732019-03-03 13:12:43 -080080
Ravago Jones6d460fe2021-07-03 16:59:55 -070081 self.field = FieldWidget()
John Park91e69732019-03-03 13:12:43 -080082
vlad56329f42020-06-12 21:41:08 -070083 self.method_connect("delete-event", basic_window.quit_main_loop)
Ravago Jones76ecec82021-08-07 14:37:08 -070084 self.method_connect("key-release-event", self.field.do_key_press_event)
John Park91e69732019-03-03 13:12:43 -080085
86 self.file_name_box = Gtk.Entry()
87 self.file_name_box.set_size_request(200, 40)
Ravago Jonesc26b9162021-06-30 20:12:48 -070088 self.file_name_box.set_text(FIELD.field_id + ".json")
John Park91e69732019-03-03 13:12:43 -080089 self.file_name_box.set_editable(True)
90
kyle0e5a9f32021-01-23 15:02:19 -080091 self.long_input = Gtk.SpinButton()
92 self.long_input.set_size_request(100, 20)
93 self.long_input.set_numeric(True)
Austin Schuhb9dc3f62021-03-31 20:06:49 -070094 self.long_input.set_range(0, 20)
kyle96e46e12021-02-10 21:47:55 -080095 self.long_input.set_digits(3)
96 self.long_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -080097 self.long_input.connect("value-changed", self.long_changed)
98 self.long_label = Gtk.Label()
kyle96e46e12021-02-10 21:47:55 -080099 self.long_label.set_text("Longitudinal Acceleration Restriction")
100 self.long_input.set_value(
Ravago Jonesfa8da562022-07-02 18:10:22 -0700101 self.field.active_multispline.getConstraint(
102 "LONGITUDINAL_ACCELERATION"))
kyle0e5a9f32021-01-23 15:02:19 -0800103
104 self.lat_input = Gtk.SpinButton()
105 self.lat_input.set_size_request(100, 20)
106 self.lat_input.set_numeric(True)
Austin Schuhb9dc3f62021-03-31 20:06:49 -0700107 self.lat_input.set_range(0, 20)
kyle96e46e12021-02-10 21:47:55 -0800108 self.lat_input.set_digits(3)
109 self.lat_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800110 self.lat_input.connect("value-changed", self.lat_changed)
111 self.lat_label = Gtk.Label()
kyle96e46e12021-02-10 21:47:55 -0800112 self.lat_label.set_text("Lateral Acceleration Restriction")
113 self.lat_input.set_value(
Ravago Jonesfa8da562022-07-02 18:10:22 -0700114 self.field.active_multispline.getConstraint(
115 "LATERAL_ACCELERATION"))
kyle0e5a9f32021-01-23 15:02:19 -0800116
kyle96e46e12021-02-10 21:47:55 -0800117 self.vel_input = Gtk.SpinButton()
118 self.vel_input.set_size_request(100, 20)
119 self.vel_input.set_numeric(True)
120 self.vel_input.set_range(0, 10)
121 self.vel_input.set_digits(3)
122 self.vel_input.set_increments(0.1, 100)
123 self.vel_input.connect("value-changed", self.vel_changed)
124 self.vel_label = Gtk.Label()
125 self.vel_label.set_text(
126 "Velocity Restriction"
127 ) #note: the velocity restrictions are not yet working, they need to be hooked up later
kyle0e5a9f32021-01-23 15:02:19 -0800128
129 self.vol_input = Gtk.SpinButton()
130 self.vol_input.set_size_request(100, 20)
131 self.vol_input.set_numeric(True)
kyle96e46e12021-02-10 21:47:55 -0800132 self.vol_input.set_range(0, 12)
133 self.vol_input.set_digits(3)
134 self.vol_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800135 self.vol_input.connect("value-changed", self.vol_changed)
kyle0e5a9f32021-01-23 15:02:19 -0800136 self.vol_label = Gtk.Label()
137 self.vol_label.set_text("Voltage Restriction")
Ravago Jonesfa8da562022-07-02 18:10:22 -0700138 self.vol_input.set_value(
139 self.field.active_multispline.getConstraint("VOLTAGE"))
kyle0e5a9f32021-01-23 15:02:19 -0800140
Ravago Jones6d460fe2021-07-03 16:59:55 -0700141 self.output_json = Gtk.Button.new_with_label("Export")
John Park909c0392020-03-05 23:56:30 -0800142 self.output_json.set_size_request(100, 40)
143 self.output_json.connect("clicked", self.output_json_clicked)
144
145 self.input_json = Gtk.Button.new_with_label("Import")
146 self.input_json.set_size_request(100, 40)
147 self.input_json.connect("clicked", self.input_json_clicked)
148
Ryan Yin85f861f2021-09-16 17:55:11 -0700149 self.clear = Gtk.Button.new_with_label("Clear")
150 self.clear.set_size_request(100, 40)
151 self.clear.connect("clicked", self.clear_clicked)
Ryan Yind8be3882021-10-13 20:59:41 -0700152
153 self.undo = Gtk.Button.new_with_label("Undo (Ctrl + Z)")
154 self.undo.set_size_request(100, 40)
155 self.undo.connect("clicked", self.undo_func)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800156 #Dropdown feature
Ravago Jones6d460fe2021-07-03 16:59:55 -0700157 self.label = Gtk.Label()
158 self.label.set_text("Change Field:")
159 self.label.set_size_request(100, 40)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800160
161 game_store = Gtk.ListStore(str)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800162
163 self.game_combo = Gtk.ComboBoxText()
164 self.game_combo.set_entry_text_column(0)
165 self.game_combo.connect("changed", self.input_combobox_choice)
166
Ravago Jones6d460fe2021-07-03 16:59:55 -0700167 for game in FIELDS.keys():
168 self.game_combo.append_text(game)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800169
Ravago Jones76ecec82021-08-07 14:37:08 -0700170 if FIELD in FIELDS.values():
171 self.game_combo.set_active(list(FIELDS.values()).index(FIELD))
Ravago Jones6d460fe2021-07-03 16:59:55 -0700172 self.game_combo.set_size_request(100, 40)
173
174 limitControls = Gtk.FlowBox()
175 limitControls.set_min_children_per_line(1)
176 limitControls.set_max_children_per_line(2)
177 limitControls.add(self.long_label)
178 limitControls.add(self.long_input)
179
180 limitControls.add(self.lat_label)
181 limitControls.add(self.lat_input)
182
183 limitControls.add(self.vel_label)
184 limitControls.add(self.vel_input)
185
186 limitControls.add(self.vol_label)
187 limitControls.add(self.vol_input)
188
189 container.attach(limitControls, 5, 1, 1, 1)
190
191 jsonControls = Gtk.FlowBox()
Ryan Yind8be3882021-10-13 20:59:41 -0700192 jsonControls.set_min_children_per_line(5)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700193 jsonControls.add(self.file_name_box)
194 jsonControls.add(self.output_json)
195 jsonControls.add(self.input_json)
Ryan Yin85f861f2021-09-16 17:55:11 -0700196 jsonControls.add(self.clear)
Ryan Yind8be3882021-10-13 20:59:41 -0700197 jsonControls.add(self.undo)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700198 container.attach(jsonControls, 1, 0, 1, 1)
199
200 container.attach(self.label, 4, 0, 1, 1)
201 container.attach(self.game_combo, 5, 0, 1, 1)
202
Ravago Jones76ecec82021-08-07 14:37:08 -0700203 container.attach(self.field, 1, 1, 4, 4)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700204
205 container.attach(self.field.graph, 0, 10, 10, 1)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800206
207 self.show_all()
John Park91e69732019-03-03 13:12:43 -0800208
Ravago Jones6d460fe2021-07-03 16:59:55 -0700209
John Park91e69732019-03-03 13:12:43 -0800210window = GridWindow()
211RunApp()