From 579f2a9e5c7248f597bb809ef514ac3f5041dbc6 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 5 Dec 2009 21:06:19 +0000 Subject: [PATCH] Add new building picker dialog --- include/IBuildingPicker.hpp | 36 +++++++++++++ layouts/editor.xml | 18 ++++--- layouts/game.xml | 2 +- src/BuildingPicker.cpp | 104 ++++++++++++++++++++++++++++++++++++ src/Editor.cpp | 24 ++------- src/gui/Canvas3D.cpp | 8 ++- 6 files changed, 165 insertions(+), 27 deletions(-) create mode 100644 include/IBuildingPicker.hpp create mode 100644 src/BuildingPicker.cpp diff --git a/include/IBuildingPicker.hpp b/include/IBuildingPicker.hpp new file mode 100644 index 0000000..76ed10d --- /dev/null +++ b/include/IBuildingPicker.hpp @@ -0,0 +1,36 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#ifndef INC_IBUILDINGPICKER_HPP +#define INC_IBUILDINGPICKER_HPP + +#include "Platform.hpp" +#include "gui/ILayout.hpp" +#include "IBuilding.hpp" + +// A dialog box for picking buildings in the editor +struct IBuildingPicker { + virtual ~IBuildingPicker() {} + + virtual IBuildingPtr get() const = 0; +}; + +typedef shared_ptr IBuildingPickerPtr; + +IBuildingPickerPtr makeBuildingPicker(gui::ILayoutPtr layout); + +#endif diff --git a/layouts/editor.xml b/layouts/editor.xml index 19aeffe..84818a0 100644 --- a/layouts/editor.xml +++ b/layouts/editor.xml @@ -1,5 +1,10 @@ + + - - - - + - diff --git a/layouts/game.xml b/layouts/game.xml index 2db6c70..2e27677 100644 --- a/layouts/game.xml +++ b/layouts/game.xml @@ -1,7 +1,7 @@ diff --git a/src/BuildingPicker.cpp b/src/BuildingPicker.cpp new file mode 100644 index 0000000..43db812 --- /dev/null +++ b/src/BuildingPicker.cpp @@ -0,0 +1,104 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include "IBuildingPicker.hpp" +#include "ILogger.hpp" +#include "ILight.hpp" +#include "OpenGLHelper.hpp" +#include "gui/Label.hpp" + +class BuildingPicker : public IBuildingPicker { +public: + BuildingPicker(gui::ILayoutPtr l) + : layout(l) + { + using namespace placeholders; + + enumResources("buildings", buildingList); + + if (buildingList.empty()) + warn() << "No buildings found"; + else { + buildingIt = buildingList.begin(); + changeActive(loadBuilding((*buildingIt)->name())); + } + + layout->get("/building_wnd/preview").connect(gui::Widget::SIG_RENDER, + bind(&::BuildingPicker::renderBuildingPreview, this, _1)); + layout->get("/building_wnd/next").connect(gui::Widget::SIG_CLICK, + bind(&::BuildingPicker::next, this)); + layout->get("/building_wnd/prev").connect(gui::Widget::SIG_CLICK, + bind(&::BuildingPicker::prev, this)); + } + + void next() + { + if (++buildingIt == buildingList.end()) + buildingIt = buildingList.begin(); + + changeActive(loadBuilding((*buildingIt)->name())); + } + + void prev() + { + if (buildingIt == buildingList.begin()) + buildingIt = buildingList.end(); + buildingIt--; + + changeActive(loadBuilding((*buildingIt)->name())); + } + + IBuildingPtr get() const + { + return activeBuilding; + } + + void renderBuildingPreview(gui::Widget& canvas) + { + static ILightPtr sun = makeSunLight(); + + sun->apply(); + + glRotatef(45.0f, 1.0f, 0.0f, 0.0f); + glRotatef(45.0f, 0.0f, 1.0f, 0.0f); + glTranslatef(1.5f, -2.6f, -1.5f); + glColor3f(1.0f, 1.0f, 1.0f); + glRotatef(35.0f, 0.0f, 1.0f, 0.0f); + + activeBuilding->model()->render(); + } + +private: + void changeActive(IBuildingPtr b); + + ResourceList buildingList; + ResourceList::const_iterator buildingIt; + IBuildingPtr activeBuilding; + gui::ILayoutPtr layout; +}; + +void BuildingPicker::changeActive(IBuildingPtr b) +{ + activeBuilding = b; + + layout->cast("/building_wnd/bld_name").text(b->name()); +} + +IBuildingPickerPtr makeBuildingPicker(gui::ILayoutPtr layout) +{ + return IBuildingPickerPtr(new BuildingPicker(layout)); +} diff --git a/src/Editor.cpp b/src/Editor.cpp index 63595e3..9979efd 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -25,6 +25,7 @@ #include "IBuilding.hpp" #include "NewMapDialog.hpp" #include "gui/ILayout.hpp" +#include "IBuildingPicker.hpp" #include #include @@ -37,7 +38,7 @@ #include #include #include - + // Concrete editor class class Editor : public IScreen { public: @@ -76,7 +77,6 @@ private: const Point& aSecondPoint) const; void dragBoxBounds(int& xMin, int& xMax, int &yMin, int& yMax) const; void deleteObjects(); - void renderBuildingPreview(gui::Widget& canvas); IMapPtr map; @@ -92,6 +92,7 @@ private: // GUI elements gui::ILayoutPtr layout; + IBuildingPickerPtr buildingPicker; }; // The FLTK toolbox @@ -236,7 +237,7 @@ void addEditorGUI() theBldRotateButton = new Fl_Button(120, 240, 60, 25, "Rotate"); theBldRotateButton->callback(onBldRotateClick); - theBuildingPicker = new BuildingPicker; + //theBuildingPicker = new BuildingPicker; theSaveButton = new Fl_Button(0, 273, panelW, 25, "Save"); theSaveButton->callback(onSaveClick); @@ -290,8 +291,7 @@ void Editor::buildGUI() layout->get("/tool_wnd/tools/building").connect(gui::Widget::SIG_CLICK, bind(&Editor::setTool, this, BUILDING_TOOL)); - layout->get("/building_wnd/preview").connect(gui::Widget::SIG_RENDER, - bind(&Editor::renderBuildingPreview, this, _1)); + buildingPicker = makeBuildingPicker(layout); } void Editor::setMap(IMapPtr aMap) @@ -311,20 +311,6 @@ void Editor::dragBoxBounds(int& xMin, int& xMax, int &yMin, int& yMax) const yMax = max(dragBegin.y, dragEnd.y); } -void Editor::renderBuildingPreview(gui::Widget& canvas) -{ - static ILightPtr sun = makeSunLight(); - static IBuildingPtr bld = loadBuilding("platform_end"); - - sun->apply(); - - glTranslatef(-4.5f, 2.5f, -13.7f); - glColor3f(1.0f, 1.0f, 1.0f); - glRotatef(45.0f, 1.0f, 1.0f, 0.0f); - glRotatef(/*myRotation*/ 35.0f, 0.0f, 1.0f, 0.0f); - bld->model()->render(); -} - // Render the next frame void Editor::display(IGraphicsPtr aContext) const { diff --git a/src/gui/Canvas3D.cpp b/src/gui/Canvas3D.cpp index e574c0c..cdb66a8 100644 --- a/src/gui/Canvas3D.cpp +++ b/src/gui/Canvas3D.cpp @@ -17,6 +17,7 @@ #include "gui/Canvas3D.hpp" #include "ILogger.hpp" +#include "GameScreens.hpp" #include #include @@ -38,10 +39,15 @@ void Canvas3D::render(RenderContext& rc) const glPushAttrib(GL_ALL_ATTRIB_BITS); glPushMatrix(); + int xo = x(), yo = y(); + rc.offset(xo, yo); + glViewport(xo, getGameWindow()->height() - yo - height(), + width(), height()); + glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - + const GLfloat wf = static_cast(width()); const GLfloat hf = static_cast(height()); gluPerspective(45.0f, wf/hf, 0.1f, 50.0f); -- 2.39.2