You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdebindings/kjsembed/docs/examples/eventhandling/jsboard.js

90 lines
2.0 KiB

#!/usr/bin/env kjscmd
// Globals
var frame = new QFrame(this);
var labels = new Array();
var startPos = new Point(0,0);
var lastPos = new Point(0,0);
frame.resize(300,300);
frame.paletteBackgroundColor = "blue";
var target = new Target(frame,"Target");
for( var idx = 0; idx < 3; ++idx)
labels[idx] = new Block(frame, "Label " + idx);
frame.show();
application.exec();
function Target( parent, text)
{
var x = Math.floor( Math.random() * parent.width );
var y = Math.floor( Math.random() * parent.height );
var w = 100;
var h = 100;
this.label = new QLabel(parent);
this.text = text;
this.label.paletteBackgroundColor = "green";
this.label.geometry = new Rect(x,y,w,h);
this.checkInside = function( objectRect )
{
return this.label.geometry.contains(objectRect);
}
}
function Block(parent,text)
{
this.label = new QLabel(parent);
var x = Math.floor( Math.random() * parent.width );
var y = Math.floor( Math.random() * parent.height );
var w = 25;
var h = 25;
this.label.text = text;
this.label.geometry = new Rect(x,y,w,h);
this.label.frameShape = QFrame.WinPanel;
this.label.frameShadow = QFrame.Plain;
this.label.paletteBackgroundColor = "white";
this.label.setMouseTracking(true);
this.label.adjustSize();
// Event handlers
this.label.mousePressEvent = function(ev)
{
//println("Press: " + this.text);
startPos = this.pos;
lastPos = ev.globalPos;
println("Start: " + startPos.x() + "," + startPos.y());
}
this.label.mouseReleaseEvent = function(ev)
{
if( target.checkInside(this.geometry) )
alert( "You got " + this.text + " on target!" );
else
{
println("Reset: " + startPos.x() + "," + startPos.y());
this.pos = startPos;
}
}
this.label.mouseMoveEvent = function(ev)
{
if( ev.state == 1)
{
var xDelta = ev.globalX - lastPos.x();
var yDelta = ev.globalY - lastPos.y();
this.move( this.x + xDelta, this.y + yDelta );
lastPos = ev.globalPos;
}
}
}