John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python3 |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 2 | import gi |
| 3 | gi.require_version('Gtk', '3.0') |
| 4 | from gi.repository import Gtk |
| 5 | from gi.repository import GLib |
| 6 | from gi.repository import Gdk |
| 7 | from gi.repository import GdkX11 |
| 8 | import cairo |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame^] | 9 | from constants import * |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 10 | |
| 11 | identity = cairo.Matrix() |
| 12 | |
| 13 | # Override the matrix of a cairo context. |
| 14 | class OverrideMatrix(object): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 15 | def __init__(self, cr, matrix): |
| 16 | self.cr = cr |
| 17 | self.matrix = matrix |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 18 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 19 | def __enter__(self): |
| 20 | self.cr.save() |
| 21 | self.cr.set_matrix(self.matrix) |
| 22 | |
| 23 | def __exit__(self, type, value, traceback): |
| 24 | self.cr.restore() |
| 25 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 26 | |
| 27 | mainloop = GLib.MainLoop() |
| 28 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 29 | def set_color(cr, color): |
| 30 | if color.a == 1.0: |
| 31 | cr.set_source_rgb(color.r, color.g, color.b) |
| 32 | else: |
| 33 | cr.set_source_rgba(color.r, color.g, color.b, color.a) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 34 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 35 | def quit_main_loop(*args): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 36 | mainloop.quit() |
| 37 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 38 | def RunApp(): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 39 | try: |
| 40 | mainloop.run() |
| 41 | except KeyboardInterrupt: |
| 42 | print('\nCtrl+C hit, quitting') |
| 43 | mainloop.quit() |
| 44 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 45 | |
| 46 | # Create a GTK+ widget on which we will draw using Cairo |
| 47 | class BaseWindow(Gtk.DrawingArea): |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 48 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 49 | # Draw in response to an expose-event |
| 50 | def __init__(self): |
| 51 | super(BaseWindow, self).__init__() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 52 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame^] | 53 | self.set_size_request(2*SCREEN_SIZE, SCREEN_SIZE) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 54 | self.center = (0, 0) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame^] | 55 | self.shape = (2*SCREEN_SIZE, SCREEN_SIZE) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 56 | self.needs_redraw = False |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 57 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 58 | def get_current_scale(self): |
| 59 | w_w, w_h = self.window_shape |
| 60 | w, h = self.shape |
| 61 | return min((w_w / w), (w_h / h)) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 62 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 63 | def init_extents(self, center, shape): |
| 64 | self.center = center |
| 65 | self.shape = shape |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 66 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 67 | # The gtk system creates cr which is a cairo_context_t (in the c docs), and then it |
| 68 | # passes it as a function argument to the "draw" event. do_draw is the default name. |
| 69 | def do_draw(self, cr): |
| 70 | cr.save() |
| 71 | cr.set_font_size(20) |
| 72 | cr.translate(self.window_shape[0] / 2, self.window_shape[1] / 2) |
| 73 | scale = self.get_current_scale() |
| 74 | cr.scale(scale, -scale) |
| 75 | cr.translate(-self.center[0], -self.center[1]) |
| 76 | self.needs_redraw = False |
| 77 | self.handle_draw(cr) |
| 78 | cr.restore() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 79 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 80 | # Handle the expose-event by drawing |
| 81 | def handle_draw(self, cr): |
| 82 | pass |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame^] | 83 | |