From 94bc4ac3a0bcffc4e49c08e8396490242b0d146e Mon Sep 17 00:00:00 2001 From: cthulhuology Date: Sat, 4 Jul 2009 10:04:53 -0400 Subject: [PATCH] fixed shitweasel ref --- js/phos.js | 73 ++++++++---------------------------------- js/phosphor-app.js | 2 ++ js/phosphor-widgets.js | 15 +++------ js/phosphor.js | 40 ++++++++++------------- js/selfish.js | 65 ++++++++++++++++++++++++++++++++++++- js/shitweasel.js | 3 ++ 6 files changed, 104 insertions(+), 94 deletions(-) diff --git a/js/phos.js b/js/phos.js index 43bb8f9..c7851ed 100644 --- a/js/phos.js +++ b/js/phos.js @@ -28,6 +28,7 @@ // Event Functions document.onkeypress = function() { return false }; // Hack to break backspace document.oncontextmenu = function(e) { return false }; // Hack to remove popup menu +window.onresize = function() { document.location.href = document.location.href }; var _doc = document; var _root = window; @@ -37,12 +38,11 @@ function nop() {} function boot() { _body = document.getElementsByTagName('body')[0]; - if (navigator.userAgent.contains('Firefox')) use('/js/shitweasel.js'); Display.init(); Keyboard.init(); Mouse.init(); Screen.init(); - navigator.userAgent.contains('Firefox') ? use('js/shitwasel.js') : Objects.init(); + navigator.userAgent.contains('Firefox') ? use('js/shitweasel.js') : Objects.init(); App.run(); } @@ -55,57 +55,11 @@ var Objects = let({ } }); -//////////////////////////////////////////////////////////////////////////////////////////////////// -// Network Functions -Object.prototype.request = function(cb) { - this._request = XMLHttpRequest ? new XMLHttpRequest(): _doc.createRequest(); - this._request.onreadystatechange = function () { - if (this.readyState != 4 || typeof(cb) != "function") return; - if (this.status == 404) cb(null); - if (this.status == 200) cb(this.responseText); - }; - return this._request; -} -Object.prototype.post = function(url,cb) { - var data = this.toString(); - this.request(cb); - this._request.open("POST",url,true); - this._request.setRequestHeader('Content-Type','appliaction/x-www-from-urlencoded'); - this._request.send(data); - return this; -} -Object.prototype.get = function(url,cb) { - this.request(function(txt) { if (typeof(cb) == "function") cb(txt) }); - this._request.open("GET",url,true); - this._request.send(""); - return this; -}; -Object.prototype.download = function() { - document.location.href = "data:application/json,".append(this.toString().encode()); - return this; -} - -var loaded = []; -Object.prototype.use = function() { - var urls = []; - for (var i = 0; i < arguments.length; ++i) urls[i] = arguments[i]; - var url = urls.shift(); - var cb = function(txt) { - if (!txt) alert('Failed to load '.append(url)); - try { - eval('('.append(txt,')')) - var url = urls.shift(); - if (url) get(url,cb); - } catch(e) { alert('Load error: '.append(e,':',txt)) } - }; - return this.get(url,cb); -} - //////////////////////////////////////////////////////////////////////////////////////////////////// // Box Object var Box = Boxes = let({ x: 0, y: 0, w: 0, h: 0, - init: function() { return this.clone() }, + init: function() { return this.clone().copy({x:0,y:0,w:0,h:0}) }, on: function(o) { var x = o.x ? o.x : 0; var y = o.y ? o.y : 0; @@ -146,6 +100,11 @@ var Box = Boxes = let({ this.y = Math.min(h - this.h,this.y); return this; }, + towards: function(x,y) { + var dx = x - this.x; + var dy = y - this.y; + return this.to(dx ? dx/Math.abs(dx): 0,dy ? dy/Math.abs(dy) : 0); + }, }); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -154,7 +113,7 @@ var Widget = Widgets = let(Box, { visible: true, draw: function() {}, // Override to draw tick: function() {}, // Override to update based on time - init: function() { return this.clone().instance() }, // Override to initialize + init: function() { return this.clone().copy({visible:true}).instance() }, // Override to init free: function() { return this.remove() }, // Override this method for custom code remove: function() { App.widgets.except(this); @@ -163,7 +122,7 @@ var Widget = Widgets = let(Box, { instance: function() { App.widgets.push(this); return this; - }, + }, add : function(o) { if (!this.children) return; this.children.push(o); @@ -209,7 +168,6 @@ var Display = let(Widget, { this.canvas = $('canvas'); if (this.canvas) return this.at(0,0).by(this.canvas.width,this.canvas.height).instance(); return this.at(0,0).by(window.innerWidth, window.innerHeight).create().instance(); - }, up: function(e) { this.moving = false }, down: function(e) { if (!e.overlaps([Display])) this.moving = e }, @@ -336,7 +294,6 @@ var Screen = let(Box,{ //////////////////////////////////////////////////////////////////////////////////////////////////// // Event Object var Event = Events = let(Box,{ - key: 0, init: function(e) { return Event.clone().copy({ button: e.button, @@ -421,20 +378,20 @@ var Mouse = let(Device, { //////////////////////////////////////////////////////////////////////////////////////////////////// // App Object var App = let(Device, { - delay: 40, widgets: [], + delay: 40, run: function () { Screen.clear(); this.dispatch('tick',{}).dispatch('draw',{}); this.timer = setTimeout("App.run()",this.delay); }, + fps: function() { return Math.floor(1000/this.delay) }, }); //////////////////////////////////////////////////////////////////////////////////////////////////// // Resource Object var Resource = Resources = let(Box,{ - loaded: false, - init: function() { return Resource.clone() }, + init: function() { return Resource.clone().copy({loaded:false}) }, load: function(t,i,cb) { var $self = this; $self.data = $_(t); @@ -469,10 +426,8 @@ var Image = Images = let(Widget,Resource, { //////////////////////////////////////////////////////////////////////////////////////////////////// // Movie Object var Movie = Movies = let(Widget,Resource,{ - div: $_('div'), - attached: false, init: function(name) { - var i = this.clone(); + var i = this.clone().copy({attached:false, div: $_('div')}); i.load('video',name,function($self) { if ($self.attached) return; $self.attached = true; diff --git a/js/phosphor-app.js b/js/phosphor-app.js index 960ae00..d985b44 100644 --- a/js/phosphor-app.js +++ b/js/phosphor-app.js @@ -66,6 +66,8 @@ var YouTube = let(Widget,{ this.d.style.top = this.y; this.d.style.left = this.x; }, + play: function() { }, + pause: function() { }, free: function() { _body.removeChild(this.d); } }); diff --git a/js/phosphor-widgets.js b/js/phosphor-widgets.js index 6a64e92..ab56a7f 100644 --- a/js/phosphor-widgets.js +++ b/js/phosphor-widgets.js @@ -38,7 +38,7 @@ var Rectangle = Rectangles = let(Graphic,{ //////////////////////////////////////////////////////////////////////////////////////////////////// // Circle var Circle = Circles = let(Graphic,{ - rad: 20, + init: function() { return this.clone().radius(20).instance() }, draw: function() { var r = Screen.rad; if(this.visible) Screen.color(this.r,this.g,this.b).radius(this.rad).at(this.x+this.rad,this.y+this.rad).circle().radius(r).white() }, @@ -52,9 +52,7 @@ var Circle = Circles = let(Graphic,{ //////////////////////////////////////////////////////////////////////////////////////////////////// // Text var Text = Texts = let(Widget,{ - print: function(p) { - Screen.color(this.r,this.g,this.b).at(this.x,this.y).print(p).white(); - } + print: function(p) { Screen.color(this.r,this.g,this.b).at(this.x,this.y).print(p).white() } }); //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -66,10 +64,7 @@ var Drawing = Drawings = let(Widget,{ //////////////////////////////////////////////////////////////////////////////////////////////////// // Picker var Picker = let(Circle,{ - init: function() { - return this.clone().instance().radius(128); - }, - gamma: 255, cr: 0, cb: 0, + init: function() { return this.clone().copy({ gamma: 255, cr: 0, cb: 0 }).instance().radius(128) }, move: function(e) { this.cr = Math.min(255,Math.max(0,e.x - this.x))/255; this.cb = Math.min(255,Math.max(0,e.y - this.y))/255; @@ -78,7 +73,5 @@ var Picker = let(Circle,{ this.color(Math.floor(this.gamma*this.cr), Math.floor(this.gamma*this.cg),Math.floor(this.gamma*this.cb)); }, - down: function(e) { - if (e.on(this) && that.can('color')) that.color(this.r,this.g,this.b); - }, + down: function(e) { if (e.on(this) && that.can('color')) that.color(this.r,this.g,this.b) }, }); diff --git a/js/phosphor.js b/js/phosphor.js index 7f887ad..2e824fa 100644 --- a/js/phosphor.js +++ b/js/phosphor.js @@ -32,7 +32,7 @@ Object.prototype.display = function(x,y) { var w = []; var $self = this; this.each(function(v,k) { - if (!k || !v) return; + if (!k || !typeof(v)) return; w.push(a(Block).says(v.parameters ? k + v.parameters() : k).at(x,y).by(200,20).copy({childof: $self })); y += 28; }); @@ -68,23 +68,22 @@ Widget.down = function(e) { this.contents = this.contents ? this.contents.collapse() : this.display(this.x,this.y+this.h) }; Widget.move = function(e) { - if (!this.moving) return; - var dx = e.x - this.moving.x; - var dy = e.y - this.moving.y; - this.to(dx,dy); - if (this.parent) this.clamp(this.parent.x,this.parent.y,this.parent.x+this.parent.w,this.parent.y+this.parent.h); - if (this.contents) this.contents.every(function(v,i) { v.to(dx,dy)}); - if (this.children) this.children.every(function(v,i) { v.to(dx,dy)}); - this.moving = e; - + if (!this.moving) return; + var dx = e.x - this.moving.x; + var dy = e.y - this.moving.y; + this.to(dx,dy); + if (this.parent) this.clamp(this.parent.x,this.parent.y,this.parent.x+this.parent.w,this.parent.y+this.parent.h); + if (this.contents) this.contents.every(function(v,i) { v.to(dx,dy)}); + if (this.children) this.children.every(function(v,i) { v.to(dx,dy)}); + this.moving = e; }; Widget.up = function(e) { - this.moving = false; - if (!e.on(this)) return; - var o = this.overlaps([Display,Phosphor,this]); - if (!o || !o.is('Graphic') || this == o) return; - if (o.children.contains(this)) return; - o.add(this); + this.moving = false; + if (!e.on(this)) return; + var o = this.overlaps([Display,Phosphor,this]); + if (!o || !o.is('Graphic') || this == o) return; + if (o.children.contains(this)) return; + o.add(this); }; //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -115,12 +114,8 @@ var HotKey = let({ //////////////////////////////////////////////////////////////////////////////////////////////////// // Block Object var Block = let(Widget,{ - bg: "gray", - moving: false, - editing: false, - expanded: false, - content: false, - init: function() { return this.clone().instance() }, + init: function() { return this.clone().copy({ + bg: "gray", moving: false, editing: false, expanded: false, content: false }).instance() }, says: function(t) { this.content = t; return this }, evaluate: function() { if (!this.content) return ""; @@ -196,7 +191,6 @@ var Block = let(Widget,{ this.free(); } if (!o.childof) { - alert('setting'); if (!window.contains(o.content)) window[o.content] = {}; o.evaluate()[this.content] = true; if (o.expanded) o.expanded.collapse(); diff --git a/js/selfish.js b/js/selfish.js index 8cb773e..6f975f3 100644 --- a/js/selfish.js +++ b/js/selfish.js @@ -47,7 +47,7 @@ String.prototype.contains = function(s) { return 0 <= this.indexOf(s) } //////////////////////////////////////////////////////////////////////////////////////////////////// // Object prototype extensions -Object.prototype.a = Object.prototype.an = function(x,v) { return x.can('init') ? x.init(v) : x.clone() }; +Object.prototype.a = Object.prototype.an = function(x,v) { return x.can('init') ? x.init(v).copy({from:x}) : x.clone().copy({from: x}) }; Object.prototype.the = function(x) { return x }; Object.prototype.clone = function() { @@ -130,12 +130,75 @@ Object.prototype.is = function(x) { return retval; } +Object.prototype.implements = function() { + var $self = this; + var retval = []; + Objects.each(function(v,k) { if ($self.is(v)) retval.push(k) }); + return retval; +} + Object.prototype.any = function(f) { var retval = null; this.each(function(v,k) { if (f(v,k)) return retval = v }); return retval; } +Object.prototype.module = function() { + var ots = Object.prototype.toString; + Object.prototype.toString = function() { + var retval = '{ '; + this.each(function(v,k) { + if (typeof(v) == 'function') retval = retval.append(k,': ',v,', '); + if (typeof(v) == 'string') retval = retval.append(k,': "', v, '", '); + if (typeof(v) == 'number') retval = retval.append(k,': ', v, ', '); + if (typeof(v) == 'boolean') retval = retval.append(k,': ', v, ', '); + if (typeof(v) == 'object') retval = retval.append(k, ': ', v.name(), ', '); + }); + return retval.append('}'); + }; + var retval = this.toString(); + Object.prototype.toString = ots; + return retval; +} + +Object.prototype.use = function() { + var urls = []; + for (var i = 0; i < arguments.length; ++i) urls[i] = arguments[i]; + var url = urls.shift(); + var cb = function(txt) { + if (!txt) alert('Failed to load '.append(url)); + try { + eval('('.append(txt,')')) + var url = urls.shift(); + if (url) get(url,cb); + } catch(e) { alert('Load error: '.append(e,':',txt)) } + }; + return this.get(url,cb); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Network Functions +Object.prototype.request = function(method,url,cb,data) { + var _request = XMLHttpRequest ? new XMLHttpRequest(): _doc.createRequest(); + _request.onreadystatechange = function () { + if (this.readyState != 4 || typeof(cb) != "function") return; + if (this.status == 404) cb(null); + if (this.status == 200) cb(this.responseText); + }; + _request.open(method,url,true); + _request.setRequestHeader('Content-Type','appliaction/x-www-from-urlencoded'); + _request.send(data ? data : ''); + return this; +} +Object.prototype.post = function(url,cb) { return this.request("POST",url,this.toString(),cb) } +Object.prototype.get = function(url,cb) { return this.request("GET",url,cb) } + +Object.prototype.download = function() { + document.location.href = "data:application/json,".append(this.toString().encode()); + return this; +} + + //////////////////////////////////////////////////////////////////////////////////////////////////// // Array extensions Array.prototype.every = function(f) { diff --git a/js/shitweasel.js b/js/shitweasel.js index c31d078..9d6b953 100644 --- a/js/shitweasel.js +++ b/js/shitweasel.js @@ -4,6 +4,9 @@ function() { // this.init = false; // }; // Objects.init(); + Keyboard.keymap[61] = '=+'; + Keyboard.keymap[107] = '++'; + Keyboard.keymap[109] = '-_'; Screen.frame = function() { this.ctx.beginPath(); this.ctx.moveTo(this.x,this.y);