blob: f56b0d32493e24d86933a07cd24456f23ade4b64 [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
John Park91e69732019-03-03 13:12:43 -08009gi.require_version('Gtk', '3.0')
10from gi.repository import Gdk, Gtk, GLib
11import cairo
vlad56329f42020-06-12 21:41:08 -070012import basic_window
Ravago Jones6d460fe2021-07-03 16:59:55 -070013import os
Ravago Jones26f7ad02021-02-05 15:45:59 -080014
Ravago Jones36c92f02021-07-24 16:35:33 -070015
John Park91e69732019-03-03 13:12:43 -080016class GridWindow(Gtk.Window):
17 def method_connect(self, event, cb):
18 def handler(obj, *args):
19 cb(*args)
20
John Park91e69732019-03-03 13:12:43 -080021 self.connect(event, handler)
22
John Park91e69732019-03-03 13:12:43 -080023 def configure(self, event):
Ravago Jones6d460fe2021-07-03 16:59:55 -070024 self.field.window_shape = (event.width, event.height)
John Park91e69732019-03-03 13:12:43 -080025
John Park909c0392020-03-05 23:56:30 -080026 def output_json_clicked(self, button):
Ravago Jones6d460fe2021-07-03 16:59:55 -070027 self.field.export_json(self.file_name_box.get_text())
John Park909c0392020-03-05 23:56:30 -080028
29 def input_json_clicked(self, button):
Ravago Jones6d460fe2021-07-03 16:59:55 -070030 self.field.import_json(self.file_name_box.get_text())
kyle96e46e12021-02-10 21:47:55 -080031 self.long_input.set_value(
Ravago Jones6d460fe2021-07-03 16:59:55 -070032 self.field.points.getConstraint("LONGITUDINAL_ACCELERATION"))
kyle96e46e12021-02-10 21:47:55 -080033 self.lat_input.set_value(
Ravago Jones6d460fe2021-07-03 16:59:55 -070034 self.field.points.getConstraint("LATERAL_ACCELERATION"))
35 self.vol_input.set_value(self.field.points.getConstraint("VOLTAGE"))
John Park91e69732019-03-03 13:12:43 -080036
kyle0e5a9f32021-01-23 15:02:19 -080037 def long_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080038 value = self.long_input.get_value()
Ravago Jones6d460fe2021-07-03 16:59:55 -070039 self.field.points.setConstraint("LONGITUDINAL_ACCELERATION", value)
kyle0e5a9f32021-01-23 15:02:19 -080040
41 def lat_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080042 value = self.lat_input.get_value()
Ravago Jones6d460fe2021-07-03 16:59:55 -070043 self.field.points.setConstraint("LATERAL_ACCELERATION", value)
kyle0e5a9f32021-01-23 15:02:19 -080044
kyle96e46e12021-02-10 21:47:55 -080045 def vel_changed(self, button):
46 value = self.vel_input.get_value()
kyle0e5a9f32021-01-23 15:02:19 -080047
48 def vol_changed(self, button):
kyle96e46e12021-02-10 21:47:55 -080049 value = self.vol_input.get_value()
Ravago Jones6d460fe2021-07-03 16:59:55 -070050 self.field.points.setConstraint("VOLTAGE", value)
kyle0e5a9f32021-01-23 15:02:19 -080051
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -080052 def input_combobox_choice(self, combo):
53 text = combo.get_active_text()
54 if text is not None:
55 print("Combo Clicked on: " + text)
Ravago Jones6d460fe2021-07-03 16:59:55 -070056 #set_field(text)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -080057
John Park91e69732019-03-03 13:12:43 -080058 def __init__(self):
59 Gtk.Window.__init__(self)
60
61 self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE)
62
Ravago Jones6d460fe2021-07-03 16:59:55 -070063 container = Gtk.Grid()
64 container.set_vexpand(True)
65 self.add(container)
John Park91e69732019-03-03 13:12:43 -080066
67 self.eventBox = Gtk.EventBox()
John Park91e69732019-03-03 13:12:43 -080068 self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
Ravago Jones128fb992021-07-31 13:56:58 -070069 | Gdk.EventMask.BUTTON_PRESS_MASK
John Park91e69732019-03-03 13:12:43 -080070 | Gdk.EventMask.BUTTON_RELEASE_MASK
71 | Gdk.EventMask.POINTER_MOTION_MASK
72 | Gdk.EventMask.SCROLL_MASK
73 | Gdk.EventMask.KEY_PRESS_MASK)
74
Ravago Jones6d460fe2021-07-03 16:59:55 -070075 self.field = FieldWidget()
John Park91e69732019-03-03 13:12:43 -080076
vlad56329f42020-06-12 21:41:08 -070077 self.method_connect("delete-event", basic_window.quit_main_loop)
Ravago Jones128fb992021-07-31 13:56:58 -070078 self.method_connect("key-release-event", self.field.key_press)
79 self.method_connect("button-press-event", self.field.button_press)
80 self.method_connect("button-release-event", self.field.button_release)
John Park91e69732019-03-03 13:12:43 -080081 self.method_connect("configure-event", self.configure)
Ravago Jones128fb992021-07-31 13:56:58 -070082 self.method_connect("motion_notify_event", self.field.mouse_move)
John Park91e69732019-03-03 13:12:43 -080083
84 self.file_name_box = Gtk.Entry()
85 self.file_name_box.set_size_request(200, 40)
Ravago Jonesc26b9162021-06-30 20:12:48 -070086 self.file_name_box.set_text(FIELD.field_id + ".json")
John Park91e69732019-03-03 13:12:43 -080087 self.file_name_box.set_editable(True)
88
kyle0e5a9f32021-01-23 15:02:19 -080089 self.long_input = Gtk.SpinButton()
90 self.long_input.set_size_request(100, 20)
91 self.long_input.set_numeric(True)
Austin Schuhb9dc3f62021-03-31 20:06:49 -070092 self.long_input.set_range(0, 20)
kyle96e46e12021-02-10 21:47:55 -080093 self.long_input.set_digits(3)
94 self.long_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -080095 self.long_input.connect("value-changed", self.long_changed)
96 self.long_label = Gtk.Label()
kyle96e46e12021-02-10 21:47:55 -080097 self.long_label.set_text("Longitudinal Acceleration Restriction")
98 self.long_input.set_value(
Ravago Jones6d460fe2021-07-03 16:59:55 -070099 self.field.points.getConstraint("LONGITUDINAL_ACCELERATION"))
kyle0e5a9f32021-01-23 15:02:19 -0800100
101 self.lat_input = Gtk.SpinButton()
102 self.lat_input.set_size_request(100, 20)
103 self.lat_input.set_numeric(True)
Austin Schuhb9dc3f62021-03-31 20:06:49 -0700104 self.lat_input.set_range(0, 20)
kyle96e46e12021-02-10 21:47:55 -0800105 self.lat_input.set_digits(3)
106 self.lat_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800107 self.lat_input.connect("value-changed", self.lat_changed)
108 self.lat_label = Gtk.Label()
kyle96e46e12021-02-10 21:47:55 -0800109 self.lat_label.set_text("Lateral Acceleration Restriction")
110 self.lat_input.set_value(
Ravago Jones6d460fe2021-07-03 16:59:55 -0700111 self.field.points.getConstraint("LATERAL_ACCELERATION"))
kyle0e5a9f32021-01-23 15:02:19 -0800112
kyle96e46e12021-02-10 21:47:55 -0800113 self.vel_input = Gtk.SpinButton()
114 self.vel_input.set_size_request(100, 20)
115 self.vel_input.set_numeric(True)
116 self.vel_input.set_range(0, 10)
117 self.vel_input.set_digits(3)
118 self.vel_input.set_increments(0.1, 100)
119 self.vel_input.connect("value-changed", self.vel_changed)
120 self.vel_label = Gtk.Label()
121 self.vel_label.set_text(
122 "Velocity Restriction"
123 ) #note: the velocity restrictions are not yet working, they need to be hooked up later
kyle0e5a9f32021-01-23 15:02:19 -0800124
125 self.vol_input = Gtk.SpinButton()
126 self.vol_input.set_size_request(100, 20)
127 self.vol_input.set_numeric(True)
kyle96e46e12021-02-10 21:47:55 -0800128 self.vol_input.set_range(0, 12)
129 self.vol_input.set_digits(3)
130 self.vol_input.set_increments(0.1, 100)
kyle0e5a9f32021-01-23 15:02:19 -0800131 self.vol_input.connect("value-changed", self.vol_changed)
kyle0e5a9f32021-01-23 15:02:19 -0800132 self.vol_label = Gtk.Label()
133 self.vol_label.set_text("Voltage Restriction")
Ravago Jones6d460fe2021-07-03 16:59:55 -0700134 self.vol_input.set_value(self.field.points.getConstraint("VOLTAGE"))
kyle0e5a9f32021-01-23 15:02:19 -0800135
Ravago Jones6d460fe2021-07-03 16:59:55 -0700136 self.output_json = Gtk.Button.new_with_label("Export")
John Park909c0392020-03-05 23:56:30 -0800137 self.output_json.set_size_request(100, 40)
138 self.output_json.connect("clicked", self.output_json_clicked)
139
140 self.input_json = Gtk.Button.new_with_label("Import")
141 self.input_json.set_size_request(100, 40)
142 self.input_json.connect("clicked", self.input_json_clicked)
143
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800144 #Dropdown feature
Ravago Jones6d460fe2021-07-03 16:59:55 -0700145 self.label = Gtk.Label()
146 self.label.set_text("Change Field:")
147 self.label.set_size_request(100, 40)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800148
149 game_store = Gtk.ListStore(str)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800150
151 self.game_combo = Gtk.ComboBoxText()
152 self.game_combo.set_entry_text_column(0)
153 self.game_combo.connect("changed", self.input_combobox_choice)
154
Ravago Jones6d460fe2021-07-03 16:59:55 -0700155 for game in FIELDS.keys():
156 self.game_combo.append_text(game)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800157
158 self.game_combo.set_active(0)
Ravago Jones6d460fe2021-07-03 16:59:55 -0700159 self.game_combo.set_size_request(100, 40)
160
161 limitControls = Gtk.FlowBox()
162 limitControls.set_min_children_per_line(1)
163 limitControls.set_max_children_per_line(2)
164 limitControls.add(self.long_label)
165 limitControls.add(self.long_input)
166
167 limitControls.add(self.lat_label)
168 limitControls.add(self.lat_input)
169
170 limitControls.add(self.vel_label)
171 limitControls.add(self.vel_input)
172
173 limitControls.add(self.vol_label)
174 limitControls.add(self.vol_input)
175
176 container.attach(limitControls, 5, 1, 1, 1)
177
178 jsonControls = Gtk.FlowBox()
179 jsonControls.set_min_children_per_line(3)
180 jsonControls.add(self.file_name_box)
181 jsonControls.add(self.output_json)
182 jsonControls.add(self.input_json)
183 container.attach(jsonControls, 1, 0, 1, 1)
184
185 container.attach(self.label, 4, 0, 1, 1)
186 container.attach(self.game_combo, 5, 0, 1, 1)
187
188 self.eventBox.add(self.field)
189 container.attach(self.eventBox, 1, 1, 4, 4)
190
191 container.attach(self.field.graph, 0, 10, 10, 1)
Kenneth Nishiyamab45b7ba2021-01-27 21:45:34 -0800192
193 self.show_all()
John Park91e69732019-03-03 13:12:43 -0800194
Ravago Jones6d460fe2021-07-03 16:59:55 -0700195
John Park91e69732019-03-03 13:12:43 -0800196window = GridWindow()
197RunApp()