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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2017 by Simon Schneegans
//
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////
using GLib.Math;
namespace GnomePie {
/////////////////////////////////////////////////////////////////////////
/// Displays the preview of a Slice.
/////////////////////////////////////////////////////////////////////////
public class PiePreviewSliceRenderer : GLib.Object {
/////////////////////////////////////////////////////////////////////
/// Called when the user clicked on this Slice.
/////////////////////////////////////////////////////////////////////
public signal void on_clicked(int position);
/////////////////////////////////////////////////////////////////////
/// Called when the user clicked on the delete sign.
/////////////////////////////////////////////////////////////////////
public signal void on_remove(int position);
/////////////////////////////////////////////////////////////////////
/// The image used to display this oject.
/////////////////////////////////////////////////////////////////////
public Icon icon { get; private set; }
public ActionGroup action_group { get; private set; }
public string name { get; private set; default=""; }
public bool delete_hovered { get; private set; default=false; }
/////////////////////////////////////////////////////////////////////
/// The parent renderer.
/////////////////////////////////////////////////////////////////////
private unowned PiePreviewRenderer parent;
/////////////////////////////////////////////////////////////////////
/// The delete sign, displayed in the upper right corner of each
/// Slice.
/////////////////////////////////////////////////////////////////////
private PiePreviewDeleteSign delete_sign = null;
/////////////////////////////////////////////////////////////////////
/// Some AnimatedValues for smooth transitions.
/////////////////////////////////////////////////////////////////////
private AnimatedValue angle;
private AnimatedValue size;
private AnimatedValue activity;
private AnimatedValue clicked;
/////////////////////////////////////////////////////////////////////
/// Some constants determining the look and behaviour of this Slice.
/////////////////////////////////////////////////////////////////////
private const double pie_radius = 126;
private const double radius = 24;
private const double delete_x = 13;
private const double delete_y = -13;
private const double click_cancel_treshold = 5;
/////////////////////////////////////////////////////////////////////
/// Storing the position where a mouse click was executed. Useful for
/// canceling the click when the mouse moves some pixels.
/////////////////////////////////////////////////////////////////////
private double clicked_x = 0.0;
private double clicked_y = 0.0;
/////////////////////////////////////////////////////////////////////
/// The index of this slice in a pie. Clockwise assigned, starting
/// from the right-most slice.
/////////////////////////////////////////////////////////////////////
private int position;
/////////////////////////////////////////////////////////////////////
/// C'tor, sets everything up.
/////////////////////////////////////////////////////////////////////
public PiePreviewSliceRenderer(PiePreviewRenderer parent) {
this.delete_sign = new PiePreviewDeleteSign();
this.delete_sign.load();
this.delete_sign.on_clicked.connect(() => {
this.on_remove(this.position);
});
this.parent = parent;
this.angle = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 0, 0, 0, 0.5);
this.size = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 0, 0, 0, 1.0);
this.activity = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 0, 0, 0, 0.0);
this.clicked = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 1, 1, 0, 1.0);
}
/////////////////////////////////////////////////////////////////////
/// Loads an Action. All members are initialized accordingly.
/////////////////////////////////////////////////////////////////////
public void load(ActionGroup group) {
this.action_group = group;
// if it's a custom ActionGroup
if (group.get_type().depth() == 2 && group.actions.size > 0) {
this.icon = new Icon(group.actions[0].icon, (int)(PiePreviewSliceRenderer.radius*2));
this.name = group.actions[0].name;
} else {
this.icon = new Icon(GroupRegistry.descriptions[group.get_type().name()].icon, (int)(PiePreviewSliceRenderer.radius*2));
this.name = GroupRegistry.descriptions[group.get_type().name()].name;
}
}
/////////////////////////////////////////////////////////////////////
/// Updates the position where this object should be displayed.
/////////////////////////////////////////////////////////////////////
public void set_position(int position, bool smoothly = true) {
double direction = 2.0 * PI * position/parent.slice_count();
if (direction != this.angle.end) {
this.position = position;
this.angle.reset_target(direction, smoothly ? 0.5 : 0.0);
if (!smoothly)
this.angle.update(1.0);
}
}
/////////////////////////////////////////////////////////////////////
/// Updates the size of this object. All transitions will be smooth.
/////////////////////////////////////////////////////////////////////
public void set_size(double size) {
this.size.reset_target(size, 0.5);
this.delete_sign.set_size(size);
}
/////////////////////////////////////////////////////////////////////
/// Notifies that all quick actions should be disabled.
/////////////////////////////////////////////////////////////////////
public void disable_quickactions() {
this.action_group.disable_quickactions();
}
/////////////////////////////////////////////////////////////////////
/// Draws the slice to the given context.
/////////////////////////////////////////////////////////////////////
public void draw(double frame_time, Cairo.Context ctx) {
this.size.update(frame_time);
this.angle.update(frame_time);
this.activity.update(frame_time);
this.clicked.update(frame_time);
ctx.save();
// transform the context
ctx.translate(cos(this.angle.val)*PiePreviewSliceRenderer.pie_radius, sin(this.angle.val)*PiePreviewSliceRenderer.pie_radius);
double scale = this.size.val*this.clicked.val
+ this.activity.val*0.1 - 0.1;
ctx.save();
ctx.scale(scale, scale);
// paint the image
icon.paint_on(ctx);
ctx.restore();
ctx.translate(PiePreviewSliceRenderer.delete_x*this.size.val, PiePreviewSliceRenderer.delete_y*this.size.val);
this.delete_sign.draw(frame_time, ctx);
ctx.restore();
}
/////////////////////////////////////////////////////////////////////
/// Called when the mouse moves to another position.
/////////////////////////////////////////////////////////////////////
public bool on_mouse_move(double angle, double x, double y) {
double direction = 2.0 * PI * position/parent.slice_count();
double diff = fabs(angle-direction);
if (diff > PI)
diff = 2 * PI - diff;
bool active = diff < 0.5*PI/parent.slice_count();
if (active) {
this.activity.reset_target(1.0, 0.3);
this.delete_sign.show();
} else {
this.activity.reset_target(0.0, 0.3);
this.delete_sign.hide();
}
if (this.clicked.end == 0.9) {
double dist = GLib.Math.pow(x-this.clicked_x, 2) + GLib.Math.pow(y-this.clicked_y, 2);
if (dist > PiePreviewSliceRenderer.click_cancel_treshold*PiePreviewSliceRenderer.click_cancel_treshold)
this.clicked.reset_target(1.0, 0.1);
}
double own_x = cos(this.angle.val)*PiePreviewSliceRenderer.pie_radius;
double own_y = sin(this.angle.val)*PiePreviewSliceRenderer.pie_radius;
this.delete_hovered = this.delete_sign.on_mouse_move(x - own_x - PiePreviewSliceRenderer.delete_x*this.size.val,
y - own_y - PiePreviewSliceRenderer.delete_y*this.size.val);
return active;
}
/////////////////////////////////////////////////////////////////////
/// Called when the mouse leaves the area of this widget.
/////////////////////////////////////////////////////////////////////
public void on_mouse_leave() {
this.activity.reset_target(0.0, 0.3);
this.delete_sign.hide();
}
/////////////////////////////////////////////////////////////////////
/// Called when a button of the mouse is pressed.
/////////////////////////////////////////////////////////////////////
public void on_button_press(double x, double y) {
bool delete_pressed = false;
if (this.activity.end == 1.0) {
double own_x = cos(this.angle.val)*PiePreviewSliceRenderer.pie_radius;
double own_y = sin(this.angle.val)*PiePreviewSliceRenderer.pie_radius;
delete_pressed = this.delete_sign.on_button_press(x - own_x - PiePreviewSliceRenderer.delete_x*this.size.val,
y - own_y - PiePreviewSliceRenderer.delete_y*this.size.val);
}
if (!delete_pressed && this.activity.end == 1.0) {
this.clicked.reset_target(0.9, 0.1);
this.clicked_x = x;
this.clicked_y = y;
}
}
/////////////////////////////////////////////////////////////////////
/// Called when a button of the mouse is released.
/////////////////////////////////////////////////////////////////////
public void on_button_release(double x, double y) {
bool deleted = false;
if (this.activity.end == 1.0)
deleted = this.delete_sign.on_button_release(x, y);
if (!deleted && this.clicked.end == 0.9) {
this.clicked.reset_target(1.0, 0.1);
this.on_clicked(this.position);
}
}
}
}
|