Skip to content

Commit

Permalink
ADDED better support for editing composite models
Browse files Browse the repository at this point in the history
  • Loading branch information
ksterker committed Oct 1, 2009
1 parent 857c267 commit 1c47d11
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/modeller/gui_modeller.cc
Expand Up @@ -856,7 +856,8 @@ GuiModeller::GuiModeller ()

// setup preview -- needs to happen before window is realized
widget = gtk_builder_get_object (Ui, "model_area");
Preview = new GuiPreview (GTK_WIDGET (widget), shapeData);
GtkTreeModel *sprites = GTK_TREE_MODEL(gtk_builder_get_object (Ui, "sprite_list"));
Preview = new GuiPreview (GTK_WIDGET (widget), shapeData, sprites);

// get reference to dialog window
Window = GTK_WIDGET (gtk_builder_get_object (Ui, "main_window"));
Expand Down Expand Up @@ -1067,7 +1068,7 @@ void GuiModeller::addSprite (const std::string & name)
printf ("*** warning: cannot create sprite path relative to data directory!\n");
}

// set relatice sprite name
// set relative sprite name
model->set_sprite (sprite_path);

// and add it to the UI
Expand Down Expand Up @@ -1288,7 +1289,7 @@ void GuiModeller::updateShapeList (world::placeable_model *model)
// enable add shape button
setActive ("add_shape", true);

// TODO: update solid flag and image offset (X and Y only)
// TODO: update solid flag
}

// enable or disable a widget
Expand Down
43 changes: 40 additions & 3 deletions src/modeller/gui_preview.cc
Expand Up @@ -102,7 +102,8 @@ static void on_number_changed (GtkEditable *editable, gpointer data)
}

// ctor
GuiPreview::GuiPreview (GtkWidget *drawing_area, GtkEntry** shape_data) : DrawingArea (drawing_area), ShapeData (shape_data)
GuiPreview::GuiPreview (GtkWidget *drawing_area, GtkEntry** shape_data, GtkTreeModel *models)
: DrawingArea (drawing_area), ShapeData (shape_data), ModelList (models)
{
#ifdef __APPLE__
// no need to use double buffering on OSX, but appears to be required elsewhere
Expand Down Expand Up @@ -178,7 +179,40 @@ void GuiPreview::render (const int & sx, const int & sy, const int & l, const in
// set clipping rectangle
gfx::drawing_area da (sx, sy, l, h);

// draw to target
// render x and y axis
u_int32 color = Target->map_color (0x40, 0x40, 0x40);
Target->draw_line (0, Target->height()/2, Target->length(), Target->height()/2, color, &da);
Target->draw_line (Target->length()/2, 0, Target->length()/2, Target->height(), color, &da);

// collect all the sprites we have for rendering
GtkTreeIter iter;
std::list<world::render_info> models;
if (gtk_tree_model_get_iter_first (ModelList, &iter))
{
world::placeable_model *model = NULL;
std::vector<world::shadow_info> shadow;

do
{
gtk_tree_model_get (ModelList, &iter, 1, &model, -1);
if (model != NULL)
{
gfx::sprite *sprt = model->get_sprite();
if (sprt != NULL)
{
// information required for rendering
world::render_info ri (model->current_shape(), sprt, world::vector3<s_int32>(), &shadow);
models.push_back (ri);
}
}
}
while (gtk_tree_model_iter_next (ModelList, &iter));
}

// draw models
Renderer.render (models, da, Target);

// draw handles
Renderer.render (Model, Handles, da, Target);
}

Expand All @@ -190,8 +224,11 @@ void GuiPreview::render (const int & sx, const int & sy, const int & l, const in
// set object being edited
void GuiPreview::setCurModel (world::placeable_model *model)
{
Renderer.setActiveModel (model);

// remember model for updates
Model = model;

// no handle selected, initially
SelectedHandle = -1;

Expand Down
4 changes: 3 additions & 1 deletion src/modeller/gui_preview.h
Expand Up @@ -43,7 +43,7 @@ class GuiPreview
* @param drawing_area the surface to draw on
* @param shape_data gui elements for displaying shape information.
*/
GuiPreview (GtkWidget *drawing_area, GtkEntry** shape_data);
GuiPreview (GtkWidget *drawing_area, GtkEntry** shape_data, GtkTreeModel *models);

/**
* Update (part of) the screen from backing pixmap.
Expand Down Expand Up @@ -155,6 +155,8 @@ class GuiPreview
GtkWidget *DrawingArea;
/// the fields for displaying/editing shape data
GtkEntry **ShapeData;
/// list of model parts
GtkTreeModel *ModelList;
/// the render surface
gfx::surface *Target;
/// the shape currently being edited
Expand Down
54 changes: 44 additions & 10 deletions src/modeller/mdl_renderer.cc
Expand Up @@ -24,28 +24,50 @@
* @brief A renderer for single shapes
*/

#include <gfx/gfx.h>

#include "mdl_handle.h"
#include "mdl_renderer.h"

// ctor
ModelRenderer::ModelRenderer () : world::default_renderer ()
{
ActiveShape = NULL;
ActiveModel = NULL;
Overlay = gfx::create_surface();
Overlay->set_alpha(96, true);
}

void ModelRenderer::render (std::list <world::render_info> & objectlist, const gfx::drawing_area & da, gfx::surface * target) const
{
// reset overlay
Overlay->resize (target->length(), target->height());
Overlay->fillrect (0, 0, target->length(), target->height(), 0);

// center on screen
s_int16 x = target->length() / 2;
s_int16 y = target->height() / 2;

// render
default_renderer::render (x, y, objectlist, da, target);

// finish rendering
Overlay->draw (0, 0, &da, target);
}

// render model and handles
void ModelRenderer::render (world::placeable_model *model, GdkPoint *handles, const gfx::drawing_area & da, gfx::surface *target)
{
gfx::sprite *sprt = model->get_sprite();
if (sprt != NULL)
{
{
// information required for rendering
std::vector<world::shadow_info> shadow;
world::render_info ri (model->current_shape(), sprt, world::vector3<s_int32>(), &shadow);

// center on screen
s_int16 x = (target->length() - sprt->length()) / 2;
s_int16 y = (target->height() - sprt->height()) / 2;
s_int16 x = target->length() / 2;
s_int16 y = target->height() / 2;

// draw to target
draw (handles, x, y, ri, da, target);
Expand All @@ -58,11 +80,26 @@ void ModelRenderer::render (world::placeable_model *model, GdkPoint *handles, co
}
}

// draw with translucency
void ModelRenderer::draw (const s_int16 & x, const s_int16 & y, const world::render_info & obj, const gfx::drawing_area & da, gfx::surface * target) const
{
if (ActiveModel != NULL && obj.Shape == ActiveModel->current_shape())
{
Overlay->draw (0, 0, &da, target);
Overlay->fillrect (0, 0, Overlay->length(), Overlay->height(), 0);
renderer_base::draw (x, y, obj, da, target);
}
else
{
renderer_base::draw (x, y, obj, da, Overlay);
}
}

// draw cube outline(s)
void ModelRenderer::draw (GdkPoint *handles, const s_int16 & x, const s_int16 & y, const world::render_info & ri, const gfx::drawing_area & da, gfx::surface * target) const
{
// render sprite
renderer_base::draw (x, y, ri, da, target);
// renderer_base::draw (x, y, ri, da, target);

// sprite screen coordinates
s_int16 sx = x + ri.screen_x ();
Expand All @@ -75,15 +112,12 @@ void ModelRenderer::draw (GdkPoint *handles, const s_int16 & x, const s_int16 &
// render shapes, if any, relative to sprite
for (std::vector<world::cube3*>::const_iterator i = ri.Shape->begin(); i != ri.Shape->end(); i++)
{
s_int16 ox = sx - ri.Shape->ox();
s_int16 oy = sy + (*i)->max_z() /*- (*i)->min_z()*/ - ri.Shape->oy();

(*i)->draw (ox, oy, &da, target);
(*i)->draw (x, y, &da, target);

if (ActiveShape == *i)
{
ox += (*i)->min_x();
oy += (*i)->min_y() - (*i)->min_z();
s_int16 ox = x + (*i)->min_x();
s_int16 oy = y + (*i)->min_y() - (*i)->min_z();

updateHandles (handles, ox, oy);
}
Expand Down
19 changes: 18 additions & 1 deletion src/modeller/mdl_renderer.h
Expand Up @@ -47,9 +47,13 @@ class ModelRenderer : public world::default_renderer
*/
virtual ~ModelRenderer () {}

void render (std::list <world::render_info> & objectlist, const gfx::drawing_area & da, gfx::surface * target) const;

void draw (const s_int16 & x, const s_int16 & y, const world::render_info & obj, const gfx::drawing_area & da, gfx::surface * target) const;

/**
* Render the given shape.
* @param model the model to draw.
* @param model the model being currently edited.
* @param handles the handles for the currently edited shape.
* @param da clipping rectangle.
* @param target surface to draw on.
Expand All @@ -65,6 +69,15 @@ class ModelRenderer : public world::default_renderer
*/
void drawHandle (const GdkPoint & handle, const bool & highlight, const gfx::drawing_area & da, gfx::surface * target) const;

/**
* Set the model that is being edited, so we can draw it specially.
* @param model the model being edited or NULL to reset.
*/
void setActiveModel (world::placeable_model *model)
{
ActiveModel = model;
}

/**
* Set the shape that is being edited, so we can draw the handles.
* @param cube the cube being edited or NULL to reset.
Expand Down Expand Up @@ -106,8 +119,12 @@ class ModelRenderer : public world::default_renderer
void drawRect (const s_int16 & x, const s_int16 & y, const u_int16 & l, const u_int16 & h, const u_int32 & color, const gfx::drawing_area & da, gfx::surface * target) const;

private:
/// this is the model currently being edited
world::placeable_model *ActiveModel;
/// this is the shape currently being edited
world::cube3 *ActiveShape;
/// additional surface for rendering with translucency
gfx::surface *Overlay;
};

#endif

0 comments on commit 1c47d11

Please sign in to comment.