103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright © 2024 Simon Forman
|
|
#
|
|
# This file is part of game
|
|
#
|
|
# game is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# game is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with game. If not see <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
|
|
RETICULE = 'reticule_tag'
|
|
|
|
|
|
class Reticule:
|
|
|
|
def __init__(self, canvas, width, height, radius_x=35, radius_y=20, color='green'):
|
|
self.canvas = canvas
|
|
self.width = width
|
|
self.height = height
|
|
self.radius_x = radius_x
|
|
self.radius_y = radius_y
|
|
self.color = color
|
|
self.item_ids = []
|
|
self.init_reticule()
|
|
self.xy_cache = 0, 0
|
|
|
|
def get_reticule_coords(self, x, y):
|
|
'''
|
|
Given the width and height of a rectangle and x-y coords in that
|
|
rectangle yield coords for four lines and an oval suitable for
|
|
passing to canvas.coords() method.
|
|
'''
|
|
width = self.width
|
|
height = self.height
|
|
|
|
top = y - self.radius_y
|
|
left = x - self.radius_x
|
|
bottom = y + self.radius_y
|
|
right = x + self.radius_x
|
|
|
|
yield 0, y, left, y
|
|
yield right, y, width - 1, y
|
|
yield x, 0, x, top
|
|
yield x, bottom, x, height - 1
|
|
yield left, top, right, bottom
|
|
|
|
def init_reticule(self):
|
|
append = self.item_ids.append
|
|
w, h = self.width, self.height
|
|
coords = self.get_reticule_coords(w >> 1, h >> 1) # Center.
|
|
|
|
append(canvas.create_line(*next(coords), tags=RETICULE, fill=self.color))
|
|
append(canvas.create_line(*next(coords), tags=RETICULE, fill=self.color))
|
|
append(canvas.create_line(*next(coords), tags=RETICULE, fill=self.color))
|
|
append(canvas.create_line(*next(coords), tags=RETICULE, fill=self.color))
|
|
append(canvas.create_oval(*next(coords), tags=RETICULE, outline=self.color))
|
|
|
|
def set_reticule(self, x, y):
|
|
self.xy_cache = x, y
|
|
coords = self.get_reticule_coords(x, y)
|
|
for item_id, item_coords in zip(self.item_ids, coords):
|
|
self.canvas.coords(item_id, *item_coords)
|
|
|
|
def handle_set_event(self, event):
|
|
self.set_reticule(event.x, event.y)
|
|
|
|
def hide(self):
|
|
self.canvas.itemconfig(RETICULE, state=HIDDEN)
|
|
|
|
def show(self):
|
|
self.canvas.itemconfig(RETICULE, state=NORMAL)
|
|
|
|
def toggle(self):
|
|
current_state = self.canvas.itemconfig(RETICULE, 'state')[4]
|
|
new_state = NORMAL if current_state == HIDDEN else HIDDEN
|
|
self.canvas.itemconfig(RETICULE, state=new_state)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
from tkinter import *
|
|
|
|
canvas = Canvas(bg='black')
|
|
|
|
canvas.pack(expand=True, fill=BOTH)
|
|
|
|
reticule = Reticule(canvas, 1024, 768)
|
|
canvas.bind('<Button-1>', reticule.handle_set_event)
|
|
canvas.bind('<B1-Motion>', reticule.handle_set_event)
|
|
|
|
#canvas.mainloop()
|