MyGUI  3.2.0
MyGUI_MaskPickInfo.cpp
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_MaskPickInfo.h"
24 #include "MyGUI_ResourceManager.h"
25 #include "MyGUI_RenderManager.h"
26 #include "MyGUI_DataManager.h"
27 
28 namespace MyGUI
29 {
30 
32  width(0),
33  height(0)
34  {
35  }
36 
37  bool MaskPickInfo::load(const std::string& _file)
38  {
39  if (!DataManager::getInstance().isDataExist(_file))
40  return false;
41 
43  ITexture* texture = render.createTexture(_file);
44  texture->loadFromFile(_file);
45 
46  uint8* buffer = (uint8*)texture->lock(TextureUsage::Read);
47  if (buffer == 0)
48  {
49  render.destroyTexture(texture);
50  return false;
51  }
52 
53  size_t pixel_size = texture->getNumElemBytes();
54 
55  width = texture->getWidth();
56  height = texture->getHeight();
57  size_t size = width * height;
58  data.resize(size);
59 
60  size_t pos = 0;
61  for (size_t pos_pix = 0; pos_pix < size; pos_pix++)
62  {
63  bool white = true;
64  for (size_t in_pix = 0; in_pix < pixel_size; in_pix++)
65  {
66  if (0xFF != buffer[pos])
67  {
68  white = false;
69  }
70  pos++;
71  }
72 
73  data[pos_pix] = white;
74  }
75 
76  texture->unlock();
77  render.destroyTexture(texture);
78 
79  return true;
80  }
81 
82  bool MaskPickInfo::pick(const IntPoint& _point, const IntCoord& _coord) const
83  {
84  if ((0 == _coord.width) || (0 == _coord.height)) return false;
85 
86  int x = ((_point.left * width) - 1) / _coord.width;
87  int y = ((_point.top * height) - 1) / _coord.height;
88 
89  return 0 != data[(size_t)(y * width + x)];
90  }
91 
92  bool MaskPickInfo::empty() const
93  {
94  return data.empty();
95  }
96 
97 } // namespace MyGUI
virtual size_t getNumElemBytes()=0
static DataManager & getInstance()
bool load(const std::string &_file)
virtual void * lock(TextureUsage _access)=0
virtual void unlock()=0
virtual int getHeight()=0
virtual void loadFromFile(const std::string &_filename)=0
unsigned char uint8
Definition: MyGUI_Types.h:61
virtual ITexture * createTexture(const std::string &_name)=0
virtual int getWidth()=0
virtual void destroyTexture(ITexture *_texture)=0
bool pick(const IntPoint &_point, const IntCoord &_coord) const