-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathdroneSplitStack.py
More file actions
134 lines (101 loc) · 4.76 KB
/
droneSplitStack.py
File metadata and controls
134 lines (101 loc) · 4.76 KB
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
import re
# noinspection PyPackageRequirements
import wx
import gui.fitCommands as cmd
import gui.mainFrame
from gui.contextMenu import ContextMenuSingle
from service.fit import Fit
class DroneSplitStack(ContextMenuSingle):
def __init__(self):
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
def display(self, callingWindow, srcContext, mainItem):
if srcContext != "droneItem":
return False
if mainItem is None:
return False
return mainItem.amount > 1
def getText(self, callingWindow, itmContext, mainItem):
return "Split {} Stack".format(itmContext)
def activate(self, callingWindow, fullContext, mainItem, i):
with DroneStackSplit(self.mainFrame, mainItem.amount) as dlg:
if dlg.ShowModal() == wx.ID_OK:
if dlg.input.GetLineText(0).strip() == '':
return
fitID = self.mainFrame.getActiveFit()
fit = Fit.getInstance().getFit(fitID)
cleanInput = re.sub(r'[^0-9.]', '', dlg.input.GetLineText(0).strip())
if mainItem in fit.drones:
position = fit.drones.index(mainItem)
self.mainFrame.command.Submit(cmd.GuiSplitLocalDroneStackCommand(
fitID=fitID, position=position, amount=int(cleanInput)))
class DroneSplitStackBandwidth(DroneSplitStack):
"""
Split drone stack to match ship's available bandwidth, ensuring that only
one of the stacks is active so as not to exceed the bandwidth limit.
"""
def getText(self, callingWindow, itmContext, mainItem):
return "Split {} Stack to Fit Max Bandwidth".format(itmContext)
def activate(self, callingWindow, fullContext, mainItem, i):
fitID = self.mainFrame.getActiveFit()
fit = Fit.getInstance().getFit(fitID)
if mainItem in fit.drones:
bandwidth_per_drone = mainItem.item.\
attributes['droneBandwidthUsed'].value
ship_bandwidth = fit.ship.item.attributes['droneBandwidth'].value
max_active_drones = int(ship_bandwidth/bandwidth_per_drone)
if max_active_drones == 0:
wx.MessageDialog(
None, "Cannot split drone stack to fit bandwidth. This "
"drone type uses {0} mbit/s and this ship only has {1} "
"mbit/s.".format(int(bandwidth_per_drone),
int(ship_bandwidth)),
"Ship drone bandwidth exceeded", wx.OK | wx.ICON_ERROR
).ShowModal()
else:
if max_active_drones > 5:
max_active_drones = 5
position = fit.drones.index(mainItem)
self.mainFrame.command.Submit(
cmd.GuiSplitLocalDroneStackCommand(fitID=fitID,
position=position,
amount=max_active_drones,
deactivate=True)
)
DroneSplitStack.register()
DroneSplitStackBandwidth.register()
class DroneStackSplit(wx.Dialog):
def __init__(self, parent, value):
super().__init__(parent, title="Split Drone Stack", style=wx.DEFAULT_DIALOG_STYLE)
self.SetMinSize((346, 156))
bSizer1 = wx.BoxSizer(wx.VERTICAL)
bSizer2 = wx.BoxSizer(wx.VERTICAL)
text = wx.StaticText(self, wx.ID_ANY, "New Amount:")
bSizer2.Add(text, 0)
bSizer1.Add(bSizer2, 0, wx.ALL, 10)
self.input = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_PROCESS_ENTER)
self.input.SetValue(str(value))
self.input.SelectAll()
bSizer1.Add(self.input, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, 15)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer3.Add(wx.StaticLine(self, wx.ID_ANY), 0, wx.BOTTOM | wx.EXPAND, 15)
bSizer3.Add(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL), 0, wx.EXPAND)
bSizer1.Add(bSizer3, 0, wx.ALL | wx.EXPAND, 10)
self.input.SetFocus()
self.input.Bind(wx.EVT_CHAR, self.onChar)
self.input.Bind(wx.EVT_TEXT_ENTER, self.processEnter)
self.SetSizer(bSizer1)
self.CenterOnParent()
self.Fit()
def processEnter(self, evt):
self.EndModal(wx.ID_OK)
# checks to make sure it's valid number
@staticmethod
def onChar(event):
key = event.GetKeyCode()
acceptable_characters = "1234567890"
acceptable_keycode = [3, 22, 13, 8, 127] # modifiers like delete, copy, paste
if key in acceptable_keycode or key >= 255 or (key < 255 and chr(key) in acceptable_characters):
event.Skip()
return
else:
return False