~3v1n0/unity/light-shortcuts

« back to all changes in this revision

Viewing changes to plugins/unity-mt-grab-handles/src/unity-mt-grab-handle-group.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-04-26 12:41:09 UTC
  • Revision ID: mail@3v1n0.net-20130426124109-t3b2shjah2omiqa2
Unity: Remove all the views, but the Shortcuts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2011 Canonical Ltd
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License version 3 as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Sam Spilsbury <sam.spilsbury@canonical.com>
17
 
 */
18
 
 
19
 
#include "unity-mt-grab-handle-group.h"
20
 
 
21
 
unsigned int unity::MT::FADE_MSEC = 0;
22
 
 
23
 
void
24
 
unity::MT::GrabHandleGroup::show(unsigned int handles)
25
 
{
26
 
  for(const unity::MT::GrabHandle::Ptr & handle : mHandles)
27
 
    if (handles & handle->id ())
28
 
      handle->show();
29
 
 
30
 
  mState = State::FADE_IN;
31
 
}
32
 
 
33
 
void
34
 
unity::MT::GrabHandleGroup::hide()
35
 
{
36
 
  for(const unity::MT::GrabHandle::Ptr & handle : mHandles)
37
 
    handle->hide();
38
 
 
39
 
  mState = State::FADE_OUT;
40
 
}
41
 
 
42
 
void
43
 
unity::MT::GrabHandleGroup::raiseHandle(const std::shared_ptr <const unity::MT::GrabHandle> &h)
44
 
{
45
 
  mOwner->raiseGrabHandle (h);
46
 
}
47
 
 
48
 
bool
49
 
unity::MT::GrabHandleGroup::animate(unsigned int msec)
50
 
{
51
 
  mMoreAnimate = false;
52
 
 
53
 
  switch (mState)
54
 
  {
55
 
    case State::FADE_IN:
56
 
 
57
 
      mOpacity += ((float) msec / (float) unity::MT::FADE_MSEC) * std::numeric_limits <unsigned short>::max ();
58
 
 
59
 
      if (mOpacity >= std::numeric_limits <unsigned short>::max ())
60
 
      {
61
 
        mOpacity = std::numeric_limits <unsigned short>::max ();
62
 
        mState = State::NONE;
63
 
      }
64
 
      break;
65
 
    case State::FADE_OUT:
66
 
      mOpacity -= ((float) msec / (float) unity::MT::FADE_MSEC) * std::numeric_limits <unsigned short>::max ();
67
 
 
68
 
      if (mOpacity <= 0)
69
 
      {
70
 
        mOpacity = 0;
71
 
        mState = State::NONE;
72
 
      }
73
 
      break;
74
 
    default:
75
 
      break;
76
 
  }
77
 
 
78
 
  mMoreAnimate = mState != State::NONE;
79
 
 
80
 
  return mMoreAnimate;
81
 
}
82
 
 
83
 
int
84
 
unity::MT::GrabHandleGroup::opacity()
85
 
{
86
 
  return mOpacity;
87
 
}
88
 
 
89
 
bool
90
 
unity::MT::GrabHandleGroup::visible()
91
 
{
92
 
  return mOpacity > 0.0f;
93
 
}
94
 
 
95
 
bool
96
 
unity::MT::GrabHandleGroup::needsAnimate()
97
 
{
98
 
  return mMoreAnimate;
99
 
}
100
 
 
101
 
void
102
 
unity::MT::GrabHandleGroup::relayout(const nux::Geometry& rect, bool hard)
103
 
{
104
 
  /* Each grab handle at each vertex, eg:
105
 
   *
106
 
   * 1 - topleft
107
 
   * 2 - top
108
 
   * 3 - topright
109
 
   * 4 - right
110
 
   * 5 - bottom-right
111
 
   * 6 - bottom
112
 
   * 7 - bottom-left
113
 
   * 8 - left
114
 
   */
115
 
 
116
 
  const float pos[9][2] =
117
 
  {
118
 
    {0.0f, 0.0f}, {0.5f, 0.0f}, {1.0f, 0.0f},
119
 
    {1.0f, 0.5f}, {1.0f, 1.0f},
120
 
    {0.5f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.5f},
121
 
    {0.5f, 0.5f} /* middle */
122
 
  };
123
 
 
124
 
  for (unsigned int i = 0; i < NUM_HANDLES; i++)
125
 
  {
126
 
    unity::MT::GrabHandle::Ptr & handle = mHandles.at(i);
127
 
 
128
 
    handle->reposition (rect.x + rect.width * pos[i][0] -
129
 
                        handle->width () / 2,
130
 
                        rect.y + rect.height * pos[i][1] -
131
 
                        handle->height () / 2,
132
 
                        unity::MT::PositionSet | (hard ? unity::MT::PositionLock : 0));
133
 
  }
134
 
}
135
 
 
136
 
unity::MT::GrabHandleGroup::GrabHandleGroup(GrabHandleWindow *owner,
137
 
                                            std::vector <unity::MT::TextureSize>  &textures) :
138
 
  mState(State::NONE),
139
 
  mOpacity(0.0f),
140
 
  mMoreAnimate(false),
141
 
  mOwner(owner)
142
 
{
143
 
}
144
 
 
145
 
unity::MT::GrabHandleGroup::Ptr
146
 
unity::MT::GrabHandleGroup::create (GrabHandleWindow *owner,
147
 
                                    std::vector<unity::MT::TextureSize> &textures)
148
 
{
149
 
    unity::MT::GrabHandleGroup::Ptr p = unity::MT::GrabHandleGroup::Ptr (new unity::MT::GrabHandleGroup (owner, textures));
150
 
    for (unsigned int i = 0; i < NUM_HANDLES; i++)
151
 
      p->mHandles.push_back(unity::MT::GrabHandle::create (textures.at(i).first,
152
 
                                                           textures.at(i).second.width,
153
 
                                                           textures.at(i).second.height,
154
 
                                                           p,
155
 
                                                           handlesMask.find (i)->second));
156
 
    return p;
157
 
}
158
 
 
159
 
unity::MT::GrabHandleGroup::~GrabHandleGroup()
160
 
{
161
 
  for (unity::MT::GrabHandle::Ptr & handle : mHandles)
162
 
    handle->damage (nux::Geometry (handle->x (),
163
 
                                   handle->y (),
164
 
                                   handle->width (),
165
 
                                   handle->height ()));
166
 
}
167
 
 
168
 
void
169
 
unity::MT::GrabHandleGroup::requestMovement (int x,
170
 
                                             int y,
171
 
                                             unsigned int direction,
172
 
                                             unsigned int button)
173
 
{
174
 
  mOwner->requestMovement (x, y, direction, button);
175
 
}
176
 
 
177
 
std::vector <unity::MT::TextureLayout>
178
 
unity::MT::GrabHandleGroup::layout(unsigned int handles)
179
 
{
180
 
  std::vector <unity::MT::TextureLayout> layout;
181
 
 
182
 
  for(const unity::MT::GrabHandle::Ptr & handle : mHandles)
183
 
    if (handle->id () & handles)
184
 
      layout.push_back (handle->layout ());
185
 
 
186
 
  return layout;
187
 
}
188
 
 
189
 
void
190
 
unity::MT::GrabHandleGroup::forEachHandle (const std::function <void (const unity::MT::GrabHandle::Ptr &)> &f)
191
 
{
192
 
  for (unity::MT::GrabHandle::Ptr &h : mHandles)
193
 
    f (h);
194
 
}