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 import gdata, client |
||
23 | import ige.ospace.Const as Const |
||
24 | |||
25 | class PlanetsAnalysisDlg: |
||
26 | """Displays analysis on planets structures. |
||
27 | |||
28 | Dialog displays list of current created structures |
||
29 | and list of planets where particular type of structure |
||
30 | is created. |
||
31 | """ |
||
32 | def __init__(self, app): |
||
33 | self._textRows = 0 |
||
34 | self.app = app |
||
35 | self.createUI() |
||
36 | |||
37 | def display(self): |
||
38 | self.show() |
||
39 | # show window |
||
40 | if not self.win.visible: |
||
41 | self.win.show() |
||
42 | |||
43 | def hide(self): |
||
44 | self.win.setStatus(_("Ready.")) |
||
45 | self.win.hide() |
||
46 | |||
47 | def update(self): |
||
48 | if self.win.visible: |
||
49 | self.show() |
||
50 | |||
51 | def show(self): |
||
52 | player = client.getPlayer() |
||
53 | |||
54 | self.buildingsCount = {} |
||
55 | self.buildingsPos = {} |
||
56 | for planetID in player.planets: |
||
57 | planet = client.get(planetID) |
||
58 | if hasattr(planet, "slots"): |
||
59 | for struct in planet.slots: |
||
60 | techID = struct[Const.STRUCT_IDX_TECHID] |
||
61 | if not techID in self.buildingsCount: |
||
62 | self.buildingsCount[techID] = 1 |
||
63 | else: |
||
64 | self.buildingsCount[techID] = self.buildingsCount[techID] + 1 |
||
65 | if not techID in self.buildingsPos: |
||
66 | self.buildingsPos[techID] = {planetID: 1} |
||
67 | else: |
||
68 | planetList = self.buildingsPos[techID] |
||
69 | if not planetID in planetList: |
||
70 | planetList[planetID] = 1 |
||
71 | else: |
||
72 | planetList[planetID] = planetList[planetID] + 1 |
||
73 | |||
74 | items = [] |
||
75 | for techID in self.buildingsCount: |
||
76 | tech = client.getTechInfo(techID) |
||
77 | items.append(ui.Item(tech.name, tStructCount = self.buildingsCount[techID], techID = techID)) |
||
78 | |||
79 | self.win.vStructures.items = items |
||
80 | self.win.vStructures.itemsChanged() |
||
81 | |||
82 | def onClose(self, widget, action, data): |
||
83 | self.hide() |
||
84 | |||
85 | def onSelectStruct(self, widget, action, data): |
||
86 | planetList = self.buildingsPos[data.techID] |
||
87 | tech = client.getTechInfo(data.techID) |
||
88 | self.win.vPlanetsTitle.text = _("Planets with structure %s") % tech.name |
||
89 | if planetList: |
||
90 | items = [] |
||
91 | for planetID in planetList: |
||
92 | planet = client.get(planetID) |
||
93 | items.append(ui.Item(planet.name, tStructCount = planetList[planetID], planetID = planetID)) |
||
94 | self.win.vPlanets.items = items |
||
95 | self.win.vPlanets.itemsChanged() |
||
96 | |||
97 | def onSelectPlanet(self, widget, action, data): |
||
98 | gdata.mainGameDlg.onSelectMapObj(None, None, data.planetID) |
||
99 | pass |
||
100 | |||
101 | def onShowLocation(self, widget, action, data): |
||
102 | planet = client.get(data.planetID, noUpdate = 1) |
||
103 | # center on map |
||
104 | if hasattr(planet, "x"): |
||
105 | gdata.mainGameDlg.win.vStarMap.highlightPos = (planet.x, planet.y) |
||
106 | gdata.mainGameDlg.win.vStarMap.setPos(planet.x, planet.y) |
||
107 | return |
||
108 | self.win.setStatus(_("Cannot show location")) |
||
109 | |||
110 | View Code Duplication | def createUI(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
111 | screenWidth, screenHeight = gdata.scrnSize |
||
112 | # size of dialog in layout metrics (for SimpleGridLM) |
||
113 | cols = 36 |
||
114 | rows = 27 |
||
115 | # dialog width and height in pixels |
||
116 | width = cols * 20 + 5 |
||
117 | height = rows * 20 + 4 |
||
118 | #creating dialog window |
||
119 | self.win = ui.Window(self.app, |
||
120 | modal = 1, |
||
121 | escKeyClose = 1, |
||
122 | movable = 0, |
||
123 | title = _("Planets analysis"), |
||
124 | rect = ui.Rect((screenWidth - width) / 2, (screenHeight - height) / 2, width, height), |
||
125 | layoutManager = ui.SimpleGridLM(), |
||
126 | ) |
||
127 | self.win.subscribeAction('*', self) |
||
128 | # first row is window title |
||
129 | rows -= 1 |
||
130 | |||
131 | halfCols = cols / 2 |
||
132 | ui.Title(self.win, layout = (0, 0, halfCols, 1), text = _("Structures"), |
||
133 | align = ui.ALIGN_W, id = "vStructuresTitle", font = "normal-bold") |
||
134 | ui.Listbox(self.win, layout = (0, 1, halfCols, rows - 2), id = "vStructures", |
||
135 | columns = ( |
||
136 | (_("Structure name"), "text", halfCols - 5, ui.ALIGN_W), |
||
137 | (_("Total #"), "tStructCount", 4, ui.ALIGN_E) |
||
138 | ), |
||
139 | columnLabels = 1, action = "onSelectStruct", sortable = True) |
||
140 | |||
141 | ui.Title(self.win, layout = (halfCols, 0, halfCols, 1), text = "", |
||
142 | align = ui.ALIGN_W, id = "vPlanetsTitle", font = "normal-bold") |
||
143 | ui.Listbox(self.win, layout = (halfCols, 1, halfCols, rows - 2), id = "vPlanets", |
||
144 | columns = ( |
||
145 | (_("Planet name"), "text", halfCols - 5, ui.ALIGN_W), |
||
146 | (_("# of structs"), "tStructCount", 4, ui.ALIGN_E) |
||
147 | ), |
||
148 | columnLabels = 1, action = "onSelectPlanet", rmbAction = "onShowLocation", sortable = True) |
||
149 | |||
150 | # dialog bottom line |
||
151 | ui.Title(self.win, layout = (0, rows - 1, cols - 5, 1)) |
||
152 | ui.TitleButton(self.win, layout = (cols - 5, rows - 1, 5, 1), text = _("Close"), action = "onClose") |
||
153 |