asgard 0.2.5 has been moved to tags/trash
[asgard.git] / drawable.py
blob57b71bb30328939e5c415cc5f41a1dbc51dc6a92
1 ########################################################
2 #Copyright (c) 2006 Russ Adams, Sean Eubanks, Asgard Contributors
3 #This file is part of Asgard.
5 #Asgard is free software; you can redistribute it and/or modify
6 #it under the terms of the GNU General Public License as published by
7 #the Free Software Foundation; either version 2 of the License, or
8 #(at your option) any later version.
10 #Asgard is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU General Public License
16 #along with Asgard; if not, write to the Free Software
17 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 ########################################################
19 import pygame
20 from pygame import Surface
22 class Drawable(Surface):
24 screen = None
25 """ Screen object that this Drawable is drawn on. """
27 id = ""
28 """ String representing name of drawable. """
30 dimensions = None
31 """ Integer tuple representing length and width of drawable. """
33 def __init__(self,id,dimensions):
34 """
35 Handles Surface inheritance.
36 @param id : identifying name of drawable
37 @param id : string
38 @param dimensions : height and width of drawable
39 @type dimensions : (int,int)
40 """
41 self.id = id
42 #self.__dimensions = dimensions
43 pass
45 def draw(self):
46 """ A virtual method that draws this drawable onto itself. (Surface) """
47 pass
49 class MenuDrawable(Drawable):
51 submenu = []
52 """
53 Sub MenuDrawable's that belong to this one. Mutually exclusive
54 with text. Drawn with reference to offset x,y. Think cellpadding.
55 """
57 text = ""
58 """
59 A string to be drawn inside this MenuDrawable. Mutually exclusive
60 with menu. Drawn at offset x,y. Think cellpadding.
61 """
63 offset = None
64 """ x,y-tuple of text or first sub-menu to be drawn to screen. """
66 border = True
67 """ Whether or not this MenuDrawable has a border or not."""
69 selindex = 0
70 """ Index of selected submenu. """
72 parent = None
73 """ MenuDrawable that this one belongs to. """
75 def __init__(self,id,screen,dimensions,offset,border,text,submenus,selindex):
76 """
77 Handles Drawable inheritance. Instantiates a MenuDrawable.
79 @param id : identifier of this drawable
80 @type id : string
81 @param dimensions : height and width of menu
82 @type dimensions : (int,int)
83 @param offset : location of menu with respect it's parent surface
84 @type offset : (int,int)
85 @param border : whether this menu has a border or not
86 @type border : bool
87 @param text : the text displayed in this menu. mutex w/ submenus
88 @type text : string
89 @param submenu : list of submenus to be display in this menu. mutex w/ text
90 @type submenu : MenuDrawable[]
91 @param selindex : index of selected submenu (if submenus)
92 @type selindex : int
93 """
94 self.id = id
95 self.dimensions = dimensions
96 self.offset = offset
98 if submenus != None:
99 self.submenu = submenus
100 else:
101 self.submenu = []
102 self.screen = screen
103 self.border = border
104 self.text = text
106 for md in self.submenu:
107 if md != None:
108 md.parent = self
110 self.selindex = selindex
112 def selIndexUp(self):
113 self.selindex += 1
115 if (self.selindex >= len(self.submenu)):
116 self.selindex = 0
118 def selIndexDown(self):
119 self.selindex -= 1
120 if (self.selindex < 0):
121 self.selindex = len(self.submenu) - 1
123 def draw(self):
124 """ Draws a menu onto this drawable's surface. """
125 width = self.dimensions[0]
126 height = self.dimensions[1]
127 font = pygame.font.SysFont("courier", 18)
129 x = 0
130 y = 0
132 p = self
133 while p != None:
134 x += p.offset[0]
135 y += p.offset[1]
136 p = p.parent
139 s = pygame.Surface((width,height))
140 s.fill((51,51,51))
142 if self.border:
143 s.fill((222,186,112))
145 # Not Perfect, but decent
146 # Replace with image borders
147 pygame.draw.lines(s,(255,255,255),True,[(2,2),(width-3,2),(width-3,height-3),(2,height-3)],1)
148 pygame.draw.lines(s,(170,170,170),True,[(1,1),(width-2,1),(width-2,height-2),(1,height-2)],1)
149 pygame.draw.lines(s,(100,100,100),True,[(0,0),(width-1,0),(width-1,height-1),(0,height-1)],1)
150 self.screen.cont.display.blit(s,(x,y))
152 useCursor = False
153 if self.parent != None:
154 if self.parent != None and self.screen.focus != None:
155 if self.parent.id != self.screen.focus.id:
156 useCursor = False
157 elif self.parent.submenu[self.parent.selindex].text == self.text:
158 useCursor = True
160 if self.text != "":
161 self.screen.cont.display.blit(font.render(self.text,True,(89,75,46)),(x+40,y+5))
163 cursor = pygame.image.load("./data/images/sword.png")
165 if useCursor:
166 self.screen.cont.display.blit(cursor,(x+5,y+9))
169 for md in self.submenu:
170 if md != None:
171 md.draw()