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 std::string
Blackboard::getText() {
51 if (var
[0].hasInt() && var
[0].getInt() >= 0 && (unsigned int)var
[0].getInt() < strings
.size())
52 return strings
[var
[0].getInt()].second
;
57 void Blackboard::showText(bool show
) {
58 if (editing
) stopEditing(false);
61 currenttext
= getText();
67 void Blackboard::addBlackboardString(unsigned int n
, unsigned int id
, std::string text
) {
68 strings
[n
] = std::pair
<unsigned int, std::string
>(id
, text
);
71 void Blackboard::renderText(class Surface
*renderer
, int xoffset
, int yoffset
) {
72 std::string ourtext
= currenttext
;
73 if (editing
) ourtext
+= "_"; // TODO: should this be rendered in aliascolour?
75 // TODO: is +1 really the right fix here?
76 renderer
->renderText(xoffset
+ textx
+ 1, yoffset
+ texty
+ 1, ourtext
, chalkcolour
, backgroundcolour
);
79 void Blackboard::startEditing() {
82 if (var
[0].hasInt() && var
[0].getInt() >= 0 && (unsigned int)var
[0].getInt() < strings
.size()) {
84 editingindex
= var
[0].getInt();
85 strings
[editingindex
].second
= currenttext
= "";
87 // TODO: this will probably be thrown all the way to main() :-(
88 throw creaturesException("tried to start editing a blackboard with invalid var0");
92 void Blackboard::stopEditing(bool losingfocus
) {
95 if (!losingfocus
&& world
.focusagent
== AgentRef(this)) {
96 world
.setFocus(0); // this will call us again via loseFocus() on the part
104 void Blackboard::broadcast(bool audible
) {
105 // TODO: blackboard broadcasts
108 Bubble::newBubble(this, true, currenttext
);
112 BlackboardPart::BlackboardPart(Blackboard
*p
, unsigned int _id
) : CompoundPart(p
, _id
, 0, 0, 1) {
113 // TODO: think about plane
116 void BlackboardPart::partRender(class Surface
*renderer
, int xoffset
, int yoffset
) {
117 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
118 bbd
->renderText(renderer
, xoffset
, yoffset
);
121 void BlackboardPart::gainFocus() {
122 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
126 void BlackboardPart::loseFocus() {
127 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
128 bbd
->stopEditing(true);
131 void BlackboardPart::handleKey(char c
) {
132 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
134 // strip non-alpha chars
135 // TODO: internationalisation?
136 if (!((c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z'))) return;
138 std::string
&s
= bbd
->strings
[bbd
->editingindex
].second
;
141 bbd
->currenttext
= s
;
145 void BlackboardPart::handleSpecialKey(char c
) {
146 Blackboard
*bbd
= dynamic_cast<Blackboard
*>(parent
);
150 if (bbd
->currenttext
.size() == 0) return;
151 { std::string
&s
= bbd
->strings
[bbd
->editingindex
].second
;
152 s
.erase(s
.begin() + (s
.size() - 1));
153 bbd
->currenttext
= s
; }
157 bbd
->stopEditing(false);