5 * Created by Alyssa Milburn on Sat Apr 26 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.
25 // class BubblePart *ourPart;
27 Bubble::Bubble(unsigned char family
, unsigned char genus
, unsigned short species
, unsigned int plane
,
28 std::string spritefile
, unsigned int firstimage
, unsigned int imagecount
,
29 unsigned int tx
, unsigned int ty
, unsigned int twidth
, unsigned int theight
,
30 unsigned int bgcolour
, unsigned int tcolour
)
31 : CompoundAgent(family
, genus
, species
, plane
, spritefile
, firstimage
, imagecount
) {
32 ourPart
= new BubblePart(this, 1, tx
, ty
);
34 ourPart
->textwidth
= twidth
;
35 ourPart
->textheight
= theight
;
36 ourPart
->backgroundcolour
= bgcolour
;
37 ourPart
->textcolour
= tcolour
;
39 ourPart
->editable
= false;
42 void Bubble::setText(std::string s
) {
46 std::string
Bubble::getText() {
50 void Bubble::setEditing(bool e
) {
51 ourPart
->editable
= e
;
53 world
.setFocus(ourPart
); // gain focus
54 else if (world
.focusagent
== AgentRef(this) && world
.focuspart
== ourPart
->id
)
55 world
.setFocus(0); // lose focus
58 void Bubble::setTimeout(unsigned int t
) {
63 CompoundAgent::tick();
65 if (!paused
&& timeout
) {
67 if (timeout
== 0) kill();
71 #include "PointerAgent.h"
72 void Bubble::turnIntoSpeech() {
73 // TODO: this should really really really be handled elsewhere, not in Bubble!!!!
76 assert(engine
.version
== 1);
78 bool leftside
= false;
79 // TODO: cope with wrap
80 if (world
.hand()->x
- world
.camera
.getX() < world
.camera
.getWidth() / 2) leftside
= true;
82 // TODO: are 1/0 good colours?
83 Bubble
*ourSpeechBubble
= new Bubble(2, 1, 2, 9000, "syst", leftside
? 10 : 9, 1, 6, 3, 144, 12, 1, 0);
84 ourSpeechBubble
->finishInit();
86 ourSpeechBubble
->attr
= 32; // floating
87 ourSpeechBubble
->floatTo(world
.hand());
89 // TODO: fix positioning
91 ourSpeechBubble
->moveTo(world
.hand()->x
+ world
.hand()->getWidth() - 2, world
.hand()->y
- getHeight());
93 ourSpeechBubble
->moveTo(world
.hand()->x
- getWidth() + 2, world
.hand()->y
- getHeight());
95 ourSpeechBubble
->setText(getText());
97 ourSpeechBubble
->setTimeout(2 * 10); // TODO: 2s is probably not right
98 // TODO: announce via shou
99 // TODO: add to speech history
105 class BubblePart : public CompoundPart {
108 unsigned int textwidth, textheight;
109 unsigned int backgroundcolour, textcolour;
112 BubblePart::BubblePart(Bubble
*p
, unsigned int _id
, int x
, int y
) : CompoundPart(p
, _id
, x
, y
, 1) {
116 textoffset
= 0; // doesn't matter when text is empty
119 void BubblePart::partRender(class Surface
*renderer
, int xoffset
, int yoffset
) {
120 renderer
->renderText(xoffset
+ x
+ textoffset
, yoffset
+ y
, text
, textcolour
, backgroundcolour
);
123 void BubblePart::gainFocus() {
127 void BubblePart::loseFocus() {
131 void BubblePart::handleKey(char c
) {
132 // TODO: reject invalid chars
137 void BubblePart::handleSpecialKey(char c
) {
140 if (text
.size() == 0) { loseFocus(); return; }
141 { std::string s
= text
;
142 s
.erase(s
.begin() + (s
.size() - 1));
144 if (text
.size() == 0) { loseFocus(); return; }
148 ((Bubble
*)parent
)->turnIntoSpeech(); // TODO: omg hax
153 void BubblePart::setText(std::string s
) {
154 unsigned int twidth
= engine
.backend
->textWidth(s
);
155 if (twidth
> textwidth
) return;
158 textoffset
= (textwidth
- twidth
) / 2;