forked from tstriker/hamster-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpie_menu.py
More file actions
executable file
·118 lines (89 loc) · 3.32 KB
/
pie_menu.py
File metadata and controls
executable file
·118 lines (89 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2010 Toms Bauģis <toms.baugis at gmail.com>
"""
Bit of pie in progress.
"""
from gi.repository import Gtk as gtk
from lib import graphics
from contrib.euclid import Vector2
import math
class Sector(graphics.Sprite):
def __init__(self, inner_radius, outer_radius, start_angle = 0, end_angle = 0):
graphics.Sprite.__init__(self, interactive = True)
self.inner_radius = inner_radius
self.outer_radius = outer_radius
self.start_angle = start_angle
self.end_angle = end_angle
self.fill = None
self.stroke = "#aaa"
self.connect("on-render", self.on_render)
def on_render(self, sprite):
angle = self.start_angle - self.end_angle
self.graphics.arc(0, 0, self.inner_radius, angle, 0)
if abs(angle) >= math.pi * 2:
self.graphics.move_to(self.outer_radius, 0)
else:
self.graphics.line_to(self.outer_radius, 0)
self.graphics.arc_negative(0, 0, self.outer_radius, 0, angle)
if self.fill:
self.graphics.close_path()
# just for fun
self.graphics.move_to(150, -15)
self.graphics.rectangle(150,-15,10,10)
self.graphics.fill_stroke(self.fill, self.stroke)
class Menu(graphics.Sprite):
def __init__(self, x, y):
graphics.Sprite.__init__(self, x, y, interactive=True, draggable=True)
self.graphics.arc(0, 0, 10, 0, math.pi * 2)
self.graphics.fill("#aaa")
self.menu = []
for i in range(20):
self.add_item()
def on_mouse_over(self, sprite):
sprite.fill = "#ddd"
def on_mouse_out(self, sprite):
sprite.fill = ""
def on_click(self, sprite, event):
self.add_item()
def add_item(self):
item = Sector(25, 50, math.pi / 2, 0)
item.connect("on-mouse-over", self.on_mouse_over)
item.connect("on-mouse-out", self.on_mouse_out)
item.connect("on-click", self.on_click)
self.menu.append(item)
self.add_child(item)
current_angle = 0
angle = math.pi * 2 / len(self.menu)
for i, item in enumerate(self.menu):
item.start_angle = current_angle
item.rotation = item.start_angle
item.end_angle = current_angle + angle #- angle * 0.1
item.inner_radius = 25 + len(self.menu) / 2.0 #+ i * 2
item.outer_radius = 50 + len(self.menu) * 2 #+ i * 2
current_angle += angle
class Scene(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self)
self.max_width = 50
self.menu = Menu(200, 200)
self.add_child(self.menu)
self.connect("on-enter-frame", self.on_enter_frame)
self.framerate = 30
def on_enter_frame(self, scene, context):
# turn the menu a bit and queue redraw
self.menu.rotation += 0.004
self.redraw()
class BasicWindow:
def __init__(self):
window = gtk.Window()
window.set_size_request(400, 400)
window.connect("delete_event", lambda *args: gtk.main_quit())
self.scene = Scene()
window.add(self.scene)
window.show_all()
if __name__ == "__main__":
example = BasicWindow()
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL) # gtk3 screws up ctrl+c
gtk.main()