1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view.shadow; 18 19 20 /** 21 * Model for spot shadow rendering. Assumes single light, single object. 22 */ 23 class SpotShadowConfig { 24 25 // No need to be final but making it immutable for now. 26 private final int mLightRadius; 27 28 29 // No need to be final but making it immutable for now. 30 private final float[] mPoly; 31 private final int mPolyLength; 32 33 private float[] mLightCoord; 34 35 private final float mShadowStrength; 36 SpotShadowConfig(SpotShadowConfig.Builder builder)37 private SpotShadowConfig(SpotShadowConfig.Builder builder) { 38 mLightRadius = builder.mLightRadius; 39 mPoly = builder.mPoly; 40 mPolyLength = builder.mPolyLength; 41 42 mLightCoord = new float[3]; 43 mLightCoord[0] = builder.mLightX; 44 mLightCoord[1] = builder.mLightY; 45 mLightCoord[2] = builder.mLightHeight; 46 mShadowStrength = builder.mShadowStrength; 47 } 48 49 /** 50 * @return size of the light source radius (light source is always generated as a circular shape) 51 */ getLightRadius()52 public int getLightRadius() { 53 return mLightRadius; 54 } 55 56 /** 57 * @return object that casts shadow. xyz coordinates. 58 */ getPoly()59 public float[] getPoly() { 60 return mPoly; 61 } 62 63 /** 64 * @return # of vertices in the object {@link #getPoly()} that casts shadow. 65 */ getPolyLength()66 public int getPolyLength() { 67 return mPolyLength; 68 } 69 70 /** 71 * Update the light source coord. 72 * @param x - horizontal coordinate 73 * @param y - vertical coordinate 74 */ setLightCoord(float x, float y)75 public void setLightCoord(float x, float y) { 76 mLightCoord[0] = x; 77 mLightCoord[1] = y; 78 } 79 80 /** 81 * @return shadow intensity from 0 to 1 82 */ getShadowStrength()83 public float getShadowStrength() { 84 return mShadowStrength; 85 } 86 getLightCoord()87 public float[] getLightCoord() { 88 return mLightCoord; 89 } 90 91 public static class Builder { 92 93 // No need to be final but making it immutable for now. 94 private int mLightRadius; 95 96 // No need to be final but making it immutable for now. 97 private float[] mPoly; 98 private int mPolyLength; 99 100 private float mLightX; 101 private float mLightY; 102 private float mLightHeight; 103 104 private float mShadowStrength; 105 106 /** 107 * @param shadowStrength from 0 to 1 108 */ setShadowStrength(float shadowStrength)109 public Builder setShadowStrength(float shadowStrength) { 110 this.mShadowStrength = shadowStrength; 111 return this; 112 } 113 setLightRadius(int mLightRadius)114 public Builder setLightRadius(int mLightRadius) { 115 this.mLightRadius = mLightRadius; 116 return this; 117 } 118 setPolygon(float[] poly, int polyLength)119 public Builder setPolygon(float[] poly, int polyLength) { 120 this.mPoly = poly; 121 this.mPolyLength = polyLength; 122 return this; 123 } 124 setLightCoord(float lightX, float lightY, float lightHeight)125 public Builder setLightCoord(float lightX, float lightY, float lightHeight) { 126 this.mLightX = lightX; 127 this.mLightY = lightY; 128 this.mLightHeight = lightHeight; 129 return this; 130 } 131 build()132 public SpotShadowConfig build() { 133 return new SpotShadowConfig(this); 134 } 135 } 136 137 }