Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/SortHelp.py
blob: e7ea0f4a39a6f95fc08ecbbbf2fd8272fe85181e (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
import Sort
from eduGames import *
import os

class MouseAction:
    def __init__(self, command):
        self.type = command[0].upper()
        if self.type == "MOVE":
            x = int(command[1])
            y = int(command[2])
            self.params = (x,y)       
        elif self.type == "WAIT":
            self.params = int(command[1])
        
class MousePointer(ImageControl):
    
    (WAITING, MOVING, MOUSE_BUTTON_DOWN, WAITINGMOVES) = (0,1,2,3)
    
       
    def __init__(self, game, path):
        mousePointerImagePath = os.path.split(path)[0]        
        mousePointerImagePath = os.path.join(mousePointerImagePath, "mousePointer.png")
        x = game.getScreen().get_width()//2
        y = game.getScreen().get_height()//2         
        ImageControl.__init__(self, game, x, y, mousePointerImagePath, "", 1)    
                                                            
    def isClickable(self):
        return False
        
class DemoEvent:
    def __init__(self, type, params):
        self.type = type
        self.params = params
        

class DemoPlayer(Sort.Sort):    
   
    def __init__(self, screen, uiMgr, sndMgr, screenMgr, path):        
        Sort.Sort.__init__(self, screen, uiMgr, sndMgr, screenMgr, path)        
        self.mousePointer = MousePointer(self, path)
        self.eventQueue = []        
        filepath = os.path.join(path, "demoscript.txt")        
        self.demoIsProcessing = False
        self.actionsQueue = []       
        self.loadQueue(filepath)
        self.currentAction = None
        self.timer = 0
        
    def loadQueue(self, filepath):
        self.actionsQueue = []
        file = open(filepath, "r")
        text = file.readlines()
        for line in text:
            if line.strip() <> "":
                command = line.split()
                self.actionsQueue.append(MouseAction(command))
            
    def showCurrentScreen(self):
        Sort.Sort.showCurrentScreen(self)
        self.getUiMgr().addControl(self.mousePointer)        
        
    def advanceQueue(self):        
        if not len(self.actionsQueue) == 0:
            action = self.actionsQueue.pop(0)                           
            if action.type == "MOVE":
                target = action.params                               
                self.mousePointer.startMovement(target, 7, LINEAR)
                self.currentAction = action
                self.eventQueue.append(DemoEvent("MOUSEMOTION", (self.mousePointer.getX(), self.mousePointer.getY())))
            if action.type == "MOUSE_BUTTON_DOWN":                
                self.eventQueue.append(DemoEvent("MOUSE_BUTTON_DOWN", (self.mousePointer.getX(), self.mousePointer.getY())))
            if action.type == "WAITMOVS":
                self.currentAction = action
            if action.type == "WAIT":
                self.currentAction = action
                self.timer = action.params
        else:
            self.finished = True   
                         
    def updateState(self):
        Sort.Sort.updateState(self)        
        if self.currentAction == None:
            self.advanceQueue()
        else:           
            mousePointerMoving = self.mousePointer.isMoving()
            if mousePointerMoving:
                self.eventQueue.append(DemoEvent("MOUSEMOTION", (self.mousePointer.getX(), self.mousePointer.getY())))
            if self.currentAction.type == "WAIT":
                self.timer = self.timer - 1
                if self.timer == 0:
                    self.currentAction = None
            elif self.currentAction.type == "MOVE":
                if not mousePointerMoving:
                    self.currentAction = None
            elif self.currentAction.type == "WAITMOVS":
                if not self.somethingMoves():
                    self.currentAction = None            
        self.processEvents()
                
                
    def somethingMoves(self):
        for control in self.getUiMgr().controls:
            if control.isMoving() and not control is self.mousePointer:
                return True
        return False
            
    def on_mouse_button_up(self, clickedControl):
        if self.demoIsProcessing:
            Sort.Sort.on_mouse_button_up(self, clickedControl)
    
    def on_mouse_button_down(self, clickedControl):
        if self.demoIsProcessing:
            Sort.Sort.on_mouse_button_down(self, clickedControl)
        
    def on_mouse_hover(self, clickedControl):
        #print clickedControl
        if self.demoIsProcessing:
            Sort.Sort.on_mouse_hover(self, clickedControl)
            
                  
    def processEvents(self):    
            self.demoIsProcessing = True
            if self.getSoundMgr().thereAreSoundsPlayingSynchronously():
                pygame.mouse.set_cursor(*pygame.cursors.diamond)                
            else:                
                if len(self.eventQueue) > 0:   
                    event = self.eventQueue.pop(0)        
                    control = self.getAffectedControl()
                    if event.type == "MOUSE_BUTTON_UP":
                        if not control is None:
                            self.on_mouse_button_up(control)
                    if event.type == "MOUSE_BUTTON_DOWN":                                    
                        if not control is None:                        
                            self.on_mouse_button_down(control)
                    if event.type == "MOUSEMOTION":
                        if not control is None:
                            self.on_mouse_hover(control)
            self.demoIsProcessing = False
                    
    def getAffectedControl(self):
        xMouse = self.mousePointer.getX()
        yMouse = self.mousePointer.getY()        
        for control in self.getUiMgr().controls:
            if control.isClickable():           
                if control.collidesWithMousePosition(xMouse, yMouse):
                    return control