Conditions | 41 |
Total Lines | 100 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like pygameui.Application.Application.processEvent() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # |
||
78 | def processEvent(self, evt): |
||
79 | if evt.type == pygame.VIDEOEXPOSE: |
||
80 | self.performFullUpdate() |
||
81 | # |
||
82 | if not pygame.key.get_focused(): |
||
83 | return Const.NoEvent |
||
84 | elif evt.type == Const.TIMEREVENT: |
||
85 | # tooltips |
||
86 | self.mouseOverCount += 1 |
||
87 | if self.mouseOverCount == self.mouseOverThreshold: |
||
88 | # show tooltip |
||
89 | if self.mouseOverWidget: |
||
90 | self.tooltip.title = self.mouseOverWidget.tooltipTitle |
||
91 | self.tooltip.text = self.mouseOverWidget.tooltip |
||
92 | self.tooltip.rect = pygame.Rect(pygame.mouse.get_pos(), (100, 100)) |
||
93 | # cursor |
||
94 | self.cursorCount += 1 |
||
95 | if self.cursorCount == 5: |
||
96 | self.cursorOn = not self.cursorOn |
||
97 | self.cursorCount = 0 |
||
98 | if self.focusedWidget: |
||
99 | self.focusedWidget.onCursorChanged() |
||
100 | # keyboard repeat |
||
101 | if self.keyEvt: |
||
102 | self.keyCount += 1 |
||
103 | if self.keyCount == 6: |
||
104 | self.processEvent(self.keyEvt) |
||
105 | self.keyCount = 4 |
||
106 | return Const.NoEvent |
||
107 | elif evt.type == pygame.MOUSEBUTTONDOWN: |
||
108 | # mouse wheel |
||
109 | if evt.button == 4 or evt.button == 5: |
||
110 | # TODO find window to deliver mouse wheel events to |
||
111 | if self.focusedWindow: |
||
112 | if self.focusedWindow.rect.collidepoint(evt.pos): |
||
113 | if evt.button == 4: |
||
114 | return self.focusedWindow.processMWUp(evt) |
||
115 | elif evt.button == 5: |
||
116 | return self.focusedWindow.processMWDown(evt) |
||
117 | else: |
||
118 | return evt |
||
119 | return Const.NoEvent |
||
120 | # TODO double click |
||
121 | # check if focused window is top level one |
||
122 | if self.focusedWindow != self.windows[-1]: |
||
123 | window = self.focusedWindow |
||
124 | self.focusWindowAt(evt) |
||
125 | # consume event, when focus has been changed |
||
126 | if self.focusedWindow != window: |
||
127 | return Const.NoEvent |
||
128 | # left and right mouse button |
||
129 | if self.focusedWindow: |
||
130 | if self.focusedWindow.rect.collidepoint(evt.pos): |
||
131 | if evt.button == 1: |
||
132 | return self.focusedWindow.processMB1Down(evt) |
||
133 | elif evt.button == 3: |
||
134 | return self.focusedWindow.processMB3Down(evt) |
||
135 | elif self.focusedWindow.modal: |
||
136 | return Const.NoEvent |
||
137 | elif self.focusedWindow.looseFocusClose: |
||
138 | self.focusedWindow.hide() |
||
139 | return self.focusWindowAt(evt) |
||
140 | else: |
||
141 | return self.focusWindowAt(evt) |
||
142 | else: |
||
143 | return self.focusWindowAt(evt) |
||
144 | return evt |
||
145 | elif evt.type == pygame.MOUSEBUTTONUP: |
||
146 | # left and right mouse button |
||
147 | if self.focusedWindow and self.focusedWindow.rect.collidepoint(evt.pos): |
||
148 | if evt.button == 1: |
||
149 | return self.focusedWindow.processMB1Up(evt) |
||
150 | elif evt.button == 3: |
||
151 | return self.focusedWindow.processMB3Up(evt) |
||
152 | return evt |
||
153 | elif evt.type == pygame.MOUSEMOTION: |
||
154 | if self.mouseOverCount < self.mouseOverThreshold: |
||
155 | # just moving across widget does not trigger tooltip |
||
156 | self.mouseOverCount = 0 |
||
157 | self.cursorPos = evt.pos |
||
158 | if self.focusedWindow: |
||
159 | return self.focusedWindow.processMMotion(evt) |
||
160 | return evt |
||
161 | elif evt.type == pygame.KEYDOWN: |
||
162 | self.keyEvt = evt |
||
163 | self.keyCount = 0 |
||
164 | if self.focusedWidget: |
||
165 | evt = self.focusedWidget.processKeyDown(evt) |
||
166 | if evt != Const.NoEvent and self.focusedWindow: |
||
167 | evt = self.focusedWindow.processKeyDown(evt) |
||
168 | return evt |
||
169 | elif evt.type == pygame.KEYUP: |
||
170 | self.keyEvt = None |
||
171 | if self.focusedWidget: |
||
172 | evt = self.focusedWidget.processKeyUp(evt) |
||
173 | if evt != Const.NoEvent and self.focusedWindow: |
||
174 | evt = self.focusedWindow.processKeyUp(evt) |
||
175 | return evt |
||
176 | else: |
||
177 | return evt |
||
178 | |||
347 |