[gnome-2048] Use internal instead of public.



commit 479b5b9161f5062cea2e1be9f30ed9b5ba106c12
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Jan 31 16:03:28 2019 +0100

    Use internal instead of public.
    
    Remove also the ColorPalette class.

 src/application.vala |   8 +--
 src/game.vala        |  73 ++++++++++++-----------
 src/grid.vala        |  86 +++++++++++++--------------
 src/view.vala        | 160 +++++++++++++++++++++------------------------------
 4 files changed, 149 insertions(+), 178 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 5af4985..c73d9c3 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -20,7 +20,7 @@
 using Games;
 using Gtk;
 
-public class Application : Gtk.Application
+private class Application : Gtk.Application
 {
     /* settings */
     private GLib.Settings _settings;
@@ -65,7 +65,7 @@ public class Application : Gtk.Application
         { "about",              about_cb                    },
     };
 
-    public static int main (string[] args)
+    private static int main (string [] args)
     {
         Intl.setlocale (LocaleCategory.ALL, "");
         Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
@@ -106,7 +106,7 @@ public class Application : Gtk.Application
         return app.run (args);
     }
 
-    public Application ()
+    private Application ()
     {
         Object (application_id: "org.gnome.TwentyFortyEight", flags: ApplicationFlags.FLAGS_NONE);
     }
@@ -413,7 +413,7 @@ public class Application : Gtk.Application
         menu.append (label, "app.new-game-sized(" + variant.print (/* annotate types */ true) + ")");
     }
 
-    public static bool is_disallowed_grid_size (ref int rows, ref int cols)
+    internal static bool is_disallowed_grid_size (ref int rows, ref int cols)
     {
         return (rows == 1 && cols == 2) || (rows == 2 && cols == 1);
     }
diff --git a/src/game.vala b/src/game.vala
index c8561a3..20a3dcb 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -17,7 +17,7 @@
  * along with GNOME 2048; if not, see <http://www.gnu.org/licenses/>.
  */
 
-public class Game : Object
+private class Game : Object
 {
     enum GameState {
         STOPPED,
@@ -37,9 +37,6 @@ public class Game : Object
     private Grid _grid;
 
     private uint _finish_move_id = 0;
-    private Clutter.Actor _view;
-    private Clutter.Actor _view_background;
-    private Clutter.Actor _view_foreground;
     private RoundedRectangle [,] _background;
     private bool _background_init_done = false;
     private TileView? [,] _foreground_cur;
@@ -65,12 +62,12 @@ public class Game : Object
 
     private uint _resize_view_id;
 
-    public signal void finished ();
-    public signal void target_value_reached (uint val);
-    public signal void undo_enabled ();
-    public signal void undo_disabled ();
+    internal signal void finished ();
+    internal signal void target_value_reached (uint val);
+    internal signal void undo_enabled ();
+    internal signal void undo_disabled ();
 
-    public Game (GLib.Settings settings)
+    internal Game (GLib.Settings settings)
     {
         Object ();
 
@@ -84,11 +81,6 @@ public class Game : Object
 
         _settings.bind ("target-value", _grid, "target-value", GLib.SettingsBindFlags.DEFAULT);
 
-        _view_background = new Clutter.Actor ();
-        _view_foreground = new Clutter.Actor ();
-        _view_background.show ();
-        _view_foreground.show ();
-
         _allow_undo = _settings.get_boolean ("allow-undo");
         _undo_stack_max_size = _settings.get_uint ("allow-undo-max");
 
@@ -97,19 +89,44 @@ public class Game : Object
         _state = GameState.STOPPED;
     }
 
-    public Clutter.Actor view {
-        get { return _view; }
-        set {
+    /*\
+    * * view
+    \*/
+
+    private Clutter.Actor _view;
+    private Clutter.Actor _view_background;
+    private Clutter.Actor _view_foreground;
+
+    internal Clutter.Actor view {
+        internal get { return _view; }
+        internal set {
             _view = value;
             _view.allocation_changed.connect (_on_allocation_changed);
+
+            _view_background = new Clutter.Actor ();
+            _view_foreground = new Clutter.Actor ();
+            _view_background.show ();
+            _view_foreground.show ();
             _view.add_child (_view_background);
             _view.add_child (_view_foreground);
         }
     }
 
+    private void _on_allocation_changed (Clutter.ActorBox box, Clutter.AllocationFlags flags)
+    {
+        if (_background_init_done)
+            _resize_view ();
+        else
+            _init_background ();
+    }
+
+    /*\
+    * * others
+    \*/
+
     internal uint score { internal get; private set; default = 0; }
 
-    public void new_game ()
+    internal void new_game ()
     {
         if (_finish_move_id > 0)
             Source.remove (_finish_move_id);
@@ -128,7 +145,7 @@ public class Game : Object
         undo_disabled ();
     }
 
-    public void undo ()
+    internal void undo ()
     {
         Grid grid = _undo_stack.poll_head ();
         uint delta_score = _undo_score_stack.poll_head ();
@@ -142,7 +159,7 @@ public class Game : Object
             undo_disabled ();
     }
 
-    public void save_game ()
+    internal void save_game ()
     {
         string contents = "";
 
@@ -158,7 +175,7 @@ public class Game : Object
         }
     }
 
-    public bool restore_game ()
+    internal bool restore_game ()
     {
         string contents;
         string [] lines;
@@ -192,7 +209,7 @@ public class Game : Object
         return _state != GameState.IDLE;
     }
 
-    public void reload_settings ()
+    internal void reload_settings ()
     {
         int rows, cols;
         bool allow_undo;
@@ -227,14 +244,6 @@ public class Game : Object
      // return false;
     }
 
-    private void _on_allocation_changed (Clutter.ActorBox box, Clutter.AllocationFlags flags)
-    {
-        if (_background_init_done)
-            _resize_view ();
-        else
-            _init_background ();
-    }
-
     private void _init_background ()
     {
         int rows = _grid.rows;
@@ -565,7 +574,7 @@ public class Game : Object
         TileView? tile_view = _foreground_cur [pos.row, pos.col];
         if (tile_view == null)
             assert_not_reached ();
-        debug (@"diming tile at $pos " + ((!) tile_view).@value.to_string ());
+        debug (@"diming tile at $pos " + ((!) tile_view).@tile_value.to_string ());
 
         Clutter.Actor actor;
         Clutter.PropertyTransition trans;
@@ -693,7 +702,7 @@ public class Game : Object
             if (tile_view == null)
                 assert_not_reached ();
             ((!) tile_view).actor.hide ();
-            debug (@"remove child " + ((!) tile_view).@value.to_string ());
+            debug (@"remove child " + ((!) tile_view).@tile_value.to_string ());
             _view_foreground.remove_child (((!) tile_view).actor);
 
             _foreground_cur [pos.row, pos.col] = null;
diff --git a/src/grid.vala b/src/grid.vala
index 6a0cca6..f0f8c3f 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -17,36 +17,28 @@
  * along with GNOME 2048; if not, see <http://www.gnu.org/licenses/>.
  */
 
-public class Grid : Object
+private class Grid : Object
 {
-    private uint[,] _grid;
+    private uint [,] _grid;
 
-    public Grid (int rows, int cols)
+    construct
     {
-        Object (rows: rows, cols: cols);
-
-        _grid = new uint[rows, cols];
+        _grid = new uint [rows, cols];
         clear ();
-        _target_value = 0;
-    }
-
-    public int rows {
-        get; set;
     }
 
-    public int cols {
-        get; set;
+    internal Grid (int rows, int cols)
+    {
+        Object (rows: rows, cols: cols);
     }
 
-    public uint target_value {
-        get; set;
-    }
+    public int rows { internal get; protected construct; }
+    public int cols { internal get; protected construct; }
 
-    public bool target_value_reached {
-        get; set;
-    }
+    internal uint target_value          { internal get; internal set; default = 0; }
+    internal bool target_value_reached  { internal get; internal set; default = false; }
 
-    public Grid clone ()
+    internal Grid clone ()
     {
         Grid grid = new Grid (_rows, _cols);
         grid._grid = _grid;
@@ -56,14 +48,14 @@ public class Grid : Object
         return grid;
     }
 
-    public void clear ()
+    internal void clear ()
     {
-        for (uint i = 0; i < _grid.length[0]; i++)
-            for (uint j = 0; j < _grid.length[1]; j++)
-                _grid[i,j] = 0;
+        for (uint i = 0; i < _grid.length [0]; i++)
+            for (uint j = 0; j < _grid.length [1]; j++)
+                _grid [i, j] = 0;
     }
 
-    public bool new_tile (out Tile tile)
+    internal bool new_tile (out Tile tile)
     {
         GridPosition pos = { 0, 0 };
         uint val;
@@ -88,9 +80,9 @@ public class Grid : Object
         }
     }
 
-    public void move_down (Gee.LinkedList<TileMovement?> to_move,
-                           Gee.LinkedList<TileMovement?> to_hide,
-                           Gee.LinkedList<Tile?> to_show)
+    internal void move_down (Gee.LinkedList<TileMovement?> to_move,
+                             Gee.LinkedList<TileMovement?> to_hide,
+                             Gee.LinkedList<Tile?> to_show)
     {
         to_move.clear ();
         to_hide.clear ();
@@ -169,9 +161,9 @@ public class Grid : Object
         }
     }
 
-    public void move_up (Gee.LinkedList<TileMovement?> to_move,
-                         Gee.LinkedList<TileMovement?> to_hide,
-                         Gee.LinkedList<Tile?> to_show)
+    internal void move_up (Gee.LinkedList<TileMovement?> to_move,
+                           Gee.LinkedList<TileMovement?> to_hide,
+                           Gee.LinkedList<Tile?> to_show)
     {
         to_move.clear ();
         to_hide.clear ();
@@ -250,9 +242,9 @@ public class Grid : Object
         }
     }
 
-    public void move_left (Gee.LinkedList<TileMovement?> to_move,
-                           Gee.LinkedList<TileMovement?> to_hide,
-                           Gee.LinkedList<Tile?> to_show)
+    internal void move_left (Gee.LinkedList<TileMovement?> to_move,
+                             Gee.LinkedList<TileMovement?> to_hide,
+                             Gee.LinkedList<Tile?> to_show)
     {
         to_move.clear ();
         to_hide.clear ();
@@ -331,9 +323,9 @@ public class Grid : Object
         }
     }
 
-    public void move_right (Gee.LinkedList<TileMovement?> to_move,
-                            Gee.LinkedList<TileMovement?> to_hide,
-                            Gee.LinkedList<Tile?> to_show)
+    internal void move_right (Gee.LinkedList<TileMovement?> to_move,
+                              Gee.LinkedList<TileMovement?> to_hide,
+                              Gee.LinkedList<Tile?> to_show)
     {
         to_move.clear ();
         to_hide.clear ();
@@ -412,7 +404,7 @@ public class Grid : Object
         }
     }
 
-    public bool is_finished ()
+    internal bool is_finished ()
     {
         if (!_grid_is_full ())
             return false;
@@ -434,15 +426,15 @@ public class Grid : Object
         return true;
     }
 
-    public new uint get (int row, int col)
+    internal new uint get (int row, int col)
     {
         if ((row >= _rows) || (col >= _cols))
             return 0;
 
-        return _grid[row,col];
+        return _grid [row, col];
     }
 
-    public string save ()
+    internal string save ()
     {
         string ret = "";
 
@@ -454,12 +446,12 @@ public class Grid : Object
         return ret;
     }
 
-    public bool load (string content)
+    internal bool load (string content)
     {
         return _load_from_string (content);
     }
 
-    public string to_string ()
+    internal string to_string ()
     {
         string ret = "\n";
         ret += _convert_to_string ();
@@ -551,24 +543,24 @@ public class Grid : Object
     }
 }
 
-public struct GridPosition
+private struct GridPosition
 {
     public int row;
     public int col;
 
-    public string to_string ()
+    internal string to_string ()
     {
         return @"($row,$col)";
     }
 }
 
-public struct TileMovement
+private struct TileMovement
 {
     public GridPosition from;
     public GridPosition to;
 }
 
-public struct Tile
+private struct Tile
 {
     public GridPosition pos;
     public uint val;
diff --git a/src/view.vala b/src/view.vala
index 3ded3a0..e4ba4a5 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -17,59 +17,47 @@
  * along with GNOME 2048; if not, see <http://www.gnu.org/licenses/>.
  */
 
-public class RoundedRectangle : Object
+private class RoundedRectangle : Object
 {
-    protected Clutter.Actor _actor;
-    protected Clutter.Canvas _canvas;
-    protected Clutter.Color? _color;
-
-    public RoundedRectangle (float x, float y, float width, float height, Clutter.Color? color)
-    {
-        Object ();
-
-        _color = color;
-
-        _canvas = new Clutter.Canvas ();
-        _canvas.set_size ((int)Math.ceilf (width), (int)Math.ceilf (height));
-
-        _actor = new Clutter.Actor ();
-        _actor.set_size (width, height);
-        _actor.set_content (_canvas);
-        _actor.x = x;
-        _actor.y = y;
-        _actor.set_pivot_point (0.5f, 0.5f);
-
-        _canvas.draw.connect (_draw);
-    }
-
-    public Clutter.Actor actor {
-        get { return _actor; }
-    }
+    internal Clutter.Actor  actor   { internal get; private set; default = new Clutter.Actor (); }
+    internal Clutter.Canvas canvas  { internal get; private set; default = new Clutter.Canvas (); }
 
+    private Clutter.Color _color;
     public Clutter.Color color {
-        get { if (_color == null) assert_not_reached (); return (!) _color; }
-        set {
+        get { return _color; }
+        construct {
             _color = value;
-            _canvas.invalidate ();
+            canvas.invalidate ();
         }
     }
 
-    public Clutter.Canvas canvas {
-        get { return _canvas; }
+    internal RoundedRectangle (float x, float y, float width, float height, Clutter.Color color)
+    {
+        Object (color: color);
+
+        canvas.set_size ((int)Math.ceilf (width), (int)Math.ceilf (height));
+
+        actor.set_size (width, height);
+        actor.set_content (canvas);
+        actor.x = x;
+        actor.y = y;
+        actor.set_pivot_point (0.5f, 0.5f);
+
+        canvas.draw.connect (_draw);
     }
 
-    public void resize (float x, float y, float width, float height)
+    internal void resize (float x, float y, float width, float height)
     {
-        _actor.x = x;
-        _actor.y = y;
-        _actor.width = width;
-        _actor.height = height;
+        actor.x = x;
+        actor.y = y;
+        actor.width = width;
+        actor.height = height;
     }
 
-    public void idle_resize ()
+    internal void idle_resize ()
     {
-        if (!_canvas.set_size ((int)Math.ceilf (_actor.width), (int)Math.ceilf (_actor.height)))
-            _canvas.invalidate ();
+        if (!canvas.set_size ((int)Math.ceilf (actor.width), (int)Math.ceilf (actor.height)))
+            canvas.invalidate ();
     }
 
     protected virtual bool _draw (Cairo.Context ctx, int width, int height)
@@ -89,28 +77,21 @@ public class RoundedRectangle : Object
         ctx.arc (radius, radius, radius, 180 * degrees, 270 * degrees);
         ctx.close_path ();
 
-        if (_color != null)
-        {
-            Clutter.cairo_set_source_color (ctx, (!) _color);
-            ctx.fill ();
-        }
+        Clutter.cairo_set_source_color (ctx, (!) _color);
+        ctx.fill ();
 
         return false;
     }
 }
 
-public class TileView : RoundedRectangle
+private class TileView : RoundedRectangle
 {
-    public TileView (float x, float y, float width, float height, uint val)
-    {
-        base (x, y, width, height, null);
-
-        _value = val;
-        _color = _pick_color ();
-    }
+    internal uint tile_value { internal get; private set; default = 2; }
 
-    public uint value {
-        get; set; default = 2;
+    internal TileView (float x, float y, float width, float height, uint val)
+    {
+        base (x, y, width, height, _pick_color (val));
+        tile_value = val;
     }
 
     protected override bool _draw (Cairo.Context ctx, int width, int height)
@@ -127,7 +108,7 @@ public class TileView : RoundedRectangle
         font_desc = Pango.FontDescription.from_string ("Sans Bold %dpx".printf (height / 4));
         layout.set_font_description (font_desc);
 
-        layout.set_text (value.to_string (), -1);
+        layout.set_text (tile_value.to_string (), -1);
 
         layout.get_extents (null, out logical_rect);
         ctx.move_to ((width / 2) - (logical_rect.width / 2 / Pango.SCALE),
@@ -137,57 +118,46 @@ public class TileView : RoundedRectangle
         return false;
     }
 
-    private Clutter.Color _pick_color ()
-    {
-        return ColorPalette.get_instance ().pick_color (_value);
-    }
-}
+    /*\
+    * * color
+    \*/
 
-public class ColorPalette : Object
-{
-    private Gee.HashMap<uint,Clutter.Color?> _palette = new Gee.HashMap<uint,Clutter.Color?> ();
-    private static ColorPalette? _singleton = null;
-
-    construct
+    private static Clutter.Color _pick_color (uint tile_value)
     {
-        _palette.set (2,    Clutter.Color.from_string ("#fce94f")); // Butter 1
-        _palette.set (4,    Clutter.Color.from_string ("#8ae234")); // Chameleon 1
-        _palette.set (8,    Clutter.Color.from_string ("#fcaf3e")); // Orange 1
-        _palette.set (16,   Clutter.Color.from_string ("#729fcf")); // Sky blue 1
-        _palette.set (32,   Clutter.Color.from_string ("#ad7fa8")); // Plum 1
-        _palette.set (64,   Clutter.Color.from_string ("#c17d11")); // Chocolate 2
-        _palette.set (128,  Clutter.Color.from_string ("#ef2929")); // Scarlet red 1
-        _palette.set (256,  Clutter.Color.from_string ("#c4a000")); // Butter 3
-        _palette.set (512,  Clutter.Color.from_string ("#4e9a06")); // Chameleon 3
-        _palette.set (1024, Clutter.Color.from_string ("#ce5c00")); // Orange 3
-        _palette.set (2048, Clutter.Color.from_string ("#204a87")); // Sky blue 3
+        if (tile_value <= 2048)
+            return _pick_palette_color (tile_value);
+        else
+            return _calculate_color (tile_value);
     }
 
-    public static ColorPalette get_instance ()
+    private static Clutter.Color _pick_palette_color (uint tile_value)
     {
-        if (_singleton == null)
-            ColorPalette._singleton = new ColorPalette ();
-
-        return (!) _singleton;
-    }
-
-    public Clutter.Color pick_color (uint val)
-    {
-        if (_palette.has_key (val))
+        switch (tile_value)
         {
-            Clutter.Color? color = _palette.@get (val);
-            if (color == null)
-                assert_not_reached ();
-            return (!) color;
+            case 2:    return Clutter.Color.from_string ("#fce94f"); // Butter 1
+            case 4:    return Clutter.Color.from_string ("#8ae234"); // Chameleon 1
+            case 8:    return Clutter.Color.from_string ("#fcaf3e"); // Orange 1
+            case 16:   return Clutter.Color.from_string ("#729fcf"); // Sky blue 1
+            case 32:   return Clutter.Color.from_string ("#ad7fa8"); // Plum 1
+            case 64:   return Clutter.Color.from_string ("#c17d11"); // Chocolate 2
+            case 128:  return Clutter.Color.from_string ("#ef2929"); // Scarlet red 1
+            case 256:  return Clutter.Color.from_string ("#c4a000"); // Butter 3
+            case 512:  return Clutter.Color.from_string ("#4e9a06"); // Chameleon 3
+            case 1024: return Clutter.Color.from_string ("#ce5c00"); // Orange 3
+            case 2048: return Clutter.Color.from_string ("#204a87"); // Sky blue 3
+            default:   assert_not_reached ();
         }
+    }
 
-        uint norm_val = val / 2048;
-        Clutter.Color? nullable_color = _palette.@get (norm_val);
+    private static Clutter.Color _calculate_color (uint tile_value)
+    {
+        uint norm_val = tile_value / 2048;
+        Clutter.Color? nullable_color = _pick_palette_color (norm_val);
         if (nullable_color == null)
             assert_not_reached ();
         Clutter.Color color = (!) nullable_color;
 
-        uint8 sbits = (uint8) (val % 7);
+        uint8 sbits = (uint8) (tile_value % 7);
         color.red   <<= sbits;
         color.green <<= sbits;
         color.blue  <<= sbits;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]