mirror of
https://github.com/Oreolek/gamebookformat.git
synced 2024-05-14 23:18:19 +03:00
Split up HTML templates to give more hooks to more easily extend.
This commit is contained in:
parent
83d1ab0880
commit
5c70a94cce
|
@ -4,7 +4,7 @@ import os.path
|
|||
import sys
|
||||
|
||||
PREPROCESS_RE = re.compile("^\s*#")
|
||||
INCLUDE_RE = re.compile('^\s*#include\s*"(\w+)"')
|
||||
INCLUDE_RE = re.compile('^\s*#include\s*"([-\w]+)"')
|
||||
|
||||
class Templates (object):
|
||||
def __init__(self, templatedirs, extension):
|
||||
|
|
|
@ -6,9 +6,11 @@
|
|||
<title>%(title)s</title>
|
||||
<script>
|
||||
#include "script"
|
||||
#include "override-script"
|
||||
</script>
|
||||
<style>
|
||||
#include "style"
|
||||
#include "override-style"
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
#include "collections"
|
||||
#include "collectiontemplate"
|
||||
<script>
|
||||
#include "before-endscript"
|
||||
#include "endscript"
|
||||
#include "after-endscript"
|
||||
</script>
|
||||
#include "showintro"
|
||||
#include "extra-end"
|
||||
</end>
|
||||
</body>
|
||||
</html>
|
||||
|
|
197
templates/html/gamebookscript.html
Normal file
197
templates/html/gamebookscript.html
Normal file
|
@ -0,0 +1,197 @@
|
|||
'player' : {
|
||||
#include "playerscript"
|
||||
#include "extra-playerscript"
|
||||
},
|
||||
|
||||
'sections' : {},
|
||||
|
||||
'turnToFunctions' : {},
|
||||
|
||||
'dropped' : {},
|
||||
|
||||
'addSection' : function(nr, element) {
|
||||
var section = {'element' : element, 'nr' : nr};
|
||||
this.sections[nr] = section;
|
||||
},
|
||||
|
||||
'turnTo' : function(nr) {
|
||||
if (!gamebook.player.started) {
|
||||
gamebook.start();
|
||||
}
|
||||
if (!nr in this.sections) {
|
||||
throw new Exception("Can not turn to non-existing section "
|
||||
+ nr + ".");
|
||||
}
|
||||
this.displaySection(nr);
|
||||
},
|
||||
|
||||
'start' : function() {
|
||||
this.hideIntroSections();
|
||||
this.addClassToClass('startlink', 'nodisplay');
|
||||
gamebook.player.started = true;
|
||||
},
|
||||
|
||||
'displaySection' : function(nr) {
|
||||
if (this.player.currentSection) {
|
||||
this.player.currentSection.element.style.display = 'none';
|
||||
}
|
||||
var e = this.sections[nr].element;
|
||||
this.runActions(e.getElementsByClassName('sectiontext')[0]);
|
||||
e.style.display = 'block';
|
||||
this.player.currentSection = gamebook.sections[nr];
|
||||
},
|
||||
|
||||
'hideIntroSections' : function() {
|
||||
this.addClassToClass('introsection', 'nodisplay');
|
||||
this.removeClassFromClass('displayintrolink', 'nodisplay');
|
||||
this.addClassToClass('hideintrolink', 'nodisplay');
|
||||
},
|
||||
|
||||
'showIntroSections' : function() {
|
||||
this.runActionsInIntroSections();
|
||||
this.removeClassFromClass('introsection', 'nodisplay');
|
||||
this.addClassToClass('displayintrolink', 'nodisplay');
|
||||
this.removeClassFromClass('hideintrolink', 'nodisplay');
|
||||
document.body.scrollIntoView();
|
||||
},
|
||||
|
||||
'runActionsInIntroSections' : function() {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName('introsectionbody'),
|
||||
gamebook.runActions);
|
||||
},
|
||||
|
||||
'addClassToClass' : function(className, addClass) {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName(className),
|
||||
function(e) {
|
||||
e.classList.add(addClass);
|
||||
});
|
||||
},
|
||||
|
||||
'removeClassFromClass' : function(className, removeClass) {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName(className),
|
||||
function(e) {
|
||||
e.classList.remove(removeClass);
|
||||
});
|
||||
},
|
||||
|
||||
'runActions' : function(e) {
|
||||
var enableNextLink = true;
|
||||
var hasXorScope = false;
|
||||
var hasAutoScope = false;
|
||||
var xorEnableNext = false;
|
||||
var autoDisableAllRemainingLinks = (
|
||||
gamebook.player.started && e.classList.contains('introsectionbody'));
|
||||
Array.prototype.forEach.call(e.childNodes, function(c) {
|
||||
if (!c.classList) {
|
||||
return;
|
||||
}
|
||||
if (c.classList.contains('sectionref')) {
|
||||
if (enableNextLink && !autoDisableAllRemainingLinks) {
|
||||
gamebook.enableLink(c);
|
||||
if (hasAutoScope) {
|
||||
autoDisableAllRemainingLinks = true;
|
||||
}
|
||||
} else {
|
||||
gamebook.disableLink(c);
|
||||
}
|
||||
enableNextLink = !(hasXorScope && !xorEnableNext);
|
||||
hasAutoScope = false;
|
||||
hasXorScope = false;
|
||||
} else if (c.classList.contains('collect')) {
|
||||
gamebook.player.collect(c.dataset.type, c.dataset.name);
|
||||
} else if (c.classList.contains('add')) {
|
||||
gamebook.player.add(c.dataset.type, c.dataset.what);
|
||||
} else if (c.classList.contains('drop')) {
|
||||
gamebook.player.drop(c.dataset.type, c.dataset.what);
|
||||
} else if (c.classList.contains('has')) {
|
||||
enableNextLink = gamebook.player.has(c.dataset.type,
|
||||
c.dataset.what);
|
||||
} else if (c.classList.contains('hasnot')) {
|
||||
enableNextLink = !gamebook.player.has(c.dataset.type,
|
||||
c.dataset.what);
|
||||
} else if (c.classList.contains('xor')) {
|
||||
hasXorScope = true;
|
||||
xorEnableNext = !enableNextLink;
|
||||
} else if (c.classList.contains('auto')) {
|
||||
hasAutoScope = true;
|
||||
} else if (c.classList.contains('random')) {
|
||||
c.addEventListener('click',
|
||||
gamebook.enableRandomLinkAfter);
|
||||
c.classList.add("enabledlink");
|
||||
c.classList.remove("disabledlink");
|
||||
autoDisableAllRemainingLinks = true;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
'enableLink' : function(e) {
|
||||
e.addEventListener('click',
|
||||
gamebook.getTurnToFunction(e.dataset.ref));
|
||||
e.classList.add("enabledlink");
|
||||
e.classList.remove("disabledlink");
|
||||
},
|
||||
|
||||
'disableLink' : function(e) {
|
||||
e.removeEventListener('click',
|
||||
gamebook.getTurnToFunction(e.dataset.ref));
|
||||
e.classList.remove("enabledlink");
|
||||
e.classList.add("disabledlink");
|
||||
},
|
||||
|
||||
'enableRandomLinkAfter' : function(event) {
|
||||
this.classList.remove("enabledlink");
|
||||
this.classList.add("disabledlink");
|
||||
var links = [];
|
||||
var e = this.nextSibling;
|
||||
while (e) {
|
||||
if (e.classList && e.classList.contains('sectionref')) {
|
||||
links.push(e);
|
||||
}
|
||||
e = e.nextSibling;
|
||||
}
|
||||
if (links.length > 0) {
|
||||
var selected = links[Math.floor(Math.random()*links.length)]
|
||||
gamebook.enableLink(selected);
|
||||
} else {
|
||||
console.log("Random with nothing to select?");
|
||||
}
|
||||
event.preventDefault();
|
||||
},
|
||||
|
||||
'addCollectionView' : function(type, name) {
|
||||
var ce = document.getElementById('collections');
|
||||
var template = document.getElementById('collectionTemplate');
|
||||
var e = template.cloneNode(true);
|
||||
e.className = "collection";
|
||||
e.getElementsByClassName('collectionheading')[0].innerHTML = name;
|
||||
e.dataset.type = type;
|
||||
ce.appendChild(e);
|
||||
},
|
||||
|
||||
'updateCollectionsView' : function() {
|
||||
var ce = document.getElementById('collections');
|
||||
Array.prototype.forEach.call(ce.childNodes, function(c) {
|
||||
if (c.className === 'collection') {
|
||||
var type = c.dataset.type;
|
||||
var collection = gamebook.player.collections[type];
|
||||
var cc = c.getElementsByClassName('collectioncontents')[0];
|
||||
cc.innerHTML = collection.contents.join(', ');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
'getTurnToFunction' : function(nr) {
|
||||
if (nr in this.turnToFunctions) {
|
||||
return this.turnToFunctions[nr];
|
||||
} else {
|
||||
var f = function () {
|
||||
gamebook.turnTo(nr);
|
||||
};
|
||||
this.turnToFunctions[nr] = f;
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
43
templates/html/playerscript.html
Normal file
43
templates/html/playerscript.html
Normal file
|
@ -0,0 +1,43 @@
|
|||
'started' : false,
|
||||
'currentSection' : null,
|
||||
'collections' : {},
|
||||
|
||||
'collect' : function(type, name) {
|
||||
this.collections[type] = {
|
||||
'name' : name,
|
||||
'contents' : [],
|
||||
'add' : function(what) {
|
||||
if (this.contents.indexOf(what) === -1
|
||||
&& !(what in gamebook.dropped[type])) {
|
||||
this.contents.push(what);
|
||||
this.contents.sort();
|
||||
}
|
||||
},
|
||||
'drop' : function(what) {
|
||||
var i = this.contents.indexOf(what);
|
||||
if (i >= 0) {
|
||||
this.contents.splice(i, 1);
|
||||
gamebook.dropped[type][what] = true;
|
||||
}
|
||||
},
|
||||
'has' : function(what) {
|
||||
return this.contents.indexOf(what) >= 0;
|
||||
}
|
||||
};
|
||||
gamebook.dropped[type] = {};
|
||||
gamebook.addCollectionView(type, name);
|
||||
},
|
||||
|
||||
'add' : function(type, what) {
|
||||
this.collections[type].add(what);
|
||||
gamebook.updateCollectionsView();
|
||||
},
|
||||
|
||||
'drop' : function(type, what) {
|
||||
this.collections[type].drop(what);
|
||||
gamebook.updateCollectionsView();
|
||||
},
|
||||
|
||||
'has' : function(type, what) {
|
||||
return this.collections[type].has(what);
|
||||
}
|
|
@ -1,241 +1,5 @@
|
|||
var gamebook = {
|
||||
'player' : {
|
||||
'started' : false,
|
||||
'currentSection' : null,
|
||||
'collections' : {},
|
||||
|
||||
'collect' : function(type, name) {
|
||||
this.collections[type] = {
|
||||
'name' : name,
|
||||
'contents' : [],
|
||||
'add' : function(what) {
|
||||
if (this.contents.indexOf(what) === -1
|
||||
&& !(what in gamebook.dropped[type])) {
|
||||
this.contents.push(what);
|
||||
this.contents.sort();
|
||||
}
|
||||
},
|
||||
'drop' : function(what) {
|
||||
var i = this.contents.indexOf(what);
|
||||
if (i >= 0) {
|
||||
this.contents.splice(i, 1);
|
||||
gamebook.dropped[type][what] = true;
|
||||
}
|
||||
},
|
||||
'has' : function(what) {
|
||||
return this.contents.indexOf(what) >= 0;
|
||||
}
|
||||
};
|
||||
gamebook.dropped[type] = {};
|
||||
gamebook.addCollectionView(type, name);
|
||||
},
|
||||
|
||||
'add' : function(type, what) {
|
||||
this.collections[type].add(what);
|
||||
gamebook.updateCollectionsView();
|
||||
},
|
||||
|
||||
'drop' : function(type, what) {
|
||||
this.collections[type].drop(what);
|
||||
gamebook.updateCollectionsView();
|
||||
},
|
||||
|
||||
'has' : function(type, what) {
|
||||
return this.collections[type].has(what);
|
||||
}
|
||||
},
|
||||
|
||||
'sections' : {},
|
||||
|
||||
'turnToFunctions' : {},
|
||||
|
||||
'dropped' : {},
|
||||
|
||||
'addSection' : function(nr, element) {
|
||||
var section = {'element' : element, 'nr' : nr};
|
||||
this.sections[nr] = section;
|
||||
},
|
||||
|
||||
'turnTo' : function(nr) {
|
||||
if (!gamebook.player.started) {
|
||||
gamebook.start();
|
||||
}
|
||||
if (!nr in this.sections) {
|
||||
throw new Exception("Can not turn to non-existing section "
|
||||
+ nr + ".");
|
||||
}
|
||||
this.displaySection(nr);
|
||||
},
|
||||
|
||||
'start' : function() {
|
||||
this.hideIntroSections();
|
||||
this.addClassToClass('startlink', 'nodisplay');
|
||||
gamebook.player.started = true;
|
||||
},
|
||||
|
||||
'displaySection' : function(nr) {
|
||||
if (this.player.currentSection) {
|
||||
this.player.currentSection.element.style.display = 'none';
|
||||
}
|
||||
var e = this.sections[nr].element;
|
||||
this.runActions(e.getElementsByClassName('sectiontext')[0]);
|
||||
e.style.display = 'block';
|
||||
this.player.currentSection = gamebook.sections[nr];
|
||||
},
|
||||
|
||||
'hideIntroSections' : function() {
|
||||
this.addClassToClass('introsection', 'nodisplay');
|
||||
this.removeClassFromClass('displayintrolink', 'nodisplay');
|
||||
this.addClassToClass('hideintrolink', 'nodisplay');
|
||||
},
|
||||
|
||||
'showIntroSections' : function() {
|
||||
this.runActionsInIntroSections();
|
||||
this.removeClassFromClass('introsection', 'nodisplay');
|
||||
this.addClassToClass('displayintrolink', 'nodisplay');
|
||||
this.removeClassFromClass('hideintrolink', 'nodisplay');
|
||||
document.body.scrollIntoView();
|
||||
},
|
||||
|
||||
'runActionsInIntroSections' : function() {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName('introsectionbody'),
|
||||
gamebook.runActions);
|
||||
},
|
||||
|
||||
'addClassToClass' : function(className, addClass) {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName(className),
|
||||
function(e) {
|
||||
e.classList.add(addClass);
|
||||
});
|
||||
},
|
||||
|
||||
'removeClassFromClass' : function(className, removeClass) {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName(className),
|
||||
function(e) {
|
||||
e.classList.remove(removeClass);
|
||||
});
|
||||
},
|
||||
|
||||
'runActions' : function(e) {
|
||||
var enableNextLink = true;
|
||||
var hasXorScope = false;
|
||||
var hasAutoScope = false;
|
||||
var xorEnableNext = false;
|
||||
var autoDisableAllRemainingLinks = (
|
||||
gamebook.player.started && e.classList.contains('introsectionbody'));
|
||||
Array.prototype.forEach.call(e.childNodes, function(c) {
|
||||
if (!c.classList) {
|
||||
return;
|
||||
}
|
||||
if (c.classList.contains('sectionref')) {
|
||||
if (enableNextLink && !autoDisableAllRemainingLinks) {
|
||||
gamebook.enableLink(c);
|
||||
if (hasAutoScope) {
|
||||
autoDisableAllRemainingLinks = true;
|
||||
}
|
||||
} else {
|
||||
gamebook.disableLink(c);
|
||||
}
|
||||
enableNextLink = !(hasXorScope && !xorEnableNext);
|
||||
hasAutoScope = false;
|
||||
hasXorScope = false;
|
||||
} else if (c.classList.contains('collect')) {
|
||||
gamebook.player.collect(c.dataset.type, c.dataset.name);
|
||||
} else if (c.classList.contains('add')) {
|
||||
gamebook.player.add(c.dataset.type, c.dataset.what);
|
||||
} else if (c.classList.contains('drop')) {
|
||||
gamebook.player.drop(c.dataset.type, c.dataset.what);
|
||||
} else if (c.classList.contains('has')) {
|
||||
enableNextLink = gamebook.player.has(c.dataset.type,
|
||||
c.dataset.what);
|
||||
} else if (c.classList.contains('hasnot')) {
|
||||
enableNextLink = !gamebook.player.has(c.dataset.type,
|
||||
c.dataset.what);
|
||||
} else if (c.classList.contains('xor')) {
|
||||
hasXorScope = true;
|
||||
xorEnableNext = !enableNextLink;
|
||||
} else if (c.classList.contains('auto')) {
|
||||
hasAutoScope = true;
|
||||
} else if (c.classList.contains('random')) {
|
||||
c.addEventListener('click',
|
||||
gamebook.enableRandomLinkAfter);
|
||||
c.classList.add("enabledlink");
|
||||
c.classList.remove("disabledlink");
|
||||
autoDisableAllRemainingLinks = true;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
'enableLink' : function(e) {
|
||||
e.addEventListener('click',
|
||||
gamebook.getTurnToFunction(e.dataset.ref));
|
||||
e.classList.add("enabledlink");
|
||||
e.classList.remove("disabledlink");
|
||||
},
|
||||
|
||||
'disableLink' : function(e) {
|
||||
e.removeEventListener('click',
|
||||
gamebook.getTurnToFunction(e.dataset.ref));
|
||||
e.classList.remove("enabledlink");
|
||||
e.classList.add("disabledlink");
|
||||
},
|
||||
|
||||
'enableRandomLinkAfter' : function(event) {
|
||||
this.classList.remove("enabledlink");
|
||||
this.classList.add("disabledlink");
|
||||
var links = [];
|
||||
var e = this.nextSibling;
|
||||
while (e) {
|
||||
if (e.classList && e.classList.contains('sectionref')) {
|
||||
links.push(e);
|
||||
}
|
||||
e = e.nextSibling;
|
||||
}
|
||||
if (links.length > 0) {
|
||||
var selected = links[Math.floor(Math.random()*links.length)]
|
||||
gamebook.enableLink(selected);
|
||||
} else {
|
||||
console.log("Random with nothing to select?");
|
||||
}
|
||||
event.preventDefault();
|
||||
},
|
||||
|
||||
'addCollectionView' : function(type, name) {
|
||||
var ce = document.getElementById('collections');
|
||||
var template = document.getElementById('collectionTemplate');
|
||||
var e = template.cloneNode(true);
|
||||
e.className = "collection";
|
||||
e.getElementsByClassName('collectionheading')[0].innerHTML = name;
|
||||
e.dataset.type = type;
|
||||
ce.appendChild(e);
|
||||
},
|
||||
|
||||
'updateCollectionsView' : function() {
|
||||
var ce = document.getElementById('collections');
|
||||
Array.prototype.forEach.call(ce.childNodes, function(c) {
|
||||
if (c.className === 'collection') {
|
||||
var type = c.dataset.type;
|
||||
var collection = gamebook.player.collections[type];
|
||||
var cc = c.getElementsByClassName('collectioncontents')[0];
|
||||
cc.innerHTML = collection.contents.join(', ');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
'getTurnToFunction' : function(nr) {
|
||||
if (nr in this.turnToFunctions) {
|
||||
return this.turnToFunctions[nr];
|
||||
} else {
|
||||
var f = function () {
|
||||
gamebook.turnTo(nr);
|
||||
};
|
||||
this.turnToFunctions[nr] = f;
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
#include "gamebookscript"
|
||||
#include "extra-gamebookscript"
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue