Skip to content

Commit

Permalink
Added leftToRight option
Browse files Browse the repository at this point in the history
  • Loading branch information
gsio committed Feb 13, 2022
1 parent d37a4a1 commit a6e88cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class _MyAppState extends State<MyApp> {
child: Text("hello"),
),
),
// leftToRight: true,
),
onHorizontalDragStart: (details) {
_oldPosition = details.globalPosition;
Expand Down
8 changes: 7 additions & 1 deletion lib/src/flip_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class FlipWidget extends StatefulWidget {

final Widget? child;
final Size textureSize;
final bool leftToRight;

/// [child] is the widget you want to flip.
/// [textureSize] is the pixel size of effect layer.
FlipWidget({
Key? key,
this.child,
this.textureSize = const Size(512, 512),
this.leftToRight = false,
}) : super(key: key);

@override
Expand Down Expand Up @@ -80,7 +82,11 @@ class FlipWidgetState extends State<FlipWidget> {
width: widget.textureSize.width,
height: widget.textureSize.height,
);
_render = GLRender(widget.textureSize.width.toInt(), widget.textureSize.height.toInt());
_render = GLRender(
textureWidth: widget.textureSize.width.toInt(),
textureHeight: widget.textureSize.height.toInt(),
leftToRight: widget.leftToRight,
);
controller.ready.then((value) {
_render.initialize();
});
Expand Down
15 changes: 11 additions & 4 deletions lib/src/gl_render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ float py(float uvy) {
}
vec2 to_uv(vec2 pos) {
return vec2(pos.x / size.x, 1.0 - pos.y / size.y);
return vec2({l2r} pos.x / size.x, 1.0 - pos.y / size.y);
}
void main()
{
const float roll_size = 6.0;
float x1 = px(uv.x);
float x1 = px({l2r} uv.x);
float y1 = py(1.0 - uv.y);
float per = px(percent);
if (y1 / tilt + per < x1) {
Expand Down Expand Up @@ -171,7 +171,9 @@ class GLRender {
void initialize() {
_templateString = malloc.allocate(sizeOf<Int8>() * 512);

_programHandle = loadProgram(_VertexShader, _FragmentShader);
_programHandle = loadProgram(_VertexShader, _FragmentShader.replaceAll(
"{l2r}", leftToRight ? "1.0 - " : ""
));

Pointer<Int32> ret = malloc.allocate(sizeOf<Int32>());
GLES20.glGetProgramiv(_programHandle, GL_LINK_STATUS, ret);
Expand Down Expand Up @@ -263,6 +265,7 @@ class GLRender {

int textureWidth;
int textureHeight;
bool leftToRight;

void draw(double percent, double tilt) {

Expand Down Expand Up @@ -309,5 +312,9 @@ class GLRender {
GLES20.glDeleteProgram(_programHandle);
}

GLRender(this.textureWidth, this.textureHeight);
GLRender({
required this.textureWidth,
required this.textureHeight,
required this.leftToRight,
});
}

0 comments on commit a6e88cc

Please sign in to comment.