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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/***********************************************************************
filename: InventoryReceiver.cpp
created: Fri Apr 22 2011
author: Paul D Turner <paul@cegui.org.uk>
*************************************************************************/
/***************************************************************************
* Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
***************************************************************************/
#include "InventoryReceiver.h"
#include "InventoryItem.h"
#include <CEGUIPropertyHelper.h>
#include <CEGUIImage.h>
// Start of CEGUI namespace section
namespace CEGUI
{
//------------------------------------------------------------------------------//
const String InventoryReceiver::WidgetTypeName("InventoryReceiver");
const String InventoryReceiver::EventNamespace("InventoryReceiver");
//------------------------------------------------------------------------------//
InventoryReceiver::InventoryReceiver(const String& type, const String& name) :
Window(type, name)
{
setDragDropTarget(true);
}
//------------------------------------------------------------------------------//
bool InventoryReceiver::addItemAtLocation(InventoryItem& item, int x, int y)
{
if (itemWillFitAtLocation(item, x, y))
{
InventoryReceiver* old_receiver =
dynamic_cast<InventoryReceiver*>(item.getParent());
if (old_receiver)
old_receiver->removeItem(item);
item.setLocationOnReceiver(x, y);
writeItemToContentMap(item);
addChildWindow(&item);
// set position and size. This ensures the items visually match the
// logical content map.
item.setPosition(UVector2(UDim(static_cast<float>(x) / contentWidth(), 0),
UDim(static_cast<float>(y) / contentHeight(), 0)));
item.setSize(UVector2(
UDim(static_cast<float>(item.contentWidth()) / contentWidth(), 0),
UDim(static_cast<float>(item.contentHeight()) / contentHeight(), 0)));
return true;
}
return false;
}
//------------------------------------------------------------------------------//
void InventoryReceiver::removeItem(InventoryItem& item)
{
if (item.getParent() != this ||
item.locationOnReceiverX() == -1 ||
item.locationOnReceiverY() == -1)
return;
eraseItemFromContentMap(item);
item.setLocationOnReceiver(-1, -1);
removeChildWindow(&item);
}
//------------------------------------------------------------------------------//
void InventoryReceiver::writeItemToContentMap(const InventoryItem& item)
{
if (item.locationOnReceiverX() == -1 || item.locationOnReceiverY() == -1)
return;
for (int y = 0; y < item.contentHeight(); ++y)
{
const int map_y = item.locationOnReceiverY() + y;
for (int x = 0; x < item.contentWidth(); ++x)
{
const int map_x = item.locationOnReceiverX() + x;
bool val = d_content.elementAtLocation(map_x, map_y) |
item.isSolidAtLocation(x, y);
d_content.setElementAtLocation(map_x, map_y, val);
}
}
invalidate();
}
//------------------------------------------------------------------------------//
void InventoryReceiver::eraseItemFromContentMap(const InventoryItem& item)
{
if (item.locationOnReceiverX() == -1 || item.locationOnReceiverY() == -1)
return;
for (int y = 0; y < item.contentHeight(); ++y)
{
const int map_y = item.locationOnReceiverY() + y;
for (int x = 0; x < item.contentWidth(); ++x)
{
const int map_x = item.locationOnReceiverX() + x;
bool val = d_content.elementAtLocation(map_x, map_y) &
!item.isSolidAtLocation(x, y);
d_content.setElementAtLocation(map_x, map_y, val);
}
}
invalidate();
}
//------------------------------------------------------------------------------//
bool InventoryReceiver::itemWillFitAtLocation(const InventoryItem& item,
int x, int y)
{
if (x < 0 || y < 0)
return false;
if (x + item.contentWidth() > d_content.width() ||
y + item.contentHeight() > d_content.height())
return false;
const bool already_attached = this == item.getParent();
// if item is already attatched erase its data from the content map so the
// test result is reliable.
if (already_attached)
eraseItemFromContentMap(item);
bool result = true;
for (int item_y = 0; item_y < item.contentHeight() && result; ++item_y)
{
for (int item_x = 0; item_x < item.contentWidth() && result; ++item_x)
{
if (d_content.elementAtLocation(item_x + x, item_y + y) &&
item.isSolidAtLocation(item_x, item_y))
result = false;
}
}
// re-write item into content map if we erased it earlier.
if (already_attached)
writeItemToContentMap(item);
return result;
}
//------------------------------------------------------------------------------//
void InventoryReceiver::onDragDropItemDropped(DragDropEventArgs &e)
{
InventoryItem* item = dynamic_cast<InventoryItem*>(e.dragDropItem);
if (!item)
return;
const Size square_size(squarePixelSize());
Rect item_area(item->getUnclippedOuterRect());
item_area.offset(Point(square_size.d_width / 2, square_size.d_height / 2));
const int drop_x = gridXLocationFromPixelPosition(item_area.d_left);
const int drop_y = gridYLocationFromPixelPosition(item_area.d_top);
addItemAtLocation(*item, drop_x, drop_y);
}
//------------------------------------------------------------------------------//
void InventoryReceiver::populateGeometryBuffer()
{
if (!isUserStringDefined("BlockImage"))
return;
const Image* img = PropertyHelper::stringToImage(getUserString("BlockImage"));
if (!img)
return;
const Size square_size(squarePixelSize());
for (int y = 0; y < d_content.height(); ++y)
{
for (int x = 0; x < d_content.width(); ++x)
{
argb_t colour = 0xFFFFFFFF;
if (d_content.elementAtLocation(x, y))
colour = 0xFF0000FF;
img->draw(*d_geometry,
Vector2(x * square_size.d_width + 1, y * square_size.d_height + 1),
Size(square_size.d_width - 2, square_size.d_height - 2), 0,
colour, colour, colour, colour);
}
}
}
//------------------------------------------------------------------------------//
Rect InventoryReceiver::gridBasePixelRect() const
{
return getChildWindowContentArea();
}
//------------------------------------------------------------------------------//
} // End of CEGUI namespace section
|