Skip to content

Commit

Permalink
ADDED overlap prevention that can optionally be disabled via the grid…
Browse files Browse the repository at this point in the history
… control
  • Loading branch information
ksterker committed May 11, 2011
1 parent 2003107 commit 2e0ee05
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 34 deletions.
100 changes: 82 additions & 18 deletions src/mapedit/grid-properties.glade

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/mapedit/gui_entity_list.cc
Expand Up @@ -174,7 +174,7 @@ static void entity_list_get_value (GtkTreeModel *self, GtkTreeIter *iter, int co
}
case COLOR_COLUMN:
{
const gchar *color = obj->is_on_map () ? (obj->getRefCount() ? "#FFFFFF" : "#80C8FE") : "#FEB380";
const gchar *color = obj->isOnMap () ? (obj->getRefCount() ? "#FFFFFF" : "#80C8FE") : "#FEB380";
g_value_set_string (value, color);
break;
}
Expand All @@ -198,7 +198,7 @@ static void selected_event (GtkTreeSelection *selection, gpointer user_data)
MapEntity *obj = (MapEntity*) entity_list_get_object (ENTITY_LIST (model), &iter);

// check if object needs to be added to map
if (!obj->is_on_map ())
if (!obj->isOnMap ())
{
// allow user to add entity to map
GuiEntityDialog dlg (obj, GuiEntityDialog::ADD_ENTITY_TO_MAP);
Expand Down
1 change: 1 addition & 0 deletions src/mapedit/gui_grid.cc
Expand Up @@ -34,6 +34,7 @@ GuiGrid::GuiGrid (gfx::surface *overlay)
Visible = false;
SnapToGrid = true;
AutoAdjust = true;
PreventOverlap = true;

CurObject = NULL;
Monitor = NULL;
Expand Down
11 changes: 6 additions & 5 deletions src/mapedit/gui_grid.h
Expand Up @@ -127,13 +127,12 @@ class GuiGrid
void set_visible (const bool & visible);

/**
* Toggle snapping on or off. Decides whether objects will be
* automatically aligned to the current grid.
* @param snap true to turn snapping on, false to turn it off.
* Returns whether it is forbidden to place overlapping objects.
* @return true if overlapping forbidden, false otherwise.
*/
void set_snap_to_grid (const bool & snap)
bool overlap_prevented () const
{
SnapToGrid = snap;
return PreventOverlap;
}

/// can edit the grid
Expand Down Expand Up @@ -170,6 +169,8 @@ class GuiGrid
bool SnapToGrid;
/// whether grid automatically adjusts to selected object
bool AutoAdjust;
/// whether overlapping objects are forbidden
bool PreventOverlap;
/// whether the grid has changed
bool Changed;
};
Expand Down
16 changes: 16 additions & 0 deletions src/mapedit/gui_grid_dialog.cc
Expand Up @@ -54,6 +54,12 @@ static void on_snap_to_grid (GtkToggleButton *btn, gpointer user_data)
dlg->setSnapToGrid(gtk_toggle_button_get_active (btn));
}

static void on_prevent_overlap (GtkToggleButton *btn, gpointer user_data)
{
GuiGridDialog* dlg = (GuiGridDialog*) user_data;
dlg->setPreventOverlap(gtk_toggle_button_get_active (btn));
}

static void on_adjust_grid (GtkButton * button, gpointer user_data)
{
GuiGrid *grid = (GuiGrid*) user_data;
Expand Down Expand Up @@ -135,6 +141,10 @@ GuiGridDialog::GuiGridDialog (GuiGrid *grid, GtkWidget* ctrl)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), Grid->SnapToGrid);
g_signal_connect (widget, "toggled", G_CALLBACK (on_snap_to_grid), this);

widget = gtk_builder_get_object (Ui, "cb_preventoverlap");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), Grid->PreventOverlap);
g_signal_connect (widget, "toggled", G_CALLBACK (on_prevent_overlap), this);

widget = gtk_builder_get_object (Ui, "btn_adjust");
gtk_widget_set_sensitive (GTK_WIDGET(widget), !Grid->AutoAdjust);
g_signal_connect (widget, "clicked", G_CALLBACK (on_adjust_grid), Grid);
Expand Down Expand Up @@ -311,3 +321,9 @@ void GuiGridDialog::setSnapToGrid (const bool & snap_to_grid)
{
Grid->SnapToGrid = snap_to_grid;
}

// toggle overlap prevention
void GuiGridDialog::setPreventOverlap (const bool & prevent_overlap)
{
Grid->PreventOverlap = prevent_overlap;
}
7 changes: 7 additions & 0 deletions src/mapedit/gui_grid_dialog.h
Expand Up @@ -89,6 +89,13 @@ class GuiGridDialog : public GuiModalDialog, public GridMonitor
*/
void setSnapToGrid (const bool & snap_to_grid);

/**
* Toggle whether overlapping objects can be placed
* on the map or not.
* @param prevent_overlap true to forbid overlapping.
*/
void setPreventOverlap (const bool & prevent_overlap);

/**
* Called when the dialog is closed by pressing ESC.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/mapedit/gui_mapedit.cc
Expand Up @@ -132,7 +132,7 @@ GuiMapedit::GuiMapedit ()
submenu = gtk_menu_new ();

// New
menuitem =gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
menuitem = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
gtk_menu_shell_append (GTK_MENU_SHELL (submenu), menuitem);
// gtk_object_set_data (GTK_OBJECT (menuitem), "help-id", GINT_TO_POINTER (1));
// g_signal_connect (G_OBJECT (menuitem), "enter-notify-event", G_CALLBACK (on_display_help), message);
Expand All @@ -141,7 +141,7 @@ GuiMapedit::GuiMapedit ()
gtk_widget_show (menuitem);

// Open
menuitem = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
menuitem = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
gtk_menu_shell_append (GTK_MENU_SHELL (submenu), menuitem);
// gtk_object_set_data (GTK_OBJECT (menuitem), "help-id", GINT_TO_POINTER (2));
// g_signal_connect (G_OBJECT (menuitem), "enter-notify-event", G_CALLBACK (on_display_help), message);
Expand Down
7 changes: 5 additions & 2 deletions src/mapedit/gui_mapview.cc
Expand Up @@ -347,7 +347,7 @@ void GuiMapview::indicateOverlap ()

// get all objects that possibly intersect with our object
std::list<world::chunk_info*> objs_on_map = area->objects_in_bbox (ci.Min, ci.Max);
if (!objs_on_map.empty() && DrawObj->intersects(objs_on_map, ci.center_min() - V1))
if (DrawObj->intersects(objs_on_map, ci.center_min() - V1))
{
gfx::surface *tint = gfx::create_surface ();
tint->set_alpha (128);
Expand Down Expand Up @@ -454,6 +454,9 @@ void GuiMapview::selectObj (MapEntity *ety)
Grid->set_visible (true);
Grid->draw ();

// update overlap indication
highlightObject();

// update screen
GdkRectangle rect = { 0, 0, Overlay->length(), Overlay->height() };
gdk_window_invalidate_rect (gtk_widget_get_window (Screen), &rect, FALSE);
Expand Down Expand Up @@ -520,7 +523,7 @@ void GuiMapview::placeCurObj()
return;
}

if (DrawObj != NULL)
if (DrawObj != NULL && (DrawObj->canPlaceOnMap() || !Grid->overlap_prevented()))
{
MapData *area = (MapData*) MapMgr::get_map();
world::entity *ety = DrawObj->entity();
Expand Down
6 changes: 4 additions & 2 deletions src/mapedit/map_entity.cc
Expand Up @@ -226,7 +226,7 @@ bool MapEntity::removeAtCurLocation()
}

// check intersection of map entity at given position with objects from the given list
bool MapEntity::intersects (const std::list<world::chunk_info*> & objects, const world::vector3<s_int32> & pos) const
bool MapEntity::intersects (const std::list<world::chunk_info*> & objects, const world::vector3<s_int32> & pos)
{
// check all objects returned
for (std::list<world::chunk_info*>::const_iterator i = objects.begin(); i != objects.end(); i++)
Expand All @@ -240,11 +240,13 @@ bool MapEntity::intersects (const std::list<world::chunk_info*> & objects, const
const world::placeable_shape *shape = (*model)->current_shape ();
if (shape->is_solid() && intersects (shape, pos - (*i)->center_min()))
{
Overlapping = true;
return true;
}
}
}

Overlapping = false;
return false;
}

Expand Down Expand Up @@ -377,7 +379,7 @@ GdkPixbuf *MapEntity::get_icon (const u_int32 & size) const

// determine background color
u_int32 color = 0xFFFFFFFF;
if (is_on_map())
if (isOnMap())
{
// present on map, but unused
if (RefCount == 0)
Expand Down
14 changes: 11 additions & 3 deletions src/mapedit/map_entity.h
Expand Up @@ -80,7 +80,7 @@ class MapEntity
* @param pos position of ourselves.
* @return true if intersection is found, false otherwise.
*/
bool intersects (const std::list<world::chunk_info*> & objects, const world::vector3<s_int32> & pos) const;
bool intersects (const std::list<world::chunk_info*> & objects, const world::vector3<s_int32> & pos);

/**
* @name Reference count
Expand Down Expand Up @@ -141,7 +141,13 @@ class MapEntity
* Determines the background color of the row in the entity list.
* @return true if object is part of the map, false otherwise.
*/
bool is_on_map () const { return RefCount != 0; };
bool isOnMap () const { return RefCount != 0; };

/**
* Whether this entity can be placed onto the map.
* @return true if placing is allowed, false otherwise.
*/
bool canPlaceOnMap () const { return !Overlapping; }
//@}

/**
Expand Down Expand Up @@ -222,7 +228,9 @@ class MapEntity
private:
/// number of times this object is present on the map
u_int32 RefCount;

/// whether the object is currently overlapping another object on the map
bool Overlapping;

/// the object this meta data is associated with
world::placeable *Object;
/// the entity this meta data is associated with
Expand Down

0 comments on commit 2e0ee05

Please sign in to comment.