Commits

Andrew Peterson committed 1d13fca

Moved the management of the Viewer Dialogs into the Viewer State. Commands are now processed by the ViewerState, but actual communication with the server is routed through the "Client". Fixed Delayed Text Rendering/Updates.

Comments (0)

Files changed (2)

pyshipcommand_gui/states/viewer.py

 from djlib.primitives import Vector, Rect
 from djlib.spatial import ExpandingRectTree
 from pyshipcommand_gui.viewer import ShipViewer
+import pyshipcommand_gui.dialogs as dialogs
 
 # TestState
 class ViewerState(GameState, INotifyable):
         self.tree = None
         self.view_node = None
 
+        self.script_wnd = None
+        self.ship_wnd = None
+        self.cmd_wnd = None
+
     def initialize(self):
         """Called the first time the game is changed to this state
            during the applications lifecycle."""
         cmdImg = pygame.image.load(cmdImg)
         self.MSHIP_IMG = cmdImg.convert_alpha()
 
+        # Create ship, script, and cmd dialogs
+        self._createDialogs()
+
     def enter(self, client_init):
         """Called every time the game is switched to this state."""
         self.ship_view = ShipViewer(self.gc.rsurf)
         self.ship_view.setClientInit(client_init)
         self.ship_view.SHIP_IMG = self.SHIP_IMG
         self.ship_view.MSHIP_IMG = self.MSHIP_IMG
+        self.ship_view.FONT = self.gc.dfont
 
         self.updateUniverse(client_init.universe)
         self.centerView(client_init.start_pos)
 
         self.scroll_dir = None
 
+        # Show dialogs
+        ui = self.gc.uirender
+        ui.add_widget(self.cmd_wnd)
+        ui.add_widget(self.script_wnd)
+        ui.add_widget(self.ship_wnd)
+
+        # Update dialogs
+        self.script_wnd.update_list()
+
         # Register for input messages
         event_mgr = self.gc.uirender.get_managers()[0]
         event_mgr.add_object(self, SIG_KEYDOWN, SIG_MOUSEDOWN, SIG_MOUSEMOVE)
             # Handle arrow keys
             if event.data.key in (K_UP, K_DOWN, K_RIGHT, K_LEFT):
                 self.move(event.data.key)
-            '''
             elif event.data.key in (K_RETURN, K_SLASH):
-                g_uirender.set_active_layer(self.editWnd.depth)
-                self.editWnd.set_focus()
+                self.gc.uirender.set_active_layer(self.cmd_wnd.depth)
+                self.cmd_wnd.set_focus()
                 if event.data.key is K_SLASH:
-                    self.editWnd.set_text('/')
-                    self.editWnd.set_caret(1)
-            '''
+                    self.cmd_wnd.set_text('/')
+                    self.cmd_wnd.set_caret(1)
+            
         elif event.signal == SIG_MOUSEDOWN:
             # set keyboard focus
             self.gc.uirender.set_active_layer(0)
             if event.data.button == 1: #Left Click
                 real_pos = Vector(*event.data.pos) + self.view_rect.pos
                 ship = self.ship_view.pickShip(real_pos)
-                #self.shipWnd.setShip(ship)
-                #self.scriptWnd.setShip(ship)
+                self.ship_wnd.setShip(ship)
+                self.script_wnd.setShip(ship)
                 print str(ship)
 
         elif event.signal == SIG_MOUSEMOVE:
 
         # Remove any expired delay text
         while len(self.delay_text) > 0 and self.gc.time > self.delay_text[0][0]:
+            print "Removing Text", self.gc.time, self.delay_text[0]
             del self.delay_text[0]
 
     def render(self):
             self.ship_view.renderText(rs, drawPos.intArgs(), dText[1])
             drawPos -= Vector(0, 20)
 
+        # draw current FPS over everything
+        self.ship_view.renderText(rs, (0,0), str(int(self.gc.fpsClock.get_fps())))
+
         # Now apply UI
-        #rs.blit(self.gc.uirender.screen, (0,0), None, pygame.BLEND_ADD)
-        pygame.display.update()
+        rs.blit(self.gc.uirender.screen, (0,0), None, pygame.BLEND_ADD)
 
+        pygame.display.update()
 
     def delayedText(self, text, delay):
         if not isinstance(text, list):
             
         for t in text:
             print t
-            self.delayText.append( (self.time+delay, t) )
-        print self.delayText
+            self.delay_text.append( (self.gc.time+delay, t) )
+        print self.delay_text
 
     def leave(self):
         """Called whenever we switch from this state to another."""
         event_mgr = self.gc.uirender.get_managers()[0]
         event_mgr.remove_object(self, SIG_KEYDOWN, SIG_MOUSEDOWN, SIG_MOUSEMOVE)
 
+        # Hide dialogs
+        ui = self.gc.uirender
+        ui.remove_widget(self.cmd_wnd)
+        ui.remove_widget(self.script_wnd)
+        ui.remove_widget(self.ship_wnd)
+
         self.ship_view = None
 
     def shutdown(self):
         print str(self.view_rect)
         self.gc.stars.setView(self.view_rect, True)
 
+    def _createDialogs(self):
+        print "CREATING VIEWER DIALOGS!!!!"
+        (w,h) = self.gc.rsurf.get_size()
+        
+        # CLI Entry
+        edit = widgets.Entry()
+        edit.topleft = (0, h-20)
+        edit.minsize = w, 20
+        edit.depth = 1
+        edit.connect_signal(SIG_INPUT, self.processCmd, edit)
+        self.cmd_wnd = edit
+
+        # Script widget
+        self.script_wnd = dialogs.ScriptWidget(w-205, 0, self.gc.client.scripts,
+                                              self.cmd_import,
+                                              self.cmd_upload, self.cmd_ship)
+
+        # Ship Info
+        self.ship_wnd = dialogs.ShipInfo(w-205, self.script_wnd.height)
+
+    # CMD HANDLERS
 
     def processCmd(self, edit):
 
 
         # Raw text should be treated as a chat message.
         if line[0] != '/':
-            self.do_chat(line)
+            self.cmd_chat(line)
             return
 
         # Split line and remove leading '/'
             except Exception, e:
                     print 'Error', str(e)
 
-    # CMD HANDLERS
-
-    def cmd_cl(self):
-        self.connect(SERVER_ADDR, SERVER_PORT)
-        self.login(DEFAULT_USER, DEFAULT_PASS)     
-
     def cmd_help(self, command=None):
         """help [command]: List commands, or show help on the given command"""
         if command:
         self.gc.quit()
 
     def cmd_info(self):
-        self.server.callRemote("info")
+        self.gc.client.info()
 
     def cmd_upload(self, scriptfile):
 
         if not scriptfile:
             raise Exception("script <script_data>")
 
-        s = self.cscript.getScript(scriptfile)
+        s = self.gc.client.scripts.getScript(scriptfile)
         if s:
             print "sending script:", s.name
-            self.server.callRemote("giveScript", s.name, s.source)
+            self.gc.client.giveScript(s.name, s.source)
             return
 
         print scriptfile + " not found!"
 
         if not scriptPath:
             script_dlg = dialogs.ImportScriptDialog(self.cmd_import, *g_rsurf.get_size())
-            g_uirender.add_widget(script_dlg)
+            self.gc.uirender.add_widget(script_dlg)
             script_dlg.set_focus()
             return
 
             scriptFiles = glob(path.join(scriptPath,"*.py"))
 
         for s in scriptFiles:
-            self.cscript.importScript(s)
+            self.gc.client.scripts.importScript(s)
 
         # Update list contents, even if we could have failed
         if len(scriptFiles) > 0:
-            self.scriptWnd.update_list()
-            #self.scriptWnd.select_script(s.name)
+            self.script_wnd.update_list()
+            #self.script_wnd.select_script(s.name)
             return
 
         print "Could not import:" + scriptFiles
 
     def cmd_reload(self):
 
-        self.cscript.reloadScripts()
-        self.scriptWnd.update_list()
+        self.gc.client.scripts.reloadScripts()
+        self.script_wnd.update_list()
 
 
     def cmd_ship(self, cmd, *args):
         print "cmd_ship", cmd, ":", args
         if cmd == "create":
-            pos = NetVector(0.0, 0.0)
+            pos = Vector(0.0, 0.0)
             if len(args) == 2:
-                pos = NetVector(float(args[0]), float(args[1]))
+                pos = Vector(float(args[0]), float(args[1]))
             print pos
-            self.server.callRemote("addShip", pos)
+            self.gc.client.addShip(pos)
         elif cmd == "move":
             ship_id = int(args[0])
-            target_pos = NetVector(float(args[1]), float(args[2]))
+            target_pos = Vector(float(args[1]), float(args[2]))
             print target_pos
-            self.server.callRemote("moveShip", ship_id, target_pos)
+            self.gc.client.moveShip(ship_id, target_pos)
         elif cmd == "orbit":
             ship_id = int(args[0])
-            target_pos = NetVector(float(args[1]), float(args[2]))
+            target_pos = Vector(float(args[1]), float(args[2]))
             radius = float(args[3])
-            self.server.callRemote("orbitShip", ship_id, target_pos, radius)
+            self.gc.client.orbitShip(ship_id, target_pos, radius)
         elif cmd == "follow":
             ship_id = int(args[0])
             target_id = int(args[1])
             dist = float(args[2])
-            self.server.callRemote("followShip", ship_id, target_id, dist)
+            self.gc.client.followShip(ship_id, target_id, dist)
         elif cmd == "script":
             ship_id = int(args[0])
             script_name = args[1]
             if len(args) > 2:
                 action = args[2]
 
-            self.server.callRemote("scriptShip", ship_id, script_name, action)
+            self.gc.client.scriptShip(ship_id, script_name, action)
 
     def cmd_scroll(self, *args):
         scroll_speed = ShipViewer.SCROLL_SPEED
 
     def cmd_msg(self, *args):
         print "cmd_msg, ", args
-        self.server.callRemote("giveMessage", args)
+        self.gc.client.giveMessage(args)
 
     def cmd_home(self):
         ship_view.centerView()
 
     def cmd_chat(self, msg):
-        self.server.callRemote("giveChat", msg)
+        self.gc.client.giveChat(msg)
 
     def cmd_players(self):
-        self.server.callRemote("getPlayers")
+        self.gc.client.getPlayers()
+        
 
 #end TestState
 

pyshipcommand_gui/viewer.py

     # ASSETS
     SHIP_IMG = None
     MSHIP_IMG = None
+    FONT = None
 
     FONT_COLOR = pygame.Color(0, 255, 0)
     TREE_COLOR = pygame.Color(128, 128, 128)
         self.tree = ExpandingRectTree(Rect.fromSides(0, 0, w, h))
         self.view_node = None
 
-        self.font = pygame.font.Font(pygame.font.get_default_font(), 12)
-        if not self.font:
-            print "Default Font not loaded!"
-
     def setClientInit(self, init_data):
 
         print "ClientInit Received with Player ID %d" % (self.player_id)
                 self.drawTree(rs, child, pos)
 
     def renderText(self, rs, pos, text, centered = False):
-        surf = self.font.render(text, False, self.FONT_COLOR)
+        surf = self.FONT.render(text, False, self.FONT_COLOR)
         if centered:
             pos = (pos[0]-surf.get_width()/2, pos[1]-surf.get_height()/2)
         rs.blit(surf, pos)