From 30c57b62f1742d48bce4823ba13d60dd10176f93 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Tue, 7 Dec 2010 11:12:34 +0530 Subject: [PATCH] Grid focus and input mechanism underway 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 | 99 ++++++++++++++++++++++++++++++++++++++++---- playerwindow.glade | 12 ++++-- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/player_mainwindow.py b/player_mainwindow.py index 00be083..4524106 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -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 () diff --git a/playerwindow.glade b/playerwindow.glade index 9441946..d13e901 100644 --- a/playerwindow.glade +++ b/playerwindow.glade @@ -19,8 +19,8 @@ - 480 - 360 + 640 + 480 True GetAClue player 640 @@ -140,10 +140,14 @@ queue - 120 - 120 True + True + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_STRUCTURE_MASK + + + + -- 2.20.1