| 1 |
/* |
| 2 |
Copyright (C) 2002 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Dlgedit is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file dlg_circle.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief The node containing the actual dialogue text. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "dlg_circle.h" |
| 27 |
#include "gui_resources.h" |
| 28 |
|
| 29 |
// Constructor |
| 30 |
DlgCircle::DlgCircle (DlgPoint &p, node_type t, int nid, int mid, DlgCircleEntry *e) |
| 31 |
{ |
| 32 |
type_ = t; |
| 33 |
nid_ = nid; |
| 34 |
mid_ = mid; |
| 35 |
entry_ = (e ? e : new DlgCircleEntry); |
| 36 |
|
| 37 |
// Align Circle to the (imaginary) grid |
| 38 |
top_left = DlgPoint (p.x () - (p.x () % CIRCLE_DIAMETER), p.y () - (p.y () % CIRCLE_DIAMETER)); |
| 39 |
bottom_right = DlgPoint (x () + CIRCLE_DIAMETER, y () + CIRCLE_DIAMETER); |
| 40 |
|
| 41 |
// make sure the point lies still within the circle |
| 42 |
p = center (); |
| 43 |
} |
| 44 |
|
| 45 |
// dtor |
| 46 |
DlgCircle::~DlgCircle () |
| 47 |
{ |
| 48 |
if (entry_) delete entry_; |
| 49 |
} |
| 50 |
|
| 51 |
// get a certain parent-circle of this circle |
| 52 |
DlgCircle *DlgCircle::parent (query_type pos, int offset) |
| 53 |
{ |
| 54 |
DlgNode *arrow = prev (pos, offset); |
| 55 |
if (arrow != NULL) return (DlgCircle *) arrow->prev (FIRST); |
| 56 |
else return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
// get a certain child-circle of this circle |
| 60 |
DlgCircle *DlgCircle::child (query_type pos, int offset) |
| 61 |
{ |
| 62 |
DlgNode *arrow = next (pos, offset); |
| 63 |
if (arrow != NULL) return (DlgCircle *) arrow->next (FIRST); |
| 64 |
else return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
// check whether the given node is a child of this circle |
| 68 |
bool DlgCircle::hasChild (DlgNode *child) |
| 69 |
{ |
| 70 |
std::list<DlgNode*>::iterator j; |
| 71 |
|
| 72 |
for (j = next_.begin (); j != next_.end (); j++) |
| 73 |
if (child == (*j)->next (FIRST)) |
| 74 |
return true; |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
// draw the circle |
| 80 |
void DlgCircle::draw (cairo_surface_t *surface, DlgPoint &os, GtkWidget *widget) |
| 81 |
{ |
| 82 |
cairo_t *cr = cairo_create (surface); |
| 83 |
|
| 84 |
// get the color and fill for drawing the circle |
| 85 |
const GdkColor *gc = GuiResources::getColor (mode_, type_); |
| 86 |
const GdkColor *fgc = GuiResources::getFill (mode_, type_); |
| 87 |
|
| 88 |
// offset circle |
| 89 |
DlgPoint position = center ().offset (os); |
| 90 |
DlgPoint tmp = topLeft ().offset (os); |
| 91 |
DlgRect area (tmp, width () + 1, height () + 1); |
| 92 |
|
| 93 |
// draw everything to the surface - First border |
| 94 |
drawCircle (cr, gc, TRUE, position.x (), position.y (), CIRCLE_RADIUS); |
| 95 |
// add a small circle inside that indicates the type (NPC, Player) |
| 96 |
drawCircle (cr, fgc, TRUE, position.x (), position.y (), CIRCLE_RADIUS-2); |
| 97 |
|
| 98 |
// Indicate whether node contains additional code |
| 99 |
if (hasCode () || entry_->loop ()) |
| 100 |
{ |
| 101 |
GString *code = g_string_sized_new (2); |
| 102 |
|
| 103 |
if (hasCode ()) g_string_append_c (code, '!'); |
| 104 |
if (entry_->loop ()) g_string_append_c (code, 'o'); |
| 105 |
|
| 106 |
// get the font to use |
| 107 |
const PangoFontDescription *desc = pango_layout_get_font_description (GuiResources::font ()); |
| 108 |
|
| 109 |
// create pango cairo compatible layout |
| 110 |
PangoLayout *font = pango_cairo_create_layout(cr); |
| 111 |
pango_layout_set_font_description (font, desc); |
| 112 |
pango_layout_set_text (font, code->str, -1); |
| 113 |
|
| 114 |
// place text in circles center |
| 115 |
int w, h; |
| 116 |
pango_layout_get_pixel_size (font, &w, &h); |
| 117 |
|
| 118 |
int x = tmp.x () + (width () - w) / 2; |
| 119 |
int y = tmp.y () + (height () + 1 - h) / 2; |
| 120 |
|
| 121 |
// set font color and position |
| 122 |
gdk_cairo_set_source_color(cr, gc); |
| 123 |
pango_cairo_update_layout (cr, font); |
| 124 |
cairo_move_to(cr, x, y); |
| 125 |
|
| 126 |
// draw text |
| 127 |
pango_cairo_show_layout (cr, font); |
| 128 |
g_object_unref(font); |
| 129 |
|
| 130 |
g_string_free (code, TRUE); |
| 131 |
} |
| 132 |
|
| 133 |
// Update the drawing area |
| 134 |
update (widget, area); |
| 135 |
|
| 136 |
cairo_destroy(cr); |
| 137 |
} |
| 138 |
|
| 139 |
// load a circle from a file |
| 140 |
bool DlgCircle::load () |
| 141 |
{ |
| 142 |
entry_ = new DlgCircleEntry; |
| 143 |
std::string str; |
| 144 |
int n; |
| 145 |
|
| 146 |
// as long as we haven't reached EOF or are finished with loading: |
| 147 |
while (1) |
| 148 |
{ |
| 149 |
// look what we find in the file |
| 150 |
switch (parse_dlgfile (str, n)) |
| 151 |
{ |
| 152 |
// EOF or finished |
| 153 |
case 0: |
| 154 |
case LOAD_END: return true; |
| 155 |
|
| 156 |
// Type of node |
| 157 |
case LOAD_TYPE: |
| 158 |
{ |
| 159 |
if (parse_dlgfile (str, n) == LOAD_NUM) type_ = (node_type) n; |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
// Loop |
| 164 |
case LOAD_LOOP: |
| 165 |
{ |
| 166 |
if (parse_dlgfile (str, n) == LOAD_NUM) entry_->setLoop (n); |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
// Module and Node id of Circle |
| 171 |
case LOAD_ID: |
| 172 |
{ |
| 173 |
if (parse_dlgfile (str, n) == LOAD_NUM) nid_ = n; |
| 174 |
|
| 175 |
break; |
| 176 |
} |
| 177 |
|
| 178 |
// Coordinates of Circle |
| 179 |
case LOAD_POS: |
| 180 |
{ |
| 181 |
int px, py; |
| 182 |
if (parse_dlgfile (str, n) == LOAD_NUM) px = n; |
| 183 |
if (parse_dlgfile (str, n) == LOAD_NUM) py = n; |
| 184 |
|
| 185 |
// Align Circle to the (imaginary) grid |
| 186 |
top_left = DlgPoint (px - (px % CIRCLE_DIAMETER), py - (py % CIRCLE_DIAMETER)); |
| 187 |
bottom_right = DlgPoint (x () + CIRCLE_DIAMETER, y () + CIRCLE_DIAMETER); |
| 188 |
|
| 189 |
break; |
| 190 |
} |
| 191 |
|
| 192 |
// The Circle's Text |
| 193 |
case LOAD_TEXT: |
| 194 |
{ |
| 195 |
if (parse_dlgfile (str, n) == LOAD_STR) entry_->setText (str); |
| 196 |
break; |
| 197 |
} |
| 198 |
|
| 199 |
// The Circle's Annotations |
| 200 |
case LOAD_NOTE: |
| 201 |
{ |
| 202 |
if (parse_dlgfile (str, n) == LOAD_STR) entry_->setAnnotation (str); |
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
// The Circle's Character |
| 207 |
case LOAD_NPC: |
| 208 |
{ |
| 209 |
if (parse_dlgfile (str, n) == LOAD_STR) entry_->setNpc (str); |
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
// The Circle's Conditions |
| 214 |
case LOAD_COND: |
| 215 |
{ |
| 216 |
if (parse_dlgfile (str, n) == LOAD_STR) entry_->setCondition (str); |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
// The Circle's Variables |
| 221 |
case LOAD_VARS: |
| 222 |
{ |
| 223 |
if (parse_dlgfile (str, n) == LOAD_STR) entry_->setCode (str); |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
// Just ignore everything else |
| 228 |
default: break; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
// save all data neccessary to restore the circle |
| 236 |
void DlgCircle::save (std::ofstream &file) |
| 237 |
{ |
| 238 |
// Keyword "Circle" and circle's number |
| 239 |
file << "\nCircle\n"; |
| 240 |
|
| 241 |
// circle's type |
| 242 |
file << " Type " << (int) type () << "\n"; |
| 243 |
|
| 244 |
// circle's id |
| 245 |
file << " Id " << nid_ << "\n"; |
| 246 |
|
| 247 |
// circle's position |
| 248 |
file << " Pos " << x () << " " << y () << "\n"; |
| 249 |
|
| 250 |
// circle's annotation |
| 251 |
if (entry_->annotation () != "") |
| 252 |
file << " Note \xa7" << entry_->annotation () << "\xa7\n"; |
| 253 |
|
| 254 |
// circle's text |
| 255 |
file << " Text \xa7" << entry_->text () << "\xa7\n"; |
| 256 |
|
| 257 |
// circle's character |
| 258 |
if (type_ == NPC && entry_->npc () != "Default") |
| 259 |
file << " NPC \xa7" << entry_->npc () << "\xa7\n"; |
| 260 |
|
| 261 |
// circle's condition(s) |
| 262 |
if (entry_->condition () != "") |
| 263 |
file << " Cond \xa7" << entry_->condition () << "\xa7\n"; |
| 264 |
|
| 265 |
// circle's code |
| 266 |
if (entry_->code () != "") |
| 267 |
file << " Vars \xa7" << entry_->code () << "\xa7\n"; |
| 268 |
|
| 269 |
// loop |
| 270 |
if (entry_->loop () != false) |
| 271 |
file << " Loop 1\n"; |
| 272 |
|
| 273 |
file << "End\n"; |
| 274 |
} |
| 275 |
|
| 276 |
// get the text of a circle |
| 277 |
std::string DlgCircle::text () |
| 278 |
{ |
| 279 |
std::string text = ""; |
| 280 |
|
| 281 |
// is there any text at all? |
| 282 |
if (entry_ == NULL) return text; |
| 283 |
|
| 284 |
// is a certain NPC attached? |
| 285 |
if (type_ == NPC && entry_->npc () != "Default") |
| 286 |
{ |
| 287 |
text += entry_->npc (); |
| 288 |
text += ": "; |
| 289 |
} |
| 290 |
|
| 291 |
// append actual text and return it |
| 292 |
return text += entry_->text (); |
| 293 |
} |
| 294 |
|
| 295 |
// get tooltip text of the circle |
| 296 |
std::string DlgCircle::tooltip () |
| 297 |
{ |
| 298 |
std::string text = ""; |
| 299 |
|
| 300 |
// is there any text at all? |
| 301 |
if (entry_ == NULL) return text; |
| 302 |
|
| 303 |
// are there a one or more conditions? |
| 304 |
if (entry_->condition () != "") |
| 305 |
{ |
| 306 |
text += entry_->condition (); |
| 307 |
text += "\n\n"; |
| 308 |
} |
| 309 |
|
| 310 |
// append text |
| 311 |
text += DlgCircle::text (); |
| 312 |
|
| 313 |
// is there further code? |
| 314 |
if (entry_->code () != "") |
| 315 |
{ |
| 316 |
text += "\n\n"; |
| 317 |
text += entry_->code (); |
| 318 |
} |
| 319 |
|
| 320 |
return text; |
| 321 |
} |