Supply stylesheet using Application constructor
[platform/core/uifw/dali-demo.git] / examples / atlas / atlas-example.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali/devel-api/images/atlas.h>
20
21 #include "shared/view.h"
22 #include <iostream>
23 #include <cstdio>
24
25 using namespace Dali;
26
27 class AtlasController;
28
29 namespace
30 {
31 const char * const BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
32 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
33 const char * const LOSE_CONTEXT_IMAGE( DALI_IMAGE_DIR "icon-cluster-wobble.png" );
34
35 Application gApplication;
36 AtlasController* gAtlasController(NULL);
37 }
38
39 class AtlasController : public ConnectionTracker
40 {
41 public:
42
43   AtlasController( Application& application )
44   : mApplication( application )
45   {
46     // Connect to the Application's Init signal
47     mApplication.InitSignal().Connect( this, &AtlasController::Create );
48   }
49
50   ~AtlasController()
51   {
52     // Nothing to do here;
53   }
54
55   void Create( Application& application )
56   {
57     // Get a handle to the stage
58     Stage stage = Stage::GetCurrent();
59     stage.SetBackgroundColor(Color::YELLOW);
60
61     // Respond to a click anywhere on the stage
62     stage.KeyEventSignal().Connect(this, &AtlasController::OnKeyEvent);
63
64     mApplication.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
65
66     mContentLayer = DemoHelper::CreateView( mApplication,
67         mView,
68         mToolBar,
69         BACKGROUND_IMAGE,
70         TOOLBAR_IMAGE,
71         "Atlas" );
72
73     mLoseContextButton = Toolkit::PushButton::New();
74     mLoseContextButton.SetBackgroundImage( ResourceImage::New( LOSE_CONTEXT_IMAGE ) );
75     mLoseContextButton.ClickedSignal().Connect( this, &AtlasController::OnLoseContextButtonClicked );
76     mToolBar.AddControl( mLoseContextButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
77
78     mAtlas = Atlas::New( 400,300, Pixel::RGBA8888);
79     mAtlas.Clear(Vector4(0.f,0.5f,0.5f,0.5f));
80     mAtlas.Upload( DALI_IMAGE_DIR "icon-change.png", 50, 30 );
81     mAtlas.Upload( DALI_IMAGE_DIR "icon-cluster-carousel.png", 100, 30 );
82     mAtlas.Upload( DALI_IMAGE_DIR "icon-effects-on.png", 150, 30 );
83     mAtlas.Upload( DALI_IMAGE_DIR "icon-effect-cross.png", 100, 80 );
84     mAtlas.Upload( DALI_IMAGE_DIR "icon-effect-fold.png", 150, 80 );
85     mAtlas.Upload( DALI_IMAGE_DIR "icon-effect-wave.png", 200, 80 );
86     mAtlas.Upload( DALI_IMAGE_DIR "icon-item-view-layout-depth.png", 150, 130 );
87     mAtlas.Upload( DALI_IMAGE_DIR "icon-item-view-layout-grid.png", 200, 130 );
88     mAtlas.Upload( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png", 250, 130 );
89     UploadBufferImages();
90
91     ImageActor imageActor1 = ImageActor::New( mAtlas );
92     imageActor1.SetY(-170.f);
93     imageActor1.SetParentOrigin(ParentOrigin::CENTER);
94     mContentLayer.Add( imageActor1 );
95
96     Atlas atlas2 = Atlas::New( 400,400, Pixel::RGB888);
97     atlas2.Clear( Color::RED );
98     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-1.jpg", 4, 4 );
99     atlas2.Clear( Color::BLUE );
100     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-2.jpg", 136, 4 );
101     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-3.jpg", 268, 4 );
102     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-4.jpg", 4, 136 );
103     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-5.jpg", 136, 136 );
104     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-6.jpg", 268, 135 );
105     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-7.jpg", 4, 268 );
106     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-7.jpg", 136, 268 );
107     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-7.jpg", 268, 268 );
108
109
110     ImageActor imageActor2 = ImageActor::New( atlas2 );
111     imageActor2.SetY(200.f);
112     imageActor2.SetZ(-1.f);
113     imageActor2.SetParentOrigin(ParentOrigin::CENTER);
114     mContentLayer.Add( imageActor2 );
115
116     mPanGestureDetector = PanGestureDetector::New();
117     mPanGestureDetector.DetectedSignal().Connect(this, &AtlasController::OnPanGesture);
118     mPanGestureDetector.Attach(imageActor1);
119     mPanGestureDetector.Attach(imageActor2);
120
121     stage.ContextLostSignal().Connect(this, &AtlasController::OnContextLost);
122     stage.ContextRegainedSignal().Connect(this, &AtlasController::OnContextRegained);
123   }
124
125   void UploadBufferImages()
126   {
127     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 1.f, 0.5f  ), 80, 90 ), 0, 210 );
128     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.75f, 0.5f  ), 80, 80 ), 40, 210 );
129     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.5f, 0.5f  ), 80, 70 ), 80, 210 );
130     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.25f, 0.5f  ), 80, 60 ), 120, 210 );
131     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.f, 0.5f  ), 80, 50 ), 160, 210 );
132     mAtlas.Upload( CreateBufferImage( Vector4(0.75f, 0.75f, 0.f, 0.5f  ), 80, 40 ), 200, 210 );
133     mAtlas.Upload( CreateBufferImage( Vector4(0.5f, 0.5f, 0.f, 0.5f  ), 80, 30 ), 240, 210 );
134     mAtlas.Upload( CreateBufferImage( Vector4(0.25f, 0.25f, 0.f, 0.5f  ), 80, 20 ), 280, 210 );
135     mAtlas.Upload( CreateBufferImage( Vector4(0.1f, 0.1f, 0.f, 0.5f  ), 80, 10 ), 320, 210 );
136     BufferImage redBlock = CreateBufferImage( Color::RED, 40, 40 );
137     mAtlas.Upload(redBlock, 320, 30);
138     mAtlas.Upload(redBlock, 320, 80);
139     mAtlas.Upload(redBlock, 320, 130);
140   }
141
142   static void NewWindow(void)
143   {
144     PositionSize posSize(0, 0, 720, 1280);
145     gApplication.ReplaceWindow(posSize, "NewWindow"); // Generates a new window
146   }
147
148   bool OnLoseContextButtonClicked( Toolkit::Button button )
149   {
150     // Add as an idle callback to avoid ProcessEvents being recursively called.
151     mApplication.AddIdle(MakeCallback( AtlasController::NewWindow ));
152     return true;
153   }
154
155   void OnKeyEvent( const KeyEvent& event )
156   {
157     if(event.state == KeyEvent::Down)
158     {
159       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
160       {
161         mApplication.Quit();
162       }
163     }
164   }
165
166   void OnPanGesture( Actor actor, const PanGesture& gesture )
167   {
168     if( gesture.state == Gesture::Continuing )
169     {
170       actor.TranslateBy( Vector3( gesture.displacement ) );
171     }
172   }
173
174   void OnContextLost()
175   {
176     printf("Stage reporting context loss\n");
177   }
178
179   void OnContextRegained()
180   {
181     printf("Stage reporting context regain\n");
182     UploadBufferImages();
183   }
184
185 private:
186
187   BufferImage CreateBufferImage( const Vector4& color, const unsigned int width, const unsigned int height )
188   {
189     BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
190
191     // Create the image
192     PixelBuffer* pixbuf = imageData.GetBuffer();
193     const unsigned int bitmapSize = width * height;
194     for( size_t i = 0; i < bitmapSize; ++i )
195     {
196       pixbuf[i*4+0] = 0xFF * color.r;
197       pixbuf[i*4+1] = 0xFF * color.g;
198       pixbuf[i*4+2] = 0xFF * color.b;
199       pixbuf[i*4+3] = 0xFF * color.a;
200     }
201
202     imageData.Update();
203
204     return imageData;
205   }
206
207
208 private:
209   Application&  mApplication;
210   PanGestureDetector mPanGestureDetector;
211
212   Toolkit::Control           mView;                              ///< The View instance.
213   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
214   Layer                      mContentLayer;                      ///< Content layer (scrolling cluster content)
215   Toolkit::PushButton        mLoseContextButton;
216   Atlas                      mAtlas;
217 };
218
219 void RunTest( Application& application )
220 {
221   gAtlasController = new AtlasController(application);
222   application.MainLoop(Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS);
223 }
224
225 // Entry point for Linux & Tizen applications
226 //
227 int main( int argc, char **argv )
228 {
229   gApplication = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
230
231   RunTest( gApplication );
232
233   return 0;
234 }