2008-11-14 19:44:11 -05:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; -*- */
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Tweener = imports.tweener.tweener;
|
|
|
|
|
|
|
|
const DEFAULT_BUTTON_COLOR = new Clutter.Color();
|
|
|
|
DEFAULT_BUTTON_COLOR.from_pixel(0xeeddccff);
|
|
|
|
|
|
|
|
const DEFAULT_PRESSED_BUTTON_COLOR = new Clutter.Color();
|
2008-11-17 17:32:28 -05:00
|
|
|
DEFAULT_PRESSED_BUTTON_COLOR.from_pixel(0xccbbaaff);
|
2008-11-14 19:44:11 -05:00
|
|
|
|
|
|
|
// Time for animation making the button darker
|
|
|
|
const ANIMATION_TIME = 0.3;
|
|
|
|
|
2008-11-17 17:32:28 -05:00
|
|
|
const NO_OPACITY = 0;
|
|
|
|
|
|
|
|
const PARTIAL_OPACITY = 0.4 * 255;
|
2008-11-14 19:44:11 -05:00
|
|
|
|
2008-11-17 17:32:28 -05:00
|
|
|
const FULL_OPACITY = 255;
|
2008-11-14 19:44:11 -05:00
|
|
|
|
2008-11-19 14:25:51 -05:00
|
|
|
function Button(text, buttonColor, pressedButtonColor, staysPressed, minWidth, minHeight) {
|
|
|
|
this._init(text, buttonColor, pressedButtonColor, staysPressed, minWidth, minHeight);
|
2008-11-14 19:44:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Button.prototype = {
|
2008-11-19 14:23:24 -05:00
|
|
|
_init : function(text, buttonColor, pressedButtonColor, staysPressed, minWidth, minHeight) {
|
2008-11-14 19:44:11 -05:00
|
|
|
|
2008-11-19 14:23:24 -05:00
|
|
|
this._buttonColor = buttonColor
|
|
|
|
if (buttonColor == null)
|
|
|
|
this._buttonColor = DEFAULT_BUTTON_COLOR;
|
2008-11-14 19:44:11 -05:00
|
|
|
|
2008-11-19 14:23:24 -05:00
|
|
|
this._pressedButtonColor = pressedButtonColor
|
|
|
|
if (pressedButtonColor == null)
|
|
|
|
this._pressedButtonColor = DEFAULT_PRESSED_BUTTON_COLOR;
|
2008-11-14 19:44:11 -05:00
|
|
|
|
2008-11-19 14:23:24 -05:00
|
|
|
if (staysPressed == null)
|
|
|
|
staysPressed = false
|
|
|
|
if (minWidth == null)
|
|
|
|
minWidth = 0;
|
|
|
|
if (minHeight == null)
|
|
|
|
minHeight = 0;
|
2008-11-14 19:44:11 -05:00
|
|
|
|
2008-11-19 14:23:24 -05:00
|
|
|
// if staysPressed is true, this.active will be true past the first release of a button, untill a subsequent one (the button
|
2008-11-14 19:44:11 -05:00
|
|
|
// is unpressed) or untill release() is called explicitly
|
|
|
|
this._active = false;
|
2008-11-19 14:23:24 -05:00
|
|
|
this._isBetweenPressAndRelease = false;
|
|
|
|
this._mouseIsOverButton = false;
|
2008-11-14 19:44:11 -05:00
|
|
|
|
|
|
|
this.button = new Clutter.Group({reactive: true});
|
2008-11-19 14:23:24 -05:00
|
|
|
this._background = new Clutter.Rectangle({ color: this._buttonColor});
|
|
|
|
this._pressedBackground = new Clutter.Rectangle({ color: this._pressedButtonColor, opacity: NO_OPACITY});
|
2008-11-14 19:44:11 -05:00
|
|
|
this._label = new Clutter.Label({ font_name: "Sans Bold 16px",
|
|
|
|
text: text});
|
|
|
|
this._label.set_position(5, 5);
|
2008-11-19 14:23:24 -05:00
|
|
|
let backgroundWidth = Math.max(this._label.get_width()+10, minWidth);
|
|
|
|
let backgroundHeight = Math.max(this._label.get_height()+10, minHeight);
|
|
|
|
this._background.set_width(backgroundWidth)
|
|
|
|
this._background.set_height(backgroundHeight)
|
|
|
|
this._pressedBackground.set_width(backgroundWidth)
|
|
|
|
this._pressedBackground.set_height(backgroundHeight)
|
2008-11-14 19:44:11 -05:00
|
|
|
this.button.add_actor(this._background);
|
2008-11-19 14:23:24 -05:00
|
|
|
this.button.add_actor(this._pressedBackground);
|
2008-11-14 19:44:11 -05:00
|
|
|
this.button.add_actor(this._label);
|
|
|
|
let me = this;
|
|
|
|
this.button.connect('button-press-event',
|
|
|
|
function(o, event) {
|
2008-11-19 14:23:24 -05:00
|
|
|
me._isBetweenPressAndRelease = true;
|
|
|
|
Tweener.addTween(me._pressedBackground,
|
2008-11-14 19:44:11 -05:00
|
|
|
{ time: ANIMATION_TIME,
|
|
|
|
opacity: FULL_OPACITY,
|
|
|
|
transition: "linear"
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.button.connect('button-release-event',
|
|
|
|
function(o, event) {
|
2008-11-19 14:23:24 -05:00
|
|
|
me._isBetweenPressAndRelease = false;
|
|
|
|
if (!staysPressed || me._active) {
|
2008-11-14 19:44:11 -05:00
|
|
|
me.release();
|
|
|
|
} else {
|
|
|
|
me._active = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.button.connect('enter-event',
|
|
|
|
function(o, event) {
|
2008-11-19 14:23:24 -05:00
|
|
|
me._mouseIsOverButton = true;
|
2008-11-14 19:44:11 -05:00
|
|
|
if (!me._active) {
|
2008-11-19 14:23:24 -05:00
|
|
|
Tweener.removeTweens(me._pressedBackground);
|
|
|
|
me._pressedBackground.set_opacity(PARTIAL_OPACITY);
|
2008-11-14 19:44:11 -05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.button.connect('leave-event',
|
|
|
|
function(o, event) {
|
2008-11-19 14:23:24 -05:00
|
|
|
me._isBetweenPressAndRelease = false;
|
|
|
|
me._mouseIsOverButton = false;
|
2008-11-14 19:44:11 -05:00
|
|
|
if (!me._active) {
|
2008-11-19 14:23:24 -05:00
|
|
|
Tweener.removeTweens(me._pressedBackground);
|
|
|
|
me._pressedBackground.set_opacity(NO_OPACITY);
|
2008-11-14 19:44:11 -05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
release : function() {
|
2008-11-19 14:23:24 -05:00
|
|
|
if (!this._isBetweenPressAndRelease) {
|
2008-11-14 19:44:11 -05:00
|
|
|
this._active = false;
|
2008-11-19 14:23:24 -05:00
|
|
|
Tweener.removeTweens(this._pressedBackground);
|
|
|
|
if (this._mouseIsOverButton) {
|
|
|
|
this._pressedBackground.set_opacity(PARTIAL_OPACITY);
|
2008-11-14 19:44:11 -05:00
|
|
|
} else {
|
2008-11-19 14:23:24 -05:00
|
|
|
this._pressedBackground.set_opacity(NO_OPACITY);
|
2008-11-14 19:44:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|