From 052641637bd20ffde6ddb8919888e2af987bafff Mon Sep 17 00:00:00 2001 From: neoraider Date: Tue, 25 Dec 2007 03:27:03 +0000 Subject: zoomedit: Added rectangular room tool. --- Drawer.cpp | 8 +++-- EditManager.cpp | 15 ++++++--- EditManager.h | 2 +- EventHandler.h | 3 +- Makefile.am | 2 +- Makefile.in | 20 ++++++++++-- SidebarManager.cpp | 3 +- SidebarManager.h | 9 ++++-- SidebarToolbox.h | 4 +++ ToolAddPolygon.cpp | 4 +-- ToolAddRect.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ToolAddRect.h | 65 +++++++++++++++++++++++++++++++++++++++ Window.h | 4 +++ 13 files changed, 210 insertions(+), 18 deletions(-) create mode 100644 ToolAddRect.cpp create mode 100644 ToolAddRect.h diff --git a/Drawer.cpp b/Drawer.cpp index abe8e20..db3dbdf 100644 --- a/Drawer.cpp +++ b/Drawer.cpp @@ -52,6 +52,10 @@ gboolean Drawer::eventHandler(GtkWidget *widget, GdkEvent *event, Drawer *drawer drawer->window->getActiveTool()->getEventHandler()->buttonPress(event->button.button); return TRUE; + case GDK_BUTTON_RELEASE: + drawer->window->getActiveTool()->getEventHandler()->buttonRelease(event->button.button); + return TRUE; + case GDK_SCROLL: switch(event->scroll.direction) { case GDK_SCROLL_UP: @@ -194,12 +198,12 @@ Drawer::Drawer(Window *window, GdkGLConfig *glconfig) : renderer(&window->getEdi g_signal_connect(G_OBJECT(drawingArea), "configure-event", G_CALLBACK(eventHandler), this); g_signal_connect(G_OBJECT(drawingArea), "expose-event", G_CALLBACK(eventHandler), this); g_signal_connect(G_OBJECT(drawingArea), "button-press-event", G_CALLBACK(eventHandler), this); - //g_signal_connect(G_OBJECT(drawingArea), "button-release-event", G_CALLBACK(eventHandler), this); + g_signal_connect(G_OBJECT(drawingArea), "button-release-event", G_CALLBACK(eventHandler), this); g_signal_connect(G_OBJECT(drawingArea), "enter-notify-event", G_CALLBACK(eventHandler), this); g_signal_connect(G_OBJECT(drawingArea), "leave-notify-event", G_CALLBACK(eventHandler), this); g_signal_connect(G_OBJECT(drawingArea), "motion-notify-event", G_CALLBACK(eventHandler), this); g_signal_connect(G_OBJECT(drawingArea), "scroll-event", G_CALLBACK(eventHandler), this); - gtk_widget_add_events(drawingArea, GDK_SCROLL_MASK | GDK_POINTER_MOTION_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK); + gtk_widget_add_events(drawingArea, GDK_SCROLL_MASK | GDK_POINTER_MOTION_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_BUTTON_RELEASE_MASK); gtk_table_attach(GTK_TABLE(drawer), drawingArea, 0, 1, 0, 1, (GtkAttachOptions)(GTK_FILL|GTK_EXPAND|GTK_SHRINK), (GtkAttachOptions)(GTK_FILL|GTK_EXPAND|GTK_SHRINK), 0, 0); hAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 10, 100, 100)); diff --git a/EditManager.cpp b/EditManager.cpp index 2d743f5..9c44a27 100644 --- a/EditManager.cpp +++ b/EditManager.cpp @@ -46,7 +46,9 @@ bool EditManager::addRoom(const Room &newRoom) { room->setName(idManager.generate("room")); window->getLevel().push_back(SharedPtr(room)); - selectedObject = &*window->getLevel().back(); + selectedObject = room; + + window->resetTool(); return true; } @@ -83,20 +85,23 @@ void EditManager::setHoveredVertex(Vertex *v) { window->update(); } -bool EditManager::vertexOk(const Room &newRoom, const Vertex &v) const { +bool EditManager::vertexOk(const Vertex &v, const Room *newRoom) const { Line l; for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { if((*room)->isOfType("Room") && ((Room*)&**room)->contains(v)) return false; } - if(newRoom.empty()) + if(!newRoom) + return true; + + if(newRoom->empty()) return true; - l.setVertex1(newRoom.back()); + l.setVertex1(newRoom->back()); l.setVertex2(v); - return lineOk(newRoom, l); + return lineOk(*newRoom, l); } bool EditManager::polygonOk(const Polygon& polygon) const { diff --git a/EditManager.h b/EditManager.h index 4413aeb..44e1e11 100644 --- a/EditManager.h +++ b/EditManager.h @@ -45,7 +45,7 @@ class EditManager { Vertex* getHoveredVertex(); void setHoveredVertex(Vertex *v); - bool vertexOk(const Room &newRoom, const Vertex &v) const; + bool vertexOk(const Vertex &v, const Room *newRoom = NULL) const; bool polygonOk(const Polygon &polygon) const; }; diff --git a/EventHandler.h b/EventHandler.h index 7708341..6bb6c0f 100644 --- a/EventHandler.h +++ b/EventHandler.h @@ -5,7 +5,8 @@ class EventHandler { public: virtual ~EventHandler() {} - virtual bool buttonPress(unsigned int button) = 0; + virtual bool buttonPress(unsigned int button) {return false;} + virtual bool buttonRelease(unsigned int button) {return false;} }; #endif /*EVENTHANDLER_H_*/ diff --git a/Makefile.am b/Makefile.am index adfd9d4..560c273 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,6 +4,6 @@ zoomedit_SOURCES = zoomedit.cpp UIManager.cpp Renderer.cpp Vertex.cpp \ IdManager.cpp WindowManager.cpp SidebarManager.cpp \ Window.cpp SidebarView.cpp SidebarAdd.cpp Drawer.cpp \ EditManager.cpp FileManager.cpp SidebarToolbox.cpp \ - ToolSelector.cpp ToolAddPolygon.cpp Vertex3d.cpp + ToolSelector.cpp ToolAddPolygon.cpp ToolAddRect.cpp Vertex3d.cpp zoomedit_CPPFLAGS = @GTK_CFLAGS@ @GTKGLEXT_CFLAGS@ @libxml2_CFLAGS@ zoomedit_LDADD = @GTK_LIBS@ @GTKGLEXT_LIBS@ @libxml2_LIBS@ \ No newline at end of file diff --git a/Makefile.in b/Makefile.in index 619fa70..17e0fa0 100644 --- a/Makefile.in +++ b/Makefile.in @@ -60,7 +60,8 @@ am_zoomedit_OBJECTS = zoomedit-zoomedit.$(OBJEXT) \ zoomedit-FileManager.$(OBJEXT) \ zoomedit-SidebarToolbox.$(OBJEXT) \ zoomedit-ToolSelector.$(OBJEXT) \ - zoomedit-ToolAddPolygon.$(OBJEXT) zoomedit-Vertex3d.$(OBJEXT) + zoomedit-ToolAddPolygon.$(OBJEXT) \ + zoomedit-ToolAddRect.$(OBJEXT) zoomedit-Vertex3d.$(OBJEXT) zoomedit_OBJECTS = $(am_zoomedit_OBJECTS) zoomedit_DEPENDENCIES = DEFAULT_INCLUDES = -I.@am__isrc@ @@ -187,7 +188,7 @@ zoomedit_SOURCES = zoomedit.cpp UIManager.cpp Renderer.cpp Vertex.cpp \ IdManager.cpp WindowManager.cpp SidebarManager.cpp \ Window.cpp SidebarView.cpp SidebarAdd.cpp Drawer.cpp \ EditManager.cpp FileManager.cpp SidebarToolbox.cpp \ - ToolSelector.cpp ToolAddPolygon.cpp Vertex3d.cpp + ToolSelector.cpp ToolAddPolygon.cpp ToolAddRect.cpp Vertex3d.cpp zoomedit_CPPFLAGS = @GTK_CFLAGS@ @GTKGLEXT_CFLAGS@ @libxml2_CFLAGS@ zoomedit_LDADD = @GTK_LIBS@ @GTKGLEXT_LIBS@ @libxml2_LIBS@ @@ -292,6 +293,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-SidebarToolbox.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-SidebarView.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-ToolAddPolygon.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-ToolAddRect.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-ToolSelector.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Triangle.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-UIManager.Po@am__quote@ @@ -595,6 +597,20 @@ zoomedit-ToolAddPolygon.obj: ToolAddPolygon.cpp @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-ToolAddPolygon.obj `if test -f 'ToolAddPolygon.cpp'; then $(CYGPATH_W) 'ToolAddPolygon.cpp'; else $(CYGPATH_W) '$(srcdir)/ToolAddPolygon.cpp'; fi` +zoomedit-ToolAddRect.o: ToolAddRect.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-ToolAddRect.o -MD -MP -MF $(DEPDIR)/zoomedit-ToolAddRect.Tpo -c -o zoomedit-ToolAddRect.o `test -f 'ToolAddRect.cpp' || echo '$(srcdir)/'`ToolAddRect.cpp +@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-ToolAddRect.Tpo $(DEPDIR)/zoomedit-ToolAddRect.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ToolAddRect.cpp' object='zoomedit-ToolAddRect.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-ToolAddRect.o `test -f 'ToolAddRect.cpp' || echo '$(srcdir)/'`ToolAddRect.cpp + +zoomedit-ToolAddRect.obj: ToolAddRect.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-ToolAddRect.obj -MD -MP -MF $(DEPDIR)/zoomedit-ToolAddRect.Tpo -c -o zoomedit-ToolAddRect.obj `if test -f 'ToolAddRect.cpp'; then $(CYGPATH_W) 'ToolAddRect.cpp'; else $(CYGPATH_W) '$(srcdir)/ToolAddRect.cpp'; fi` +@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-ToolAddRect.Tpo $(DEPDIR)/zoomedit-ToolAddRect.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ToolAddRect.cpp' object='zoomedit-ToolAddRect.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-ToolAddRect.obj `if test -f 'ToolAddRect.cpp'; then $(CYGPATH_W) 'ToolAddRect.cpp'; else $(CYGPATH_W) '$(srcdir)/ToolAddRect.cpp'; fi` + zoomedit-Vertex3d.o: Vertex3d.cpp @am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-Vertex3d.o -MD -MP -MF $(DEPDIR)/zoomedit-Vertex3d.Tpo -c -o zoomedit-Vertex3d.o `test -f 'Vertex3d.cpp' || echo '$(srcdir)/'`Vertex3d.cpp @am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-Vertex3d.Tpo $(DEPDIR)/zoomedit-Vertex3d.Po diff --git a/SidebarManager.cpp b/SidebarManager.cpp index 393d748..42731a8 100644 --- a/SidebarManager.cpp +++ b/SidebarManager.cpp @@ -3,7 +3,7 @@ SidebarManager::SidebarManager(Window *window) - : sidebarToolbox(window), toolAddPolygon(&window->getEditManager()) + : sidebarToolbox(window), toolAddRect(&window->getEditManager()), toolAddPolygon(&window->getEditManager()) { this->window = window; activeSidebar = NULL; @@ -27,6 +27,7 @@ SidebarManager::SidebarManager(Window *window) viewport = gtk_viewport_new(NULL, NULL); gtk_container_add(GTK_CONTAINER(scrolledWindow), viewport); + sidebarToolbox.addTool(&toolAddRect); sidebarToolbox.addTool(&toolAddPolygon); gtk_widget_show(sidebar); diff --git a/SidebarManager.h b/SidebarManager.h index 4401c9b..75fc06b 100644 --- a/SidebarManager.h +++ b/SidebarManager.h @@ -4,10 +4,8 @@ #include #include "Sidebar.h" #include "SidebarToolbox.h" -#include "SidebarView.h" -#include "SidebarAdd.h" #include "EditManager.h" -#include "ToolSelector.h" +#include "ToolAddRect.h" #include "ToolAddPolygon.h" @@ -24,6 +22,7 @@ class SidebarManager { Window *window; + ToolAddRect toolAddRect; ToolAddPolygon toolAddPolygon; // prevent shallow copy @@ -38,6 +37,10 @@ class SidebarManager { return sidebar; } + void resetTool() { + sidebarToolbox.resetTool(); + } + Tool* getActiveTool() { return &sidebarToolbox.getActiveTool(); } diff --git a/SidebarToolbox.h b/SidebarToolbox.h index 961df24..1be8b6a 100644 --- a/SidebarToolbox.h +++ b/SidebarToolbox.h @@ -47,6 +47,10 @@ class SidebarToolbox : Sidebar { return widget; } + void resetTool() { + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(buttons[&toolSelector]), TRUE); + } + Tool& getActiveTool() { return *activeTool; } diff --git a/ToolAddPolygon.cpp b/ToolAddPolygon.cpp index 184d107..0ef7225 100644 --- a/ToolAddPolygon.cpp +++ b/ToolAddPolygon.cpp @@ -36,7 +36,7 @@ void ToolAddPolygon::render(const Level &level, const Rectangle &rect, float sca drawPolygon(newRoom, false); if(!newRoom.empty() && editManager->getHoveredVertex()) { - if(!editManager->vertexOk(newRoom, *editManager->getHoveredVertex())) + if(!editManager->vertexOk(*editManager->getHoveredVertex(), &newRoom)) glColor4f(1.0f, 0.3f, 0.3f, 0.7f); glBegin(GL_LINES); @@ -55,7 +55,7 @@ bool ToolAddPolygon::buttonPress(unsigned int button) { if(!editManager->getHoveredVertex()) return false; - if(!editManager->vertexOk(newRoom, *editManager->getHoveredVertex())) + if(!editManager->vertexOk(*editManager->getHoveredVertex(), &newRoom)) return false; newRoom.push_back(*editManager->getHoveredVertex()); diff --git a/ToolAddRect.cpp b/ToolAddRect.cpp new file mode 100644 index 0000000..a266756 --- /dev/null +++ b/ToolAddRect.cpp @@ -0,0 +1,89 @@ +#include "ToolAddRect.h" +#include "GL/gl.h" + + +Room ToolAddRect::createRoom() { + Vertex *v2 = editManager->getHoveredVertex(); + + Room room; + room.push_back(v1); + room.push_back(Vertex(v1.getX(), v2->getY())); + room.push_back(*v2); + room.push_back(Vertex(v2->getX(), v1.getY())); + + return room; +} + +ToolAddRect::ToolAddRect(EditManager *editManager) : Renderer(editManager), sidebar(editManager) { + this->editManager = editManager; + + image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_BUTTON); + g_object_ref_sink(G_OBJECT(image)); +} + +ToolAddRect::~ToolAddRect() { + g_object_unref(G_OBJECT(image)); +} + +void ToolAddRect::activate() { + editManager->setSelectedObject(NULL); +} + +void ToolAddRect::render(const Level &level, const Rectangle &rect, float scale) { + if(!pressed || !editManager->getHoveredVertex()) + return; + + Room room = createRoom(); + + if(editManager->polygonOk(room)) + glColor4f(0.0f, 0.7f, 1.0f, 0.2f); + else + glColor4f(1.0f, 0.3f, 0.3f, 0.2f); + + fillPolygon(room); + + glLineWidth(2.0f); + glColor4f(0.0f, 0.7f, 1.0f, 0.7f); + drawPolygon(room); +} + +bool ToolAddRect::buttonPress(unsigned int button) { + if(button != 1) + return false; + + if(!editManager->getHoveredVertex()) + return false; + + if(!editManager->vertexOk(*editManager->getHoveredVertex())) + return false; + + pressed = true; + v1 = *editManager->getHoveredVertex(); + + editManager->redraw(); + sidebar.update(); + + return true; +} + +bool ToolAddRect::buttonRelease(unsigned int button) { + if(button != 1 || !pressed) + return false; + + pressed = false; + + if(!editManager->getHoveredVertex()) + return false; + + Room room = createRoom(); + + if(!editManager->polygonOk(room)) + return false; + + editManager->addRoom(room); + + editManager->redraw(); + sidebar.update(); + + return true; +} diff --git a/ToolAddRect.h b/ToolAddRect.h new file mode 100644 index 0000000..ee915fa --- /dev/null +++ b/ToolAddRect.h @@ -0,0 +1,65 @@ +#ifndef TOOLADDRECT_H_ +#define TOOLADDRECT_H_ + +#include "Tool.h" +#include "SidebarAdd.h" + + +class ToolAddRect : public Tool, public EventHandler, public Renderer { + private: + GtkWidget *image; + + EditManager *editManager; + + SidebarAdd sidebar; + + bool pressed; + Vertex v1; + + // prevent shallow copy + ToolAddRect(const ToolAddRect &t); + const ToolAddRect& operator=(const ToolAddRect &t); + + Room createRoom(); + + public: + ToolAddRect(EditManager *editManager); + virtual ~ToolAddRect(); + + virtual void activate(); + + virtual const char *getType() const { + return "ToolAddRect"; + } + + virtual const char *getName() const { + return "Add rectangular room"; + } + + virtual bool isSensitive() { + return TRUE; + } + + virtual GtkWidget *getImage() { + return image; + } + + virtual EventHandler* getEventHandler() { + return this; + } + + virtual Renderer *getRenderer() { + return this; + } + + virtual void render(const Level &level, const Rectangle &rect, float scale); + + virtual bool buttonPress(unsigned int button); + virtual bool buttonRelease(unsigned int button); + + virtual Sidebar* getSidebar() { + return &sidebar; + } +}; + +#endif /*TOOLADDRECT_H_*/ diff --git a/Window.h b/Window.h index c8fa469..c6d3b3e 100644 --- a/Window.h +++ b/Window.h @@ -49,6 +49,10 @@ class Window { drawer.update(); } + void resetTool() { + sidebar.resetTool(); + } + Tool* getActiveTool() { return sidebar.getActiveTool(); } -- cgit v1.2.3