void InspectorExportButton::setWorker(ExportWorker* w)
{
    clearWorker();
    worker = w;
    if (!isVisible())
        show();
}
void RtspStreamThread::start(const QUrl &url)
{
    QMutexLocker locker(&m_workerMutex);

    qDebug() << Q_FUNC_INFO << LoggableUrl(url);

    if (!m_worker)
    {
        Q_ASSERT(!m_thread);
        m_thread = new QThread();

        RtspStreamWorker *worker = new RtspStreamWorker(m_frameQueue);
        m_worker = worker;

        worker->moveToThread(m_thread.data());

        m_worker.data()->setUrl(url);

        connect(m_thread.data(), SIGNAL(started()), m_worker.data(), SLOT(run()));
        connect(m_thread.data(), SIGNAL(finished()), m_thread.data(), SLOT(deleteLater()));
        connect(m_worker.data(), SIGNAL(fatalError(QString)), this, SIGNAL(fatalError(QString)));        
        connect(m_worker.data(), SIGNAL(destroyed()), this, SLOT(clearWorker()), Qt::DirectConnection);
        connect(m_worker.data(), SIGNAL(destroyed()), m_thread.data(), SLOT(quit()));
        connect(m_worker.data(), SIGNAL(audioFormat(enum AVSampleFormat, int, int)), this, SIGNAL(audioFormat(enum AVSampleFormat,int,int)), Qt::DirectConnection);
        connect(m_worker.data(), SIGNAL(audioSamplesAvailable(void *, int, int)), this, SIGNAL(audioSamplesAvailable(void*,int,int)), Qt::DirectConnection);

        connect(m_worker.data(), SIGNAL(bytesDownloaded(uint)), bcApp->globalRate, SLOT(addSampleValue(uint)));

        m_thread.data()->start();
    }
void Mainscene::update(float deltaTime){

    if(enemiesToSpawn > 0 && !waveEnd){
        spawnTimer += deltaTime;
    }else{

        if(waveEnd){
            spawnTimer = 0;
            waveTimer -= deltaTime;
            waveTimerText->color.a = 255;
            std::ostringstream wts;
            wts << (int)waveTimer;
            waveTimerText->setText(wts.str());
            if(waveTimer <= 0){
                wave ++;
                std::ostringstream ws;
                ws << "Wave: ";
                ws << wave;
                waveText->setText(ws.str());
                waveTimerText->color.a = 0;
                waveTimer = 8;
                if(wave > 1){
                    enemiesToSpawn = 10+(2*wave);
                    enemiesThisWave = enemiesToSpawn;
                }
                waveEnd = false;
                spawnEnemy();
                enemiesToSpawn --;
            }
        }
    }
    if(spawnTimer >= 4.0f){
        spawnEnemy();
        spawnTimer = 0;
        enemiesToSpawn --;

    }
    if(!usedInit){
        usedInit = true;
        init();
    }
    if(enemies.size() == 0 ){
        waveEnd = true;
    }
    handleMenuItems();
    upgradeBtn->position =  Vector2(740,toolbar->position.y);
    upgradetxt->position = upgradeBtn->position+Vector2(-upgradetxt->getWidth()/2,0);
    upgradePriceText->position = Vector2(200,toolbar->position.y+30);

    rangeText->position = Vector2(200,toolbar->position.y-30);
    damageText->position = Vector2(200,toolbar->position.y);

    cursor->position = input->getMouseToScreen()+(Vector2(3,cursor->height()*cursor->scale.y-15));
    if(input->getMouseButton(1)){
        cursor->setPng("assets/pointerClick.png");
    }else{
        cursor->setPng("assets/pointer.png");
    }
    if(input->getMouseToScreen().y > 640 && !toolbar->mouseOver()){
        camera->position.y += (((input->getMouseToScreen().y - 640)/100)*400)*deltaTime;
    }

    if(input->getMouseToScreen().y < 80){
        camera->position.y -= (((80-input->getMouseToScreen().y)/100)*400)*deltaTime;
    }

    if(notenoughAlpha < 0){
        notenoughAlpha = 0;
    }


    notenoughAlpha -= 150*deltaTime;
    if(notEnoughText->color.a > 0){
        notEnoughText->position.y -= 10*deltaTime;
    }
    notEnoughText->color.a = notenoughAlpha;
    std::ostringstream ss;
    ss << "Available Workers: ";
    ss << readyWorkers.size();
    availableWorkersText->setText(ss.str());

    std::ostringstream cc;

    cc << "Coins: ";
    cc << coins;
    coinsText->setText(cc.str());
    priceText->position = pricetag->position+Vector2(-80, 10);

    counter += deltaTime;
    if(counter >= 1.0f/60){
        fixedUpdate();
        counter = 0;
    }

    for(unsigned int i = 0; i < bullets.size(); i++){
        if(bullets[i]->wantsTwinkle){
            Particle* t;
            t = new Particle(bullets[i]->rotation);
            twinkles.push_back(t);
            addEntity(t);
            t->layer = 3;
            t->position = bullets[i]->position;
            bullets[i]->wantsTwinkle = false;

        }
    }
    for(unsigned int i = 0; i < twinkles.size(); i++){
        if(twinkles[i]->color.a <= 0){
            removeTwinkle(twinkles[i]);
        }
    }
    pricetag->position = Vector2(-1000, -1000);
    for(unsigned int i = 0; i < menuItems.size(); i++){
        if(menuItems[i]->mouseOver() && lockedItem == NULL){
            std::ostringstream ct;
            ct << menuItems[i]->price;
            ct << " coins";
            priceText->setText(ct.str());
            pricetag->position = Vector2(menuItems[i]->position.x-180, menuItems[i]->position.y);
        }
    }

    if(input->getKeyDown(SDLK_c)){
        coins += 10;
    }

    for(unsigned int i = 0; i < bullets.size(); i++){
        bool done = false;

        if(!bullets[i]->hasTarget && bullets[i]->disToTarget < 10){
            removeBullet(bullets[i]);
            done = true;
        }

        if(!done){
            if(bullets[i]->disToTarget < bullets[i]->target->collisionRadius && bullets[i]->hasTarget){
                bullets[i]->target->health -= bullets[i]->damage;
                bullets[i]->target->slowness += bullets[i]->slowingDamage;
                coins += 2;

                bullets[i]->destroyMe = true;
                bullets[i]->explodeSound->play();
                if(bullets[i]->target->health <= 0){
                    bullets[i]->target->dead = true;
                }
                if(bullets[i]->target->dead){
                    for(unsigned int b = 0; b < bullets.size(); b++){
                        if(bullets[i]->target->getEntityId() == bullets[b]->target->getEntityId()){
                            if(bullets[i]->getEntityId() != bullets[b]->getEntityId()){
                                bullets[b]->lastKnownPos = bullets[i]->lastKnownPos;
                                bullets[b]->hasTarget = false;
                            }
                        }
                    }
                }
            }
        }
    }
    int enemycounter = 0;
    for(unsigned int i = 0; i < enemies.size(); i ++){
        if(enemies[i]->dead){
            enemycounter ++;
        }
        if(enemies[i]->atTarget){
            if(pathpoints.size()-1 > (unsigned)enemies[i]->currPathPoint){
                enemies[i]->atTarget = false;
                enemies[i]->currPathPoint ++;
                enemies[i]->curtarget = pathpoints[enemies[i]->currPathPoint];
            }
        }
        if(enemies[i]->atTarget ){
            if(enemies[i]->dead){
                dreamHealth += 3;
            }else{
                dreamHealth -= 10;
            }

            removeEnemy(enemies[i]);
        }
    }


    if(enemycounter == enemiesThisWave  && enemies.size() > 0){
        for(unsigned int i = 0; i < enemies.size(); i ++){
            if(enemies[i]->dead){
                enemies[i]->speed = 400;
            }
        }
    }

    for(unsigned int i = 0; i < bullets.size(); i++){
        if(bullets[i]->destroyMe){
            removeBullet(bullets[i]);
        }
    }

    if(towers.size() > 0 && enemies.size() > 0){
        Enemy* target = NULL;
        for(unsigned int i = 0; i < enemies.size(); i++){
            if(!enemies[i]->dead){
                target = enemies[i];
                i = enemies.size();
            }
        }


        for(unsigned int t = 0; t < towers.size(); t++){
            for(unsigned int i = 0;  i  < enemies.size(); i ++){
                if(!enemies[i]->dead ){
                    bool canpass = false;
                    if(towers[t]->slowingDamage > 0 && towers[t]->damage == 0 && enemies[i]->slowness < 60){
                        canpass = true;

                    }
                    if(towers[t]->slowingDamage == 0){
                        canpass = true;
                    }

                    if(canpass){
                        Vector2 curdisvec = Vector2(target->position, towers[t]->position);
                        Vector2 disvec = Vector2(enemies[i]->position, towers[t]->position);
                        if(disvec.magnitude() < curdisvec.magnitude()){
                            target = enemies[i];
                        }

                    }

                }
            }
            if((towers[t]->target == NULL || (towers[t]->target->dead ))&& towers[t]->ready){
                towers[t]->target = target;
            }

            if(towers[t]->wantsToShoot && towers[t]->target != NULL && !towers[t]->target->dead){
                towers[t]->shootcounter = 0;
                towers[t]->wantsToShoot = false;
                Bullet* b;

                b = towers[t]->shoot();

                addEntity(b);
                b->layer = 4;
                b->hasTarget = true;
                bullets.push_back(b);
                towers[t]->shootSound->play();
            }
        }
    }

    for(unsigned int i = 0; i < busyWorkers.size(); i++){
        if(busyWorkers[i]->wantsCloud){
            SimpleEntity* c;
            c = new SimpleEntity();
            c->position = busyWorkers[i]->cloudpos;
            c->setPng("assets/Bouwwolkje.png");
            c->scale = Vector2(0.5f, 0.5f);
            addEntity(c);
            c->layer = 4;
            clouds.push_back(c);
            busyWorkers[i]->wantsCloud = false;
        }
        if(!busyWorkers[i]->working){
            clearWorker(busyWorkers[i]);
        }
    }
    for(unsigned int i = 0; i < clouds.size(); i++){
        clouds[i]->rotation += 15*deltaTime;
        clouds[i]->color.a -= 50*deltaTime;

        if(clouds[i]->color.a < 0){
            removeCloud(clouds[i]);
        }
    }
    if(camera->position.y < 0){
        camera->position.y = 0;
    }

    if(camera->position.y > 1296-720){
        camera->position.y = 1296-720;
    }

    if(toolbarMustPop){
        if(toolbar->position.y > 720-60){
            toolbar->position.y -= 300*deltaTime;
        }
    }else{
        if(toolbar->position.y < 720+64){
            toolbar->position.y += 300*deltaTime;
        }
    }

    if(input->getMouseButtonDown(1)){
        if(!toolbar->mouseOver()){
            for(unsigned int i = 0; i < towers.size(); i++){
                removeShape(towers[i]->shootingRangeShape);

            }
        }
        if(upgradeBtn->mouseOver() && selectedTower->level < 5 && coins-selectedTower->upgradePrice >= 0){
            coins -= selectedTower->upgradePrice;
            selectedTower->upgrade();
            setToolBarContent(selectedTower->shootingRange, selectedTower->damage, selectedTower->upgradePrice);
            if(selectedTower->level < 5){
                upgradeBtn->color = WHITE;
            }else{
                upgradeBtn->color = GREY;
                upgradeBtn->color.a = 100;
            }
        }else{
            if(upgradeBtn->mouseOver() && selectedTower->level < 5 && coins-selectedTower->upgradePrice <= 0){
                notenoughAlpha = 255;
                notenoughAlpha = 255.0f;
                notEnoughText->position.y = 720/2;
            }
        }

    }

    if(dreamHealth == 200){
        for(unsigned int i = 0; i < enemies.size(); i++){
            enemies[i]->speed = 0;
            enemies[i]->health = 0;
        }

        wintimer += deltaTime;

        if(wintimer >= 1){
            blackfader->color.a += deltaTime*100;
            if(blackfader->color.a >= 255){
                reset();
                this->nextScene = true;
                blackfader->color.a = 0;
                wintimer = 0;
                bgmusic->stop();
                usedInit = false;
            }
        }
    }else if(dreamHealth == 0){
        reset();
    }
    upgradetxt->color = upgradeBtn->color;
    for(unsigned int i = 0; i < towers.size(); i++){
        if(input->getMouseButtonDown(1)){
            if(Vector2(input->getMouseToWorld(camera), towers[i]->position).magnitude() < 30 && towers[i]->ready){
                towers[i]->shootingRangeShape->circle(towers[i]->shootingRange);
                addShape(towers[i]->shootingRangeShape);
                toolbarMustPop = true;
                setToolBarContent(towers[i]->shootingRange, towers[i]->damage, towers[i]->upgradePrice);
                selectedTower = towers[i];
                if(selectedTower->level < 5){
                    upgradeBtn->color = WHITE;
                }else{
                    upgradeBtn->color = GREY;
                    upgradeBtn->color.a = 100;
                }
                i = towers.size();
            }else{
                if(!toolbar->mouseOver()){
                    toolbarMustPop = false;
                }

            }
        }
    }

    if(lockedItem != NULL ){
        if(canPlaceMenuItem(lockedItem)){
            lockedItem->color.g = 255;
            lockedItem->color.b = 255;
        }else{
            lockedItem->color.g = 50;
            lockedItem->color.b = 50;
        }
    }
    if(dreamHealth > 200){
        dreamHealth = 200;
    }

    if(dreamHealth < 0){
        dreamHealth = 0;
    }

    if(dreamHealth > 100.0f){
        statusBarHappiness->uvOffset = Vector2(((1.0f/100)*(-(dreamHealth-100))+1), 0);
        statusBarAnger->uvOffset = Vector2(-1, 0);
    }else if(dreamHealth < 100.0f){
        statusBarHappiness->uvOffset = Vector2(1, 0);
        statusBarAnger->uvOffset = Vector2(-(1.0f/100.0f)*dreamHealth, 0);
    }else{
        statusBarHappiness->uvOffset = Vector2(1, 0);
        statusBarAnger->uvOffset = Vector2(-1, 0);
    }

}