-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update method for drawing slider bars (#6297)
* update method for drawing slider bars Instead of calling ofPath tessellation on each draw call for sliders, slider knows how to generate mesh for bar rectangles itself. The mesh is cached, and only updated iff the slider value changes. Rect mesh caching works similarly to the slider's internal caching of text meshes. You might notice a slight perfomance improvement on GUIs with multiple sliders with this PR. As an additional check/optimisation, slider fill rectangles with either width or height < 1 pixels will also not be drawn (they would not be visible anyway). By *not* using ofPath (and by implication the tessellator) to draw the slider we avoid having to ask the tessellator to tessellate extremely small paths, in which case it had a tendency to throw its hands up in despair or disgust (hard to tell from a stack trace) and cause crashes (see issue #6294). #changelog #addons #ofxGui
- Loading branch information
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#pragma once | ||
|
||
#include "ofColor.h" | ||
#include "ofRectangle.h" | ||
#include "ofVboMesh.h" | ||
|
||
/* | ||
* Internal helper to generate and cache rectangle meshes for ofxGui. | ||
*/ | ||
class ofxGuiRectMesh { | ||
ofColor mColorFill = {}; | ||
ofRectangle mRect = {}; | ||
ofVboMesh mMesh = {}; | ||
bool isDirty = true; | ||
bool mHasMesh = false; | ||
|
||
public: | ||
void clear() { | ||
mColorFill = {}; | ||
mRect = {}; | ||
mMesh.clear(); | ||
mHasMesh = false; | ||
isDirty = false; | ||
} | ||
|
||
void draw() { | ||
|
||
if ( isDirty && mHasMesh ) { | ||
mMesh.clear(); | ||
mHasMesh = false; | ||
isDirty = false; | ||
} | ||
|
||
if ( mRect.width < 1.f || mRect.height < 1.f ) { | ||
// We will not draw a mesh for rectangles | ||
// which are smaller than one pixel for | ||
// either w, or height. | ||
return; | ||
} | ||
|
||
if ( mHasMesh == false ) { | ||
|
||
mMesh.addVertex( mRect.getBottomLeft() ); | ||
mMesh.addVertex( mRect.getBottomRight() ); | ||
mMesh.addVertex( mRect.getTopLeft() ); | ||
mMesh.addVertex( mRect.getTopRight() ); | ||
|
||
mMesh.addColor( mColorFill ); | ||
mMesh.addColor( mColorFill ); | ||
mMesh.addColor( mColorFill ); | ||
mMesh.addColor( mColorFill ); | ||
|
||
mMesh.addIndex( 0 ); | ||
mMesh.addIndex( 1 ); | ||
mMesh.addIndex( 2 ); | ||
mMesh.addIndex( 2 ); | ||
mMesh.addIndex( 1 ); | ||
mMesh.addIndex( 3 ); | ||
|
||
mMesh.setMode( ofPrimitiveMode::OF_PRIMITIVE_TRIANGLES ); | ||
mHasMesh = true; | ||
} | ||
|
||
mMesh.draw(); | ||
} | ||
|
||
void setFillColor( ofColor const &color ) { | ||
isDirty |= ( color != mColorFill ); | ||
mColorFill = color; | ||
} | ||
|
||
void setExtents( ofRectangle const &rect ) { | ||
isDirty |= ( rect != mRect ); | ||
mRect = rect; | ||
} | ||
|
||
void setExtents( float x, float y, float w, float h ) { | ||
setExtents( {x, y, w, h} ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters