Skip to content

Instantly share code, notes, and snippets.

@PongoEngine
Created January 25, 2018 01:52
Show Gist options
  • Save PongoEngine/1c0bffc38e267694a81085af161e2d9a to your computer and use it in GitHub Desktop.
Save PongoEngine/1c0bffc38e267694a81085af161e2d9a to your computer and use it in GitHub Desktop.
package;
import kha.System;
import jasper.*;
import tweenx909.TweenX;
import tweenx909.EaseX;
class Main {
public static function main() : Void
{
System.init({title: "jasper-example", width: 800, height: 600}, function() {
var width = System.windowWidth();
var height = System.windowHeight();
var solver = new Solver();
var window = new Rectangle(0xff444444);
solver.addConstraint(window.x == 10);
solver.addConstraint(window.y == 10);
solver.addConstraint((window.width == width - 20) | Strength.WEAK);
solver.addConstraint((window.height == height - 20) | Strength.WEAK);
solver.addConstraint((window.width <= width - 20) | Strength.REQUIRED);
solver.addConstraint((window.height <= height - 20) | Strength.REQUIRED);
solver.addConstraint((window.width >= 50) | Strength.REQUIRED);
solver.addConstraint((window.height >= 50) | Strength.REQUIRED);
var leftChild = new Rectangle(0xffaaaaaa);
solver.addConstraint(leftChild.x == window.x + 10);
solver.addConstraint(leftChild.y == window.y + 10);
solver.addConstraint(leftChild.width == (window.width/2 - 20));
solver.addConstraint(leftChild.height == (window.height - 20));
var rightChild = new Rectangle(0xffdddddd);
solver.addConstraint(rightChild.x == (window.x + window.width) - (window.width/2 - 10));
solver.addConstraint(rightChild.y == window.y + 10);
solver.addConstraint(rightChild.width == (window.width/2 - 20));
solver.addConstraint(rightChild.height == (window.height - 20));
var centerChild = new Rectangle(0xffffff00);
solver.addConstraint(centerChild.x == window.x + window.width/4);
solver.addConstraint(centerChild.y == window.y + window.height/4);
solver.addConstraint(centerChild.width == (window.width/2));
solver.addConstraint(centerChild.height == (window.height/2));
_rectangles =
[ window
, leftChild
, rightChild
, centerChild
];
solver.updateVariables();
solver.addEditVariable(window.width, Strength.MEDIUM);
solver.addEditVariable(window.height, Strength.MEDIUM);
solver.suggestValue( window.height, System.windowHeight() );
var nWidth = {width: System.windowWidth()};
TweenX.to( nWidth, { width :50.0 }).delay( 1 ).time(10).ease( EaseX.elasticOut ).onUpdate(function(){
solver.suggestValue( window.width, nWidth.width );
solver.updateVariables();
});
System.notifyOnRender(render);
});
}
public static function render(framebuffer: kha.Framebuffer): Void
{
framebuffer.g2.begin(0xffeeeeee);
for(rectangle in _rectangles) {
rectangle.render(framebuffer);
}
framebuffer.g2.end();
}
private static var _rectangles :Array<Rectangle>;
}
class Rectangle
{
public var x :Variable;
public var y :Variable;
public var width :Variable;
public var height :Variable;
public var color :Int;
public function new(color :Int) : Void
{
this.color = color;
x = new Variable();
y = new Variable();
width = new Variable();
height = new Variable();
}
public function render(framebuffer :kha.Framebuffer) : Void
{
framebuffer.g2.color = this.color;
framebuffer.g2.fillRect(x.m_value, y.m_value, width.m_value, height.m_value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment