Skip to content

Commit

Permalink
Add block for mouse/touch down
Browse files Browse the repository at this point in the history
  • Loading branch information
kano31 committed Dec 6, 2015
1 parent 6c43aaf commit 03b59e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions js/app.js
Expand Up @@ -224,6 +224,8 @@ Event.on(window, 'input:keydown', null, Event.handleKeyDown);
Event.on(window, 'input:keyup', null, Event.handleKeyUp);
Event.on(window, 'touchend', null, Event.handleMouseOrTouchUp);
Event.on(window, 'mouseup', null, Event.handleMouseOrTouchUp);
Event.on(window, 'touchstart', null, Event.handleMouseOrTouchDown);
Event.on(window, 'mousedown', null, Event.handleMouseOrTouchDown);

Event.on(document.body, 'ui:click', '.undo', Undo.handleUndoButton);
Event.on(document.body, 'ui:click', '.redo', Undo.handleRedoButton);
Expand Down
25 changes: 17 additions & 8 deletions js/event.js
Expand Up @@ -456,19 +456,27 @@
*
******************************/

function handleMouseOrTouchUp(evt) {
if(Event.mouseOrTouchHandlers['up']) {
Event.mouseOrTouchHandlers['up'].forEach(function(handler){
function handleMouseOrTouchEvent(type, evt) {
if(Event.mouseOrTouchHandlers[type]) {
Event.mouseOrTouchHandlers[type].forEach(function(handler){
handler(evt);
});
}
}

function mouseOrTouchUp(handler) {
if (! Event.mouseOrTouchHandlers['up'] ){
Event.mouseOrTouchHandlers['up'] = [];
function handleMouseOrTouchUp(evt) {
handleMouseOrTouchEvent('up', evt);
}

function handleMouseOrTouchDown(evt) {
handleMouseOrTouchEvent('down', evt);
}

function mouseOrTouchEvent(type, handler) {
if (! Event.mouseOrTouchHandlers[type] ){
Event.mouseOrTouchHandlers[type] = [];
}
Event.mouseOrTouchHandlers['up'].push(handler);
Event.mouseOrTouchHandlers[type].push(handler);
}


Expand Down Expand Up @@ -503,7 +511,8 @@
keyForEvent: keyForEvent,
mouseOrTouchHandlers: {},
handleMouseOrTouchUp: handleMouseOrTouchUp,
mouseOrTouchUp: mouseOrTouchUp
mouseOrTouchEvent: mouseOrTouchEvent,
handleMouseOrTouchDown: handleMouseOrTouchDown,
};

})();
10 changes: 8 additions & 2 deletions js/runtime.js
Expand Up @@ -564,9 +564,15 @@
self.gatherSteps().forEach(runBlock);
});
},
whenMouseReleased: function inputWhenMouseReleasedCtx(key){
whenMousePressed: function(){
var self = this;
Event.mouseOrTouchUp(function(){
Event.mouseOrTouchEvent('down', function(){
self.gatherSteps().forEach(runBlock);
});
},
whenMouseReleased: function(){
var self = this;
Event.mouseOrTouchEvent('up', function(){
self.gatherSteps().forEach(runBlock);
});
}
Expand Down
3 changes: 3 additions & 0 deletions playground.html
Expand Up @@ -1052,6 +1052,9 @@ <h1>Waterbear Playground</h1>
<wb-value type="list" allow="literal" value="a" options="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,1,2,3,4,5,6,7,8,9,0,*,+,-,.,/,up,down,left,right,backspace,tab,return,shift,ctrl,alt,pause,capslock,esc,space,pageup,pagedown,end,home,insert,del,numlock,scroll,meta">when</wb-value>
<wb-value>key pressed</wb-value>
</wb-context>
<wb-context ns="input" fn="whenMousePressed">
<wb-value>when mouse pressed</wb-value>
</wb-context>
<wb-context ns="input" fn="whenMouseReleased">
<wb-value>when mouse released</wb-value>
</wb-context>
Expand Down

0 comments on commit 03b59e6

Please sign in to comment.