Merge pull request #3031 from stojce/19755

Code Review for Job #19755
This commit is contained in:
Brad Hefta-Gaub 2014-06-17 17:28:55 -07:00
commit cee6f1be6a
9 changed files with 220 additions and 10 deletions

119
examples/myBalance.js Normal file
View file

@ -0,0 +1,119 @@
//
// myBalance.js
// examples
//
// Created by Stojce Slavkovski on June 5, 2014
// Copyright 2014 High Fidelity, Inc.
//
// Show wallet ₵ balance
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var Controller = Controller || {};
var Overlays = Overlays || {};
var Script = Script || {};
var Account = Account || {};
(function () {
"use strict";
var iconUrl = 'http://highfidelity-public.s3-us-west-1.amazonaws.com/images/tools/',
overlayWidth = 150,
overlayHeight = 50,
overlayTopOffset = 15,
overlayRightOffset = 140,
textRightOffset = 105,
maxIntegers = 5,
downColor = {
red: 0,
green: 0,
blue: 255
},
upColor = {
red: 0,
green: 255,
blue: 0
},
normalColor = {
red: 204,
green: 204,
blue: 204
},
balance = -1,
walletBox = Overlays.addOverlay("image", {
x: 0,
y: overlayTopOffset,
width: 122,
height: 32,
imageURL: iconUrl + "wallet.svg",
alpha: 1
}),
textOverlay = Overlays.addOverlay("text", {
x: 0,
y: overlayTopOffset,
topMargin: 10,
font: {
size: 16
},
color: normalColor
});
function scriptEnding() {
Overlays.deleteOverlay(walletBox);
Overlays.deleteOverlay(textOverlay);
}
function update(deltaTime) {
var xPos = Controller.getViewportDimensions().x;
Overlays.editOverlay(walletBox, {
x: xPos - overlayRightOffset,
visible: Account.isLoggedIn()
});
Overlays.editOverlay(textOverlay, {
x: xPos - textRightOffset,
visible: Account.isLoggedIn()
});
}
function formatedBalance() {
var integers = balance.toFixed(0).length,
decimals = Math.abs(maxIntegers - integers) + 2;
var x = balance.toFixed(decimals).split('.'),
x1 = x[0],
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function updateBalance(newBalance) {
if (balance === newBalance) {
return;
}
var change = newBalance - balance,
textColor = change < 0 ? downColor : upColor;
balance = newBalance;
Overlays.editOverlay(textOverlay, {
text: formatedBalance(),
color: textColor
});
Script.setTimeout(function () {
Overlays.editOverlay(textOverlay, {
color: normalColor
});
}, 1000);
}
updateBalance(Account.getBalance());
Account.balanceChanged.connect(updateBalance);
Script.scriptEnding.connect(scriptEnding);
Script.update.connect(update);
}());

View file

@ -73,6 +73,7 @@
#include "devices/TV3DManager.h"
#include "renderer/ProgramObject.h"
#include "scripting/AccountScriptingInterface.h"
#include "scripting/AudioDeviceScriptingInterface.h"
#include "scripting/ClipboardScriptingInterface.h"
#include "scripting/MenuScriptingInterface.h"
@ -3572,6 +3573,7 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScript
scriptEngine->registerGlobalObject("AudioDevice", AudioDeviceScriptingInterface::getInstance());
scriptEngine->registerGlobalObject("AnimationCache", &_animationCache);
scriptEngine->registerGlobalObject("AudioReflector", &_audioReflector);
scriptEngine->registerGlobalObject("Account", AccountScriptingInterface::getInstance());
QThread* workerThread = new QThread(this);

View file

@ -0,0 +1,41 @@
//
// AccountScriptingInterface.cpp
// interface/src/scripting
//
// Created by Stojce Slavkovski on 6/07/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "AccountManager.h"
#include "AccountScriptingInterface.h"
AccountScriptingInterface::AccountScriptingInterface() {
AccountManager& accountManager = AccountManager::getInstance();
connect(&accountManager, &AccountManager::balanceChanged, this,
&AccountScriptingInterface::updateBalance);
}
AccountScriptingInterface* AccountScriptingInterface::getInstance() {
static AccountScriptingInterface sharedInstance;
return &sharedInstance;
}
float AccountScriptingInterface::getBalance() {
AccountManager& accountManager = AccountManager::getInstance();
return accountManager.getAccountInfo().getBalanceInSatoshis();
}
bool AccountScriptingInterface::isLoggedIn() {
AccountManager& accountManager = AccountManager::getInstance();
return accountManager.isLoggedIn();
}
void AccountScriptingInterface::updateBalance() {
AccountManager& accountManager = AccountManager::getInstance();
emit balanceChanged(accountManager.getAccountInfo().getBalanceInSatoshis());
}

View file

@ -0,0 +1,31 @@
//
// AccountScriptingInterface.h
// interface/src/scripting
//
// Created by Stojce Slavkovski on 6/07/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_AccountScriptingInterface_h
#define hifi_AccountScriptingInterface_h
#include <QObject>
class AccountScriptingInterface : public QObject {
Q_OBJECT
AccountScriptingInterface();
signals:
void balanceChanged(float newBalance);
public slots:
static AccountScriptingInterface* getInstance();
float getBalance();
bool isLoggedIn();
void updateBalance();
};
#endif // hifi_AccountScriptingInterface_h

View file

@ -26,9 +26,9 @@ Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int wid
}
TextRenderer::TextRenderer(const char* family, int pointSize, int weight,
bool italic, EffectType effectType, int effectThickness)
bool italic, EffectType effectType, int effectThickness, QColor color)
: _font(family, pointSize, weight, italic), _metrics(_font), _effectType(effectType),
_effectThickness(effectThickness), _x(IMAGE_SIZE), _y(IMAGE_SIZE), _rowHeight(0) {
_effectThickness(effectThickness), _x(IMAGE_SIZE), _y(IMAGE_SIZE), _rowHeight(0), _color(color) {
_font.setKerning(false);
}
@ -157,7 +157,7 @@ const Glyph& TextRenderer::getGlyph(char c) {
// render the glyph into an image and copy it into the texture
QImage image(bounds.width(), bounds.height(), QImage::Format_ARGB32);
if (c == SOLID_BLOCK_CHAR) {
image.fill(QColor(255, 255, 255));
image.fill(_color);
} else {
image.fill(0);
@ -180,7 +180,7 @@ const Glyph& TextRenderer::getGlyph(char c) {
painter.setRenderHint(QPainter::Antialiasing);
painter.drawPath(path);
}
painter.setPen(QColor(255, 255, 255));
painter.setPen(_color);
painter.drawText(-bounds.x(), -bounds.y(), ch);
}
glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, bounds.width(), bounds.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());

View file

@ -12,6 +12,7 @@
#ifndef hifi_TextRenderer_h
#define hifi_TextRenderer_h
#include <QColor>
#include <QFont>
#include <QFontMetrics>
#include <QHash>
@ -41,7 +42,8 @@ public:
enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT };
TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
EffectType effect = NO_EFFECT, int effectThickness = 1);
EffectType effect = NO_EFFECT, int effectThickness = 1,
QColor color = QColor(255, 255, 255));
~TextRenderer();
const QFontMetrics& metrics() const { return _metrics; }
@ -85,6 +87,9 @@ private:
// the list of all texture ids for which we're responsible
QVector<GLuint> _allTextureIDs;
// text color
QColor _color;
};
class Glyph {

View file

@ -19,7 +19,8 @@
TextOverlay::TextOverlay() :
_leftMargin(DEFAULT_MARGIN),
_topMargin(DEFAULT_MARGIN)
_topMargin(DEFAULT_MARGIN),
_fontSize(DEFAULT_FONTSIZE)
{
}
@ -32,7 +33,7 @@ void TextOverlay::render() {
}
const float MAX_COLOR = 255;
glColor4f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, _alpha);
glColor4f(0 / MAX_COLOR, 0 / MAX_COLOR, 0 / MAX_COLOR, _alpha);
glBegin(GL_QUADS);
glVertex2f(_bounds.left(), _bounds.top());
@ -43,8 +44,9 @@ void TextOverlay::render() {
//TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
// EffectType effect = NO_EFFECT, int effectThickness = 1);
TextRenderer textRenderer(SANS_FONT_FAMILY, 11, 50);
TextRenderer textRenderer(SANS_FONT_FAMILY, _fontSize, 50, false, TextRenderer::NO_EFFECT, 1,
QColor(_color.red, _color.green, _color.blue));
const int leftAdjust = -1; // required to make text render relative to left edge of bounds
const int topAdjust = -2; // required to make text render relative to top edge of bounds
int x = _bounds.left() + _leftMargin + leftAdjust;
@ -67,6 +69,13 @@ void TextOverlay::render() {
void TextOverlay::setProperties(const QScriptValue& properties) {
Overlay2D::setProperties(properties);
QScriptValue font = properties.property("font");
if (font.isObject()) {
if (font.property("size").isValid()) {
setFontSize(font.property("size").toInt32());
}
}
QScriptValue text = properties.property("text");
if (text.isValid()) {

View file

@ -29,6 +29,7 @@
#include "Overlay2D.h"
const int DEFAULT_MARGIN = 10;
const int DEFAULT_FONTSIZE = 11;
class TextOverlay : public Overlay2D {
Q_OBJECT
@ -47,6 +48,7 @@ public:
void setText(const QString& text) { _text = text; }
void setLeftMargin(int margin) { _leftMargin = margin; }
void setTopMargin(int margin) { _topMargin = margin; }
void setFontSize(int fontSize) { _fontSize = fontSize; }
virtual void setProperties(const QScriptValue& properties);
@ -55,7 +57,7 @@ private:
QString _text;
int _leftMargin;
int _topMargin;
int _fontSize;
};

View file

@ -38,6 +38,7 @@ public:
void setDiscourseApiKey(const QString& discourseApiKey);
qint64 getBalance() const { return _balance; }
float getBalanceInSatoshis() const { return _balance / SATOSHIS_PER_CREDIT; }
void setBalance(qint64 balance);
bool hasBalance() const { return _hasBalance; }
void setHasBalance(bool hasBalance) { _hasBalance = hasBalance; }