| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- #include <TurboBadger/tb_core.h>
- #include <TurboBadger/tb_system.h>
- #include <TurboBadger/tb_debug.h>
- #include <TurboBadger/animation/tb_widget_animation.h>
- #include <TurboBadger/renderers/tb_renderer_batcher.h>
- #include <TurboBadger/tb_font_renderer.h>
- #include <TurboBadger/tb_node_tree.h>
- #include <TurboBadger/tb_widgets_reader.h>
- #include <TurboBadger/tb_window.h>
- void register_tbbf_font_renderer();
- void register_stb_font_renderer();
- void register_freetype_font_renderer();
- using namespace tb;
- #include "../Core/CoreEvents.h"
- #include "../Input/Input.h"
- #include "../Input/InputEvents.h"
- #include "../Resource/ResourceCache.h"
- #include "../Graphics/Graphics.h"
- #include "../Graphics/GraphicsEvents.h"
- #include "../Graphics/Texture2D.h"
- #include "../Graphics/VertexBuffer.h"
- #include "UIRenderer.h"
- #include "UI.h"
- #include "UIButton.h"
- #include "UITextField.h"
- namespace tb
- {
- void TBSystem::RescheduleTimer(double fire_time)
- {
- }
- }
- namespace Atomic
- {
- WeakPtr<Context> UI::readerContext_;
- UI::UI(Context* context) :
- Object(context),
- rootWidget_(0),
- inputDisabled_(false),
- keyboardDisabled_(false),
- initialized_(false)
- {
- }
- UI::~UI()
- {
- if (initialized_)
- {
- tb::TBWidgetsAnimationManager::Shutdown();
- delete rootWidget_;
- // leak
- //delete TBUIRenderer::renderer_;
- tb_core_shutdown();
- }
- }
- void UI::Shutdown()
- {
- SetInputDisabled(true);
- }
- void UI::Initialize(const String& languageFile)
- {
- Graphics* graphics = GetSubsystem<Graphics>();
- assert(graphics);
- assert(graphics->IsInitialized());
- graphics_ = graphics;
- vertexBuffer_ = new VertexBuffer(context_);
- readerContext_ = context_;
- TBFile::SetReaderFunction(TBFileReader);
- TBWidgetsAnimationManager::Init();
- renderer_ = new UIRenderer(graphics_->GetContext());
- tb_core_init(renderer_, languageFile.CString());
- //register_tbbf_font_renderer();
- //register_stb_font_renderer();
- register_freetype_font_renderer();
- rootWidget_ = new TBWidget();
- int width = graphics_->GetWidth();
- int height = graphics_->GetHeight();
- rootWidget_->SetSize(width, height);
- rootWidget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
- SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UI, HandleMouseButtonDown));
- SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UI, HandleMouseButtonUp));
- SubscribeToEvent(E_MOUSEMOVE, HANDLER(UI, HandleMouseMove));
- SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UI, HandleMouseWheel));
- SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
- SubscribeToEvent(E_KEYUP, HANDLER(UI, HandleKeyUp));
- SubscribeToEvent(E_TEXTINPUT, HANDLER(UI, HandleTextInput));
- SubscribeToEvent(E_UPDATE, HANDLER(UI, HandleUpdate));
- SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
- initialized_ = true;
- //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
- }
- void UI::LoadSkin(const String& skin, const String& overrideSkin)
- {
- // Load the default skin, and override skin
- tb::g_tb_skin->Load(skin.CString(), overrideSkin.CString());
- }
- void UI::SetDefaultFont(const String& name, int size)
- {
- tb::TBFontDescription fd;
- fd.SetID(tb::TBIDC(name.CString()));
- fd.SetSize(tb::g_tb_skin->GetDimensionConverter()->DpToPx(12));
- tb::g_font_manager->SetDefaultFontDescription(fd);
- // Create the font now.
- tb::TBFontFace *font = tb::g_font_manager->CreateFontFace(tb::g_font_manager->GetDefaultFontDescription());
- // Render some glyphs in one go now since we know we are going to use them. It would work fine
- // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap.
- if (font)
- font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ");
- }
- void UI::AddFont(const String& fontFile, const String& name)
- {
- tb::g_font_manager->AddFontInfo(fontFile.CString(), name.CString());
- }
- void UI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
- {
- if (batches.Empty())
- return;
- Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
- Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
- Vector2 offset(-1.0f, 1.0f);
- Matrix4 projection(Matrix4::IDENTITY);
- projection.m00_ = scale.x_;
- projection.m03_ = offset.x_;
- projection.m11_ = scale.y_;
- projection.m13_ = offset.y_;
- projection.m22_ = 1.0f;
- projection.m23_ = 0.0f;
- projection.m33_ = 1.0f;
- graphics_->ClearParameterSources();
- graphics_->SetColorWrite(true);
- graphics_->SetCullMode(CULL_NONE);
- graphics_->SetDepthTest(CMP_ALWAYS);
- graphics_->SetDepthWrite(false);
- graphics_->SetDrawAntialiased(false);
- graphics_->SetFillMode(FILL_SOLID);
- graphics_->SetStencilTest(false);
- graphics_->ResetRenderTargets();
- graphics_->SetVertexBuffer(buffer);
- ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
- ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
- ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
- ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
- ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
- ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
- unsigned alphaFormat = Graphics::GetAlphaFormat();
- for (unsigned i = batchStart; i < batchEnd; ++i)
- {
- const UIBatch& batch = batches[i];
- if (batch.vertexStart_ == batch.vertexEnd_)
- continue;
- ShaderVariation* ps;
- ShaderVariation* vs;
- if (!batch.texture_)
- {
- ps = noTexturePS;
- vs = noTextureVS;
- }
- else
- {
- // If texture contains only an alpha channel, use alpha shader (for fonts)
- vs = diffTextureVS;
- if (batch.texture_->GetFormat() == alphaFormat)
- ps = alphaTexturePS;
- else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
- ps = diffMaskTexturePS;
- else
- ps = diffTexturePS;
- }
- graphics_->SetShaders(vs, ps);
- if (graphics_->NeedParameterUpdate(SP_OBJECTTRANSFORM, this))
- graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
- if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
- graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
- if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
- graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
- graphics_->SetBlendMode(batch.blendMode_);
- graphics_->SetScissorTest(true, batch.scissor_);
- graphics_->SetTexture(0, batch.texture_);
- graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
- UI_VERTEX_SIZE);
- }
- }
- void UI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
- {
- if (vertexData.Empty())
- return;
- // Update quad geometry into the vertex buffer
- // Resize the vertex buffer first if too small or much too large
- unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
- if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
- dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
- dest->SetData(&vertexData[0]);
- }
- void UI::Render()
- {
- SetVertexData(vertexBuffer_, vertexData_);
- Render(vertexBuffer_, batches_, 0, batches_.Size());
- }
- void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
- {
- // Get rendering batches from the non-modal UI elements
- batches_.Clear();
- vertexData_.Clear();
- tb::TBRect rect = rootWidget_->GetRect();
- IntRect currentScissor = IntRect(0, 0, rect.w, rect.h);
- GetBatches(batches_, vertexData_, currentScissor);
- }
- void UI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
- {
- //if (!initialized_)
- // return;
- TBAnimationManager::Update();
- rootWidget_->InvokeProcessStates();
- rootWidget_->InvokeProcess();
- tb::g_renderer->BeginPaint(rootWidget_->GetRect().w, rootWidget_->GetRect().h);
- renderer_->currentScissor_ = currentScissor;
- renderer_->batches_ = &batches;
- renderer_->vertexData_ = &vertexData;
- rootWidget_->InvokePaint(tb::TBWidget::PaintProps());
- tb::g_renderer->EndPaint();
- }
- void UI::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
- {
- UIBatch b(BLEND_ALPHA , renderer_->currentScissor_, texture, &vertexData_);
- unsigned begin = b.vertexData_->Size();
- b.vertexData_->Resize(begin + vertexData.Size());
- float* dest = &(b.vertexData_->At(begin));
- b.vertexEnd_ = b.vertexData_->Size();
- for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
- {
- *dest = vertexData[i];
- }
- UIBatch::AddOrMerge(b, batches_);
- }
- void UI::TBFileReader(const char* filename, void** data, unsigned* length)
- {
- *data = 0;
- *length = 0;
- ResourceCache* cache = readerContext_->GetSubsystem<ResourceCache>();
- SharedPtr<File> file = cache->GetFile(filename);
- if (!file || !file->IsOpen())
- return;
- unsigned size = file->GetSize();
- if (!size)
- return;
- void* _data = malloc(size);
- if (!_data)
- return;
- if (file->Read(_data, size) != size)
- {
- free(_data);
- return;
- }
- *length = size;
- *data = _data;
- }
- bool UI::LoadResourceFile(TBWidget* widget, const String& filename)
- {
- tb::TBNode node;
- if (!node.ReadFile(filename.CString()))
- return false;
- tb::g_widgets_reader->LoadNodeTree(widget, &node);
- return true;
- }
- void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
- {
- using namespace ScreenMode;
- rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
- //SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
- }
- void UI::HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- TBMessageHandler::ProcessMessages();
- }
- UIWidget* UI::WrapWidget(tb::TBWidget* widget)
- {
- if (widgetWrap_.Contains(widget))
- return widgetWrap_[widget];
- if (widget->IsOfType<TBButton>())
- {
- UIButton* button = new UIButton(context_, false);
- button->SetWidget(widget);
- widgetWrap_[widget] = button;
- return button;
- }
- if (widget->IsOfType<TBTextField>())
- {
- UITextField* textfield = new UITextField(context_, false);
- textfield->SetWidget(widget);
- widgetWrap_[widget] = textfield;
- return textfield;
- }
- return 0;
- }
- }
|