void opacity_map_editor::paintEvent(QPaintEvent* paint_event) { // paint the frame QFrame::paintEvent(paint_event); QPainter painter(this); // fill background with gradient QLinearGradient gradient(0, 0, 0, 0); switch (_orientation) { case horizontal: gradient.setFinalStop(this->contentsRect().width(), 0); break; case vertical: gradient.setStart(0, this->contentsRect().height()); break; } gradient.setColorAt(0.0, Qt::gray); gradient.setColorAt(1.0, Qt::white); QBrush brush(gradient); brush.setStyle(Qt::LinearGradientPattern); painter.fillRect(this->contentsRect(), brush); // draw polyline painter.setRenderHint(QPainter::Antialiasing); painter.setPen(_line_pen); painter.drawPolyline(_polyline.get(), static_cast<unsigned>(_polyline_len)); // draw line edit anchors for (anchor_container::iterator anchor = _anchors.begin(); anchor != _anchors.end(); ++anchor) { if (anchor != _selected_anchor) { painter.setPen(_anchor_pen); painter.setBrush(_anchor_brush); painter.drawEllipse(bounding_rect(anchor->point(), _anchor_point_size)); } } if (_selected_anchor != _anchors.end()) { painter.setPen(_anchor_highlight_pen); painter.setBrush(_anchor_highlight_brush); painter.drawEllipse(bounding_rect(_selected_anchor->point(), _anchor_point_size)); } }
//inner functions static ps_rect _path_bounding_rect(const graphic_path& path) { ps_rect r = {1, 1, 0, 0}; scalar x1 = 1, y1 = 1, x2 = 0 ,y2 = 0; if (bounding_rect(const_cast<graphic_path&>(path), 0, &x1, &y1, &x2, &y2)) { r.x = x1; r.y = y1; r.w = x2-x1; r.h = y2-y1; } return r; }
bool Cinder2DInteractItem::detect_click_selection(ci::app::MouseEvent mouse_event) { if (!detect_selection_) return false; Rect bounds = bounding_rect(); bounds *= scale().x; bounds += ci::Vec2f(position().x, position().y); if (bounds.isInside(ci::Vec2f(mouse_event.getX(), mouse_event.getY()))) { activate(Client::user_identity()); return true; } return false; }
void scale(double w, double h) { m_affine.reset(); double x1, y1, x2, y2; bounding_rect(m_path, *this, 0, m_styles.size(), &x1, &y1, &x2, &y2); if(x1 < x2 && y1 < y2) { trans_viewport vp; vp.preserve_aspect_ratio(0.5, 0.5, aspect_ratio_meet); vp.world_viewport(x1, y1, x2, y2); vp.device_viewport(0, 0, w, h); m_affine = vp.to_affine(); } m_curve.approximation_scale(m_affine.scale()); }
void widget::process_event(const event& ev) { switch (ev.type) { case event::mouse_move_abs: { bool inside (point_in_rectangle(ev.xy, bounding_rect())); if (inside != mouse_inside_flag_) { if (inside) on_mouse_enter(); else on_mouse_leave(); mouse_inside_flag_ = inside; } } break; default: ; } }
Rectf FontProvider_Vector::get_bounding_box(const std::string &reference_string) { Rectf bounding_rect(100000.0f, 100000.0f, -100000.0f, -100000.0f); for( unsigned int i=0; i<reference_string.length(); i++ ) { store_in_char_cache(reference_string[i]); std::vector<Vec2f> &primitives_array = char_cache[reference_string[i]].primitives_array; for (unsigned int cnt=0; cnt< primitives_array.size(); cnt++) { clan::Vec2f pos = primitives_array[cnt]; bounding_rect.left = clan::min(bounding_rect.left, pos.x); bounding_rect.top = clan::min(bounding_rect.top, pos.y); bounding_rect.right = clan::max(bounding_rect.right, pos.x); bounding_rect.bottom = clan::max(bounding_rect.bottom, pos.y); } } if (bounding_rect.left > bounding_rect.right) return Rectf(); return bounding_rect; }
void MainWindow::on_actionRecenter_triggered() { this->graphicsView->setSceneRect(bounding_rect()); this->graphicsView->fitInView(bounding_rect(), Qt::KeepAspectRatio); }
void opacity_map_editor::mousePressEvent(QMouseEvent* mouse_event) { if (_transfer_function) { anchor_container::iterator hit_existing_anchor = _anchors.end(); for (anchor_container::iterator i = _anchors.begin(); i != _anchors.end() && hit_existing_anchor == _anchors.end(); ++i) { QPainterPath painter_path; painter_path.addEllipse(bounding_rect(i->point(), _anchor_point_size)); if (painter_path.contains(mouse_event->pos())) { hit_existing_anchor = i; } } if (mouse_event->button() == Qt::LeftButton) { if (hit_existing_anchor != _anchors.end()) { _active_anchor = hit_existing_anchor; } else { QPointF new_point; opacity_map::stop_type new_stop; new_point.setX(scm::math::clamp(mouse_event->pos().x(), this->contentsRect().x(), this->contentsRect().x() + this->contentsRect().width())); new_point.setY(scm::math::clamp(mouse_event->pos().y(), this->contentsRect().y(), this->contentsRect().y() + this->contentsRect().height())); switch (_orientation) { case horizontal: new_stop = horizontal_stop(new_point, this->contentsRect()); break; case vertical: new_stop = vertical_stop(new_point, this->contentsRect()); break; } opacity_map::type::insert_return_type ret = _transfer_function->add_stop(new_stop); if (ret.second == true) { _anchors.push_back(opacity_map_anchor(new_point, new_stop)); _transfer_function->add_stop(new_stop); //std::cout << mouse_event->pos().x() << "x" << mouse_event->pos().y() << " " // << _anchors.back().stop().first << "x" << _anchors.back().stop().second << std::endl; _active_anchor = boost::prior(_anchors.end()); /*emit*/ function_changed(); update_representation(); } } set_selected_anchor(_active_anchor); update(); } else if(mouse_event->button() == Qt::RightButton) { if (hit_existing_anchor != _anchors.end()) { _transfer_function->del_stop(hit_existing_anchor->stop()); _anchors.erase(hit_existing_anchor); _active_anchor = _anchors.end(); set_selected_anchor(_anchors.end()); /*emit*/ function_changed(); update_representation(); update(); } } } }
static rect get_bounding_rect(graphic_path& path) { rect_s rc(0,0,0,0); bounding_rect(path, 0, &rc.x1, &rc.y1, &rc.x2, &rc.y2); return rect((int)Floor(rc.x1), (int)Floor(rc.y1), (int)Ceil(rc.x2), (int)Ceil(rc.y2)); }
void VertexArrayRenderer::RefreshArray() { SortPrimitives(); m_vertex_data.clear(); m_color_data.clear(); m_texture_data.clear(); m_index_data.clear(); m_vertex_data.reserve( m_vertex_count ); m_color_data.reserve( m_vertex_count ); m_texture_data.reserve( m_vertex_count ); m_index_data.reserve( m_index_count ); m_batches.clear(); m_last_vertex_count = 0; m_last_index_count = 0; auto primitives_size = m_primitives.size(); // Default viewport Batch current_batch; current_batch.viewport = m_default_viewport; current_batch.atlas_page = 0; current_batch.start_index = 0; current_batch.index_count = 0; current_batch.min_index = 0; current_batch.max_index = static_cast<GLuint>( m_vertex_count - 1 ); current_batch.custom_draw = false; sf::FloatRect window_viewport( 0.f, 0.f, static_cast<float>( m_window_size.x ), static_cast<float>( m_window_size.y ) ); for( std::size_t primitive_index = 1; primitive_index != primitives_size + 1; primitive_index += 1 ) { Primitive* primitive = m_primitives[primitive_index - 1].get(); primitive->SetSynced(); if( !primitive->IsVisible() ) { continue; } sf::Vector2f position_transform( primitive->GetPosition() ); auto viewport = primitive->GetViewport(); std::size_t atlas_page = 0; auto viewport_rect = window_viewport; // Check if primitive needs to be rendered in a custom viewport. if( viewport && ( ( *viewport ) != ( *m_default_viewport ) ) ) { sf::Vector2f destination_origin( viewport->GetDestinationOrigin() ); sf::Vector2f size( viewport->GetSize() ); position_transform += ( destination_origin - viewport->GetSourceOrigin() ); if( m_cull ) { viewport_rect.left = destination_origin.x; viewport_rect.top = destination_origin.y; viewport_rect.width = size.x; viewport_rect.height = size.y; } } const std::shared_ptr<Signal>& custom_draw_callback( primitive->GetCustomDrawCallback() ); if( custom_draw_callback ) { // Start a new batch. current_batch.max_index = m_last_vertex_count ? ( static_cast<GLuint>( m_last_vertex_count ) - 1 ) : 0; m_batches.push_back( current_batch ); // Mark current_batch custom draw batch. current_batch.viewport = viewport; current_batch.start_index = 0; current_batch.index_count = 0; current_batch.min_index = 0; current_batch.max_index = 0; current_batch.custom_draw = true; current_batch.custom_draw_callback = custom_draw_callback; // Start a new batch. m_batches.push_back( current_batch ); // Reset current_batch to defaults. current_batch.viewport = m_default_viewport; current_batch.start_index = m_last_index_count; current_batch.index_count = 0; current_batch.min_index = m_last_vertex_count ? ( static_cast<GLuint>( m_last_vertex_count ) - 1 ) : 0; current_batch.custom_draw = false; } else { // Process primitive's vertices and indices const std::vector<Primitive::Vertex>& vertices( primitive->GetVertices() ); const std::vector<GLuint>& indices( primitive->GetIndices() ); sf::Vector2f position( 0.f, 0.f ); sf::FloatRect bounding_rect( 0.f, 0.f, 0.f, 0.f ); for( const auto& vertex : vertices ) { position.x = vertex.position.x + position_transform.x; position.y = vertex.position.y + position_transform.y; m_vertex_data.push_back( position ); m_color_data.push_back( vertex.color ); atlas_page = static_cast<unsigned int>( vertex.texture_coordinate.y ) / m_max_texture_size; // Used to normalize texture coordinates. sf::Vector2f normalizer( 1.f / static_cast<float>( m_texture_atlas[atlas_page]->getSize().x ), 1.f / static_cast<float>( m_texture_atlas[atlas_page]->getSize().y ) ); // Normalize SFML's pixel texture coordinates. m_texture_data.push_back( sf::Vector2f( vertex.texture_coordinate.x * normalizer.x, static_cast<float>( static_cast<unsigned int>( vertex.texture_coordinate.y ) % m_max_texture_size ) * normalizer.y ) ); // Update the bounding rect. if( m_cull ) { if( position.x < bounding_rect.left ) { bounding_rect.width += bounding_rect.left - position.x; bounding_rect.left = position.x; } else if( position.x > bounding_rect.left + bounding_rect.width ) { bounding_rect.width = position.x - bounding_rect.left; } if( position.y < bounding_rect.top ) { bounding_rect.height += bounding_rect.top - position.y; bounding_rect.top = position.y; } else if( position.y > bounding_rect.top + bounding_rect.height ) { bounding_rect.height = position.y - bounding_rect.top; } } } if( m_cull && !viewport_rect.intersects( bounding_rect ) ) { m_vertex_data.resize( m_last_vertex_count ); m_color_data.resize( m_last_vertex_count ); m_texture_data.resize( m_last_vertex_count ); } else { for( const auto& index : indices ) { m_index_data.push_back( m_last_vertex_count + index ); } // Check if we need to start a new batch. if( ( ( *viewport ) != ( *current_batch.viewport ) ) || ( atlas_page != current_batch.atlas_page ) ) { current_batch.max_index = m_last_vertex_count ? ( static_cast<GLuint>( m_last_vertex_count ) - 1 ) : 0; m_batches.push_back( current_batch ); // Reset current_batch to defaults. current_batch.viewport = viewport; current_batch.atlas_page = atlas_page; current_batch.start_index = m_last_index_count; current_batch.index_count = 0; current_batch.min_index = m_last_vertex_count ? ( static_cast<GLuint>( m_last_vertex_count ) - 1 ) : 0; current_batch.custom_draw = false; } current_batch.index_count += static_cast<unsigned int>( indices.size() ); m_last_vertex_count += static_cast<GLsizei>( vertices.size() ); m_last_index_count += static_cast<GLsizei>( indices.size() ); } } } current_batch.max_index = m_last_vertex_count ? ( static_cast<GLuint>( m_last_vertex_count ) - 1 ) : 0; m_batches.push_back( current_batch ); }
/* method: "get_rect() -> (x,y,w,h)\n Returns the bounding rectangle. " */ static Rect Shape_rect(const Object& self){ return bounding_rect(self.GetTri()); }
inline bool outside(Object* obj, const PosInfo& info){ return !bounding_rect(obj->GetTri()).Contains(info.pos); }