├── .gitignore
├── LICENSE
├── README.md
├── StarWars.iml
├── build.gradle
├── demo
├── .gitignore
├── build.gradle
├── demo.iml
├── fabric.properties
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── yalantis
│ │ └── starwarsdemo
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── yalantis
│ │ │ └── starwarsdemo
│ │ │ ├── App.java
│ │ │ ├── adapter
│ │ │ └── ProfileAdapter.java
│ │ │ ├── interfaces
│ │ │ ├── DemoActivityInterface.java
│ │ │ ├── GreetingFragmentInterface.java
│ │ │ ├── ProfileAdapterListener.java
│ │ │ └── TilesRendererInterface.java
│ │ │ ├── model
│ │ │ └── User.java
│ │ │ ├── particlesys
│ │ │ ├── ParticleSystem.java
│ │ │ ├── ParticleSystemRenderer.java
│ │ │ └── ParticlesGenerator.java
│ │ │ ├── util
│ │ │ └── gl
│ │ │ │ ├── Const.java
│ │ │ │ └── math
│ │ │ │ └── MathHelper.java
│ │ │ ├── view
│ │ │ ├── BrightSideFragment.java
│ │ │ ├── DarkSideFragment.java
│ │ │ ├── DemoActivity.java
│ │ │ ├── GreetingsFragment.java
│ │ │ └── SideFragment.java
│ │ │ └── widget
│ │ │ ├── BackgroundDrawableSwitchCompat.java
│ │ │ ├── CenterTopImageView.java
│ │ │ └── ClipRevealFrame.java
│ └── res
│ │ ├── anim
│ │ ├── slide_downward.xml
│ │ └── slide_upward.xml
│ │ ├── drawable-hdpi
│ │ ├── anakin.png
│ │ ├── darth.png
│ │ ├── ic_close.png
│ │ ├── star_wars_logo.png
│ │ └── track_off.png
│ │ ├── drawable-mdpi
│ │ └── anakin.png
│ │ ├── drawable-v21
│ │ └── button_setup_profile.xml
│ │ ├── drawable-xhdpi
│ │ ├── anakin.png
│ │ ├── darth.png
│ │ ├── ic_close.png
│ │ ├── star_wars_logo.png
│ │ └── track_off.png
│ │ ├── drawable-xxhdpi
│ │ ├── darth.png
│ │ ├── ic_close.png
│ │ ├── ic_menu.png
│ │ ├── star_wars_logo.png
│ │ └── track_off.png
│ │ ├── drawable
│ │ ├── btn_shape.xml
│ │ ├── button_setup_profile.xml
│ │ ├── leia.jpg
│ │ ├── star_wars_logo.png
│ │ ├── track_off.png
│ │ └── track_on.png
│ │ ├── layout
│ │ ├── activity_demo.xml
│ │ ├── fragment_greetings.xml
│ │ ├── fragment_side.xml
│ │ ├── fragment_star_wars.xml
│ │ ├── item_profile_gender.xml
│ │ ├── item_profile_other.xml
│ │ └── item_profile_side.xml
│ │ ├── menu
│ │ └── menu_star_wars.xml
│ │ ├── mipmap-nodpi
│ │ └── ic_launcher.png
│ │ ├── raw
│ │ ├── star_frag.glsl
│ │ └── star_vert.glsl
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── yalantis
│ └── starwars
│ └── ExampleUnitTest.java
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── library.iml
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── yalantis
│ │ └── starwars
│ │ ├── Const.java
│ │ ├── TilesFrameLayout.java
│ │ ├── interfaces
│ │ ├── Renderable.java
│ │ ├── ShaderType.java
│ │ └── TilesFrameLayoutListener.java
│ │ ├── render
│ │ ├── Buffers.java
│ │ ├── GenerateVerticesData.java
│ │ ├── StarWarsRenderer.java
│ │ └── StarWarsTiles.java
│ │ ├── utils
│ │ └── gl
│ │ │ ├── RawResourceReader.java
│ │ │ ├── ShaderHelper.java
│ │ │ └── TextureHelper.java
│ │ └── widget
│ │ └── StarWarsTilesGLSurfaceView.java
│ └── res
│ ├── raw
│ ├── tiles_frag.glsl
│ └── tiles_vert.glsl
│ └── values
│ ├── attrs_sw_tiles.xml
│ └── strings.xml
├── settings.gradle
└── star_wars-concept.gif
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | /.ignored
6 | .DS_Store
7 | /build
8 | /captures
9 | /logs
10 | /.idea
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright © 2015 Yalantis, https://yalantis.com
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # StarWars.Android
2 |
3 | []()
4 | []()
5 | [](https://jitpack.io/#yalantis/starwars.android)
6 | [](https://yalantis.com/?utm_source=github)
7 |
8 | This component implements transition animation to crumble view into tiny pieces.
9 |
10 |
11 |
12 |
13 |
Check this project on dribbble.
14 |
15 | Also, read how it was done in [our blog](https://yalantis.com/blog/star-wars-the-force-awakens-or-how-to-crumble-view-into-tiny-pieces-on-android)
16 |
17 | ##Requirements
18 | - Android SDK 15+
19 | - OpenGL ES 2.0+
20 |
21 | ##Usage
22 |
23 | Add to your module's build.gradle:
24 | ```Groovy
25 | dependencies {
26 | //...
27 | compile 'com.yalantis:starwarstiles:0.1.1'
28 | }
29 | ```
30 |
31 | Wrap your fragment or activity main view in TilesFrameLayout:
32 | ```xml
33 |
39 |
40 |
41 |
42 |
43 | ```
44 |
45 |
46 | Adjust animation with these parameters:
47 | - ```app:sw_animationDuration``` – duration in milliseconds
48 | - ```app:sw_numberOfTilesX``` – the number of square tiles the plane is tessellated into broadwise
49 |
50 | ```java
51 | mTilesFrameLayout = (TilesFrameLayout) findViewById(R.id.tiles_frame);
52 | mTilesFrameLayout.setOnAnimationFinishedListener(this);
53 | ```
54 | In your activity or fragment’s onPause() and onResume() it’s important to call the corresponding methods:
55 | ```java
56 | @Override
57 | public void onResume() {
58 | super.onResume();
59 | mTilesFrameLayout.onResume();
60 | }
61 |
62 | @Override
63 | public void onPause() {
64 | super.onPause();
65 | mTilesFrameLayout.onPause();
66 | }
67 | ```
68 | To start the animation simply call:
69 | ```java
70 | mTilesFrameLayout.startAnimation();
71 | ```
72 | Your callback will be called when the animation ends:
73 | ```java
74 | @Override
75 | public void onAnimationFinished() {
76 | // Hide or remove your view/fragment/activity here
77 | }
78 | ```
79 |
80 | Have fun! :)
81 |
82 | #### Let us know!
83 |
84 | We’d be really happy if you sent us links to your projects where you use our component. Just send an email to github@yalantis.com And do let us know if you have any questions or suggestion regarding the animation.
85 |
86 | P.S. We’re going to publish more awesomeness wrapped in code and a tutorial on how to make UI for iOS (Android) better than better. Stay tuned!
87 |
88 | ## License
89 |
90 | The MIT License (MIT)
91 |
92 | Copyright © 2017 Yalantis, https://yalantis.com
93 |
94 | Permission is hereby granted, free of charge, to any person obtaining a copy
95 | of this software and associated documentation files (the "Software"), to deal
96 | in the Software without restriction, including without limitation the rights
97 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
98 | copies of the Software, and to permit persons to whom the Software is
99 | furnished to do so, subject to the following conditions:
100 |
101 | The above copyright notice and this permission notice shall be included in
102 | all copies or substantial portions of the Software.
103 |
104 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
105 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
106 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
107 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
109 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
110 | THE SOFTWARE.
111 |
112 |
--------------------------------------------------------------------------------
/StarWars.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | maven { url 'https://maven.fabric.io/public' }
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:2.1.0-alpha3'
10 | classpath 'io.fabric.tools:gradle:1.21.0'
11 |
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | jcenter()
20 | }
21 | }
22 |
23 | task clean(type: Delete) {
24 | delete rootProject.buildDir
25 | }
26 |
--------------------------------------------------------------------------------
/demo/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/demo/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | maven { url 'https://maven.fabric.io/public' }
4 | }
5 |
6 | dependencies {
7 | classpath 'io.fabric.tools:gradle:1.21.0'
8 | }
9 | }
10 | apply plugin: 'com.android.application'
11 |
12 | repositories {
13 | maven { url 'https://maven.fabric.io/public' }
14 | }
15 |
16 | apply plugin: 'io.fabric'
17 |
18 |
19 | android {
20 | compileSdkVersion 23
21 | buildToolsVersion '23.0.2'
22 |
23 | defaultConfig {
24 | applicationId "com.yalantis.starwarsdemo"
25 | minSdkVersion 15
26 | targetSdkVersion 23
27 | versionCode 1
28 | versionName "0.1.0"
29 | }
30 | buildTypes {
31 | release {
32 | minifyEnabled false
33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34 | }
35 | }
36 | dataBinding {
37 | enabled = false
38 | }
39 | }
40 |
41 | dependencies {
42 | compile fileTree(dir: 'libs', include: ['*.jar'])
43 | testCompile 'junit:junit:4.12'
44 | compile 'com.android.support:appcompat-v7:23.1.1'
45 | compile 'com.android.support:design:23.1.1'
46 | compile 'com.jakewharton.timber:timber:4.1.0'
47 | compile 'com.jakewharton:butterknife:7.0.1'
48 | compile 'com.android.support:recyclerview-v7:23.1.1'
49 | compile 'com.android.support:percent:23.1.1'
50 | compile project(':library')
51 | compile('com.crashlytics.sdk.android:crashlytics:2.5.3@aar') {
52 | transitive = true;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/demo/demo.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
--------------------------------------------------------------------------------
/demo/fabric.properties:
--------------------------------------------------------------------------------
1 | apiKey=fce5ef09bbc74ca549bb0716a8f911f0b913e4be
2 | apiSecret=e4ebb3d3fd9e249e06ef2a0269fe21a298a97701ba57956187c912aba956991c
3 |
--------------------------------------------------------------------------------
/demo/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /home/ice/dev/android-sdk-linux/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/demo/src/androidTest/java/com/yalantis/starwarsdemo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/demo/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
14 |
15 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/App.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | import com.crashlytics.android.Crashlytics;
7 | import io.fabric.sdk.android.Fabric;
8 | import timber.log.Timber;
9 |
10 | /**
11 | * Created by Artem Kholodnyi on 11/2/15.
12 | */
13 | public class App extends Application{
14 | private static App app;
15 |
16 | @Override
17 | public void onCreate() {
18 | super.onCreate();
19 | Fabric.with(this, new Crashlytics());
20 | app = this;
21 |
22 | Timber.plant(new Timber.DebugTree());
23 | }
24 |
25 | public static Context getAppContext() {
26 | return app;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/adapter/ProfileAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.LayoutRes;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.CompoundButton;
11 | import android.widget.TextView;
12 |
13 | import com.yalantis.starwarsdemo.R;
14 | import com.yalantis.starwarsdemo.interfaces.ProfileAdapterListener;
15 | import com.yalantis.starwarsdemo.model.User;
16 | import com.yalantis.starwarsdemo.widget.BackgroundDrawableSwitchCompat;
17 |
18 | import butterknife.Bind;
19 | import butterknife.ButterKnife;
20 |
21 | /**
22 | * Created by Artem Kholodnyi on 11/17/15.
23 | */
24 | public class ProfileAdapter extends RecyclerView.Adapter {
25 | private static final int VIEW_TYPE_SIDE = 0;
26 | private static final int VIEW_TYPE_TEXT_FIELD = 1;
27 | private static final int VIEW_GENDER_FIELD = 2;
28 | private final ProfileAdapterListener mListener;
29 | private User mUser;
30 |
31 | private final String mFullNameLabel;
32 | private final String mHomeWorldLabel;
33 | private final String mBirthdayLabel;
34 | private final String mGenderLabel;
35 |
36 | public ProfileAdapter(Context context, User user, ProfileAdapterListener listener) {
37 | mUser = user;
38 | mListener = listener;
39 |
40 | mFullNameLabel = context.getString(R.string.label_full_name);
41 | mHomeWorldLabel = context.getString(R.string.label_homeworld);
42 | mBirthdayLabel = context.getString(R.string.label_birthday);
43 | mGenderLabel = context.getString(R.string.label_gender);
44 | }
45 |
46 | @Override
47 | public int getItemViewType(int position) {
48 | switch (position) {
49 | case 0:
50 | return VIEW_TYPE_SIDE;
51 | case 1:
52 | case 2:
53 | case 3:
54 | return VIEW_TYPE_TEXT_FIELD;
55 | case 4:
56 | return VIEW_GENDER_FIELD;
57 | default:
58 | throw new IllegalStateException();
59 | }
60 | }
61 |
62 | @Override
63 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
64 | @LayoutRes int layoutRes = getLayoutId(viewType);
65 |
66 | View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
67 | return new ViewHolder(itemView);
68 | }
69 |
70 | @LayoutRes
71 | private int getLayoutId(int viewType) {
72 | switch (viewType) {
73 | case VIEW_TYPE_SIDE:
74 | return R.layout.item_profile_side;
75 | case VIEW_TYPE_TEXT_FIELD:
76 | return R.layout.item_profile_other;
77 | case VIEW_GENDER_FIELD:
78 | return R.layout.item_profile_gender;
79 | default:
80 | throw new IllegalStateException();
81 | }
82 | }
83 |
84 | @SuppressWarnings("ConstantConditions")
85 | @Override
86 | public void onBindViewHolder(final ViewHolder holder, int position) {
87 | switch (position) {
88 | case 0:
89 | // holder.getBinding().setVariable(BR.callback, mListener);
90 | // holder.getBinding().setVariable(BR.user, mUser);
91 | holder.mySwitch.setCheckedImmediate(mUser.isDarkSide());
92 | holder.mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
93 | @Override
94 | public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
95 | mListener.onSideSwitch(holder.mySwitch);
96 | }
97 | });
98 | holder.label.setText(mUser.getSideText());
99 | break;
100 | case 1:
101 | holder.label.setText(mFullNameLabel);
102 | holder.value.setText(mUser.getFullName());
103 | break;
104 | case 2:
105 | holder.label.setText(mHomeWorldLabel);
106 | holder.value.setText(mUser.getHomeworld());
107 | break;
108 | case 3:
109 | holder.label.setText(mBirthdayLabel);
110 | holder.value.setText(mUser.getBirthday());
111 | break;
112 | case 4:
113 | holder.label.setText(mGenderLabel);
114 | break;
115 | default:
116 | throw new IllegalStateException("unknown");
117 | }
118 | }
119 |
120 | @Override
121 | public int getItemCount() {
122 | return 5;
123 | }
124 |
125 | public class ViewHolder extends RecyclerView.ViewHolder {
126 | @Nullable
127 | @Bind(R.id.side_switch)
128 | BackgroundDrawableSwitchCompat mySwitch;
129 |
130 | @Nullable
131 | @Bind(R.id.tv_label)
132 | TextView label;
133 |
134 | @Nullable
135 | @Bind(R.id.tv_value)
136 | TextView value;
137 |
138 | public ViewHolder(View itemView) {
139 | super(itemView);
140 | ButterKnife.bind(this, itemView);
141 | }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/interfaces/DemoActivityInterface.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.interfaces;
2 |
3 | /**
4 | * Created by Artem Kholodnyi on 11/19/15.
5 | */
6 | public interface DemoActivityInterface {
7 | void goToSide(int cx, int cy, boolean appBarExpanded, String side);
8 | void removeAllFragmentExcept(String tag);
9 | }
10 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/interfaces/GreetingFragmentInterface.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.interfaces;
2 |
3 | /**
4 | * Created by Artem Kholodnyi on 11/17/15.
5 | */
6 | public interface GreetingFragmentInterface {
7 | void onSetupProfileClick();
8 | }
9 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/interfaces/ProfileAdapterListener.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.interfaces;
2 |
3 | import android.support.v7.widget.SwitchCompat;
4 |
5 | /**
6 | * Created by Artem Kholodnyi on 11/17/15.
7 | */
8 | public interface ProfileAdapterListener {
9 | void onSideSwitch(SwitchCompat view);
10 | }
11 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/interfaces/TilesRendererInterface.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.interfaces;
2 |
3 | /**
4 | * Created by Artem Kholodnyi on 11/26/15.
5 | */
6 | public interface TilesRendererInterface {
7 | void onTilesFinished();
8 | }
9 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/model/User.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.model;
2 |
3 | import android.support.annotation.DrawableRes;
4 | import android.support.annotation.StringRes;
5 |
6 | import com.yalantis.starwarsdemo.R;
7 |
8 | /**
9 | * Created by Artem Kholodnyi on 11/19/15.
10 | */
11 | public class User {
12 | private final String mFullName;
13 | private final boolean mDarkSide;
14 | private final String mHomeworld;
15 | private final String mBirthday;
16 |
17 | public User(boolean darkSide, String fullName, String homeworld, String birthday) {
18 | mDarkSide = darkSide;
19 | mFullName = fullName;
20 | mHomeworld = homeworld;
21 | mBirthday = birthday;
22 | }
23 |
24 | public @DrawableRes int getPhotoRes() {
25 | return mDarkSide ? R.drawable.darth : R.drawable.anakin;
26 | }
27 |
28 | public boolean isDarkSide() {
29 | return mDarkSide;
30 | }
31 |
32 | public String getFullName() {
33 | return mFullName;
34 | }
35 |
36 | public String getHomeworld() {
37 | return mHomeworld;
38 | }
39 |
40 | public String getBirthday() {
41 | return mBirthday;
42 | }
43 |
44 | public @StringRes int getSideText() {
45 | return isDarkSide() ? R.string.dark_side_label : R.string.light_side_label;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/particlesys/ParticleSystem.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.particlesys;
2 |
3 | import android.opengl.GLES20;
4 |
5 | import com.yalantis.starwars.interfaces.Renderable;
6 | import com.yalantis.starwarsdemo.util.gl.Const;
7 |
8 | import java.nio.ByteBuffer;
9 | import java.nio.ByteOrder;
10 | import java.nio.FloatBuffer;
11 | import java.nio.ShortBuffer;
12 |
13 | import timber.log.Timber;
14 |
15 | import static android.opengl.GLES20.GL_ARRAY_BUFFER;
16 | import static android.opengl.GLES20.GL_STATIC_DRAW;
17 | import static android.opengl.GLES20.glBindBuffer;
18 | import static android.opengl.GLES20.glBufferData;
19 | import static android.opengl.GLES20.glGenBuffers;
20 |
21 | /**
22 | * Created by Artem Kholodnyi on 11/12/15.
23 | */
24 | public class ParticleSystem implements Renderable {
25 | private final ParticleSystemRenderer mRenderer;
26 | public static final int PARTICLE_COUNT = 1_000;
27 | private int mBufferId;
28 |
29 | public static final int POS_DATA_SIZE = 3;
30 | public static final int TEXTURE_COORDS_DATA_SIZE = 2;
31 | public static final int MISC_DATA_SIZE = 3;
32 |
33 |
34 | public ParticleSystem(ParticleSystemRenderer renderer, FloatBuffer vertexBuffer) {
35 | long startTime = System.currentTimeMillis();
36 |
37 | mRenderer = renderer;
38 |
39 | Timber.d("generated in %d ms", System.currentTimeMillis() - startTime);
40 |
41 | // Copy buffer into OpenGL's memory. After, we don't need to keep the client-side buffers around.
42 | final int buffers[] = new int[1];
43 | glGenBuffers(1, buffers, 0);
44 |
45 | glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
46 | glBufferData(GL_ARRAY_BUFFER, vertexBuffer.capacity() * Const.BYTES_PER_FLOAT, vertexBuffer, GL_STATIC_DRAW);
47 |
48 | glBindBuffer(GL_ARRAY_BUFFER, 0);
49 |
50 | mBufferId = buffers[0];
51 |
52 | vertexBuffer.limit(0);
53 | Timber.d("done in %d ms", System.currentTimeMillis() - startTime);
54 | }
55 |
56 |
57 | // use to make native order buffers
58 | private ShortBuffer makeShortBuffer(short[] arr) {
59 | ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
60 | bb.order(ByteOrder.nativeOrder());
61 | ShortBuffer ib = bb.asShortBuffer();
62 | ib.put(arr);
63 | ib.position(0);
64 | return ib;
65 | }
66 |
67 | @Override
68 | public void render() {
69 | final int stride = Const.BYTES_PER_FLOAT
70 | * (POS_DATA_SIZE + TEXTURE_COORDS_DATA_SIZE + MISC_DATA_SIZE);
71 |
72 | // a_Position
73 | glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
74 | GLES20.glEnableVertexAttribArray(mRenderer.positionHandle);
75 | GLES20.glVertexAttribPointer(mRenderer.positionHandle,
76 | POS_DATA_SIZE,
77 | GLES20.GL_FLOAT,
78 | false,
79 | stride,
80 | 0
81 | );
82 |
83 | // a_TexCoordinate
84 | glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
85 | GLES20.glEnableVertexAttribArray(mRenderer.textureCoordinateHandle);
86 | GLES20.glVertexAttribPointer(mRenderer.textureCoordinateHandle,
87 | TEXTURE_COORDS_DATA_SIZE,
88 | GLES20.GL_FLOAT,
89 | false,
90 | stride,
91 | Const.BYTES_PER_FLOAT * (POS_DATA_SIZE)
92 | );
93 |
94 | // a_Misc
95 | glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
96 | GLES20.glEnableVertexAttribArray(mRenderer.miscHandle);
97 | GLES20.glVertexAttribPointer(mRenderer.miscHandle,
98 | MISC_DATA_SIZE,
99 | GLES20.GL_FLOAT,
100 | false,
101 | stride,
102 | Const.BYTES_PER_FLOAT * (POS_DATA_SIZE + TEXTURE_COORDS_DATA_SIZE)
103 | );
104 |
105 | // Clear the currently bound buffer (so future OpenGL calls do not use this buffer).
106 | glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
107 |
108 | // Draw tiles
109 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, PARTICLE_COUNT * 6);
110 | }
111 |
112 | @Override
113 | public void release() {
114 | final int[] buffersToDelete = new int[] { mBufferId };
115 | GLES20.glDeleteBuffers(buffersToDelete.length, buffersToDelete, 0);
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/particlesys/ParticleSystemRenderer.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.particlesys;
2 |
3 | import android.animation.ValueAnimator;
4 | import android.graphics.Bitmap;
5 | import android.opengl.GLES20;
6 | import android.opengl.GLSurfaceView;
7 | import android.opengl.Matrix;
8 |
9 | import com.yalantis.starwars.utils.gl.RawResourceReader;
10 | import com.yalantis.starwars.utils.gl.ShaderHelper;
11 | import com.yalantis.starwarsdemo.App;
12 | import com.yalantis.starwarsdemo.R;
13 |
14 | import java.util.concurrent.ExecutorService;
15 | import java.util.concurrent.Executors;
16 |
17 | import javax.microedition.khronos.egl.EGLConfig;
18 | import javax.microedition.khronos.opengles.GL10;
19 |
20 | import timber.log.Timber;
21 |
22 | /**
23 | * Created by Artem Kholodnyi on 11/12/15.
24 | */
25 | public class ParticleSystemRenderer implements GLSurfaceView.Renderer {
26 | public float ratio;
27 | public int mvpMatrixHandle;
28 | public int mvMatrixHandle = -1;
29 | public int positionHandle;
30 | public int normalHandle;
31 | public int textureCoordinateHandle;
32 | public int programHandle;
33 | public int miscHandle;
34 | public int sizeX = 35;
35 | public int sizeY = 70;
36 | public float mTime;
37 | private GLSurfaceView mGlSurfaceView;
38 | /**
39 | * Store the model matrix. This matrix is used to move models from object space (where each model can be thought
40 | * of being located at the center of the universe) to world space.
41 | */
42 | private float[] mModelMatrix = new float[16];
43 | /**
44 | * Store the view matrix. This can be thought of as our camera. This matrix transforms world space to eye space;
45 | * it positions things relative to our eye.
46 | */
47 | private float[] mViewMatrix = new float[16];
48 | /** Store the projection matrix. This is used to project the scene onto a 2D viewport. */
49 | private float[] mProjectionMatrix = new float[16];
50 | /** Allocate storage for the final combined matrix. This will be passed into the shader program. */
51 | private float[] mMVPMatrix = new float[16];
52 | private float[] mTemporaryMatrix = new float[16];
53 | private int timeHandle;
54 | private long mStartTime;
55 | private int frames;
56 | private long startTime;
57 | private boolean mStart;
58 | private long timePassed;
59 | private float dt;
60 | private long t_current;
61 | private long t_prev;
62 | private float dt_prev = 1;
63 | private ValueAnimator animator;
64 | private Bitmap mBitmap;
65 | private ParticleSystem mParticleSystem;
66 | private int resolutionHandle;
67 | private int mWidth;
68 | private int mHeight;
69 | private int timesRepeated;
70 | private float delta;
71 | private ExecutorService mExecutor = Executors.newSingleThreadExecutor();
72 |
73 |
74 | public ParticleSystemRenderer(GLSurfaceView glSurfaceView) {
75 | mGlSurfaceView = glSurfaceView;
76 | }
77 |
78 | @Override
79 | public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
80 | GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
81 |
82 | // Use culling to remove back faces.
83 | GLES20.glEnable(GLES20.GL_CULL_FACE);
84 | GLES20.glFrontFace(GLES20.GL_CW);
85 |
86 | // Enable depth testing
87 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
88 |
89 | // Position the eye in front of the origin.
90 | final float eyeX = 0.0f;
91 | final float eyeY = 0.0f;
92 | final float eyeZ = 0.0f;
93 |
94 | // We are looking toward the distance
95 | final float lookX = 0.0f;
96 | final float lookY = 0.0f;
97 | final float lookZ = 1.0f;
98 |
99 | // Set our up vector. This is where our head would be pointing were we holding the camera.
100 | final float upX = 0.0f;
101 | final float upY = 1.0f;
102 | final float upZ = 0.0f;
103 |
104 | Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
105 |
106 | final String vertexShader = RawResourceReader.readTextFileFromRawResource(App.getAppContext(), R.raw.star_vert);
107 | final String fragmentShader = RawResourceReader.readTextFileFromRawResource(App.getAppContext(), R.raw.star_frag);
108 |
109 | final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
110 | final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
111 |
112 | programHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
113 | new String[]{"a_Position", "a_TexCoordinate", "a_TileXY"});
114 | }
115 |
116 | /**
117 | *
118 | */
119 | @Override
120 | public void onSurfaceChanged(GL10 unused, int width, int height) {
121 | // Set the OpenGL viewport to the same size as the surface.
122 | GLES20.glViewport(0, 0, width, height);
123 |
124 | mWidth = width;
125 | mHeight = height;
126 |
127 | // Create a new perspective projection matrix. The height will stay the same
128 | // while the width will vary as per aspect ratio.
129 | final float ratio = (float) width / height;
130 |
131 | final float left = -ratio;
132 | @SuppressWarnings("UnnecessaryLocalVariable")
133 | final float right = ratio;
134 | final float bottom = -1.0f;
135 | final float top = 1.0f;
136 | final float near = 1.0f;
137 | final float far = 10.0f;
138 |
139 | this.ratio = ratio;
140 |
141 | Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
142 |
143 | mStartTime = System.currentTimeMillis();
144 |
145 | mExecutor.execute(new ParticlesGenerator(this));
146 | }
147 |
148 | @Override
149 | public void onDrawFrame(GL10 gl10) {
150 | logFrame();
151 | drawGl();
152 |
153 | if (mParticleSystem != null) {
154 | mParticleSystem.render();
155 | }
156 | }
157 |
158 | private void drawGl() {
159 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
160 |
161 | GLES20.glUseProgram(programHandle);
162 |
163 | // Set program handles
164 | mvpMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
165 | mvMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix");
166 | timeHandle = GLES20.glGetUniformLocation(programHandle, "u_Time");
167 | resolutionHandle = GLES20.glGetUniformLocation(programHandle, "u_Resolution");
168 |
169 | positionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
170 | normalHandle = GLES20.glGetAttribLocation(programHandle, "a_Normal");
171 | textureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "a_TexCoordinate");
172 | miscHandle = GLES20.glGetAttribLocation(programHandle, "a_Misc");
173 |
174 | Matrix.setIdentityM(mModelMatrix, 0);
175 | Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, 5f);
176 |
177 | Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
178 |
179 | // Pass in the modelview matrix.
180 | GLES20.glUniformMatrix4fv(mvMatrixHandle, 1, false, mMVPMatrix, 0);
181 |
182 | Matrix.multiplyMM(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
183 | System.arraycopy(mTemporaryMatrix, 0, mMVPMatrix, 0, 16);
184 |
185 | // Pass in the combined matrix.
186 | GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mMVPMatrix, 0);
187 |
188 | // Pass in u_Time
189 | GLES20.glUniform1f(timeHandle, (System.currentTimeMillis() - mStartTime) / 3500f);
190 |
191 | // u_Resolution
192 | GLES20.glUniform2f(resolutionHandle, mWidth, mHeight);
193 |
194 | GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
195 | GLES20.glEnable(GLES20.GL_BLEND);
196 | }
197 |
198 | public void logFrame() {
199 | frames++;
200 | timePassed = (System.nanoTime() - startTime) / 1_000_000;
201 | if(timePassed >= 10_000) {
202 | Timber.d("fps %d @ %d stars || %f", frames / 10, ParticleSystem.PARTICLE_COUNT, mTime);
203 | frames = 0;
204 | startTime = System.nanoTime();
205 | }
206 | }
207 |
208 | public void onTouchEvent() {
209 | if (mStart) {
210 | reset();
211 | }
212 | mStart = !mStart;
213 | mStartTime = System.nanoTime();
214 | }
215 |
216 | private void reset() {
217 | if (animator != null) {
218 | animator.cancel();
219 | }
220 | mStartTime = 0;
221 | dt = 0;
222 | t_prev = 0;
223 | }
224 |
225 |
226 | public ParticleSystem getParticleSystem() {
227 | return mParticleSystem;
228 | }
229 |
230 | public void setParticleSystem(final ParticleSystem particleSystem) {
231 | mParticleSystem = particleSystem;
232 | }
233 |
234 | public void queue(Runnable runnable) {
235 | mGlSurfaceView.queueEvent(runnable);
236 | }
237 | }
238 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/particlesys/ParticlesGenerator.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.particlesys;
2 |
3 | import com.yalantis.starwarsdemo.util.gl.Const;
4 | import com.yalantis.starwarsdemo.util.gl.math.MathHelper;
5 |
6 | import java.nio.ByteBuffer;
7 | import java.nio.ByteOrder;
8 | import java.nio.FloatBuffer;
9 |
10 | /**
11 | * Started by Artem Kholodnyi on 11/29/15 6:13 PM -- com.yalantis.starwarsdemo.particlesys
12 | */
13 | public class ParticlesGenerator implements Runnable {
14 | private final ParticleSystemRenderer mRenderer;
15 |
16 | public ParticlesGenerator(ParticleSystemRenderer particleSystemRenderer) {
17 | mRenderer = particleSystemRenderer;
18 | }
19 |
20 | @Override
21 | public void run() {
22 | final FloatBuffer vertexBuffer = makeInterleavedBuffer(
23 | generatePositions(),
24 | generateTextureUv(),
25 | generateMisc(),
26 | ParticleSystem.PARTICLE_COUNT
27 | );
28 | mRenderer.queue(new Runnable() {
29 | @Override
30 | public void run() {
31 | mRenderer.setParticleSystem(new ParticleSystem(mRenderer, vertexBuffer));
32 | }
33 | });
34 | }
35 |
36 | private float[] generatePositions() {
37 | final int size = ParticleSystem.PARTICLE_COUNT * ParticleSystem.POS_DATA_SIZE * 6;
38 |
39 | final float height = 5.0f;
40 | final float width = height * mRenderer.ratio;
41 |
42 | final float[] posData = new float[size];
43 |
44 | final float z = 0f;
45 |
46 | int offset = 0;
47 | for (int p = 0; p < ParticleSystem.PARTICLE_COUNT; p++) {
48 | final float uc = MathHelper.mix(-width, width, Math.random());
49 | final float vc = MathHelper.mix(-height, height, Math.random());
50 |
51 | final float u0 = uc;// + delta;
52 | final float v0 = vc;// + delta;
53 | final float u1 = uc;// - delta;
54 | final float v1 = vc;// - delta;
55 |
56 | final int elementsPerPoint = ParticleSystem.POS_DATA_SIZE;
57 |
58 | // 1---2
59 | // | / |
60 | // 3---4
61 |
62 | final float[] p1 = {u1, v0, z};
63 | final float[] p2 = {u0, v0, z};
64 | final float[] p3 = {u1, v1, z};
65 | final float[] p4 = {u0, v1, z};
66 |
67 | for (int i = 0; i < elementsPerPoint; i++)
68 | posData[offset++] = p1[i];
69 | for (int i = 0; i < elementsPerPoint; i++)
70 | posData[offset++] = p3[i];
71 | for (int i = 0; i < elementsPerPoint; i++)
72 | posData[offset++] = p2[i];
73 |
74 | for (int i = 0; i < elementsPerPoint; i++)
75 | posData[offset++] = p3[i];
76 | for (int i = 0; i < elementsPerPoint; i++)
77 | posData[offset++] = p4[i];
78 | for (int i = 0; i < elementsPerPoint; i++)
79 | posData[offset++] = p2[i];
80 | }
81 |
82 | return posData;
83 | }
84 |
85 | private float[] generateTextureUv() {
86 | int size = ParticleSystem.TEXTURE_COORDS_DATA_SIZE * 6;
87 |
88 | final float[] thisUvData = new float[size];
89 |
90 | final float u0 = 0f;
91 | final float v0 = 0f;
92 | final float u1 = 1f;
93 | final float v1 = 1f;
94 |
95 | final int elementsPerPoint = ParticleSystem.TEXTURE_COORDS_DATA_SIZE;
96 |
97 | int offset = 0;
98 |
99 | // 1---2
100 | // | / |
101 | // 3---4
102 |
103 | final float[] p1 = {u1, v0};
104 | final float[] p2 = {u0, v0};
105 | final float[] p3 = {u1, v1};
106 | final float[] p4 = {u0, v1};
107 |
108 | for (int i = 0; i < elementsPerPoint; i++)
109 | thisUvData[offset++] = p1[i];
110 | for (int i = 0; i < elementsPerPoint; i++)
111 | thisUvData[offset++] = p3[i];
112 | for (int i = 0; i < elementsPerPoint; i++)
113 | thisUvData[offset++] = p2[i];
114 |
115 | for (int i = 0; i < elementsPerPoint; i++)
116 | thisUvData[offset++] = p3[i];
117 | for (int i = 0; i < elementsPerPoint; i++)
118 | thisUvData[offset++] = p4[i];
119 | for (int i = 0; i < elementsPerPoint; i++)
120 | thisUvData[offset++] = p2[i];
121 |
122 | return thisUvData;
123 | }
124 |
125 | private float[] generateMisc() {
126 | final int dataLength = ParticleSystem.PARTICLE_COUNT * ParticleSystem.MISC_DATA_SIZE * 6;
127 |
128 | float[] starData = new float[dataLength];
129 |
130 | int offset = 0;
131 | for (int i = 0; i < ParticleSystem.PARTICLE_COUNT; i++) {
132 | final float particleSize = MathHelper.mix(0.05f, 0.15f, Math.random());
133 | float accel = MathHelper.mix(1.1f, 3f, Math.random());
134 | float rand = (float) Math.random();
135 |
136 | for (int v = 0; v < 6; v++) {
137 | starData[offset++] = particleSize;
138 | starData[offset++] = accel;
139 | starData[offset++] = rand;
140 | }
141 | }
142 |
143 | return starData;
144 |
145 | }
146 |
147 |
148 | private FloatBuffer makeInterleavedBuffer(float[] posData,
149 | float[] uvData,
150 | float[] miscData,
151 | int numStars) {
152 |
153 | int dataLength = posData.length
154 | + uvData.length * numStars
155 | + miscData.length;
156 |
157 | final FloatBuffer interleavedBuffer = ByteBuffer.allocateDirect(dataLength * Const.BYTES_PER_FLOAT)
158 | .order(ByteOrder.nativeOrder()).asFloatBuffer();
159 |
160 | int positionOffset = 0, uvOffset = 0, starDataOffset = 0;
161 |
162 | for (int i = 0; i < numStars; i++) {
163 | for (int v = 0; v < 6; v++) {
164 | interleavedBuffer.put(posData, positionOffset, ParticleSystem.POS_DATA_SIZE);
165 | positionOffset += ParticleSystem.POS_DATA_SIZE;
166 |
167 | interleavedBuffer.put(uvData, uvOffset, ParticleSystem.TEXTURE_COORDS_DATA_SIZE);
168 | uvOffset = (uvOffset + ParticleSystem.TEXTURE_COORDS_DATA_SIZE ) % uvData.length;
169 |
170 | interleavedBuffer.put(miscData, starDataOffset, ParticleSystem.MISC_DATA_SIZE);
171 | starDataOffset += ParticleSystem.MISC_DATA_SIZE;
172 | }
173 | }
174 |
175 | interleavedBuffer.position(0);
176 | return interleavedBuffer;
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/util/gl/Const.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.util.gl;
2 |
3 | /**
4 | * Started by Artem Kholodnyi on 11/1/15 12:55 PM -- com.yalantis.com.yalantis.starwars.util.gl
5 | */
6 | public class Const {
7 |
8 | public static final int POSITION_DATA_SIZE = 3;
9 | public static final int NORMALS_DATA_SIZE = 3;
10 | public static final int TEXTURE_COORDS_DATA_SIZE = 2;
11 | public static final int TILE_XY_DATA_SIZE = 3;
12 |
13 |
14 | public static final int STAR_DATA_SIZE = 4;
15 | public static final int BYTES_PER_FLOAT = 4;
16 | public static final int POINTS_PER_TILE = 6;
17 | }
18 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/util/gl/math/MathHelper.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.util.gl.math;
2 |
3 | /**
4 | * Created by Artem Kholodnyi on 11/13/15.
5 | */
6 | public class MathHelper {
7 |
8 | public static float mix(float x, float y, float a) {
9 | return x * (1 - a) + y * a;
10 | }
11 |
12 | public static float mix(float a, float b , double k) {
13 | return (float) (a * (1 - k) + b * k);
14 | }
15 |
16 | public static float smoothstep(float edge0, float edge1, float x) {
17 | float t = (float) clamp((x - edge0) / (edge1 - edge0), 0f, 1f);
18 | return t * t * (3f - 2f * t);
19 | }
20 |
21 | public static double clamp(float x, float f, double c) {
22 | return Math.max(f, Math.min(x, c));
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/view/BrightSideFragment.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.view;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.yalantis.starwarsdemo.R;
6 | import com.yalantis.starwarsdemo.model.User;
7 |
8 | /**
9 | * Created by Artem Kholodnyi on 11/17/15.
10 | */
11 | public class BrightSideFragment extends SideFragment {
12 |
13 | public static BrightSideFragment newInstance(int centerX, int centerY, boolean appBarExpanded) {
14 | Bundle args = new Bundle();
15 | args.putInt(ARG_CX, centerX);
16 | args.putInt(ARG_CY, centerY);
17 | args.putBoolean(ARG_SHOULD_EXPAND, appBarExpanded);
18 |
19 | BrightSideFragment frag = newInstance();
20 | frag.setArguments(args);
21 |
22 | return frag;
23 | }
24 |
25 | public static BrightSideFragment newInstance() {
26 | return new BrightSideFragment();
27 | }
28 |
29 | @Override
30 | int getTheme() {
31 | return R.style.StarWarsAppThemeLight;
32 | }
33 |
34 | @Override
35 | User getUser() {
36 | return new User(false, "Anakin Skywalker", "Tatooine", "41.9 BBY");
37 | }
38 |
39 | @Override
40 | public String getTagString() {
41 | return "bright";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/view/DarkSideFragment.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.view;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.yalantis.starwarsdemo.R;
6 | import com.yalantis.starwarsdemo.model.User;
7 |
8 | /**
9 | * Created by Artem Kholodnyi on 11/17/15.
10 | */
11 | public class DarkSideFragment extends SideFragment {
12 |
13 | public static DarkSideFragment newInstance(int centerX, int centerY, boolean appBarExpanded) {
14 | Bundle args = new Bundle();
15 | args.putInt(ARG_CX, centerX);
16 | args.putInt(ARG_CY, centerY);
17 | args.putBoolean(ARG_SHOULD_EXPAND, appBarExpanded);
18 |
19 | DarkSideFragment frag = newInstance();
20 | frag.setArguments(args);
21 |
22 | return frag;
23 | }
24 |
25 | public static DarkSideFragment newInstance() {
26 | return new DarkSideFragment();
27 | }
28 |
29 |
30 | @Override
31 | int getTheme() {
32 | return R.style.StarWarsAppThemeDark;
33 | }
34 |
35 | @Override
36 | User getUser() {
37 | return new User(true, "Darth Vader", "Tatooine", "41.9 BBY");
38 | }
39 |
40 | @Override
41 | public String getTagString() {
42 | return "dark";
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/view/DemoActivity.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.view;
2 |
3 | import android.app.ActivityManager;
4 | import android.content.Context;
5 | import android.content.pm.ConfigurationInfo;
6 | import android.opengl.GLSurfaceView;
7 | import android.os.Bundle;
8 | import android.os.Handler;
9 | import android.support.annotation.Nullable;
10 | import android.support.v4.app.Fragment;
11 | import android.support.v4.app.FragmentTransaction;
12 | import android.support.v7.app.AppCompatActivity;
13 |
14 | import com.yalantis.starwarsdemo.R;
15 | import com.yalantis.starwarsdemo.interfaces.DemoActivityInterface;
16 | import com.yalantis.starwarsdemo.interfaces.GreetingFragmentInterface;
17 | import com.yalantis.starwarsdemo.interfaces.TilesRendererInterface;
18 | import com.yalantis.starwarsdemo.particlesys.ParticleSystemRenderer;
19 |
20 | import java.util.List;
21 |
22 | import butterknife.Bind;
23 | import butterknife.ButterKnife;
24 |
25 | /**
26 | * Created by Artem Kholodnyi on 11/11/15.
27 | */
28 | public class DemoActivity extends AppCompatActivity implements GreetingFragmentInterface,
29 | DemoActivityInterface, TilesRendererInterface {
30 | @Bind(R.id.gl_surface_view)
31 | GLSurfaceView mGlSurfaceView;
32 |
33 | private SideFragment mDarkFragment;
34 | private SideFragment mBrightFragment;
35 | private GreetingsFragment mGreetingsFragment;
36 |
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 |
41 | setContentView(R.layout.activity_demo);
42 | ButterKnife.bind(this);
43 |
44 | // Check if the system supports OpenGL ES 2.0.
45 | final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
46 | final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
47 | final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
48 |
49 | if (supportsEs2) {
50 | // Request an OpenGL ES 2.0 compatible context.
51 | mGlSurfaceView.setEGLContextClientVersion(2);
52 |
53 | // Set the renderer to our demo renderer, defined below.
54 | ParticleSystemRenderer mRenderer = new ParticleSystemRenderer(mGlSurfaceView);
55 | mGlSurfaceView.setRenderer(mRenderer);
56 | mGlSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
57 | } else {
58 | throw new UnsupportedOperationException();
59 | }
60 |
61 | if (savedInstanceState == null) {
62 | showGreetings();
63 | }
64 |
65 | }
66 |
67 | @Override
68 | protected void onPause() {
69 | super.onPause();
70 | mGlSurfaceView.onPause();
71 | }
72 |
73 | @Override
74 | protected void onResume() {
75 | super.onResume();
76 | mGlSurfaceView.onResume();
77 | }
78 |
79 | private void showGreetings() {
80 | mGreetingsFragment = GreetingsFragment.newInstance();
81 | getSupportFragmentManager().beginTransaction()
82 | .setCustomAnimations(R.anim.slide_downward, 0, R.anim.slide_downward, 0)
83 | .add(R.id.container, mGreetingsFragment, "greetings")
84 | .commit();
85 | }
86 |
87 | @Override
88 | public void onSetupProfileClick() {
89 | new Handler().post(new Runnable() {
90 | @Override
91 | public void run() {
92 | getSupportFragmentManager().beginTransaction()
93 | .setCustomAnimations(R.anim.slide_upward, 0)
94 | .add(R.id.container, BrightSideFragment.newInstance(), "bright")
95 | .commit();
96 | }
97 | });
98 |
99 | }
100 |
101 | @Override
102 | public void goToSide(int cx, int cy, boolean appBarExpanded, String side) {
103 | FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
104 |
105 |
106 | mDarkFragment = DarkSideFragment.newInstance(cx, cy, appBarExpanded);
107 | Fragment fragment;
108 | switch (side) {
109 | case "bright":
110 | fragment = BrightSideFragment.newInstance(cx, cy, appBarExpanded);
111 | break;
112 | case "dark":
113 | fragment = DarkSideFragment.newInstance(cx, cy, appBarExpanded);
114 | break;
115 | default:
116 | throw new IllegalStateException();
117 | }
118 | ft.add(R.id.container, fragment, side).commit();
119 | }
120 |
121 | @Override
122 | public void removeAllFragmentExcept(@Nullable String tag) {
123 | List frags = getSupportFragmentManager().getFragments();
124 | FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
125 | Fragment frag;
126 | for (int i = 0; i < frags.size(); i++) {
127 | frag = frags.get(i);
128 | if (frag == null) {
129 | continue;
130 | }
131 | if (tag == null || !tag.equals(frag.getTag())) {
132 | ft.remove(frag);
133 | }
134 | }
135 | ft.commit();
136 | }
137 |
138 | @Override
139 | public void onTilesFinished() {
140 | showGreetings();
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/view/GreetingsFragment.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.view;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.support.v7.widget.Toolbar;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 |
11 | import com.yalantis.starwarsdemo.R;
12 | import com.yalantis.starwarsdemo.interfaces.GreetingFragmentInterface;
13 |
14 | import butterknife.Bind;
15 | import butterknife.ButterKnife;
16 | import butterknife.OnClick;
17 |
18 | /**
19 | * Created by Artem Kholodnyi on 11/17/15.
20 | */
21 | public class GreetingsFragment extends Fragment {
22 | public final static String TAG = GreetingsFragment.class.getCanonicalName();
23 |
24 | @Bind(R.id.toolbar)
25 | Toolbar mToolbar;
26 |
27 | private GreetingFragmentInterface mListener;
28 |
29 | public static GreetingsFragment newInstance() {
30 | return new GreetingsFragment();
31 | }
32 |
33 | @Override
34 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
35 | super.onActivityCreated(savedInstanceState);
36 | mListener = (GreetingFragmentInterface) getActivity();
37 | }
38 |
39 | @Nullable
40 | @Override
41 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
42 | View view = inflater.inflate(R.layout.fragment_greetings, container, false);
43 | ButterKnife.bind(this, view);
44 | return view;
45 | }
46 |
47 | @Override
48 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
49 | super.onViewCreated(view, savedInstanceState);
50 | mToolbar.setNavigationIcon(R.drawable.ic_menu);
51 | }
52 |
53 | @Override
54 | public void onDestroyView() {
55 | super.onDestroyView();
56 | ButterKnife.unbind(this);
57 | }
58 |
59 | @OnClick(R.id.btn_setup_profile)
60 | void onClick() {
61 | mListener.onSetupProfileClick();
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/view/SideFragment.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.view;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ObjectAnimator;
5 | import android.app.Activity;
6 | import android.graphics.Rect;
7 | import android.os.Build;
8 | import android.os.Bundle;
9 | import android.support.annotation.StyleRes;
10 | import android.support.design.widget.AppBarLayout;
11 | import android.support.design.widget.CollapsingToolbarLayout;
12 | import android.support.v4.app.Fragment;
13 | import android.support.v7.view.ContextThemeWrapper;
14 | import android.support.v7.widget.LinearLayoutManager;
15 | import android.support.v7.widget.RecyclerView;
16 | import android.support.v7.widget.SwitchCompat;
17 | import android.support.v7.widget.Toolbar;
18 | import android.view.LayoutInflater;
19 | import android.view.MenuItem;
20 | import android.view.View;
21 | import android.view.ViewAnimationUtils;
22 | import android.view.ViewGroup;
23 | import android.view.animation.AccelerateInterpolator;
24 | import android.widget.ImageView;
25 |
26 | import com.yalantis.starwars.TilesFrameLayout;
27 | import com.yalantis.starwars.interfaces.TilesFrameLayoutListener;
28 | import com.yalantis.starwarsdemo.R;
29 | import com.yalantis.starwarsdemo.adapter.ProfileAdapter;
30 | import com.yalantis.starwarsdemo.interfaces.DemoActivityInterface;
31 | import com.yalantis.starwarsdemo.interfaces.ProfileAdapterListener;
32 | import com.yalantis.starwarsdemo.interfaces.TilesRendererInterface;
33 | import com.yalantis.starwarsdemo.model.User;
34 | import com.yalantis.starwarsdemo.widget.ClipRevealFrame;
35 |
36 | import butterknife.Bind;
37 | import butterknife.ButterKnife;
38 | import butterknife.OnClick;
39 |
40 | /**
41 | * Created by Artem Kholodnyi on 11/19/15.
42 | */
43 | public abstract class SideFragment extends Fragment implements ProfileAdapterListener,
44 | TilesFrameLayoutListener {
45 | public static final String ARG_CX = "cx";
46 | public static final String ARG_CY = "cy";
47 | public static final String ARG_SHOULD_EXPAND = "should expand";
48 | private static final long ANIM_DURATION = 250L;
49 | protected float mRadius;
50 | @Bind(R.id.recycler)
51 | RecyclerView mRecycler;
52 | @Bind(R.id.toolbar)
53 | Toolbar mToolbar;
54 | @Bind(R.id.header)
55 | ImageView mHeader;
56 | @Bind(R.id.tessellation_frame_layout)
57 | TilesFrameLayout mTilesFrameLayout;
58 | @Bind(R.id.collapsing_toolbar_layout)
59 | CollapsingToolbarLayout mCollapsingToolbarLayout;
60 | @Bind(R.id.app_bar_layout)
61 | AppBarLayout mAppBarLayout;
62 | private View mRootView;
63 | private Toolbar.OnMenuItemClickListener onMenuItemClickListener = new Toolbar.OnMenuItemClickListener() {
64 | @Override
65 | public boolean onMenuItemClick(final MenuItem item) {
66 | if (R.id.action_close == item.getItemId()) {
67 | doBreak();
68 | }
69 | return false;
70 | }
71 | };
72 | private TilesRendererInterface mTilesListener;
73 | private DemoActivityInterface mDemoActivityInterface;
74 |
75 | @Override
76 | public void onResume() {
77 | super.onResume();
78 | // mTilesFrameLayout.onResume();
79 | }
80 |
81 | @Override
82 | public void onPause() {
83 | super.onPause();
84 | mTilesFrameLayout.onPause();
85 | }
86 |
87 | @Override
88 | public void onAttach(Activity activity) {
89 | super.onAttach(activity);
90 | if (activity instanceof TilesRendererInterface) {
91 | if (activity instanceof TilesRendererInterface) {
92 | mTilesListener = (TilesRendererInterface) activity;
93 | }
94 | if (activity instanceof DemoActivityInterface) {
95 | mDemoActivityInterface = (DemoActivityInterface) activity;
96 | }
97 | }
98 | }
99 |
100 | abstract @StyleRes int getTheme();
101 |
102 | protected Animator createCheckoutRevealAnimator(final ClipRevealFrame view, int x, int y, float startRadius, float endRadius) {
103 | setMenuVisibility(false);
104 | Animator retAnimator;
105 | mRadius = endRadius;
106 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
107 | retAnimator = ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius);
108 | } else {
109 | view.setClipOutLines(true);
110 | view.setClipCenter(x, y);
111 | view.setClipRadius(startRadius);
112 |
113 | retAnimator = ObjectAnimator.ofFloat(view, "clipRadius", startRadius, endRadius);
114 | }
115 | retAnimator.setDuration(ANIM_DURATION);
116 | retAnimator.addListener(new Animator.AnimatorListener() {
117 | @Override
118 | public void onAnimationStart(Animator animation) {
119 |
120 | }
121 |
122 | @Override
123 | public void onAnimationEnd(Animator animation) {
124 | view.setClipOutLines(false);
125 | removeOldSideFragment();
126 | }
127 |
128 | @Override
129 | public void onAnimationCancel(Animator animation) {
130 |
131 | }
132 |
133 | @Override
134 | public void onAnimationRepeat(Animator animation) {
135 |
136 | }
137 | });
138 |
139 | retAnimator.setInterpolator(new AccelerateInterpolator(2.0f));
140 | return retAnimator;
141 | }
142 |
143 | private void removeOldSideFragment() {
144 | if (mDemoActivityInterface != null) {
145 | mDemoActivityInterface.removeAllFragmentExcept(getTagString());
146 | }
147 | }
148 |
149 | @Override
150 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
151 | mRootView = inflater.cloneInContext(new ContextThemeWrapper(getContext(), getTheme()))
152 | .inflate(R.layout.fragment_side, container, false);
153 | final Bundle args = getArguments();
154 | if (args != null) {
155 | mRootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
156 | @Override
157 | public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
158 | int oldRight, int oldBottom) {
159 | v.removeOnLayoutChangeListener(this);
160 | int cx = args.getInt("cx");
161 | int cy = args.getInt("cy");
162 | // get the hypothenuse so the mRadius is from one corner to the other
163 | float radius = (float) Math.hypot(right, bottom);
164 |
165 | // Hardware-supported clipPath()
166 | // http://developer.android.com/guide/topics/graphics/hardware-accel.html
167 | if (Build.VERSION.SDK_INT >= 18) {
168 | Animator reveal = createCheckoutRevealAnimator((ClipRevealFrame) v, cx, cy, 28f, radius);
169 | reveal.start();
170 | } else {
171 | removeOldSideFragment();
172 | }
173 | }
174 | });
175 | }
176 |
177 | ButterKnife.bind(this, mRootView);
178 | return mRootView;
179 | }
180 |
181 |
182 |
183 |
184 |
185 | @Override
186 | public void onViewCreated(View view, Bundle savedInstanceState) {
187 | super.onViewCreated(view, savedInstanceState);
188 | mTilesFrameLayout.setOnAnimationFinishedListener(this);
189 | mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
190 | mRecycler.setAdapter(new ProfileAdapter(getContext(), getUser(), this));
191 | setUpToolbar(mToolbar);
192 | mHeader.setImageResource(getUser().getPhotoRes());
193 | if (getArguments() != null) {
194 | mAppBarLayout.setExpanded(getArguments().getBoolean(ARG_SHOULD_EXPAND), false);
195 | }
196 | }
197 |
198 | @Override
199 | public void onDetach() {
200 | super.onDetach();
201 | mTilesListener = null;
202 | mDemoActivityInterface = null;
203 | }
204 |
205 | private void setUpToolbar(final Toolbar toolbar) {
206 | toolbar.setNavigationIcon(R.drawable.ic_menu);
207 | toolbar.setTitle(R.string.settings);
208 | toolbar.inflateMenu(R.menu.menu_star_wars);
209 | toolbar.setOnMenuItemClickListener(onMenuItemClickListener);
210 | }
211 |
212 | abstract User getUser();
213 |
214 |
215 |
216 | @Override
217 | public void onDestroyView() {
218 | super.onDestroyView();
219 | ButterKnife.unbind(this);
220 | }
221 |
222 | @Override
223 | public void onSideSwitch(SwitchCompat v) {
224 | Rect rect = new Rect();
225 | v.getGlobalVisibleRect(rect);
226 | final int cy = rect.centerY() - getStatusBarHeight();
227 | final int halfThumbWidth = v.getThumbDrawable().getIntrinsicWidth() / 2;
228 | final int cx;
229 |
230 | if (this instanceof BrightSideFragment && v.isChecked()) {
231 | cx = rect.right - halfThumbWidth;
232 | postGoToSide(cy, cx, "dark");
233 | } else if (!v.isChecked()){
234 | cx = rect.left + halfThumbWidth;
235 | postGoToSide(cy, cx, "bright");
236 | }
237 | }
238 |
239 | private void postGoToSide(final int cy, final int cx, final String side) {
240 | new android.os.Handler().post(new Runnable() {
241 | @Override
242 | public void run() {
243 | if (mDemoActivityInterface != null) {
244 | mDemoActivityInterface.goToSide(cx, cy, isAppBarExpanded(), side);
245 | }
246 | }
247 | });
248 | }
249 |
250 | private boolean isAppBarExpanded() {
251 | return mAppBarLayout != null && mAppBarLayout.getBottom() == mAppBarLayout.getHeight();
252 | }
253 |
254 | public int getStatusBarHeight() {
255 | int result = 0;
256 | int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
257 | if (resourceId > 0) {
258 | result = getResources().getDimensionPixelSize(resourceId);
259 | }
260 | return result;
261 | }
262 |
263 | public void doBreak() {
264 | if (mDemoActivityInterface != null) {
265 | mDemoActivityInterface.removeAllFragmentExcept(getTagString());
266 | }
267 | if (mTilesFrameLayout != null) {
268 | mTilesFrameLayout.startAnimation();
269 | }
270 | }
271 |
272 | @Override
273 | public void onAnimationFinished() {
274 | if (mTilesListener != null) {
275 | mTilesListener.onTilesFinished();
276 | }
277 | }
278 |
279 | public abstract String getTagString();
280 |
281 | @OnClick(R.id.btn_save)
282 | void onClick() {
283 | doBreak();
284 | }
285 | }
286 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/widget/BackgroundDrawableSwitchCompat.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.widget;
2 |
3 | import android.animation.ValueAnimator;
4 | import android.content.Context;
5 | import android.graphics.Canvas;
6 | import android.graphics.ColorFilter;
7 | import android.graphics.Rect;
8 | import android.graphics.drawable.Drawable;
9 | import android.graphics.drawable.LevelListDrawable;
10 | import android.support.annotation.IntRange;
11 | import android.support.v4.content.ContextCompat;
12 | import android.support.v7.widget.SwitchCompat;
13 | import android.util.AttributeSet;
14 | import android.view.animation.Animation;
15 | import android.view.animation.LinearInterpolator;
16 |
17 | import com.yalantis.starwarsdemo.R;
18 |
19 | /**
20 | * Created by Artem Kholodnyi on 11/17/15.
21 | */
22 | public class BackgroundDrawableSwitchCompat extends SwitchCompat {
23 | public static final int OPAQUE = 255;
24 | public static final int TRANSPARENT = 0;
25 |
26 | // Duration taken from SwitchCompat.java
27 | public static final int THUMB_ANIMATION_DURATION = 250;
28 |
29 | private int mFirstDrawableAlpha;
30 | private int mSecondDrawableAlpha;
31 | private TwoStatesTrackDrawable mTwoStatesDrawable;
32 | private ValueAnimator animator;
33 |
34 | public BackgroundDrawableSwitchCompat(Context context) {
35 | super(context);
36 | init(context);
37 | }
38 |
39 | public BackgroundDrawableSwitchCompat(Context context, AttributeSet attrs) {
40 | super(context, attrs);
41 | init(context);
42 | }
43 |
44 | public BackgroundDrawableSwitchCompat(Context context, AttributeSet attrs, int defStyleAttr) {
45 | super(context, attrs, defStyleAttr);
46 | init(context);
47 | }
48 |
49 | private void init(Context context) {
50 | Drawable offDrawable = ContextCompat.getDrawable(context, R.drawable.track_off);
51 | Drawable onDrawable = ContextCompat.getDrawable(context, R.drawable.track_on);
52 | mTwoStatesDrawable = new TwoStatesTrackDrawable(offDrawable, onDrawable);
53 | setTrackDrawable(mTwoStatesDrawable);
54 | }
55 |
56 | @Override
57 | public void setChecked(boolean checked) {
58 | super.setChecked(checked);
59 | if (mTwoStatesDrawable != null) {
60 | startAnimation(checked);
61 | }
62 | }
63 |
64 | public void setCheckedImmediate(boolean checked) {
65 | super.setChecked(checked);
66 | mTwoStatesDrawable.setBlend(checked ? OPAQUE : TRANSPARENT);
67 | }
68 |
69 | private class TwoStatesTrackDrawable extends LevelListDrawable {
70 | private final Drawable mFirstDrawable;
71 | private final Drawable mSecondDrawable;
72 | private Animation mBackgroundAnimator;
73 |
74 | public TwoStatesTrackDrawable(Drawable firstDrawable, Drawable secondDrawable) {
75 | mFirstDrawable = firstDrawable;
76 | mSecondDrawable = secondDrawable;
77 | }
78 |
79 | @Override
80 | public void draw(Canvas canvas) {
81 | mFirstDrawable.setAlpha(mFirstDrawableAlpha);
82 | mSecondDrawable.setAlpha(mSecondDrawableAlpha);
83 | int paddingX = getThumbDrawable().getIntrinsicWidth() / 4;
84 | int paddingY = getThumbDrawable().getIntrinsicHeight() / 4;
85 | Rect rect = new Rect(paddingX, paddingY, canvas.getWidth() - paddingX, canvas.getHeight() - paddingY);
86 |
87 | mFirstDrawable.setBounds(rect);
88 | mSecondDrawable.setBounds(rect);
89 |
90 | mFirstDrawable.draw(canvas);
91 | mSecondDrawable.draw(canvas);
92 | }
93 |
94 | @Override
95 | public void setAlpha(int alpha) {
96 |
97 | }
98 |
99 | @Override
100 | public void setColorFilter(ColorFilter colorFilter) {
101 |
102 | }
103 |
104 | @Override
105 | public int getOpacity() {
106 | return 0;
107 | }
108 |
109 | private void setBlend(@IntRange(from = TRANSPARENT, to = OPAQUE) int k) {
110 | mFirstDrawableAlpha = OPAQUE - k;
111 | mSecondDrawableAlpha = k;
112 | invalidate();
113 | }
114 | }
115 |
116 | private void startAnimation(boolean firstToSecond) {
117 | if (animator != null) {
118 | animator.cancel();
119 | }
120 | animator = ValueAnimator.ofInt(firstToSecond ? OPAQUE : TRANSPARENT, firstToSecond ? TRANSPARENT : OPAQUE);
121 | animator.setDuration(THUMB_ANIMATION_DURATION);
122 | animator.setInterpolator(new LinearInterpolator());
123 | // animator.setRepeatMode(ValueAnimator.RESTART);
124 | // animator.setRepeatCount(ValueAnimator.INFINITE);
125 |
126 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
127 | @Override
128 | public void onAnimationUpdate(ValueAnimator animation) {
129 | mFirstDrawableAlpha = (int) animation.getAnimatedValue();
130 | mSecondDrawableAlpha = OPAQUE - (int) animation.getAnimatedValue();
131 | invalidate();
132 | }
133 | });
134 |
135 | animator.start();
136 | }
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/widget/CenterTopImageView.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.widget;
2 |
3 | import android.content.Context;
4 | import android.graphics.Matrix;
5 | import android.util.AttributeSet;
6 | import android.widget.ImageView;
7 |
8 | /**
9 | * Created by Artem Kholodnyi on 11/23/15.
10 | */
11 | public class CenterTopImageView extends ImageView {
12 | private Matrix matrix = new Matrix();
13 |
14 | public CenterTopImageView(Context context) {
15 | super(context);
16 | init();
17 | }
18 |
19 | public CenterTopImageView(Context context, AttributeSet attrs) {
20 | super(context, attrs);
21 | init();
22 | }
23 |
24 | public CenterTopImageView(Context context, AttributeSet attrs, int defStyleAttr) {
25 | super(context, attrs, defStyleAttr);
26 | }
27 |
28 | private void init() {
29 | setScaleType(ScaleType.MATRIX);
30 | }
31 |
32 | @Override
33 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
34 | super.onLayout(changed, left, top, right, bottom);
35 | if (getDrawable() != null) {
36 | float k = (right - left) / (float)getDrawable().getIntrinsicWidth();
37 | matrix.setScale(k, k);
38 | setImageMatrix(matrix);
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/yalantis/starwarsdemo/widget/ClipRevealFrame.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo.widget;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Path;
6 | import android.util.AttributeSet;
7 | import android.widget.FrameLayout;
8 |
9 | /**
10 | * Created by Artem Kholodnyi on 11/17/15.
11 | */
12 | public class ClipRevealFrame extends FrameLayout {
13 | private Path mRevealPath;
14 | boolean mClipOutlines;
15 | float mCenterX;
16 | float mCenterY;
17 | float mRadius;
18 |
19 | public ClipRevealFrame(Context context) {
20 | super(context);
21 | init();
22 | }
23 |
24 | public ClipRevealFrame(Context context, AttributeSet attrs) {
25 | super(context, attrs);
26 | init();
27 | }
28 |
29 | public ClipRevealFrame(Context context, AttributeSet attrs, int defStyleAttr) {
30 | super(context, attrs, defStyleAttr);
31 | init();
32 | }
33 |
34 | private void init(){
35 | mRevealPath = new Path();
36 | mClipOutlines = false;
37 | setWillNotDraw(false);
38 | }
39 |
40 | public void setClipOutLines(boolean shouldClip){
41 | mClipOutlines = shouldClip;
42 | }
43 |
44 | public void setClipCenter(final int x, final int y){
45 | mCenterX = x;
46 | mCenterY = y;
47 | }
48 |
49 | public void setClipRadius(final float radius){
50 | mRadius = radius;
51 | invalidate();
52 | }
53 |
54 | @Override
55 | public void draw(Canvas canvas) {
56 | if(!mClipOutlines){
57 | super.draw(canvas);
58 | return;
59 | }
60 | final int state = canvas.save();
61 | mRevealPath.reset();
62 | mRevealPath.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);
63 | canvas.clipPath(mRevealPath);
64 | super.draw(canvas);
65 | canvas.restoreToCount(state);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/demo/src/main/res/anim/slide_downward.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
--------------------------------------------------------------------------------
/demo/src/main/res/anim/slide_upward.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-hdpi/anakin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-hdpi/anakin.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-hdpi/darth.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-hdpi/darth.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-hdpi/ic_close.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-hdpi/ic_close.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-hdpi/star_wars_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-hdpi/star_wars_logo.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-hdpi/track_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-hdpi/track_off.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-mdpi/anakin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-mdpi/anakin.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-v21/button_setup_profile.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
-
6 |
7 |
-
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | -
17 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xhdpi/anakin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xhdpi/anakin.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xhdpi/darth.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xhdpi/darth.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xhdpi/ic_close.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xhdpi/ic_close.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xhdpi/star_wars_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xhdpi/star_wars_logo.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xhdpi/track_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xhdpi/track_off.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xxhdpi/darth.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xxhdpi/darth.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xxhdpi/ic_close.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xxhdpi/ic_close.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xxhdpi/ic_menu.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xxhdpi/ic_menu.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xxhdpi/star_wars_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xxhdpi/star_wars_logo.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-xxhdpi/track_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable-xxhdpi/track_off.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/btn_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/button_setup_profile.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | -
6 |
8 |
9 |
10 |
11 |
12 |
13 | -
14 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/leia.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable/leia.jpg
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/star_wars_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable/star_wars_logo.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/track_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable/track_off.png
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/track_on.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/drawable/track_on.png
--------------------------------------------------------------------------------
/demo/src/main/res/layout/activity_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
18 |
19 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/fragment_greetings.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
16 |
24 |
25 |
41 |
42 |
59 |
60 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/fragment_side.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
18 |
19 |
24 |
25 |
29 |
30 |
38 |
39 |
46 |
47 |
53 |
54 |
55 |
56 |
57 |
58 |
69 |
70 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/fragment_star_wars.xml:
--------------------------------------------------------------------------------
1 |
6 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/item_profile_gender.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
20 |
21 |
26 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/item_profile_other.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
16 |
17 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/item_profile_side.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/demo/src/main/res/menu/menu_star_wars.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-nodpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/demo/src/main/res/mipmap-nodpi/ic_launcher.png
--------------------------------------------------------------------------------
/demo/src/main/res/raw/star_frag.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | uniform float u_Time;
4 | uniform vec2 u_Resolution;
5 |
6 | varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
7 | varying float v_Radius;
8 |
9 | void main()
10 | {
11 | // Render a star
12 | float color = smoothstep(1.0, 0.0, length(v_TexCoordinate - vec2(0.5)) / v_Radius);
13 | gl_FragColor = vec4(color);
14 | }
15 |
--------------------------------------------------------------------------------
/demo/src/main/res/raw/star_vert.glsl:
--------------------------------------------------------------------------------
1 | uniform mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix.
2 | uniform mat4 u_MVMatrix; // A constant representing the combined model/view matrix.
3 | uniform float u_Time;
4 | uniform vec2 u_Resolution;
5 |
6 | attribute vec4 a_Position; //initial
7 | attribute vec2 a_TexCoordinate;
8 | attribute vec4 a_Misc; //initial
9 |
10 | varying vec2 v_TexCoordinate;
11 | varying float v_Radius;
12 |
13 | #define RADIUS 3.5
14 |
15 | float rand( vec2 co )
16 | {
17 | return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
18 | }
19 |
20 | void rotate( in float angle, inout vec2 vector )
21 | {
22 | mat2 rotationMatrix = mat2( cos( angle ), -sin( angle ),
23 | sin( angle ), cos( angle ));
24 | vector *= rotationMatrix;
25 | }
26 |
27 | void main()
28 | {
29 | // Transform the vertex into eye space.
30 | //v_Position = vec3(u_MVMatrix * a_Position);
31 |
32 | float aspect = u_Resolution.x / u_Resolution.y;
33 |
34 | // Pass through the texture coordinate.
35 | v_TexCoordinate = a_TexCoordinate;
36 |
37 | vec2 centerPos = a_Position.xy;
38 |
39 | float f = mix(1.0, a_Misc.t, u_Time);
40 |
41 | centerPos *= mod(f, RADIUS);
42 |
43 | float size = a_Misc.s;
44 |
45 | size = mix(0.0, size, mod(f, RADIUS)/RADIUS);
46 |
47 | vec2 relativePos = vec2(
48 | (a_TexCoordinate.s - 0.5) * 2.0 * size,
49 | (a_TexCoordinate.t - 0.5) * 2.0 * size
50 | );
51 |
52 | vec2 v = vec2(0.0, 1.0);
53 |
54 |
55 | vec4 pos = vec4(
56 | relativePos + centerPos,
57 | 0.0,
58 | 1.0
59 | );
60 |
61 |
62 | gl_Position = u_MVPMatrix * pos;
63 |
64 | v_Radius = size * 2.5;
65 | }
66 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #ffc2b65d
5 | #80c2b65d
6 | #f8f8f8
7 | #141217
8 | #090710
9 | #80000000
10 |
11 | #acacac
12 | @color/darkish
13 | #504F51
14 | #cacaca
15 |
16 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 | 200dp
7 | 256dp
8 | 8dp
9 | 48dp
10 |
11 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Star Wars
3 | StarWarsActivity
4 | Settings
5 | Setup your profile
6 | greetings,\nmy young padawan
7 | Settings
8 | Close
9 | Dark side
10 | Light side
11 | Female
12 | Male
13 |
14 | Full Name
15 | Homeworld
16 | Birthday
17 | Gender
18 | Save
19 |
20 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
15 |
16 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
49 |
50 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
65 |
66 |
72 |
73 |
76 |
77 |
80 |
81 |
--------------------------------------------------------------------------------
/demo/src/test/java/com/yalantis/starwars/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwarsdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Feb 18 12:33:48 EET 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '23.0.2'
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 23
10 | versionCode 2
11 | versionName "0.1.1"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | resourcePrefix "sw_"
20 | }
21 |
22 | dependencies {
23 | compile 'com.android.support:support-annotations:23.1.1'
24 | compile 'com.jakewharton.timber:timber:4.1.0'
25 | }
26 |
--------------------------------------------------------------------------------
/library/library.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /home/artem/Android/Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/Const.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars;
2 |
3 | /**
4 | * Started by Artem Kholodnyi on 11/1/15 12:55 PM -- com.yalantis.com.yalantis.starwars.util.gl
5 | */
6 | public class Const {
7 | public static final int BYTES_PER_FLOAT = 4 ;
8 |
9 | public static final int POSITION_DATA_SIZE = 3;
10 | public static final int NORMALS_DATA_SIZE = 3;
11 | public static final int TEXTURE_COORDS_DATA_SIZE = 2;
12 | public static final int TILE_DATA_SIZE = 3;
13 |
14 | public static final int POINTS_PER_TILE = 6;
15 |
16 | public static final float PLANE_HEIGHT = 5.0f; // arbitrary number
17 | }
18 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/TilesFrameLayout.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.ActivityManager;
5 | import android.content.Context;
6 | import android.content.pm.ConfigurationInfo;
7 | import android.content.res.TypedArray;
8 | import android.graphics.Bitmap;
9 | import android.graphics.Canvas;
10 | import android.graphics.Color;
11 | import android.graphics.PixelFormat;
12 | import android.opengl.GLSurfaceView;
13 | import android.os.Build;
14 | import android.os.Handler;
15 | import android.os.Looper;
16 | import android.util.AttributeSet;
17 | import android.widget.FrameLayout;
18 |
19 | import com.yalantis.starwars.interfaces.TilesFrameLayoutListener;
20 | import com.yalantis.starwars.render.StarWarsRenderer;
21 | import com.yalantis.starwars.widget.StarWarsTilesGLSurfaceView;
22 |
23 | /**
24 | * Created by Artem Kholodnyi on 11/9/15.
25 | */
26 | public class TilesFrameLayout extends FrameLayout {
27 |
28 | private StarWarsTilesGLSurfaceView mGLSurfaceView;
29 | private StarWarsRenderer mRenderer;
30 | private TilesFrameLayoutListener mListener;
31 | private int mAnimationDuration;
32 | private int mNumberOfTilesX;
33 |
34 | public TilesFrameLayout(Context context) {
35 | super(context);
36 | init(context, null, 0);
37 | }
38 |
39 | public TilesFrameLayout(Context context, AttributeSet attrs) {
40 | super(context, attrs);
41 | init(context, attrs, 0);
42 | }
43 |
44 | public TilesFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
45 | super(context, attrs, defStyleAttr);
46 | init(context, attrs, defStyleAttr);
47 | }
48 |
49 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
50 | public TilesFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
51 | super(context, attrs, defStyleAttr, defStyleRes);
52 | init(context, attrs, defStyleAttr);
53 | }
54 |
55 |
56 | private void init(Context context, AttributeSet attrs, int defStyle) {
57 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.sw_StarWarsTilesFrameLayout, defStyle, 0);
58 |
59 | mAnimationDuration = a.getInt(R.styleable.sw_StarWarsTilesFrameLayout_sw_animationDuration, 1500);
60 | mNumberOfTilesX = a.getInt(R.styleable.sw_StarWarsTilesFrameLayout_sw_numberOfTilesX, 35);
61 |
62 | a.recycle();
63 | initGlSurfaceView();
64 | }
65 |
66 | @Override
67 | protected void onFinishInflate() {
68 | super.onFinishInflate();
69 | addView(mGLSurfaceView);
70 | }
71 |
72 | @Override
73 | protected void onDraw(Canvas canvas) {
74 | super.onDraw(canvas);
75 | }
76 |
77 | private void initGlSurfaceView() {
78 | mGLSurfaceView = new StarWarsTilesGLSurfaceView(getContext());
79 | mGLSurfaceView.setBackgroundColor(Color.TRANSPARENT);
80 |
81 | // Check if the system supports OpenGL ES 2.0.
82 | final ActivityManager activityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
83 | final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
84 | final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
85 |
86 | if (supportsEs2) {
87 | // Request an OpenGL ES 2.0 compatible context.
88 | mGLSurfaceView.setEGLContextClientVersion(2);
89 |
90 | mRenderer = new StarWarsRenderer(mGLSurfaceView, this, mAnimationDuration, mNumberOfTilesX);
91 | mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
92 | mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
93 | mGLSurfaceView.setRenderer(mRenderer);
94 | mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
95 | mGLSurfaceView.setZOrderOnTop(true);
96 | } else {
97 | throw new UnsupportedOperationException();
98 | }
99 | }
100 |
101 | public void startAnimation() {
102 | drawToTexture();
103 | }
104 |
105 | private void drawToTexture() {
106 | Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);
107 | Canvas canvas = new Canvas(bitmap);
108 | super.draw(canvas);
109 | mRenderer.updateTexture(bitmap);
110 | }
111 |
112 | public void reveal() {
113 | mGLSurfaceView.queueEvent(new Runnable() {
114 | @Override
115 | public void run() {
116 | new Handler(Looper.getMainLooper()).post(new Runnable() {
117 | @Override
118 | public void run() {
119 | mRenderer.startAnimation();
120 | removeViewAt(0);
121 | }
122 | });
123 | }
124 | });
125 | }
126 |
127 | @Override
128 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
129 | super.onLayout(changed, left, top, right, bottom);
130 | }
131 |
132 | public void onResume() {
133 | mGLSurfaceView.onResume();
134 | }
135 |
136 | public void onPause() {
137 | mGLSurfaceView.onPause();
138 | }
139 |
140 | public void onAnimationFinished() {
141 | if (mListener != null) {
142 | mListener.onAnimationFinished();
143 | }
144 | }
145 |
146 | public void setOnAnimationFinishedListener(TilesFrameLayoutListener listener) {
147 | this.mListener = listener;
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/interfaces/Renderable.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.interfaces;
2 |
3 | /**
4 | * Started by Artem Kholodnyi on 11/1/15 12:41 PM
5 | */
6 | public interface Renderable {
7 | void render();
8 | void release();
9 | }
10 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/interfaces/ShaderType.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.interfaces;
2 |
3 | import android.opengl.GLES20;
4 | import android.support.annotation.IntDef;
5 |
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 |
9 | /**
10 | * Started by Artem Kholodnyi on 11/1/15 12:25 PM
11 | */
12 | @Retention(RetentionPolicy.SOURCE)
13 | @IntDef({GLES20.GL_VERTEX_SHADER, GLES20.GL_FRAGMENT_SHADER})
14 | public @interface ShaderType {
15 | }
16 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/interfaces/TilesFrameLayoutListener.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.interfaces;
2 |
3 | /**
4 | * Started by Artem Kholodnyi on 11/22/15 11:58 AM -- com.yalantis.com.yalantis.starwars.widget
5 | */
6 | public interface TilesFrameLayoutListener {
7 | /**
8 | * Called after tiles breaking animation is finished.
9 | * You may want to remove your view or fragment here
10 | */
11 | void onAnimationFinished();
12 | }
13 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/render/Buffers.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.render;
2 |
3 | import com.yalantis.starwars.Const;
4 |
5 | import java.nio.ByteBuffer;
6 | import java.nio.ByteOrder;
7 | import java.nio.FloatBuffer;
8 |
9 | /**
10 | * Started by Artem Kholodnyi on 11/1/15 12:48 PM
11 | */
12 | public class Buffers {
13 |
14 | public static FloatBuffer makeInterleavedBuffer(
15 | float[] positionData,
16 | float[] normals,
17 | float[] uvData,
18 | float[] tileXyData,
19 | int tiles) {
20 |
21 | int dataLength = positionData.length + normals.length * tiles + uvData.length + tileXyData.length;
22 |
23 | final FloatBuffer interleavedBuffer = ByteBuffer.allocateDirect(dataLength * Const.BYTES_PER_FLOAT)
24 | .order(ByteOrder.nativeOrder()).asFloatBuffer();
25 |
26 | int positionOffset = 0, normalOffset = 0, textureOffset = 0, tileXyOffset = 0;
27 |
28 | for (int i = 0; i < tiles; i++) {
29 | for (int j = 0; j < Const.POINTS_PER_TILE; j++) {
30 | interleavedBuffer.put(positionData, positionOffset, Const.POSITION_DATA_SIZE);
31 | positionOffset += Const.POSITION_DATA_SIZE;
32 |
33 | interleavedBuffer.put(normals, normalOffset, Const.NORMALS_DATA_SIZE);
34 | // Normals are the same
35 |
36 | interleavedBuffer.put(uvData, textureOffset, Const.TEXTURE_COORDS_DATA_SIZE);
37 | textureOffset += Const.TEXTURE_COORDS_DATA_SIZE;
38 |
39 | interleavedBuffer.put(tileXyData, tileXyOffset, Const.TILE_DATA_SIZE);
40 | tileXyOffset += Const.TILE_DATA_SIZE;
41 | }
42 | }
43 |
44 |
45 | interleavedBuffer.position(0);
46 | return interleavedBuffer;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/render/GenerateVerticesData.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.render;
2 |
3 | import com.yalantis.starwars.Const;
4 |
5 | /**
6 | * Created by Artem Kholodnyi on 11/2/15.
7 | */
8 | @SuppressWarnings("ForLoopReplaceableByForEach")
9 | public class GenerateVerticesData implements Runnable {
10 |
11 | private final StarWarsRenderer mStarWarsRenderer;
12 |
13 | public GenerateVerticesData(StarWarsRenderer starWarsRenderer) {
14 | mStarWarsRenderer = starWarsRenderer;
15 | }
16 |
17 | @Override
18 | public void run() {
19 | // X, Y, Z
20 | final float[] normalData = {
21 | // Tile
22 | 0.0f, 0.0f, -1.0f,
23 | 0.0f, 0.0f, -1.0f,
24 | 0.0f, 0.0f, -1.0f,
25 | 0.0f, 0.0f, -1.0f,
26 | 0.0f, 0.0f, -1.0f,
27 | 0.0f, 0.0f, -1.0f
28 | };
29 |
30 | final float[] positionData = genPositionData();
31 |
32 | final float[] uvData = genUvData();
33 |
34 | final float[] tileXYData = genTileData();
35 |
36 | // Run on the GL thread -- the same thread the other members of the renderer run in.
37 | mStarWarsRenderer.mGlSurfaceView.queueEvent(new Runnable() {
38 | @Override
39 | public void run() {
40 | if (mStarWarsRenderer.mPlane != null) {
41 | mStarWarsRenderer.mPlane.release();
42 | mStarWarsRenderer.mPlane = null;
43 | }
44 |
45 | mStarWarsRenderer.mPlane = new StarWarsTiles(
46 | mStarWarsRenderer,
47 | Buffers.makeInterleavedBuffer(
48 | positionData,
49 | normalData,
50 | uvData,
51 | tileXYData,
52 | mStarWarsRenderer.sizeX * mStarWarsRenderer.sizeY
53 | )
54 | );
55 |
56 | }
57 | });
58 | }
59 |
60 | private float[] genTileData() {
61 | float[] tileData = new float[6 * Const.TILE_DATA_SIZE * mStarWarsRenderer.getTilesCount()];
62 |
63 | int tileDataOffset = 0;
64 | for (int x = 0; x < mStarWarsRenderer.sizeX; x++) {
65 | for (int y = 0; y < mStarWarsRenderer.sizeY; y++) {
66 |
67 | float[] thisTileData = new float [6 * Const.TILE_DATA_SIZE];
68 |
69 | float rand = (float) Math.random();
70 | for(int v = 0; v < 6 * Const.TILE_DATA_SIZE; v+= Const.TILE_DATA_SIZE) {
71 | thisTileData[v] = x;
72 | thisTileData[v + 1] = y;
73 | thisTileData[v + 2] = rand;
74 | }
75 |
76 | System.arraycopy(
77 | thisTileData, 0,
78 | tileData, tileDataOffset,
79 | thisTileData.length
80 | );
81 |
82 | tileDataOffset += thisTileData.length;
83 | }
84 | }
85 |
86 | return tileData;
87 | }
88 |
89 | private float[] genPositionData() {
90 | float[] positionData = new float[6 * Const.POSITION_DATA_SIZE * mStarWarsRenderer.sizeX * mStarWarsRenderer.sizeY];
91 |
92 | float height = Const.PLANE_HEIGHT;
93 | float width = height * mStarWarsRenderer.ratio;
94 |
95 | final float stepX = width * 2f / mStarWarsRenderer.sizeX;
96 | final float stepY = height * 2f / mStarWarsRenderer.sizeY;
97 |
98 | final float minPositionX = -width;
99 | final float minPositionY = -height;
100 | final float z = 0f;
101 |
102 | int positionDataOffset = 0;
103 | for (int x = 0; x < mStarWarsRenderer.sizeX; x++) {
104 | for (int y = 0; y < mStarWarsRenderer.sizeY; y++) {
105 | final float x1 = minPositionX + x * stepX;
106 | final float x2 = x1 + stepX;
107 |
108 | final float y1 = minPositionY + y * stepY;
109 | final float y2 = y1 + stepY;
110 |
111 | // Define points for a plane.
112 | final float[] p1 = {x1, y2, z};
113 | final float[] p2 = {x2, y2, z};
114 | final float[] p3 = {x1, y1, z};
115 | final float[] p4 = {x2, y1, z};
116 |
117 | int elementsPerPoint = p1.length;
118 | final int size = elementsPerPoint * 6;
119 | final float[] thisPositionData = new float[size];
120 |
121 | int offset = 0;
122 | // Build the triangles
123 | // 1---2
124 | // | / |
125 | // 3---4
126 | for (int i = 0; i < elementsPerPoint; i++) {
127 | thisPositionData[offset++] = p1[i];
128 | }
129 | for (int i = 0; i < elementsPerPoint; i++) {
130 | thisPositionData[offset++] = p3[i];
131 | }
132 | for (int i = 0; i < elementsPerPoint; i++) {
133 | thisPositionData[offset++] = p2[i];
134 | }
135 | for (int i = 0; i < elementsPerPoint; i++) {
136 | thisPositionData[offset++] = p3[i];
137 | }
138 | for (int i = 0; i < elementsPerPoint; i++) {
139 | thisPositionData[offset++] = p4[i];
140 | }
141 | for (int i = 0; i < elementsPerPoint; i++) {
142 | thisPositionData[offset++] = p2[i];
143 | }
144 |
145 | System.arraycopy(
146 | thisPositionData, 0,
147 | positionData, positionDataOffset,
148 | thisPositionData.length
149 | );
150 | positionDataOffset += thisPositionData.length;
151 | }
152 | }
153 |
154 | return positionData;
155 | }
156 |
157 | private float[] genUvData() {
158 | float[] uvData = new float[6 * Const.TEXTURE_COORDS_DATA_SIZE *
159 | mStarWarsRenderer.sizeX * mStarWarsRenderer.sizeY];
160 | int uvDataOffset = 0;
161 |
162 | final float stepX = 1f / mStarWarsRenderer.sizeX;
163 | final float stepY = 1f / mStarWarsRenderer.sizeY;
164 |
165 | for (int x = mStarWarsRenderer.sizeX - 1; x >= 0; x--) {
166 | for (int y = mStarWarsRenderer.sizeY - 1; y >= 0; y--) {
167 | final float u0 = x * stepX;
168 | final float v0 = y * stepY;
169 | final float u1 = u0 + stepX;
170 | final float v1 = v0 + stepY;
171 |
172 | final int elementsPerPoint = 2;
173 | final int size = elementsPerPoint * 6;
174 | final float[] thisUvData = new float[size];
175 |
176 | int offset = 0;
177 | // Build the triangles
178 | // 1---2
179 | // | / |
180 | // 3---4
181 | // Define points for a plane.
182 |
183 | final float[] p1 = {u1, v0};
184 | final float[] p2 = {u0, v0};
185 | final float[] p3 = {u1, v1};
186 | final float[] p4 = {u0, v1};
187 |
188 | for (int i = 0; i < elementsPerPoint; i++)
189 | thisUvData[offset++] = p1[i];
190 | for (int i = 0; i < elementsPerPoint; i++)
191 | thisUvData[offset++] = p3[i];
192 | for (int i = 0; i < elementsPerPoint; i++)
193 | thisUvData[offset++] = p2[i];
194 |
195 | for (int i = 0; i < elementsPerPoint; i++)
196 | thisUvData[offset++] = p3[i];
197 | for (int i = 0; i < elementsPerPoint; i++)
198 | thisUvData[offset++] = p4[i];
199 | for (int i = 0; i < elementsPerPoint; i++)
200 | thisUvData[offset++] = p2[i];
201 |
202 | System.arraycopy(
203 | thisUvData, 0,
204 | uvData, uvDataOffset,
205 | thisUvData.length
206 | );
207 | uvDataOffset += thisUvData.length;
208 | }
209 | }
210 |
211 | return uvData;
212 | }
213 |
214 | }
215 |
216 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/render/StarWarsRenderer.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.render;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ValueAnimator;
5 | import android.graphics.Bitmap;
6 | import android.opengl.GLES20;
7 | import android.opengl.GLSurfaceView;
8 | import android.opengl.Matrix;
9 | import android.os.Handler;
10 | import android.os.Looper;
11 | import android.view.animation.AccelerateDecelerateInterpolator;
12 |
13 | import com.yalantis.starwars.Const;
14 | import com.yalantis.starwars.R;
15 | import com.yalantis.starwars.TilesFrameLayout;
16 | import com.yalantis.starwars.utils.gl.RawResourceReader;
17 | import com.yalantis.starwars.utils.gl.ShaderHelper;
18 | import com.yalantis.starwars.utils.gl.TextureHelper;
19 | import com.yalantis.starwars.widget.StarWarsTilesGLSurfaceView;
20 |
21 | import java.util.concurrent.Executors;
22 |
23 | import javax.microedition.khronos.egl.EGLConfig;
24 | import javax.microedition.khronos.opengles.GL10;
25 |
26 | import timber.log.Timber;
27 |
28 |
29 | /**
30 | * Created by Artem Kholodnyi on 11/2/15.
31 | */
32 | public class StarWarsRenderer implements GLSurfaceView.Renderer {
33 | /** Store the accumulated rotation. */
34 | private final float[] mAccumulatedRotation = new float[16];
35 | /** Store the current rotation. */
36 | private final float[] mCurrentRotation = new float[16];
37 | public StarWarsTiles mPlane;
38 | public GLSurfaceView mGlSurfaceView;
39 | public float ratio;
40 | public int mvpMatrixHandle;
41 | public int mvMatrixHandle;
42 | public int textureUniformHandle;
43 | public int positionHandle;
44 | public int normalHandle;
45 | public int textureCoordinateHandle;
46 | public int programHandle;
47 | public int tileXyHandle;
48 | public int deltaPosHandle;
49 | public int sizeX;
50 | public int sizeY;
51 | private TilesFrameLayout mListener;
52 | private int mNumberOfTilesX = 35;
53 | /**
54 | * Store the model matrix. This matrix is used to move models from object space (where each model can be thought
55 | * of being located at the center of the universe) to world space.
56 | */
57 | private float[] mModelMatrix = new float[16];
58 | /**
59 | * Store the view matrix. This can be thought of as our camera. This matrix transforms world space to eye space;
60 | * it positions things relative to our eye.
61 | */
62 | private float[] mViewMatrix = new float[16];
63 | /** Store the projection matrix. This is used to project the scene onto a 2D viewport. */
64 | private float[] mProjectionMatrix = new float[16];
65 | /** Allocate storage for the final combined matrix. This will be passed into the shader program. */
66 | private float[] mMVPMatrix = new float[16];
67 | private int mAndroidDataHandle;
68 | private float[] mTemporaryMatrix = new float[16];
69 | private int frames;
70 | private long startTime;
71 |
72 | private float deltaPosX;
73 | private long timePassed;
74 | private ValueAnimator animator;
75 | private long mAnimationDuration;
76 | private boolean requestedReveal;
77 |
78 |
79 | public StarWarsRenderer(StarWarsTilesGLSurfaceView glSurfaceView,
80 | TilesFrameLayout TilesFrameLayout,
81 | int animationDuration, int numberOfTilesX) {
82 | mGlSurfaceView = glSurfaceView;
83 | mListener = TilesFrameLayout;
84 | mAnimationDuration = animationDuration;
85 | mNumberOfTilesX = numberOfTilesX;
86 | }
87 |
88 | @Override
89 | public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
90 | GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
91 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
92 |
93 |
94 | // Use culling to remove back faces.
95 | GLES20.glEnable(GLES20.GL_CULL_FACE);
96 | GLES20.glFrontFace(GLES20.GL_CW);
97 |
98 | // Enable depth testing
99 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
100 |
101 | // Position the eye in front of the origin.
102 | final float eyeX = 0.0f;
103 | final float eyeY = 0.0f;
104 | final float eyeZ = 0.0f;
105 |
106 | // We are looking toward the distance
107 | final float lookX = 0.0f;
108 | final float lookY = 0.0f;
109 | final float lookZ = 1.0f;
110 |
111 | // Set our up vector. This is where our head would be pointing were we holding the camera.
112 | final float upX = 0.0f;
113 | final float upY = 1.0f;
114 | final float upZ = 0.0f;
115 |
116 | Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
117 |
118 | final String vertexShader = RawResourceReader.readTextFileFromRawResource(mGlSurfaceView.getContext(), R.raw.tiles_vert);
119 | final String fragmentShader = RawResourceReader.readTextFileFromRawResource(mGlSurfaceView.getContext(), R.raw.tiles_frag);
120 |
121 | final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
122 | final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
123 |
124 | programHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
125 | new String[]{"a_Position", "a_Normal", "a_TexCoordinate"});
126 |
127 | // Initialize the accumulated rotation matrix
128 | Matrix.setIdentityM(mAccumulatedRotation, 0);
129 | }
130 |
131 | private void genTilesData() {
132 | Executors.newSingleThreadExecutor().submit(new GenerateVerticesData(this));
133 | }
134 |
135 | @Override
136 | public void onSurfaceChanged(GL10 unused, int width, int height) {
137 | sizeX = mNumberOfTilesX;
138 | sizeY = height * sizeX / width;
139 |
140 | // Set the OpenGL viewport to the same size as the surface.
141 | GLES20.glViewport(0, 0, width, height);
142 |
143 | // Create a new perspective projection matrix. The height will stay the same
144 | // while the width will vary as per aspect ratio.
145 | final float ratio = (float) width / height;
146 |
147 | final float left = -ratio;
148 | final float right = ratio;
149 | final float bottom = -1.0f;
150 | final float top = 1.0f;
151 | final float near = 1.0f;
152 | final float far = 10.0f;
153 |
154 | this.ratio = ratio;
155 |
156 | Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
157 |
158 | genTilesData();
159 | }
160 |
161 | @Override
162 | public void onDrawFrame(GL10 gl10) {
163 | logFrame();
164 | drawGl();
165 | if (!requestedReveal && mAndroidDataHandle > 0) {
166 | requestedReveal = true;
167 | mListener.reveal();
168 | }
169 | }
170 |
171 | private void drawGl() {
172 | GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
173 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
174 | if (mAndroidDataHandle > 0) {
175 |
176 | GLES20.glUseProgram(programHandle);
177 |
178 | // Set program handles
179 | mvpMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
180 | mvMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix");
181 | textureUniformHandle = GLES20.glGetUniformLocation(programHandle, "u_Texture");
182 | deltaPosHandle = GLES20.glGetUniformLocation(programHandle, "u_DeltaPos");
183 |
184 | positionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
185 | normalHandle = GLES20.glGetAttribLocation(programHandle, "a_Normal");
186 | textureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "a_TexCoordinate");
187 | tileXyHandle = GLES20.glGetAttribLocation(programHandle, "a_TileXY");
188 |
189 | Matrix.setIdentityM(mModelMatrix, 0);
190 | Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, 5f);
191 |
192 | // Set a matrix that contains the current rotation.
193 | Matrix.setIdentityM(mCurrentRotation, 0);
194 |
195 | Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
196 | System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
197 |
198 | Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
199 |
200 | // Pass in the modelview matrix.
201 | GLES20.glUniformMatrix4fv(mvMatrixHandle, 1, false, mMVPMatrix, 0);
202 |
203 | Matrix.multiplyMM(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
204 | System.arraycopy(mTemporaryMatrix, 0, mMVPMatrix, 0, 16);
205 |
206 | // Pass in the combined matrix.
207 | GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mMVPMatrix, 0);
208 |
209 | // Pass in u_Gravity
210 | GLES20.glUniform1f(deltaPosHandle, deltaPosX);
211 |
212 | // Pass in the texture information
213 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
214 |
215 | // Bind the texture to this unit.
216 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mAndroidDataHandle);
217 |
218 | GLES20.glUniform1i(textureUniformHandle, 0);
219 |
220 | if (mPlane != null) {
221 | mPlane.render();
222 | }
223 |
224 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
225 | }
226 | }
227 |
228 | public int getTilesCount() {
229 | return sizeX * sizeY;
230 | }
231 |
232 | public void logFrame() {
233 | frames++;
234 | timePassed = (System.nanoTime() - startTime) / 1_000_000;
235 | if(timePassed >= 1000) {
236 | Timber.d("%d tiles @ %d fps", getTilesCount(), frames);
237 | frames = 0;
238 | startTime = System.nanoTime();
239 | }
240 | }
241 |
242 | public void startAnimation() {
243 | new Handler(Looper.getMainLooper()).post(new Runnable() {
244 | @Override
245 | public void run() {
246 | animator = ValueAnimator.ofFloat(0, -Const.PLANE_HEIGHT * 2); // plane height
247 | animator.setDuration(mAnimationDuration);
248 | animator.setInterpolator(new AccelerateDecelerateInterpolator());
249 |
250 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
251 | @Override
252 | public void onAnimationUpdate(ValueAnimator animation) {
253 | float value = (float) animation.getAnimatedValue();
254 | deltaPosX = value;
255 | mGlSurfaceView.requestRender();
256 | }
257 | });
258 | animator.addListener(new Animator.AnimatorListener() {
259 | @Override
260 | public void onAnimationStart(Animator animation) {
261 | mGlSurfaceView.requestRender();
262 | }
263 |
264 | @Override
265 | public void onAnimationEnd(Animator animation) {
266 | mListener.onAnimationFinished();
267 | }
268 |
269 | @Override
270 | public void onAnimationCancel(Animator animation) {
271 |
272 | }
273 |
274 | @Override
275 | public void onAnimationRepeat(Animator animation) {
276 |
277 | }
278 | });
279 |
280 | animator.start();
281 |
282 | }
283 | });
284 | }
285 |
286 | public void updateTexture(final Bitmap bitmap) {
287 | mGlSurfaceView.queueEvent(new Runnable() {
288 | @Override
289 | public void run() {
290 | requestedReveal = false;
291 | mAndroidDataHandle = TextureHelper.loadTexture(bitmap);
292 | mGlSurfaceView.requestRender();
293 | }
294 | });
295 | }
296 |
297 | public void cancelAnimation() {
298 | if (animator != null && animator.isRunning()) {
299 | animator.removeAllListeners();
300 | animator.cancel();
301 | }
302 | }
303 | }
304 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/render/StarWarsTiles.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.render;
2 |
3 | import android.opengl.GLES20;
4 |
5 | import com.yalantis.starwars.Const;
6 | import com.yalantis.starwars.interfaces.Renderable;
7 |
8 | import java.nio.FloatBuffer;
9 |
10 | /**
11 | * Started by Artem Kholodnyi on 11/1/15 12:40 PM -- com.yalantis.com.yalantis.starwars
12 | */
13 | public class StarWarsTiles implements Renderable {
14 |
15 | private final int mBufferId;
16 | private final StarWarsRenderer mRenderer;
17 |
18 | public StarWarsTiles(StarWarsRenderer renderer, FloatBuffer vboBuffer) {
19 | this.mRenderer = renderer;
20 |
21 | // copy the buffer into OpenGL's memory
22 |
23 | final int buffers[] = new int[1];
24 | GLES20.glGenBuffers(1, buffers, 0);
25 |
26 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
27 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vboBuffer.capacity() * Const.BYTES_PER_FLOAT,
28 | vboBuffer, GLES20.GL_STATIC_DRAW);
29 |
30 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
31 |
32 | mBufferId = buffers[0];
33 |
34 | vboBuffer.limit(0);
35 | vboBuffer = null;
36 | }
37 |
38 | @Override
39 | public void render() {
40 | GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
41 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
42 |
43 | final int stride = Const.BYTES_PER_FLOAT
44 | * (Const.POSITION_DATA_SIZE
45 | + Const.NORMALS_DATA_SIZE
46 | + Const.TEXTURE_COORDS_DATA_SIZE
47 | + Const.TILE_DATA_SIZE);
48 |
49 | // Pass in the position information
50 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
51 | GLES20.glEnableVertexAttribArray(mRenderer.positionHandle);
52 | GLES20.glVertexAttribPointer(mRenderer.positionHandle,
53 | Const.POSITION_DATA_SIZE,
54 | GLES20.GL_FLOAT,
55 | false,
56 | stride,
57 | 0
58 | );
59 |
60 | // Pass in the normal information
61 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
62 | GLES20.glEnableVertexAttribArray(mRenderer.normalHandle);
63 | GLES20.glVertexAttribPointer(mRenderer.normalHandle,
64 | Const.NORMALS_DATA_SIZE,
65 | GLES20.GL_FLOAT,
66 | false,
67 | stride,
68 | Const.BYTES_PER_FLOAT * Const.POSITION_DATA_SIZE
69 | );
70 |
71 | // Pass in the texture information
72 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
73 | GLES20.glEnableVertexAttribArray(mRenderer.textureCoordinateHandle);
74 | GLES20.glVertexAttribPointer(mRenderer.textureCoordinateHandle,
75 | Const.TEXTURE_COORDS_DATA_SIZE,
76 | GLES20.GL_FLOAT,
77 | false,
78 | stride,
79 | Const.BYTES_PER_FLOAT * (Const.POSITION_DATA_SIZE + Const.NORMALS_DATA_SIZE)
80 | );
81 |
82 | // Pass in the tile x,y information
83 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferId);
84 | GLES20.glEnableVertexAttribArray(mRenderer.tileXyHandle);
85 | GLES20.glVertexAttribPointer(mRenderer.tileXyHandle,
86 | Const.TILE_DATA_SIZE,
87 | GLES20.GL_FLOAT,
88 | false,
89 | stride,
90 | Const.BYTES_PER_FLOAT * (Const.POSITION_DATA_SIZE + Const.NORMALS_DATA_SIZE + Const.TEXTURE_COORDS_DATA_SIZE)
91 | );
92 |
93 | // Clear the currently bound buffer (so future OpenGL calls do not use this buffer).
94 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
95 |
96 | // Draw tiles
97 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, mRenderer.sizeX * mRenderer.sizeY * 6);
98 | }
99 |
100 | @Override
101 | public void release() {
102 | // delete the buffer from OpenGL's memory
103 | final int[] buffersToDelete = new int[] {mBufferId};
104 | GLES20.glDeleteBuffers(buffersToDelete.length, buffersToDelete, 0);
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/utils/gl/RawResourceReader.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.utils.gl;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.annotation.RawRes;
7 |
8 | import java.io.BufferedReader;
9 | import java.io.IOException;
10 | import java.io.InputStreamReader;
11 |
12 | /**
13 | * Created by Artem Kholodnyi on 11/1/15 12:17 PM
14 | *
15 | */
16 | public class RawResourceReader {
17 |
18 | /**
19 | * Reads a raw resource text file into a String
20 | * @param context
21 | * @param resId
22 | * @return
23 | */
24 | @Nullable
25 | public static String readTextFileFromRawResource(@NonNull final Context context,
26 | @RawRes final int resId) {
27 |
28 | final BufferedReader bufferedReader = new BufferedReader(
29 | new InputStreamReader(context.getResources().openRawResource(resId))
30 | );
31 |
32 | String line;
33 | final StringBuilder body = new StringBuilder();
34 |
35 | try {
36 | while ((line = bufferedReader.readLine()) != null) {
37 | body.append(line).append('\n');
38 | }
39 | } catch (IOException e) {
40 | return null;
41 | }
42 |
43 | return body.toString();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/utils/gl/ShaderHelper.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.utils.gl;
2 |
3 | import android.opengl.GLES20;
4 | import android.util.Log;
5 |
6 | import com.yalantis.starwars.interfaces.ShaderType;
7 |
8 | import timber.log.Timber;
9 |
10 | public class ShaderHelper {
11 | private static final String TAG = "ShaderHelper";
12 |
13 | /**
14 | * Helper function to compile a shader.
15 | *
16 | * @param shaderType The shader type.
17 | * @param shaderSource The shader source code.
18 | * @return An OpenGL handle to the shader.
19 | */
20 | public static int compileShader(@ShaderType final int shaderType, final String shaderSource) {
21 | int shaderHandle = GLES20.glCreateShader(shaderType);
22 |
23 | if (shaderHandle != 0) {
24 | GLES20.glShaderSource(shaderHandle, shaderSource);
25 |
26 | GLES20.glCompileShader(shaderHandle);
27 |
28 | final int[] compilationStatus = new int[1];
29 | GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compilationStatus, 0);
30 |
31 | if (compilationStatus[0] == 0) {
32 | Timber.e("Error compiling shader %s", GLES20.glGetShaderInfoLog(shaderHandle));
33 | GLES20.glDeleteShader(shaderHandle);
34 | shaderHandle = 0;
35 | }
36 | }
37 |
38 | if (shaderHandle == 0) {
39 | throw new RuntimeException("Error creating shader.");
40 | }
41 |
42 | return shaderHandle;
43 | }
44 |
45 | /**
46 | * Helper function to compile and link a program.
47 | *
48 | * @param vertexShaderHandle An OpenGL handle to an already-compiled vertex shader.
49 | * @param fragmentShaderHandle An OpenGL handle to an already-compiled fragment shader.
50 | * @param attributes Attributes that need to be bound to the program.
51 | * @return An OpenGL handle to the program.
52 | */
53 | public static int createAndLinkProgram(final int vertexShaderHandle,
54 | final int fragmentShaderHandle,
55 | final String[] attributes) {
56 |
57 | int programHandle = GLES20.glCreateProgram();
58 |
59 | if (programHandle != 0) {
60 | // Bind the vertex shader to the program.
61 | GLES20.glAttachShader(programHandle, vertexShaderHandle);
62 |
63 | // Bind the fragment shader to the program.
64 | GLES20.glAttachShader(programHandle, fragmentShaderHandle);
65 |
66 | // Bind attributes
67 | if (attributes != null) {
68 | final int size = attributes.length;
69 | for (int i = 0; i < size; i++) {
70 | GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
71 | }
72 | }
73 |
74 | // Link the two shaders together into a program.
75 | GLES20.glLinkProgram(programHandle);
76 |
77 | // Get the link status.
78 | final int[] linkStatus = new int[1];
79 | GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
80 |
81 | // If the link failed, delete the program.
82 | if (linkStatus[0] == 0) {
83 | Log.e(TAG, "Error compiling program: " + GLES20.glGetProgramInfoLog(programHandle));
84 | GLES20.glDeleteProgram(programHandle);
85 | programHandle = 0;
86 | }
87 | }
88 |
89 | if (programHandle == 0) {
90 | throw new RuntimeException("Error creating program.");
91 | }
92 |
93 | return programHandle;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/utils/gl/TextureHelper.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.utils.gl;
2 |
3 | import android.graphics.Bitmap;
4 | import android.opengl.GLES20;
5 | import android.opengl.GLUtils;
6 |
7 | import timber.log.Timber;
8 |
9 | import static android.opengl.GLES20.GL_CLAMP_TO_EDGE;
10 | import static android.opengl.GLES20.GL_NEAREST;
11 | import static android.opengl.GLES20.GL_TEXTURE_2D;
12 | import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER;
13 | import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER;
14 | import static android.opengl.GLES20.GL_TEXTURE_WRAP_S;
15 | import static android.opengl.GLES20.GL_TEXTURE_WRAP_T;
16 | import static android.opengl.GLES20.glBindTexture;
17 | import static android.opengl.GLES20.glGenTextures;
18 | import static android.opengl.GLES20.glTexParameteri;
19 |
20 | public class TextureHelper {
21 |
22 | public static int loadTexture(final Bitmap bitmap) {
23 | final int[] textureHandle = new int[1];
24 |
25 | glGenTextures(1, textureHandle, 0);
26 |
27 | if (textureHandle[0] != 0) {
28 | // Bind to the texture in OpenGL
29 | glBindTexture(GL_TEXTURE_2D, textureHandle[0]);
30 |
31 | // Set filtering
32 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
33 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
34 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
35 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
36 |
37 | // Load the bitmap into the bound texture.
38 | GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
39 |
40 | // Recycle the bitmap, since its data has been loaded into OpenGL.
41 | bitmap.recycle();
42 | } else {
43 | int errorCode = GLES20.glGetError();
44 | String errorString = GLUtils.getEGLErrorString(errorCode);
45 | RuntimeException e = new RuntimeException(errorCode + " " + errorString);
46 | Timber.e(e, "");
47 | throw e;
48 | }
49 |
50 | return textureHandle[0];
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/library/src/main/java/com/yalantis/starwars/widget/StarWarsTilesGLSurfaceView.java:
--------------------------------------------------------------------------------
1 | package com.yalantis.starwars.widget;
2 |
3 | import android.content.Context;
4 | import android.opengl.GLSurfaceView;
5 | import android.util.AttributeSet;
6 |
7 | import com.yalantis.starwars.render.StarWarsRenderer;
8 |
9 | /**
10 | * Created by Artem Kholodnyi on 11/3/15.
11 | */
12 | public class StarWarsTilesGLSurfaceView extends GLSurfaceView {
13 | private StarWarsRenderer mRenderer;
14 |
15 | public StarWarsTilesGLSurfaceView(Context context) {
16 | super(context);
17 | }
18 |
19 | public StarWarsTilesGLSurfaceView(Context context, AttributeSet attrs) {
20 | super(context, attrs);
21 | }
22 |
23 | @Override
24 | public void onPause() {
25 | super.onPause();
26 | mRenderer.cancelAnimation();
27 | }
28 |
29 | public void setRenderer(StarWarsRenderer renderer) {
30 | super.setRenderer(renderer);
31 | mRenderer = renderer;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/library/src/main/res/raw/tiles_frag.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float; // Set the default precision to medium. We don't need as high of a precision in the fragment shader.
2 | uniform sampler2D u_Texture; // The input texture.
3 |
4 | varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
5 |
6 | void main()
7 | {
8 | gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
9 | }
10 |
--------------------------------------------------------------------------------
/library/src/main/res/raw/tiles_vert.glsl:
--------------------------------------------------------------------------------
1 | uniform mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix.
2 | uniform mat4 u_MVMatrix; // A constant representing the combined model/view matrix.
3 | uniform float u_DeltaPos;
4 |
5 | attribute vec4 a_Position; // Per-vertex position information we will pass in.
6 | attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in.
7 |
8 | // Contains tile's x and y position + random value at .z
9 | attribute vec3 a_TileXY;
10 |
11 | varying vec2 v_TexCoordinate; // This will be passed into the fragment shader.
12 |
13 | void main()
14 | {
15 | // Pass through the texture coordinate.
16 | v_TexCoordinate = a_TexCoordinate;
17 |
18 | // Tiles' elevation
19 | float z = - (a_TileXY.z - 0.5) * sin(u_DeltaPos/7.0) * 1.3;
20 |
21 | vec4 calcPos = a_Position;
22 |
23 | calcPos.z = z;
24 |
25 | float randomShift = a_TileXY.z;
26 |
27 | // Just some random perturbations
28 | calcPos.x += u_DeltaPos * (randomShift - 0.5) * 0.3;
29 | calcPos.y += u_DeltaPos + randomShift * sin(u_DeltaPos / 30.0) * 3.3;
30 |
31 | gl_Position = u_MVPMatrix * calcPos;
32 | }
33 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs_sw_tiles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Star Wars Animation
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':demo', ':library'
2 |
--------------------------------------------------------------------------------
/star_wars-concept.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Yalantis/StarWars.Android/a947b5a5b59e4543004af6c6ca08d5fa0451233a/star_wars-concept.gif
--------------------------------------------------------------------------------