SystemEvents.process()   F
last analyzed

Complexity

Conditions 15

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 15
dl 0
loc 61
rs 2.9944

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like 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
2
3
import sdl2 as sdl
4
5
from ed2d.platforms import sdl2keymap as keymap
6
from ed2d.events import Events
7
from ed2d import cursor
8
9
MOUSE_LEFT = 1
10
MOUSE_MIDDLE = 2
11
MOUSE_RIGHT = 3
12
MOUSE_EX1 = 4
13
MOUSE_EX2 = 5
14
15
class SystemEvents(object):
16
    ''' Handles Event stuff... '''
17
    def __init__(self):
18
        sdl.SDL_InitSubSystem(sdl.SDL_INIT_EVENTS)
19
20
    def process(self):
21
        '''
22
        Processes the events polled from sdl.
23
        Custom events might be a possiblility if we need them.
24
        '''
25
26
        event = sdl.SDL_Event()
27
28
        while sdl.SDL_PollEvent(ct.byref(event)):
29
30
            eventName = None
31
            data = None
32
33
            if event.type == sdl.SDL_QUIT:
34
                eventName = 'quit'
35
                data = ()
36
            elif event.type == sdl.SDL_MOUSEMOTION:
37
                eventName = 'mouse_move'
38
                if cursor.is_relative():
39
                    data = (event.motion.xrel, event.motion.yrel)
40
                else:
41
                    data = (event.motion.x, event.motion.y)
42
            elif event.type == sdl.SDL_WINDOWEVENT:
43
                winEvent = event.window
44
                wEventName = event.window.event
45
                # For now this will only support one window
46
                # If we want two later on then we can do it then.
47
                if wEventName == sdl.SDL_WINDOWEVENT_CLOSE:
48
                    eventName = 'window_close'
49
                    data = (winEvent.windowID)
50
                elif wEventName == sdl.SDL_WINDOWEVENT_RESIZED:
51
                    eventName = 'window_resized'
52
                    data = (winEvent.windowID, winEvent.data1, winEvent.data2)
53
54
            elif event.type == sdl.SDL_KEYUP:
55
                if not event.key.repeat:
56
                    eventName = 'key_up'
57
                    keyID = keymap.keymap[event.key.keysym.scancode]
58
                    keyName = keymap.process_key_char(event.key.keysym.sym)
59
                    modKeys = keymap.process_modkeys(event.key.keysym.mod)
60
                    data = (keyName, keyID, modKeys)
61
62
            elif event.type == sdl.SDL_KEYDOWN:
63
                if not event.key.repeat:
64
                    eventName = 'key_down'
65
                    keyID = keymap.keymap[event.key.keysym.scancode]
66
                    keyName = keymap.process_key_char(event.key.keysym.sym)
67
                    modKeys = keymap.process_modkeys(event.key.keysym.mod)
68
                    data = (keyName, keyID, modKeys)
69
            elif event.type == sdl.SDL_MOUSEBUTTONUP:
70
                eventName = 'mouse_button_up'
71
                data = (event.button.button, event.button.clicks, event.button.x, event.button.y)
72
            elif event.type == sdl.SDL_MOUSEBUTTONDOWN:
73
                eventName = 'mouse_button_down'
74
                data = (event.button.button, event.button.clicks, event.button.x, event.button.y)
75
            else:
76
                # Will add more event types later
77
                pass
78
79
            if eventName is not None:
80
                Events.broadcast_event(eventName, data)
81
82
__all__ = ['Events']
83