Back to index

unity  6.0.0
LauncherDragWindow.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2010 Canonical Ltd
00004 *
00005 * This program is free software: you can redistribute it and/or modify
00006 * it under the terms of the GNU General Public License version 3 as
00007 * published by the Free Software Foundation.
00008 *
00009 * This program is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 * GNU General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU General Public License
00015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 *
00017 * Authored by: Jason Smith <jason.smith@canonical.com>
00018 */
00019 
00020 #include <Nux/Nux.h>
00021 #include <Nux/BaseWindow.h>
00022 #include <NuxGraphics/GraphicsEngine.h>
00023 #include <Nux/TextureArea.h>
00024 
00025 #include "LauncherDragWindow.h"
00026 
00027 namespace unity
00028 {
00029 namespace launcher
00030 {
00031 
00032 NUX_IMPLEMENT_OBJECT_TYPE(LauncherDragWindow);
00033 
00034 LauncherDragWindow::LauncherDragWindow(nux::ObjectPtr<nux::IOpenGLBaseTexture> icon)
00035   : nux::BaseWindow("")
00036   , _icon(icon)
00037 {
00038   SetBaseSize(_icon->GetWidth(), _icon->GetHeight());
00039 }
00040 
00041 LauncherDragWindow::~LauncherDragWindow()
00042 {
00043   if (on_anim_completed.connected())
00044     on_anim_completed.disconnect();
00045 }
00046 
00047 bool LauncherDragWindow::Animating()
00048 {
00049   return bool(animation_timer_);
00050 }
00051 
00052 void LauncherDragWindow::SetAnimationTarget(int x, int y)
00053 {
00054   _animation_target = nux::Point2(x, y);
00055 }
00056 
00057 void LauncherDragWindow::StartAnimation()
00058 {
00059   if (animation_timer_)
00060     return;
00061 
00062   animation_timer_.reset(new glib::Timeout(15));
00063   animation_timer_->Run(sigc::mem_fun(this, &LauncherDragWindow::OnAnimationTimeout));
00064 }
00065 
00066 bool LauncherDragWindow::OnAnimationTimeout()
00067 {
00068   nux::Geometry const& geo = GetGeometry();
00069 
00070   int half_size = geo.width / 2;
00071 
00072   int target_x = static_cast<int>(_animation_target.x) - half_size;
00073   int target_y = static_cast<int>(_animation_target.y) - half_size;
00074 
00075   int x_delta = static_cast<int>(static_cast<float>(target_x - geo.x) * .3f);
00076   if (std::abs(x_delta) < 5)
00077     x_delta = (x_delta >= 0) ? std::min(5, target_x - geo.x) : std::max(-5, target_x - geo.x);
00078 
00079   int y_delta = static_cast<int>(static_cast<float>(target_y - geo.y) * .3f);
00080   if (std::abs(y_delta) < 5)
00081     y_delta = (y_delta >= 0) ? std::min(5, target_y - geo.y) : std::max(-5, target_y - geo.y);
00082 
00083   SetBaseXY(geo.x + x_delta, geo.y + y_delta);
00084 
00085   nux::Geometry const& new_geo = GetGeometry();
00086 
00087   if (new_geo.x == target_x && new_geo.y == target_y)
00088   {
00089     anim_completed.emit();
00090     animation_timer_.reset();
00091 
00092     return false;
00093   }
00094 
00095   return true;
00096 }
00097 
00098 void
00099 LauncherDragWindow::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
00100 {
00101   nux::Geometry geo = GetGeometry();
00102   geo.SetX(0);
00103   geo.SetY(0);
00104 
00105   GfxContext.PushClippingRectangle(geo);
00106 
00107   nux::TexCoordXForm texxform;
00108   texxform.FlipVCoord(true);
00109 
00110   GfxContext.QRP_1Tex(0,
00111                       0,
00112                       _icon->GetWidth(),
00113                       _icon->GetHeight(),
00114                       _icon,
00115                       texxform,
00116                       nux::color::White);
00117 
00118   GfxContext.PopClippingRectangle();
00119 }
00120 
00121 } // namespace launcher
00122 } // namespace unity