From: <sv...@ww...> - 2008-01-03 20:13:34
|
Author: nsmoooose Date: 2008-01-03 12:13:25 -0800 (Thu, 03 Jan 2008) New Revision: 2204 Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.pyc branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/MoveCameraToHomeCommand.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/QuitCommand.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/__init__.py Modified: branches/layout_tool_improvements/csp/tools/layout2/readme.txt branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/SceneWindow.py Log: Added a command infrastructure to the layout2 test application. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=2204 Modified: branches/layout_tool_improvements/csp/tools/layout2/readme.txt =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/readme.txt 2008-01-01 19:11:40 UTC (rev 2203) +++ branches/layout_tool_improvements/csp/tools/layout2/readme.txt 2008-01-03 20:13:25 UTC (rev 2204) @@ -7,9 +7,12 @@ * Convert 3d file using osgconv.exe to the model.osg. * Object browser. Make it possible to display an object before we insert it. We could have a list of all the objects that are insertable. * All commands that can be issued should be its own object. ex: + MoveCameraToHomeCommand + + DeleteCommand UndoCommand ExitCommand FileLoadCommand (To generic name for this class) FileSaveCommand (To generic name for this class) - * Display the underlying terrain. \ No newline at end of file + * Display the underlying terrain. Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -0,0 +1,37 @@ +#!/usr/bin/env python +import wx + +class CommandControlFactory: + """A factory that creates ui controls that is bound to command objects.""" + + def __init__(self, controlIdGenerator): + """ Constructs the control factory with a control id generator. The + generator is needed in order to create a unique id for each command + in the scope of the parent control.""" + self.controlIdGenerator = controlIdGenerator + + def GenerateMenuItems(self, parent, commands): + menu = wx.Menu() + for command in commands: + instance = command() + + controlId = self.controlIdGenerator.Generate() + menu.Append(controlId, instance.GetCaption()) + + wx.EVT_MENU(parent, controlId, CommandExecutor(instance).Execute) + return menu + +class CommandExecutor: + """This class can be bound to a wx event (click on a menuitem, button, toolbar + button etc). When the event is fired the command sent to the constructor is + executed.""" + + def __init__(self, command): + """Constructs this instance with a command object. This command will be + executed when the bound event is fired.""" + self.command = command + + def Execute(self, event): + """Bind this method to a wx event. When the event is fired the command will + be executed.""" + self.command.Execute() \ No newline at end of file Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +class ControlIdGenerator: + """This class purpose is to generate identities for user interface + controls. The id generated is an integer. These id's are usefull + together with the wx framework.""" + + def __init__(self, identity_start = 20000): + """Constructs a new generator. If no identity_start is specified + it will default to 20000.""" + self.identity = identity_start + + def Generate(self): + """Generates a new id and returns it.""" + newid = self.identity + self.identity = self.identity + 1 + return newid Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.pyc =================================================================== (Binary files differ) Property changes on: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/ControlIdGenerator.pyc ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py 2008-01-01 19:11:40 UTC (rev 2203) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -1,8 +1,13 @@ #!/usr/bin/env python import wx +from csp.tools.layout2.scripts.ui.ControlIdGenerator import ControlIdGenerator +from csp.tools.layout2.scripts.ui.CommandControlFactory import CommandControlFactory from csp.tools.layout2.scripts.ui.controls.SceneWindow import SceneWindow +from csp.tools.layout2.scripts.ui.commands.MoveCameraToHomeCommand import MoveCameraToHomeCommand +from csp.tools.layout2.scripts.ui.commands.QuitCommand import QuitCommand + ID_FILE_OPEN = 2000 ID_FILE_SAVE = 2001 ID_FILE_QUIT = 2002 @@ -12,14 +17,27 @@ # First, call the base class' __init__ method to create the frame wx.Frame.__init__(self, parent, id, title) + # Create a control id generator. This is used by all helper classes + # to actualy define an unique id for each control (menu, toolbar button) + # etc. + self.controlIdGenerator = ControlIdGenerator() + + # Declare a class that is responsible for creating instances of + # menu items, toolbar buttons and keyboard shortcuts to command + # objects. + controlFactory = CommandControlFactory(self.controlIdGenerator) + + fileMenuCommands = [QuitCommand] + viewMenuCommands = [MoveCameraToHomeCommand] + # Menu items. menuBar = wx.MenuBar() - fileMenu = wx.Menu() - fileMenu.Append(ID_FILE_OPEN, "Load") - fileMenu.Append(ID_FILE_SAVE, "Save") - fileMenu.Append(ID_FILE_QUIT, "Quit") + + fileMenu = controlFactory.GenerateMenuItems(self, fileMenuCommands) + menuBar.Append(fileMenu, "File") - menuBar.Append(fileMenu, "File") + viewMenu = controlFactory.GenerateMenuItems(self, viewMenuCommands) + menuBar.Append(viewMenu, "View") self.SetMenuBar(menuBar) Property changes on: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands ___________________________________________________________________ Name: svn:ignore + *.pyc Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/MoveCameraToHomeCommand.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/MoveCameraToHomeCommand.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/MoveCameraToHomeCommand.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -0,0 +1,28 @@ +#!/usr/bin/env python +import wx + +from csp.tools.layout2.scripts.ui.controls.SceneWindow import SceneWindow + +class MoveCameraToHomeCommand: + """This command moves the camera to the home position for + the current scene control. If the current wx control isn't + a SceneWindow the command is ignored.""" + + def GetCaption(self): + return "Move camera to home" + + def Execute(self): + focusWindow = wx.Window.FindFocus() + + # Test to see if we have got a window with focus. If not + # we simply return and ignore this command. + if focusWindow == None: + return + + # Test to see if the window is of the right type. If not + # we simply return and ignore this command. + if isinstance(focusWindow, SceneWindow) == False: + return + + # All is well. Lets execute the command. + focusWindow.MoveCameraToHome() Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/QuitCommand.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/QuitCommand.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/QuitCommand.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -0,0 +1,12 @@ +#!/usr/bin/env python +import wx + +class QuitCommand: + + def GetCaption(self): + return "Quit" + + def Execute(self): + topWindow = wx.GetApp().GetTopWindow() + if topWindow != None: + topWindow.Close() Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/__init__.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/__init__.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/__init__.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -0,0 +1 @@ +#!/usr/bin/env python Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/SceneWindow.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/SceneWindow.py 2008-01-01 19:11:40 UTC (rev 2203) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/SceneWindow.py 2008-01-03 20:13:25 UTC (rev 2204) @@ -18,6 +18,9 @@ self.graphicsWindow.connectToSetCurrent(self.on_SetCurrent) self.graphicsWindow.connectToSwapBuffers(self.on_SwapBuffers) + def MoveCameraToHome(self): + print('not implemented yet') + def Frame(self): self.graphicsWindow.Frame() @@ -36,6 +39,13 @@ event.Skip() def on_Mouse(self, event): + # Set focus to this control in order for it to + # retreive keyboard input. The view that has focus should + # handle the keyboard command sent. + self.SetFocus() + + # The mouse event (movement and clicking) should be + # processed by the manipulator implemented in c++. x, y = event.GetX(), event.GetY() if event.ButtonDown(): button = event.GetButton() |