5 * Created by Alyssa Milburn on Sat Jan 12 2008.
6 * Copyright (c) 2008 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "Blackboard.h"
22 #include "World.h" // setFocus
25 Blackboard::Blackboard(std::string spritefile
, unsigned int firstimage
, unsigned int imagecount
,
26 unsigned int tx
, unsigned int ty
, unsigned int bgcolour
, unsigned int ckcolour
,
27 unsigned int alcolour
) : CompoundAgent(spritefile
, firstimage
, imagecount
) {
28 textx
= tx
; texty
= ty
;
29 backgroundcolour
= bgcolour
; chalkcolour
= ckcolour
; aliascolour
= alcolour
;
33 if (engine
.version
== 1)
34 strings
.resize(16, std::pair
<unsigned int, std::string
>(0, std::string()));
36 strings
.resize(48, std::pair
<unsigned int, std::string
>(0, std::string()));
39 void Blackboard::addPart(CompoundPart
*p
) {
40 CompoundAgent::addPart(p
);
42 // if we're adding the first part..
43 if (parts
.size() == 1) {
44 // add the part responsible for text; id #10 keeps it safely out of the way
45 ourPart
= new BlackboardPart(this, 10);
50 void Blackboard::showText(bool show
) {
51 if (editing
) stopEditing(false);
53 if (show
&& var
[0].hasInt() && var
[0].getInt() >= 0 && (unsigned int)var
[0].getInt() < strings
.size()) {
54 currenttext
= strings
[var
[0].getInt()].second
;
60 void Blackboard::addBlackboardString(unsigned int n
, unsigned int id
, std::string text
) {
61 strings
[n
] = std::pair
<unsigned int, std::string
>(id
, text
);
64 void Blackboard::renderText(class Surface
*renderer
, int xoffset
, int yoffset
) {
65 std::string ourtext
= currenttext
;
66 if (editing
) ourtext
+= "_"; // TODO: should this be rendered in aliascolour?
68 // TODO: is +1 really the right fix here?
69 renderer
->renderText(xoffset
+ textx
+ 1, yoffset
+ texty
+ 1, ourtext
, chalkcolour
, backgroundcolour
);
72 void Blackboard::startEditing() {
75 if (var
[0].hasInt() && var
[0].getInt() >= 0 && (unsigned int)var
[0].getInt() < strings
.size()) {
77 editingindex
= var
[0].getInt();
78 strings
[editingindex
].second
= currenttext
= "";
80 // TODO: this will probably be thrown all the way to main() :-(
81 throw creaturesException("tried to start editing a blackboard with invalid var0");
85 void Blackboard::stopEditing(bool losingfocus
) {
88 if (!losingfocus
&& world
.focusagent
== AgentRef(this)) {
89 world
.setFocus(0); // this will call us again via loseFocus() on the part
96 BlackboardPart::BlackboardPart(Blackboard
*p
, unsigned int _id
) : CompoundPart(p
, _id
, 0, 0, 1) {
97 // TODO: think about plane
100 void BlackboardPart::partRender(class Surface
*renderer
, int xoffset
, int yoffset
) {
101 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
102 bbd
->renderText(renderer
, xoffset
, yoffset
);
105 void BlackboardPart::gainFocus() {
106 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
110 void BlackboardPart::loseFocus() {
111 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
112 bbd
->stopEditing(true);
115 void BlackboardPart::handleKey(char c
) {
116 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
118 // strip non-alpha chars
119 // TODO: internationalisation?
120 if (!((c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z'))) return;
122 std::string
&s
= bbd
->strings
[bbd
->editingindex
].second
;
125 bbd
->currenttext
= s
;
129 void BlackboardPart::handleSpecialKey(char c
) {
130 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
134 if (bbd
->currenttext
.size() == 0) return;
135 { std::string
&s
= bbd
->strings
[bbd
->editingindex
].second
;
136 s
.erase(s
.begin() + (s
.size() - 1));
137 bbd
->currenttext
= s
; }
141 bbd
->stopEditing(false);