blob: b8d49a96df606e4d7fe83cf4cd22a954a295f226 [file] [log] [blame]
John Park91e69732019-03-03 13:12:43 -08001#!/usr/bin/python3
Parker Schuh19b93b12018-03-02 23:26:58 -08002import gi
3gi.require_version('Gtk', '3.0')
4from gi.repository import Gtk
5from gi.repository import GLib
6from gi.repository import Gdk
7from gi.repository import GdkX11
8import cairo
John Park91e69732019-03-03 13:12:43 -08009from constants import *
Parker Schuh19b93b12018-03-02 23:26:58 -080010
11identity = cairo.Matrix()
12
Ravago Jones26f7ad02021-02-05 15:45:59 -080013
Parker Schuh19b93b12018-03-02 23:26:58 -080014# Override the matrix of a cairo context.
15class OverrideMatrix(object):
Parker Schuhdc682952018-03-03 18:24:01 -080016 def __init__(self, cr, matrix):
17 self.cr = cr
18 self.matrix = matrix
Parker Schuh19b93b12018-03-02 23:26:58 -080019
Parker Schuhdc682952018-03-03 18:24:01 -080020 def __enter__(self):
21 self.cr.save()
22 self.cr.set_matrix(self.matrix)
23
24 def __exit__(self, type, value, traceback):
25 self.cr.restore()
26
Parker Schuh19b93b12018-03-02 23:26:58 -080027
28mainloop = GLib.MainLoop()
29
Ravago Jones26f7ad02021-02-05 15:45:59 -080030
Tabitha Jarvis1007a132018-12-12 21:47:54 -080031def set_color(cr, color):
32 if color.a == 1.0:
33 cr.set_source_rgb(color.r, color.g, color.b)
34 else:
35 cr.set_source_rgba(color.r, color.g, color.b, color.a)
Parker Schuhdc682952018-03-03 18:24:01 -080036
Ravago Jones26f7ad02021-02-05 15:45:59 -080037
Parker Schuh19b93b12018-03-02 23:26:58 -080038def quit_main_loop(*args):
Parker Schuhdc682952018-03-03 18:24:01 -080039 mainloop.quit()
40
Ravago Jones26f7ad02021-02-05 15:45:59 -080041
Parker Schuh19b93b12018-03-02 23:26:58 -080042def RunApp():
Parker Schuhdc682952018-03-03 18:24:01 -080043 try:
44 mainloop.run()
45 except KeyboardInterrupt:
46 print('\nCtrl+C hit, quitting')
47 mainloop.quit()
48
Parker Schuh19b93b12018-03-02 23:26:58 -080049
50# Create a GTK+ widget on which we will draw using Cairo
51class BaseWindow(Gtk.DrawingArea):
Parker Schuh19b93b12018-03-02 23:26:58 -080052
Parker Schuhdc682952018-03-03 18:24:01 -080053 # Draw in response to an expose-event
54 def __init__(self):
55 super(BaseWindow, self).__init__()
Parker Schuh19b93b12018-03-02 23:26:58 -080056
Ravago Jones26f7ad02021-02-05 15:45:59 -080057 self.set_size_request(2 * SCREEN_SIZE, SCREEN_SIZE)
Parker Schuhdc682952018-03-03 18:24:01 -080058 self.center = (0, 0)
Ravago Jones26f7ad02021-02-05 15:45:59 -080059 self.shape = (2 * SCREEN_SIZE, SCREEN_SIZE)
Parker Schuhdc682952018-03-03 18:24:01 -080060 self.needs_redraw = False
Parker Schuh19b93b12018-03-02 23:26:58 -080061
Parker Schuhdc682952018-03-03 18:24:01 -080062 def get_current_scale(self):
63 w_w, w_h = self.window_shape
64 w, h = self.shape
65 return min((w_w / w), (w_h / h))
Parker Schuh19b93b12018-03-02 23:26:58 -080066
Parker Schuhdc682952018-03-03 18:24:01 -080067 def init_extents(self, center, shape):
68 self.center = center
69 self.shape = shape
Parker Schuh19b93b12018-03-02 23:26:58 -080070
Parker Schuhdc682952018-03-03 18:24:01 -080071 # The gtk system creates cr which is a cairo_context_t (in the c docs), and then it
72 # passes it as a function argument to the "draw" event. do_draw is the default name.
73 def do_draw(self, cr):
74 cr.save()
75 cr.set_font_size(20)
76 cr.translate(self.window_shape[0] / 2, self.window_shape[1] / 2)
77 scale = self.get_current_scale()
78 cr.scale(scale, -scale)
79 cr.translate(-self.center[0], -self.center[1])
Ravago Jonesb7e46f72021-01-16 15:43:29 -080080 cr.reset_clip()
Parker Schuhdc682952018-03-03 18:24:01 -080081 self.needs_redraw = False
82 self.handle_draw(cr)
83 cr.restore()
Parker Schuh19b93b12018-03-02 23:26:58 -080084
Parker Schuhdc682952018-03-03 18:24:01 -080085 # Handle the expose-event by drawing
86 def handle_draw(self, cr):
87 pass