2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/public-api/image-loader/image.h>
20 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
21 #include <dali-toolkit/public-api/image-loader/image-url.h>
22 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
23 #include <dali/dali.h>
25 #include "shared/view.h"
28 using namespace Dali::Toolkit;
32 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
33 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
34 const char* APPLICATION_TITLE("Image view with encoded image buffer");
36 const char* IMAGE_PATH[] = {
37 DEMO_IMAGE_DIR "gallery-small-23.jpg",
38 DEMO_IMAGE_DIR "woodEffect.jpg",
39 DEMO_IMAGE_DIR "wood.png", // 32bits image
42 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
44 const unsigned int NUMBER_OF_IMAGES_ROW = 3;
45 const unsigned int NUMBER_OF_IMAGES_COLUMN = 2;
46 const unsigned int NUMBER_OF_IMAGES = NUMBER_OF_IMAGES_ROW * NUMBER_OF_IMAGES_COLUMN;
48 constexpr Vector2 IMAGE_VIEW_SIZE(200.0f, 200.0f);
50 const unsigned int TIMER_INTERVAL = 1000; // ms
52 EncodedImageBuffer ConvertFileToEncodedImageBuffer(const char* url)
54 EncodedImageBuffer buffer;
56 fp = fopen(url, "rb");
59 fseek(fp, 0, SEEK_END);
60 size_t size = ftell(fp);
61 Dali::Vector<uint8_t> data;
63 fseek(fp, 0, SEEK_SET);
64 size_t realSize = fread(data.Begin(), size, sizeof(uint8_t), fp);
66 data.Resize(realSize);
67 buffer = EncodedImageBuffer::New(data);
74 class ImageViewEncodedImageBufferApp : public ConnectionTracker
77 ImageViewEncodedImageBufferApp(Application& application)
78 : mApplication(application)
80 // Connect to the Application's Init signal
81 mApplication.InitSignal().Connect(this, &ImageViewEncodedImageBufferApp::Create);
84 ~ImageViewEncodedImageBufferApp()
89 void Create(Application& application)
91 // The Init signal is received once (only) during the Application lifetime
93 ImageUrl backgroundImageUrl = Image::GenerateUrl(ConvertFileToEncodedImageBuffer(BACKGROUND_IMAGE));
94 ImageUrl toolbarImageUrl = Image::GenerateUrl(ConvertFileToEncodedImageBuffer(TOOLBAR_IMAGE));
96 // Creates a default view with a default tool bar.
97 // The view is added to the window.
98 mContentLayer = DemoHelper::CreateView(application,
101 backgroundImageUrl.GetUrl(),
102 toolbarImageUrl.GetUrl(),
111 // Create automatic unparent and create ticker.
112 mTimer = Timer::New(TIMER_INTERVAL);
113 mTimer.TickSignal().Connect(this, &ImageViewEncodedImageBufferApp::OnTick);
116 application.GetWindow().GetRootLayer().TouchedSignal().Connect(this, &ImageViewEncodedImageBufferApp::OnTouch);
118 application.GetWindow().KeyEventSignal().Connect(this, &ImageViewEncodedImageBufferApp::OnKeyEvent);
123 void UpdateImageUrl()
125 mImageBuffer = ConvertFileToEncodedImageBuffer(IMAGE_PATH[mImageIndex]);
126 mImageUrl = Image::GenerateUrl(mImageBuffer);
128 void UpdateImageViews()
130 for(int i = 0; i < static_cast<int>(NUMBER_OF_IMAGES_ROW); i++)
132 for(int j = 0; j < static_cast<int>(NUMBER_OF_IMAGES_COLUMN); j++)
134 int viewId = i * NUMBER_OF_IMAGES_COLUMN + j;
135 // Remove old image and set null
136 if(mImageViews[viewId])
138 mImageViews[viewId].Unparent();
139 mImageViews[viewId] = ImageView(); // Set null image view
142 bool needToCreate = true;
144 // If current state don't wanna create current view, just skip
145 unsigned int currentViewState = 1u << ((i + j) & 1);
146 if(mState & currentViewState)
148 needToCreate = false;
153 mImageViews[viewId] = CreateImageView(i, j);
154 mContentLayer.Add(mImageViews[viewId]);
159 ImageView CreateImageView(int offset_y, int offset_x)
161 ImageView view = ImageView::New();
162 view.SetProperty(Actor::Property::SIZE, IMAGE_VIEW_SIZE);
163 view.SetProperty(Actor::Property::POSITION, Vector2(IMAGE_VIEW_SIZE.x * offset_x, IMAGE_VIEW_SIZE.y * offset_y + mToolBar.GetNaturalSize().y));
164 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
166 Property::Map imagePropertyMap;
167 imagePropertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
168 imagePropertyMap.Insert(ImageVisual::Property::URL, mImageUrl.GetUrl());
170 view.SetProperty(ImageView::Property::IMAGE, imagePropertyMap);
177 mState = (mState + 1) % 4;
179 return true; // Keep tick always
182 bool OnTouch(Actor actor, const TouchEvent& touch)
184 if(touch.GetState(0) == PointState::UP)
187 mImageIndex = (mImageIndex + 1) % NUMBER_OF_RESOURCES;
188 // Change resource's url
190 // Update image views
197 * Main key event handler
199 void OnKeyEvent(const KeyEvent& event)
201 if(event.GetState() == KeyEvent::DOWN)
203 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
205 if(mTimer.IsRunning())
208 mTimer.TickSignal().Disconnect(this, &ImageViewEncodedImageBufferApp::OnTick);
216 Application& mApplication;
218 Toolkit::Control mView; ///< The View instance.
219 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
220 Layer mContentLayer; ///< Content layer
222 Toolkit::ImageView mImageViews[NUMBER_OF_IMAGES];
224 unsigned int mImageIndex;
225 EncodedImageBuffer mImageBuffer;
228 // Automatic unparent and create ticker.
230 unsigned int mState; ///< 0 : draw all images, 1 : draw half, 2 : draw another half, 3 : don't draw images.
233 int DALI_EXPORT_API main(int argc, char** argv)
235 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
236 ImageViewEncodedImageBufferApp test(application);
237 application.MainLoop();