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
|
/***********************************************************************
filename: InventoryItem.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 "InventoryItem.h"
#include "InventoryReceiver.h"
#include <CEGUIPropertyHelper.h>
#include <CEGUIImage.h>
// Start of CEGUI namespace section
namespace CEGUI
{
//------------------------------------------------------------------------------//
const String InventoryItem::WidgetTypeName("InventoryItem");
const String InventoryItem::EventNamespace("InventoryItem");
//------------------------------------------------------------------------------//
InventoryItem::InventoryItem(const String& type, const String& name) :
DragContainer(type, name),
d_validDropTarget(false),
d_receiverLocationX(-1),
d_receiverLocationY(-1)
{
}
//------------------------------------------------------------------------------//
void InventoryItem::setContentSize(int width, int height)
{
InventoryBase::setContentSize(width, height);
d_content.clear(true);
}
//------------------------------------------------------------------------------//
bool InventoryItem::isSolidAtLocation(int x, int y) const
{
return d_content.elementAtLocation(x, y);
}
//------------------------------------------------------------------------------//
void InventoryItem::setItemLayout(const bool* layout)
{
for (int y = 0; y < d_content.height(); ++y)
for (int x = 0; x < d_content.width(); ++x)
d_content.setElementAtLocation(x, y, *layout++);
}
//------------------------------------------------------------------------------//
int InventoryItem::locationOnReceiverX() const
{
return d_receiverLocationX;
}
//------------------------------------------------------------------------------//
int InventoryItem::locationOnReceiverY() const
{
return d_receiverLocationY;
}
//------------------------------------------------------------------------------//
void InventoryItem::setLocationOnReceiver(int x, int y)
{
d_receiverLocationX = x;
d_receiverLocationY = y;
}
//------------------------------------------------------------------------------//
bool InventoryItem::isHit(const Vector2& position, const bool allow_disabled) const
{
if (!DragContainer::isHit(position, allow_disabled))
return false;
int gx = gridXLocationFromPixelPosition(position.d_x);
int gy = gridYLocationFromPixelPosition(position.d_y);
if (gx < 0 || gx >= d_content.width() || gy < 0 || gy >= d_content.height())
return false;
return d_content.elementAtLocation(gx, gy);
}
//------------------------------------------------------------------------------//
bool InventoryItem::currentDropTargetIsValid() const
{
return d_validDropTarget;
}
//------------------------------------------------------------------------------//
void InventoryItem::populateGeometryBuffer()
{
if (!isUserStringDefined("BlockImage"))
return;
const Image* img = PropertyHelper::stringToImage(getUserString("BlockImage"));
if (!img)
return;
const Size square_size(squarePixelSize());
argb_t colour = 0xFF00FF00;
if (d_dragging && !currentDropTargetIsValid())
colour = 0xFFFF0000;
for (int y = 0; y < d_content.height(); ++y)
{
for (int x = 0; x < d_content.width(); ++x)
{
if (d_content.elementAtLocation(x, y))
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 InventoryItem::gridBasePixelRect() const
{
return getUnclippedOuterRect();
}
//------------------------------------------------------------------------------//
void InventoryItem::onMoved(WindowEventArgs& e)
{
invalidate();
DragContainer::onMoved(e);
InventoryReceiver* receiver = dynamic_cast<InventoryReceiver*>(d_dropTarget);
if (receiver)
{
const Size square_size(receiver->squarePixelSize());
Rect area(getUnclippedOuterRect());
area.offset(Point(square_size.d_width / 2, square_size.d_height / 2));
const int x = receiver->gridXLocationFromPixelPosition(area.d_left);
const int y = receiver->gridYLocationFromPixelPosition(area.d_top);
d_validDropTarget = receiver->itemWillFitAtLocation(*this, x, y);
return;
}
d_validDropTarget = false;
}
//------------------------------------------------------------------------------//
void InventoryItem::onDragDropTargetChanged(DragDropEventArgs& e)
{
DragContainer::onDragDropTargetChanged(e);
d_validDropTarget = (dynamic_cast<InventoryReceiver*>(d_dropTarget) != 0);
invalidate();
}
//------------------------------------------------------------------------------//
} // End of CEGUI namespace section
|