~starplant/treegui/oldtreegui

« back to all changes in this revision

Viewing changes to _gui/theme.py

  • Committer: me
  • Date: 2009-03-27 07:36:55 UTC
  • Revision ID: me@centurion-20090327073655-wmo1eirlzbnkhote
 old code 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pandac.PandaModules import *
 
2
 
 
3
class Stretch:
 
4
    
 
5
    def __init__(self,xStart,yStart,xEnd,yEnd):
 
6
        self.xStart,self.yStart,self.xEnd,self.yEnd = xStart,yStart,xEnd,yEnd
 
7
 
 
8
    def draw(self,theme,pos,size):
 
9
        x,y = pos[0],pos[1]
 
10
        xs,ys = size[0],size[1]
 
11
        u,v,us,vs = self.xStart,self.yStart,self.xEnd,self.yEnd
 
12
        theme.rectStreatch((x,y,xs,ys),(u,v,us,vs))
 
13
 
 
14
class StretchBorder:
 
15
    
 
16
    def __init__(self,xStart,yStart,xEnd,yEnd,border):
 
17
        self.xStart,self.yStart,self.xEnd,self.yEnd,self.border = xStart,yStart,xEnd,yEnd,border
 
18
 
 
19
    def draw(self,theme,pos,size):
 
20
        u,v,us,vs,b = self.xStart,self.yStart,self.xEnd,self.yEnd,self.border
 
21
        self.drawBlock(theme,pos,size,u,v,us,vs) # center
 
22
        x,y = pos[0],pos[1]
 
23
        xs,ys = size[0],size[1]
 
24
        
 
25
        self.drawBlock(theme,Vec2(x,y-b),Vec2(xs,b),u,v-b,us,b) # N
 
26
        self.drawBlock(theme,Vec2(x,y+ys),Vec2(xs,b),u,v+vs,us,b) # S
 
27
        self.drawBlock(theme,Vec2(x-b,y),Vec2(b,ys),u-b,v,b,vs) # W
 
28
        self.drawBlock(theme,Vec2(x+xs,y),Vec2(b,ys),u+us+b,v,b,vs) # E
 
29
 
 
30
        self.drawBlock(theme,Vec2(x-b,y-b),Vec2(b,b),u-b,v-b,b,b) # NW
 
31
        self.drawBlock(theme,Vec2(x+xs,y+ys),Vec2(b,b),u+us+b,v+vs,b,b) # SE 
 
32
        self.drawBlock(theme,Vec2(x-b,y+ys),Vec2(b,b),u-b,v+vs,b,b) # SW
 
33
        self.drawBlock(theme,Vec2(x+xs,y-b),Vec2(b,b),u+us+b,v-b,b,b) # NW
 
34
        
 
35
    def drawBlock(self,theme,pos,size,u,v,us,vs):
 
36
        x,y = pos[0],pos[1]
 
37
        xs,ys = size[0],size[1]
 
38
        #u,v,us,vs = self.xStart,self.yStart,self.xEnd,self.yEnd
 
39
        theme.rectStreatch((x,y,xs,ys),(u,v,us,vs))
 
40
        
 
41
class Tiled:
 
42
    
 
43
    def __init__(self,xStart,yStart,xEnd,yEnd):
 
44
        self.xStart,self.yStart,self.xEnd,self.yEnd = xStart,yStart,xEnd,yEnd
 
45
 
 
46
    def draw(self,theme,pos,size):
 
47
        x,y = pos[0],pos[1]
 
48
        xs,ys = size[0],size[1]
 
49
        z = 0
 
50
        u,v,us,vs = self.xStart,self.yStart,self.xEnd,self.yEnd
 
51
        xFit = xs/us
 
52
        yFit = ys/vs
 
53
        xPos = x
 
54
        while xFit > 0:
 
55
            yPos = y
 
56
            yFit = ys/vs           
 
57
            while yFit > 0:
 
58
                fixed_us,fixed_vs = us,vs
 
59
                if xFit < 1: fixed_us = xs%us 
 
60
                if yFit < 1: fixed_vs = ys%vs
 
61
                theme.rect((xPos,yPos,fixed_us,fixed_vs),(u,v))
 
62
                yPos += vs
 
63
                yFit -= 1
 
64
            xPos += us
 
65
            xFit -= 1
 
66
        
 
67
class TileBorder:
 
68
    
 
69
    def __init__(self,xStart,yStart,xEnd,yEnd,border):
 
70
        self.xStart,self.yStart,self.xEnd,self.yEnd,self.border = xStart,yStart,xEnd,yEnd,border
 
71
 
 
72
    def draw(self,theme,pos,size):
 
73
        u,v,us,vs,b = self.xStart,self.yStart,self.xEnd,self.yEnd,self.border
 
74
        self.drawBlock(theme,pos,size,u,v,us,vs) # center
 
75
        x,y = pos[0],pos[1]
 
76
        xs,ys = size[0],size[1]
 
77
        
 
78
        self.drawBlock(theme,Vec2(x,y-b),Vec2(xs,b),u,v-b,us,b) # N
 
79
        self.drawBlock(theme,Vec2(x,y+ys),Vec2(xs,b),u,v+vs,us,b) # S
 
80
        self.drawBlock(theme,Vec2(x-b,y),Vec2(b,ys),u-b,v,b,vs) # W
 
81
        self.drawBlock(theme,Vec2(x+xs,y),Vec2(b,ys),u+us+b,v,b,vs) # E
 
82
 
 
83
        self.drawBlock(theme,Vec2(x-b,y-b),Vec2(b,b),u-b,v-b,b,b) # NW
 
84
        self.drawBlock(theme,Vec2(x+xs,y+ys),Vec2(b,b),u+us+b,v+vs,b,b) # SE 
 
85
        self.drawBlock(theme,Vec2(x-b,y+ys),Vec2(b,b),u-b,v+vs,b,b) # SW
 
86
        self.drawBlock(theme,Vec2(x+xs,y-b),Vec2(b,b),u+us+b,v-b,b,b) # NW
 
87
        
 
88
 
 
89
    def drawBlock(self,theme,pos,size,u,v,us,vs):
 
90
        x,y = pos[0],pos[1]
 
91
        xs,ys = size[0],size[1]
 
92
        z = 0
 
93
        xFit = xs/us
 
94
        yFit = ys/vs
 
95
        xPos = x
 
96
        while xFit > 0:
 
97
            yPos = y
 
98
            yFit = ys/vs           
 
99
            while yFit > 0:
 
100
                fixed_us,fixed_vs = us,vs
 
101
                if xFit < 1: fixed_us = xs%us 
 
102
                if yFit < 1: fixed_vs = ys%vs
 
103
                theme.rect((xPos,yPos,fixed_us,fixed_vs),(u,v))
 
104
                yPos += vs
 
105
                yFit -= 1
 
106
            xPos += us
 
107
            xFit -= 1
 
108
 
 
109
class TileBarX(TileBorder):
 
110
    def draw(self,theme,pos,size):
 
111
        u,v,us,vs,b = self.xStart,self.yStart,self.xEnd,self.yEnd,self.border
 
112
        self.drawBlock(theme,pos,size,u,v,us,vs) # center
 
113
        x,y = pos[0],pos[1]
 
114
        xs,ys = size[0],size[1]
 
115
        self.drawBlock(theme,Vec2(x-b,y),Vec2(b,ys),u-b,v,b,vs) # W
 
116
        self.drawBlock(theme,Vec2(x+xs,y),Vec2(b,ys),u+us+b,v,b,vs) # E
 
117
 
 
118
class Theme:
 
119
    
 
120
    TEXTURE = "data/guipa2.png"
 
121
    CHECKON = Stretch(360,40,20,20)
 
122
    CHECKOFF = Stretch(400,40,20,20)
 
123
    RADIOON = Stretch(280,40,20,20)
 
124
    RADIOOFF = Stretch(320,40,20,20)
 
125
    PANDA = Tiled(280,80,20,20)
 
126
    INPUT = TileBorder(20,300,180,180,20)
 
127
    FRAME = StretchBorder(20,300,180,180,20)
 
128
    FORM = StretchBorder(20,20,202-20,182-20,20)
 
129
    FRAMEBAR = TileBarX(280,480,140,20,20)
 
130
    BUTTON = Tiled(320,420,20,20)
 
131
    BUTTON = TileBarX(280,480,140,20,20)
 
132
    BUTTON = TileBorder(288,170,490-288,190-170,5)
 
133
    #BUTTON = Tiled(280,80,20,20)
 
134
    #BUTTON = TileBorder(20,20,202-20,182-20,20)
 
135
    DOWN = Tiled(300,160,200,40)
 
136
    TEXTCOLOR = (1,1,1,1)
 
137
    LABLECOLOR = TEXTCOLOR
 
138
    INPUTCOLOR = TEXTCOLOR
 
139
            
 
140
    
 
141
            
 
142
    def __init__(self):
 
143
        import __builtin__
 
144
        __builtin__.theme = self
 
145
        
 
146
        self.texture = loader.loadTexture(self.TEXTURE)   
 
147
        self.tx = float(self.texture.getXSize())
 
148
        self.ty = float(self.texture.getYSize())
 
149
        self.resetZ()
 
150
        self.first = True
 
151
        self.boxes = []
 
152
        
 
153
    def add(self,box):
 
154
        self.boxes.append(box)
 
155
        
 
156
    def rect(self,(x,y,xs,ys),(u,v)):
 
157
        us = xs
 
158
        vs = ys
 
159
        self.rectStreatch((x,y,xs,ys),(u,v,us,vs))
 
160
        
 
161
    def rectStreatch(self,(x,y,xs,ys),(u,v,us,vs)):
 
162
        z = 0
 
163
        color = Vec4(1,1,1,1)
 
164
        v1 = Vec3(x,z,y)
 
165
        v2 = Vec3(x+xs,z,y)
 
166
        v3 = Vec3(x+xs,z,y+ys)
 
167
        v4 = Vec3(x,z,y+ys)
 
168
        u,v,us,vs = u/self.tx,1-v/self.ty,(u+us)/self.tx,1-(v+vs)/self.ty,
 
169
        self.vertex.addData3f(v1); self.pigment.addData4f(color);   self.uv.addData2f(u,v)
 
170
        self.vertex.addData3f(v2); self.pigment.addData4f(color);   self.uv.addData2f(us,v)
 
171
        self.vertex.addData3f(v3); self.pigment.addData4f(color);   self.uv.addData2f(us,vs)
 
172
        self.vertex.addData3f(v3); self.pigment.addData4f(color);   self.uv.addData2f(us,vs)
 
173
        self.vertex.addData3f(v4); self.pigment.addData4f(color);   self.uv.addData2f(u,vs)
 
174
        self.vertex.addData3f(v1); self.pigment.addData4f(color);   self.uv.addData2f(u,v)
 
175
        self.number += 2
 
176
        
 
177
    def resetZ(self):
 
178
        self.z = 0
 
179
           
 
180
    def fixZ(self,thing):
 
181
        if thing.geom:
 
182
            thing.geom.setBin("fixed",self.z)
 
183
            self.z += 1
 
184
        if "textNode" in thing.__dict__:
 
185
            thing.textNode.setBin("fixed",self.z)
 
186
            self.z += 1
 
187
        
 
188
    def generate(self,box):
 
189
        vdata = GeomVertexData('shadow', GeomVertexFormat.getV3c4t2() , Geom.UHStatic)
 
190
        self.vertex = GeomVertexWriter(vdata, 'vertex')
 
191
        self.pigment = GeomVertexWriter(vdata, 'color')
 
192
        self.uv = GeomVertexWriter(vdata, 'texcoord')
 
193
        self.number = 0
 
194
        color = Vec4(1,1,1,1)
 
195
        name,pos,size,thing = box
 
196
        
 
197
        if not name in self.__class__.__dict__:
 
198
            return None
 
199
        drawer = self.__class__.__dict__[name]
 
200
        drawer.draw(self,pos,size)
 
201
       
 
202
        prim = GeomTriangles(Geom.UHStatic)
 
203
        for n in range(self.number):
 
204
            prim.addVertices(n*3,n*3+1,n*3+2)
 
205
        prim.closePrimitive()
 
206
        if self.number == 0 : 
 
207
            return None
 
208
        geom = Geom(vdata)
 
209
        geom.addPrimitive(prim)
 
210
        
 
211
        geomnode = GeomNode('gnode')
 
212
        geomnode.addGeom(geom)
 
213
        
 
214
        guipart = NodePath("guisys%i"%id(self))
 
215
        guipart.setTexture(self.texture)
 
216
        guipart.attachNewNode(geomnode)
 
217
        guipart.setTransparency(True)
 
218
        guipart.setDepthWrite(False)
 
219
        guipart.setDepthTest(False)
 
220
        guipart.setTwoSided(True)
 
221
        guipart.setAttrib(LightAttrib.makeAllOff())
 
222
        guipart.setTexture(self.texture)
 
223
        guipart.setBin("fixed",0)
 
224
        return guipart
 
225
    
 
226
    def drawSet(self,boxes):
 
227
        vdata = GeomVertexData('shadow', GeomVertexFormat.getV3c4t2() , Geom.UHStatic)
 
228
        self.vertex = GeomVertexWriter(vdata, 'vertex')
 
229
        self.pigment = GeomVertexWriter(vdata, 'color')
 
230
        self.uv = GeomVertexWriter(vdata, 'texcoord')
 
231
        self.number = 0
 
232
        color = Vec4(1,1,1,1)
 
233
        
 
234
        for box in boxes:
 
235
            name,pos,size,thing = box
 
236
            if "textNode" in thing.__dict__:
 
237
                thing.textNode.setY(self.z-.001)
 
238
            if not name in self.__class__.__dict__:
 
239
                continue
 
240
            drawer = self.__class__.__dict__[name]
 
241
            drawer.draw(self,pos,size)
 
242
            #self.z -= .1
 
243
        prim = GeomTriangles(Geom.UHStatic)
 
244
        for n in range(self.number):
 
245
            prim.addVertices(n*3,n*3+1,n*3+2)
 
246
        prim.closePrimitive()
 
247
        geom = Geom(vdata)
 
248
        geom.addPrimitive(prim)
 
249
        geomnode = GeomNode('gnode')
 
250
        geomnode.addGeom(geom)
 
251
        return geomnode
 
252
 
 
253
    def draw(self):
 
254
        self.resetZ()
 
255