Ejemplo n.º 1
0
bool TweenEx::init(Composite& rootNode) {
	TextLabelPtr label = MakeTextLabel("Hello There!", CoordPercent(0.5f, 0.9f), CoordPX(100, 30), FontType::Normal, UnitPT(16.0f),RGBA(255, 255, 255, 255), HorizontalAlignment::Center,VerticalAlignment::Bottom);
	label->setOrigin(Origin::MiddleCenter);
	std::shared_ptr<ImageGlyph> img = createImageGlyph(getFullPath("images/sfmarket.png"));
	GlyphRegionPtr imgr = MakeGlyphRegion(img, CoordPercent(0.5f, 0.45f),CoordPX(100,300), AspectRule::FixedHeight, Color(32, 64, 128, 255),Color(128, 128, 128, 128), Color(200, 200, 200, 255), UnitPX(1.0f));
	GlyphRegionPtr iconr = MakeGlyphRegion(createAwesomeGlyph(0xf1b3),CoordPX(20, 20), CoordPX(50, 50), Color(32, 64, 128, 255),Color(255, 255, 255, 255));
	imgr->setOrigin(Origin::MiddleCenter);
	addTween(imgr->foregroundColor, Color(128, 128, 128, 255),Color(128, 128, 128, 0), 3.0, SineOut());
	addTween(imgr->dimensions, CoordPX(50, 50), CoordPX(300, 300), 1.0, SineOut());
	addTween(iconr->backgroundColor, Color(255, 64, 32, 255),Color(32, 64, 255, 255), 3.0, SineIn());
	addTween(iconr->foregroundColor, Color(0, 0, 0, 255),Color(255, 255, 255, 255), 3.0, SineIn());
	addTween(iconr->position, CoordPX(100, 10), CoordPerPX(0.5f,0.0f,-25.0f, 10.0f), 3.0, ExponentialOut());
	addTween(label->position, CoordPercent(0.5f, 0.7f), CoordPercent(0.5f, 0.9f), 1.0f, ExponentialOut())->addCompleteEvent([=](Tweenable* tween) {
		addTween(label->fontSize, UnitPT(16.0f), UnitPT(36.0f), 1.0f, ExponentialIn())->addCompleteEvent(
			[=](Tweenable* object) {
			label->setLabel( "Did you like that tween?");
		});
	});
	
	rootNode.add(label);
	rootNode.add(iconr);
	rootNode.add(imgr);
	return true;
}
void Application::run(int swapInterval) {
    const double POLL_INTERVAL_SEC = 0.5f;

    context->makeCurrent();
    if (!init(rootRegion)) {
        throw std::runtime_error("Error occurred in application init()");
    }
    rootRegion.add(getContext()->getGlassPane());
    if (showDebugIcon) {
        GlyphRegionPtr debug = MakeGlyphRegion(
                                   createAwesomeGlyph(0xf188, FontStyle::Outline, 20),
                                   CoordPercent(1.0f, 1.0f), CoordPX(20, 20), RGBA(0, 0, 0, 0),
                                   RGBA(64, 64, 64, 128), RGBA(192, 192, 192, 128), UnitPX(0));

        debug->setOrigin(Origin::BottomRight);
        debug->onMouseDown =
        [this,debug](AlloyContext* context,const InputEvent& e) {
            if(e.button==GLFW_MOUSE_BUTTON_LEFT) {
                context->toggleDebug();
                debug->foregroundColor=context->isDebugEnabled()?MakeColor(255,64,64,255):MakeColor(64,64,64,128);
                context->setMouseFocusObject(nullptr);
                return true;
            }
            return false;
        };
        rootRegion.add(debug);
    }

    //First pack triggers computation of aspect ratios  for components.
    rootRegion.pack(context.get());
    context->getGlassPane()->setVisible(false);
    context->requestPack();
    glfwSwapInterval(swapInterval);
    glfwSetTime(0);
    uint64_t frameCounter = 0;
    std::chrono::steady_clock::time_point endTime;
    std::chrono::steady_clock::time_point lastFpsTime =
        std::chrono::steady_clock::now();
    if (!forceClose) {
        glfwShowWindow(context->window);
    } else {
        context->executeDeferredTasks();
        context->dirtyUI = true;
        context->dirtyLayout = true;
        draw();
        context->dirtyUI = true;
        context->dirtyLayout = true;
        context->update(rootRegion);
    }
    do {
        //Events could have modified layout! Pack before draw to make sure things are correctly positioned.
        if (context->dirtyLayout) {
            context->dirtyLayout = false;
            context->dirtyCursorLocator = true;
            rootRegion.pack();
        }
        draw();
        context->update(rootRegion);
        double elapsed =
            std::chrono::duration<double>(endTime - lastFpsTime).count();
        frameCounter++;
        if (elapsed > POLL_INTERVAL_SEC) {
            frameRate = (float) (frameCounter / elapsed);
            lastFpsTime = endTime;
            frameCounter = 0;
        }
        glfwSwapBuffers(context->window);
        glfwPollEvents();
        for (std::exception_ptr e : caughtExceptions) {
            std::rethrow_exception(e);
        }
        if (glfwWindowShouldClose(context->offscreenWindow)) {
            context->setOffScreenVisible(false);
        }
    } while (!glfwWindowShouldClose(context->window) && !forceClose);
}