| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../IO/Log.h"
- #include "../Input/InputEvents.h"
- #include "UIEvents.h"
- #include "UI.h"
- #include "UIWidget.h"
- #include "UILayout.h"
- #include "UIFontDescription.h"
- #include "UIView.h"
- #include "UISelectItem.h"
- using namespace tb;
- namespace Atomic
- {
- UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
- widget_(0),
- preferredSize_(new UIPreferredSize()),
- multiTouch_(false)
- {
- if (createWidget)
- {
- widget_ = new TBWidget();
- widget_->SetDelegate(this);
- GetSubsystem<UI>()->WrapWidget(this, widget_);
- }
- }
- UIWidget::~UIWidget()
- {
- }
- void UIWidget::SetIsFocusable(bool value)
- {
- if (!widget_)
- return;
- widget_->SetIsFocusable(value);
- }
- bool UIWidget::Load(const String& filename)
- {
- UI* ui = GetSubsystem<UI>();
- if (!ui->LoadResourceFile(widget_ , filename))
- return false;
- VariantMap eventData;
- eventData[WidgetLoaded::P_WIDGET] = this;
- SendEvent(E_WIDGETLOADED, eventData);
- return true;
- }
- UIPreferredSize* UIWidget::GetPreferredSize()
- {
- // error
- if (!widget_)
- return preferredSize_;
- preferredSize_->SetFromTBPreferredSize(widget_->GetPreferredSize());
- return preferredSize_;
- }
- UIWidget* UIWidget::GetWidget(const String& id)
- {
- if (!widget_)
- return 0;
- TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
- if (!child)
- return 0;
- UI* ui = GetSubsystem<UI>();
- return ui->WrapWidget(child);
- }
- void UIWidget::SetWidget(tb::TBWidget* widget)
- {
- widget_ = widget;
- widget_->SetDelegate(this);
- }
- /*
- enum SPECIAL_KEY
- {
- TB_KEY_UNDEFINED = 0,
- TB_KEY_UP, TB_KEY_DOWN, TB_KEY_LEFT, TB_KEY_RIGHT,
- TB_KEY_PAGE_UP, TB_KEY_PAGE_DOWN, TB_KEY_HOME, TB_KEY_END,
- TB_KEY_TAB, TB_KEY_BACKSPACE, TB_KEY_INSERT, TB_KEY_DELETE,
- TB_KEY_ENTER, TB_KEY_ESC,
- TB_KEY_F1, TB_KEY_F2, TB_KEY_F3, TB_KEY_F4, TB_KEY_F5, TB_KEY_F6,
- TB_KEY_F7, TB_KEY_F8, TB_KEY_F9, TB_KEY_F10, TB_KEY_F11, TB_KEY_F12
- };
- */
- void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
- {
- UI* ui = GetSubsystem<UI>();
- String refid;
- ui->GetTBIDString(ev.ref_id, refid);
- int key = ev.key;
- if (ev.special_key)
- {
- switch (ev.special_key)
- {
- case TB_KEY_ENTER:
- key = KEY_RETURN;
- break;
- case TB_KEY_BACKSPACE:
- key = KEY_BACKSPACE;
- break;
- case TB_KEY_DELETE:
- key = KEY_DELETE;
- break;
- case TB_KEY_DOWN:
- key = KEY_DOWN;
- break;
- case TB_KEY_UP:
- key = KEY_UP;
- break;
- case TB_KEY_LEFT:
- key = KEY_LEFT;
- break;
- case TB_KEY_RIGHT:
- key = KEY_RIGHT;
- break;
- default:
- break;
- }
- }
- unsigned modifierKeys = 0;
- if( ev.special_key && TB_SHIFT)
- modifierKeys |= QUAL_SHIFT;
- if( ev.special_key && TB_CTRL)
- modifierKeys |= QUAL_CTRL;
- if( ev.special_key && TB_ALT)
- modifierKeys |= QUAL_ALT;
- using namespace WidgetEvent;
- data[P_HANDLER] = handler;
- data[P_TARGET] = target;
- data[P_TYPE] = (unsigned) ev.type;
- data[P_X] = ev.target_x;
- data[P_Y] = ev.target_y;
- data[P_DELTAX] = ev.delta_x;
- data[P_DELTAY] = ev.delta_y;
- data[P_COUNT] = ev.count;
- data[P_KEY] = key;
- data[P_SPECIALKEY] = (unsigned) ev.special_key;
- data[P_MODIFIERKEYS] = modifierKeys;
- data[P_REFID] = refid;
- data[P_TOUCH] = (unsigned) ev.touch;
- }
- void UIWidget::OnDelete()
- {
- VariantMap eventData;
- eventData[WidgetDeleted::P_WIDGET] = this;
- this->SendEvent(E_WIDGETDELETED, eventData);
- UnsubscribeFromAllEvents();
- if (widget_)
- {
- // if we don't have a UI subsystem, we are exiting
- UI* ui = GetSubsystem<UI>();
- if (ui)
- {
- ui->UnwrapWidget(widget_);
- }
- }
- widget_ = 0;
- }
- void UIWidget::AddChildAfter(UIWidget* child, UIWidget* otherChild)
- {
- if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
- return;
- widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_AFTER, otherChild->widget_);
- }
- void UIWidget::AddChildBefore(UIWidget* child, UIWidget* otherChild)
- {
- if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
- return;
- widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_BEFORE, otherChild->widget_);
- }
- void UIWidget::AddChild(UIWidget* child)
- {
- if (!widget_ || !child || !child->widget_)
- return;
- widget_->AddChild(child->widget_);
- }
- void UIWidget::AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference)
- {
- if (!widget_ || !child || !child->widget_ || !reference || !reference->widget_)
- return;
- widget_->AddChildRelative(child->widget_, (WIDGET_Z_REL) z, reference->widget_);
- }
- String UIWidget::GetText()
- {
- if (!widget_)
- return String::EMPTY;
- return widget_->GetText().CStr();
- }
- void UIWidget::SetText(const String& text)
- {
- if (!widget_)
- return;
- widget_->SetText(text.CString());
- }
- void UIWidget::SetGravity(UI_GRAVITY gravity)
- {
- if (!widget_)
- return;
- widget_->SetGravity((WIDGET_GRAVITY) gravity);
- }
- void UIWidget::SetAxis(UI_AXIS axis)
- {
- if (!widget_)
- return;
- widget_->SetAxis((AXIS) axis);
- }
- bool UIWidget::IsAncestorOf(UIWidget* widget)
- {
- if (!widget_ || !widget || !widget->widget_)
- return false;
- return widget_->IsAncestorOf(widget->widget_);
- }
- void UIWidget::SetPosition(int x, int y)
- {
- if (!widget_)
- return;
- widget_->SetPosition(TBPoint(x, y));
- }
- IntRect UIWidget::GetRect()
- {
- IntRect rect(0, 0, 0, 0);
- if (!widget_)
- return rect;
- tb::TBRect tbrect = widget_->GetRect();
- rect.top_ = tbrect.y;
- rect.left_ = tbrect.x;
- rect.right_ = tbrect.x + tbrect.w;
- rect.bottom_ = tbrect.y + tbrect.h;
- return rect;
- }
- void UIWidget::SetRect(IntRect rect)
- {
- if (!widget_)
- return;
- tb::TBRect tbrect;
- tbrect.y = rect.top_;
- tbrect.x = rect.left_;
- tbrect.w = rect.right_ - rect.left_;
- tbrect.h = rect.bottom_ - rect.top_;
- widget_->SetRect(tbrect);
- }
- bool UIWidget::SetSize(int width, int height)
- {
- if (!widget_)
- return false;
- widget_->SetSize(width, height);
- return true;
- }
- void UIWidget::Invalidate()
- {
- if (!widget_)
- return;
- widget_->Invalidate();
- }
- /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
- UIWidget *UIWidget::FindWidget ( const String& searchid )
- {
- if (!widget_)
- return NULL;
- TBWidget* child = widget_->FindWidget(TBID(searchid.CString()));
- if (!child)
- return 0;
- UI* ui = GetSubsystem<UI>();
- return ui->WrapWidget(child);
- }
- void UIWidget::PrintPrettyTree()
- {
- if (!widget_)
- return;
- widget_->PrintPretty("", true);
- }
- /// return all of the widgets of the specified classname
- void UIWidget::SearchWidgetClass ( const String& className, PODVector<UIWidget*> &results )
- {
- results.Clear();
- if (!widget_)
- return;
- tb::TBValue tbval(TBValue::TYPE_ARRAY); // TB array of values
- tbval.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC); // dont delete pointers on destruction
- widget_->SearchWidgetClass(className.CString(), tbval ); // visit all children for search
- UI* ui = GetSubsystem<UI>();
- int nn=0;
- for ( nn=0; nn<tbval.GetArrayLength(); nn++ ) // copy tbwidget ptr to uiwidget ptr
- {
- tb::TBWidget *tbw = (tb::TBWidget *)tbval.GetArray()->GetValue(nn)->GetObject();
- UIWidget *wrp = ui->WrapWidget(tbw);
- results.Push( wrp );
- }
- }
- /// return all of the widgets of the specified id
- void UIWidget::SearchWidgetId ( const String& searchid, PODVector<UIWidget*> &results )
- {
- results.Clear();
- if (!widget_)
- return;
- tb::TBValue tbval(TBValue::TYPE_ARRAY);
- tbval.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
- widget_->SearchWidgetId(TBID(searchid.CString()), tbval );
- UI* ui = GetSubsystem<UI>();
- int nn=0;
- for ( nn=0; nn<tbval.GetArrayLength(); nn++ )
- {
- tb::TBWidget *tbw = (tb::TBWidget *)tbval.GetArray()->GetValue(nn)->GetObject();
- UIWidget *wrp = ui->WrapWidget(tbw);
- results.Push( wrp );
- }
- }
- /// return all of the widgets with the specified text
- void UIWidget::SearchWidgetText ( const String& searchText, PODVector<UIWidget*> &results )
- {
- results.Clear();
- if (!widget_)
- return;
- tb::TBValue tbval(TBValue::TYPE_ARRAY);
- tbval.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
- widget_->SearchWidgetText(searchText.CString(), tbval );
- UI* ui = GetSubsystem<UI>();
- int nn=0;
- for ( nn=0; nn<tbval.GetArrayLength(); nn++ )
- {
- tb::TBWidget *tbw = (tb::TBWidget *)tbval.GetArray()->GetValue(nn)->GetObject();
- UIWidget *wrp = ui->WrapWidget(tbw);
- results.Push( wrp );
- }
- }
- void UIWidget::Center()
- {
- if (!widget_)
- return;
- TBRect rect = widget_->GetRect();
- TBWidget* root = widget_->GetParent();
- if (!root)
- {
- UI* ui = GetSubsystem<UI>();
- root = ui->GetRootWidget();
- }
- TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
- widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
- }
- UIWidget* UIWidget::GetParent()
- {
- if (!widget_)
- return 0;
- TBWidget* parent = widget_->GetParent();
- if (!parent)
- return 0;
- UI* ui = GetSubsystem<UI>();
- return ui->WrapWidget(parent);
- }
- UIWidget* UIWidget::GetContentRoot()
- {
- if (!widget_)
- return 0;
- TBWidget* root = widget_->GetContentRoot();
- if (!root)
- return 0;
- UI* ui = GetSubsystem<UI>();
- return ui->WrapWidget(root);
- }
- void UIWidget::Die()
- {
- if (!widget_)
- return;
- // clear delegate
- widget_->SetDelegate(NULL);
- // explictly die (can trigger an animation)
- widget_->Die();
- // call OnDelete, which unwraps the widget and does some bookkeeping
- OnDelete();
- }
- void UIWidget::SetLayoutParams(UILayoutParams* params)
- {
- if (!widget_)
- return;
- widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
- }
- void UIWidget::SetFontDescription(UIFontDescription* fd)
- {
- if (!widget_)
- return;
- widget_->SetFontDescription(*(fd->GetTBFontDescription()));
- }
- void UIWidget::SetFontId(const String& fontId)
- {
- if (!widget_)
- return;
- tb::TBFontDescription fd(widget_->GetFontDescription());
- fd.SetID(fontId.CString());
- widget_->SetFontDescription(fd);
- }
- String UIWidget::GetFontId()
- {
- if (!widget_)
- return "";
- tb::TBFontDescription fd(widget_->GetFontDescription());
- if (!g_font_manager->HasFontFace(fd))
- {
- return "";
- }
- return g_font_manager->GetFontInfo(fd.GetID())->GetName();
- }
- void UIWidget::SetFontSize(int size)
- {
- if (!widget_)
- return;
- tb::TBFontDescription fd(widget_->GetFontDescription());
- fd.SetSize(size);
- widget_->SetFontDescription(fd);
- }
- int UIWidget::GetFontSize()
- {
- if (!widget_)
- return 0;
- tb::TBFontDescription fd(widget_->GetFontDescription());
- return fd.GetSize();
- }
- void UIWidget::SetLayoutWidth(int width)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.SetWidth(width);
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutWidth()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->pref_w;
- }
- void UIWidget::SetLayoutHeight(int height)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.SetHeight(height);
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutHeight()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->pref_h;
- }
- void UIWidget::SetLayoutPrefWidth(int width)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.pref_w = width;
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutPrefWidth()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->pref_w;
- }
- void UIWidget::SetLayoutPrefHeight(int height)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.pref_h = height;
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutPrefHeight()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->pref_h;
- }
- void UIWidget::SetLayoutMinWidth(int width)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.min_w = width;
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutMinWidth()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->min_w;
- }
- void UIWidget::SetLayoutMinHeight(int height)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.min_h = height;
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutMinHeight()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->min_h;
- }
- void UIWidget::SetLayoutMaxWidth(int width)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.max_w = width;
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutMaxWidth()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->max_w;
- }
- void UIWidget::SetLayoutMaxHeight(int height)
- {
- if (!widget_)
- return;
- tb::LayoutParams lp;
- const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
- if (oldLp)
- lp = *oldLp;
- lp.max_h = height;
- widget_->SetLayoutParams(lp);
- }
- int UIWidget::GetLayoutMaxHeight()
- {
- if (!widget_)
- return tb::LayoutParams::UNSPECIFIED;
- const tb::LayoutParams *lp(widget_->GetLayoutParams());
- if (!lp)
- return tb::LayoutParams::UNSPECIFIED;
- return lp->max_h;
- }
- void UIWidget::SetOpacity(float opacity)
- {
- if (!widget_)
- return;
- widget_->SetOpacity(opacity);
- }
- float UIWidget::GetOpacity()
- {
- if (!widget_)
- return -0.0f;
- return widget_->GetOpacity();
- }
- void UIWidget::SetAutoOpacity(float autoOpacity)
- {
- if (!widget_)
- return;
- if (autoOpacity == 0.0f)
- {
- widget_->SetOpacity(autoOpacity);
- widget_->SetVisibilility(tb::WIDGET_VISIBILITY_INVISIBLE);
- }
- else
- {
- widget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
- widget_->SetOpacity(autoOpacity);
- }
- }
- float UIWidget::GetAutoOpacity()
- {
- if (!widget_)
- return -0.0f;
- float autoOpacity(widget_->GetOpacity());
- if (autoOpacity == 0.0f)
- {
- if (widget_->GetVisibility() == tb::WIDGET_VISIBILITY_VISIBLE)
- return 0.0001f; // Don't say that it's completly invisible.
- }
- else
- {
- if (widget_->GetVisibility() != tb::WIDGET_VISIBILITY_VISIBLE)
- return 0.0f; // Say it's invisible.
- }
- return autoOpacity;
- }
- void UIWidget::DeleteAllChildren()
- {
- if (!widget_)
- return;
- widget_->DeleteAllChildren();
- }
- void UIWidget::SetSkinBg(const String& id)
- {
- if (!widget_)
- return;
- widget_->SetSkinBg(TBIDC(id.CString()));
- }
- void UIWidget::Remove()
- {
- if (!widget_ || !widget_->GetParent())
- return;
- widget_->GetParent()->RemoveChild(widget_);
- }
- void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
- {
- if (!widget_ || !child)
- return;
- TBWidget* childw = child->GetInternalWidget();
- if (!childw)
- return;
- widget_->RemoveChild(childw);
- if (cleanup)
- delete childw;
- }
- const String& UIWidget::GetId()
- {
- if (!widget_ || !widget_->GetID())
- {
- if (id_.Length())
- id_.Clear();
- return id_;
- }
- if (id_.Length())
- return id_;
- UI* ui = GetSubsystem<UI>();
- ui->GetTBIDString(widget_->GetID(), id_);
- return id_;
- }
- void UIWidget::SetId(const String& id)
- {
- if (!widget_)
- {
- if (id_.Length())
- id_.Clear();
- return;
- }
- id_ = id;
- widget_->SetID(TBIDC(id.CString()));
- }
- void UIWidget::SetState(UI_WIDGET_STATE state, bool on)
- {
- if (!widget_)
- return;
- widget_->SetState((WIDGET_STATE) state, on);
- }
- void UIWidget::SetFocus()
- {
- if (!widget_)
- return;
- widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
- }
- bool UIWidget::GetFocus() const
- {
- if (!widget_)
- return false;
- return widget_->GetIsFocused();
- }
- void UIWidget::SetFocusRecursive()
- {
- if (!widget_)
- return;
- widget_->SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
- }
- void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
- {
- if (!widget_)
- return;
- widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
- }
- UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
- {
- if (!widget_)
- return UI_WIDGET_VISIBILITY_GONE;
- return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
- }
- UIWidget* UIWidget::GetFirstChild()
- {
- if (!widget_)
- return NULL;
- return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
- }
- UIWidget* UIWidget::GetNext()
- {
- if (!widget_)
- return NULL;
- return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
- }
- void UIWidget::SetValue(double value)
- {
- if (!widget_)
- return;
- widget_->SetValueDouble(value);
- }
- double UIWidget::GetValue()
- {
- if (!widget_)
- return 0.0;
- return widget_->GetValueDouble();
- }
- void UIWidget::Enable()
- {
- if (!widget_)
- return;
- widget_->SetState(WIDGET_STATE_DISABLED, false);
- }
- void UIWidget::Disable()
- {
- if (!widget_)
- return;
- widget_->SetState(WIDGET_STATE_DISABLED, true);
- }
- bool UIWidget::GetState(UI_WIDGET_STATE state)
- {
- if (!widget_)
- return false;
- return widget_->GetState((WIDGET_STATE) state);
- }
- void UIWidget::SetStateRaw(UI_WIDGET_STATE state)
- {
- if (!widget_)
- return;
- widget_->SetStateRaw((WIDGET_STATE) state);
- }
- UI_WIDGET_STATE UIWidget::GetStateRaw()
- {
- if (!widget_)
- return UI_WIDGET_STATE_NONE;
- return (UI_WIDGET_STATE) widget_->GetStateRaw();
- }
- UIView* UIWidget::GetView()
- {
- if (!widget_)
- return 0;
- if (GetType() == UIView::GetTypeStatic())
- return (UIView*) this;
- TBWidget* tbw = widget_->GetParent();
- while(tbw)
- {
- TBWidgetDelegate* delegate = tbw->GetDelegate();
- if (delegate)
- {
- UIWidget* d = (UIWidget*) delegate;
- if (d->GetType() == UIView::GetTypeStatic())
- return (UIView*) d;
- }
- tbw = tbw->GetParent();
- }
- return 0;
- }
- void UIWidget::OnResized(int old_w, int old_h)
- {
- // default implementation does nothing
- }
- void UIWidget::OnFocusChanged(bool focused)
- {
- using namespace WidgetFocusChanged;
- VariantMap eventData;
- eventData[P_WIDGET] = this;
- eventData[P_FOCUSED] = focused;
- SendEvent(E_WIDGETFOCUSCHANGED, eventData);
- }
- bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
- {
- UI* ui = GetSubsystem<UI>();
- if ((ev.type == EVENT_TYPE_CHANGED && !ui->GetBlockChangedEvents()) || ev.type == EVENT_TYPE_KEY_UP)
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- else if (ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- else if (ev.type == EVENT_TYPE_POINTER_DOWN)
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- else if (ev.type == EVENT_TYPE_SHORTCUT)
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- else if (ev.type == EVENT_TYPE_TAB_CHANGED)
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- else if (ev.type == EVENT_TYPE_CLICK)
- {
- if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
- {
- // popup menu
- if (JSGetHeapPtr())
- {
- VariantMap eventData;
- eventData[PopupMenuSelect::P_BUTTON] = this;
- String id;
- ui->GetTBIDString(ev.ref_id, id);
- eventData[PopupMenuSelect::P_REFID] = id;
- SendEvent(E_POPUPMENUSELECT, eventData);
- }
- return true;
- }
- else
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- }
- if (ev.type == EVENT_TYPE_CUSTOM)
- {
- if (!ev.target || ui->IsWidgetWrapped(ev.target))
- {
- VariantMap eventData;
- ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
- SendEvent(E_WIDGETEVENT, eventData);
- if (eventData[WidgetEvent::P_HANDLED].GetBool())
- return true;
- }
- }
- return false;
- }
- bool UIWidget::GetCaptured()
- {
- if (!widget_)
- return false;
- return widget_->IsCaptured();
- }
- void UIWidget::SetCapturing(bool capturing)
- {
- if (!widget_)
- return;
- widget_->SetCapturing(capturing);
- }
- bool UIWidget::GetCapturing()
- {
- if (!widget_)
- return false;
- return widget_->GetCapturing();
- }
- void UIWidget::InvalidateLayout()
- {
- if (!widget_)
- return;
- widget_->InvalidateLayout(tb::TBWidget::INVALIDATE_LAYOUT_TARGET_ONLY);
- }
- void UIWidget::InvokeShortcut(const String& shortcut)
- {
- TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
- ev.ref_id = TBIDC(shortcut.CString());
- widget_->OnEvent(ev);
- }
- bool UIWidget::GetShortened()
- {
- if (!widget_)
- return false;
- return widget_->GetShortened();
- }
- void UIWidget::SetShortened(bool shortened)
- {
- if (!widget_)
- return;
- widget_->SetShortened(shortened);
- }
- String UIWidget::GetTooltip()
- {
- if (!widget_)
- return String::EMPTY;
- return widget_->GetTooltip().CStr();
- }
- void UIWidget::SetTooltip(const String& tooltip)
- {
- if (!widget_)
- return;
- widget_->SetTooltip(tooltip.CString());
- }
- IntVector2 UIWidget::ConvertToRoot(const IntVector2 position) const
- {
- IntVector2 result = position;
- if (widget_)
- widget_->ConvertToRoot(result.x_, result.y_);
- return result;
- }
- IntVector2 UIWidget::ConvertFromRoot(const IntVector2 position) const
- {
- IntVector2 result = position;
- if (widget_)
- widget_->ConvertFromRoot(result.x_, result.y_);
- return result;
- }
- }
|