this was the best i could do:
The strange thing is that if you have more than one "imagemenuitem" only the first can be left/clicked/hovered.
And if i hover the first and then take the mouse away to another "imagemenuitem" the previous won't be left.
Code:
#ifndef _IMAGEMENU_HPP // Inclution guard
#define _IMAGEMENU_HPP
#ifdef WIN32
#include <vector>
#include "ImageMenuItem.hpp"
namespace Win
{
class ImageMenu
{
private:
std::vector<ImageMenuItem> m_items;
int m_indexOfClickedItem, m_indexOfHoveredItem;
int m_xStart, m_yStart;
int m_nWidth, m_nHeight;
int m_offset;
int m_perImage;
Color m_bgColor, m_clickColor, m_hoverColor;
public:
ImageMenu(void)
{
}
ImageMenu(int _xStart, int _yStart, int _nWidth, int _nHeight, int _perImage, int _offset, Color _bgColor, Color _clickColor, Color _hoverColor)
{
this->m_xStart = _xStart;
this->m_yStart = _yStart;
this->m_nWidth = _nWidth;
this->m_nHeight = _nHeight;
this->m_offset = _offset;
this->m_perImage = _perImage;
this->m_bgColor = _bgColor;
this->m_clickColor = _clickColor;
this->m_hoverColor = _hoverColor;
this->m_indexOfClickedItem = this->m_indexOfHoveredItem = -1;
}
bool initiate()
{
return true;
}
void insert()
{
int _xStart;
if(!this->m_items.empty())
_xStart = this->m_items.back().m_xStart + this->m_items.back().m_nWidth + this->m_offset;
else
_xStart = this->m_offset;
ImageMenuItem _item(_xStart, this->m_yStart, this->m_perImage, this->m_nHeight);
this->m_items.push_back(_item);
}
void paint(Win::Window _window)
{
HDC _dc = GetDC(_window.receiveHWND());
ReleaseDC(_window.receiveHWND(), _dc);
}
bool isInBounds(int xPos, int yPos)
{
if(this->m_items.empty())
return false;
if( ((xPos >= this->m_xStart) && (xPos <= this->m_nWidth)) && ((yPos >= this->m_yStart) && (yPos <= this->m_nHeight)) )
return true;
return false;
}
void routeClick(Win::Window _window, int xPos, int yPos)
{
if((!this->m_items.empty()) && (yPos >= this->m_yStart) && (yPos <= this->m_nHeight))
{
for(int _index = 0; _index < (int)this->m_items.size(); _index++)
{
ImageMenuItem _item = this->m_items.at(_index);
if(!((xPos >= _item.m_xStart) && (xPos <= _item.m_nWidth)))
continue;
else if(this->m_indexOfClickedItem == _index)
continue;
_item.onClick(_window, this->m_clickColor);
this->m_indexOfClickedItem = _index;
break;
}
}
}
void routeHover(Win::Window _window, int xPos, int yPos)
{
if((!this->m_items.empty()) && (yPos >= this->m_yStart) && (yPos <= this->m_nHeight))
{
for(int _index = 0; _index < (int)this->m_items.size(); _index++)
{
ImageMenuItem _item = this->m_items.at(_index);
if(((xPos < _item.m_xStart) || (xPos > _item.m_nWidth)))
{
continue;
}
else if(this->m_indexOfClickedItem == _index || this->m_indexOfHoveredItem == _index)
{
continue;
}
// char buffer[33];
// ::itoa(xPos, buffer, 10);
// t(buffer);
_item.onHover(_window, this->m_hoverColor);
this->m_indexOfHoveredItem = _index;
}
}
}
bool isLeft(int xPos, int yPos)
{
if(this->m_items.empty())
return false;
if(this->m_indexOfHoveredItem == -1 || this->m_indexOfClickedItem == this->m_indexOfHoveredItem)
return false;
ImageMenuItem _item(this->m_items.at(this->m_indexOfHoveredItem));
if( ((xPos >= _item.m_xStart) && (xPos <= _item.m_nWidth)) && ((yPos >= _item.m_yStart) && (yPos <= _item.m_nHeight)) )
return false;
return true;
}
void routeLeave(Win::Window _window)
{
if((this->m_indexOfHoveredItem != -1) && (!this->m_items.empty()) )
{
this->m_items.at(this->m_indexOfHoveredItem).onLeave(_window, this->m_bgColor);
this->m_indexOfHoveredItem = -1;
}
}
~ImageMenu(void)
{
}
};
}
#endif
#endif
Code:
#ifndef _IMAGEMENUITEM_HPP // Inclution guard
#define _IMAGEMENUITEM_HPP
#ifdef WIN32
class ImageMenuItem
{
public:
int m_xStart, m_yStart;
int m_nWidth, m_nHeight;
ImageMenuItem(int _xStart, int _yStart, int _nWidth, int _nHeight)
{
this->m_xStart = _xStart;
this->m_yStart = _yStart;
this->m_nWidth = _nWidth;
this->m_nHeight = _nHeight;
}
ImageMenuItem(const ImageMenuItem &_imageMenuItem)
{
this->m_xStart = _imageMenuItem.m_xStart;
this->m_yStart = _imageMenuItem.m_yStart;
this->m_nWidth = _imageMenuItem.m_nWidth;
this->m_nHeight = _imageMenuItem.m_nHeight;
}
void onClick(Win::Window _window, Color _color)
{
HDC _dc = GetDC(_window.receiveHWND());
Graphics graphics(_dc);
SolidBrush _brush(_color);
graphics.FillRectangle(&_brush, this->m_xStart, this->m_yStart, this->m_nWidth, this->m_nHeight);
ReleaseDC(_window.receiveHWND(), _dc);
}
void onHover(Win::Window _window, Color _color)
{
HDC _dc = GetDC(_window.receiveHWND());
Graphics graphics(_dc);
SolidBrush _brush(_color);
graphics.FillRectangle(&_brush, this->m_xStart, this->m_yStart, this->m_nWidth, this->m_nHeight);
ReleaseDC(_window.receiveHWND(), _dc);
}
void onLeave(Win::Window _window, Color _color)
{
HDC _dc = GetDC(_window.receiveHWND());
Graphics graphics(_dc);
SolidBrush _brush(_color);
graphics.FillRectangle(&_brush, this->m_xStart, this->m_yStart, this->m_nWidth, this->m_nHeight);
ReleaseDC(_window.receiveHWND(), _dc);
}
~ImageMenuItem(void)
{
}
};
#endif
#endif