[gnome-sudoku] Show earmark picker on right-click and Ctrl-Click
- From: Parin Porecha <parinporecha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-sudoku] Show earmark picker on right-click and Ctrl-Click
- Date: Sun, 1 Jun 2014 06:04:15 +0000 (UTC)
commit ade97fef8c16e72a5e526cca70e6acd77b0fa44a
Author: Parin Porecha <parinporecha gmail com>
Date: Sun Jun 1 11:34:37 2014 +0530
Show earmark picker on right-click and Ctrl-Click
and show earmarked numbers inline across the top
src/number-picker.vala | 26 ++++++++++++++++++++-
src/sudoku-board.vala | 10 ++++++++
src/sudoku-view.vala | 58 +++++++++++++++++++++++++++++++++---------------
3 files changed, 75 insertions(+), 19 deletions(-)
---
diff --git a/src/number-picker.vala b/src/number-picker.vala
index 761ca1a..a6bf8aa 100644
--- a/src/number-picker.vala
+++ b/src/number-picker.vala
@@ -11,8 +11,12 @@ private class NumberPicker : Gtk.Grid
private Button clear_button;
+ private static const int EARMARKS_MAX_ALLOWED = 5;
+ private int earmarks_active;
+
public NumberPicker (ref SudokuBoard board, bool earmark = false) {
this.board = board;
+ earmarks_active = 0;
for (var col = 0; col < board.block_cols; col++)
{
@@ -37,7 +41,13 @@ private class NumberPicker : Gtk.Grid
{
var toggle_button = (ToggleButton) button;
toggle_button.toggled.connect (() => {
- earmark_state_changed (n, toggle_button.get_active ());
+ var toggle_active = toggle_button.get_active ();
+ earmark_state_changed (n, toggle_active);
+ earmarks_active = toggle_active ? earmarks_active + 1 : earmarks_active - 1;
+ if (earmarks_active >= EARMARKS_MAX_ALLOWED)
+ set_toggle_sensitive (false);
+ else if (earmarks_active == (EARMARKS_MAX_ALLOWED - 1))
+ set_toggle_sensitive (true);
});
}
@@ -84,4 +94,18 @@ private class NumberPicker : Gtk.Grid
button.set_active (board.earmarks[row, col, i]);
}
}
+
+ private void set_toggle_sensitive (bool state)
+ {
+ if (state)
+ for (var i = 0; i < board.max_val; i++)
+ this.get_child_at (i % board.block_cols, i / board.block_rows).sensitive = true;
+ else
+ for (var i = 0; i < board.max_val; i++)
+ {
+ var button = (ToggleButton) this.get_child_at (i % board.block_cols, i / board.block_rows);
+ if (!button.active)
+ button.sensitive = false;
+ }
+ }
}
diff --git a/src/sudoku-board.vala b/src/sudoku-board.vala
index 2c2dcaf..810bd40 100644
--- a/src/sudoku-board.vala
+++ b/src/sudoku-board.vala
@@ -525,6 +525,16 @@ public class SudokuBoard
return file.query_exists ();
}
+
+ public string get_earmarks_string (int row, int col)
+ {
+ string s = "";
+ for (var i = 1; i <= max_val; i++)
+ if (earmarks[row, col, i-1])
+ s += i.to_string ();
+
+ return s;
+ }
}
public enum House {
diff --git a/src/sudoku-view.vala b/src/sudoku-view.vala
index 87ae89b..279d376 100644
--- a/src/sudoku-view.vala
+++ b/src/sudoku-view.vala
@@ -194,7 +194,7 @@ private class SudokuCellView : Gtk.DrawingArea
public override bool button_press_event (Gdk.EventButton event)
{
- if (event.button != 1)
+ if (event.button != 1 && event.button != 3)
return false;
if (!is_focus)
@@ -203,11 +203,15 @@ private class SudokuCellView : Gtk.DrawingArea
return false;
}
- var event_height = event.y / get_allocated_height ();
- if (!_show_possibilities && (event_height < 0.25 || event_height > 0.75))
+ if (event.button == 1) // Primary-Click
+ {
+ if (!_show_possibilities && (event.state & ModifierType.CONTROL_MASK) > 0)
+ show_earmark_picker ();
+ else
+ show_number_picker ();
+ }
+ else if (!_show_possibilities && event.button == 3) // Secondary-Click
show_earmark_picker ();
- else
- show_number_picker ();
return false;
}
@@ -343,25 +347,43 @@ private class SudokuCellView : Gtk.DrawingArea
c.restore ();
}
- double earmark_size = get_allocated_height () / (size_ratio * 2);
- c.set_font_size (earmark_size);
- c.set_source_rgb (0.0, 0.0, 0.0);
+ if (!_show_possibilities)
+ {
+ // Draw the earmarks
+ double earmark_size = get_allocated_height () / (size_ratio * 2);
+ c.set_font_size (earmark_size);
- int height = get_allocated_height () / game.board.block_cols;
- int width = get_allocated_height () / game.board.block_rows;
+ c.move_to (0, earmark_size);
- int num = 0;
- for (int row = 0; row < game.board.block_rows; row++)
- for (int col = 0; col < game.board.block_cols; col++)
- {
- num++;
+ c.set_source_rgb (0.0, 0.0, 0.0);
+ c.show_text (game.board.get_earmarks_string (_row, _col));
+ }
+ else if (value == 0)
+ {
+ double possibility_size = get_allocated_height () / (size_ratio * 2);
+ c.set_font_size (possibility_size);
+ c.set_source_rgb (0.0, 0.0, 0.0);
+
+ bool[] possibilities = game.board.get_possibilities_as_bool_array(row, col);
- if (game.board.earmarks[_row, _col, num-1] || (_show_possibilities && game.board.is_possible
(_row, _col, num)))
+ int height = get_allocated_height () / game.board.block_cols;
+ int width = get_allocated_height () / game.board.block_rows;
+
+ int num = 0;
+ for (int row = 0; row < game.board.block_rows; row++)
+ {
+ for (int col = 0; col < game.board.block_cols; col++)
{
- c.move_to (col * width, (row * height) + earmark_size);
- c.show_text ("%d".printf(num));
+ num++;
+
+ if (possibilities[num - 1])
+ {
+ c.move_to (col * width, (row * height) + possibility_size);
+ c.show_text ("%d".printf(num));
+ }
}
}
+ }
if (is_fixed)
return false;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]