Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Help.py
blob: b73f567119a3173a43d780a18c982adf4f1afb2f (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from eduGames import *
from GamesHelpers 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, "", 2)    
        self.persistent = True
                                                            
    def isClickable(self):
        return False
        
class DemoEvent:
    def __init__(self, type, params):
        self.type = type
        self.params = params       
        
class DemoPlayer:    
   
    def __init__(self, path, gameEngine):
        self.gameEngine = gameEngine      
        self.mousePointer = MousePointer(self.gameEngine, path)
        self.mousePointer.setX(self.mousePointer.getX() + self.gameEngine.xMargin)
        self.mousePointer.setY(self.mousePointer.getY() + self.gameEngine.yMargin)
        self.eventQueue = []
        self.path = path      
        filepath = os.path.join(path, "demoscript.txt")        
        self.demoIsProcessing = False
        self.actionsQueue = []       
        self.loadQueue(filepath)
        self.currentAction = None
        self.timer = 0        
        self.finished = False
        self.gameEngine.getUiMgr().addControl(self.mousePointer)      
        self.mousePointer.type = "fakeMouse"        
        
    def constructPath(self, slashedPath):
        dirs = slashedPath.split("/")
        index = 0
        for d in dirs:
            if index == 0:
                curDir = os.path.abspath(os.path.curdir)
            else:
                curDir = dirs[index - 1]
            dirs[index] = os.path.join(curDir, dirs[index])            
            index += 1        
        return dirs[index - 1]

    def initializeGameData(self):
        if self.gameEngine.settings.has_key("resourcesDir"):
            self.gameEngine.resourcesDir = self.constructPath(self.gameEngine.settings["resourcesDir"])
        if self.gameEngine.settings.has_key("soundsDir"):
            self.gameEngine.soundsDir = self.constructPath(self.gameEngine.settings["soundsDir"])
        self.gameEngine.initializeGameData()                
            
    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 advanceQueue(self):        
        if not len(self.actionsQueue) == 0:
            action = self.actionsQueue.pop(0)                           
            if action.type == "MOVE":
                target = (action.params[0] + self.gameEngine.xMargin, action.params[1] + self.gameEngine.yMargin)                                               
                self.mousePointer.startMovement(target, 7, LINEAR)
                self.currentAction = action                
            if action.type == "MOUSE_BUTTON_DOWN":
                resourcesPath = os.path.split(self.path)[0]      
                clickPath = os.path.join(resourcesPath, "click.wav")
                self.gameEngine.getSoundMgr().playSoundImmediately(clickPath)
                self.eventQueue.append(DemoEvent("MOUSE_BUTTON_DOWN", (self.mousePointer.getX(), self.mousePointer.getY())))
            if action.type == "MOUSE_BUTTON_UP":                    
                self.eventQueue.append(DemoEvent("MOUSE_BUTTON_UP", (self.mousePointer.getX(), self.mousePointer.getY())))
            if action.type == "WAITMOVS":
                self.currentAction = action
            if action.type == "WAIT":
                self.currentAction = action
                self.timer = action.params
            if self.mousePointer.isMoving():
                self.eventQueue.append(DemoEvent("MOUSEMOTION", (self.mousePointer.getX(), self.mousePointer.getY())))
                                               
    def updateState(self):        
        self.gameEngine.updateState()      
        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.gameEngine.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:
            self.gameEngine.on_mouse_button_up(clickedControl)
    
    def on_mouse_button_down(self, clickedControl):
        if self.demoIsProcessing:
            self.gameEngine.on_mouse_button_down(clickedControl)
        
    def on_mouse_hover(self, clickedControl):
        #print clickedControl
        if self.demoIsProcessing:
            self.gameEngine.on_mouse_hover(clickedControl)
            
                  
    def processEvents(self):    
            self.demoIsProcessing = True
            if self.gameEngine.getSoundMgr().thereAreSoundsPlayingSynchronously():
                #pygame.mouse.set_cursor(*pygame.cursors.diamond)
                pass                         
            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.gameEngine.getUiMgr().controls:
            if control.isClickable():           
                if control.collidesWithMousePosition(xMouse, yMouse):
                    return control            

class MainHelp(Game):
    def __init__(self, screenSurface, uiMgr, soundMgr, screenMgr, path):
        Game.__init__(self, screenSurface, uiMgr, soundMgr, screenMgr, path)
        self.xMargin = 0
        self.yMargin = 0
        self.realGame = None
        self.level2MenuItems = 0
        self.level3MenuItems = 0
        self.level4MenuItems = 0    
        
    def initializeGameData(self):        
        self.filebox = Archiver(self, self.settings["rootIconX"], self.settings["rootIconY"], self.masterResourcesDir)
        self.filebox.setLayer(1)
        #self.menu = Control(self, self.filebox.getX(), self.filebox.getY(), self.filebox.getWidth(), self.filebox.getHeight(),"", 0)
        diffX = self.filebox.getX() - self.realGame.filebox.getX()
        diffY = self.filebox.getY() - self.realGame.filebox.getY()
        self.menu = self.realGame.buildMenu(self.realGame.root, diffX, diffY, self)
        self.menu.mainControl = self.filebox
        self.menu.setX(self.filebox.getX())
        self.menu.setY(self.filebox.getY())
        self.menu.setLayer(0)
        self.getUiMgr().addControl(self.filebox) 
        self.hoveredOnControl = None
        #self.getUiMgr().addControl(self.menu)
        
    def on_mouse_hover(self, clickedControl):        
        if clickedControl is self.menu:
            if not self.filebox.isOpen():
                self.filebox.hover()
        elif clickedControl.type == "menu":
            if not self.hoveredOnControl is None:
                self.hoveredOnControl.turnHoverOff()
                if not clickedControl.isOpen():
                    self.hoveredOnControl = clickedControl
                    self.hoveredOnControl.turnHoverOn()
            else:            
                if not self.filebox.isOpen():
                    self.filebox.closeCompletely()           
                    
    def on_mouse_button_down(self, clickedControl):    
        if clickedControl.type == "menu":                
            if len(clickedControl.submenus) > 0:
                clickedControl.click()
                
    def makePath(self, fileName):
        return self.realGame.makePath(fileName)