Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Help.py
diff options
context:
space:
mode:
Diffstat (limited to 'Help.py')
-rw-r--r--Help.py220
1 files changed, 220 insertions, 0 deletions
diff --git a/Help.py b/Help.py
new file mode 100644
index 0000000..b73f567
--- /dev/null
+++ b/Help.py
@@ -0,0 +1,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)
+