| Conditions | 12 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 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 ed2d.platforms.SystemEvents.process() 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 | import ctypes as ct |
||
| 13 | def process(self): |
||
| 14 | ''' |
||
| 15 | Processes the events polled from sdl. |
||
| 16 | Custom events might be a possiblility if we need them. |
||
| 17 | ''' |
||
| 18 | |||
| 19 | event = sdl.SDL_Event() |
||
| 20 | |||
| 21 | while sdl.SDL_PollEvent(ct.byref(event)): |
||
| 22 | |||
| 23 | eventName = None |
||
| 24 | data = None |
||
| 25 | |||
| 26 | if event.type == sdl.SDL_QUIT: |
||
| 27 | eventName = 'quit' |
||
| 28 | data = () |
||
| 29 | elif event.type == sdl.SDL_MOUSEMOTION: |
||
| 30 | eventName = 'mouse_move' |
||
| 31 | data = (event.motion.x, event.motion.y) |
||
| 32 | elif event.type == sdl.SDL_WINDOWEVENT: |
||
| 33 | winEvent = event.window |
||
| 34 | wEventName = event.window.event |
||
| 35 | # For now this will only support one window |
||
| 36 | # If we want two later on then we can do it then. |
||
| 37 | if wEventName == sdl.SDL_WINDOWEVENT_CLOSE: |
||
| 38 | eventName = 'window_close' |
||
| 39 | data = (winEvent.windowID) |
||
| 40 | elif wEventName == sdl.SDL_WINDOWEVENT_RESIZED: |
||
| 41 | eventName = 'window_resized' |
||
| 42 | data = (winEvent.windowID, winEvent.data1, winEvent.data2) |
||
| 43 | |||
| 44 | elif event.type == sdl.SDL_KEYUP: |
||
| 45 | if not event.key.repeat: |
||
| 46 | eventName = 'key_up' |
||
| 47 | keyID = keymap.keymap[event.key.keysym.scancode] |
||
| 48 | keyName = keymap.process_key_char(event.key.keysym.sym) |
||
| 49 | modKeys = keymap.process_modkeys(event.key.keysym.mod) |
||
| 50 | data = (keyName, keyID, modKeys) |
||
| 51 | |||
| 52 | elif event.type == sdl.SDL_KEYDOWN: |
||
| 53 | if not event.key.repeat: |
||
| 54 | eventName = 'key_down' |
||
| 55 | keyID = keymap.keymap[event.key.keysym.scancode] |
||
| 56 | keyName = keymap.process_key_char(event.key.keysym.sym) |
||
| 57 | modKeys = keymap.process_modkeys(event.key.keysym.mod) |
||
| 58 | data = (keyName, keyID, modKeys) |
||
| 59 | else: |
||
| 60 | # Will add more event types later |
||
| 61 | pass |
||
| 62 | |||
| 63 | if eventName is not None: |
||
| 64 | Events.broadcast_event(eventName, data) |
||
| 65 | |||
| 67 |