File: menu.py

package info (click to toggle)
lightyears 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,324 kB
  • ctags: 454
  • sloc: python: 3,499; makefile: 2
file content (187 lines) | stat: -rw-r--r-- 5,515 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 
# 20,000 Light Years Into Space
# This game is licensed under GPL v2, and copyright (C) Jack Whitham 2006-07.
# 

# A very lightweight menu system.

import pygame
from pygame.locals import *

import stats , extra , resource , render , sound


class Menu:
    def __init__(self, menu_options, force_width=0):
        self.options = menu_options

        self.control_rects = []
        self.hover = None
        self.bbox = None

        self.selection = None
        self.update_required = True

        width_hint = height_hint = 10

        if ( force_width > 0 ):
            width_hint = force_width

        # Two attempts at drawing required.
        (discard1, discard2,
            (width_hint, height_hint)) = self.__Draw((width_hint, height_hint))

        if ( width_hint < 150 ):
            width_hint = 150
        if ( force_width > 0 ):
            width_hint = force_width

        (self.surf_store, self.control_rects,
            (discard1, discard2)) = self.__Draw((width_hint, height_hint))

        self.bbox = Rect(0, 0, width_hint, height_hint)

    def Get_Command(self):
        return self.selection

    def Select(self, snum):
        self.update_required = True
        self.selection = snum

    def Mouse_Move(self, spos):
        if (( spos == None )
        or ( not self.bbox.collidepoint(spos) )):
            self.hover = None
            return

        self.update_required = True
        (x,y) = spos

        old_sel = self.hover
        self.hover = None
        x -= self.bbox.left
        y -= self.bbox.top
        for (num, r) in self.control_rects:
            if ( r.collidepoint(x,y) ):
                self.hover = num
                if ( old_sel != self.hover ):
                    sound.FX("click_s")
                return

    def Mouse_Down(self, spos):

        self.Mouse_Move(spos)
        if ( self.hover != None ):
            self.selection = self.hover
            sound.FX("click")

    def Key_Press(self, k):
        for (num, name, hotkeys) in self.options:
            if (( hotkeys != None ) and ( k in hotkeys )):
                self.selection = num
                self.update_required = True
                sound.FX("click")
                return

    def Draw(self, output, centre=None):
        if ( self.update_required ):
            self.update_required = False

            if ( centre == None ):
                self.bbox.center = output.get_rect().center
            else:
                self.bbox.center = centre

            self.bbox.clamp_ip(output.get_rect())

            output.blit(self.surf_store, self.bbox.topleft)

            for (num, r) in self.control_rects:
                r = Rect(r)
                r.top += self.bbox.top
                r.left += self.bbox.left
                if ( num == self.selection ):
                    pygame.draw.rect(output, (255, 255, 255), r, 1)
                elif ( num == self.hover ):
                    pygame.draw.rect(output, (0, 180, 0), r, 1)


    def __Draw(self, (width_hint, height_hint)):
        surf = pygame.Surface((width_hint, height_hint))
        bbox = Rect(0, 0, width_hint, height_hint)

        extra.Tile_Texture(surf, "006metal.jpg", surf.get_rect())

        margin = 8
        w = bbox.width - ( margin * 2 )
        th = None
        y = margin + bbox.top
        control_rects = []
        max_width = 0
        first_item = True

        for (num, name, hotkeys) in self.options:
            if ( name == None ): # a gap
                if ( first_item ):
                    img = resource.Load_Image("header.jpg")
                    img_r = img.get_rect()
                    img_r.center = bbox.center
                    img_r.top = y
                    surf.blit(img, img_r.topleft)
                    extra.Edge_Effect(surf, img_r)
                    max_width = img_r.width + ( margin * 2 )
                    y += img_r.height

                y += margin * 2
                continue

            txt = render.Render(name, 18, (50,200,20), (200,200,0))
            if ( th == None ):
                th = txt.get_rect().height + ( margin * 2 )
            tw = txt.get_rect().width + ( margin * 2 )
            if ( tw > max_width ):
                max_width = tw

            x = bbox.left + margin 
            r = Rect(x,y,w,th)
            x += self.Justify(w,txt.get_rect().width)
        
            extra.Tile_Texture(surf, "greenrust.jpg", r)
            extra.Edge_Effect(surf, r)
            self.Enhancement_Interface(surf, num, r, margin)

            surf.blit(txt, (x,y + margin - 1))
            y += th + margin
            control_rects.append((num, r))

            first_item = False


        # Finalise drawing
        extra.Line_Edging(surf, bbox, True)

        return (surf, control_rects, (max_width, y))


    def Justify(self, width, text_width):
        return ( width - text_width ) / 2

    def Enhancement_Interface(self, surf, num, rect, margin):
        pass

# Menu plus pictures!
class Enhanced_Menu(Menu):
    def __init__(self, menu_options, pictures, force_width=0):
        self.pictures = pictures
        Menu.__init__(self, menu_options, force_width)

    def Enhancement_Interface(self, surf, num, rect, margin):
        if ( self.pictures.has_key( num ) ):
            img = resource.Load_Image( self.pictures[ num ] )
            img_r = img.get_rect()
            img_r.center = rect.center
            img_r.left = rect.left + margin
            surf.blit(img, img_r.topleft)