view.h 6.9 KB
#ifndef __DALI_DEMO_HELPER_VIEW_H__
#define __DALI_DEMO_HELPER_VIEW_H__

/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <dali-toolkit/dali-toolkit.h>

namespace DemoHelper
{

/**
 * Provide a style for the view and its tool bar.
 */
struct ViewStyle
{
  ViewStyle( float toolBarButtonPercentage, float toolBarTitlePercentage, float dpi, float toolBarHeight, float toolBarPadding )
  : mToolBarButtonPercentage( toolBarButtonPercentage ),
    mToolBarTitlePercentage( toolBarTitlePercentage ),
    mDpi( dpi ),
    mToolBarHeight( toolBarHeight ),
    mToolBarPadding( toolBarPadding )
  {}

  float mToolBarButtonPercentage; ///< The tool bar button width is a percentage of the tool bar width.
  float mToolBarTitlePercentage;  ///< The tool bar title width is a percentage of the tool bar width.
  float mDpi;                     ///< This style is indented for the given dpi.
  float mToolBarHeight;           ///< The tool bar height for the given dpi above.
  float mToolBarPadding;          ///< The tool bar padding between controls for the given dpi above.
};

const ViewStyle DEFAULT_VIEW_STYLE( 0.1f, 0.7f, 315.f, 80.f, 4.f );

const char*                   DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
const char*                   DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
const Dali::PointSize         DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::EXTRALIGHT);
const Dali::Vector4           DEFAULT_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);

const Dali::Toolkit::Alignment::Padding DEFAULT_PLAY_PADDING(12.0f, 12.0f, 12.0f, 12.0f);
const Dali::Toolkit::Alignment::Padding DEFAULT_MODE_SWITCH_PADDING(8.0f, 8.0f, 8.0f, 8.0f);

static Dali::TextStyle defaultTextStyle;
static bool textStyleSet=false;

Dali::TextStyle& GetDefaultTextStyle()
{
  if(!textStyleSet)
  {
    defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
    defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
    defaultTextStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
    defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
    defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
    textStyleSet = true;
  }

  return defaultTextStyle;
}

Dali::Layer CreateView( Dali::Application& application,
                        Dali::Toolkit::View& view,
                        Dali::Toolkit::ToolBar& toolBar,
                        const std::string& backgroundImagePath,
                        const std::string& toolbarImagePath,
                        const std::string& title,
                        const ViewStyle& style,
                        const Dali::TextStyle& textStyle )
{
  Dali::Stage stage = Dali::Stage::GetCurrent();

  // Create default View.
  view = Dali::Toolkit::View::New();

  // Add the view to the stage before setting the background.
  stage.Add( view );

  // Set background image.
  if ( ! backgroundImagePath.empty() )
  {
    Dali::Image backgroundImage = Dali::Image::New( backgroundImagePath );
    Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( backgroundImage );
    view.SetBackground( backgroundImageActor );
  }

  // FIXME
  // Connects the orientation signal with the View::OrientationChanged method.
  //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );

  // Create default ToolBar

  Dali::Vector2 dpi = stage.GetDpi();

  // Create toolbar layer.
  Dali::Layer toolBarLayer = Dali::Layer::New();
  toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
  toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
  toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::SourceWidthFixedHeight( style.mToolBarHeight * dpi.y / style.mDpi ) ) );
  toolBarLayer.SetSize( 0.0f, style.mToolBarHeight * dpi.y / style.mDpi );

  // Add tool bar layer to the view.
  view.AddContentLayer( toolBarLayer );

  // Raise tool bar layer to the top.
  toolBarLayer.RaiseToTop();

  // Tool bar
  Dali::Image image = Dali::Image::New( toolbarImagePath );
  Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
  toolBar = Dali::Toolkit::ToolBar::New();
  toolBar.SetBackground( toolBarBackground );
  toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
  toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
  toolBar.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
  toolBar.SetSize( 0.0f, style.mToolBarHeight * dpi.y / style.mDpi );
  toolBarBackground.SetSortModifier(1.0f);

  // Add the tool bar to the too bar layer.
  toolBarLayer.Add( toolBar );

  Dali::Font font = Dali::Font::New();

  // Tool bar text.
  if( !title.empty() )
  {
    Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
    titleActor.SetName( "ToolbarTitle" );
    titleActor.SetText( title );
    titleActor.SetSize( font.MeasureText( title ) );
    titleActor.SetStyleToCurrentText(textStyle);

    // Add title to the tool bar.
    const float padding( style.mToolBarPadding * dpi.x / style.mDpi );
    toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter, Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
  }

  // Create a content layer.
  Dali::Layer contentLayer = Dali::Layer::New();
  contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
  contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
  contentLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
  view.AddContentLayer( contentLayer );
  contentLayer.LowerBelow( toolBarLayer );

  return contentLayer;
}

Dali::Layer CreateView( Dali::Application& application,
                        Dali::Toolkit::View& view,
                        Dali::Toolkit::ToolBar& toolBar,
                        const std::string& backgroundImagePath,
                        const std::string& toolbarImagePath,
                        const std::string& title,
                        const ViewStyle& style  = DEFAULT_VIEW_STYLE )
{
  return CreateView( application, view, toolBar, backgroundImagePath, toolbarImagePath, title, style,
                     GetDefaultTextStyle() );
}


} // DemoHelper

#endif // __DALI_DEMO_HELPER_VIEW_H__