Reticule lines to extend through the whole window.
They are mostly meant to help locate the focus when scrolling through the large star map: scroll x (or y) until you see the line then scroll y (or x) to find the focus; so they should extend to the scrollregion.
This commit is contained in:
parent
b04472619b
commit
8e88347e37
38
reticule.py
38
reticule.py
|
|
@ -22,19 +22,16 @@
|
||||||
|
|
||||||
class Reticule:
|
class Reticule:
|
||||||
|
|
||||||
def __init__(self, canvas, radius_x=35, radius_y=20, color='green'):
|
def __init__(self, canvas, width, height, radius_x=35, radius_y=20, color='green'):
|
||||||
self.canvas = canvas
|
self.canvas = canvas
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
self.radius_x = radius_x
|
self.radius_x = radius_x
|
||||||
self.radius_y = radius_y
|
self.radius_y = radius_y
|
||||||
self.color = color
|
self.color = color
|
||||||
self.item_ids = []
|
self.item_ids = []
|
||||||
self.init_reticule()
|
self.init_reticule()
|
||||||
self.xy_cache = 0, 0
|
self.xy_cache = 0, 0
|
||||||
self.canvas.bind(
|
|
||||||
'<Configure>',
|
|
||||||
self.handle_canvas_resize,
|
|
||||||
add='+',
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_reticule_coords(self, x, y):
|
def get_reticule_coords(self, x, y):
|
||||||
'''
|
'''
|
||||||
|
|
@ -42,8 +39,8 @@ class Reticule:
|
||||||
rectangle yield coords for four lines and an oval suitable for
|
rectangle yield coords for four lines and an oval suitable for
|
||||||
passing to canvas.coords() method.
|
passing to canvas.coords() method.
|
||||||
'''
|
'''
|
||||||
width = int(self.canvas['width'])
|
width = self.width
|
||||||
height = int(self.canvas['height'])
|
height = self.height
|
||||||
|
|
||||||
top = y - self.radius_y
|
top = y - self.radius_y
|
||||||
left = x - self.radius_x
|
left = x - self.radius_x
|
||||||
|
|
@ -58,7 +55,7 @@ class Reticule:
|
||||||
|
|
||||||
def init_reticule(self):
|
def init_reticule(self):
|
||||||
append = self.item_ids.append
|
append = self.item_ids.append
|
||||||
w, h = int(self.canvas['width']), int(self.canvas['height'])
|
w, h = self.width, self.height
|
||||||
coords = self.get_reticule_coords(w >> 1, h >> 1) # Center.
|
coords = self.get_reticule_coords(w >> 1, h >> 1) # Center.
|
||||||
|
|
||||||
append(canvas.create_line(*next(coords), fill=self.color))
|
append(canvas.create_line(*next(coords), fill=self.color))
|
||||||
|
|
@ -76,36 +73,15 @@ class Reticule:
|
||||||
def handle_set_event(self, event):
|
def handle_set_event(self, event):
|
||||||
self.set_reticule(event.x, event.y)
|
self.set_reticule(event.x, event.y)
|
||||||
|
|
||||||
def handle_canvas_resize(self, _event):
|
|
||||||
# We're going to assume that some other callback has updated the
|
|
||||||
# canvas values. Otherwise we could get the new width and height
|
|
||||||
# from the Configure event.
|
|
||||||
self.set_reticule(*self.xy_cache)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from tkinter import *
|
from tkinter import *
|
||||||
|
|
||||||
def handle_canvas_resize(event):
|
|
||||||
# I don't know why the Tk system doesn't update these values.
|
|
||||||
canvas['width'] = event.width
|
|
||||||
canvas['height'] = event.height
|
|
||||||
# update reticule here/now?
|
|
||||||
# If the canvas window has enlarged then some of the lines will have
|
|
||||||
# to be extended.
|
|
||||||
|
|
||||||
canvas = Canvas(bg='black')
|
canvas = Canvas(bg='black')
|
||||||
|
|
||||||
canvas.bind('<Configure>', handle_canvas_resize)
|
|
||||||
canvas.pack(expand=True, fill=BOTH)
|
canvas.pack(expand=True, fill=BOTH)
|
||||||
canvas.update()
|
|
||||||
|
|
||||||
# Let the configure event propagate and trigger the
|
reticule = Reticule(canvas, 1024, 768)
|
||||||
# handle_canvas_resize() callback to set the width
|
|
||||||
# and height values of the canvas correctly so that
|
|
||||||
# the reticule lines get drawn correctly.
|
|
||||||
|
|
||||||
reticule = Reticule(canvas)
|
|
||||||
canvas.bind('<Button-1>', reticule.handle_set_event)
|
canvas.bind('<Button-1>', reticule.handle_set_event)
|
||||||
canvas.bind('<B1-Motion>', reticule.handle_set_event)
|
canvas.bind('<B1-Motion>', reticule.handle_set_event)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue