├── Readme.markdown
├── debug_display.js
├── entity_tween.js
├── entity_utilities.js
├── game_utilities.js
├── html_button.js
└── license.txt
/Readme.markdown:
--------------------------------------------------------------------------------
1 | # ImpactJS Plugins
2 |
3 | Plugins for the Impact JS library
4 |
5 | * Debug Display
6 | * HTML Button
7 | * Sprite tween
8 |
9 | To use, pull down the repository and place the files in a folder called 'empika' inside your plugins directory. e.g. /lib/plugins/empika/*.*
10 |
11 | ## Debug Display
12 | ig.module(
13 | 'game.main'
14 | )
15 | .requires(
16 | 'impact.game',
17 | 'impact.font',
18 | 'plugins.empika.debug_display' // require the debug display plugin
19 | )
20 | .defines(function(){
21 | MyGame = ig.Game.extend({
22 | font: new ig.Font( 'media/04b03.font.png' ),
23 | init: function() {
24 | // create a new DebugDisplay, pass in your font
25 | this.debugDisplay = new DebugDisplay( this.font );
26 | },
27 | update: function() {
28 | this.parent();
29 | },
30 | draw: function() {
31 | this.parent();
32 | var x = ig.system.width/2, y = ig.system.height/2;
33 | // this.debugDisplay.draw(info, display_fps, display_average, average_time, interval_count)
34 | // info, array: this will display each array element on a new line
35 | // display_fps, bool: pass in true or false to either show the FPS or not. defaults to true
36 | // display_average, bool: pass in true or false to either show the average FPS over a period of time or not.
37 | // defaults to false
38 | // average_time, integer: amount of of time between samples. defaults to 10000 (10 seconds)
39 | // interval_count, integer: amount of samples to take over time. defaults to 500
40 | this.debugDisplay.draw(["my", "info", "here"], true, true, 10000, 100);
41 | }
42 | });
43 | ig.main( '#canvas', MyGame, 60, 512, 384, 2 );
44 | });
45 |
46 | ## HTML Buttons
47 | ### main.js
48 | ig.module(
49 | 'game.main'
50 | )
51 | .requires(
52 | 'impact.game',
53 | 'plugins.empika.html_button',
54 | 'game.entities.button'
55 | )
56 | .defines(function(){
57 |
58 | MyGame = ig.Game.extend({
59 | init: function() {
60 | // ig.input.bindTouch( element_name, mousedown_func, mouseup_func, click_func );
61 | // element_name, string: name of an element, required. eg '#button'
62 | // mousedown_func, string: name of a function to call on mouse down, defaults to false
63 | // mouseup_func, string: name of a function to call on mouse up, defaults to false
64 | // click_func, string: name of a function to call on mouse click, defaults to false
65 | ig.input.bindTouch( '#button', 'btndown', 'btnup', 'btnclick' );
66 | },
67 | update: function() {
68 | this.parent();
69 | },
70 | draw: function() {
71 | // Draw all entities and backgroundMaps
72 | this.parent();
73 | // Add your own drawing code here
74 | var x = ig.system.width/2, y = ig.system.height/2;
75 | }
76 | });
77 |
78 | ### game.entities.button.js
79 | ig.module(
80 | 'game.entities.button'
81 | )
82 | .requires(
83 | 'impact.entity'
84 | )
85 | .defines(function() {
86 | EntityButton = ig.Entity.extend({
87 | init: function( x, y, settings ) {
88 | this.parent( x, y, settings );
89 | },
90 | update: function(){
91 | if( ig.input.pressed('btndown')){
92 | console.log("button log down");
93 | }
94 | if( ig.input.pressed('btnup')){
95 | console.log("button log up");
96 | }
97 | if( ig.input.pressed('btnclick')){
98 | console.log("button log click");
99 | }
100 | }
101 | });
102 | });
103 |
104 | ## Entity Tweening
105 | ### game.entities.player.js
106 | ig.module('game.entities.player')
107 | .requires(
108 | 'impact.entity',
109 | 'plugins.empika.entity_tween'
110 | )
111 | .defines(function () {
112 | EntityPlayer = ig.Entity.extend({
113 | init: function(x,y,settings){
114 | this.parent(x,y,settings);
115 | // actions object
116 | // actions, array: An array of action objects
117 | // post_anim, string: The animation to revert to after the animation has finished
118 |
119 | // action objects
120 | // x, integer: Translation along the x axis
121 | // y, integer: Translation along the y axis
122 | // duration, float: The time that the tween will take
123 | // anim, string: The name of the animation to use during the tween
124 | // easing, Tween object: The ease factore applied to the tween. See the source for a full list.
125 | // Many thanks to https://github.com/nefD and https://github.com/xdissent for these
126 | // callback, function: a function to call once the tween action has completed
127 | // callback_args, array: an array of arguments to pass in to the callback function
128 | actions = {actions: [
129 | { x: 15, y: 10, duration: 2, anim: 'walk', easing: ig.Tween.Easing.Cubic.EaseIn },
130 | { x: 0,
131 | y: 0,
132 | duration: 5,
133 | anim: 'idle',
134 | callback: function(){
135 | console.log(arguments[0], arguments[1]);
136 | },
137 | callback_args: ["argument 1", "argument 2"],
138 | },
139 | { x: -15, y: -10, duration: 2, anim: 'idle', easing: ig.Tween.Easing.Cubic.EaseOut }
140 | ], post_anim: 'idle'};
141 |
142 | // Add the actions to the entity
143 | this.initialize_animations(actions);
144 |
145 | // Kick off the Tweening
146 | this.anim_start();
147 | }
148 | });
149 | });
150 |
151 | ### index.html
152 |
153 |
154 |
155 | Impact Game
156 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
--------------------------------------------------------------------------------
/debug_display.js:
--------------------------------------------------------------------------------
1 | /*
2 | Plugin Name: Debug Display
3 | Plugin URI: https://github.com/empika/ImpactJS-Plugins
4 | Description: Show FPS as well as arbitrary debug information in an ImpactJS game
5 | Version: 0.4
6 | Revision Date: 28-03-2013
7 | Requires: ImpactJS
8 | Author: Edward Parris
9 | Author URI: http://www.nixonmcinnes.co.uk/people/edward/
10 | Changelog
11 | ---------
12 | 0.4: Can set position where font will be drawn to.
13 | 0.3: Namespace the plugin.
14 | 0.2: Add average FPS over longer time periods.
15 | 0.1: Initial release.
16 | */
17 |
18 | ig.module(
19 | 'plugins.empika.debug_display'
20 | )
21 | .requires(
22 | 'impact.game'
23 | )
24 | .defines(function(){
25 | DebugDisplay = ig.Class.extend({
26 |
27 | pos: { x: 2, y: 2 },
28 | framerateNow: (new Date()).getTime(),
29 | frames: [],
30 | average: [],
31 | frameCounter: 0,
32 | info: [],
33 | avg_fps: 0,
34 |
35 | init: function(font)
36 | {
37 | this.font = font;
38 | },
39 |
40 | draw: function(info, display_fps, display_average, average_time, interval_count){
41 | var info = typeof(info) != 'undefined' ? info : [];
42 | var display_fps = typeof(display_fps) != 'undefined' ? display_fps : true;
43 | var display_average = typeof(display_average) != 'undefined' ? display_average : false;
44 | var average_time = typeof(average_time) != 'undefined' ? average_time : 10000; // 10 seconds
45 | var interval_count = typeof(interval_count) != 'undefined' ? interval_count : 500; // 10 seconds
46 |
47 | var offset = 0;
48 | var fps = 0;
49 | if(display_fps){
50 | fps = this.calculateFrameRate();
51 | this.font.draw( 'FPS: ' + fps, this.pos.x, this.pos.y );
52 | offset = this.font.height;
53 | }
54 |
55 | if(display_fps && display_average){
56 | var min = this.average.min() !== Infinity ? this.average.min() : 0;
57 | var max = this.average.max() !== Infinity ? this.average.max() : 0;
58 | if((new Date()).getTime() % average_time < 100){
59 | this.avg_fps = this.calculateAverage(fps, interval_count);
60 | }
61 | this.font.draw('Avg FPS: ' + this.avg_fps + ' Min: ' + min + ' Max: ' + max, this.pos.x, offset + this.pos.y);
62 | offset = offset + offset;
63 | }
64 | for(var x = 0; x < info.length; x = x + 1){
65 | this.font.draw( info[x], this.pos.x, offset + (this.font.height * x) + this.pos.y);
66 | }
67 | },
68 |
69 | calculateFrameRate: function(){
70 | var now = (new Date()).getTime();
71 | var delta = now - this.framerateNow;
72 | var avg = this.frames.sum();
73 | var av_length = this.frames.length;
74 | if(av_length > 11){
75 | this.frames.shift();
76 | }
77 | this.frames.push(1000/delta);
78 | this.framerateNow = now;
79 | return Math.floor(avg / av_length);
80 | },
81 |
82 | calculateAverage: function(current, interval_count){
83 | var av_length = this.average.length;
84 | if(av_length > interval_count){
85 | this.average.shift();
86 | }
87 | this.average.push(current);
88 | return Math.floor(this.average.sum() / av_length);
89 | }
90 |
91 |
92 | });
93 | });
94 |
95 | // From http://snippets.dzone.com/posts/show/769
96 | Array.prototype.sum = function(){
97 | for(var i=0,sum=0;i 0 ? true : false;
97 |
98 | if(positive){
99 | new_pos = current > initial + pos_delta ? 0 : new_pos;
100 | }
101 | else{
102 | new_pos = current < initial + pos_delta ? 0 : new_pos;
103 | }
104 |
105 | return new_pos;
106 | },
107 |
108 | anim_update_entity: function(){
109 | if ( this.anim_timer.delta() < 0 ){
110 | //this.anim_calculate_pos_delta();
111 | var x = this.anim_calculate_pos_delta(
112 | this.anim_current_pos.x,
113 | this.pos.x,
114 | this.anim_current_action.x,
115 | this.anim_current_action.duration,
116 | this.anim_timer.delta(),
117 | this.anim_current_action.easing
118 | );
119 | var y = this.anim_calculate_pos_delta(
120 | this.anim_current_pos.y,
121 | this.pos.y,
122 | this.anim_current_action.y,
123 | this.anim_current_action.duration,
124 | this.anim_timer.delta(),
125 | this.anim_current_action.easing
126 | );
127 |
128 | this.pos.x = x;
129 | this.pos.y = y;
130 | }
131 | else{
132 | this.anim_complete();
133 | }
134 | },
135 |
136 | update: function(){
137 | this.parent();
138 | if(this.anim_started === true){
139 | this.anim_update_entity();
140 | }
141 | }
142 |
143 | });
144 |
145 | // Tween easing from https://github.com/xdissent/impact-tween
146 | ig.Tween = {};
147 | ig.Tween.Easing = { Linear: {}, Quadratic: {}, Cubic: {}, Quartic: {}, Quintic: {}, Sinusoidal: {}, Exponential: {}, Circular: {}, Elastic: {}, Back: {}, Bounce: {} };
148 |
149 | ig.Tween.Easing.Linear.EaseNone = function ( k ) {
150 | return k;
151 | };
152 |
153 | ig.Tween.Easing.Quadratic.EaseIn = function ( k ) {
154 | return k * k;
155 | };
156 |
157 | ig.Tween.Easing.Quadratic.EaseOut = function ( k ) {
158 | return - k * ( k - 2 );
159 | };
160 |
161 | ig.Tween.Easing.Quadratic.EaseInOut = function ( k ) {
162 | if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
163 | return - 0.5 * ( --k * ( k - 2 ) - 1 );
164 | };
165 |
166 | ig.Tween.Easing.Cubic.EaseIn = function ( k ) {
167 | return k * k * k;
168 | };
169 |
170 | ig.Tween.Easing.Cubic.EaseOut = function ( k ) {
171 | return --k * k * k + 1;
172 | };
173 |
174 | ig.Tween.Easing.Cubic.EaseInOut = function ( k ) {
175 | if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k;
176 | return 0.5 * ( ( k -= 2 ) * k * k + 2 );
177 | };
178 |
179 | ig.Tween.Easing.Quartic.EaseIn = function ( k ) {
180 | return k * k * k * k;
181 | };
182 |
183 | ig.Tween.Easing.Quartic.EaseOut = function ( k ) {
184 | return - ( --k * k * k * k - 1 );
185 | }
186 |
187 | ig.Tween.Easing.Quartic.EaseInOut = function ( k ) {
188 | if ( ( k *= 2 ) < 1) return 0.5 * k * k * k * k;
189 | return - 0.5 * ( ( k -= 2 ) * k * k * k - 2 );
190 | };
191 |
192 | ig.Tween.Easing.Quintic.EaseIn = function ( k ) {
193 | return k * k * k * k * k;
194 | };
195 |
196 | ig.Tween.Easing.Quintic.EaseOut = function ( k ) {
197 | return ( k = k - 1 ) * k * k * k * k + 1;
198 | };
199 |
200 | ig.Tween.Easing.Quintic.EaseInOut = function ( k ) {
201 | if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k * k * k;
202 | return 0.5 * ( ( k -= 2 ) * k * k * k * k + 2 );
203 | };
204 |
205 | ig.Tween.Easing.Sinusoidal.EaseIn = function ( k ) {
206 | return - Math.cos( k * Math.PI / 2 ) + 1;
207 | };
208 |
209 | ig.Tween.Easing.Sinusoidal.EaseOut = function ( k ) {
210 | return Math.sin( k * Math.PI / 2 );
211 | };
212 |
213 | ig.Tween.Easing.Sinusoidal.EaseInOut = function ( k ) {
214 | return - 0.5 * ( Math.cos( Math.PI * k ) - 1 );
215 | };
216 |
217 | ig.Tween.Easing.Exponential.EaseIn = function ( k ) {
218 | return k == 0 ? 0 : Math.pow( 2, 10 * ( k - 1 ) );
219 | };
220 |
221 | ig.Tween.Easing.Exponential.EaseOut = function ( k ) {
222 | return k == 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1;
223 | };
224 |
225 | ig.Tween.Easing.Exponential.EaseInOut = function ( k ) {
226 | if ( k == 0 ) return 0;
227 | if ( k == 1 ) return 1;
228 | if ( ( k *= 2 ) < 1 ) return 0.5 * Math.pow( 2, 10 * ( k - 1 ) );
229 | return 0.5 * ( - Math.pow( 2, - 10 * ( k - 1 ) ) + 2 );
230 | };
231 |
232 | ig.Tween.Easing.Circular.EaseIn = function ( k ) {
233 | return - ( Math.sqrt( 1 - k * k ) - 1);
234 | };
235 |
236 | ig.Tween.Easing.Circular.EaseOut = function ( k ) {
237 | return Math.sqrt( 1 - --k * k );
238 | };
239 |
240 | ig.Tween.Easing.Circular.EaseInOut = function ( k ) {
241 | if ( ( k /= 0.5 ) < 1) return - 0.5 * ( Math.sqrt( 1 - k * k) - 1);
242 | return 0.5 * ( Math.sqrt( 1 - ( k -= 2) * k) + 1);
243 | };
244 |
245 | ig.Tween.Easing.Elastic.EaseIn = function( k ) {
246 | var s, a = 0.1, p = 0.4;
247 | if ( k == 0 ) return 0; if ( k == 1 ) return 1; if ( !p ) p = 0.3;
248 | if ( !a || a < 1 ) { a = 1; s = p / 4; }
249 | else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
250 | return - ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
251 | };
252 |
253 | ig.Tween.Easing.Elastic.EaseOut = function( k ) {
254 | var s, a = 0.1, p = 0.4;
255 | if ( k == 0 ) return 0; if ( k == 1 ) return 1; if ( !p ) p = 0.3;
256 | if ( !a || a < 1 ) { a = 1; s = p / 4; }
257 | else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
258 | return ( a * Math.pow( 2, - 10 * k) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) + 1 );
259 | };
260 |
261 | ig.Tween.Easing.Elastic.EaseInOut = function( k ) {
262 | var s, a = 0.1, p = 0.4;
263 | if ( k == 0 ) return 0; if ( k == 1 ) return 1; if ( !p ) p = 0.3;
264 | if ( !a || a < 1 ) { a = 1; s = p / 4; }
265 | else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a );
266 | if ( ( k *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
267 | return a * Math.pow( 2, -10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
268 | };
269 |
270 | ig.Tween.Easing.Back.EaseIn = function( k ) {
271 | var s = 1.70158;
272 | return k * k * ( ( s + 1 ) * k - s );
273 | };
274 |
275 | ig.Tween.Easing.Back.EaseOut = function( k ) {
276 | var s = 1.70158;
277 | return ( k = k - 1 ) * k * ( ( s + 1 ) * k + s ) + 1;
278 | };
279 |
280 | ig.Tween.Easing.Back.EaseInOut = function( k ) {
281 | var s = 1.70158 * 1.525;
282 | if ( ( k *= 2 ) < 1 ) return 0.5 * ( k * k * ( ( s + 1 ) * k - s ) );
283 | return 0.5 * ( ( k -= 2 ) * k * ( ( s + 1 ) * k + s ) + 2 );
284 | };
285 |
286 | ig.Tween.Easing.Bounce.EaseIn = function( k ) {
287 | return 1 - ig.Tween.Easing.Bounce.EaseOut( 1 - k );
288 | };
289 |
290 | ig.Tween.Easing.Bounce.EaseOut = function( k ) {
291 | if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
292 | return 7.5625 * k * k;
293 | } else if ( k < ( 2 / 2.75 ) ) {
294 | return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
295 | } else if ( k < ( 2.5 / 2.75 ) ) {
296 | return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
297 | } else {
298 | return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
299 | }
300 | };
301 |
302 | ig.Tween.Easing.Bounce.EaseInOut = function( k ) {
303 | if ( k < 0.5 ) return ig.Tween.Easing.Bounce.EaseIn( k * 2 ) * 0.5;
304 | return ig.Tween.Easing.Bounce.EaseOut( k * 2 - 1 ) * 0.5 + 0.5;
305 | };
306 | });
--------------------------------------------------------------------------------
/entity_utilities.js:
--------------------------------------------------------------------------------
1 | /*
2 | Plugin Name: HTML Button
3 | Plugin URI: https://github.com/empika/ImpactJS-Plugins
4 | Description: Bind HTML buttons to ordinary mouse clicks (mouseup, mousedown, click)
5 | Version: 0.2
6 | Revision Date: 20-05-2012
7 | Requires: ImpactJS
8 | Author: Edward Parris
9 | Author URI: http://www.nixonmcinnes.co.uk/people/edward/
10 | Changelog
11 | ---------
12 | 0.2: Namespace the plugin.
13 | 0.1: Initial release.
14 | */
15 |
16 | ig.module(
17 | 'plugins.empika.entity_utilities'
18 | )
19 | .requires(
20 | 'impact.entity',
21 | 'impact.input'
22 | )
23 | .defines(function() {
24 | ig.Entity.inject({
25 |
26 | pos_delta: {x: 0, y: 0},
27 | mod_x: {x:0, y:0},
28 | is_clicked: false,
29 |
30 | isMouseInside: function (){
31 | var entities = ig.game.entitiesUnderMouse();
32 | if(this == entities[entities.length -1]){
33 | return true;
34 | }
35 | return false;
36 | },
37 |
38 | dragAndDrop: function (snap, snap_after_drop, snap_grid_size_x, snap_grid_size_y){
39 | var snap = typeof(snap) !== 'undefined' ? snap : false;
40 | var snap_after_drop = typeof(snap_after_drop) !== 'undefined' ? snap_after_drop : false;
41 | var snap_grid_size_x = typeof(snap_grid_size_x) !== 'undefined' ? snap_grid_size_x : this.size.x;
42 | var snap_grid_size_y = typeof(snap_grid_size_y) !== 'undefined' ? snap_grid_size_y : this.size.y;
43 |
44 | var mouse_x = ig.input.mouse.x;
45 | var mouse_y = ig.input.mouse.y;
46 | var mod_x = 0; //mouse_x % snap_grid_size_x;
47 | var mod_y = 0; //mouse_y % snap_grid_size_y;
48 | var is_clicked = this.is_clicked;
49 | if( is_clicked && ig.input.state('lbtn') ) {
50 | if( !snap ){
51 | this.pos.x = mouse_x - this.pos_delta.x;
52 | this.pos.y = mouse_y - this.pos_delta.y;
53 | }
54 | else if( snap && !snap_after_drop ){
55 | mod_x = mouse_x % snap_grid_size_x;
56 | mod_y = mouse_y % snap_grid_size_y;
57 | this.pos.x = mod_x == 0 ? mouse_x : mouse_x - mod_x;
58 | this.pos.y = mod_y == 0 ? mouse_y : mouse_y - mod_y;
59 | }
60 | }
61 | else if( is_clicked && snap_after_drop ){
62 | mod_x = mouse_x % snap_grid_size_x;
63 | mod_y = mouse_y % snap_grid_size_y;
64 | this.pos.x = mod_x == 0 ? mouse_x : mouse_x - mod_x;
65 | this.pos.y = mod_y == 0 ? mouse_y : mouse_y - mod_y;
66 | this.is_clicked = false;
67 | }
68 | else if( ig.input.pressed('lbtn') && this.isMouseInside() ){
69 | this.pos_delta.x = mouse_x - this.pos.x;
70 | this.pos_delta.y = mouse_y - this.pos.y;
71 | this.is_clicked = true;
72 | }
73 | else{
74 | this.is_clicked = false;
75 | }
76 | },
77 |
78 | touchesAnything: function( ) {
79 | return !(
80 | this.pos.x > other.pos.x + other.size.x ||
81 | this.pos.x + this.size.x < other.pos.x ||
82 | this.pos.y > other.pos.y + other.size.y ||
83 | this.pos.y + this.size.y < other.pos.y
84 | );
85 | },
86 |
87 | });
88 | });
--------------------------------------------------------------------------------
/game_utilities.js:
--------------------------------------------------------------------------------
1 | /*
2 | Plugin Name: Game utilities
3 | Plugin URI: https://github.com/empika/ImpactJS-Plugins
4 | Description: Some general helper methods
5 | Version: 0.2
6 | Revision Date: 20-05-2012
7 | Requires: ImpactJS
8 | Author: Edward Parris
9 | Author URI: http://www.nixonmcinnes.co.uk/people/edward/
10 | Changelog
11 | ---------
12 | 0.2: Namespace the plugin.
13 | 0.1: Initial release.
14 | */
15 | ig.module(
16 | 'plugins.empika.game_utilities'
17 | )
18 | .requires(
19 | 'impact.game'
20 | )
21 | .defines(function() {
22 | ig.Game.inject({
23 |
24 | // Return an array of entities that are under the mouse
25 | entitiesUnderMouse: function(){
26 | var mouse_x = ig.input.mouse.x;
27 | var mouse_y = ig.input.mouse.y;
28 |
29 | var entities = this.entities;
30 | var under_mouse = [];
31 | for(var x = 0; x < entities.length; x = x+1){
32 | var entity = entities[x];
33 | var pos_x = entity.pos.x;
34 | var pos_y = entity.pos.y;
35 | if( pos_x <= mouse_x && mouse_x <= pos_x + entity.size.x &&
36 | pos_y <= mouse_y && mouse_y <= pos_y + entity.size.y ){
37 | under_mouse.push(entity);
38 | }
39 | }
40 | return under_mouse;
41 | }
42 | });
43 | });
--------------------------------------------------------------------------------
/html_button.js:
--------------------------------------------------------------------------------
1 | /*
2 | Plugin Name: HTML Button
3 | Plugin URI: https://github.com/empika/ImpactJS-Plugins
4 | Description: Bind HTML buttons to ordinary mouse clicks (mouseup, mousedown, click)
5 | Version: 0.2
6 | Revision Date: 20-05-2012
7 | Requires: ImpactJS
8 | Author: Edward Parris
9 | Author URI: http://www.nixonmcinnes.co.uk/people/edward/
10 | Changelog
11 | ---------
12 | 0.2: Namespace the plugin.
13 | 0.1: Initial release.
14 | */
15 |
16 | ig.module(
17 | 'plugins.empika.html_button'
18 | )
19 | .requires(
20 | 'impact.input'
21 | )
22 | .defines(function() {
23 | ig.Input.inject({
24 |
25 | // Overwrite bindTouch so we can bind click if touch is not available
26 | bindTouch: function( selector, action_down, action_up, click ) {
27 | var action_down = typeof(action_down) != 'undefined' ? action_down : false;
28 | var action_up = typeof(action_up) != 'undefined' ? action_up : false;
29 | var click = typeof(click) != 'undefined' ? click : false;
30 |
31 | var element = ig.$( selector );
32 |
33 | var that = this;
34 | if('ontouchstart' in element && 'ontouchend' in element){
35 | element.addEventListener('touchstart', function(ev) {
36 | that.touchStart( ev, action_down );
37 | }, false);
38 |
39 | element.addEventListener('touchend', function(ev) {
40 | that.touchEnd( ev, action_down );
41 | }, false);
42 | }
43 | else{
44 | if( action_down ){
45 | element.addEventListener('mousedown', function(ev) {
46 | that.touchStart( ev, action_down );
47 | }, false);
48 | }
49 |
50 | if( action_up ){
51 | element.addEventListener('mouseup', function(ev) {
52 | that.touchStart( ev, action_up );
53 | }, false);
54 | }
55 |
56 | if( click ){
57 | element.addEventListener('click', function(ev) {
58 | that.touchStart( ev, click );
59 | }, false);
60 | }
61 | }
62 | }
63 |
64 | });
65 | });
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) 2011 by Edward Parris.
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
--------------------------------------------------------------------------------