4
4
#include < SFML/Config.hpp>
5
5
#include < SFML/Graphics/Color.hpp>
6
6
#include < SFML/Graphics/RenderTarget.hpp>
7
+ #include < SFML/Graphics/RenderTexture.hpp>
7
8
#include < SFML/Graphics/RenderWindow.hpp>
8
9
#include < SFML/Graphics/Sprite.hpp>
9
10
#include < SFML/Graphics/Texture.hpp>
@@ -155,12 +156,6 @@ GLuint convertImTextureIDToGLTextureHandle(ImTextureID textureID);
155
156
void RenderDrawLists (
156
157
ImDrawData* draw_data); // rendering callback function prototype
157
158
158
- // Implementation of ImageButton overload
159
- bool imageButtonImpl (const sf::Texture& texture,
160
- const sf::FloatRect& textureRect, const sf::Vector2f& size,
161
- const int framePadding, const sf::Color& bgColor,
162
- const sf::Color& tintColor);
163
-
164
159
// Default mapping is XInput gamepad mapping
165
160
void initDefaultJoystickMapping ();
166
161
@@ -549,51 +544,48 @@ void SetLStickYAxis(sf::Joystick::Axis lStickYAxis, bool inverted) {
549
544
550
545
} // end of namespace SFML
551
546
552
- // ///////////// Image Overloads
547
+ // ///////////// Image Overloads for sf::Texture
553
548
554
549
void Image (const sf::Texture& texture, const sf::Color& tintColor,
555
550
const sf::Color& borderColor) {
556
- Image (texture, static_cast <sf::Vector2f>(texture.getSize ()), tintColor,
557
- borderColor);
551
+ Image (texture, static_cast <sf::Vector2f>(texture.getSize ()),
552
+ tintColor, borderColor);
558
553
}
559
554
560
555
void Image (const sf::Texture& texture, const sf::Vector2f& size,
561
556
const sf::Color& tintColor, const sf::Color& borderColor) {
562
557
ImTextureID textureID =
563
558
convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
564
559
565
- ImGui::Image (textureID, ImVec2 (size.x ,size.y ), ImVec2 (0 , 0 ), ImVec2 (1 , 1 ), toImColor (tintColor),
566
- toImColor (borderColor));
567
- }
568
-
569
- void Image (const sf::Texture& texture, const sf::FloatRect& textureRect,
570
- const sf::Color& tintColor, const sf::Color& borderColor) {
571
- Image (
572
- texture,
573
- sf::Vector2f (std::abs (textureRect.width ), std::abs (textureRect.height )),
574
- textureRect, tintColor, borderColor);
560
+ ImGui::Image (textureID, ImVec2 (size.x ,size.y ),
561
+ ImVec2 (0 , 0 ), ImVec2 (1 , 1 ),
562
+ toImColor (tintColor), toImColor (borderColor));
575
563
}
576
564
577
- void Image ( const sf::Texture& texture, const sf::Vector2f& size,
578
- const sf::FloatRect& textureRect , const sf::Color& tintColor,
565
+ // ///////////// Image Overloads for sf::RenderTexture
566
+ void Image ( const sf::RenderTexture& texture , const sf::Color& tintColor,
579
567
const sf::Color& borderColor) {
580
- sf::Vector2f textureSize = static_cast <sf::Vector2f>(texture.getSize ());
581
- ImVec2 uv0 (textureRect.left / textureSize.x ,
582
- textureRect.top / textureSize.y );
583
- ImVec2 uv1 ((textureRect.left + textureRect.width ) / textureSize.x ,
584
- (textureRect.top + textureRect.height ) / textureSize.y );
568
+ Image (texture, static_cast <sf::Vector2f>(texture.getSize ()),
569
+ tintColor, borderColor);
570
+ }
585
571
572
+ void Image (const sf::RenderTexture& texture, const sf::Vector2f& size,
573
+ const sf::Color& tintColor, const sf::Color& borderColor) {
586
574
ImTextureID textureID =
587
- convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
588
- ImGui::Image (textureID, ImVec2 (size.x , size.y ), uv0, uv1, toImColor (tintColor),
589
- toImColor (borderColor));
575
+ convertGLTextureHandleToImTextureID (texture.getTexture ().getNativeHandle ());
576
+
577
+ ImGui::Image (textureID, ImVec2 (size.x ,size.y ),
578
+ ImVec2 (0 , 1 ), ImVec2 (1 , 0 ), // flipped vertically, because textures in sf::RenderTexture are stored this way
579
+ toImColor (tintColor), toImColor (borderColor));
590
580
}
591
581
582
+ // ///////////// Image Overloads for sf::Sprite
583
+
592
584
void Image (const sf::Sprite& sprite, const sf::Color& tintColor,
593
585
const sf::Color& borderColor) {
594
586
sf::FloatRect bounds = sprite.getGlobalBounds ();
595
- Image (sprite, sf::Vector2f (bounds.width , bounds.height ), tintColor,
596
- borderColor);
587
+ Image (sprite, sf::Vector2f (bounds.width , bounds.height ),
588
+ tintColor, borderColor);
597
589
}
598
590
599
591
void Image (const sf::Sprite& sprite, const sf::Vector2f& size,
@@ -604,12 +596,22 @@ void Image(const sf::Sprite& sprite, const sf::Vector2f& size,
604
596
return ;
605
597
}
606
598
607
- Image (*texturePtr, size,
608
- static_cast <sf::FloatRect>(sprite.getTextureRect ()), tintColor,
609
- borderColor);
599
+ const sf::Texture& texture = *texturePtr;
600
+ sf::Vector2f textureSize = static_cast <sf::Vector2f>(texture.getSize ());
601
+ const sf::IntRect& textureRect = sprite.getTextureRect ();
602
+ ImVec2 uv0 (textureRect.left / textureSize.x ,
603
+ textureRect.top / textureSize.y );
604
+ ImVec2 uv1 ((textureRect.left + textureRect.width ) / textureSize.x ,
605
+ (textureRect.top + textureRect.height ) / textureSize.y );
606
+
607
+ ImTextureID textureID =
608
+ convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
609
+
610
+ ImGui::Image (textureID, ImVec2 (size.x , size.y ), uv0, uv1,
611
+ toImColor (tintColor), toImColor (borderColor));
610
612
}
611
613
612
- // ///////////// Image Button Overloads
614
+ // ///////////// Image Button Overloads for sf::Texture
613
615
614
616
bool ImageButton (const sf::Texture& texture, const int framePadding,
615
617
const sf::Color& bgColor, const sf::Color& tintColor) {
@@ -620,12 +622,35 @@ bool ImageButton(const sf::Texture& texture, const int framePadding,
620
622
bool ImageButton (const sf::Texture& texture, const sf::Vector2f& size,
621
623
const int framePadding, const sf::Color& bgColor,
622
624
const sf::Color& tintColor) {
623
- sf::Vector2f textureSize = static_cast <sf::Vector2f>(texture.getSize ());
624
- return ::imageButtonImpl (
625
- texture, sf::FloatRect (0 .f , 0 .f , textureSize.x , textureSize.y ), size,
626
- framePadding, bgColor, tintColor);
625
+ ImTextureID textureID =
626
+ convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
627
+
628
+ return ImGui::ImageButton (textureID, ImVec2 (size.x ,size.y ),
629
+ ImVec2 (0 , 0 ), ImVec2 (1 , 1 ), framePadding,
630
+ toImColor (tintColor), toImColor (tintColor));
631
+ }
632
+
633
+ // ///////////// Image Button Overloads for sf::RenderTexture
634
+
635
+ bool ImageButton (const sf::RenderTexture& texture, const int framePadding,
636
+ const sf::Color& bgColor, const sf::Color& tintColor) {
637
+ return ImageButton (texture, static_cast <sf::Vector2f>(texture.getSize ()),
638
+ framePadding, bgColor, tintColor);
639
+ }
640
+
641
+ bool ImageButton (const sf::RenderTexture& texture, const sf::Vector2f& size,
642
+ const int framePadding, const sf::Color& bgColor,
643
+ const sf::Color& tintColor) {
644
+ ImTextureID textureID =
645
+ convertGLTextureHandleToImTextureID (texture.getTexture ().getNativeHandle ());
646
+
647
+ return ImGui::ImageButton (textureID, ImVec2 (size.x ,size.y ),
648
+ ImVec2 (0 , 1 ), ImVec2 (1 , 0 ), // flipped vertically, because textures in sf::RenderTexture are stored this way
649
+ framePadding, toImColor (tintColor), toImColor (tintColor));
627
650
}
628
651
652
+ // ///////////// Image Button Overloads for sf::Sprite
653
+
629
654
bool ImageButton (const sf::Sprite& sprite, const int framePadding,
630
655
const sf::Color& bgColor, const sf::Color& tintColor) {
631
656
sf::FloatRect spriteSize = sprite.getGlobalBounds ();
@@ -638,12 +663,23 @@ bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size,
638
663
const int framePadding, const sf::Color& bgColor,
639
664
const sf::Color& tintColor) {
640
665
const sf::Texture* texturePtr = sprite.getTexture ();
666
+ // sprite without texture cannot be drawn
641
667
if (!texturePtr) {
642
668
return false ;
643
669
}
644
- return ::imageButtonImpl (
645
- *texturePtr, static_cast <sf::FloatRect>(sprite.getTextureRect ()), size,
646
- framePadding, bgColor, tintColor);
670
+
671
+ const sf::Texture& texture = *texturePtr;
672
+ sf::Vector2f textureSize = static_cast <sf::Vector2f>(texture.getSize ());
673
+ const sf::IntRect& textureRect = sprite.getTextureRect ();
674
+ ImVec2 uv0 (textureRect.left / textureSize.x ,
675
+ textureRect.top / textureSize.y );
676
+ ImVec2 uv1 ((textureRect.left + textureRect.width ) / textureSize.x ,
677
+ (textureRect.top + textureRect.height ) / textureSize.y );
678
+
679
+ ImTextureID textureID =
680
+ convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
681
+ return ImGui::ImageButton (textureID, ImVec2 (size.x ,size.y ), uv0, uv1, framePadding,
682
+ toImColor (bgColor), toImColor (tintColor));
647
683
}
648
684
649
685
// ///////////// Draw_list Overloads
@@ -799,23 +835,6 @@ void RenderDrawLists(ImDrawData* draw_data) {
799
835
#endif
800
836
}
801
837
802
- bool imageButtonImpl (const sf::Texture& texture,
803
- const sf::FloatRect& textureRect, const sf::Vector2f& size,
804
- const int framePadding, const sf::Color& bgColor,
805
- const sf::Color& tintColor) {
806
- sf::Vector2f textureSize = static_cast <sf::Vector2f>(texture.getSize ());
807
-
808
- ImVec2 uv0 (textureRect.left / textureSize.x ,
809
- textureRect.top / textureSize.y );
810
- ImVec2 uv1 ((textureRect.left + textureRect.width ) / textureSize.x ,
811
- (textureRect.top + textureRect.height ) / textureSize.y );
812
-
813
- ImTextureID textureID =
814
- convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
815
- return ImGui::ImageButton (textureID, ImVec2 (size.x ,size.y ), uv0, uv1, framePadding, toImColor (bgColor),
816
- toImColor (tintColor));
817
- }
818
-
819
838
unsigned int getConnectedJoystickId () {
820
839
for (unsigned int i = 0 ; i < (unsigned int )sf::Joystick::Count; ++i) {
821
840
if (sf::Joystick::isConnected (i)) return i;
0 commit comments