Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
(function() {
'use strict';

var toTranscript = require('./ui/content/transcript');

// To avoid the need to include any utility libraries when this is
// used in a browser, define some helper functions we'd normally
// rely on libraries for.
Expand Down Expand Up @@ -235,12 +237,13 @@
return this;
};


DendryEngine.prototype.displayChoices = function() {
var choices = this.getCurrentChoices();
assert(choices);
this.ui.displayChoices(choices);
if (this.state.enableTranscript) {
this.transcript.push(choices);
toTranscript.logChoices(choices, this.transcript);
}
return this;
};
Expand Down Expand Up @@ -269,7 +272,7 @@
if (scene.content !== undefined && !restorePage) {
var displayContent = this._makeDisplayContent(scene.content, true);
if (this.state.enableTranscript) {
this.transcript = this.transcript.concat(displayContent);
toTranscript.log(displayContent, this.transcript);
}
this.state.currentContent = this.state.currentContent.concat(displayContent);
this.ui.displayContent(displayContent);
Expand Down Expand Up @@ -358,7 +361,7 @@
// Should the transcription feature be part of the UI or the engine?
// Should the transcript-enabling flag be part of the game state, or be part of the UI options?
// let's just say that it's part of the game state. But the actual transcript-writing process is done in the UIs? Because different UIs might want to save states...
enableTranscript: false,
enableTranscript: false
};
// TODO: transcript
this.transcript = [];
Expand Down
18 changes: 18 additions & 0 deletions lib/templates/html/default/+game.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
save_element.style.display = "none";
};

window.enableTranscript = function() {
var checkbox = document.getElementById('transcript_checkbox');
// Update the engine's transcripting state
window.dendryUI.dendryEngine.state.enableTranscript = checkbox.checked;
// Show or hide the Save Transcript links
var save_links = document.querySelectorAll('.save-transcript');
for (var i = 0; i < save_links.length; ++i) {
var link = save_links[i];
if (checkbox.checked) {
link.style.display = '';
} else {
link.style.display = 'none';
}
}
}

window.disableBg = function() {
window.dendryUI.disable_bg = true;
document.body.style.backgroundImage = 'none';
Expand Down Expand Up @@ -80,9 +96,11 @@

// populates the checkboxes in the options view
window.populateOptions = function() {
var enable_transcript = window.dendryUI.dendryEngine.state.enableTranscript;
var disable_bg = window.dendryUI.disable_bg;
var animate = window.dendryUI.animate;
var animate_bg = window.dendryUI.animate_bg;
$('#transcript_checkbox')[0].checked = enable_transcript;
if (disable_bg) {
$('#backgrounds_no')[0].checked = true;
} else {
Expand Down
10 changes: 10 additions & 0 deletions lib/templates/html/default/+index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ <h2>{{game.author}}</h2>
<!--<a id="stats-link" onclick="window.showStats();" href="#">Status</a>-->
<a onclick="dendryUI.showSaveSlots();" href="#">Save/Load</a>
<a onclick="dendryUI.showOptions();" href="#">Options</a>
<a onclick="window.saveTranscript();" href="#" class="save-transcript">Save Transcript</a>
</div>

</header>

<div id="options" class="overlay" style="display: none;">
<div class="overlay_top">
<table>
<tr>
<td>
<label for="transcript_checkbox">Enable transcript</label>
</td>
<td>
<input type="checkbox" id="transcript_checkbox" name="transcript" value="1" onclick="window.enableTranscript();">
</td>
</tr>
<tr>
<td>
<label for="backgrounds_yes">Enable backgrounds</label>
Expand Down Expand Up @@ -224,6 +233,7 @@ <h2>{{game.author}}</h2>
<a onclick="window.showSaveSlots();" href="#">Save/Load</a>
<a onclick="window.quickSave();" href="#">Quick Save</a>
<a onclick="window.quickLoad();" href="#">Quick Load</a>
<a onclick="window.saveTranscript();" href="#" class="save-transcript">Save Transcript</a>
</p>

&copy; 2021 Autumn Chen.
Expand Down
26 changes: 26 additions & 0 deletions lib/ui/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,32 @@
save_element.style.display = 'none';
};

var _0pad = function(n) {
return (n < 10 ? '0' : '') + n;
}

window.saveTranscript = function() {
var transcript = window.dendryUI.dendryEngine.transcript;
var blob = new Blob([transcript.join('\n\n')]);
var data = URL.createObjectURL(blob);
var now = new Date();
var year = '' + now.getFullYear();
var month = _0pad(now.getMonth() + 1);
var day = _0pad(now.getDate());
var hours = _0pad(now.getHours());
var minutes = _0pad(now.getMinutes());
var date = year + '-' + month + '-' + day;
var time = hours + '-' + minutes;
var when = date + '_' + time;
var link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', 'bee-transcript' + when + '.txt');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
link.remove();
};


// functions for dealing with options
BrowserUserInterface.prototype.setOption = function(option, toggle) {
Expand Down
121 changes: 121 additions & 0 deletions lib/ui/content/transcript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* dendry
* http://github.com/idmillington/dendry
*
* MIT License
*/
/*jshint indent:2 */
(function() {
'use strict';

var _repeat = function(str, times) {
if (times < 1) return '';
if (times % 2 !== 0) return str + _repeat(str, times - 1);
var half = _repeat(str, times / 2);
return half + half;
}

var _indent = function(spaces, text) {
return _repeat(' ', spaces) + text;
}

var _objToText = function(obj) {
if (obj.type == null) {
return obj;
} else {
switch (obj.type) {
case 'emphasis-1':
return '*' + _lineToText(obj.content) + '*';
case 'emphasis-2':
return '**' + _lineToText(obj.content) + '**';
case 'hidden':
return _lineToText(obj.content);
case 'line-break':
return '\n';

// We can't handle elements that require state-dependency.
case 'insert':
case 'conditional':
throw new Error(
obj.type + ' should have been evaluated by now.'
);
}
}
};

var _lineToText = function(content) {
var output;
if (Array.isArray(content)) {
output = content.map(_objToText).join('')
} else {
output = _objToText(content);
}
return output.replace(/ +/g, ' ').replace(/ *\n */g, '\n');
};

var _paragraphsToText = function(paragraphs) {
var text;
var result = [];
for (var i = 0; i < paragraphs.length; ++i) {
var paragraph = paragraphs[i];
switch (paragraph.type) {
case 'heading':
text = _lineToText(paragraph.content).trim();
result.push('\n' + text);
result.push(_repeat('-', text.length) + '\n');
break;
case 'paragraph':
text = _lineToText(paragraph.content);
if (text !== '')
result.push(text.trim() + '\n');
break;
case 'quotation':
text = _lineToText(paragraph.content);
result.push(
_indent(4, text.trim()) +
((i === paragraphs.length - 1 ||
paragraphs[i + 1].type !== 'attribution') ? '\n' : '')
);
break;
case 'attribution':
text = _lineToText(paragraph.content);
result.push(_indent(8, text.trim()) + '\n');
break;
case 'hrule':
result.push('-----\n');
break;
}
}
return result.join('\n');
};

var _logParagraphs = function(paragraphs, log) {
var text = _paragraphsToText(paragraphs).trimEnd();
log.push(text);
};

var _logChoices = function(choices, log) {
var out = [];
var titleIndent = 4;
var subtitleIndent = 7;
for (var i = 0; i < choices.length; ++i) {
var choice = choices[i];
var title = (i + 1) + '. ' + _lineToText(choice.title);
if (!choice.canChoose) {
title += ' [Unavailable]';
}
out.push(_indent(titleIndent, title));
if (choice.subtitle) {
out.push(_indent(subtitleIndent, choice.subtitle));
}
}
log.push(out.join('\n'));
};

module.exports = {
indent: _indent,
convert: _paragraphsToText,
convertLine: _lineToText,
log: _logParagraphs,
logChoices: _logChoices
};
}());