Back to index

unity  6.0.0
CairoBaseWindow.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2012 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: Marco Trevisan <3v1n0@ubuntu.com>
00018  */
00019 
00020 
00021 #include <Nux/Nux.h>
00022 
00023 #include "unity-shared/CairoTexture.h"
00024 #include "CairoBaseWindow.h"
00025 
00026 namespace unity
00027 {
00028 namespace
00029 {
00030   const int ANCHOR_WIDTH = 14;
00031   const int ANCHOR_HEIGHT = 18;
00032   const int CORNER_RADIUS = 4;
00033   const int PADDING = 15;
00034   const int TEXT_PADDING = 8;
00035   const int MINIMUM_TEXT_WIDTH = 100;
00036   const int TOP_SIZE = 0;
00037 }
00038 
00039 NUX_IMPLEMENT_OBJECT_TYPE(CairoBaseWindow);
00040 
00041 CairoBaseWindow::CairoBaseWindow() :
00042   _use_blurred_background(false),
00043   _compute_blur_bkg(false)
00044 {
00045   SetWindowSizeMatchLayout(true);
00046 }
00047 
00048 CairoBaseWindow::~CairoBaseWindow()
00049 {
00050   // nothing to do
00051 }
00052 
00053 void CairoBaseWindow::Draw(nux::GraphicsEngine& gfxContext, bool forceDraw)
00054 {
00055   nux::Geometry base(GetGeometry());
00056 
00057   // Get the background and apply some blur
00058   if (_use_blurred_background && _compute_blur_bkg)
00059   {
00060     auto current_fbo = nux::GetGraphicsDisplay()->GetGpuDevice()->GetCurrentFrameBufferObject();
00061     nux::GetGraphicsDisplay()->GetGpuDevice()->DeactivateFrameBuffer();
00062 
00063     gfxContext.SetViewport(0, 0, gfxContext.GetWindowWidth(), gfxContext.GetWindowHeight());
00064     gfxContext.SetScissor(0, 0, gfxContext.GetWindowWidth(), gfxContext.GetWindowHeight());
00065     gfxContext.GetRenderStates().EnableScissor(false);
00066 
00067     nux::ObjectPtr <nux::IOpenGLBaseTexture> bkg_texture = gfxContext.CreateTextureFromBackBuffer(base.x, base.y, base.width, base.height);
00068 
00069     nux::TexCoordXForm texxform_bkg;
00070     bg_blur_texture_ = gfxContext.QRP_GetBlurTexture(0, 0, base.width, base.height, bkg_texture, texxform_bkg, nux::color::White, 1.0f, 3);
00071 
00072     if (current_fbo.IsValid())
00073     {
00074       current_fbo->Activate(true);
00075       gfxContext.Push2DWindow(current_fbo->GetWidth(), current_fbo->GetHeight());
00076     }
00077     else
00078     {
00079       gfxContext.SetViewport(0, 0, gfxContext.GetWindowWidth(), gfxContext.GetWindowHeight());
00080       gfxContext.Push2DWindow(gfxContext.GetWindowWidth(), gfxContext.GetWindowHeight());
00081       gfxContext.ApplyClippingRectangle();
00082     }
00083     _compute_blur_bkg = false;
00084   }
00085 
00086   // the elements position inside the window are referenced to top-left window
00087   // corner. So bring base to (0, 0).
00088   base.SetX(0);
00089   base.SetY(0);
00090 
00091   gfxContext.PushClippingRectangle(base);
00092 
00093   /* "Clear" out the background */
00094   nux::ROPConfig rop;
00095   rop.Blend = true;
00096   rop.SrcBlend = GL_ONE;
00097   rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;
00098 
00099   nux::ColorLayer layer(nux::Color(0x00000000), true, rop);
00100   nux::GetPainter().PushDrawLayer(gfxContext, base, &layer);
00101 
00102   nux::TexCoordXForm texxform_bg;
00103   texxform_bg.SetWrap(nux::TEXWRAP_CLAMP, nux::TEXWRAP_CLAMP);
00104   texxform_bg.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00105 
00106   nux::TexCoordXForm texxform_mask;
00107   texxform_mask.SetWrap(nux::TEXWRAP_CLAMP, nux::TEXWRAP_CLAMP);
00108   texxform_mask.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00109 
00110   if (bg_blur_texture_.IsValid() && texture_mask_.IsValid())
00111   {
00112     nux::TexCoordXForm texxform_blur_bkg;
00113 
00114     nux::GetWindowThread()->GetGraphicsEngine().GetRenderStates().SetBlend(true);
00115     nux::GetWindowThread()->GetGraphicsEngine().GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00116 
00117     gfxContext.QRP_2TexMod(
00118       base.x,
00119       base.y,
00120       base.width,
00121       base.height,
00122       bg_blur_texture_,
00123       texxform_blur_bkg,
00124       nux::color::White,
00125       texture_mask_->GetDeviceTexture(),
00126       texxform_mask,
00127       nux::color::White);
00128   }
00129 
00130   if (texture_bg_.IsValid() && texture_mask_.IsValid())
00131   {
00132     nux::GetWindowThread()->GetGraphicsEngine().GetRenderStates().SetBlend(true);
00133     nux::GetWindowThread()->GetGraphicsEngine().GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00134 
00135     gfxContext.QRP_2TexMod(base.x,
00136                            base.y,
00137                            base.width,
00138                            base.height,
00139                            texture_bg_->GetDeviceTexture(),
00140                            texxform_bg,
00141                            nux::color::White,
00142                            texture_mask_->GetDeviceTexture(),
00143                            texxform_mask,
00144                            nux::color::White);
00145   }
00146 
00147   if (texture_outline_.IsValid())
00148   {
00149     nux::TexCoordXForm texxform;
00150     texxform.SetWrap(nux::TEXWRAP_CLAMP, nux::TEXWRAP_CLAMP);
00151     texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00152 
00153     nux::GetWindowThread()->GetGraphicsDisplay().GetGraphicsEngine()->GetRenderStates().SetBlend(true);
00154     nux::GetWindowThread()->GetGraphicsDisplay().GetGraphicsEngine()->GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00155     
00156     gfxContext.QRP_1Tex(base.x,
00157                         base.y,
00158                         base.width,
00159                         base.height,
00160                         texture_outline_->GetDeviceTexture(),
00161                         texxform,
00162                         nux::color::White);
00163   }
00164 
00165   nux::GetWindowThread()->GetGraphicsDisplay().GetGraphicsEngine()->GetRenderStates().SetBlend(false);
00166   nux::GetPainter().PopBackground();
00167   gfxContext.PopClippingRectangle();
00168 }
00169 
00170 } // namespace nux