Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension library draft #837

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions constraintlayout/extensionLibrary/Compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ConstraintLayout Extension Library for Jetpack Compose

[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-pager)](https://search.maven.org/search?q=g:com.google.accompanist)

> :warning: This library has been deprecated as official support is now available in Compose 1.4.0. Please see our [Migration Guide](https://google.github.io/accompanist/pager/) for how to migrate.

For more information, visit the documentation: https://google.github.io/accompanist/pager

## Download

```groovy
repositories {
mavenCentral()
}

dependencies {
implementation 'androidx.constraintlayout:constraintlayout-compose:1.1.0-alpha07'
}
```
130 changes: 130 additions & 0 deletions constraintlayout/extensionLibrary/Compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("UnstableApiUsage")

plugins {
id(libs.plugins.android.library.get().pluginId)
id(libs.plugins.android.kotlin.get().pluginId)
id(libs.plugins.jetbrains.dokka.get().pluginId)
id(libs.plugins.gradle.metalava.get().pluginId)
id(libs.plugins.vanniktech.maven.publish.get().pluginId)
}

kotlin {
explicitApi()
}

android {
namespace = "com.google.accompanist.pager"

compileSdk = 34

defaultConfig {
minSdk = 21
// targetSdkVersion has no effect for libraries. This is only used for the test APK
targetSdk = 33
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

buildFeatures {
buildConfig = false
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
}

lint {
textReport = true
textOutput = File("stdout")
// We run a full lint analysis as build part in CI, so skip vital checks for assemble tasks
checkReleaseBuilds = false
disable += setOf("GradleOverrides")
}

packaging {
// Some of the META-INF files conflict with coroutines-test. Exclude them to enable
// our test APK to build (has no effect on our AARs)
resources {
excludes += listOf("/META-INF/AL2.0", "/META-INF/LGPL2.1")
}
}

testOptions {
unitTests {
isIncludeAndroidResources = true
}
animationsDisabled = true
}

sourceSets {
named("test") {
java.srcDirs("src/sharedTest/kotlin")
res.srcDirs("src/sharedTest/res")
}
named("androidTest") {
java.srcDirs("src/sharedTest/kotlin")
res.srcDirs("src/sharedTest/res")
}
}
}

metalava {
sourcePaths.setFrom("src/main")
filename.set("api/current.api")
reportLintsAsErrors.set(true)
}

dependencies {
api(libs.compose.foundation.foundation)
api(libs.snapper)

implementation(libs.napier)
implementation(libs.kotlin.coroutines.android)

// ======================
// Test dependencies
// ======================

androidTestImplementation(project(":internal-testutils"))
testImplementation(project(":internal-testutils"))

androidTestImplementation(project(":testharness"))
testImplementation(project(":testharness"))

androidTestImplementation(libs.junit)
testImplementation(libs.junit)

androidTestImplementation(libs.truth)
testImplementation(libs.truth)

androidTestImplementation(libs.compose.ui.test.junit4)
testImplementation(libs.compose.ui.test.junit4)

androidTestImplementation(libs.compose.ui.test.manifest)
testImplementation(libs.compose.ui.test.manifest)

androidTestImplementation(libs.androidx.test.runner)
testImplementation(libs.androidx.test.runner)

testImplementation(libs.robolectric)
}
3 changes: 3 additions & 0 deletions constraintlayout/extensionLibrary/Compose/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=accompanist-pager
POM_NAME=Accompanist Pager layouts
POM_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.google.constraintlayout.ext.graph3d

import android.support.composegraph3d.lib.objects.AxisBox
import android.support.composegraph3d.lib.objects.Surface3D
import android.support.composegraph3d.lib.objects.Surface3D.Function
import java.util.*
import kotlin.math.*

class FunctionSetup(var mWidth: Int, var mHeight: Int) {
var mScene3D: Scene3D
private var mImageBuff: IntArray
var mGraphType = 2
private var mLastTouchX0 = Float.NaN
private var mLastTouchY0 = 0f
private var mLastTrackBallX = 0f
private var mLastTrackBallY = 0f
var mDownScreenWidth = 0.0
var mSurface: Surface3D? = null
var mAxisBox: AxisBox? = null
var range = 20f
var minZ = -10f
var maxZ = 10f
var mZoomFactor = 1f
var animated = false
var zBuff: FloatArray = FloatArray(mWidth * mHeight)
var nanoTime: Long = 0
var time = 0f

fun buildSurface() {
mSurface = Surface3D(mFunction = object : Function {
override fun eval(x: Float, y: Float): Float {
val d = Math.sqrt((x * x + y * y).toDouble())
return 0.3f * (Math.cos(d) * (y * y - x * x) / (1 + d)).toFloat()
}
})
mSurface!!.setRange(-range, range, -range, range, minZ, maxZ)
mScene3D.setObject(mSurface!!)
mScene3D.resetCamera()
mAxisBox = AxisBox()
mAxisBox!!.setRange(-range, range, -range, range, minZ, maxZ)
mScene3D.addPostObject(mAxisBox!!)
return buildAnimatedSurface()
}


init {
mImageBuff = IntArray(mWidth * mHeight)
// zBuff = new float[w*h];
mScene3D = Scene3D()
buildSurface()
mScene3D.setUpMatrix(mWidth, mHeight)
mScene3D.setScreenDim(mWidth, mHeight, mImageBuff, 0x00AAAAAA)
}

fun buildAnimatedSurface() {
mSurface = Surface3D(object : Function {
override fun eval(x: Float, y: Float): Float {
val d = sqrt((x * x + y * y).toDouble()).toFloat()
val d2 = (x * x + y * y).toDouble().pow(0.125).toFloat()
val angle = atan2(y.toDouble(), x.toDouble()).toFloat()
val s = sin((d + angle - time * 5).toDouble()).toFloat()
val s2 = sin(time.toDouble()).toFloat()
val c = cos((d + angle - time * 5).toDouble()).toFloat()
return (s2 * s2 + 0.1f) * d2 * 5 * (s + c) / (1 + d * d / 20)
}
})
nanoTime = System.nanoTime()
mScene3D.setObject(mSurface!!)
mSurface!!.setRange(-range, range, -range, range, minZ, maxZ)
}

fun tick(now: Long) {
time += (now - nanoTime) * 1E-9f
nanoTime = now
mSurface!!.calcSurface(false)
mScene3D.update()
}

fun onKeyTyped(c: Long) {
println(c)
// switch ((char) c) {
// case ' ':
// buildAnimatedSurface();
// }
}

fun onMouseDown(x: Float, y: Float) {
mDownScreenWidth = mScene3D.screenWidth
mLastTouchX0 = x
mLastTouchY0 = y
mScene3D.trackBallDown(mLastTouchX0, mLastTouchY0)
mLastTrackBallX = mLastTouchX0
mLastTrackBallY = mLastTouchY0
}

fun onMouseDrag(x: Float, y: Float) {
if (java.lang.Float.isNaN(mLastTouchX0)) {
return
}
val moveX = mLastTrackBallX - x
val moveY = mLastTrackBallY - y
if (moveX * moveX + moveY * moveY < 4000f) {
mScene3D.trackBallMove(x, y)
}
mLastTrackBallX = x
mLastTrackBallY = y
}

fun onMouseUP() {
mLastTouchX0 = Float.NaN
mLastTouchY0 = Float.NaN
}

fun onMouseWheel(rotation: Float, ctlDown: Boolean) {
if (ctlDown) {
mZoomFactor *= 1.01.pow(rotation.toDouble()).toFloat()
mScene3D.zoom = mZoomFactor
mScene3D.setUpMatrix(mWidth, mHeight)
mScene3D.update()
} else {
range *= 1.01.pow(rotation.toDouble()).toFloat()
mSurface!!.setArraySize(Math.min(300, (range * 5).toInt()))
mSurface!!.setRange(-range, range, -range, range, minZ, maxZ)
mAxisBox!!.setRange(-range, range, -range, range, minZ, maxZ)
mScene3D.update()
}
}

fun getImageBuff(time: Long): IntArray {
tick(time)
if (mScene3D.notSetUp()) {
mScene3D.setUpMatrix(mWidth, mHeight)
}
render(2)
return mImageBuff
}

fun render(type: Int) {
Arrays.fill(mImageBuff, -0x777778)
mScene3D.render(2)

// Arrays.fill(mScene3D.getZBuff(),Float.MAX_VALUE);

// mSurface.render(this, zBuff, mImageBuff, mWidth, mHeight);
// raster_phong(mSurface,mScene3D,zBuff,mImageBuff,mWidth,mHeight);
}

fun setSize(width: Int, height: Int) {
if (mWidth == width && mHeight == height) {
return
}
println("$width $height")
mWidth = width
mHeight = height
mImageBuff = IntArray(mWidth * mHeight)
buildSurface()
mScene3D.setUpMatrix(mWidth, mHeight)
mScene3D.setScreenDim(mWidth, mHeight, mImageBuff, 0x00AAAAAA)
} ///////////////////////////////////////
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.google.constraintlayout.ext.graph3d

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.ImageBitmapConfig
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.input.pointer.PointerInputChange

class Graph {
var w = 512
var h = 512
val scale = 2
var downX = 0.0f
var downY = 0.0f
var last = System.nanoTime()
var count = 0;
val showFps = true;
var graphFunctions = FunctionSetup(w, h)
var bitmap = ImageBitmap(w, h, ImageBitmapConfig.Argb8888)
fun setSize(width: Int, height: Int) {
if (w == width/scale && h == height/scale) {
return
}
w = width/scale
h = height/scale
graphFunctions.setSize(w, h)
bitmap = ImageBitmap(w, h, ImageBitmapConfig.Argb8888)
println("$w x $h")
}

fun getImageForTime(nanoTime: Long): ImageBitmap {
val pix = graphFunctions.getImageBuff(nanoTime)
bitmap.asAndroidBitmap().setPixels(pix, 0, w, 0, 0, w, h)
if (showFps) {
count++
val now = System.nanoTime()
if ((now - last) > 1000000000) {
println("rate : " + count / ((now - last).toFloat() * 1E-9f) + "f/sec")
last = now;
count = 0;
}
}
return bitmap
}

fun dragStart(down: Offset) {
downX = down.x/scale
downY = down.y/scale
graphFunctions.onMouseDown(downX, downY)
}

fun dragStopped() {
downX = 0.0f
downY = 0.0f
}

fun drag(change: PointerInputChange, drag: Offset) {
downX += drag.x/scale
downY += drag.y/scale
graphFunctions.onMouseDrag(downX, downY)

}

}