diff --git a/res/app/components/stf/screen/screen-controller.js b/res/app/components/stf/screen/screen-controller.js index 8e7f4755..05c6b3c1 100644 --- a/res/app/components/stf/screen/screen-controller.js +++ b/res/app/components/stf/screen/screen-controller.js @@ -1,11 +1,19 @@ -module.exports = function DeviceScreenCtrl($scope, ScalingService) { - $scope.ready = false +module.exports = function DeviceScreenCtrl($scope, $rootScope, ScalingService) { $scope.displayError = false $scope.canView = true $scope.showScreen = true $scope.ScalingService = ScalingService - $scope.device.promise.then(function() { - $scope.ready = true + var deregisterPageHidden = $rootScope.$on('pageHidden', function () { + $scope.canView = false + }) + + var deregisterPageVisible = $rootScope.$on('pageVisible', function () { + $scope.canView = true + }) + + $scope.$on('$destroy', function() { + deregisterPageHidden() + deregisterPageVisible() }) } diff --git a/res/app/components/stf/screen/screen-directive.js b/res/app/components/stf/screen/screen-directive.js index c3f9ab7b..2a07a604 100644 --- a/res/app/components/stf/screen/screen-directive.js +++ b/res/app/components/stf/screen/screen-directive.js @@ -5,67 +5,110 @@ module.exports = function DeviceScreenDirective($document, ScalingService, $root restrict: 'E', template: require('./screen.jade'), link: function (scope, element, attrs) { - scope.device.promise.then(function (device) { - var canvas = element.find('canvas')[0] - , imageRender = new FastImageRender(canvas, {render: 'canvas'}) - , finger = element.find('span') - , input = element.find('textarea') - , displayWidth = 0 // TODO: cache inside FastImageRender? - , displayHeight = 0 - , cachedDisplayWidth = 0 - , cachedDisplayHeight = 0 - , scaler = ScalingService.coordinator( - device.display.width - , device.display.height - ) + var canvas = element.find('canvas')[0] + , imageRender = new FastImageRender(canvas, {render: 'canvas'}) + , finger = element.find('span') + , input = element.find('textarea') + , displayWidth = 0 // TODO: cache inside FastImageRender? + , displayHeight = 0 + , cachedDisplayWidth = 0 + , cachedDisplayHeight = 0 + , loading = false + , scaler - $rootScope.$on('pageHidden', function () { - scope.canView = false - }) + function sendTouch(type, e) { + var scaled = scaler.coords( + displayWidth + , displayHeight + , e.offsetX + , e.offsetY + ) - $rootScope.$on('pageVisible', function () { - scope.canView = true - }) + finger[0].style.webkitTransform = + 'translate3d(' + e.offsetX + 'px,' + e.offsetY + 'px,0)' - scope.$watch('canView', function (val) { - if (val) { - loadScreen() - } else { - scope.fps = null - //imageRender.clear() - } - }) + scope.control[type]( + scaled.xP * scope.device.display.width + , scaled.yP * scope.device.display.height + ) + } - scope.$watch('showScreen', function (val) { - if (val) { - loadScreen(); - } else { - scope.fps = null - //imageRender.clear() - } - }) + function stopTouch() { + element.removeClass('fingering') + element.unbind('mousemove', moveListener) + $document.unbind('mouseup', upListener) + $document.unbind('mouseleave', upListener) + } - function updateDisplaySize() { - displayWidth = element[0].offsetWidth - displayHeight = element[0].offsetHeight + function updateDisplaySize() { + displayWidth = element[0].offsetWidth + displayHeight = element[0].offsetHeight - // Developer error, let's try to reduce debug time - if (!displayWidth || !displayHeight) { - throw new Error( - 'Unable to update display size; container must have dimensions' - ) - } - } - - function loadScreen() { - imageRender.load(device.display.url + - '?width=' + displayWidth + - '&height=' + displayHeight + - '&time=' + Date.now() + // Developer error, let's try to reduce debug time + if (!displayWidth || !displayHeight) { + throw new Error( + 'Unable to update display size; container must have dimensions' ) } + } + + function downListener(e) { + e.preventDefault() + input[0].focus() + element.addClass('fingering') + sendTouch('touchDown', e) + element.bind('mousemove', moveListener) + $document.bind('mouseup', upListener) + $document.bind('mouseleave', upListener) + } + + function moveListener(e) { + sendTouch('touchMove', e) + } + + function upListener(e) { + sendTouch('touchUp', e) + stopTouch() + } + + function keydownListener(e) { + scope.control.keyDown(e.keyCode) + } + + function keyupListener(e) { + scope.control.keyUp(e.keyCode) + } + + function keypressListener(e) { + e.preventDefault() // no need to change value + scope.control.type(String.fromCharCode(e.charCode)) + } + + function pasteListener(e) { + e.preventDefault() // no need to change value + scope.control.type(e.clipboardData.getData('text/plain')) + } + + function maybeLoadScreen() { + if (!loading && scope.canView && scope.showScreen && scope.device) { + loading = true + imageRender.load(scope.device.display.url + + '?width=' + displayWidth + + '&height=' + displayHeight + + '&time=' + Date.now() + ) + } + } + + function on() { + scaler = ScalingService.coordinator( + scope.device.display.width + , scope.device.display.height + ) imageRender.onLoad = function (image) { + loading = false + if (scope.canView && scope.showScreen) { // Check to set the size only if updated @@ -93,89 +136,69 @@ module.exports = function DeviceScreenDirective($document, ScalingService, $root } // Next please - loadScreen() + maybeLoadScreen() } else { console.log('Nothing to show') } - } imageRender.onError = function () { + loading = false + scope.$apply(function () { scope.displayError = true }) } - function sendTouch(type, e) { - var scaled = scaler.coords( - displayWidth - , displayHeight - , e.offsetX - , e.offsetY - ) - - finger[0].style.webkitTransform = - 'translate3d(' + e.offsetX + 'px,' + e.offsetY + 'px,0)' - - scope.control[type]( - scaled.xP * device.display.width - , scaled.yP * device.display.height - ) - } - - function downListener(e) { - e.preventDefault() - input[0].focus() - element.addClass('fingering') - sendTouch('touchDown', e) - element.bind('mousemove', moveListener) - $document.bind('mouseup', upListener) - $document.bind('mouseleave', upListener) - } - - function moveListener(e) { - sendTouch('touchMove', e) - } - - function upListener(e) { - sendTouch('touchUp', e) - stop() - } - - function stop() { - element.removeClass('fingering') - element.unbind('mousemove', moveListener) - $document.unbind('mouseup', upListener) - $document.unbind('mouseleave', upListener) - } - - scope.$on('$destroy', function () { - imageRender.onLoad = imageRender.onError = null - stop() - }) - - input.bind('keydown', function (e) { - scope.control.keyDown(e.keyCode) - }) - - input.bind('keyup', function (e) { - scope.control.keyUp(e.keyCode) - }) - - input.bind('keypress', function (e) { - e.preventDefault() // no need to change value - scope.control.type(String.fromCharCode(e.charCode)) - }) - - input.bind('paste', function (e) { - e.preventDefault() // no need to change value - scope.control.type(e.clipboardData.getData('text/plain')) - }) - - element.bind('mousedown', downListener) updateDisplaySize() - loadScreen() + maybeLoadScreen() + + input.bind('keydown', keydownListener) + input.bind('keyup', keyupListener) + input.bind('keypress', keypressListener) + input.bind('paste', pasteListener) + element.bind('mousedown', downListener) + } + + function off() { + imageRender.onLoad = imageRender.onError = null + loading = false + stopTouch() + input.unbind('keydown', keydownListener) + input.unbind('keyup', keyupListener) + input.unbind('keypress', keypressListener) + input.unbind('paste', pasteListener) + element.unbind('mousedown', downListener) + } + + scope.$watch('canView', function (val) { + if (val) { + maybeLoadScreen() + } else { + scope.fps = null + //imageRender.clear() + } }) + + scope.$watch('showScreen', function (val) { + if (val) { + maybeLoadScreen() + } else { + scope.fps = null + //imageRender.clear() + } + }) + + scope.$watch('device.using', function(using) { + if (using) { + on() + } + else { + off() + } + }) + + scope.$on('$destroy', off) } } } diff --git a/res/app/components/stf/screen/screen.jade b/res/app/components/stf/screen/screen.jade index 837b8f53..2cf0ce07 100644 --- a/res/app/components/stf/screen/screen.jade +++ b/res/app/components/stf/screen/screen.jade @@ -1,4 +1,4 @@ -canvas(ng-show='ready') +canvas(ng-show='device') div(ng-if='displayError') Screen error textarea(tabindex='-1') -span.finger \ No newline at end of file +span.finger diff --git a/res/app/device-control/device-control-controller.js b/res/app/device-control/device-control-controller.js index cb1096e7..31c631f9 100644 --- a/res/app/device-control/device-control-controller.js +++ b/res/app/device-control/device-control-controller.js @@ -1,17 +1,18 @@ module.exports = function DeviceControlCtrl($scope, $routeParams, $location, DeviceService, GroupService, ControlService) { $scope.control = null - $scope.device = { - promise: DeviceService.get($routeParams.serial, $scope) - .then(function(device) { - return GroupService.invite(device) - }) - .then(function(device) { - $scope.device.value = device - $scope.control = ControlService.forOne(device, device.channel) - return device - }) - .catch(function(err) { - $location.path('/') - }) - } + $scope.device = null + $scope.control = null + + DeviceService.get($routeParams.serial, $scope) + .then(function(device) { + return GroupService.invite(device) + }) + .then(function(device) { + $scope.device = device + $scope.control = ControlService.forOne(device, device.channel) + return device + }) + .catch(function(err) { + $location.path('/') + }) } diff --git a/res/app/device-control/device-control.jade b/res/app/device-control/device-control.jade index 8894a40d..3f59ee54 100644 --- a/res/app/device-control/device-control.jade +++ b/res/app/device-control/device-control.jade @@ -1,4 +1,4 @@ -h1 {{ device.value.serial }} {{ device.value.present ? 'present' : 'absent' }} +h1 {{ device.serial }} {{ device.present ? 'present' : 'absent' }} button(ng-click='showScreen = !showScreen') Show/Hide button(ng-click='control.identify()') Identify