X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Frefraction-effect%2Frefraction-effect-example.cpp;h=b6eebe397c92b91cea26682c59fee6b6be67735a;hb=1a473d5189ca7e7d55aca3a64a8a4ff2dc3b6c67;hp=b1983d25b15f2802f0490a20bcbbaee62ff4fc48;hpb=46e6a0e92784c860b2b5425e34941c6574dd8062;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/refraction-effect/refraction-effect-example.cpp b/examples/refraction-effect/refraction-effect-example.cpp index b1983d2..b6eebe3 100644 --- a/examples/refraction-effect/refraction-effect-example.cpp +++ b/examples/refraction-effect/refraction-effect-example.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 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. @@ -17,15 +17,17 @@ // EXTERNAL INCLUDES #include -#include #include +#include +#include -#include #include #include +#include // INTERNAL INCLUDES #include "shared/view.h" +#include "shared/utility.h" using namespace Dali; @@ -74,19 +76,6 @@ struct LightOffsetConstraint }; /** - * @brief Load an image, scaled-down to no more than the stage dimensions. - * - * Uses image scaling mode SCALE_TO_FILL to resize the image at - * load time to cover the entire stage with pixels with no borders, - * and filter mode BOX_THEN_LINEAR to sample the image with maximum quality. - */ -ResourceImage LoadStageFillingImage( const char * const imagePath ) -{ - Size stageSize = Stage::GetCurrent().GetSize(); - return ResourceImage::New( imagePath, ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR ); -} - -/** * structure of the vertex in the mesh */ struct Vertex @@ -223,6 +212,21 @@ class RefractionEffectExample : public ConnectionTracker public: RefractionEffectExample( Application &application ) : mApplication( application ), + mContent(), + mTextureSet(), + mGeometry(), + mRenderer(), + mMeshActor(), + mShaderFlat(), + mShaderRefraction(), + mLightAnimation(), + mStrenghAnimation(), + mLightXYOffsetIndex( Property::INVALID_INDEX ), + mSpinAngleIndex( Property::INVALID_INDEX ), + mLightIntensityIndex( Property::INVALID_INDEX ), + mEffectStrengthIndex( Property::INVALID_INDEX ), + mChangeTextureButton(), + mChangeMeshButton(), mCurrentTextureId( 1 ), mCurrentMeshId( 0 ) { @@ -239,13 +243,13 @@ private: // The Init signal is received once (only) during the Application lifetime void Create(Application& application) { - Stage stage = Stage::GetCurrent(); - Vector2 stageSize = stage.GetSize(); + Window window = application.GetWindow(); + Vector2 windowSize = window.GetSize(); - stage.KeyEventSignal().Connect(this, &RefractionEffectExample::OnKeyEvent); + window.KeyEventSignal().Connect(this, &RefractionEffectExample::OnKeyEvent); // Creates a default view with a default tool bar. - // The view is added to the stage. + // The view is added to the window. Toolkit::ToolBar toolBar; Toolkit::Control view; mContent = DemoHelper::CreateView( application, @@ -257,21 +261,21 @@ private: // Add a button to change background. (right of toolbar) mChangeTextureButton = Toolkit::PushButton::New(); - mChangeTextureButton.SetUnselectedImage( CHANGE_TEXTURE_ICON ); - mChangeTextureButton.SetSelectedImage( CHANGE_TEXTURE_ICON_SELECTED ); + mChangeTextureButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_TEXTURE_ICON ); + mChangeTextureButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_TEXTURE_ICON_SELECTED ); mChangeTextureButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeTexture ); toolBar.AddControl( mChangeTextureButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, - Toolkit::Alignment::HorizontalRight, + Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING ); // Add a button to change mesh pattern. ( left of bar ) mChangeMeshButton = Toolkit::PushButton::New(); - mChangeMeshButton.SetUnselectedImage( CHANGE_MESH_ICON ); - mChangeMeshButton.SetSelectedImage( CHANGE_MESH_ICON_SELECTED ); + mChangeMeshButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_MESH_ICON ); + mChangeMeshButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_MESH_ICON_SELECTED ); mChangeMeshButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeMesh ); toolBar.AddControl( mChangeMeshButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, - Toolkit::Alignment::HorizontalLeft, + Toolkit::Alignment::HORIZONTAL_LEFT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING ); @@ -280,16 +284,17 @@ private: mShaderFlat = Shader::New( VERTEX_SHADER_FLAT, FRAGMENT_SHADER_FLAT ); mGeometry = CreateGeometry( MESH_FILES[mCurrentMeshId] ); - Image texture = LoadStageFillingImage( TEXTURE_IMAGES[mCurrentTextureId] ); - mMaterial = Material::New( mShaderFlat ); - mMaterial.AddTexture( texture, "sTexture" ); + Texture texture = DemoHelper::LoadWindowFillingTexture( window.GetSize(), TEXTURE_IMAGES[mCurrentTextureId] ); + mTextureSet = TextureSet::New(); + mTextureSet.SetTexture( 0u, texture ); - mRenderer = Renderer::New( mGeometry, mMaterial ); + mRenderer = Renderer::New( mGeometry, mShaderFlat ); + mRenderer.SetTextures( mTextureSet ); mMeshActor = Actor::New(); mMeshActor.AddRenderer( mRenderer ); - mMeshActor.SetSize( stageSize ); - mMeshActor.SetParentOrigin(ParentOrigin::CENTER); + mMeshActor.SetProperty( Actor::Property::SIZE, windowSize ); + mMeshActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER); mContent.Add( mMeshActor ); // Connect the callback to the touch signal on the mesh actor @@ -305,13 +310,13 @@ private: mEffectStrengthIndex = mMeshActor.RegisterProperty( "uEffectStrength", 0.f ); - Vector3 lightPosition( -stageSize.x*0.5f, -stageSize.y*0.5f, stageSize.x*0.5f ); // top_left + Vector3 lightPosition( -windowSize.x*0.5f, -windowSize.y*0.5f, windowSize.x*0.5f ); // top_left mMeshActor.RegisterProperty( "uLightPosition", lightPosition ); Property::Index lightSpinOffsetIndex = mMeshActor.RegisterProperty( "uLightSpinOffset", Vector2::ZERO ); mSpinAngleIndex = mMeshActor.RegisterProperty("uSpinAngle", 0.f ); - Constraint constraint = Constraint::New( mMeshActor, lightSpinOffsetIndex, LightOffsetConstraint(stageSize.x*0.1f) ); + Constraint constraint = Constraint::New( mMeshActor, lightSpinOffsetIndex, LightOffsetConstraint(windowSize.x*0.1f) ); constraint.AddSource( LocalSource(mSpinAngleIndex) ); constraint.Apply(); @@ -342,21 +347,20 @@ private: bool OnChangeTexture( Toolkit::Button button ) { mCurrentTextureId = ( mCurrentTextureId + 1 ) % NUM_TEXTURE_IMAGES; - Image texture = LoadStageFillingImage( TEXTURE_IMAGES[mCurrentTextureId] ); - mMaterial.SetTextureImage( 0, texture ); + Texture texture = DemoHelper::LoadWindowFillingTexture( mApplication.GetWindow().GetSize(), TEXTURE_IMAGES[mCurrentTextureId] ); + mTextureSet.SetTexture( 0u, texture ); return true; } - bool OnTouch( Actor actor , const TouchEvent& event ) + bool OnTouch( Actor actor, const TouchEvent& event ) { - const TouchPoint &point = event.GetPoint(0); - switch(point.state) + switch( event.GetState( 0 ) ) { - case TouchPoint::Down: + case PointState::DOWN: { - mMaterial.SetShader( mShaderRefraction ); + mRenderer.SetShader( mShaderRefraction ); - SetLightXYOffset( point.screen ); + SetLightXYOffset( event.GetScreenPosition( 0 ) ); mLightAnimation.Play(); @@ -371,15 +375,15 @@ private: break; } - case TouchPoint::Motion: + case PointState::MOTION: { // make the light position following the finger movement - SetLightXYOffset( point.screen ); + SetLightXYOffset( event.GetScreenPosition( 0 ) ); break; } - case TouchPoint::Up: - case TouchPoint::Leave: - case TouchPoint::Interrupted: + case PointState::UP: + case PointState::LEAVE: + case PointState::INTERRUPTED: { mLightAnimation.Pause(); @@ -393,9 +397,7 @@ private: mStrenghAnimation.Play(); break; } - case TouchPoint::Stationary: - case TouchPoint::Last: - default: + case PointState::STATIONARY: { break; } @@ -406,7 +408,7 @@ private: void OnTouchFinished( Animation& source ) { - mMaterial.SetShader( mShaderFlat ); + mRenderer.SetShader( mShaderFlat ); SetLightXYOffset( Vector2::ZERO ); } @@ -456,8 +458,8 @@ private: vertexFormat["aPosition"] = Property::VECTOR3; vertexFormat["aNormal"] = Property::VECTOR3; vertexFormat["aTexCoord"] = Property::VECTOR2; - PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat, vertices.size() ); - surfaceVertices.SetData( &vertices[0] ); + VertexBuffer surfaceVertices = VertexBuffer::New( vertexFormat ); + surfaceVertices.SetData( &vertices[0], vertices.size() ); Geometry surface = Geometry::New(); surface.AddVertexBuffer( surfaceVertices ); @@ -470,14 +472,24 @@ private: std::vector& vertexPositions, Vector& faceIndices) { - std::ifstream ifs( objFileName.c_str(), std::ios::in ); + std::streampos bufferSize = 0; + Dali::Vector fileBuffer; + if( !Dali::FileLoader::ReadFile( objFileName, bufferSize, fileBuffer, Dali::FileLoader::FileType::TEXT ) ) + { + DALI_LOG_WARNING( "file open failed for: \"%s\"", objFileName.c_str() ); + return; + } + + fileBuffer.PushBack( '\0' ); + + std::stringstream iss( &fileBuffer[0], std::ios::in ); boundingBox.Resize( 6 ); boundingBox[0]=boundingBox[2]=boundingBox[4] = std::numeric_limits::max(); boundingBox[1]=boundingBox[3]=boundingBox[5] = -std::numeric_limits::max(); std::string line; - while( std::getline( ifs, line ) ) + while( std::getline( iss, line ) ) { if( line[0] == 'v' && std::isspace(line[1])) // vertex { @@ -508,7 +520,8 @@ private: } std::istringstream iss(line.substr(2), std::istringstream::in); - unsigned int indices[ numOfInt ]; + Dali::Vector indices; + indices.Resize(numOfInt); unsigned int i=0; while( iss >> indices[i++] && i < numOfInt); unsigned int step = (i+1) / 3; @@ -517,8 +530,6 @@ private: faceIndices.PushBack( indices[2*step]-1 ); } } - - ifs.close(); } void ShapeResizeAndTexureCoordinateCalculation( const Vector& boundingBox, @@ -528,8 +539,8 @@ private: Vector3 bBoxSize( boundingBox[1] - boundingBox[0], boundingBox[3] - boundingBox[2], boundingBox[5] - boundingBox[4]); Vector3 bBoxMinCorner( boundingBox[0], boundingBox[2], boundingBox[4] ); - Vector2 stageSize = Stage::GetCurrent().GetSize(); - Vector3 scale( stageSize.x / bBoxSize.x, stageSize.y / bBoxSize.y, 1.f ); + Vector2 windowSize = mApplication.GetWindow().GetSize(); + Vector3 scale( windowSize.x / bBoxSize.x, windowSize.y / bBoxSize.y, 1.f ); scale.z = (scale.x + scale.y)/2.f; textureCoordinates.reserve(vertexPositions.size()); @@ -550,7 +561,7 @@ private: */ void OnKeyEvent(const KeyEvent& event) { - if(event.state == KeyEvent::Down) + if(event.GetState() == KeyEvent::DOWN) { if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) ) { @@ -563,7 +574,7 @@ private: Application& mApplication; Layer mContent; - Material mMaterial; + TextureSet mTextureSet; Geometry mGeometry; Renderer mRenderer; Actor mMeshActor; @@ -587,21 +598,10 @@ private: /*****************************************************************************/ -static void -RunTest(Application& app) +int DALI_EXPORT_API main(int argc, char **argv) { + Application app = Application::New(&argc, &argv, DEMO_THEME_PATH); RefractionEffectExample theApp(app); app.MainLoop(); -} - -/*****************************************************************************/ - -int -main(int argc, char **argv) -{ - Application app = Application::New(&argc, &argv, DEMO_THEME_PATH); - - RunTest(app); - return 0; }