qtgui: minor include fiddling
[openc2e.git] / Bubble.cpp
blob8d45a0841f75047e0c487248f817ef6c302a7759
1 /*
2 * Bubble.cpp
3 * openc2e
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.
20 #include "Bubble.h"
21 #include "World.h"
22 #include "Engine.h"
23 #include "Backend.h"
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);
33 addPart(ourPart);
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) {
43 ourPart->setText(s);
46 std::string Bubble::getText() {
47 return ourPart->text;
50 void Bubble::setEditing(bool e) {
51 ourPart->editable = e;
52 if (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) {
59 timeout = t;
62 void Bubble::tick() {
63 CompoundAgent::tick();
65 if (!paused && timeout) {
66 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!!!!
75 // TODO: C2 support
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
90 if (leftside)
91 ourSpeechBubble->moveTo(world.hand()->x + world.hand()->getWidth() - 2, world.hand()->y - getHeight());
92 else
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
101 kill();
105 class BubblePart : public CompoundPart {
106 bool editable;
107 std::string text;
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) {
113 editable = false;
114 textwidth = 0;
115 textheight = 0;
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() {
124 assert(editable);
127 void BubblePart::loseFocus() {
128 parent->kill();
131 void BubblePart::handleKey(char c) {
132 // TODO: reject invalid chars
134 setText(text + c);
137 void BubblePart::handleSpecialKey(char c) {
138 switch (c) {
139 case 8: // backspace
140 if (text.size() == 0) { loseFocus(); return; }
141 { std::string s = text;
142 s.erase(s.begin() + (s.size() - 1));
143 setText(s); }
144 if (text.size() == 0) { loseFocus(); return; }
145 break;
147 case 13: // return
148 ((Bubble *)parent)->turnIntoSpeech(); // TODO: omg hax
149 break;
153 void BubblePart::setText(std::string s) {
154 unsigned int twidth = engine.backend->textWidth(s);
155 if (twidth > textwidth) return;
157 text = s;
158 textoffset = (textwidth - twidth) / 2;
161 /* vim: set noet: */