Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Overlay Color Editor #34

Merged
merged 4 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"author": "Brian Clark",
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.3",
"discord.js": "^11.3.2",
"dotenv": "^5.0.1",
"es6-promise": "^4.2.5",
Expand Down
21 changes: 21 additions & 0 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { appendFile, existsSync, writeFile } from 'fs';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this file can be more descriptive. I don't know the future of the file, but its explicitly writing to a custom styles class

import { resolve as resolvePath } from 'path';

const FILE_NAME = resolvePath(__dirname, '../src/assets/custom-styles.css');

export function write(data: any) {
if (existsSync(FILE_NAME)) {
return new Promise((resolve, reject) => {
appendFile(FILE_NAME, data, (err: any) => {
if (err) reject(err);
resolve(data);
});
});
}
return new Promise((resolve, reject) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complexity is high with this one, probably going to refactor moving forward, but pull request to me makes it seems like this code is going into master.

writeFile(FILE_NAME, data, (err: any) => {
if (err) reject(err);
resolve(data);
});
});
}
21 changes: 21 additions & 0 deletions src/routes/save-css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from 'express';
import * as files from '../files';

export const saveCssRoute = (req: express.Request, res: express.Response) => {
const captains = console;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use the log class

captains.log(req.body);
const { colorName, hueRotateDeg } = req.body;
const data = formatForCSS(colorName, hueRotateDeg);
files
.write(data)
.then((result: any) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to type what the result is going to be.

res.send({ message: 'Saved' });
})
.catch((error: any) => {
res.status(500).send(error);
});
};

function formatForCSS(colorName: string, hueRotateDeg: string) {
return `.${colorName} {\n filter: hue-rotate(${hueRotateDeg}deg);\n}\n`;
}
43 changes: 38 additions & 5 deletions src/scripts/overlay-colors.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
$(document).ready(() => {
let hueValue = 0;
const captains = console;
$('#get-overlay').click(() => {
const url = $('#overlay-url').val();
$('#overlay-frame').attr('src', url);
});

$('#increase-hue').click(() => {
hueValue = hueValue < 361 ? (hueValue += 1) : 360;
$('#container').css('filter', `hue-rotate(${hueValue}deg)`);
let currentHueValue = $('#rotation').val();
const hueValue = currentHueValue < 361 ? (currentHueValue += 1) : 360;
applyHue(hueValue);
});

$('#decrease-hue').click(() => {
hueValue = hueValue > 0 ? (hueValue -= 1) : 0;
$('#container').css('filter', `hue-rotate(${hueValue}deg)`);
let currentHueValue = $('#rotation').val();
const hueValue = currentHueValue > 0 ? (currentHueValue -= 1) : 0;
applyHue(hueValue);
});

$('#save-color').click(() => {
const hueRotateDeg = $('#rotation').val();
const colorName = $('#color-name').val();
const data = JSON.stringify({ colorName, hueRotateDeg });
const requestOptions = {
url: '/save',
method: 'POST',
data,
contentType: 'application/json'
};
request(requestOptions).done(result => captains.log(result));
});

$('#update-hue').click(() => {
const hueRotateDeg = $('#rotation').val();
applyHue(hueRotateDeg);
});

function applyHue(hueRotateDeg) {
$('#container').css('filter', `hue-rotate(${hueRotateDeg}deg)`);
}

function request(requestOptions) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this

return $.ajax(requestOptions)
.done(result => result)
.fail(error => {
captains.error(error);
return error;
});
}
});
5 changes: 5 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bodyParser from 'body-parser';
import { WebhookClient } from 'discord.js';
import express = require('express');
import { Server } from 'http';
Expand All @@ -9,6 +10,7 @@ import { DiscordBot } from './discord-bot';
import { log } from './log';
import { Overlay } from './overlay';
import { changeLightColor, sendLightEffect } from './routes/lights';
import { saveCssRoute } from './routes/save-css';
import { scenesRoute } from './routes/scenes';

/**
Expand Down Expand Up @@ -70,6 +72,7 @@ export class AppServer {
* Config Express
*/
private configApp(): void {
this.app.use(bodyParser.json());
this.app.set('view engine', 'pug');
this.app.set('views', `${__dirname}/views`);
this.app.use(express.static(__dirname));
Expand All @@ -82,6 +85,8 @@ export class AppServer {
const router: express.Router = express.Router();
router.get('/scenes', scenesRoute);

router.post('/save', saveCssRoute);

router.get('/overlay-colors', (req, res) => {
res.render('overlay-colors');
});
Expand Down
1 change: 1 addition & 0 deletions src/views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ doctype html
html(lang='en')
head
link(rel='stylesheet', href='../assets/styles.css', type='text/css')
link(rel='stylesheet', href='../assets/custom-styles.css', type='text/css')
script(src='https://code.jquery.com/jquery-3.3.1.min.js', integrity='sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=', crossorigin='anonymous')
script(src='/socket.io/socket.io.js')
script(src='../scripts/stream.js')
Expand Down
6 changes: 5 additions & 1 deletion src/views/overlay-colors.pug
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ html(lang='en')
br
button#increase-hue(style={height:'40px;', width:'70px;'}) Up ⬆
button#decrease-hue(style={height:'40px;', width:'70px;'}) Down ⬇
input#rotation(style={height:'40px;', width:'100px;'})
button#update-hue(style={height:'40px;', width:'70px;'}) Update Hue
input#color-name(style={height:'40px;', width:'100px;'})
button#save-color(style={height:'40px;', width:'70px;'}) Save Color
br
div#container
iframe#overlay-frame(style={height:'720px;', width:'1280px;'})
iframe#overlay-frame(style={height:'720px;', width:'1280px;'})