summaryrefslogtreecommitdiff
path: root/src/gui/piePreviewAddSign.vala
blob: 53705c571f8196ccdc8c9e4aa63d06644b1a30d0 (plain)
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
/////////////////////////////////////////////////////////////////////////
// Copyright 2011-2021 Simon Schneegans
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////

using GLib.Math;

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// A liitle plus-sign displayed on the preview widget to indicate where
/// the user may add a new Slice.
/////////////////////////////////////////////////////////////////////////

public class PiePreviewAddSign : GLib.Object {

    /////////////////////////////////////////////////////////////////////
    /// Gets emitted, when the users clicks on this object.
    /////////////////////////////////////////////////////////////////////

    public signal void on_clicked(int position);

    /////////////////////////////////////////////////////////////////////
    /// The image used to display this oject.
    /////////////////////////////////////////////////////////////////////

    public Image icon { get; private set; }

    /////////////////////////////////////////////////////////////////////
    /// True, when the add sign is currently visible.
    /////////////////////////////////////////////////////////////////////

    public bool visible { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// The position of the sign in its parent Pie. May be 2.5 for
    /// example.
    /////////////////////////////////////////////////////////////////////

    private double position = 0;

    /////////////////////////////////////////////////////////////////////
    /// The parent renderer.
    /////////////////////////////////////////////////////////////////////

    private unowned PiePreviewRenderer parent;

    /////////////////////////////////////////////////////////////////////
    /// Some values used for displaying this sign.
    /////////////////////////////////////////////////////////////////////

    private double time = 0;
    private double max_size = 0;
    private double angle = 0;
    private AnimatedValue size;
    private AnimatedValue alpha;
    private AnimatedValue activity;
    private AnimatedValue clicked;

    /////////////////////////////////////////////////////////////////////
    /// C'tor, sets everything up.
    /////////////////////////////////////////////////////////////////////

    public PiePreviewAddSign(PiePreviewRenderer parent) {
        this.parent = parent;

        this.size = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 0, 0, 0, 2.0);
        this.alpha = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 0, 0, 0, 0.0);
        this.activity = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, -3, -3, 0, 0.0);
        this.clicked = new AnimatedValue.cubic(AnimatedValue.Direction.OUT, 1, 1, 0, 0.0);
    }

    /////////////////////////////////////////////////////////////////////
    /// Loads the desired icon for this sign.
    /////////////////////////////////////////////////////////////////////

    public void load() {
        this.icon = new Icon("list-add", 36);
    }

    /////////////////////////////////////////////////////////////////////
    /// Updates the position where this object should be displayed.
    /////////////////////////////////////////////////////////////////////

    public void set_position(int position) {
        double new_position = position;

        if (!this.parent.drag_n_drop_mode)
            new_position += 0.5;

        this.position = new_position;
        this.angle = 2.0 * PI * new_position/parent.slice_count();
    }

    /////////////////////////////////////////////////////////////////////
    /// Makes this object visible.
    /////////////////////////////////////////////////////////////////////

    public void show() {
        this.visible = true;
        this.size.reset_target(this.max_size, 0.3);
        this.alpha.reset_target(1.0, 0.3);
    }

    /////////////////////////////////////////////////////////////////////
    /// Makes this object invisible.
    /////////////////////////////////////////////////////////////////////

    public void hide() {
        this.visible = false;
        this.size.reset_target(0.0, 0.3);
        this.alpha.reset_target(0.0, 0.3);
    }

    /////////////////////////////////////////////////////////////////////
    /// Updates the size of this object. All transitions will be smooth.
    /////////////////////////////////////////////////////////////////////

    public void set_size(double size) {
        this.max_size = size;
        this.size.reset_target(size, 0.5);
    }

    /////////////////////////////////////////////////////////////////////
    /// Draws the sign to the given context.
    /////////////////////////////////////////////////////////////////////

    public void draw(double frame_time, Cairo.Context ctx) {

        this.time += frame_time;

        this.size.update(frame_time);
        this.alpha.update(frame_time);
        this.activity.update(frame_time);
        this.clicked.update(frame_time);

        if (this.parent.slice_count() == 0) {
            ctx.save();

            double scale = this.clicked.val
                         + GLib.Math.sin(this.time*10)*0.02*this.alpha.val
                         + this.alpha.val*0.08 - 0.1;
            ctx.scale(scale, scale);

            // paint the image
            icon.paint_on(ctx);

            ctx.restore();

        } else if (this.alpha.val*this.activity.val > 0) {
            ctx.save();

            // distance from the center
            double radius = 120;

            // transform the context
            ctx.translate(cos(this.angle)*radius, sin(this.angle)*radius);
            double scale = this.size.val*this.clicked.val
                         + this.activity.val*0.07
                         + GLib.Math.sin(this.time*10)*0.03*this.activity.val
                         - 0.1;
            ctx.scale(scale, scale);

            // paint the image
            icon.paint_on(ctx, this.alpha.val*this.activity.val);

            ctx.restore();
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse moves to another position.
    /////////////////////////////////////////////////////////////////////

    public void on_mouse_move(double angle) {
        if (parent.slice_count() > 0) {
            double direction = 2.0 * PI * position/parent.slice_count();
            double diff = fabs(angle-direction);

            if (diff > PI)
                diff = 2 * PI - diff;

            if (diff < 0.5*PI/parent.slice_count()) this.activity.reset_target(1.0, 1.0);
            else                                    this.activity.reset_target(-3.0, 1.5);
        } else {
            this.activity.reset_target(1.0, 1.0);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when a button of the mouse is pressed.
    /////////////////////////////////////////////////////////////////////

    public void on_button_press(double x, double y) {
        if (this.activity.end == 1.0) {
            this.clicked.reset_target(0.9, 0.1);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when a button of the mouse is released.
    /////////////////////////////////////////////////////////////////////

    public void on_button_release(double x, double y) {
        if (this.clicked.end == 0.9) {
            this.on_clicked((int)this.position);
            this.clicked.reset_target(1.0, 0.1);
        }
    }
}

}