Browse Source

v0.1.4

master
frtk 5 years ago
parent
commit
9677666b48
16 changed files with 254 additions and 285 deletions
  1. +1
    -1
      client/index.html
  2. +16
    -3
      client/lib/core/page_handler.js
  3. +12
    -19
      client/lib/keyboard/event_handler.js
  4. +7
    -9
      client/lib/npg_client.js
  5. +0
    -13
      client/lib/pages/login_page.js
  6. +42
    -24
      client/lib/ui/objects/ui_cursor.js
  7. +3
    -1
      client/lib/ui/objects/ui_input_text.js
  8. +0
    -35
      client/lib/ui/objects/ui_line.js
  9. +66
    -34
      client/lib/ui/objects/ui_object.js
  10. +14
    -34
      client/lib/ui/objects/ui_rect.js
  11. +0
    -45
      client/lib/ui/objects/ui_shape.js
  12. +75
    -8
      client/lib/utils/client_utils.js
  13. +0
    -57
      client/lib/utils/utils.js
  14. +1
    -1
      package.json
  15. +1
    -1
      server/npg_server_config.js
  16. +16
    -0
      version.md

+ 1
- 1
client/index.html View File

@ -16,7 +16,7 @@
<script type="text/javascript" src="./lib/core/page_base.js"></script>
<script type="text/javascript" src="./lib/core/page_handler.js"></script>
<script type="text/javascript" src="./lib/ui/ui.js"></script>
<script type="text/javascript" src="./lib/ui/objects/ui_shape.js"></script>
<script type="text/javascript" src="./lib/ui/objects/ui_object.js"></script>
<script type="text/javascript" src="./lib/ui/objects/ui_style.js"></script>
<script type="text/javascript" src="./lib/ui/objects/ui_point2d.js"></script>
<script type="text/javascript" src="./lib/ui/objects/ui_rect.js"></script>


+ 16
- 3
client/lib/core/page_handler.js View File

@ -24,15 +24,28 @@ NPGClient.PageHandler = {
//
getCurrPageUIElemByName: function(name) {
/*
var self = this;
if (self.pages.length > 0) {
for (var i = 0; i < self.pages.length; i++) {
if (name == self.pages[i].name) return self.pages[i];
if (self.pages[i].name == self.currPage) {
return self.pages[i].getUIElemByName(name);
}
}
}
return undefined;
},
// @need rework
getUIElemFromPage: function(elem, page) {
var self = this;
if (self.pages.length > 0) {
for (var i = 0; i < self.pages.length; i++) {
if (page == self.pages[i].name) {
// return self.pages[i];
}
}
}
return undefined;
*/
},


+ 12
- 19
client/lib/keyboard/event_handler.js View File

@ -55,11 +55,9 @@ NPGClient.evtHandler = {
*/
//
userLogin : function (evt) {
//
var self = this;
var oldNameWidth, newNameWidth;
var size = NPGClient.PageHandler.getPageByName('login').ctx.measureText(NPGClient.userName).width;
oldNameWidth = 2.1*size; // corr factor 2.1
//
switch (evt.keyCode) {
case NPGClient.KEYS.ENTER:
if (NPGClient.SocketIO.isConnected) {
@ -67,32 +65,27 @@ NPGClient.evtHandler = {
NPGClient.SocketIO.sendMsg('registration', NPGClient.userName);
}
} else {
//self.serverExit();
NPGClient.Utils.resetName();
NPGClient.PageHandler.getCurrPageUIElemByName('login_cursor').reset();
}
break;
case NPGClient.KEYS.BACKSPACE:
//--- remove a character
evt.preventDefault();
NPGClient.Utils.removeChar();
newNameWidth = NPGClient.PageHandler.getPageByName('login').ctx.measureText(NPGClient.userName).width
// newNameWidth = 2.1*self.ctx.measureText(self.tmpName).width; // corr factor 2.1
// self.cursor.shiftX(newNameWidth - oldNameWidth);
if (!NPGClient.Utils.nameEmpty()) {
NPGClient.Utils.removeChar();
NPGClient.PageHandler.getCurrPageUIElemByName('login_cursor').translateX(-15);
}
break;
default:
//console.log(evt.keyCode + ' ' + self.isValidKey(evt.keyCode));
//--- add character
if (self.isValidKey(evt.keyCode)) {
//self.cursor.stopBlink;
if (self.isValidKey(evt.keyCode) && !NPGClient.Utils.maxNameSize()) {
NPGClient.Utils.addChar(evt.keyCode);
//newNameWidth = 2.1*self.ctx.measureText(self.tmpName).width; // corr factor 2.1
//if (self.validName()) {
// self.cursor.shiftX(newNameWidth - oldNameWidth);
//}
//self.cursor.startBlink;
newNameW = NPGClient.ui.ctx.measureText(NPGClient.userName).width;
NPGClient.PageHandler.getCurrPageUIElemByName('login_cursor').translateX(15);
}
break;
}
},
}
}

+ 7
- 9
client/lib/npg_client.js View File

@ -44,7 +44,7 @@ NPGClient.LOGIN = {
'x': 400,
'y': 450,
'style': {
'font': '15px Lucida Console',
'font': '15px Arial',
'col': '#FFFFFF',
'align': 'center'
},
@ -54,17 +54,17 @@ NPGClient.LOGIN = {
'x': 400,
'y': 150,
'style': {
'font': '50px Lucida Console',
'font': '50px Arial',
'col': '#FFFFFF',
'align': 'center'
}
},
INPUT: {
'text': 'enter a name: ',
'x': 265,
'text': 'login: ',
'x': 285,
'y': 300,
'style': {
'font': '25px Lucida Console',
'font': '25px Monospace',
'col': '#FFFFFF',
'align': 'left'
}
@ -72,10 +72,8 @@ NPGClient.LOGIN = {
CURSOR: {
'w': 19,
'h': 19,
'pos': {
'x': 435,
'y': 283,
},
'x': 390,
'y': 283,
'style': {
'fm': true,
'fc': '#FFFFFF',


+ 0
- 13
client/lib/pages/login_page.js View File

@ -1,13 +0,0 @@
/**
* @file login_page.js
* @author frtk@tetalab
*/
NPGClient.LoginPage = {
pageIdx: 0,
page: new NPGClient.AppPage('login'),
};

+ 42
- 24
client/lib/ui/objects/ui_cursor.js View File

@ -5,10 +5,18 @@
NPGClient.UICursor = function(n, o) {
//
this.rect = new NPGClient.UIRect(n, o);
// inheritance
NPGClient.UIObject.call(this, o);
// obj name
this.name = n !== undefined ? n : '';
this.w = o.w !== undefined ? o.w : 0;
this.h = o.h !== undefined ? o.h : 0;
// starting position
this.x0 = this.x();
this.y0 = this.y();
// style
this.style = o.style !== undefined ? o.style : {};
// state
this.bState = true;
this.bCount = 0;
@ -16,28 +24,38 @@ NPGClient.UICursor = function(n, o) {
};
NPGClient.UICursor.prototype = {
// Constructor
constructor: NPGClient.UICursor,
//
draw: function(ctx) {
var self = this;
if (self.bState) {
if (self.bCount == self.bCountMax) {
self.bCount = 0;
self.bState = false;
} else self.rect.draw(ctx);
self.bCount++;
} else {
if (self.bCount == self.bCountMax) {
self.bCount = 0;
self.bState = true;
self.rect.draw(ctx);
} else self.bCount++;
}
},
// inheritance
NPGClient.UICursor.prototype = Object.create(NPGClient.UIObject.prototype);
// constructor
NPGClient.UICursor.prototype.constructor = NPGClient.UICursor;
// reset
NPGClient.UICursor.prototype.reset = function() {
var self = this;
self.moveTo(self.x0, self.y0);
}
// draw
NPGClient.UICursor.prototype.draw = function(ctx) {
var self = this;
if (self.bState) {
if (self.bCount == self.bCountMax) {
self.bCount = 0;
self.bState = false;
} else {
NPGClient.Utils.drawFillRect(ctx, self.x(), self.y(), self.w, self.h);
self.bCount++;
}
} else {
if (self.bCount == self.bCountMax) {
self.bCount = 0;
self.bState = true;
NPGClient.Utils.drawFillRect(ctx, self.x(), self.y(), self.w, self.h);
} else self.bCount++;
}
};

+ 3
- 1
client/lib/ui/objects/ui_input_text.js View File

@ -36,9 +36,11 @@ NPGClient.UIInputText.prototype = {
//
draw: function(ctx, state) {
var self = this;
var t = self.text + self.input;
self.updateInput();
//console.log(self.s);
NPGClient.Utils.setTxtStyle(ctx, self.s);
ctx.fillText(self.text + self.input, self.x, self.y);
ctx.fillText(t, self.x, self.y);
}
};


+ 0
- 35
client/lib/ui/objects/ui_line.js View File

@ -1,35 +0,0 @@
/**
* @file ui_line.js
* @author frtk@tetalab
*/
NPGClient.Line = function(name, p1, p2, style) {
NPGClient.UIObject.call(this, name, style);
this.p1 = p1 !== undefined ? p1 : new NPGClient.Point2D();
this.p2 = p2 !== undefined ? p2 : new NPGClient.Point2D();
};
NPGClient.Line.prototype = Object.create(NPGClient.UIObject.prototype);
NPGClient.Line.prototype.constructor = NPGClient.Line;
NPGCLient.Line.prototype.draw = function(ctx) {
var self = this;
self.drawLine(ctx);
};
NPGClient.Line.prototype.drawLine = function(ctx) {
var self = this;
self.applyStyle(ctx);
ctx.beginPath();
ctx.moveTo(self.p1.x, self.p1.y);
ctx.lineTo(self.p2.x, self.p2.y);
ctx.stroke();
};

+ 66
- 34
client/lib/ui/objects/ui_object.js View File

@ -1,45 +1,77 @@
/**
* @file ui_object
* @file ui_object.js
* @author frtk@tetalab
*/
//
NPGClient.UIObject = function(name, style) {
NPGClient.UIObject = function(o) {
//Object.defineProperty( this, 'id', { value: GT4UI.Object2DIdCount++ });
this.name = name !== undefined ? name : '';
this.style = style !== undefined ? style : new NPGClient.UIStyle();
this._x = (o.x !== undefined) ? o.x : 0; // x position
this._y = (o.y !== undefined) ? o.y : 0; // y position
this._theta = (o.theta !== undefined) ? o.theta : 0; // theta wrt horizon
};
/**
*
*/
NPGClient.UIObject.prototype = {
constructor: NPGClient.UIObject,
setStyle: function(s) {
this.style = s;
},
hasBorders: function() {
return this.style.getBorderMode();
},
hasFillStyle: function() {
return this.style.getFillMode();
},
applyStyle: function(ctx) {
var self = this;
if (self.style.getBorderMode()) {
ctx.lineWidth = self.style.bw;
ctx.strokeStyle = self.style.bc;
}
if (self.style.getFillMode()) {
ctx.fillStyle = self.style.fc;
}
// Constructor
constructor: NPGClient.UIObject,
//set/get
x: function(data) {
var self = this;
if (data !== undefined) {
self._x = data;
} else {
return self._x;
}
};
},
//set/get
y: function(data) {
var self = this;
if (data !== undefined) {
self._y = data;
} else {
return self._y;
}
},
//
moveTo: function(x, y) {
var self = this;
self._x = x;
self._y = y;
},
//
translateXY: function(x, y) {
var self = this;
self._x += x;
self._y += y;
},
//
translateX: function(x) {
this._x += x;
},
//
translateY: function(y) {
this._y += y;
},
//
rotate: function(x, y, t) {
},
};

+ 14
- 34
client/lib/ui/objects/ui_rect.js View File

@ -6,47 +6,27 @@
NPGClient.UIRect = function(n, o) {
//
NPGClient.UIShape.call(this);
NPGClient.UIObject.call(this, o);
// obj name
this.name = n !== undefined ? n : '';
// geom
this.pos = o.pos !== undefined ? o.pos : {};
this.w = o.w !== undefined ? o.w : 0;
this.h = o.h !== undefined ? o.h : 0;
// style
this.style = o.style !== undefined ? o.style : new NPGClient.UIStyle();
};
NPGClient.UIRect.prototype = {
// Constructor
constructor: NPGClient.UIRect,
//
draw: function(ctx) {
var self = this;
if (!self.style.fm) {
self.drawNoFill(ctx, self.pos.x, self.pos.y, self.w, self.h, self.style.lw, self.style.lc);
} else {
self.drawFill(ctx, self.pos.x, self.pos.y, self.w, self.h, self.style.fc);
}
},
//
drawFill: function(ctx) {
var self = this;
NPGClient.Utils.applyStyle(ctx, self.style);
ctx.fillRect(self.pos.x, self.pos.y, self.w, self.h);
},
//
drawNoFill: function(ctx, x1, y1, w, h, lw, c) {
var self = this;
ctx.beginPath();
NPGClient.Utils.applyStyle(ctx, self.style);
ctx.rect(self.pos.x, self.pos.y, self.w, self.h);
ctx.stroke();
},
// inheritance
NPGClient.UIRect.prototype = Object.create(NPGClient.UIObject.prototype);
// Constructor
NPGClient.UIRect.prototype.constructor = NPGClient.UIRect;
NPGClient.UIRect.prototype.draw = function(ctx) {
var self = this;
NPGClient.Utils.applyStyle(ctx, self.style);
if (!self.style.fm) {
NPGClient.Utils.drawRect(ctx, self.x(), self.y(), self.w, self.h);
} else {
NPGClient.Utils.drawFillRect(ctx, self.x(), self.y(), self.w, self.h);
}
};

+ 0
- 45
client/lib/ui/objects/ui_shape.js View File

@ -1,45 +0,0 @@
/**
* @file ui_shape.js
* @author frtk@tetalab
*/
//
NPGClient.UIShape = function() {
this.x = 0; // x position
this.y = 0; // y position
};
/**
*
*/
NPGClient.UIShape.prototype = {
// Constructor
constructor: NPGClient.UIShape,
//
moveTo: function(x, y) {
var self = this;
self.x = x;
self.y = y;
},
//
move: function(x, y) {
var self = this;
self.x += x;
self.y += y;
},
//
shiftX: function(x) {
this.x += x;
},
//
shiftY: function(y) {
this.y += y;
}
};

+ 75
- 8
client/lib/utils/client_utils.js View File

@ -6,7 +6,64 @@
NPGClient.Utils = {
/**
* UI
* UI draw
*/
//
drawText: function(ctx, x1, y1, t, s, c) {
ctx.font = s;
ctx.fillStyle = c;
ctx.fillText(t, x1, y1);
},
//
drawFillRect: function(ctx, x1, y1, w, h) {
ctx.fillRect(x1, y1, w, h);
},
//
drawRect: function(ctx, x1, y1, w, h) {
ctx.beginPath();
ctx.rect(x1, y1, w, h);
ctx.stroke();
},
//
drawLine: function(ctx, x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
},
//
drawSemiCircle: function(ctx, x, y, r, a1, a2, o, w, c) {
ctx.lineWidth = w;
ctx.strokeStyle = c;
ctx.beginPath();
ctx.arc(x, y, r, a1, a2, o);
ctx.closePath();
ctx.stroke();
},
drawFillCircle: function(ctx, x, y, r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2, false);
ctx.fill();
ctx.stroke();
ctx.closePath();
},
//
drawCircle: function(ctx, x, y, r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2, false);
ctx.closePath();
ctx.stroke();
},
/**
* UI Style
*/
//
setTxtStyle : function(ctx, style) {
@ -37,32 +94,42 @@ NPGClient.Utils = {
//console.log(key + ' ' + NPGClient.userName);
if (NPGClient.userName.length < NPGClient.NAMEMAXSIZE) {
NPGClient.userName = NPGClient.userName + String.fromCharCode(key).toLowerCase();
console.log(NPGClient.userName);
//console.log(NPGClient.userName);
}
},
//
removeChar : function(key) {
removeChar: function(key) {
if (NPGClient.userName.length > 0) {
NPGClient.userName = NPGClient.userName.substring(0, NPGClient.userName.length - 1);
console.log(NPGClient.userName);
//console.log(NPGClient.userName);
}
},
//
validChar : function(key) {
validChar: function(key) {
return NPGClient.evtHandler.loginValidKey(key);
},
//
validName : function(key) {
validName: function() {
var len = NPGClient.userName.length;
return (len >= 0 && len <= NPGClient.NAMEMAXSIZE);
return (len >= 3 && len <= NPGClient.NAMEMAXSIZE);
},
//
maxNameSize: function() {
return NPGClient.userName.length == NPGClient.NAMEMAXSIZE;
},
//
resetName : function() {
resetName: function() {
NPGClient.userName = '';
},
//
nameEmpty: function() {
return (NPGClient.userName.length == 0);
},
}

+ 0
- 57
client/lib/utils/utils.js View File

@ -1,57 +0,0 @@
/**
* @file utils.js
* @author frtk@tetalab
*/
NPGClient.Utils = {
/**
* UI
*/
//
setTxtStyle : function(ctx, style) {
ctx.font = style.font;
ctx.fillStyle = style.col;
ctx.textAlign = style.align;
},
/**
* User Name
*/
//
addChar : function(key, name) {
if (name.length < NPGClient.NAMEMAXSIZE) {
name = name + String.fromCharCode(key).toLowerCase();
}
},
//
removeChar : function(key) {
if (name.length > 0) {
name = name.substring(0, name.length - 1);
}
},
//
validChar : function(key) {
return NPGClient.evtHandler.loginValidKey(key);
},
//
validName : function(key) {
var len = name.length;
return (len >= 0 && len <= NPGClient.MAXNAMESIZE);
},
//
resetName : function(name) {
name = '';
},
}

+ 1
- 1
package.json View File

@ -1,6 +1,6 @@
{
"name": "npg_app.js",
"version": "0.1.3",
"version": "0.1.4",
"description": "Multiplayer online Pong Game using nodejs and socket.io",
"directories": {
"server": "server",


+ 1
- 1
server/npg_server_config.js View File

@ -4,7 +4,7 @@
*/
var Config = {
VERSION : '0.1.3',
VERSION : '0.1.4',
HTTP: {
host: '127.0.0.1',
port: 8042


+ 16
- 0
version.md View File

@ -1,3 +1,19 @@
### **v0.1.4:**
--------------------------
focus: client ui lib
--------------------------
---- changed font (Lucida -> Arial)
- npg_client.js
---- added in /ui/objects/
- ui_object.js (base object)
---- updated objects in
- ui_rect.js (inherits from UIObject)
- ui_cursor.js (inherits from UIObject)
---- /keyboard/evt_handler.js
- userLogin()
---- cursor object
- fixed behaviour on login page - working fine now
- reset position at login attempt when server is down
### **v0.1.3:**


Loading…
Cancel
Save