Window v1.0

Название: Window – окно

Предназначение: служит  вспомогательным классом для класса Windows, так-же может использоваться как самостоятельный класс.

Возможности:
1. отрисовка во входящем sf::RenderWindow окне
2. менять дизайн окна
3. перемещаться
4. проверяться на события
5.* менять свои размеры

Исходный код:

#include "stdafx.h"
#include "Image.h"
#include "DesignWindow.h"
#include "Mouse.h"
#pragma once
class Window
{
protected:
std::vector<DesignWindow*> vecDesign; // Вектор для 1 дизайна
sf::Image imageWindow; // Результирующая картинка окна
sf::Texture texture;
sf::Sprite sprite;
int width_current;
int width_min;
int width_max;
int height_current;
int height_min;
int height_max;
int height_x;
//=================
bool flag1;
int mXc;
int mYc;
int wXc;
int wYc;
//=================
bool isMove;       // Можно перемещать окно?
bool isConsiderBorder; // Учитывать границы?
bool isChangeSize; // Можно изменять размеры?
public:
Window(int width, int height);
~Window(void);
void setSize(int width, int height);
// Установить дизайн окна
bool setDesign(sf::String filename); 
void update(void);

void data();
void move(int dx, int dy);
void setPosition(int x, int y);
int getPositionX();
int getPositionY();
bool check1(int* x, int* y); // Входит ли в окно
bool check2(int* x, int* y); // Входит ли в верхнюю часть, за которую перетаскивают
void behavior(Mouse* mouse);
sf::Image getImage();
void draw(sf::RenderWindow *window);
};
#include "Window.h"
Window::Window(int width, int height)
{
data();
width_current = width;
height_current = height;
}
Window::~Window(void)
{
//
}
bool Window::setDesign(sf::String filename)
{
DesignWindow* dw = new DesignWindow(filename);
vecDesign.push_back(dw);
update();
return true;
}
void Window::setSize(int width, int height)
{
width_current = width;
height_current = height;
update();
}
void Window::update(void)
{
//imageWindow.create(width_current, height_current, sf::Color(0, 0 , 0, 255));
imageWindow.create(width_current, height_current, sf::Color(255, 255, 255, 255));
std::vector<int> w; // вектор ширин
std::vector<int> h; // вектор высот
for(int i=0; i<9; i++)
{
int width  = vecDesign[0]->getImage(i).getSize().x; w.push_back(width);
int height = vecDesign[0]->getImage(i).getSize().y; h.push_back(height);
}
height_x = h[4];
sf::Image img;
// 1. рисуем полотно
int h8 = height_current - h[4] - h[7];
int w8 = width_current  - w[5] - w[6];
img = vecDesign[0]->getImage(8, w8, h8);
imageWindow.copy(img, w[5], h[4]);
// 3. вставляем уголки
// 3.1 Лев Верх
img = vecDesign[0]->getImage(0);
int w0 = img.getSize().x;
int h0 = img.getSize().y;
imageWindow.copy(img, 0, 0);
// 3.2 Прав Верх
img = vecDesign[0]->getImage(1);
imageWindow.copy(img, width_current - w0, 0);
// 3.3 Лев Низ
img = vecDesign[0]->getImage(2);
int w2 = img.getSize().x;
int h2 = img.getSize().y;
imageWindow.copy(img, 0, height_current - h2);
// 3.4 Прав Низ
img = vecDesign[0]->getImage(3);
imageWindow.copy(img, width_current - w2, height_current - h2);
// 2. вставляем границы
// 2.1. Верхняя
img = vecDesign[0]->getImage(4, width_current -2*w0, 1);
int h4 = img.getSize().y;
imageWindow.copy(img, w0, 0);
// 2.2. Левая
img = vecDesign[0]->getImage(5, 1, height_current-h0-h2);
int w5 = img.getSize().x; 
int h5 = img.getSize().y;
imageWindow.copy(img, 0, h0);
// 2.3. Правая
img = vecDesign[0]->getImage(6, 1, height_current-h0-h2);
imageWindow.copy(img, width_current-w5, h0);
// 2.4. Нижняя
img = vecDesign[0]->getImage(7, width_current-w[2]-w[3], 1);
imageWindow.copy(img, w[2], height_current-h[7]);
texture.loadFromImage(imageWindow);
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0,0,width_current,height_current)); // фича 2.04.2016. 0:41 - убирает лишнее при изменении размеров окна (уберите, увидите)
}
sf::Image Window::getImage()
{
return imageWindow;
}
void Window::data(void)
{
vecDesign.reserve(1);
width_min = 0;
width_max = 0;
height_min = 0;
height_max = 0;
height_x = 0;
flag1 = false;
mXc = 0;
mYc = 0;
wXc = 0;
wYc = 0;
isMove = true;
isConsiderBorder = true;
isChangeSize = false;
sprite.setPosition(0, 0);
}
void Window::move(int dx, int dy)
{
sprite.move(dx, dy);
}
void Window::setPosition(int x, int y)
{
sprite.setPosition(x, y);
}
int Window::getPositionX()
{
return sprite.getPosition().x;
}
int Window::getPositionY()
{
return sprite.getPosition().y;
}
void Window::draw(sf::RenderWindow *window)
{
window->draw(sprite);
}
void Window::behavior(Mouse* mouse)
{
int x = (*mouse).getX();
int y = (*mouse).getY();
switch((*mouse).getEventButton(sf::Mouse::Left))
{
case 0:
break;
case 1:
if(check1(&x, &y))
{
// Если клик мыши был в окне
// запоминаем координаты мыши
mXc = x;
mYc = y;
// запоминаем координаты окна
wXc = sprite.getPosition().x;
wYc = sprite.getPosition().y;
if(check2(&x, &y))
{ 
// Если клик мыши был по перетаскиваемой зоне
flag1 = true;
}
}
break;
case 2: 
if(flag1 == true)
{
int dx = x - mXc;
int dy = y - mYc;
setPosition(wXc + dx, wYc + dy);
}
break;
case 3: 
flag1 = false; // отпущено
break;
}
}
bool Window::check1(int* x, int* y)
{
if(  (sprite.getPosition().x <= *x) 
&& (*x <= sprite.getPosition().x + sprite.getTexture()->getSize().x)
&& (sprite.getPosition().y <= *y)
&& (*y <= sprite.getPosition().y + sprite.getTexture()->getSize().y)
) return true;
return false;
}
bool Window::check2(int* x, int* y)
{
if( (sprite.getPosition().x +1 <= *x) 
&& (*x <= sprite.getPosition().x + width_current -1)
&& (sprite.getPosition().y +1 <= *y)
&& (*y <= sprite.getPosition().y + height_x -1)
) return true;
return false;
}

 

 
Буду благодарен, если поделитесь: