<commit_before>/******************************************************************************
 * This file is part of the Gluon Development Platform
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (c) 2010 <NAME> <<EMAIL>>
 * Copyright (c) 2010 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "plasmaapplet.h"
#include "overlaytoolbox.h"

#include <engine/game.h>
#include <engine/gameproject.h>
#include <graphics/glwidget.h>
#include <engine/scene.h>
#include <graphics/item.h>
#include <graphics/camera.h>
#include <models/gamesmodel.h>

#include <QFileDialog>
#include <QTimer>
#include <QGraphicsSceneResizeEvent>

#include <KAction>

using namespace GluonPlayer;
using namespace GluonGraphics;

PlasmaApplet::PlasmaApplet(QObject* parent, const QVariantList& args): GLApplet(parent, args),
                            m_viewportWidth(0), m_viewportHeight(0), m_project(0), m_camera(0)
{
}

PlasmaApplet::~PlasmaApplet()
{
    if (m_gameFileName.isEmpty()) {
        return;
    }
    GluonEngine::Game::instance()->stopGame();
    GluonEngine::Game::instance()->cleanupAll();
}

void PlasmaApplet::init()
{
    m_gamesModel = new GamesModel(this);
    m_overlay = new OverlayToolBox("Select a game", this);
    m_overlay->setGeometry(geometry());
    
    for (int i=0; i<m_gamesModel->rowCount(); i++) {
        QString gameName = m_gamesModel->data(m_gamesModel->index(i, 0)).toString();
        KAction *option = new KAction(KIcon("gluon_creator"), gameName, m_overlay);
        connect(option, SIGNAL(triggered()), this, SLOT(setProject()));
        m_overlay->addTool(option);
    }
}

void PlasmaApplet::setProject()
{
    KAction *option = qobject_cast<KAction*>(sender());
    m_gameFileName = option->text();
    qDebug() << m_gameFileName;
    //openProject();    //This crashes right now, trying to fix
}

void PlasmaApplet::openProject()
{
    if (m_gameFileName.isEmpty()) {
        return;
    }

    delete m_overlay;
    setBusy(true);

    initGL();
    GluonCore::GluonObjectFactory::instance()->loadPlugins();

    m_project = new GluonEngine::GameProject();
    m_project->loadFromFile(QUrl(m_gameFileName));
    GluonEngine::Game::instance()->setGameProject(m_project);
    GluonEngine::Game::instance()->setCurrentScene(m_project->entryPoint());

    connect(GluonEngine::Game::instance(), SIGNAL(painted(int)), this, SLOT(doPaint()));
    QTimer::singleShot(1000, this, SLOT(startGame()));
}

void PlasmaApplet::doPaint()
{
    makeCurrent();
    update();
}

void PlasmaApplet::startGame()
{
    setBusy(false);
    GluonEngine::Game::instance()->runGame();
}

void PlasmaApplet::initGL()
{
    m_camera = Engine::instance()->activeCamera();
    connect(Engine::instance(), SIGNAL(activeCameraChanged(GluonGraphics::Camera*)),
            SLOT(setCamera(GluonGraphics::Camera*)));

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glEnable(GL_SCISSOR_TEST);
    glEnable(GL_ALPHA_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glAlphaFunc(GL_GREATER, 0.01);

}

void PlasmaApplet::resizeEvent(QGraphicsSceneResizeEvent* event)
{
    m_viewportWidth = event->newSize().width();
    m_viewportHeight = event->newSize().height();
    m_overlay->setGeometry(geometry());

    if (m_camera) {
        m_camera->setViewport(0, 0, m_viewportWidth, m_viewportHeight);
        m_camera->applyViewport();
        m_camera->applyOrtho();
    }
}


void PlasmaApplet::setCamera(Camera* camera)
{
    m_camera = camera;
}

void PlasmaApplet::render()
{
    Engine::instance()->sortItems();
    QList<Item*> items = Engine::instance()->items();
    foreach(Item *it, items) {
        it->paintGL();
    }
}

void PlasmaApplet::paintGLInterface(QPainter* painter, const QStyleOptionGraphicsItem* option)
{
    Q_UNUSED(painter);
    Q_UNUSED(option);

    glScissor(0, 0, 400, 400);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    if (m_camera) {
        m_camera->applyView();
    }

    glPushAttrib(GL_ALL_ATTRIB_BITS);
    render();
    glPopAttrib();
}

#include "plasmaapplet.moc"<commit_msg>Selection with overlay now loads the game. yay!<commit_after>/******************************************************************************
 * This file is part of the Gluon Development Platform
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 * Copyright (c) 2010 <NAME> <<EMAIL>>
 * Copyright (c) 2010 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "plasmaapplet.h"
#include "overlaytoolbox.h"

#include <engine/game.h>
#include <engine/gameproject.h>
#include <graphics/glwidget.h>
#include <engine/scene.h>
#include <graphics/item.h>
#include <graphics/camera.h>
#include <models/gamesmodel.h>

#include <QFileDialog>
#include <QTimer>
#include <QGraphicsSceneResizeEvent>

#include <KAction>

using namespace GluonPlayer;
using namespace GluonGraphics;

PlasmaApplet::PlasmaApplet(QObject* parent, const QVariantList& args): GLApplet(parent, args),
                            m_viewportWidth(0), m_viewportHeight(0), m_project(0), m_camera(0)
{
}

PlasmaApplet::~PlasmaApplet()
{
    if (m_gameFileName.isEmpty()) {
        return;
    }
    GluonEngine::Game::instance()->stopGame();
    GluonEngine::Game::instance()->cleanupAll();
}

void PlasmaApplet::init()
{
    m_gamesModel = new GamesModel(this);
    m_overlay = new OverlayToolBox("Select a game", this);
    m_overlay->setGeometry(geometry());
    
    for (int i=0; i<m_gamesModel->rowCount(); i++) {
        QString gameName = m_gamesModel->data(m_gamesModel->index(i, 0)).toString();
        KAction *option = new KAction(KIcon("gluon_creator"), gameName, m_overlay);
        connect(option, SIGNAL(triggered()), this, SLOT(setProject()));
        m_overlay->addTool(option);
    }
}

void PlasmaApplet::setProject()
{
    KAction *option = qobject_cast<KAction*>(sender());
    m_gameFileName = option->text();
    m_overlay->hide();
    openProject();
}

void PlasmaApplet::openProject()
{
    if (m_gameFileName.isEmpty()) {
        return;
    }

    setBusy(true);

    initGL();
    GluonCore::GluonObjectFactory::instance()->loadPlugins();

    m_project = new GluonEngine::GameProject();
    m_project->loadFromFile(QUrl(m_gameFileName));
    GluonEngine::Game::instance()->setGameProject(m_project);
    GluonEngine::Game::instance()->setCurrentScene(m_project->entryPoint());

    connect(GluonEngine::Game::instance(), SIGNAL(painted(int)), this, SLOT(doPaint()));
    QTimer::singleShot(1000, this, SLOT(startGame()));
}

void PlasmaApplet::doPaint()
{
    makeCurrent();
    update();
}

void PlasmaApplet::startGame()
{
    setBusy(false);
    GluonEngine::Game::instance()->runGame();
}

void PlasmaApplet::initGL()
{
    m_camera = Engine::instance()->activeCamera();
    connect(Engine::instance(), SIGNAL(activeCameraChanged(GluonGraphics::Camera*)),
            SLOT(setCamera(GluonGraphics::Camera*)));

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glEnable(GL_SCISSOR_TEST);
    glEnable(GL_ALPHA_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glAlphaFunc(GL_GREATER, 0.01);

}

void PlasmaApplet::resizeEvent(QGraphicsSceneResizeEvent* event)
{
    m_viewportWidth = event->newSize().width();
    m_viewportHeight = event->newSize().height();
    m_overlay->setGeometry(geometry());

    if (m_camera) {
        m_camera->setViewport(0, 0, m_viewportWidth, m_viewportHeight);
        m_camera->applyViewport();
        m_camera->applyOrtho();
    }
}


void PlasmaApplet::setCamera(Camera* camera)
{
    m_camera = camera;
}

void PlasmaApplet::render()
{
    Engine::instance()->sortItems();
    QList<Item*> items = Engine::instance()->items();
    foreach(Item *it, items) {
        it->paintGL();
    }
}

void PlasmaApplet::paintGLInterface(QPainter* painter, const QStyleOptionGraphicsItem* option)
{
    Q_UNUSED(painter);
    Q_UNUSED(option);

    glScissor(0, 0, 400, 400);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    if (m_camera) {
        m_camera->applyView();
    }

    glPushAttrib(GL_ALL_ATTRIB_BITS);
    render();
    glPopAttrib();
}

#include "plasmaapplet.moc"
{'fraction_non_alphanumeric': 0.15460852329038652, 'fraction_numerical': 0.015064420218037661, 'mean_word_length': 4.396256684491979, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '224478', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3233, 'n_words': 724}