-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_rectangle.cpp
102 lines (92 loc) · 2.91 KB
/
bubble_rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "bubble_rectangle.hpp"
#include <Imlib2.h>
//#include <stdlib.h>
//#include <math.h>
#include <iostream>
#include <stdio.h>
#include "utils.hpp"
BubbleRectangle::BubbleRectangle(int _x0, int _y0, int _width, int _height,
CFont *_font, Color *_bgcolor)
: Bubble(_font, _bgcolor, ""),
x0(_x0), y0(_y0), width(_width), height(_height)
{
font = _font;
bgcolor = _bgcolor;
if (width < 8) std::cerr<< "Warning: Rectangle Width " <<width <<" too small.\n";
if (height < 8) std::cerr<< "Warning: Rectangle Height "<<height<<" too small.\n";
}
bool BubbleRectangle::contains(int x, int y) const {
if (x0 <= x && x < x0+width &&
y0 <= y && y < y0+height) {
return true;
} else return false;
}
int BubbleRectangle::renderText() const {
// Text schreiben
string rest = text;
int fontsize = static_cast<int>(font->size);
int txtheight = 0.8*fontsize;
font->use();
while(rest.length() > 0) {
if (txtheight > height) {
std::cerr<< "Error: text does not fit, aborting: "<< text <<".\n";
break;
}
rest = drawTextLine(x0, y0+txtheight, rest, width, txtheight/(float)width, false);
txtheight += fontsize*1.7;
}
return txtheight;
}
void BubbleRectangle::writeImage() const {
bgcolor->use();
imlib_image_fill_rectangle(x0, y0, width, height);
this->renderText();
}
void BubbleRectangle::writeXML(std::ostream& str, uint16_t indent) const {
str << utils::indent(indent) << "<rectangle "
<< "x0=\""<< x0 <<"\" "
<< "y0=\""<< y0 <<"\" "
<< "width=\""<< width <<"\" "
<< "height=\""<< height <<"\" "
<< "font=\"default\" "
<< "bgcolor=\"default\">"
<< text
<< "</rectangle>\n";
}
void BubbleRectangle::writeJSON(std::ostream& str, uint16_t indent) const {
str << utils::indent(indent) << "\"{rectangle\":{\n" << utils::indent(indent)
<< "\"x0\": " << x0 << ", "
<< "\"y0\": " << y0 << ", "
<< "\"width\": " << width << ", "
<< "\"height\": " << height << ", "
<< "\"font\": \"default\", "
<< "\"bgcolor\": \"default\", "
<< "\"text\": \"" << text << "\"}},\n";
}
void BubbleRectangle::writeYAML(std::ostream& str, uint16_t indent) const {
str << utils::indent(indent) << "- rectangle\n"
<< utils::indent(indent) << " x0: " << x0 <<"\n"
<< utils::indent(indent) << " y0: " << y0 <<"\n"
<< utils::indent(indent) << " width: " << width <<"\n"
<< utils::indent(indent) << " height: " << height <<"\n"
<< utils::indent(indent) << " font: default\n"
<< utils::indent(indent) << " bgcolor: default\n"
<< utils::indent(indent) << " text: " << text << "\n";
}
void BubbleRectangle::draw(Bubble::DrawMode mode) const {
switch(mode) {
case OUTLINE:
imlib_context_set_color(128,0,0,128);
break;
case FILL:
bgcolor->use();
case ALL:
bgcolor->use();
}
imlib_image_fill_rectangle(x0, y0, width, height);
this->renderText();
}
void BubbleRectangle::setPosition(int x, int y) {
x0 = x - width/2;
y0 = y - height/2;
}