Find file
Fetching contributors…
Cannot retrieve contributors at this time
84 lines (83 sloc) 2.75 KB
An.object().
named('Block').
from(Widget).
does('init', function() {
return this.clone().
has('font',A.font('16 Monaco')).
has('background',"gray").
has('highlight',"black").
has('selection',"blue").
has('over', false).
has('moving', false).
has('editing', false).
has('expanded', false).
has('content', '').
instance()
}).
does('says', function(T) {
return this.has('content',T)
}).
does('run', function() {
A.block().at(this.x()+this.width(),this.y()).says(this.value())
return this.content()
}).
does('value', function() {
if (this.can('of')) return this.of(this.content())
Sound.click().play()
return this.content().toString().eval()
}).
does('press', function(E) {
if (!this.editing()
|| Keyboard.command()
|| Keyboard.control()) return;
this.has('content',
E.key() == Keyboard.enter() ? this.content().eval():
E.key() == Keyboard.backspace() ?
this.content().length == 0 || Keyboard.shift() ?
this.done():
this.content().substring(0,this.content().length-1):
this.content().append(E.key()))
if (this.content() == undefined) this.free()
}).
does('selected', function() {
return Display.selected().contains(this)
}).
does('draw', function() {
if (!this.visible()) return;
Screen.save()
var Color = this.editing() || this.over() ? this.highlight() : this.selected() ? this.selection() : this.background()
Screen[Color]()
this.font().use()
Screen.at(this.x(),this.y()+this.font().size()).by(this.width()-this.font().size(),this.height()).print(this.content())
this.by(Math.max(this.width(),(Screen.x()+Display.x())-this.x() + this.font().size()),(Screen.y()+Display.y())-this.y() + this.font().size()/2)
Screen.at(this.x()-this.font().size()/2,this.y()).by(this.width(),this.height()).frame().restore()
}).
does('down', function(E) { // right clicking on a Block instantiates a Mirror
if (!E.on(this)) return;
this.has('moving',E)
if (!this.content()) return;
if (E.button() < 2) return;
Sound.click().play()
this.value().mirror()
}).
does('up', function(E) {
var I = this
if (this.moving() && (global['that'] = Widget.widgets().any(function(O,K) {
return ![ I, Display, Desktop ].contains(O) && O.can('on') && O.on(I)
}))) {
if (that.can('drop')) {
that.drop(this.value());
this.free();
} else if (this.content().toString().isCall() && that.can(this.content().deparameterized())) {
('that.' + this.content()).eval()
this.free()
}
}
this.has('moving',false)
}).
does('move', function(E) {
this.has('over',E.on(this))
this.has('editing',E.on(this))
if (this.editing()) global['editing'] = this // editing is a special global
if (this.moving()) this.to(E.x() - this.moving().x(),E.y() - this.moving().y()).has('moving',E)
})