1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#ifdef WIN32
#pragma warning(disable : 4355)
#pragma warning(disable : 4786)
#endif
#include "editview.h"
#include "mainframe.h"
#include "component_node.h"
#include <ClanLib/GUI/button.h>
#include <ClanLib/GUI/component_type.h>
#include <ClanLib/display.h>
/////////////////////////////////////////////////////////////////////////////
// EditView construction:
EditView::EditView(MainFrame *mainframe)
: CL_Component(mainframe), root(0), mode(mode_select),
mouse_capture_mode(mousemove_none), resize_grabber(0), mainframe(mainframe)
{
create_simulation();
Document &doc = get_document();
slots.connect(doc.sig_node_added, this, &EditView::on_node_added);
slots.connect(doc.sig_node_removed, this, &EditView::on_node_removed);
slots.connect(doc.sig_node_options_changed, this, &EditView::on_node_options_changed);
slots.connect(doc.sig_node_type_changed, this, &EditView::on_node_type_changed);
slots.connect(sig_resize(), this, &EditView::on_resize);
slots.connect(sig_paint(), this, &EditView::on_paint);
slots.connect(sig_mouse_down(), this, &EditView::on_mouse_down);
slots.connect(sig_mouse_up(), this, &EditView::on_mouse_up);
slots.connect(sig_mouse_move(), this, &EditView::on_mouse_move);
slots.connect(mainframe->sig_selection_changed, this, &EditView::on_selection_changed);
on_resize(get_width(), get_height());
}
EditView::~EditView()
{
delete root;
}
/////////////////////////////////////////////////////////////////////////////
// EditView attributes:
Document &EditView::get_document()
{
return mainframe->document;
}
/////////////////////////////////////////////////////////////////////////////
// EditView operations:
void EditView::set_mode(EditView::Mode new_mode)
{
mode = new_mode;
}
/////////////////////////////////////////////////////////////////////////////
// EditView implementation:
void EditView::create_simulation()
{
delete root;
root = new CL_GUIManager(get_style_manager());
root->disable_input();
Document &doc = get_document();
std::list<ComponentNode *>::iterator it;
for (it = doc.nodes.begin(); it != doc.nodes.end(); it++)
{
on_node_added(*it);
}
}
void EditView::on_paint()
{
CL_Display::fill_rect(0, 0, get_width(), get_height(), 0.6f,0.6f,0.6f);
// All this shit (except root->show()) should not be needed, but it
// seems to be some kind of bug in clanDisplay, and frankly, I prefer
// a hack than to try and fix clanDisp... -mbn 4th Jan 2002
CL_Display::push_clip_rect();
CL_Display::set_clip_rect(CL_ClipRect(0,0,CL_Display::get_width(),CL_Display::get_height()));
int tx = CL_Display::get_translate_offset_x();
int ty = CL_Display::get_translate_offset_y();
CL_Display::set_translate_offset(0, 0);
root->show();
CL_Display::set_translate_offset(tx, ty);
CL_Display::pop_clip_rect();
// Draw component selection:
std::list<ComponentNode *>::iterator it;
for (it = selection.begin(); it != selection.end(); it++)
{
ComponentNode *cur_selection = *it;
CL_Component *cur_component = components[cur_selection];
CL_Rect rect = get_rect(cur_component);
CL_Display::draw_rect(rect.x1-1, rect.y1-1, rect.x2+1, rect.y2+1, 0.3f, 0.3f, 1.0f);
// Dont draw grabbers while moving the gui.
if (mouse_capture_mode == mousemove_move) continue;
std::vector<CL_Rect> grabbers = get_grab_rects(cur_component);
for (int i=0; i<8; i++)
{
CL_Display::fill_rect(
grabbers[i].x1, grabbers[i].y1, grabbers[i].x2, grabbers[i].y2,
0.4f, 0.4f, 1.0f);
}
}
// Draw selection gfx:
CL_Point mpos = get_mouse_position();
CL_Point delta(mpos.x - mouse_anker.x, mpos.y - mouse_anker.y);
switch (mouse_capture_mode)
{
case mousemove_select_net:
CL_Display::draw_rect(mouse_anker.x, mouse_anker.y, mpos.x, mpos.y, 1.0f, 0.4f, 0.4f, 0.7f);
break;
case mousemove_resize:
break;
case mousemove_move:
for (it = selection.begin(); it != selection.end(); it++)
{
ComponentNode *cur_selection = *it;
CL_Component *cur_component = components[cur_selection];
CL_Rect rect = get_rect(cur_component);
rect.move(delta.x, delta.y);
CL_Display::draw_rect(rect.x1-1, rect.y1-1, rect.x2+1, rect.y2+1, 0.8f, 0.8f, 1.0f, 0.5f);
}
break;
}
}
void EditView::on_resize(int old_width, int old_height)
{
// Position GUI simulation where we are.
// root->set_position(CL_Rect(4, 4, get_width()-4, get_height()-4));
CL_Rect screen_rect = get_screen_rect();
int scr_x = screen_rect.x1+4;
int scr_y = screen_rect.y1+4;
int width = screen_rect.get_width()-8;
int height = screen_rect.get_height()-8;
root->set_position(CL_Rect(scr_x, scr_y, scr_x+width, scr_y+height));
}
void EditView::on_node_added(ComponentNode *node)
{
CL_Component *parent = root;
if (node->get_parent() != 0)
{
std::map<ComponentNode *, CL_Component *>::iterator it_comp;
it_comp = components.find(node->get_parent());
if (it_comp != components.end()) parent = it_comp->second;
}
CL_ComponentType *comp_type = node->find_component_type();
CL_Component *component = comp_type->create_component(parent, root->get_style_manager());
component->sig_set_options().call(node->get_options());
components[node] = component;
std::list<ComponentNode *>::iterator it;
for (it = node->nodes.begin(); it != node->nodes.end(); it++)
{
on_node_added(*it);
}
}
void EditView::on_node_removed(ComponentNode *node)
{
// todo: find node in simulation and remove it.
// todo: remove node from selection, if its in it.
}
void EditView::on_node_options_changed(ComponentNode *node)
{
CL_Component *component = components[node];
component->sig_set_options()(node->get_options());
}
void EditView::on_node_type_changed(ComponentNode *node)
{
// todo: find node in simulation, remove it
// todo: create new node (eg by calling on_node_added).
}
void EditView::on_selection_changed(std::list<ComponentNode *> &new_selection)
{
selection = new_selection;
}
void EditView::on_mouse_down(const CL_Key &key)
{
mouse_anker = CL_Point(key.x, key.y);
std::list<ComponentNode *>::iterator it;
switch (mode)
{
case mode_select:
for (it = selection.begin(); it != selection.end(); it++)
{
ComponentNode *cur_selection = *it;
CL_Component *cur_component = components[cur_selection];
// Try and see if click was on a grabber:
bool gribber = false;
std::vector<CL_Rect> grabbers = get_grab_rects(cur_component);
for (int i=0; i<8; i++)
{
if (grabbers[i].inside(key.x, key.y))
{
mouse_capture_mode = mousemove_resize;
resize_grabber = i;
gribber = true;
break;
}
}
if (gribber) break;
// Clicked one of the selected components - switch to move mode:
CL_Rect rect = get_rect(cur_component);
if (rect.inside(key.x, key.y))
{
mouse_capture_mode = mousemove_move;
}
}
if (mouse_capture_mode == mousemove_none)
{
// Since it was none of the above, it has to be
// a new net selection:
mouse_capture_mode = mousemove_select_net;
// Clear current selection:
std::list<ComponentNode *> empty_list;
mainframe->sig_selection_changed(empty_list);
}
break;
case mode_new:
break;
}
if (mouse_capture_mode != mousemove_none) capture_mouse();
}
void EditView::on_mouse_up(const CL_Key &key)
{
if (mouse_capture_mode == mousemove_none) return;
switch (mouse_capture_mode)
{
case mousemove_select_net:
if (mouse_anker.x == key.x && mouse_anker.y == key.y)
{
// Check if component was clicked:
CL_Component *comp = root->get_component_at(key.x - 4, key.y - 4);
std::map<ComponentNode *, CL_Component *>::iterator it;
for (it = components.begin(); it != components.end(); it++)
{
if (it->second == comp)
{
selection.push_back(it->first);
break;
}
}
mainframe->sig_selection_changed(selection);
}
else
{
CL_Rect selection_rect(mouse_anker.x, mouse_anker.y, key.x, key.y);
selection_rect.normalize();
std::map<ComponentNode *, CL_Component *>::iterator it;
for (it = components.begin(); it != components.end(); it++)
{
CL_Rect comp_rect = get_rect(it->second);
if (
selection_rect.inside(comp_rect.x1, comp_rect.y1) &&
selection_rect.inside(comp_rect.x2, comp_rect.y2))
{
selection.push_back(it->first);
}
}
mainframe->sig_selection_changed(selection);
}
break;
case mousemove_resize:
break;
case mousemove_move:
{
int delta_x = key.x - mouse_anker.x;
int delta_y = key.y - mouse_anker.y;
std::list<ComponentNode *>::iterator it;
for (it = selection.begin(); it != selection.end(); it++)
{
ComponentNode *node = *it;
CL_ComponentOptions options = node->get_options();
int x = options.get_value_as_int("x") + delta_x;
int y = options.get_value_as_int("y") + delta_y;
options.remove_option("x");
options.remove_option("y");
options.add_option("x", x);
options.add_option("y", y);
node->set_options(options);
}
}
break;
}
release_mouse();
mouse_capture_mode = mousemove_none;
}
void EditView::on_mouse_move(CL_Component *component, int x, int y)
{
switch (mouse_capture_mode)
{
case mousemove_select_net:
break;
case mousemove_resize:
break;
case mousemove_move:
break;
}
}
std::vector<CL_Rect> EditView::get_grab_rects(CL_Component *component)
{
std::vector<CL_Rect> rects;
CL_Rect rect = get_rect(component);
int cx = rect.x1 + rect.get_width()/2;
int cy = rect.y1 + rect.get_height()/2;
rects.push_back(CL_Rect(rect.x1-2, rect.y1-2, rect.x1+2, rect.y1+2));
rects.push_back(CL_Rect(rect.x1-2, rect.y2-2, rect.x1+2, rect.y2+2));
rects.push_back(CL_Rect(rect.x2-2, rect.y1-2, rect.x2+2, rect.y1+2));
rects.push_back(CL_Rect(rect.x2-2, rect.y2-2, rect.x2+2, rect.y2+2));
rects.push_back(CL_Rect(cx-2, rect.y1-2, cx+2, rect.y1+2));
rects.push_back(CL_Rect(cx-2, rect.y2-2, cx+2, rect.y2+2));
rects.push_back(CL_Rect(rect.x1-2, cy-2, rect.x1+2, cy+2));
rects.push_back(CL_Rect(rect.x2-2, cy-2, rect.x2+2, cy+2));
return rects;
}
CL_Rect EditView::get_rect(CL_Component *component)
{
CL_Rect rect = component->get_screen_rect();
// Adjust rect from screen position to editview position:
rect.move(-root->get_position().x1, -root->get_position().y1);
rect.move(4, 4); // we got a 4 pixel border.
return rect;
}
|