Skip to content

Commit

Permalink
Small refactor making types more consistent
Browse files Browse the repository at this point in the history
Add a NONE to GameMode and SudokuWindowScreen
Initialize objects before using them
Always set the value label to 0 when it's empty
  • Loading branch information
JohanG-75 committed Jun 24, 2024
1 parent a58ab38 commit 08c4ca4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 70 deletions.
3 changes: 2 additions & 1 deletion lib/sudoku-game.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class SudokuGame : Object
public SudokuGame (SudokuBoard board)
{
this.board = board;
this.mode = GameMode.PLAY;
this.mode = GameMode.NONE;
timer = new Timer();
stack = new ArrayList<stack_item?>();
}
Expand Down Expand Up @@ -396,6 +396,7 @@ public class SudokuGame : Object

public enum GameMode
{
NONE,
PLAY,
CREATE;
}
Expand Down
64 changes: 25 additions & 39 deletions src/sudoku-cell.vala
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ private class SudokuCell : Widget
private SudokuGame game;
private unowned SudokuView view;

private GestureClick button_controller = new GestureClick ();
private GestureLongPress long_press_controller = new GestureLongPress ();
private GestureClick button_controller;
private GestureLongPress long_press_controller;

//Only initialized when the cell is not fixed
private bool control_key_pressed;
private Popover popover;
private EventControllerKey key_controller;
private EventControllerKey popover_controller;
private NumberPicker earmark_picker;
private NumberPicker value_picker;

// The label can also be set to X if the label is invalid.
// If this happens, the value **must not** be changed, only the label.
private Label value_label = new Label ("") {
visible = false
};
private Label[] earmark_labels = new Label[9];
private Label value_label;
private Label[] earmark_labels;

public SudokuCell (int row, int col, SudokuGame game, SudokuView view)
{
Expand All @@ -56,29 +53,28 @@ private class SudokuCell : Widget
this.game = game;
this.view = view;

if (value != 0)
{
value_label.set_label (this.value.to_string ());
value_label.set_visible (true);
}
value_label = new Label (this.value.to_string ());
value_label.visible = value != 0;
value_label.set_parent (this);

focusable = true;
can_focus = true;
notify["has-focus"].connect (focus_changed_cb);

this.set_fixed_css (true);

this.notify["has-focus"].connect (focus_changed_cb);
this.button_controller.set_button (0 /* all buttons */);
set_fixed_css (true);

this.add_controller (this.button_controller);
this.add_controller (this.long_press_controller);
button_controller = new GestureClick ();
button_controller.set_button (0 /* all buttons */);
add_controller (this.button_controller);

this.long_press_controller.pressed.connect (long_press_cb);
this.button_controller.released.connect (button_released_cb);
long_press_controller = new GestureLongPress ();
add_controller (this.long_press_controller);

value_label.set_parent (this);
long_press_controller.pressed.connect (long_press_cb);
button_controller.released.connect (button_released_cb);

int num = 0;
earmark_labels = new Label[9];
for (int row_tmp = 0; row_tmp < game.board.block_rows; row_tmp++)
{
for (int col_tmp = 0; col_tmp < game.board.block_cols; col_tmp++)
Expand All @@ -103,7 +99,8 @@ private class SudokuCell : Widget
popover = new Popover ();
popover.set_autohide (false);
popover.set_parent (this);
var popover_controller = new EventControllerKey ();

popover_controller = new EventControllerKey ();
popover_controller.key_pressed.connect (key_pressed_cb);
popover_controller.key_released.connect (key_released_cb);
(popover as Widget)?.add_controller (popover_controller);
Expand Down Expand Up @@ -226,14 +223,8 @@ private class SudokuCell : Widget

public void update_value ()
{
if (value != 0)
{
value_label.set_label (this.value.to_string ());
value_label.set_visible (true);
}
else
value_label.set_visible (false);

value_label.set_label (this.value.to_string ());
value_label.visible = value != 0;
get_visible_earmarks ();
}

Expand Down Expand Up @@ -326,18 +317,14 @@ private class SudokuCell : Widget

gesture.set_state (EventSequenceState.CLAIMED);

if (this.view.number_picker_second_click && !selected)
if ((this.view.number_picker_second_click && !selected) ||
is_fixed || game.paused)
{
grab_focus ();
return;
}
else
{
grab_focus ();
}

if (is_fixed || game.paused)
return;
grab_focus ();

bool want_earmark = control_key_pressed;
if (view.earmark_mode)
Expand All @@ -349,7 +336,6 @@ private class SudokuCell : Widget
show_value_picker ();
else if (game.mode == GameMode.PLAY && this.value == 0)
show_earmark_picker ();

}
else if (gesture.get_current_button () == BUTTON_SECONDARY)
{
Expand Down
17 changes: 4 additions & 13 deletions src/sudoku-view.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public class SudokuView : Adw.Bin
{
private SudokuGame game;
private SudokuCell[,] cells;
private SudokuFrame frame;
private Label paused_label;

public bool earmark_mode = false;
public bool autoclean_earmarks;
public bool number_picker_second_click;
public bool highlight_row_column;
public bool highlight_block;
public bool highlight_numbers;

SudokuFrame frame;
Label paused_label;

public int selected_row { get; private set; default = 0; }
public int selected_col { get; private set; default = 0; }

Expand Down Expand Up @@ -75,10 +75,10 @@ public class SudokuView : Adw.Bin
else
this._show_warnings = settings.get_boolean ("show-warnings");
this._show_possibilities = settings.get_boolean ("show-possibilities");
this._number_picker_second_click = settings.get_boolean ("number-picker-second-click");
this._simple_warnings = settings.get_boolean ("simple-warnings");
this._show_earmark_warnings = settings.get_boolean ("show-earmark-warnings");
this._highlighter = settings.get_boolean ("highlighter");
this.number_picker_second_click = settings.get_boolean ("number-picker-second-click");
this.autoclean_earmarks = settings.get_boolean ("autoclean-earmarks");
this.highlight_row_column = settings.get_boolean ("highlight-row-column");
this.highlight_block = settings.get_boolean ("highlight-block");
Expand Down Expand Up @@ -388,15 +388,6 @@ public class SudokuView : Adw.Bin
}
}

private bool _number_picker_second_click;
public bool number_picker_second_click
{
get { return _number_picker_second_click; }
set {
_number_picker_second_click = value;
}
}

private bool _simple_warnings;
public bool simple_warnings
{
Expand Down
35 changes: 18 additions & 17 deletions src/sudoku-window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ using Gdk;
public class SudokuWindow : Adw.ApplicationWindow
{
[GtkChild] private unowned Adw.WindowTitle windowtitle;
[GtkChild] private unowned Adw.HeaderBar headerbar;

[GtkChild] private unowned Box game_box; // Holds the view

[GtkChild] private unowned Box start_box;
[GtkChild] private unowned Button start_button;
Expand All @@ -34,22 +37,18 @@ public class SudokuWindow : Adw.ApplicationWindow
[GtkChild] private unowned CheckButton hard_check;
[GtkChild] private unowned CheckButton very_hard_check;

[GtkChild] private unowned Box game_box; // Holds the view

[GtkChild] private unowned MenuButton main_menu;
[GtkChild] private unowned ToggleButton earmark_mode_button;
[GtkChild] private unowned Button undo_button;
[GtkChild] private unowned Button redo_button;
[GtkChild] private unowned Button back_button;
[GtkChild] private unowned Button unfullscreen_button;
[GtkChild] private unowned Button play_custom_game_button;
[GtkChild] private unowned Button play_pause_button;

[GtkChild] private unowned Box clock_box;
[GtkChild] private unowned Label clock_label;

[GtkChild] private unowned Button play_custom_game_button;
[GtkChild] private unowned Button play_pause_button;
[GtkChild] private unowned Adw.HeaderBar headerbar;

private bool window_is_maximized;
private bool window_is_fullscreen;
private int window_width;
Expand All @@ -61,21 +60,21 @@ public class SudokuWindow : Adw.ApplicationWindow
private int smallest_possible_height;
private bool window_width_is_small;
private bool window_height_is_small;
Adw.Breakpoint small_window_breakpoint;
Adw.BreakpointCondition small_window_condition;
private Adw.Breakpoint small_window_breakpoint;
private Adw.BreakpointCondition small_window_condition;

private const int margin_default_size = 25;
private const int margin_small_size = 10;
private const int margin_size_diff = margin_default_size - margin_small_size;

public GLib.Settings settings;
private SudokuGame? game = null;

public SudokuView? view { get; private set; default = null;}
public SudokuWindowScreen? current_screen = null;
private GestureClick button_controller;
private GestureLongPress long_press_controller;

private GestureClick button_controller = new GestureClick ();
private GestureLongPress long_press_controller = new GestureLongPress ();
public GLib.Settings settings { get; private set;}
public SudokuView? view { get; private set; default = null;}
public SudokuWindowScreen current_screen { get; private set; default = SudokuWindowScreen.NONE;}

public SudokuWindow (GLib.Settings settings)
{
Expand Down Expand Up @@ -126,12 +125,13 @@ public class SudokuWindow : Adw.ApplicationWindow
set_gamebox_width_margins (window_width);
set_gamebox_height_margins (window_height);
}

this.button_controller.set_button (0 /* all buttons */);
this.button_controller.released.connect (button_released_cb);
button_controller = new GestureClick ();
button_controller.set_button (0 /* all buttons */);
button_controller.released.connect (button_released_cb);
((Widget)this).add_controller (this.button_controller);

this.long_press_controller.pressed.connect (long_press_cb);
long_press_controller = new GestureLongPress ();
long_press_controller.pressed.connect (long_press_cb);
((Widget)this).add_controller (this.long_press_controller);

this.close_request.connect (close_cb);
Expand Down Expand Up @@ -449,6 +449,7 @@ public class SudokuWindow : Adw.ApplicationWindow
//must be aligned with GameMode
public enum SudokuWindowScreen
{
NONE,
PLAY,
CREATE,
MENU;
Expand Down

0 comments on commit 08c4ca4

Please sign in to comment.