#!/usr/bin/env python
import cairo
import pygame
import array
import math
import sys

import direct.directbase.DirectStart
from pandac.PandaModules import *

from jgui.surface.surface import *

Width, Height = 1024, 1024
props = WindowProperties()
props.setSize(1024,1024)
base.setBackgroundColor(0.5,1,1,1)
base.cam.setPos(0,-10,0)
base.win.requestProperties(props)
c = CardMaker('plane')
c.setFrame(-1, 1, 1, -1)
c.setHasUvs(True)
screen = render2d.attachNewNode(c.generate())
#screen = loader.loadModel('plane')
screen.setTransparency(TransparencyAttrib.MAlpha)
screen.setTwoSided(True)
#screen.setScale(2)

surf = Surface([Width, Height])

cairoTexture = Texture()
cairoTexture.setXSize(Width)
cairoTexture.setYSize(Height)
cairoTexture.setFormat(cairoTexture.FRgba8)
cairoTexture.setup2dTexture(Width, Height, Texture.TUnsignedByte, Texture.FRgba32)

screen.setTexture(cairoTexture)
#screen.reparentTo(render2d)
def move():
    print surf.root_window.children[0].position
    surf.root_window.children[0].position += [1, 1]
def click():
    surf.root_window.injectMouseDown('mouse1')
def up():
    surf.root_window.injectMouseUp('mouse1')
base.accept('d-repeat', move)
base.accept('mouse1', click)
base.accept('mouse1-up', up)

def drawall(task):
    surf.draw()
    if base.mouseWatcherNode.hasMouse():
        x = base.win.getXSize() * (1 + base.mouseWatcherNode.getMouseX()) / 2
        y = base.win.getYSize() * (1 - base.mouseWatcherNode.getMouseY()) / 2
        surf.root_window.injectMousePosition([x, y])
    cairoTexture.setRamImage(surf.csurface.get_data())
    return task.cont

taskMgr.add(drawall, 'draw')
run()

@@ -1066,14 +1066,16 @@
 ject
-M
+_m
 ouse
-D
+_d
 own(
@@ -1080,17 +1080,21 @@
 n('mouse
-1
+-left
 ')%0Adef u
@@ -1129,22 +1129,24 @@
 ject
-M
+_m
 ouse
-U
+_u
 p('mouse
 1')%0A
@@ -1141,17 +1141,21 @@
 p('mouse
-1
+-left
 ')%0Abase.
@@ -1273,16 +1273,114 @@
 .draw()%0A
+    cairoTexture.setRamImage(surf.csurface.get_data())%0A    return task.cont%0A%0Adef mousemove(task):%0A
     if b
@@ -1603,14 +1603,16 @@
 ject
-M
+_m
 ouse
-P
+_p
 osit
@@ -1631,80 +1631,136 @@
     
-cairoTexture.setRamImage(surf.csurface.get_data())%0A    return task.cont%0A
+return task.cont%0A%0AtaskMgr.setupTaskChain('move_chain', numThreads=1)%0AtaskMgr.add(mousemove, 'mousemove', taskChain='move_chain')
 %0Atas

Put mouse position setting in a different thread.