Working cursor. Quit button has been tested and works.
[asgard.git] / drawable.py
blobf88752f87873118e14decf639abc4d841956d889
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 md.parent = self
109 self.selindex = selindex
111 def selIndexUp(self):
112 print "was: " + str(self.selindex)
113 self.selindex += 1
114 print "is: " + str(self.selindex)
116 print "sb" + str(len(self.submenu))
117 if (self.selindex >= len(self.submenu)):
118 self.selindex = 0
120 def selIndexDown(self):
121 self.selindex -= 1
122 if (self.selindex < 0):
123 self.selindex = len(self.submenu) - 1
125 def draw(self):
126 """ Draws a menu onto this drawable's surface. """
127 width = self.dimensions[0]
128 height = self.dimensions[1]
129 font = pygame.font.SysFont("andalemono", 18)
131 x = 0
132 y = 0
134 p = self
135 while p != None:
136 x += p.offset[0]
137 y += p.offset[1]
138 p = p.parent
141 s = pygame.Surface((width,height))
142 s.fill((51,51,51))
144 if self.border:
145 s.fill((222,186,112))
146 pygame.draw.rect(s,(255,255,255),(0,0,width,height),2)
147 self.screen.cont.display.blit(s,(x,y))
149 useCursor = False
150 if self.parent != None:
151 if self.parent.submenu[self.parent.selindex].text == self.text:
152 useCursor = True
154 if self.text != "":
155 self.screen.cont.display.blit(font.render(self.text,True,(89,75,46)),(x+40,y+5))
157 cursor = pygame.image.load("./data/images/sword.png")
158 if useCursor:
159 self.screen.cont.display.blit(cursor,(x+5,y+9))
162 for md in self.submenu:
163 md.draw()