Completed
Pull Request — master (#192)
by Marek
01:39
created

ConstrSelTechDlg.onSelect()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import pygameui as ui
22
from osci.StarMapWidget import StarMapWidget
23
from osci import gdata, res, client, sequip
24
import ige.ospace.Const as Const
25
26
ENGINE = 1
27
SMALL = 2
28
MEDIUM = 4
29
LARGE = 8
30
PLANET = 16
31
OTHER = 32
32
ARMOR = 64
33
SHIELD = 128
34
POD = 256
35
SCANNER = 512
36
37
typeText = {
38
    ENGINE: _('Engine'),
39
    SMALL:  _('Anti-small'),
40
    MEDIUM: _('Anti-medium'),
41
    LARGE:  _('Anti-large'),
42
    PLANET: _('Anti-planet'),
43
    OTHER:  _('Other'),
44
    ARMOR:  _('Armor'),
45
    SHIELD: _('Shield'),
46
    POD:    _('Deployable'),
47
    SCANNER:_('Scanner'),
48
}
49
50
class ConstrSelTechDlg:
51
52
    def __init__(self, app):
53
        self.app = app
54
        self.createUI()
55
56
    def display(self, techType, techSubtype, selectCallback, selected = 0, hullID = 0):
57
        self.techID = Const.OID_NONE
58
        self.selectCallback = selectCallback
59
        self.techType = techType
60
        self.techSubtype = techSubtype
61
        self.selected = selected
62
        if hullID:
63
            self.hullType = client.getFullTechInfo(hullID).combatClass
64
        else:
65
            self.hullType = 0
66
        self.typeFilter = ENGINE | \
67
                          SMALL | MEDIUM | LARGE | PLANET | \
68
                          OTHER | ARMOR | SHIELD | POD | SCANNER
69
        self.show()
70
        self.win.show()
71
        # register for updates
72
        if self not in gdata.updateDlgs:
73
            gdata.updateDlgs.append(self)
74
75
    def hide(self):
76
        self.win.setStatus(_("Ready."))
77
        self.win.hide()
78
        # unregister updates
79
        if self in gdata.updateDlgs:
80
            gdata.updateDlgs.remove(self)
81
82
    def update(self):
83
        self.show()
84
85
    def getTechType(self, tech):
86
        if getattr(tech, "engPwr") > 0:
87
            return ENGINE
88
        if getattr(tech, "weaponClass") == 0 and getattr(tech, "weaponDmgMin") > 0:
89
            return SMALL
90
        if getattr(tech, "weaponClass") == 1:
91
            return MEDIUM
92
        if getattr(tech, "weaponClass") == 2:
93
            return LARGE
94
        if getattr(tech, "weaponClass") == 3:
95
            return PLANET
96
        if getattr(tech, "subtype") == "seq_mod" \
97
            and (
98
                getattr(tech, "maxHP") > 0
99
                or getattr(tech, "autoRepairFix") > 0
100
                or getattr(tech, "autoRepairPerc") > 0
101
                ):
102
            # armor modules
103
            return ARMOR
104
        if getattr(tech, "shieldPerc") > 0 or getattr(tech, "hardShield") > 0:
105
            return SHIELD
106
        if getattr(tech, "subtype") == "seq_struct":
107
            return POD
108
        if getattr(tech, "subtype") == "seq_mod" and getattr(tech, 'scannerPwr') > 0:
109
            return SCANNER
110
        return OTHER
111
112
    def show(self):
113
        self.showItems()
114
115
    def showItems(self):
116
        items = []
117
        selected = None
118
        player = client.getPlayer()
119
        for techID in player.techs:
120
            tech = client.getTechInfo(techID)
121
            if getattr(tech, self.techType) == 0:
122
                continue
123
            if self.techSubtype and tech.subtype not in self.techSubtype:
124
                continue
125
            # skip equipment not suitable for this hull
126
            if tech.minHull > self.hullType:
127
                continue
128
            if tech.maxHull < self.hullType:
129
                continue
130
131
            techType = self.getTechType(tech)
132
            if self.typeFilter & techType == 0:
133
                continue
134
135
            item = ui.Item(tech.name, tData = sequip.getLongDescr(techID), techID = techID,
136
                            tType = typeText[techType])
137
138
            if techID == self.selected:
139
                selected = item
140
141
            items.append(item)
142
        self.win.vList.items = items
143
        self.win.vList.itemsChanged()
144
        self.win.vList.selectItem(selected)
145
146
    def onCancel(self, widget, action, data):
147
        self.hide()
148
149
    def onFilter(self, widget, action, data):
150
        if widget.checked:
151
            self.typeFilter |= widget.data
152
        else:
153
            self.typeFilter &= ~widget.data
154
        self.showItems()
155
156
    def onSelect(self, widget, action, data):
157
        self.hide()
158
        if self.win.vList.selection:
159
            self.selectCallback(self.win.vList.selection[0].techID)
160
161
    def createUI(self):
162
        w, h = gdata.scrnSize
163
        self.win = ui.Window(self.app,
164
            modal = 1,
165
            escKeyClose = 1,
166
            movable = 0,
167
            title = _('Select component'),
168
            rect = ui.Rect((w - 764) / 2, (h - 304) / 2, 764, 304),
169
            layoutManager = ui.SimpleGridLM(),
170
        )
171
        self.win.subscribeAction('*', self)
172
        # component list
173
        ui.Listbox(self.win, layout = (0, 0, 38, 13), id = "vList",
174
            columns = (
175
                (_('Name'), 'text', 9, ui.ALIGN_W),
176
                (_('Type'), 'tType', 4, ui.ALIGN_W),
177
                (_('Data'), 'tData', 0, ui.ALIGN_W),
178
            ),
179
            sortedBy = ('tType', 1),
180
            action = "onSelect",
181
            rmbAction = "onCancel"
182
        )
183
184
        # status bar + submit/cancel
185
        ui.TitleButton(self.win, layout = (33, 13, 5, 1), text = _('Cancel'), action = 'onCancel')
186
        ui.Title(self.win, id = 'vStatusBar', layout = (0, 13, 33, 1), align = ui.ALIGN_W)
187
        #self.win.statusBar = self.win.vStatusBar
188