Ejemplo n.º 1
0
MapEditor::MapEditor(int mapwidth, int mapheight, const QString &fileName, QWidget *parent):
    QWidget(parent),
    name("Simple map editor"),
    newBtn(new QPushButton("New")),
    loadBtn(new QPushButton("Load...")),
    saveBtn(new QPushButton("Save...")),
    randomBtn(new QPushButton("Random")),
    editArea(new EditArea(this)),
    snapRadiusSlider(new QSlider(Qt::Horizontal))
{
    connect(newBtn, SIGNAL(clicked()), this, SLOT(createNewMap()));
    connect(loadBtn, SIGNAL(clicked()), this, SLOT(loadFromFile()));
    connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveToFile()));
    connect(randomBtn, SIGNAL(clicked()), this, SLOT(generateRandom()));
    
    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addWidget(newBtn);
    btnLayout->addWidget(loadBtn);
    btnLayout->addWidget(saveBtn);
    btnLayout->addWidget(randomBtn);

    editArea->move(10, 10);
    editArea->setFixedSize(mapwidth, mapheight);
    editArea->setMouseTracking(true);
    editArea->setSnapRadius(25);
    if (!fileName.isEmpty())
    {
        editArea->setMap(getMapFromFile(fileName));
        setWindowTitle(name + " - " + fileName);
        fileSaved = true;
    }
    else
    {
        createNewMap();
    }
    connect(editArea, SIGNAL(mapChanged()), this, SLOT(mapChanged()));

    snapRadiusSlider->setMaximum(50);
    snapRadiusSlider->setSliderPosition(25);
    connect(snapRadiusSlider, SIGNAL(sliderMoved(int)), editArea, SLOT(setSnapRadius(int)));

    QVBoxLayout *snapLayout = new QVBoxLayout();
    QLabel *lbl = new QLabel("Snap precision:");
    snapLayout->addWidget(lbl);
    snapLayout->addWidget(snapRadiusSlider);
    snapLayout->addStretch(1);

    QGridLayout *mainLayout = new QGridLayout();
    mainLayout->addWidget(editArea, 0, 0);
    mainLayout->addLayout(btnLayout, 1, 0);
    mainLayout->addLayout(snapLayout, 0, 1);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);

    setLayout(mainLayout);
}
Ejemplo n.º 2
0
void EditorToolBar::update(float deltaTime)
{
	ASSERT(world!=0,                 "world was null!  Call setWorld first!");
	ASSERT(toolBarTools!=0,          "toolBarTools was null");
	ASSERT(toolBarMisc!=0,           "toolBarMisc was null");
	ASSERT(toolBarZone!=0,           "toolBarZone was null");
	ASSERT(mousePosLabel!=0,         "mousePosLabel was null");
	ASSERT(objectPalette!=0,         "objectPalette was null");
	ASSERT(tileTypeSelector!=0,      "tileTypeSelector was null");
	ASSERT(texturePalette_Wall!=0,   "texturePalette_Wall was null");
	ASSERT(texturePalette_Floor!=0,  "texturePalette_Floor was null");
	ASSERT(tileHeightSelector!=0,    "tileHeightSelector was null");




	if(g_Application.getState() == GAME_STATE_EDITOR)
	{
		toolBarTools->m_bVisible = true;
		toolBarMisc->m_bVisible = true;
		toolBarZone->m_bVisible = true;
		mousePosLabel->m_bVisible = true;



		// Hide the object palette (overridden by create tool)
		objectPalette->m_bVisible = false;

		// Hide the tile properties panel (overridden in map editor mode)
		toolBarTilePropreties->m_bVisible = false;


		// Get the point under the mouse cursor
		mousePos = getGroundPickPos(0.0f);

		// update the mouse cursor to show this position
		mousePosLabel->setLabel(string("Mouse: (") + ftoa(mousePos.x) +
								string(", ") + ftoa(mousePos.y) +
								string(", ") + ftoa(mousePos.z) +
								string(")"));

        if(g_Keys.isKeyDown(KEY_TEST))
        {
            world->getMap().removeAllMaterials();
        }


		// Update the tool bar widgets
		switch(toolBarTools->getTool())
		{
		case ToolBarForEditorTools::EDITOR_CREATE_TOOL:
			// Show the object palette
			objectPalette->m_bVisible = true;

			// Decide the selected object
			{
				string o = chooseNextObject();
				if(!o.empty())
				{
					nextObject = o;
				}
			}
			break;

		case ToolBarForEditorTools::EDITOR_TILE_PENCIL_TOOL:
			toolBarTilePropreties->m_bVisible = true;

			// Update tile properties from the selectors
			tileEditor_wallTextureFile = chooseTileWallTexture();
			tileEditor_floorTextureFile = chooseTileFloorTexture();
			tileEditor_type = getTileType();

			break;

		case ToolBarForEditorTools::EDITOR_TILE_BLOCK_TOOL:
			toolBarTilePropreties->m_bVisible = true;

			// Update tile properties from the selectors
			tileEditor_wallTextureFile = chooseTileWallTexture();
			tileEditor_floorTextureFile = chooseTileFloorTexture();
			tileEditor_type = getTileType();

			// Has a drag stopped?
			if(!g_Input.MouseLeft)
			{
				// drag exitted
				if(drag)
				{
					// get a reference to the map
					Map &map = world->getMap();

					// Get the position on the ground plane that the mouse was over
					vec3 groundPos = getGroundPickPos(0.0f);

					// Fill a block of tiles
					if(map.onATile(groundPos.x, groundPos.z) && map.onATile(mouseDownPos.x, mouseDownPos.z))
					{
						map.fillBlock(groundPos.x, groundPos.z,
									  mouseDownPos.x, mouseDownPos.z,
							          tileEditor_type,
									  tileEditor_properties,
									  tileEditor_floorTextureFile,
									  tileEditor_wallTextureFile,
							          tileEditor_height);

						// Rebuild the map display list
						map.reaquire();

						g_SoundSystem.play("data/sound/activate.wav");
					}
				}

				drag = false;
			}

			break;

		case ToolBarForEditorTools::EDITOR_DESTROY_TOOL:
			// (This tool doesn't require updating every tick)
			break;

		case ToolBarForEditorTools::EDITOR_ROTATE_TOOL:
			// (This tool doesn't require updating every tick)
			break;

		case ToolBarForEditorTools::EDITOR_ROTATE_X_TOOL:
			// (This tool doesn't require updating every tick)
			break;

		case ToolBarForEditorTools::EDITOR_ROTATE_Z_TOOL:
			// (This tool doesn't require updating every tick)
			break;

		case ToolBarForEditorTools::EDITOR_MOVE_TOOL:
			// (This tool doesn't require updating every tick)
			break;

		case ToolBarForEditorTools::EDITOR_SELECT_TOOL:
			// (This tool doesn't require updating every tick)
			break;
		};




		// The tile selectors cannot be opened if the tile properties dialog is not
		if(toolBarTilePropreties->m_bVisible)
		{
			switch(textureSelectorState)
			{
			case TEXTURE_SELECTOR_HIDE:
				texturePalette_Wall->m_bVisible = false;
				texturePalette_Floor->m_bVisible = false;
				break;

			case TEXTURE_SELECTOR_FLOOR:
				texturePalette_Wall->m_bVisible = false;
				texturePalette_Floor->m_bVisible = true;
				break;

			case TEXTURE_SELECTOR_WALL:
				texturePalette_Wall->m_bVisible = true;
				texturePalette_Floor->m_bVisible = false;
				break;
			};
		}
		else
		{
			tileTypeSelector->m_bVisible = false;
			tileHeightSelector->m_bVisible = false;
			textureSelectorState = TEXTURE_SELECTOR_HIDE;
			texturePalette_Wall->m_bVisible = false;
			texturePalette_Floor->m_bVisible = false;
		}




		// In editor mode, only update the selected object
		if(selected) selected->updateForEditor(deltaTime);




		// Call the event handler for left-click
		if(g_Input.MouseLeft)
		{
			onLeftMouseDown();
		}
		else
		{
			leftClickDebounce=false;
		}

		// Save / Load the game
		if(shouldSave)
		{
			world->saveToFile();
		}
		else if(shouldLoad)
		{
			world->loadFromFile();
		}
		else if(shouldNew)
		{
			createNewMap();
		}
	}
	else
	{// None of these should be visible in game mode

		toolBarTools->m_bVisible = false;
		toolBarMisc->m_bVisible = false;
		mousePosLabel->m_bVisible = false;

		objectPalette->m_bVisible = false;
		toolBarZone->m_bVisible = false;

		// Hide tile properties dialogs
		toolBarTilePropreties->m_bVisible = false;
		tileTypeSelector->m_bVisible = false;
		tileHeightSelector->m_bVisible = false;
		texturePalette_Wall->m_bVisible = false;
		texturePalette_Floor->m_bVisible = false;
		tileHeightSelector->m_bVisible = false;
	}

	// reset each tick
	shouldSave = false;
	shouldLoad = false;
	shouldNew = false;
}