MyGUI  3.2.0
MyGUI_Window.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_Window.h"
24 #include "MyGUI_Macros.h"
25 #include "MyGUI_Gui.h"
27 #include "MyGUI_InputManager.h"
28 #include "MyGUI_WidgetManager.h"
29 #include "MyGUI_ResourceSkin.h"
30 
31 namespace MyGUI
32 {
34  const float WINDOW_ALPHA_FOCUS = 0.7f;
35  const float WINDOW_ALPHA_DEACTIVE = 0.3f;
36  const float WINDOW_SPEED_COEF = 3.0f;
37 
38  const int WINDOW_SNAP_DISTANSE = 10;
39 
41  mWidgetCaption(nullptr),
42  mMouseRootFocus(false),
43  mKeyRootFocus(false),
44  mIsAutoAlpha(false),
45  mSnap(false),
46  mAnimateSmooth(false),
47  mClient(nullptr),
48  mMovable(true)
49  {
50  }
51 
53  {
54  Base::initialiseOverride();
55 
56  // FIXME нам нужен фокус клавы
57  setNeedKeyFocus(true);
58 
59  // дефолтные размеры
60  mMinmax.set(
61  (std::numeric_limits<int>::min)(),
62  (std::numeric_limits<int>::min)(),
63  (std::numeric_limits<int>::max)(),
64  (std::numeric_limits<int>::max)());
65 
66  bool main_move = false;
67  if (isUserString("MainMove"))
68  {
69  setUserString("Scale", "1 1 0 0");
70  main_move = true;
71  }
72 
73  assignWidget(mClient, "Client");
74  if (mClient != nullptr)
75  {
76  if (main_move)
77  {
78  mClient->setUserString("Scale", "1 1 0 0");
82  }
83  setWidgetClient(mClient);
84  }
85 
86  assignWidget(mWidgetCaption, "Caption");
87  if (mWidgetCaption != nullptr)
88  {
89  mWidgetCaption->setUserString("Scale", "1 1 0 0");
92  mWidgetCaption->eventMouseDrag += newDelegate(this, &Window::notifyMouseDrag);
93  }
94 
95  VectorWidgetPtr buttons = getSkinWidgetsByName("Button");
96  for (VectorWidgetPtr::iterator iter = buttons.begin(); iter != buttons.end(); ++iter)
97  {
98  (*iter)->eventMouseButtonClick += newDelegate(this, &Window::notifyPressedButtonEvent);
99  }
100 
101  VectorWidgetPtr actions = getSkinWidgetsByName("Action");
102  for (VectorWidgetPtr::iterator iter = actions.begin(); iter != actions.end(); ++iter)
103  {
104  (*iter)->eventMouseButtonPressed += newDelegate(this, &Window::notifyMousePressed);
105  (*iter)->eventMouseButtonReleased += newDelegate(this, &Window::notifyMouseReleased);
106  (*iter)->eventMouseDrag += newDelegate(this, &Window::notifyMouseDrag);
107  }
108 
109  const size_t countNames = 8;
110  const char* resizers[2][countNames] =
111  {
112  {"ResizeLeftTop", "ResizeTop", "ResizeRightTop", "ResizeRight", "ResizeRightBottom", "ResizeBottom", "ResizeLeftBottom", "ResizeLeft"},
113  {"Left Top", "Top", "Right Top", "Right", "Right Bottom", "Bottom", "Left Bottom", "Left"}
114  };
115 
116  for (size_t index = 0; index < countNames; ++ index)
117  {
118  Widget* widget = nullptr;
119  assignWidget(widget, resizers[0][index]);
120  if (widget != nullptr)
121  {
125  widget->setUserString("Action", resizers[1][index]);
126  }
127  }
128  }
129 
131  {
132  mClient = nullptr;
133  mWidgetCaption = nullptr;
134 
135  Base::shutdownOverride();
136  }
137 
139  {
140  mMouseRootFocus = _focus;
141  updateAlpha();
142 
143  Base::onMouseChangeRootFocus(_focus);
144  }
145 
147  {
148  mKeyRootFocus = _focus;
149  updateAlpha();
150 
151  Base::onKeyChangeRootFocus(_focus);
152  }
153 
154  void Window::onMouseDrag(int _left, int _top, MouseButton _id)
155  {
156  // на тот случай, если двигать окно, можно за любое место виджета
157  notifyMouseDrag(this, _left, _top, _id);
158 
159  Base::onMouseDrag(_left, _top, _id);
160  }
161 
162  void Window::onMouseButtonPressed(int _left, int _top, MouseButton _id)
163  {
164  notifyMousePressed(this, _left, _top, _id);
165 
166  Base::onMouseButtonPressed(_left, _top, _id);
167  }
168 
169  void Window::onMouseButtonReleased(int _left, int _top, MouseButton _id)
170  {
171  notifyMouseReleased(this, _left, _top, _id);
172 
173  Base::onMouseButtonReleased(_left, _top, _id);
174  }
175 
176  void Window::notifyMousePressed(MyGUI::Widget* _sender, int _left, int _top, MouseButton _id)
177  {
178  if (MouseButton::Left == _id)
179  {
180  mPreActionCoord = mCoord;
181  mCurrentActionScale = _getActionScale(_sender);
182  }
183  }
184 
186  {
187  eventWindowButtonPressed(this, _sender->getUserString("Event"));
188  }
189 
190  void Window::notifyMouseDrag(MyGUI::Widget* _sender, int _left, int _top, MouseButton _id)
191  {
192  if (_id != MouseButton::Left)
193  return;
194 
196 
197  IntCoord coord = mCurrentActionScale;
198  coord.left *= (_left - point.left);
199  coord.top *= (_top - point.top);
200  coord.width *= (_left - point.left);
201  coord.height *= (_top - point.top);
202 
203  if (coord.empty())
204  return;
205 
206  if (coord.left == 0 && coord.top == 0)
207  setSize((mPreActionCoord + coord).size());
208  else if (coord.width == 0 && coord.height == 0)
209  setPosition((mPreActionCoord + coord).point());
210  else
211  setCoord(mPreActionCoord + coord);
212 
213  // посылаем событие о изменении позиции и размере
215  }
216 
218  {
219  if (!mIsAutoAlpha)
220  return;
221 
222  float alpha;
223  if (mKeyRootFocus)
224  alpha = WINDOW_ALPHA_ACTIVE;
225  else if (mMouseRootFocus)
226  alpha = WINDOW_ALPHA_FOCUS;
227  else
228  alpha = WINDOW_ALPHA_DEACTIVE;
229 
230  ControllerFadeAlpha* controller = createControllerFadeAlpha(alpha, WINDOW_SPEED_COEF, true);
231  ControllerManager::getInstance().addItem(this, controller);
232  }
233 
234  void Window::setAutoAlpha(bool _auto)
235  {
236  mIsAutoAlpha = _auto;
237  if (!_auto)
239  else
240  {
241  if (mKeyRootFocus)
243  else if (mMouseRootFocus)
245  else
247  }
248  }
249 
250  void Window::setPosition(const IntPoint& _point)
251  {
252  IntPoint point = _point;
253  // прилепляем к краям
254  if (mSnap)
255  {
256  IntCoord coord(point, mCoord.size());
257  getSnappedCoord(coord);
258  point = coord.point();
259  }
260 
261  Base::setPosition(point);
262  }
263 
264  void Window::setSize(const IntSize& _size)
265  {
266  IntSize size = _size;
267  // прилепляем к краям
268 
269  if (size.width < mMinmax.left)
270  size.width = mMinmax.left;
271  else if (size.width > mMinmax.right)
272  size.width = mMinmax.right;
273  if (size.height < mMinmax.top)
274  size.height = mMinmax.top;
275  else if (size.height > mMinmax.bottom)
276  size.height = mMinmax.bottom;
277  if ((size.width == mCoord.width) && (size.height == mCoord.height))
278  return;
279 
280  if (mSnap)
281  {
282  IntCoord coord(mCoord.point(), size);
283  getSnappedCoord(coord);
284  size = coord.size();
285  }
286 
287  Base::setSize(size);
288  }
289 
290  void Window::setCoord(const IntCoord& _coord)
291  {
292  IntPoint pos = _coord.point();
293  IntSize size = _coord.size();
294 
295  if (size.width < mMinmax.left)
296  {
297  int offset = mMinmax.left - size.width;
298  size.width = mMinmax.left;
299  if ((pos.left - mCoord.left) > offset)
300  pos.left -= offset;
301  else
302  pos.left = mCoord.left;
303  }
304  else if (size.width > mMinmax.right)
305  {
306  int offset = mMinmax.right - size.width;
307  size.width = mMinmax.right;
308  if ((pos.left - mCoord.left) < offset)
309  pos.left -= offset;
310  else
311  pos.left = mCoord.left;
312  }
313  if (size.height < mMinmax.top)
314  {
315  int offset = mMinmax.top - size.height;
316  size.height = mMinmax.top;
317  if ((pos.top - mCoord.top) > offset)
318  pos.top -= offset;
319  else
320  pos.top = mCoord.top;
321  }
322  else if (size.height > mMinmax.bottom)
323  {
324  int offset = mMinmax.bottom - size.height;
325  size.height = mMinmax.bottom;
326  if ((pos.top - mCoord.top) < offset)
327  pos.top -= offset;
328  else
329  pos.top = mCoord.top;
330  }
331 
332  // прилепляем к краям
333  if (mSnap)
334  {
335  IntCoord coord(pos, size);
336  getSnappedCoord(coord);
337  size = coord.size();
338  }
339 
340  IntCoord coord(pos, size);
341  if (coord == mCoord)
342  return;
343 
344  Base::setCoord(coord);
345  }
346 
347  void Window::setCaption(const UString& _caption)
348  {
349  if (mWidgetCaption != nullptr)
350  mWidgetCaption->setCaption(_caption);
351  else
352  Base::setCaption(_caption);
353  }
354 
356  {
357  if (mWidgetCaption != nullptr)
358  return mWidgetCaption->getCaption();
359  return Base::getCaption();
360  }
361 
363  {
364  ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, WINDOW_SPEED_COEF, false);
366  ControllerManager::getInstance().addItem(this, controller);
367  }
368 
370  {
371  if (mAnimateSmooth)
372  {
374  mAnimateSmooth = false;
375  }
376  }
377 
378  void Window::setVisible(bool _visible)
379  {
380  if (mAnimateSmooth)
381  {
383  setAlpha(getAlphaVisible());
384  setEnabledSilent(true);
385  mAnimateSmooth = false;
386  }
387 
388  Base::setVisible(_visible);
389  }
390 
391  float Window::getAlphaVisible() const
392  {
393  return (mIsAutoAlpha && !mKeyRootFocus) ? WINDOW_ALPHA_DEACTIVE : ALPHA_MAX;
394  }
395 
396  void Window::getSnappedCoord(IntCoord& _coord)
397  {
398  if (abs(_coord.left) <= WINDOW_SNAP_DISTANSE) _coord.left = 0;
399  if (abs(_coord.top) <= WINDOW_SNAP_DISTANSE) _coord.top = 0;
400 
401  const IntSize view_size = getParentSize();
402 
403  if ( abs(_coord.left + _coord.width - view_size.width) < WINDOW_SNAP_DISTANSE)
404  _coord.left = view_size.width - _coord.width;
405  if ( abs(_coord.top + _coord.height - view_size.height) < WINDOW_SNAP_DISTANSE)
406  _coord.top = view_size.height - _coord.height;
407  }
408 
409  void Window::setVisibleSmooth(bool _visible)
410  {
411  mAnimateSmooth = true;
413 
414  if (_visible)
415  {
416  setEnabledSilent(true);
417  if (!getVisible())
418  {
420  Base::setVisible(true);
421  }
422  ControllerFadeAlpha* controller = createControllerFadeAlpha(getAlphaVisible(), WINDOW_SPEED_COEF, true);
423  controller->eventPostAction += newDelegate(this, &Window::animateStop);
424  ControllerManager::getInstance().addItem(this, controller);
425  }
426  else
427  {
428  setEnabledSilent(false);
429  ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, WINDOW_SPEED_COEF, false);
431  ControllerManager::getInstance().addItem(this, controller);
432  }
433  }
434 
435  ControllerFadeAlpha* Window::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
436  {
438  ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
439 
440  controller->setAlpha(_alpha);
441  controller->setCoef(_coef);
442  controller->setEnabled(_enable);
443 
444  return controller;
445  }
446 
447  void Window::setMinSize(const IntSize& _value)
448  {
449  mMinmax.left = _value.width;
450  mMinmax.top = _value.height;
451  }
452 
454  {
455  return IntSize(mMinmax.left, mMinmax.top);
456  }
457 
458  void Window::setMaxSize(const IntSize& _value)
459  {
460  mMinmax.right = _value.width;
461  mMinmax.bottom = _value.height;
462  }
463 
465  {
466  return IntSize(mMinmax.right, mMinmax.bottom);
467  }
468 
469  void Window::setPropertyOverride(const std::string& _key, const std::string& _value)
470  {
471  if (_key == "AutoAlpha")
472  setAutoAlpha(utility::parseValue<bool>(_value));
473  else if (_key == "Snap")
474  setSnap(utility::parseValue<bool>(_value));
475  else if (_key == "MinSize")
476  setMinSize(utility::parseValue<IntSize>(_value));
477  else if (_key == "MaxSize")
478  setMaxSize(utility::parseValue<IntSize>(_value));
479  else if (_key == "Movable")
480  setMovable(utility::parseValue<bool>(_value));
481  else
482  {
483  Base::setPropertyOverride(_key, _value);
484  return;
485  }
486  eventChangeProperty(this, _key, _value);
487  }
488 
490  {
491  return mCurrentActionScale;
492  }
493 
494  bool Window::getAutoAlpha() const
495  {
496  return mIsAutoAlpha;
497  }
498 
500  {
501  return mWidgetCaption;
502  }
503 
504  void Window::setMinSize(int _width, int _height)
505  {
506  setMinSize(IntSize(_width, _height));
507  }
508 
509  void Window::setMaxSize(int _width, int _height)
510  {
511  setMaxSize(IntSize(_width, _height));
512  }
513 
514  void Window::setPosition(int _left, int _top)
515  {
516  setPosition(IntPoint(_left, _top));
517  }
518 
519  void Window::setSize(int _width, int _height)
520  {
521  setSize(IntSize(_width, _height));
522  }
523 
524  void Window::setCoord(int _left, int _top, int _width, int _height)
525  {
526  setCoord(IntCoord(_left, _top, _width, _height));
527  }
528 
529  bool Window::getSnap() const
530  {
531  return mSnap;
532  }
533 
534  void Window::setSnap(bool _value)
535  {
536  mSnap = _value;
537  }
538 
539  void Window::notifyMouseReleased(MyGUI::Widget* _sender, int _left, int _top, MouseButton _id)
540  {
541  if (MouseButton::Left == _id)
542  {
543  mCurrentActionScale.clear();
544  }
545  }
546 
547  IntCoord Window::_getActionScale(Widget* _widget)
548  {
549  if (_widget->isUserString("Scale"))
550  {
551  IntCoord result = IntCoord::parse(_widget->getUserString("Scale"));
552 
553  if (result == IntCoord(1, 1, 0, 0) && !mMovable)
554  result.clear();
555 
556  return result;
557  }
558  else if (_widget->isUserString("Action"))
559  {
560  const std::string& action = _widget->getUserString("Action");
561  if (action == "Move")
562  {
563  if (mMovable)
564  return IntCoord(1, 1, 0, 0);
565  else
566  return IntCoord();
567  }
568 
569  IntCoord coord;
570  Align align = Align::parse(action);
571 
572  if (align.isLeft())
573  {
574  coord.left = 1;
575  coord.width = -1;
576  }
577  else if (align.isRight())
578  {
579  coord.width = 1;
580  }
581 
582  if (align.isTop())
583  {
584  coord.top = 1;
585  coord.height = -1;
586  }
587  else if (align.isBottom())
588  {
589  coord.height = 1;
590  }
591 
592  return coord;
593  }
594 
595  return IntCoord();
596  }
597 
598  void Window::setMovable(bool _value)
599  {
600  mMovable = _value;
601  }
602 
603  bool Window::getMovable() const
604  {
605  return mMovable;
606  }
607 
608 } // namespace MyGUI
void addItem(Widget *_widget, ControllerItem *_item)
void animateStop(Widget *_widget)
const float ALPHA_MIN
Definition: MyGUI_Macros.h:35
void notifyMouseReleased(MyGUI::Widget *_sender, int _left, int _top, MouseButton _id)
EventPair< EventHandle_WidgetString, EventHandle_WindowPtrCStringRef > eventWindowButtonPressed
Definition: MyGUI_Window.h:117
void notifyMouseDrag(MyGUI::Widget *_sender, int _left, int _top, MouseButton _id)
types::TSize< int > IntSize
Definition: MyGUI_Types.h:44
void setUserString(const std::string &_key, const std::string &_value)
const float WINDOW_ALPHA_ACTIVE
const IntPoint & getLastPressedPosition(MouseButton _id) const
EventHandle_WidgetIntIntButton eventMouseButtonReleased
delegates::IDelegate0 * newDelegate(void(*_func)())
void notifyPressedButtonEvent(MyGUI::Widget *_sender)
static InputManager & getInstance()
TSize< T > size() const
Definition: MyGUI_TCoord.h:205
bool getAutoAlpha() const
const IntCoord & getActionScale() const
void setSnap(bool _value)
void assignWidget(T *&_widget, const std::string &_name)
Definition: MyGUI_Widget.h:324
virtual const UString & getCaption()
void onMouseButtonReleased(int _left, int _top, MouseButton _id)
static Align parse(const std::string &_value)
Definition: MyGUI_Align.h:142
void setMinSize(const IntSize &_value)
virtual void setCaption(const UString &_value)
const float WINDOW_ALPHA_FOCUS
#define nullptr
IntSize getParentSize() const
void onMouseDrag(int _left, int _top, MouseButton _id)
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:50
virtual const UString & getCaption()
void onMouseButtonPressed(int _left, int _top, MouseButton _id)
EventPair< EventHandle_WidgetVoid, EventHandle_WindowPtr > eventWindowChangeCoord
Definition: MyGUI_Window.h:124
virtual void shutdownOverride()
virtual void setPosition(const IntPoint &_value)
bool empty() const
Definition: MyGUI_TCoord.h:195
const float ALPHA_MAX
Definition: MyGUI_Macros.h:34
static const std::string & getClassTypeName()
EventHandle_WidgetStringString eventChangeProperty
Definition: MyGUI_Widget.h:271
std::vector< Widget * > VectorWidgetPtr
void setWidgetClient(Widget *_widget)
void setEnabledSilent(bool _value)
const float WINDOW_ALPHA_DEACTIVE
void actionWidgetDestroy(Widget *_widget)
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:33
void set(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition: MyGUI_TRect.h:136
delegates::CMultiDelegate1< Widget * > eventPostAction
virtual void setSize(const IntSize &_value)
bool isUserString(const std::string &_key) const
void destroySmooth()
A UTF-16 string with implicit conversion to/from std::string and std::wstring.
IntSize getMaxSize()
virtual void setCaption(const UString &_value)
void setMaxSize(const IntSize &_value)
VectorWidgetPtr getSkinWidgetsByName(const std::string &_name)
IntSize getMinSize()
virtual void setCoord(const IntCoord &_value)
const float WINDOW_SPEED_COEF
void actionWidgetHide(Widget *_widget)
bool getMovable() const
static TCoord< int > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:222
const int WINDOW_SNAP_DISTANSE
bool getVisible() const
void setNeedKeyFocus(bool _value)
EventHandle_WidgetIntIntButton eventMouseButtonPressed
void setAutoAlpha(bool _value)
void setMovable(bool _value)
TextBox * getCaptionWidget()
void onMouseChangeRootFocus(bool _focus)
void notifyMousePressed(MyGUI::Widget *_sender, int _left, int _top, MouseButton _id)
virtual void setPropertyOverride(const std::string &_key, const std::string &_value)
void onKeyChangeRootFocus(bool _focus)
void setAlpha(float _value)
void setVisibleSmooth(bool _value)
virtual void initialiseOverride()
virtual void setVisible(bool _value)
TPoint< T > point() const
Definition: MyGUI_TCoord.h:200
EventPair3to4< EventHandle_WidgetIntInt, EventHandle_WidgetIntIntButton > eventMouseDrag
const std::string & getUserString(const std::string &_key) const
bool getSnap() const
ControllerItem * createItem(const std::string &_type)
types::TPoint< int > IntPoint
Definition: MyGUI_Types.h:41