GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
image_component.h
Go to the documentation of this file.
1#pragma once
3#include "ga/layout.h"
4#include "ga/math.h"
5#include "ga/texture.h"
6
7namespace ga {
8
9class Image : public ga::Component
10{
11public:
12 std::string textureName = "";
13 ga::Rect bounds2D = { 0, 0, 0, 0 };
17 bool crop = false;
18
19 Image() = default;
20
21 Image( const std::string& texName,
22 const ga::Rect& bounds,
23 const ga::FitMode& fit,
24 const HorzAlign& hAlign = HorzAlign::LEFT,
25 const VertAlign& vAlign = VertAlign::TOP,
26 const bool& bCrop = false )
27 : textureName( texName )
28 , bounds2D( bounds )
29 , fitMode( fit )
30 , horzAlign( hAlign )
31 , vertAlign( vAlign )
32 , crop( bCrop )
33 {
34 }
35
36 void draw()
37 {
38 if ( auto tex = ga::textureCache().get( textureName ) ) {
39 if ( tex->isAllocated() ) {
40 ga::vec2 texDims { tex->getWidth(), tex->getHeight() };
41 auto texScale = ga::calcScaleToFit( texDims, bounds2D.size(), fitMode );
42 auto texSize = texDims * texScale;
43 auto boundsAnchor = bounds2D.position() + anchor( horzAlign, vertAlign ) * bounds2D.size();
44 auto texPos = boundsAnchor - anchor( horzAlign, vertAlign ) * texSize;
45
46 if ( !crop ) {
47 tex->draw( texPos, texSize.x, texSize.y );
48
49 } else {
50 auto cropPtA = glm::max( bounds2D.min(), texPos );
51 auto cropPtB = glm::min( bounds2D.max(), texPos + texSize );
52 auto cropSz = cropPtB - cropPtA;
53 auto subPtA = ( cropPtA - texPos ) / texScale;
54 auto subPtB = ( cropPtB - texPos ) / texScale;
55 auto subSz = subPtB - subPtA;
56 tex->drawSubsection( cropPtA.x, cropPtA.y, cropSz.x, cropSz.y, subPtA.x, subPtA.y, subSz.x, subSz.y );
57 // tex->drawSubsection(ofRectangle(cropPtA, cropPtB), ofRectangle(subPtA, subPtB));
58 }
59 } else {
60 // texture is unallocated
61 // todo: log warning?
62 }
63 } else {
64 // texture not found in cache
65 // todo: log warning?
66 }
67 }
68
70 {
71 auto drawBounds = bounds2D;
72 if ( auto img = ga::textureCache().get( textureName ) ) {
73 if ( img->isAllocated() ) {
74 ga::vec2 texDims { img->getWidth(), img->getHeight() };
75 auto texScale = ga::calcScaleToFit( texDims, bounds2D.size(), fitMode );
76 auto texSize = texDims * texScale;
77 auto boundsAnchor = bounds2D.position() + anchor( horzAlign, vertAlign ) * bounds2D.size();
78 auto texPos = boundsAnchor - anchor( horzAlign, vertAlign ) * texSize;
79 if ( !crop ) {
80 drawBounds = ga::Rect { texPos, texPos + texSize };
81 } else {
82 drawBounds = ga::Rect { glm::max( bounds2D.min(), texPos ),
83 glm::min( bounds2D.max(), texPos + texSize ) };
84 }
85 }
86 }
87 return drawBounds;
88 }
89};
90} // namespace ga
Definition: component.h:11
Definition: image_component.h:10
ga::Rect bounds2D
Definition: image_component.h:13
VertAlign vertAlign
Definition: image_component.h:16
std::string textureName
Definition: image_component.h:12
Image(const std::string &texName, const ga::Rect &bounds, const ga::FitMode &fit, const HorzAlign &hAlign=HorzAlign::LEFT, const VertAlign &vAlign=VertAlign::TOP, const bool &bCrop=false)
Definition: image_component.h:21
void draw()
Definition: image_component.h:36
ga::FitMode fitMode
Definition: image_component.h:14
Image()=default
ga::Rect getDrawBounds()
Definition: image_component.h:69
bool crop
Definition: image_component.h:17
HorzAlign horzAlign
Definition: image_component.h:15
Definition: color.h:9
vec3 calcScaleToFit(const vec3 &sourceSize, const vec3 &containerSize, const FitMode &fitMode)
Definition: layout.h:55
FitMode
Definition: layout.h:45
VertAlign
Definition: layout.h:12
HorzAlign
Definition: layout.h:6
vec2 anchor(HorzAlign h, VertAlign v)
Definition: layout.h:19
A 2D axis-aligned rectangle, with x,y (float) position and w,h (float) size components.
Definition: math.h:64
vec2 position() const
Definition: math.h:93
vec2 max() const
Definition: math.h:97
vec2 size() const
Definition: math.h:94
vec2 min() const
Definition: math.h:96