MyGUI 3.5.3
MyGUI_Window.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_Window.h"
9#include "MyGUI_Macros.h"
10#include "MyGUI_Gui.h"
12#include "MyGUI_InputManager.h"
13#include "MyGUI_WidgetManager.h"
14#include "MyGUI_ResourceSkin.h"
15#include <array>
16#include <cmath>
17
18namespace MyGUI
19{
20
22 const float WINDOW_ALPHA_FOCUS = 0.7f;
23 const float WINDOW_ALPHA_DEACTIVE = 0.3f;
24 const float WINDOW_SPEED_COEF = 3.0f;
25
26 const int WINDOW_SNAP_DISTANSE = 10;
27
28 void Window::initialiseOverride()
29 {
30 Base::initialiseOverride();
31
32 // FIXME we need keyboard focus
33 setNeedKeyFocus(true);
34
35 mMinmax.set(
36 (std::numeric_limits<int>::min)(),
37 (std::numeric_limits<int>::min)(),
38 (std::numeric_limits<int>::max)(),
39 (std::numeric_limits<int>::max)());
40
41 bool main_move = false;
42 if (isUserString("MainMove"))
43 {
44 setUserString("Scale", "1 1 0 0");
45 main_move = true;
46 }
47
48 if (getClientWidget() != nullptr)
49 {
50 if (main_move)
51 {
52 getClientWidget()->setUserString("Scale", "1 1 0 0");
53 getClientWidget()->eventMouseButtonPressed += newDelegate(this, &Window::notifyMousePressed);
54 getClientWidget()->eventMouseButtonReleased += newDelegate(this, &Window::notifyMouseReleased);
55 getClientWidget()->eventMouseDrag += newDelegate(this, &Window::notifyMouseDrag);
56 }
57 }
58
60 assignWidget(mWidgetCaption, "Caption");
61 if (mWidgetCaption != nullptr)
62 {
63 mWidgetCaption->setUserString("Scale", "1 1 0 0");
64 mWidgetCaption->eventMouseButtonPressed += newDelegate(this, &Window::notifyMousePressed);
65 mWidgetCaption->eventMouseButtonReleased += newDelegate(this, &Window::notifyMouseReleased);
66 mWidgetCaption->eventMouseDrag += newDelegate(this, &Window::notifyMouseDrag);
67 }
68
69 VectorWidgetPtr buttons = getSkinWidgetsByName("Button");
70 for (auto& button : buttons)
71 {
72 button->eventMouseButtonClick += newDelegate(this, &Window::notifyPressedButtonEvent);
73 }
74
75 VectorWidgetPtr actions = getSkinWidgetsByName("Action");
76 for (auto& action : actions)
77 {
78 action->eventMouseButtonPressed += newDelegate(this, &Window::notifyMousePressed);
79 action->eventMouseButtonReleased += newDelegate(this, &Window::notifyMouseReleased);
80 action->eventMouseDrag += newDelegate(this, &Window::notifyMouseDrag);
81 action->eventMouseWheel += newDelegate(this, &Window::notifyMouseWheel);
82 }
83
84 constexpr size_t countNames = 8;
85 const std::array<std::pair<std::string_view, std::string_view>, countNames> resizers{
86 {{"ResizeLeftTop", "Left Top"},
87 {"ResizeTop", "Top"},
88 {"ResizeRightTop", "Right Top"},
89 {"ResizeRight", "Right"},
90 {"ResizeRightBottom", "Right Bottom"},
91 {"ResizeBottom", "Bottom"},
92 {"ResizeLeftBottom", "Left Bottom"},
93 {"ResizeLeft", "Left"}}};
94
95 for (const auto& resizer : resizers)
96 {
97 Widget* widget = nullptr;
98 assignWidget(widget, resizer.first);
99 if (widget != nullptr)
100 {
101 widget->eventMouseButtonPressed += newDelegate(this, &Window::notifyMousePressed);
102 widget->eventMouseButtonReleased += newDelegate(this, &Window::notifyMouseReleased);
103 widget->eventMouseDrag += newDelegate(this, &Window::notifyMouseDrag);
104 widget->eventMouseWheel += newDelegate(this, &Window::notifyMouseWheel);
105 widget->setUserString("Action", resizer.second);
106 }
107 }
108 }
109
110 void Window::shutdownOverride()
111 {
112 mWidgetCaption = nullptr;
113
114 Base::shutdownOverride();
115 }
116
117 void Window::onMouseChangeRootFocus(bool _focus)
118 {
119 mMouseRootFocus = _focus;
120 updateAlpha();
121
122 Base::onMouseChangeRootFocus(_focus);
123 }
124
125 void Window::onKeyChangeRootFocus(bool _focus)
126 {
127 mKeyRootFocus = _focus;
128 updateAlpha();
129
130 Base::onKeyChangeRootFocus(_focus);
131 }
132
133 void Window::onMouseDrag(int _left, int _top, MouseButton _id)
134 {
135 // in case window can be moved by any widget area
136 notifyMouseDrag(this, _left, _top, _id);
137
138 Base::onMouseDrag(_left, _top, _id);
139 }
140
141 void Window::onMouseButtonPressed(int _left, int _top, MouseButton _id)
142 {
143 notifyMousePressed(this, _left, _top, _id);
144
145 Base::onMouseButtonPressed(_left, _top, _id);
146 }
147
148 void Window::onMouseButtonReleased(int _left, int _top, MouseButton _id)
149 {
150 notifyMouseReleased(this, _left, _top, _id);
151
152 Base::onMouseButtonReleased(_left, _top, _id);
153 }
154
155 void Window::notifyMousePressed(MyGUI::Widget* _sender, int _left, int _top, MouseButton _id)
156 {
157 if (MouseButton::Left == _id)
158 {
159 mPreActionCoord = mCoord;
160 mCurrentActionScale = _getActionScale(_sender);
161 if (mCurrentActionScale.width != 0 || mCurrentActionScale.height != 0)
162 mPreActionAnchor = localToLayer(getResizeAnchor());
163 }
164 }
165
166 void Window::notifyPressedButtonEvent(MyGUI::Widget* _sender)
167 {
168 eventWindowButtonPressed(this, _sender->getUserString("Event"));
169 }
170
171 void Window::notifyMouseDrag(MyGUI::Widget*, int, int, MouseButton _id)
172 {
173 if (_id != MouseButton::Left || mCurrentActionScale.empty())
174 return;
175
176 const InputManager& input = InputManager::getInstance();
177 const IntPoint screenDelta = input.getMousePositionByLayer() - input.getLastPressedPosition(MouseButton::Left);
178 // Movement uses the parent's axes; resizing uses the window's axes.
179 const bool moveOnly = mCurrentActionScale.width == 0 && mCurrentActionScale.height == 0;
180 const Widget* axes = moveOnly ? getParent() : this;
181 const IntPoint localDelta = axes ? axes->unrotateVector(screenDelta) : screenDelta;
182
183 const IntCoord coordDelta(
184 mCurrentActionScale.left * localDelta.left,
185 mCurrentActionScale.top * localDelta.top,
186 mCurrentActionScale.width * localDelta.left,
187 mCurrentActionScale.height * localDelta.top);
188 const IntCoord requestedCoord = mPreActionCoord + coordDelta;
189
190 if (moveOnly)
191 setPosition(requestedCoord.point());
192 else if (mCurrentActionScale.left == 0 && mCurrentActionScale.top == 0)
193 setSize(requestedCoord.size());
194 else
195 setCoord(requestedCoord);
196
197 if (!moveOnly)
198 restoreResizeAnchor();
199
200 // send event about position and size change
201 eventWindowChangeCoord(this);
202 }
203
204 void Window::updateAlpha()
205 {
206 if (!mIsAutoAlpha)
207 return;
208
209 float alpha;
210 if (mKeyRootFocus)
211 alpha = WINDOW_ALPHA_ACTIVE;
212 else if (mMouseRootFocus)
213 alpha = WINDOW_ALPHA_FOCUS;
214 else
215 alpha = WINDOW_ALPHA_DEACTIVE;
216
217 ControllerFadeAlpha* controller = createControllerFadeAlpha(alpha, WINDOW_SPEED_COEF, true);
218 ControllerManager::getInstance().addItem(this, controller);
219 }
220
221 void Window::setAutoAlpha(bool _auto)
222 {
223 mIsAutoAlpha = _auto;
224 if (!_auto)
225 setAlpha(ALPHA_MAX);
226 else
227 {
228 if (mKeyRootFocus)
229 setAlpha(WINDOW_ALPHA_ACTIVE);
230 else if (mMouseRootFocus)
231 setAlpha(WINDOW_ALPHA_FOCUS);
232 else
233 setAlpha(WINDOW_ALPHA_DEACTIVE);
234 }
235 }
236
237 void Window::setPosition(const IntPoint& _point)
238 {
239 IntPoint point = _point;
240 // snap to edges
241 if (mSnap)
242 {
243 IntCoord coord(point, mCoord.size());
244 getSnappedCoord(coord, Snap::Position);
245 point = coord.point();
246 }
247
248 Base::setPosition(point);
249 }
250
251 void Window::setSize(const IntSize& _size)
252 {
253 IntSize size = _size;
254 // snap to edges
255
256 if (size.width < mMinmax.left)
257 size.width = mMinmax.left;
258 else if (size.width > mMinmax.right)
259 size.width = mMinmax.right;
260 if (size.height < mMinmax.top)
261 size.height = mMinmax.top;
262 else if (size.height > mMinmax.bottom)
263 size.height = mMinmax.bottom;
264 if ((size.width == mCoord.width) && (size.height == mCoord.height))
265 return;
266
267 if (mSnap)
268 {
269 IntCoord coord(mCoord.point(), size);
270 getSnappedCoord(coord, Snap::Size);
271 size = coord.size();
272 }
273
274 Base::setSize(size);
275 }
276
277 void Window::setCoord(const IntCoord& _coord)
278 {
279 IntPoint pos = _coord.point();
280 IntSize size = _coord.size();
281
282 if (size.width < mMinmax.left)
283 {
284 int offset = mMinmax.left - size.width;
285 size.width = mMinmax.left;
286 if ((pos.left - mCoord.left) > offset)
287 pos.left -= offset;
288 else
289 pos.left = mCoord.left;
290 }
291 else if (size.width > mMinmax.right)
292 {
293 int offset = mMinmax.right - size.width;
294 size.width = mMinmax.right;
295 if ((pos.left - mCoord.left) < offset)
296 pos.left -= offset;
297 else
298 pos.left = mCoord.left;
299 }
300 if (size.height < mMinmax.top)
301 {
302 int offset = mMinmax.top - size.height;
303 size.height = mMinmax.top;
304 if ((pos.top - mCoord.top) > offset)
305 pos.top -= offset;
306 else
307 pos.top = mCoord.top;
308 }
309 else if (size.height > mMinmax.bottom)
310 {
311 int offset = mMinmax.bottom - size.height;
312 size.height = mMinmax.bottom;
313 if ((pos.top - mCoord.top) < offset)
314 pos.top -= offset;
315 else
316 pos.top = mCoord.top;
317 }
318
319 IntCoord coord(pos, size);
320 if (mSnap)
321 {
322 // prefer size change over position change
323 getSnappedCoord(coord, Snap::Size);
324 }
325
326 if (coord == mCoord)
327 return;
328
329 Base::setCoord(coord);
330 }
331
332 void Window::setCaption(const UString& _caption)
333 {
334 if (mWidgetCaption != nullptr)
335 mWidgetCaption->setCaption(_caption);
336 else
337 Base::setCaption(_caption);
338 }
339
340 const UString& Window::getCaption() const
341 {
342 if (mWidgetCaption != nullptr)
343 return mWidgetCaption->getCaption();
344 return Base::getCaption();
345 }
346
347 void Window::destroySmooth()
348 {
349 ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, WINDOW_SPEED_COEF, false);
350 controller->eventPostAction += newDelegate(action::actionWidgetDestroy);
351 ControllerManager::getInstance().addItem(this, controller);
352 }
353
354 void Window::animateStop(Widget* _widget, ControllerItem* _controller)
355 {
356 if (mAnimateSmooth)
357 {
358 ControllerManager::getInstance().removeItem(this);
359 mAnimateSmooth = false;
360 }
361 }
362
363 void Window::setVisible(bool _visible)
364 {
365 if (mAnimateSmooth)
366 {
367 ControllerManager::getInstance().removeItem(this);
368 setAlpha(getAlphaVisible());
369 setEnabledSilent(true);
370 mAnimateSmooth = false;
371 }
372
373 Base::setVisible(_visible);
374 }
375
376 float Window::getAlphaVisible() const
377 {
378 return (mIsAutoAlpha && !mKeyRootFocus) ? WINDOW_ALPHA_DEACTIVE : ALPHA_MAX;
379 }
380
381 void Window::getSnappedCoord(IntCoord& _coord, Snap snapMode) const
382 {
383 const IntSize view_size = getParentSize();
384 bool nearLeftSide = abs(_coord.left) <= WINDOW_SNAP_DISTANSE;
385 bool nearTopSide = abs(_coord.top) <= WINDOW_SNAP_DISTANSE;
386 bool nearRightSide = abs(_coord.left + _coord.width - view_size.width) <= WINDOW_SNAP_DISTANSE;
387 bool nearBottomSide = abs(_coord.top + _coord.height - view_size.height) <= WINDOW_SNAP_DISTANSE;
388
389 switch (snapMode)
390 {
391 case Snap::Position:
392 if (nearLeftSide)
393 _coord.left = 0;
394 if (nearTopSide)
395 _coord.top = 0;
396
397 if (nearRightSide)
398 _coord.left = view_size.width - _coord.width;
399 if (nearBottomSide)
400 _coord.top = view_size.height - _coord.height;
401 break;
402 case Snap::Size:
403 if (nearLeftSide)
404 {
405 _coord.width = _coord.right();
406 _coord.left = 0;
407 }
408 if (nearTopSide)
409 {
410 _coord.height = _coord.bottom();
411 _coord.top = 0;
412 }
413
414 if (nearRightSide)
415 _coord.width = view_size.width - _coord.left;
416 if (nearBottomSide)
417 _coord.height = view_size.height - _coord.top;
418 break;
419 }
420 }
421
422 void Window::setVisibleSmooth(bool _visible)
423 {
424 mAnimateSmooth = true;
425 ControllerManager::getInstance().removeItem(this);
426
427 if (_visible)
428 {
429 setEnabledSilent(true);
430 if (!getVisible())
431 {
432 setAlpha(ALPHA_MIN);
433 Base::setVisible(true);
434 }
435 ControllerFadeAlpha* controller = createControllerFadeAlpha(getAlphaVisible(), WINDOW_SPEED_COEF, true);
436 controller->eventPostAction += newDelegate(this, &Window::animateStop);
437 ControllerManager::getInstance().addItem(this, controller);
438 }
439 else
440 {
441 setEnabledSilent(false);
442 ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, WINDOW_SPEED_COEF, false);
443 controller->eventPostAction += newDelegate(action::actionWidgetHide);
444 ControllerManager::getInstance().addItem(this, controller);
445 }
446 }
447
448 ControllerFadeAlpha* Window::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
449 {
450 ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
451 ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
452
453 controller->setAlpha(_alpha);
454 controller->setCoef(_coef);
455 controller->setEnabled(_enable);
456
457 return controller;
458 }
459
460 void Window::setMinSize(const IntSize& _value)
461 {
462 mMinmax.left = _value.width;
463 mMinmax.top = _value.height;
464 }
465
466 IntSize Window::getMinSize() const
467 {
468 return {mMinmax.left, mMinmax.top};
469 }
470
471 void Window::setMaxSize(const IntSize& _value)
472 {
473 mMinmax.right = _value.width;
474 mMinmax.bottom = _value.height;
475 }
476
477 IntSize Window::getMaxSize() const
478 {
479 return {mMinmax.right, mMinmax.bottom};
480 }
481
482 void Window::setPropertyOverride(std::string_view _key, std::string_view _value)
483 {
485 if (_key == "AutoAlpha")
486 setAutoAlpha(utility::parseValue<bool>(_value));
487
489 else if (_key == "Snap")
490 setSnap(utility::parseValue<bool>(_value));
491
493 else if (_key == "MinSize")
494 setMinSize(utility::parseValue<IntSize>(_value));
495
497 else if (_key == "MaxSize")
498 setMaxSize(utility::parseValue<IntSize>(_value));
499
501 else if (_key == "Movable")
502 setMovable(utility::parseValue<bool>(_value));
503
504 else
505 {
506 Base::setPropertyOverride(_key, _value);
507 return;
508 }
509
510 eventChangeProperty(this, _key, _value);
511 }
512
513 const IntCoord& Window::getActionScale() const
514 {
515 return mCurrentActionScale;
516 }
517
518 bool Window::getAutoAlpha() const
519 {
520 return mIsAutoAlpha;
521 }
522
523 TextBox* Window::getCaptionWidget() const
524 {
525 return mWidgetCaption;
526 }
527
528 void Window::setMinSize(int _width, int _height)
529 {
530 setMinSize(IntSize(_width, _height));
531 }
532
533 void Window::setMaxSize(int _width, int _height)
534 {
535 setMaxSize(IntSize(_width, _height));
536 }
537
538 bool Window::getSnap() const
539 {
540 return mSnap;
541 }
542
543 void Window::setSnap(bool _value)
544 {
545 mSnap = _value;
546 }
547
548 void Window::notifyMouseReleased(MyGUI::Widget* _sender, int _left, int _top, MouseButton _id)
549 {
550 if (MouseButton::Left == _id)
551 {
552 mCurrentActionScale.clear();
553 }
554 }
555
556 IntCoord Window::_getActionScale(Widget* _widget) const
557 {
558 if (_widget->isUserString("Scale"))
559 {
560 IntCoord result = IntCoord::parse(_widget->getUserString("Scale"));
561
562 if (result == IntCoord(1, 1, 0, 0) && !mMovable)
563 result.clear();
564
565 return result;
566 }
567 if (_widget->isUserString("Action"))
568 {
569 std::string_view action = _widget->getUserString("Action");
570 if (action == "Move")
571 {
572 if (mMovable)
573 return {1, 1, 0, 0};
574 return {};
575 }
576
577 IntCoord coord;
578 Align align = Align::parse(action);
579
580 if (align.isLeft())
581 {
582 coord.left = 1;
583 coord.width = -1;
584 }
585 else if (align.isRight())
586 {
587 coord.width = 1;
588 }
589
590 if (align.isTop())
591 {
592 coord.top = 1;
593 coord.height = -1;
594 }
595 else if (align.isBottom())
596 {
597 coord.height = 1;
598 }
599
600 return coord;
601 }
602
603 return {};
604 }
605
606 FloatPoint Window::getResizeAnchor() const
607 {
608 return FloatPoint(
609 (float)(mCurrentActionScale.width < 0 ? mCoord.width : 0),
610 (float)(mCurrentActionScale.height < 0 ? mCoord.height : 0));
611 }
612
613 void Window::restoreResizeAnchor()
614 {
615 // Resizing can move the rotation pivot; restore the opposite displayed corner.
616 const FloatPoint currentAnchor = localToLayer(getResizeAnchor());
617 const Widget* parent = getParent();
618 const FloatPoint targetInParent = parent ? parent->layerToLocal(mPreActionAnchor) : mPreActionAnchor;
619 const FloatPoint currentInParent = parent ? parent->layerToLocal(currentAnchor) : currentAnchor;
620 const IntPoint correction(
621 (int)std::lround(targetInParent.left - currentInParent.left),
622 (int)std::lround(targetInParent.top - currentInParent.top));
623 if (correction != IntPoint())
624 {
625 // Bypass window snapping so the anchor correction is applied exactly.
626 Base::setPosition(mCoord.point() + correction);
627 }
628 }
629
630 void Window::setMovable(bool _value)
631 {
632 mMovable = _value;
633 }
634
635 bool Window::getMovable() const
636 {
637 return mMovable;
638 }
639
640 void Window::notifyMouseWheel(MyGUI::Widget* _sender, int _rel)
641 {
642 onMouseWheel(_rel);
643 eventMouseWheel(_sender, _rel);
644 }
645
646} // namespace MyGUI
void actionWidgetDestroy(Widget *_widget, ControllerItem *_controller)
void actionWidgetHide(Widget *_widget, ControllerItem *_controller)
T parseValue(std::string_view value)
constexpr float ALPHA_MAX
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
constexpr float ALPHA_MIN
const float WINDOW_ALPHA_FOCUS
const float WINDOW_SPEED_COEF
const float WINDOW_ALPHA_ACTIVE
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
const float WINDOW_ALPHA_DEACTIVE
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
const int WINDOW_SNAP_DISTANSE
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
std::vector< Widget * > VectorWidgetPtr
static TCoord< int > parse(std::string_view _value)