UNPKG

dreemgl

Version:

DreemGL is an open-source multi-screen prototyping framework for mediated environments, with a visual editor and shader styling for webGL and DALi runtimes written in JavaScript. As a toolkit for gpu-accelerated multiscreen development, DreemGL includes

281 lines (233 loc) 8.4 kB
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others. Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/ define.class(function(require){ var parse = new (require('$system/parse/onejsparser'))() this.atConstructor = function(cursorset){ this.cursorset = cursorset this.editor = cursorset.editor this.start = 0 this.end = 0 this.max = 0 } Object.defineProperty(this, 'lo', { get:function(){ return this.end > this.start? this.start: this.end }, set: function(v){ if(this.end > this.start) this.start = v else this.end = v } }) Object.defineProperty(this, 'hi', { get: function(){ return this.end > this.start? this.end: this.start }, set: function(v){ if(this.end > this.start) this.end = v else this.start = v } }) Object.defineProperty(this, 'span', { get:function(){ return abs(this.end - this.start) } }) this.moveToOffset = function(offset){ this.start = this.end = offset this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveLeft = function(only_end){ this.end = this.end - 1 if(this.end < 0) this.end = 0 if(!only_end){ if(this.editor.atMoveLeft) this.end = this.editor.atMoveLeft(this.end) this.start = this.end } // this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveRight = function(only_end){ this.end = this.end + 1 if(this.end > this.editor.textbuf.char_count) this.end = this.editor.textbuf.char_count if(!only_end){ if(this.editor.atMoveRight) this.end = this.editor.atMoveRight(this.end) this.start = this.end } this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveUp = function(only_end, lines){ if(!lines) lines = 1 var rect = this.editor.textbuf.cursorRect(this.end) //console.log(max, rect.y + .5*rect.h - lines * cursorset.text_layer.line_height) //cursorset.text_layer.debugChunks() if(this.editor.textbuf.charCodeAt(this.end-1) === 10 && this.editor.textbuf.charCodeAt(this.end-2) === 10) this.end-- else this.end = this.editor.textbuf.offsetFromPos(this.max, rect.y + .5*rect.h - lines * this.editor.textbuf.line_height) if(this.end < 0) this.end = 0 if(!only_end) this.start = this.end } this.moveDown = function(only_end, lines){ if(!lines) lines = 1 var rect = this.editor.textbuf.cursorRect(this.end) //if(this.editor.textbuf.charCodeAt(this.end+1) === 10 && this.editor.textbuf.charCodeAt(this.end+2) === 10) this.end++ //else this.end = this.editor.textbuf.offsetFromPos(this.max, rect.y + .5*rect.h + lines * this.editor.textbuf.line_height) if(this.end < 0) this.end = this.editor.textbuf.char_count if(!only_end) this.start = this.end } this.moveTo = function(x, y, only_end){ var off = this.editor.textbuf.offsetFromPos(x, y) if(off<0){ if(off === -4 || off === -3) off = this.editor.textbuf.lengthQuad() else off = 0 } var change = this.end != off this.end = off if(!only_end) change = this.start != this.end || change, this.start = this.end var r = this.editor.textbuf.cursorRect(off) this.max = r.x } this.moveLeftWord = function(only_end){ var pos = editor.scanLeftWord(this.end) if(pos == this.end) this.end -- else this.end = pos if(!only_end) this.start = this.end this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveRightWord = function(only_end){ var pos = editor.scanRightWord(this.end) if(pos == this.end) this.end ++ else this.end = pos if(!only_end) this.start = this.end this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveLeftLine = function(only_end){ // if we are a tab we scan to the right. this.end = this.editor.scanLeftLine(this.end) if(!only_end) this.start = this.end this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveRightLine = function(only_end){ this.end = this.editor.scanRightLine(this.end) if(!only_end) this.start = this.end this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveTop = function(only_end){ this.end = 0 if(!only_end) this.start = this.end this.max = this.editor.textbuf.cursorRect(this.end).x } this.moveBottom = function(only_end){ this.end = this.editor.textbuf.char_count if(!only_end) this.start = this.end this.max = this.editor.textbuf.cursorRect(this.end).x } // we need to make a whole bunch of these things. this.deleteRange = function(from, to){ this.editor.addUndoInsert(from, to) this.editor.textbuf.removeText(from, to) this.cursorset.delta -= to - from this.start = this.end = from this.editor.forkRedo() this.max = this.editor.textbuf.cursorRect(this.end).x this.editor.textChanged() } this.deleteWord = function(){ var my = this.editor.scanRightWord(hi) if(my == this.lo) return this.delete() this.deleteRange(this.lo, my) } this.deleteLine = function(){ this.deleteRange(this.lo, this.editor.scanRightLine(hi)) } this.backspaceLine = function(){ this.deleteRange(this.editor.scanLeftLine(this.lo), this.hi) } this.backspaceWord = function(){ var my = this.editor.scanLeftWord(this.lo) if(my == this.hi) return this.backspace() this.deleteRange(my, this.hi) } this.selectWord = function(){ this.start = this.editor.scanLeftWord(this.lo) this.end = this.editor.scanRightWord(this.hi) this.max = this.editor.textbuf.cursorRect(this.end).x } this.selectLine = function(){ this.start = this.editor.scanLeftLine(this.lo) this.end = this.editor.scanRightLine(this.hi) this.max = this.editor.textbuf.cursorRect(this.end).x } this.selectAll = function(){ this.start = 0 this.end = this.editor.textbuf.char_count this.max = this.editor.textbuf.cursorRect(this.end).x } this.delete = function(){ if(this.start != this.end) return this.deleteRange(this.lo, this.hi) // otherwise we have to delete the character upnext this.editor.addUndoInsert(this.end, this.end + 1) this.editor.textbuf.removeText(this.end, this.end + 1) this.cursorset.delta -= 1 this.editor.forkRedo() this.max = this.editor.textbuf.cursorRect(this.end).x this.editor.textChanged() } this.backspace = function(){ if(this.start != this.end) return this.deleteRange(this.lo, this.hi) this.start += this.cursorset.delta this.end += this.cursorset.delta if(this.editor.stripNextOnBackspace){ if(this.editor.stripNextOnBackspace(this.lo - 1)){ this.hi++ } var t if(parse.isNonNewlineWhiteSpace(this.editor.textbuf.charCodeAt(this.lo - 1))){ while(t = parse.isNonNewlineWhiteSpace(this.editor.textbuf.charCodeAt(this.lo - 1))){ this.lo = this.lo - 1 if(t == 2) break } } } if(this.lo == 0) return this.editor.addUndoInsert(this.lo -1, this.hi) this.editor.textbuf.removeText(this.lo - 1, this.hi) this.cursorset.delta -= this.span this.editor.forkRedo() this.start = this.end = this.lo - 1 this.max = this.editor.textbuf.cursorRect(this.end).x this.editor.textChanged() } this.insert = function(text){ var cdelta = 0 if(this.editor.processInsert){ var pi = this.editor.processInsert(this.lo, this.hi, text) text = pi[0] cdelta = pi[1] } this.start += this.cursorset.delta this.end += this.cursorset.delta if(this.start != this.end){ this.editor.addUndoInsert(this.lo, this.hi) this.editor.textbuf.removeText(this.lo, this.hi) this.cursorset.delta -= this.span } if(text.length){ var len = this.editor.textbuf.insertText(this.lo, text) this.cursorset.delta += len this.editor.addUndoDelete(this.lo, this.lo + len) } this.editor.forkRedo() this.start = this.end = this.lo + text.length + cdelta this.max = this.editor.textbuf.cursorRect(this.end).x this.editor.textChanged() } this.isSelection = function(){ return this.start !== this.end } this.isCursor = function(){ return this.start === this.end } })