#include #include #include #include #include #include #include #include #include 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 "UIState.h" namespace Atomic { WeakPtr UIState::readerContext_; UIState::UIState(Context* context) : Object(context), rootWidget_(0), inputDisabled_(false), keyboardDisabled_(false) { Graphics* graphics = GetSubsystem(); assert(graphics); assert(graphics->IsInitialized()); graphics_ = graphics; vertexBuffer_ = new VertexBuffer(context_); Initialize(); } UIState::~UIState() { tb::TBWidgetsAnimationManager::Shutdown(); delete rootWidget_; // leak //delete TBUIRenderer::renderer_; tb_core_shutdown(); } // refactor void UIState::Initialize() { readerContext_ = context_; TBFile::SetReaderFunction(TBFileReader); TBWidgetsAnimationManager::Init(); renderer_ = new UIRenderer(graphics_->GetContext()); tb_core_init(renderer_, "AtomicEditor/resources/language/lng_en.tb.txt"); // Load the default skin, and override skin that contains the graphics specific to the demo. tb::g_tb_skin->Load("AtomicEditor/resources/default_skin/skin.tb.txt", "AtomicEditor/editor/skin/skin.tb.txt"); //register_tbbf_font_renderer(); //register_stb_font_renderer(); register_freetype_font_renderer(); tb::g_font_manager->AddFontInfo("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco"); tb::g_font_manager->AddFontInfo("AtomicEditor/resources/vera.ttf", "Vera"); tb::TBFontDescription fd; fd.SetID(tb::TBIDC("Vera")); 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{|}~•·åäöÅÄÖ"); rootWidget_ = new TBWidget(); //rootWidget_->SetSkinBg(tb::TBIDC("background")); int width = graphics_->GetWidth(); int height = graphics_->GetHeight(); rootWidget_->SetSize(width, height); //SetSize(width, height); rootWidget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE); SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UIState, HandleMouseButtonDown)); SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UIState, HandleMouseButtonUp)); SubscribeToEvent(E_MOUSEMOVE, HANDLER(UIState, HandleMouseMove)); SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UIState, HandleMouseWheel)); SubscribeToEvent(E_KEYDOWN, HANDLER(UIState, HandleKeyDown)); SubscribeToEvent(E_KEYUP, HANDLER(UIState, HandleKeyUp)); SubscribeToEvent(E_TEXTINPUT, HANDLER(UIState, HandleTextInput)); SubscribeToEvent(E_UPDATE, HANDLER(UIState, HandleUpdate)); SubscribeToEvent(E_RENDERUPDATE, HANDLER(UIState, HandleRenderUpdate)); //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1; } void UIState::Render(VertexBuffer* buffer, const PODVector& 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 UIState::SetVertexData(VertexBuffer* dest, const PODVector& 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 UIState::Render() { SetVertexData(vertexBuffer_, vertexData_); Render(vertexBuffer_, batches_, 0, batches_.Size()); } void UIState::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 UIState::GetBatches(PODVector& batches, PODVector& 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 UIState::SubmitBatchVertexData(Texture* texture, const PODVector& 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 UIState::TBFileReader(const char* filename, void** data, unsigned* length) { *data = 0; *length = 0; ResourceCache* cache = readerContext_->GetSubsystem(); SharedPtr 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 UIState::LoadResourceFile(TBWidget* widget, const String& filename) { tb::TBNode node; // TODO: use Urho resources if (!node.ReadFile(filename.CString())) return false; tb::g_widgets_reader->LoadNodeTree(widget, &node); return true; } void UIState::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 UIState::HandleUpdate(StringHash eventType, VariantMap& eventData) { TBMessageHandler::ProcessMessages(); } }