Grid focus and input mechanism underway
authorHarishankar <v.harishankar@gmail.com>
Tue, 7 Dec 2010 05:42:34 +0000 (11:12 +0530)
committerHarishankar <v.harishankar@gmail.com>
Tue, 7 Dec 2010 05:42:34 +0000 (11:12 +0530)
Puzzle Grid now responds to focus and input. Also implemented
the movement of the item selection in the grid by using
shift+arrow keys

player_mainwindow.py
playerwindow.glade

index 00be083..4524106 100644 (file)
@@ -15,34 +15,117 @@ class MainWindow:
        def gtk_main_quit (self, *args):
                gtk.main_quit ()
 
-       # drawing for puzzle grid
+       # callback for puzzle grid mouse button release event
+       def on_puzzlegrid_button_release_event (self, drawarea, event):
+               self.window.set_focus (drawarea)
+               return True
+
+       # moving the current selection in grid by one up or down
+       def move_selection_updown (self, step):
+               # increase or reduce the row by step until an occupied grid is found
+               # black block
+               last_occupied_row = self.selected_row
+               while True:
+                       self.selected_row += step
+                       if self.selected_row < 0 or self.selected_row >= self.puzzle.rows:
+                               self.selected_row = last_occupied_row
+                               break
+                       if (self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True
+                               or self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True):
+                               break
+
+       # moving the current selection in grid by one across either way
+       def move_selection_across (self, step):
+               # increase or reduce the row by step until an occupied grid is found
+               # black block
+               last_occupied_col = self.selected_col
+               while True:
+                       self.selected_col += step
+                       if self.selected_col < 0 or self.selected_col >= self.puzzle.cols:
+                               self.selected_col = last_occupied_col
+                               break
+                       if (self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True
+                               or self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True):
+                               break
+
+       # callback for puzzle grid key release event
+       def on_puzzlegrid_key_press_event (self, drawarea, event):
+               key = gtk.gdk.keyval_name (event.keyval)
+
+               if event.state == gtk.gdk.SHIFT_MASK and key == "Up":
+                       # reduce the row by 1 until you find an occupied grid and not a
+                       # black block
+                       self.move_selection_updown (-1)
+                       drawarea.queue_draw ()
+               elif event.state == gtk.gdk.SHIFT_MASK and key == "Down":
+                       # increase the row by 1 until you find an occupied grid and not a
+                       # black block
+                       self.move_selection_updown (1)
+                       drawarea.queue_draw ()
+               elif event.state == gtk.gdk.SHIFT_MASK and key == "Right":
+                       # increase the column by 1 until you find an occupied grid and not
+                       # a black block
+                       self.move_selection_across (1)
+                       drawarea.queue_draw ()
+               elif event.state == gtk.gdk.SHIFT_MASK and key == "Left":
+                       # decrease the column by 1 until you find an occupied grid and not
+                       # a black block
+                       self.move_selection_across (-1)
+                       drawarea.queue_draw ()
+               return False
+
+       # puzzle grid focus in event
+       def on_puzzlegrid_focus_out_event (self, drawarea, event):
+               col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("gray"))
+               drawarea.window.set_background (col)
+
+               return False
+
+       # puzzle grid focus out event
+       def on_puzzlegrid_focus_in_event (self, drawarea, event):
+               col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("white"))
+               drawarea.window.set_background (col)
+               return False
+
+       # callback for drawing the puzzle grid
        def on_puzzlegrid_expose_event (self, drawarea, event):
                # if puzzle is loaded
                if self.puzzle:
                        # size the area
                        drawarea.set_size_request (self.puzzle.cols*30+2, self.puzzle.rows*30+2)
 
-                       # set background color to white
-                       col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("white"))
-                       drawarea.window.set_background (col)
-
                        #numlayout = gtk.PrintContext().create_pango_layout ()
                        #numlayout.set_font_description (pango.FontDescription ("Sans 8"))
                        ctx = drawarea.window.cairo_create ()
                        ctx.set_line_width (1.5)
 
+                       # run through the grid
                        for row in range (self.puzzle.rows):
                                for col in range (self.puzzle.cols):
+                                       # (re)set foreground color
+                                       ctx.set_source_rgb (0, 0, 0)
                                        # if the area is not occupied
                                        if (self.puzzle.data[row][col].occupied_across is False and
                                                self.puzzle.data[row][col].occupied_down is False):
                                                ctx.rectangle (col*30, row*30, 30, 30)
                                                ctx.fill ()
                                        else:
-                                               ctx.rectangle (col*30, row*30, 30, 30)
-                                               ctx.stroke ()
+                                               # if selected row/column
+                                               if row == self.selected_row and col == self.selected_col:
+                                                       ctx.set_source_rgb (1, 1, 0.6)
+                                                       ctx.rectangle (col*30,row*30, 30, 30)
+                                                       ctx.fill ()
+                                               else:
+                                                       ctx.set_source_rgb (1, 1, 1)
+                                                       ctx.rectangle (col*30, row*30, 30, 30)
+                                                       ctx.fill ()
+                                                       ctx.set_source_rgb (0, 0, 0)
+                                                       ctx.rectangle (col*30, row*30, 30, 30)
+                                                       ctx.stroke ()
+
                                                # if numbered
                                                if self.puzzle.data[row][col].numbered <> 0:
+                                                       ctx.set_source_rgb (0, 0, 0)
                                                        ctx.select_font_face ("Sans 7")
                                                        ctx.move_to (col*30+2, row*30+10)
                                                        ctx.show_text (str(self.puzzle.data[row][col].numbered))
@@ -71,6 +154,8 @@ class MainWindow:
 
        def open_file (self, file):
                self.puzzle = cPickle.load (open (file, "rb"))
+               self.selected_row = 0
+               self.selected_col = 0
                self.window.set_title ("GetAClue player - " + file)
                self.load_clues ()
 
index 9441946..d13e901 100644 (file)
@@ -19,8 +19,8 @@
     </columns>
   </object>
   <object class="GtkWindow" id="mainwindow">
-    <property name="width_request">480</property>
-    <property name="height_request">360</property>
+    <property name="width_request">640</property>
+    <property name="height_request">480</property>
     <property name="visible">True</property>
     <property name="title" translatable="yes">GetAClue player</property>
     <property name="default_width">640</property>
                     <property name="resize_mode">queue</property>
                     <child>
                       <object class="GtkDrawingArea" id="puzzlegrid">
-                        <property name="width_request">120</property>
-                        <property name="height_request">120</property>
                         <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="events">GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_STRUCTURE_MASK</property>
                         <signal name="expose_event" handler="on_puzzlegrid_expose_event"/>
+                        <signal name="key_press_event" handler="on_puzzlegrid_key_press_event"/>
+                        <signal name="focus_in_event" handler="on_puzzlegrid_focus_in_event"/>
+                        <signal name="button_release_event" handler="on_puzzlegrid_button_release_event"/>
+                        <signal name="focus_out_event" handler="on_puzzlegrid_focus_out_event"/>
                       </object>
                     </child>
                   </object>