📄 objectmanager.cpp
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006 Daniel Lindstr鰉 and Daniel Nilsson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "StdAfx.h"
#include "ObjectManager.h"
ObjectManager::ObjectManager(int clientid) {
int l, i;
for (l = 0; l < NUM_LAYERS; l++) {
counts[l] = 0;
for (i = 0; i < MAX_OBJECTS; i++) {
objects[l][i] = NULL;
}
}
map = new Map<Object *>(NULL);
oid_count = 0;
this->clientid = clientid;
InitializeCriticalSection(&lock);
}
ObjectManager::~ObjectManager(void) {
delete(map);
DeleteCriticalSection(&lock);
}
void ObjectManager::setCanvas(Canvas *c) {
canvas = c;
}
Iterator *ObjectManager::getIterator(int layer) {
return new Iterator(this, layer);
}
int ObjectManager::addObject(Object *obj, int layer){
int id = oid_count | (clientid << 16);
int ret = this->addObject(obj, layer, id);
oid_count++;
return(ret);
}
int ObjectManager::addObject(Object *obj, int layer, int oid) {
if (counts[layer] >= MAX_OBJECTS)
throw "Too many objects in addObject!";
EnterCriticalSection(&lock);
int i = counts[layer]++;
objects[layer][i] = obj;
map->put(oid, obj);
LeaveCriticalSection(&lock);
canvas->objectAdded(obj);
return(oid);
}
Object *ObjectManager::getObject(int oid){
Object *ret;
EnterCriticalSection(&lock);
ret = map->get(oid);
LeaveCriticalSection(&lock);
return ret;
}
void ObjectManager::deleteObject(Object *obj) {
int l, i, oid;
EnterCriticalSection(&lock);
for (l = 0; l < NUM_LAYERS; l++) {
for (i = 0; i < counts[l]; i++) {
if (objects[l][i] == obj) {
counts[l]--;
// Compact the array...
memmove(&(objects[l][i]), &(objects[l][i+1]), (counts[l]-i) * sizeof(Object *));
objects[l][counts[i]] = NULL;
if (map->findKey(&oid, obj)) {
map->remove(oid);
}
LeaveCriticalSection(&lock);
FRECT frect;
obj->getBounds(&frect);
canvas->objectDeleted(&frect);
return;
}
}
}
LeaveCriticalSection(&lock);
throw "Object not found in deleteObject!";
}
void ObjectManager::objectMoved(Object *obj, FRECT *oldrect) {
canvas->objectMoved(obj, oldrect);
}
void ObjectManager::objectChanged(Object *obj, FRECT *changed) {
canvas->objectChanged(obj, changed);
}
void ObjectManager::clearLayer(LAYER layer){
int oid;
EnterCriticalSection(&lock);
for(int i = 0; i<counts[layer]; i++){
if (map->findKey(&oid, objects[layer][i])) {
map->remove(oid);
}
delete(objects[layer][i]);
objects[layer][i] = NULL;
}
counts[layer] = 0;
LeaveCriticalSection(&lock);
canvas->redraw();
}
// class Iterator
Iterator::Iterator(ObjectManager *om, int l) {
objman = om;
layer = l;
pos = 0;
}
Iterator::~Iterator(void) {
}
Object *Iterator::getNext(void) {
Object *ret = NULL;
EnterCriticalSection(&objman->lock);
if (pos < objman->counts[layer])
ret = objman->objects[layer][pos++];
LeaveCriticalSection(&objman->lock);
return ret;
}
bool Iterator::hasNext(void) {
bool ret;
EnterCriticalSection(&objman->lock);
ret = pos < objman->counts[layer];
LeaveCriticalSection(&objman->lock);
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -