Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from igalata/feature/place_bubbles
Browse files Browse the repository at this point in the history
done
  • Loading branch information
igalata authored Apr 17, 2017
2 parents 6b5af0c + c4f01e2 commit 0bda8d2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CircleBody(val world: World, var position: Vec2, var radius: Float, var in

var increased = false

var isVisible = true

private val margin = 0.01f
private val damping = 25f
private val shape: CircleShape
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.igalata.bubblepicker.physics

import com.igalata.bubblepicker.even
import com.igalata.bubblepicker.rendering.Item
import com.igalata.bubblepicker.sqr
import org.jbox2d.common.Vec2
Expand All @@ -22,6 +21,7 @@ object Engine {
gravity = interpolate(20f, 80f, value / 100f)
standardIncreasedGravity = interpolate(500f, 800f, value / 100f)
}
var centerImmediately = false
private var standardIncreasedGravity = interpolate(500f, 800f, 0.5f)
private var bubbleRadius = 0.17f

Expand All @@ -39,12 +39,15 @@ object Engine {
private val currentGravity: Float
get() = if (touch) increasedGravity else gravity
private val toBeResized = ArrayList<Item>()
private val startX
get() = if (centerImmediately) 0.5f else 2.2f
private var stepsCount = 0

fun build(bodiesCount: Int, scaleX: Float, scaleY: Float): List<CircleBody> {
val density = interpolate(0.8f, 0.2f, radius / 100f)
for (i in 0..bodiesCount - 1) {
val x = if (Random().nextInt(2).even()) -2.2f else 2.2f
val y = if (Random().nextInt(2).even()) -0.5f / scaleY else 0.5f / scaleY
val x = if (Random().nextBoolean()) -startX else startX
val y = if (Random().nextBoolean()) -0.5f / scaleY else 0.5f / scaleY
bodies.add(CircleBody(world, Vec2(x, y), bubbleRadius * scaleX, (bubbleRadius * scaleX) * 1.3f, density))
}
this.scaleX = scaleX
Expand All @@ -56,9 +59,13 @@ object Engine {

fun move() {
toBeResized.forEach { it.circleBody.resize(resizeStep) }
world.step(step, 11, 11)
world.step(if (centerImmediately) 0.035f else step, 11, 11)
bodies.forEach { move(it) }
toBeResized.removeAll(toBeResized.filter { it.circleBody.finished })
stepsCount++
if (stepsCount >= 10) {
centerImmediately = false
}
}

fun swipe(x: Float, y: Float) {
Expand Down Expand Up @@ -102,6 +109,7 @@ object Engine {

private fun move(body: CircleBody) {
body.physicalBody.apply {
body.isVisible = centerImmediately.not()
val direction = gravityCenter.sub(position)
val distance = direction.length()
val gravity = if (body.increased) 1.3f * currentGravity else currentGravity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class BubblePicker : GLSurfaceView {
val selectedItems: List<PickerItem?>
get() = renderer.selectedItems

var centerImmediately = false
set(value) {
field = value
renderer.centerImmediately = value
}

private val renderer = PickerRenderer(this)
private var startX = 0f
private var startY = 0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object BubbleShader {
const val U_MATRIX = "u_Matrix"
const val U_BACKGROUND = "u_Background"
const val U_TEXT = "u_Text"
const val U_VISIBILITY = "u_Visibility"

const val A_POSITION = "a_Position"
const val A_UV = "a_UV"
Expand All @@ -34,13 +35,15 @@ object BubbleShader {
uniform vec4 u_Background;
uniform sampler2D u_Texture;
uniform int u_Visibility;
varying vec2 v_UV;
void main()
{
float distance = distance(vec2(0.5, 0.5), v_UV);
gl_FragColor = mix(texture2D(u_Texture, v_UV), u_Background, smoothstep(0.49, 0.5, distance));
gl_FragColor = u_Visibility > 0 ?
mix(texture2D(u_Texture, v_UV), u_Background, smoothstep(0.49, 0.5, distance)) : vec4(0);
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ data class Item(val pickerItem: PickerItem, val circleBody: CircleBody) {
val currentPosition: Vec2
get() = circleBody.physicalBody.position

private var isVisible = true
get() = circleBody.isVisible
private var texture: Int = 0
private var imageTexture: Int = 0
private val currentTexture: Int
Expand All @@ -55,6 +57,7 @@ data class Item(val pickerItem: PickerItem, val circleBody: CircleBody) {
glActiveTexture(GL_TEXTURE)
glBindTexture(GL_TEXTURE_2D, currentTexture)
glUniform1i(glGetUniformLocation(programId, BubbleShader.U_TEXT), 0)
glUniform1i(glGetUniformLocation(programId, BubbleShader.U_VISIBILITY), if (isVisible) 1 else -1)
glUniformMatrix4fv(glGetUniformLocation(programId, U_MATRIX), 1, false, calculateMatrix(scaleX, scaleY), 0)
glDrawArrays(GL_TRIANGLE_STRIP, index * 4, 4)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class PickerRenderer(val glView: View) : GLSurfaceView.Renderer {
lateinit var items: ArrayList<PickerItem>
val selectedItems: List<PickerItem?>
get() = Engine.selectedBodies.map { circles.firstOrNull { circle -> circle.circleBody == it }?.pickerItem }
var centerImmediately = false
set(value) {
field = value
Engine.centerImmediately = value
}

private var programId = 0
private var verticesBuffer: FloatBuffer? = null
Expand Down Expand Up @@ -70,6 +75,7 @@ class PickerRenderer(val glView: View) : GLSurfaceView.Renderer {

private fun initialize() {
clear()
Engine.centerImmediately = centerImmediately
Engine.build(items.size, scaleX, scaleY).forEachIndexed { index, body ->
circles.add(Item(items[index], body))
}
Expand Down

0 comments on commit 0bda8d2

Please sign in to comment.