allow any CompoundPart to gain focus, if it canGainFocus()
[openc2e.git] / MetaRoom.cpp
blob0ae0cf0cb520610c88e531ad24abcf29e6ef123d
1 /*
2 * MetaRoom.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Tue May 25 2004.
6 * Copyright (c) 2004 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 "MetaRoom.h"
21 #include "Room.h"
22 #include "World.h"
23 #include "Engine.h"
24 #include "blkImage.h"
25 #include <assert.h>
26 #include "Backend.h"
28 MetaRoom::MetaRoom(int _x, int _y, int _width, int _height, const std::string &back, shared_ptr<creaturesImage> spr, bool wrap) {
29 xloc = _x; yloc = _y; wid = _width; hei = _height; wraps = wrap;
31 // if we were provided with a background, add it
32 if (!back.empty()) {
33 if (spr) {
34 addBackground(back, spr);
35 } else {
36 addBackground(back);
41 void MetaRoom::addBackground(std::string back, shared_ptr<creaturesImage> spr) {
42 shared_ptr<creaturesImage> backsprite;
43 unsigned int totalwidth, totalheight;
45 caos_assert(!back.empty());
46 // TODO: cadv adds backgrounds which have already been added as the default, look into this,
47 // should we preserve the default once extra backgrounds have been added and change this to
48 // a caos_assert?
49 if (backgrounds.find(back) != backgrounds.end()) return;
51 if (!spr) {
52 // we weren't passed a sprite, so we need to load one
53 backsprite = world.gallery.getImage(back, true);
54 blkImage *background = dynamic_cast<blkImage *>(backsprite.get());
55 if (!background && engine.bmprenderer) {
56 totalwidth = backsprite->width(0);
57 totalheight = backsprite->height(0);
58 } else {
59 caos_assert(background);
61 totalwidth = background->totalwidth;
62 totalheight = background->totalheight;
64 } else {
65 // we were provided with a sprite, so use it
66 backsprite = spr;
67 totalwidth = wid;
68 totalheight = hei;
71 // store the background
72 backgrounds[back] = backsprite;
73 if (!firstback) {
74 // set the first background
75 firstback = backsprite;
76 fullwid = totalwidth;
77 fullhei = totalheight;
78 } else {
79 // make sure other backgrounds are the same size
80 if (world.gametype == "sm") return; // TODO: seamonkeys fails the background size checks :/
81 assert(totalwidth == fullwid);
82 assert(totalheight == fullhei);
86 std::vector<std::string> MetaRoom::backgroundList() {
87 // construct a temporary vector from our std::map
89 std::vector<std::string> b;
90 for (std::map<std::string, shared_ptr<creaturesImage> >::iterator i = backgrounds.begin(); i != backgrounds.end(); i++)
91 b.push_back(i->first);
92 return b;
95 shared_ptr<creaturesImage> MetaRoom::getBackground(std::string back) {
96 // return the first background by default
97 if (back.empty()) {
98 return firstback;
101 // if this background name isn't found, return null
102 if (backgrounds.find(back) != backgrounds.end()) return shared_ptr<creaturesImage>();
104 // otherwise, return the relevant background
105 return backgrounds[back];
108 MetaRoom::~MetaRoom() {
109 // we hold the only strong reference to our contained rooms, so they'll be auto-deleted
112 shared_ptr<Room> MetaRoom::nextFloorFromPoint(float x, float y) {
113 shared_ptr<Room> closest_up, closest_down;
114 float dist_down = -1, dist_up = -1;
115 for (std::vector<shared_ptr<Room> >::iterator r = rooms.begin(); r != rooms.end(); r++) {
116 if (!(*r)->bot.containsX(x)) continue;
117 float dist = (*r)->bot.pointAtX(x).y - y; // down is positive
118 float absdist = fabs(dist);
119 if (dist >= 0 && (absdist < dist_down || dist_down < 0)) {
120 dist_down = absdist;
121 closest_down = *r;
122 } else if (dist < 0 && (absdist < dist_up || dist_up < 0)) {
123 dist_up = absdist;
124 closest_up = *r;
127 if (closest_down) return closest_down;
128 if (closest_up) return closest_up;
129 return shared_ptr<Room>();
132 unsigned int MetaRoom::addRoom(shared_ptr<Room> r) {
133 // add to both our local list and the global list
134 rooms.push_back(r);
135 world.map.rooms.push_back(r);
137 // set the id and return
138 r->id = world.map.room_base++;
139 return r->id;
142 shared_ptr<Room> MetaRoom::roomAt(float _x, float _y) {
143 if (wraps) {
144 if (_x > (int)xloc + (int)wid) _x -= wid;
145 else if (_x < (int)xloc) _x += wid;
148 for (std::vector<shared_ptr<Room> >::iterator i = rooms.begin(); i != rooms.end(); i++) {
149 shared_ptr<Room> r = *i;
150 if (r->containsPoint(_x, _y)) return r;
153 return shared_ptr<Room>();
156 std::vector<shared_ptr<Room> > MetaRoom::roomsAt(float _x, float _y) {
157 if (wraps) {
158 if (_x > (int)xloc + (int)wid) _x -= wid;
159 else if (_x < (int)xloc) _x += wid;
162 std::vector<shared_ptr<Room> > ourlist;
164 for (std::vector<shared_ptr<Room> >::iterator i = rooms.begin(); i != rooms.end(); i++) {
165 shared_ptr<Room> r = *i;
166 if (r->containsPoint(_x, _y)) ourlist.push_back(r);
169 return ourlist;
172 /* vim: set noet: */