[gnome-games] [lightsoff] Code cleanup; put some functions on prototype for performance



commit aadfde704d30b2f63257a165ac06f41e085722fa
Author: Tim Horton <hortont424 gmail com>
Date:   Sat Aug 1 21:23:09 2009 -0400

    [lightsoff] Code cleanup; put some functions on prototype for performance

 lightsoff/src/About.js |    3 -
 lightsoff/src/Board.js |  146 ++++++++++++++++++++++-------------------------
 lightsoff/src/Game.js  |   12 ++--
 lightsoff/src/Light.js |   15 +++--
 lightsoff/src/main.js  |   20 ++-----
 5 files changed, 87 insertions(+), 109 deletions(-)
---
diff --git a/lightsoff/src/About.js b/lightsoff/src/About.js
index 330a200..1041474 100644
--- a/lightsoff/src/About.js
+++ b/lightsoff/src/About.js
@@ -22,9 +22,6 @@ function show_about_dialog()
 	about_dialog.set_authors(["Tim Horton"]);
 	about_dialog.set_artists(["Tim Horton", "Ulisse Perusin"]);
 
-	// TODO: some form of wrapper so we can use gtk_show_about_dialog instead
-	// of faking all of its window-management-related stuff
-
 	about_dialog.set_transient_for(main.window);
 	about_dialog.run();
 	
diff --git a/lightsoff/src/Board.js b/lightsoff/src/Board.js
index 5363969..74e4fd3 100644
--- a/lightsoff/src/Board.js
+++ b/lightsoff/src/Board.js
@@ -3,16 +3,79 @@ GLib = imports.gi.GLib;
 Clutter = imports.gi.Clutter;
 Light = imports.Light;
 
-var tiles = 5; // fix this
+var tiles = 5;
+
+// All lights need to be shifted down and right by half a light,
+// as lights have center gravity.
+function position_for_light(x, y)
+{
+	var p_l = {x: (x + 0.5) * Settings.theme.light[0].width,
+	           y: (y + 0.5) * Settings.theme.light[0].height};
+	
+	return p_l;
+}
 
 BoardView = new GType({
 	parent: Clutter.Group.type,
 	name: "BoardView",
 	signals: [{name: "game_won"}],
+	class_init: function(klass, prototype)
+	{
+		function fade(out)
+		{
+			return function(timeline)
+			{
+				this.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_SINE, 
+			                               timeline,
+				{
+					opacity: out ? 0 : 255
+				});
+			}
+		}
+	
+		prototype.fade_in = fade(false);
+		prototype.fade_out = fade(true);
+		
+		function slide(out)
+		{
+			return function(direction, sign, timeline)
+			{
+				this.x = out ? 0 : (-sign) * direction * this.width;
+				this.y = out ? 0 : (-sign) * !direction * this.height;
+				
+				this.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_BOUNCE, 
+					                       timeline,
+				{
+					x: out ? sign * direction * this.width : 0,
+					y: out ? sign * !direction * this.height : 0
+				});
+			}
+		}
+		
+		prototype.slide_in = slide(false);
+		prototype.slide_out = slide(true);
+		
+		function swap(out)
+		{
+			return function(direction, timeline)
+			{
+				this.animate_with_timeline(Clutter.AnimationMode.EASE_IN_SINE, 
+					                       timeline,
+				{
+					depth: out ? (250 * direction) : 0,
+					x: 0,
+					y: 0,
+					opacity: out ? 0 : 255
+				});
+			}
+		}
+		
+		prototype.swap_in = swap(false);
+		prototype.swap_out = swap(true);
+	},
 	init: function(self)
 	{
 		// Private
-		var self = this;
 		var playable = true;
 		var lights = [];
 		var loading_level = false;
@@ -28,7 +91,7 @@ BoardView = new GType({
 				for(var y = 0; y < tiles; y++)
 				{
 					var l = new Light.LightView();
-					var loc = self.position_for_light(x, y);
+					var loc = position_for_light(x, y);
 					l.set_position(loc.x, loc.y);
 					l.signal.button_release_event.connect(light_clicked, {"x":x, "y":y});
 					
@@ -42,7 +105,6 @@ BoardView = new GType({
 		
 		// Check if the game was won; if so, emit the game_won signal
 		// in order to notify the Game controller.
-		
 		var check_won = function()
 		{
 			if(cleared())
@@ -100,14 +162,6 @@ BoardView = new GType({
 				timeline.start();
 		}
 		
-		this.position_for_light = function(x, y)
-		{
-			var p_l = {x: (x + 0.5) * Settings.theme.light[0].width,
-			           y: (y + 0.5) * Settings.theme.light[0].height};
-			
-			return p_l;
-		}
-		
 		// Pseudorandomly generates and sets the state of each light based on
 		// a level number; hopefully this is stable between machines, but that
 		// depends on GLib's PRNG stability. Also, provides some semblance of 
@@ -124,7 +178,8 @@ BoardView = new GType({
 			
 			do
 			{
-				// log(level^2) gives a reasonable progression of difficulty
+				// Simulate log(level^2) clicks; this seems to give
+				// a reasonable progression of difficulty
 				var count = Math.floor(Math.log(level * level) + 1);
 				var sym = Math.floor(3 * GLib.random_double());
 
@@ -157,71 +212,6 @@ BoardView = new GType({
 			playable = p;
 		}
 		
-		this.fade_out = function(timeline)
-		{
-			self.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_SINE, 
-			                           timeline,
-			{
-				opacity: 0
-			});
-		}
-		
-		this.fade_in = function(timeline)
-		{
-			self.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_SINE, 
-			                           timeline,
-			{
-				opacity: 255
-			});
-		}
-		
-		this.animate_out = function(direction, sign, timeline)
-		{
-			self.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_BOUNCE, 
-			                           timeline,
-			{
-				x: sign * direction * self.width,
-				y: sign * !direction * self.height
-			});
-		}
-		
-		this.animate_in = function(direction, sign, timeline)
-		{
-			self.x = (-sign) * direction * self.width;
-			self.y = (-sign) * !direction * self.height;
-			
-			self.animate_with_timeline(Clutter.AnimationMode.EASE_OUT_BOUNCE, 
-			                           timeline,
-			{
-				x: 0,
-				y: 0
-			});
-		}
-		
-		this.swap_in = function(direction, timeline)
-		{
-			self.animate_with_timeline(Clutter.AnimationMode.EASE_IN_SINE, 
-			                           timeline,
-			{
-				depth: 0,
-				x: 0,
-				y: 0,
-				opacity: 255
-			});
-		}
-		
-		this.swap_out = function(direction, timeline)
-		{
-			self.animate_with_timeline(Clutter.AnimationMode.EASE_IN_SINE, 
-			                           timeline,
-			{
-				depth: 250 * direction,
-				x: 0,
-				y: 0,
-				opacity: 0
-			});
-		}
-		
 		// Implementation
 		
 		create_lights();
diff --git a/lightsoff/src/Game.js b/lightsoff/src/Game.js
index 7f470f5..1b89814 100644
--- a/lightsoff/src/Game.js
+++ b/lightsoff/src/Game.js
@@ -78,8 +78,8 @@ GameView = new GType({
 			create_next_board();
 			new_board_view.show();
 			
-			new_board_view.animate_in(direction, sign, timeline);
-			board_view.animate_out(direction, sign, timeline);
+			new_board_view.slide_in(direction, sign, timeline);
+			board_view.slide_out(direction, sign, timeline);
 			timeline.signal.completed.connect(board_transition_complete);
 				
 			return false;
@@ -215,12 +215,12 @@ GameView = new GType({
 			}
 			
 			if(event.key.keyval != Clutter.Down &&
-				event.key.keyval != Clutter.Up &&
-				event.key.keyval != Clutter.Left &&
-				event.key.keyval != Clutter.Right)
+			   event.key.keyval != Clutter.Up &&
+			   event.key.keyval != Clutter.Left &&
+			   event.key.keyval != Clutter.Right)
 				return false;
 		
-			var loc = board_view.position_for_light(keycursor.x, keycursor.y);
+			var loc = Board.position_for_light(keycursor.x, keycursor.y);
 			
 			if(keycursor.ready)
 			{
diff --git a/lightsoff/src/Light.js b/lightsoff/src/Light.js
index 8601426..9e61c5a 100644
--- a/lightsoff/src/Light.js
+++ b/lightsoff/src/Light.js
@@ -4,10 +4,16 @@ Clutter = imports.gi.Clutter;
 LightView = new GType({
 	parent: Clutter.Group.type,
 	name: "LightView",
-	init: function()
+	class_init: function(klass, prototype)
+	{
+		prototype.toggle = function(timeline)
+		{
+			this.set_state(!this.get_state(), timeline);
+		};
+	},
+	init: function(self)
 	{
 		// Private
-		var self = this; // Robb promises something better
 		var on = new Clutter.Clone({source: Settings.theme.light[1],
 		                            anchor_gravity: Clutter.Gravity.CENTER});
 		var off = new Clutter.Clone({source: Settings.theme.light[0],
@@ -55,11 +61,6 @@ LightView = new GType({
 			return state;
 		}
 		
-		this.toggle = function(timeline)
-		{
-			self.set_state(!self.get_state(), timeline);
-		}
-		
 		// Implementation
 		
 		this.add_actor(on);
diff --git a/lightsoff/src/main.js b/lightsoff/src/main.js
index 5b76bdf..4f44560 100755
--- a/lightsoff/src/main.js
+++ b/lightsoff/src/main.js
@@ -19,14 +19,8 @@ About = imports.About;
 themes = imports.ThemeLoader;
 
 handlers = {
-	show_settings: function(selector, ud)
-	{
-		Settings.show_settings();
-	},
-	show_about: function(selector, ud)
-	{
-		About.show_about_dialog();
-	},
+	show_settings: Settings.show_settings,
+	show_about: About.show_about_dialog,
 	show_help: function(selector, ud)
 	{
 		GnomeGamesSupport.help_display(window, "lightsoff", null);
@@ -35,10 +29,7 @@ handlers = {
 	{
 		game.reset_game();
 	},
-	quit: function(selector, ud)
-	{
-		Gtk.main_quit();
-	}
+	quit: Gtk.main_quit
 };
 
 b = new Gtk.Builder();
@@ -54,9 +45,6 @@ var stage = clutter_embed.get_stage();
 stage.color = {alpha:255};
 stage.set_use_fog(false);
 
-// TODO: determine size of window before we show it
-// NOTE: show the window before the stage, and the stage before any children
-window.show_all();
 stage.show_all();
 
 themes.load_theme(stage, Settings.theme);
@@ -68,4 +56,6 @@ clutter_embed.set_size_request(stage.width, stage.height);
 
 stage.signal.key_release_event.connect(game.update_keyboard_selection);
 
+window.show_all();
+
 Gtk.main();



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