Ejemplo n.º 1
0
 def resize(self, ev=None):
     self.visible_lines = int(self.stage.stageHeight
         / self.cache.lineheight * 2/3)-self.inputlines.length
     self.mybitmap = BitmapData(self.stage.stageWidth,
         self.cache.lineheight*(self.visible_lines+self.inputlines.length)+4,
         False, 0x000000)
     self.bmp.bitmapData = self.mybitmap
     self.refresh()
Ejemplo n.º 2
0
		
		//@desc	This should always be the name of your main project/document class (e.g. GravityHook)
		protected var className:String;
		//@desc	If you want to use site-locking, set your website URL here
		protected var myURL:String;
		
		public function FlxFactory()
		{
			stop();
            stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var tmp:Bitmap;
			if((myURL != null) && (myURL.length > 0) && (root.loaderInfo.url.indexOf(myURL) < 0) && (root.loaderInfo.url.indexOf("localhost") < 0))
			{
				tmp = new Bitmap(new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFFFFFFFF));
				addChild(tmp);
				
				var fmt:TextFormat = new TextFormat();
				fmt.color = 0x000000;
				fmt.size = 16;
				fmt.align = "center";
				fmt.bold = true;
				
				var txt:TextField = new TextField();
				txt.width = tmp.width-16;
				txt.height = tmp.height-16;
				txt.y = 8;
				txt.multiline = true;
				txt.wordWrap = true;
				txt.defaultTextFormat = fmt;
Ejemplo n.º 3
0
 def _make_glyph(self, code):
     self.textfield.text = String.fromCharCode(code)
     bmp = BitmapData(self.textfield.textWidth + 1, self.lineheight, True, 0x0)
     bmp.draw(self.textfield)
     return bmp
Ejemplo n.º 4
0
class Console(Sprite):
    def __init__(self, canvas,
        evaluator=None,
        format="{1} at {file}:{line}: {2}"):
        self.levelcolors = {
            str(LogLevel.DEBUG): 0xC0C0C0,
            str(LogLevel.INFO): 0xC0C0C0,
            str(LogLevel.WARNING): 0xFFFFFF,
            str(LogLevel.ERROR): 0xFFC0C0,
            str(LogLevel.CRITICAL): 0xFFC0C0,
            }
        self.background_color = 0x000000
        self.input_color = 0xFFFFFF
        self.canvas = canvas
        self.evaluator = evaluator
        self.visible = False
        self.lines = []
        self.visible_lines = 0
        self.inputlines = ['']
        self.cursor = Point(0, 0)
        self.max_history = 100
        self.max_lines = 11000
        self.min_lines = 10000
        self.history = []
        self.firstvisible = -1
        self.history_index = 0
        self.cache = GlyphCache()
        self.format = format
        self.bmp = Bitmap(None, PixelSnapping.ALWAYS, False)
        self.addChild(self.bmp)
        canvas.stage.addEventListener(KeyboardEvent.KEY_DOWN, self.key)

    def log_record(self, rec):
        s = self.format.format(rec, LogLevel.name[rec.level], rec.format())
        self.add_text(s, self.levelcolors[rec.level])

    def add_text(self, text, color=0xFFFFFF):
        for rline in values(text.split('\n')):
            self.lines.push(Line(color, rline))
        if self.visible:
            if self.lines.length > self.max_lines:
                self.lines.splice(0, self.max_lines - self.min_lines)
            if self.firstvisible < 0 \
                or self.lines.length <= self.firstvisible+self.visible_lines:
                self.refresh()

    def clearinput(self):
        self.inputlines = [""]
        del self._storedlines
        self.cursor = Point(0, 0)
        self.resize()

    def key(self, event):
        if self.visible:
            if event.keyCode == 192: # tilde
                self.hide()
            elif event.keyCode == 13:
                if event.shiftKey or (self.evaluator \
                    and self.evaluator.need_continue(self.inputlines)):
                    self.inputlines.splice(self.cursor.y+1, 0, "")
                    self.cursor.y += 1
                    self.cursor.x = 0
                    self.resize()
                else:
                    code = self.inputlines.join('\n')
                    if self.evaluator:
                        self.evaluator.eval(self, self.inputlines)
                    else:
                        Log.info(code)
                    self.history.push(code)
                    self.history_index = -1
            elif event.keyCode == 33: # pageup
                oldline = self.firstvisible
                if oldline < 0:
                    self.firstvisible = max(0,
                        self.lines.length - self.visible_lines*3/2)
                else:
                    self.firstvisible = max(oldline - self.visible_lines/2, 0)
                if oldline != self.firstvisible:
                    self.refresh()
            elif event.keyCode == 34: # pagedown
                oldline = self.firstvisible
                if oldline < 0:
                    return
                self.firstvisible = oldline + self.visible_lines
                if self.firstvisible + self.visible_lines > self.lines.length:
                    self.firstvisible = -1
                if oldline != self.firstvisible:
                    self.refresh()
            elif event.keyCode == 37: # left
                if self.cursor.x == 0:
                    if self.inputlines.length > 1 and self.cursor.y:
                        self.cursor.y -= 1
                        self.cursor.x = self.inputlines[self.cursor.y].length
                else:
                    self.cursor.x -= 1
                self.refreshinput()
            elif event.keyCode == 39: # right
                if self.cursor.x == self.inputlines[self.cursor.y].length:
                    if self.inputlines.length > self.cursor.y+1:
                        self.cursor.y += 1
                        self.cursor.x = 0
                else:
                    self.cursor.x += 1
                self.refreshinput()
            elif event.keyCode == 38: # up
                if event.shiftKey:
                    self.cursor.y = max(self.cursor.y-1, 0)
                else:
                    if self.history_index < 0:
                        self._storedlines = self.inputlines
                        self.history_index = self.history.length
                    if self.history_index > 0:
                        self.inputlines = self.history[self.history_index-1]\
                            .split('\n')
                        self.cursor = Point(
                            self.inputlines[self.inputlines.length-1].length,
                            self.inputlines.length-1)
                        self.history_index -= 1
                self.refreshinput()
            elif event.keyCode == 40: # down
                if event.shiftKey:
                    self.cursor.y = min(self.cursor.y+1,
                        self.inputlines.length-1)
                else:
                    if self.history_index+1 < self.history.length:
                        self.inputlines = self.history[self.history_index+1]\
                            .split('\n')
                        self.history_index += 1
                    elif self._storedlines:
                        self.inputlines = self._storedlines
                        self.history_index = -1
                        del self._stored_lines
                    self.cursor = Point(
                        self.inputlines[self.inputlines.length-1].length,
                        self.inputlines.length-1)
                self.refreshinput()
            elif event.keyCode == 0x08: #backspace
                if self.cursor.x:
                    ln = self.inputlines[self.cursor.y]
                    if self.cursor.x == ln.length:
                        nln = ln.substr(0, ln.length-1)
                    else:
                        nln = ln.substr(0, self.cursor.x-1)+ln.substr(self.cursor.x)
                    self.inputlines[self.cursor.y] = nln
                    self.cursor.x -= 1
                    self.refreshinput()
                elif self.cursor.y:
                    self.cursor.y -= 1
                    prevline = self.inputlines[self.cursor.y-1]
                    self.cursor.x = prevline.length
                    self.inputlines[self.cursor.y-1] \
                        = prevline + self.inputlines[self.cursor.y]
                    self.resize()
            elif event.charCode >= 0x20: # all chars starting from space
                ln = self.inputlines[self.cursor.y]
                ch = String.fromCharCode(event.charCode)
                if self.cursor.x == ln.length:
                    nln = ln+ch
                else:
                    nln = ln.substr(0,self.cursor.x)+ch+ln.substr(self.cursor.x)
                self.inputlines[self.cursor.y] = nln
                self.cursor.x += 1
                self.refreshinput()
        else:
            if event.keyCode == 192: # tilde
                self.show()

    def show(self):
        self.visible = True
        self.canvas.addChild(self)
        self.stage.addEventListener(Event.RESIZE, self.resize)
        self.resize()

    def hide(self):
        self.visible = False
        self.bmp.bitmapData.dispose()
        self.stage.removeEventListener(Event.RESIZE, self.resize)
        self.canvas.removeChild(self)

    def resize(self, ev=None):
        self.visible_lines = int(self.stage.stageHeight
            / self.cache.lineheight * 2/3)-self.inputlines.length
        self.mybitmap = BitmapData(self.stage.stageWidth,
            self.cache.lineheight*(self.visible_lines+self.inputlines.length)+4,
            False, 0x000000)
        self.bmp.bitmapData = self.mybitmap
        self.refresh()

    def refresh(self):
        self.mybitmap.fillRect(self.mybitmap.rect, self.background_color)
        if self.lines.length < self.visible_lines:
            for i in range(self.lines.length):
                line = self.lines[i]
                self.cache.draw_line(line.text, 2, i*self.cache.lineheight+2,
                    line.color, self.mybitmap)
        else:
            if self.firstvisible < 0:
                start = self.lines.length - self.visible_lines
            else:
                start = self.firstvisible
            for i in range(self.visible_lines):
                line = self.lines[start + i]
                self.cache.draw_line(line.text, 2, i*self.cache.lineheight+2,
                    line.color, self.mybitmap)
        self.refreshinput()

    def refreshinput(self):
        self.mybitmap.fillRect(Rectangle(0, self.cache.lineheight*self.visible_lines,
            self.stage.stageWidth, self.inputlines.length*self.cache.lineheight+2), self.background_color)
        if self.evaluator:
            ps1 = self.evaluator.ps1
            ps2 = self.evaluator.ps2
        else:
            ps1 = ''
            ps2 = ''
        for i in range(self.inputlines.length):
            line = self.inputlines[i]
            if i == 0:
                line = ps1 + line
                cx = self.cursor.x+ps1.length if self.cursor.y == i else undefined
            else:
                line = ps2 + line
                cx = self.cursor.x+ps2.length if self.cursor.y == i else undefined
            self.cache.draw_line(line, 2,
                (self.visible_lines+i)*self.cache.lineheight+2,
                self.input_color, self.mybitmap, cx)