Updated test code to be up to date with the changes in the surface.py

code.
Added resize window call.
This commit is contained in:
Joey Payne 2014-02-03 16:05:46 -07:00
commit 8bfce9538c
2 changed files with 76 additions and 43 deletions

View file

@ -9,11 +9,12 @@ class JGUIWidget(gtk.DrawingArea):
def __init__(self, width, height):
super(JGUIWidget, self).__init__()
self.surf = Surface([width, height])
self.surf = TestSurface([width, height])
self.connect("expose_event", self.expose)
self.connect("motion_notify_event", self.mousemoved)
self.connect("button_press_event", self.mouse_down)
self.connect("button_release_event", self.mouse_up)
self.connect("configure_event", self.resize_win)
self.add_events(gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.POINTER_MOTION_MASK)
gobject.timeout_add(int((1.0/60.0)*1000), self.tick)
self.width, self.height = width, height
@ -25,6 +26,9 @@ class JGUIWidget(gtk.DrawingArea):
def mouse_down(self, widget, event):
self.surf.inject_mouse_down(self.buttons[event.button])
def resize_win(self, widget, event):
self.surf.notify_window_resize(event.width, event.height)
def tick(self):
self.alloc = self.get_allocation()
rect = gdk.Rectangle(self.alloc.x, self.alloc.y, self.alloc.width, self.alloc.height)

113
test.py
View file

@ -5,57 +5,86 @@ import array
import math
import sys
import direct.directbase.DirectStart
from direct.showbase.ShowBase import ShowBase
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)
class Test(ShowBase):
def __init__(self, width=1024, height=1024):
ShowBase.__init__(self)
self._render = True
self.width = width
self.height = height
props = WindowProperties()
props.setSize(width, height)
base.setBackgroundColor(0.5,1,1,1)
base.cam.setPos(0,-3.7,0)
base.win.requestProperties(props)
base.disableMouse()
c = CardMaker('plane')
c.setFrame(-1, 1, 1, -1)
c.setHasUvs(True)
screen = render2d.attachNewNode(c.generate())
screen.setTransparency(TransparencyAttrib.MAlpha)
screen.setTwoSided(True)
surf = Surface([Width, Height])
self.surf = TestSurface([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)
cairoTexture = Texture()
cairoTexture.setXSize(width)
cairoTexture.setYSize(height)
cairoTexture.setFormat(cairoTexture.FRgba8)
cairoTexture.setup2dTexture(width, height, Texture.TUnsignedByte, Texture.FRgba32)
self.cairoTexture = cairoTexture
screen.setTexture(cairoTexture)
self.screen = screen
#screen.reparentTo(render2d)
def click():
surf.inject_mouse_down('mouse-left')
def up():
surf.inject_mouse_up('mouse-left')
self.accept('mouse1', self.click)
self.accept('mouse1-up', self.up)
base.accept('mouse1', click)
base.accept('mouse1-up', up)
def drawall(task):
surf.draw()
cairoTexture.setRamImage(surf.csurface.get_data())
return task.cont
taskMgr.add(self.mousemove, 'mousemove')
taskMgr.add(self.drawall, 'draw')
self.accept('window-event', self.windowEvent)
def mousemove(task):
if base.mouseWatcherNode.hasMouse():
x = base.win.getXSize() * (1 + base.mouseWatcherNode.getMouseX()) / 2
y = base.win.getYSize() * (1 - base.mouseWatcherNode.getMouseY()) / 2
surf.inject_mouse_position([x, y])
return task.cont
def click(self):
self.surf.inject_mouse_down('mouse-left')
taskMgr.add(mousemove, 'mousemove')
taskMgr.add(drawall, 'draw')
run()
def up(self):
self.surf.inject_mouse_up('mouse-left')
def windowEvent(self, window):
ShowBase.windowEvent(self, window) #call the super method to handle all other cases
if window.isClosed():
self._render = False
return
width, height = window.getXSize(), window.getYSize()
if width != self.width or height != self.height:
self.width, self.height = width, height
cairoTexture = Texture()
cairoTexture.setXSize(self.width)
cairoTexture.setYSize(self.height)
cairoTexture.setFormat(cairoTexture.FRgba8)
cairoTexture.setup2dTexture(width, height, Texture.TUnsignedByte, Texture.FRgba32)
self.cairoTexture = cairoTexture
self.screen.setTexture(cairoTexture)
self.surf.notify_window_resize(window.getXSize(), window.getYSize())
def drawall(self, task):
if self._render:
self.surf.draw()
self.cairoTexture.setRamImage(self.surf.csurface.get_data())
return task.cont
def mousemove(self, task):
if self._render:
if base.mouseWatcherNode.hasMouse():
x = base.win.getXSize() * (1 + base.mouseWatcherNode.getMouseX()) / 2
y = base.win.getYSize() * (1 - base.mouseWatcherNode.getMouseY()) / 2
self.surf.inject_mouse_position([x, y])
return task.cont
if __name__ == '__main__':
Test().run()