/* * Copyright (c) 2015 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 "shared/view.h" #include #include using namespace Dali; namespace { const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" ); const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" ); const char* APPLICATION_TITLE( "Image view" ); const char* IMAGE_PATH[] = { DEMO_IMAGE_DIR "blocks-ball.png", DEMO_IMAGE_DIR "gallery-small-23.jpg", DEMO_IMAGE_DIR "selection-popup-bg.2.9.png", DEMO_IMAGE_DIR "heartsframe.9.png", }; const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*); const unsigned int COLUMNS = 3; const unsigned int ROWS = 4; } // namespace class ImageViewController: public ConnectionTracker { public: ImageViewController( Application& application ) : mApplication( application ), mCurrentPositionToggle( 0, 0 ), mCurrentPositionImage( 0, 0 ), mToggleOff( true ), mImageIdx( 1 ) { // Connect to the Application's Init signal mApplication.InitSignal().Connect( this, &ImageViewController::Create ); } ~ImageViewController() { // Nothing to do here } void Create( Application& application ) { // The Init signal is received once (only) during the Application lifetime // Creates a default view with a default tool bar. // The view is added to the stage. mContentLayer = DemoHelper::CreateView( application, mView, mToolBar, BACKGROUND_IMAGE, TOOLBAR_IMAGE, APPLICATION_TITLE ); mTable = Toolkit::TableView::New( ROWS, COLUMNS ); mTable.SetAnchorPoint( AnchorPoint::CENTER ); mTable.SetParentOrigin( ParentOrigin::CENTER ); mTable.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS ); Vector3 offset( -50.0f, -350.0f, 0.0f ); mTable.SetSizeModeFactor( offset ); mContentLayer.Add( mTable ); for( unsigned int y = 0; y < ROWS; ++y ) { for( unsigned int x = 0; x < COLUMNS; ++x ) { mImageViews[x][y] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] ); mImageViews[x][y].SetParentOrigin( ParentOrigin::CENTER ); mImageViews[x][y].SetAnchorPoint( AnchorPoint::CENTER ); mImageViews[x][y].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); mTable.AddChild( mImageViews[x][y], Toolkit::TableView::CellPosition( y, x ) ); } } Toolkit::TableView buttonsTable = Toolkit::TableView::New( 3, 1 ); buttonsTable.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER ); buttonsTable.SetParentOrigin( ParentOrigin::BOTTOM_CENTER ); buttonsTable.SetFitHeight( 0 ); buttonsTable.SetFitHeight( 1 ); buttonsTable.SetFitHeight( 2 ); buttonsTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); Toolkit::PushButton button = Toolkit::PushButton::New(); button.SetLabelText( "Toggle on/off stage" ); button.SetParentOrigin( ParentOrigin::CENTER ); button.SetAnchorPoint( AnchorPoint::CENTER ); button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage ); button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); buttonsTable.AddChild( button, Toolkit::TableView::CellPosition( 0, 0 ) ); Toolkit::PushButton button2 = Toolkit::PushButton::New(); button2.SetLabelText( "Change Image" ); button2.SetParentOrigin( ParentOrigin::CENTER ); button2.SetAnchorPoint( AnchorPoint::CENTER ); button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked ); button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); buttonsTable.AddChild( button2, Toolkit::TableView::CellPosition( 1, 0 ) ); mContentLayer.Add(buttonsTable); Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent); } private: bool ToggleImageOnStage( Toolkit::Button button ) { Toolkit::ImageView imageView = mImageViews[ mCurrentPositionToggle.columnIndex ][ mCurrentPositionToggle.rowIndex ]; if( mToggleOff ) { imageView.Unparent(); } else { mTable.AddChild( imageView, mCurrentPositionToggle ); } ++mCurrentPositionToggle.columnIndex; if( mCurrentPositionToggle.columnIndex == COLUMNS ) { mCurrentPositionToggle.columnIndex = 0; ++mCurrentPositionToggle.rowIndex; } if( mCurrentPositionToggle.rowIndex == ROWS ) { mCurrentPositionToggle.rowIndex = 0; mToggleOff = !mToggleOff; } return true; } bool ChangeImageClicked( Toolkit::Button button ) { Toolkit::ImageView imageView = mImageViews[ mCurrentPositionImage.columnIndex ][ mCurrentPositionImage.rowIndex ]; imageView.SetImage( IMAGE_PATH[ mImageIdx ] ); ++mCurrentPositionImage.columnIndex; if( mCurrentPositionImage.columnIndex == COLUMNS ) { mCurrentPositionImage.columnIndex = 0; ++mCurrentPositionImage.rowIndex; } if( mCurrentPositionImage.rowIndex == ROWS ) { mCurrentPositionImage.rowIndex = 0; ++mImageIdx; if( mImageIdx == NUM_IMAGES ) { mImageIdx = 0; } } return true; } /** * Main key event handler */ void OnKeyEvent(const KeyEvent& event) { if(event.state == KeyEvent::Down) { if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) ) { mApplication.Quit(); } } } private: Application& mApplication; Toolkit::Control mView; ///< The View instance. Toolkit::ToolBar mToolBar; ///< The View's Toolbar. Layer mContentLayer; ///< Content layer Toolkit::TableView mTable; Toolkit::ImageView mImageViews[ COLUMNS ][ ROWS ]; Toolkit::TableView::CellPosition mCurrentPositionToggle; Toolkit::TableView::CellPosition mCurrentPositionImage; bool mToggleOff; int mImageIdx; }; void RunTest( Application& application ) { ImageViewController test( application ); application.MainLoop(); } // Entry point for Linux & Tizen applications // int DALI_EXPORT_API main( int argc, char **argv ) { Application application = Application::New( &argc, &argv, DEMO_THEME_PATH ); RunTest( application ); return 0; }