| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302 |
- //
- // 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
- /// only cpp can get full access to the widget pointers by using the
- /// TBGenericStringItemSource that is inside the UISelectItemSource
- /// and pulling outthe tag.GetObject().
- void UIWidget::SearchWidgetClass ( const String& className, UISelectItemSource *results )
- {
- if (!widget_)
- return;
- // we are going to fill out the TBGenericStringItemSource with all the data
- tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
- widget_->SearchWidgetClass(className.CString(), myuis );
- UI* ui = GetSubsystem<UI>(); // but copy the named id'd ones back into the UISelectItemSource
- int nn=0;
- for ( nn=0; nn<myuis->GetNumItems(); nn++ )
- {
- String idstr;
- ui->GetTBIDString( myuis->GetItemID(nn), idstr);
- if ( !idstr.Empty() )
- {
- results->AddItem ( new UISelectItem (context_, idstr, idstr) );
- }
- }
- }
- /// return all of the widgets of the specified id
- void UIWidget::SearchWidgetId ( const String& searchid, UISelectItemSource *results )
- {
- if (!widget_)
- return;
- tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
- widget_->SearchWidgetId(TBID(searchid.CString()),myuis );
- UI* ui = GetSubsystem<UI>();
- int nn=0;
- for ( nn=0; nn<myuis->GetNumItems(); nn++ )
- {
- String idstr;
- ui->GetTBIDString( myuis->GetItemID(nn), idstr);
- if ( !idstr.Empty() )
- {
- results->AddItem ( new UISelectItem (context_, idstr, idstr) );
- }
- }
- }
- /// return all of the widgets with the specified text
- void UIWidget::SearchWidgetText ( const String& searchText, UISelectItemSource *results )
- {
- if (!widget_)
- return;
- tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
- widget_->SearchWidgetText(searchText.CString(), myuis );
- UI* ui = GetSubsystem<UI>();
- int nn=0;
- for ( nn=0; nn<myuis->GetNumItems(); nn++ )
- {
- String idstr;
- ui->GetTBIDString( myuis->GetItemID(nn), idstr);
- if ( !idstr.Empty() )
- {
- results->AddItem ( new UISelectItem (context_, idstr, idstr) );
- }
- }
- }
- 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::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;
- }
- }
|