PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/of/of_v0.9.3_vs_release/examples/graphics/rectangleAlignmentAndScaling/src/ofApp.cpp

https://gitlab.com/cocoroac/walkingartists
C++ | 393 lines | 292 code | 50 blank | 51 comment | 115 complexity | 83cad61daf1b527022e3a1523dfa51f3 MD5 | raw file
  1. #include "ofApp.h"
  2. //--------------------------------------------------------------
  3. void ofApp::setup(){
  4. // This example is designed to show all of the different aligning
  5. // and scaling functions available in ofRectangle.
  6. //
  7. // Please run the example and use the keyboard to see the visual
  8. // results of each of the alignment and scaling operations.
  9. //
  10. // The basic premise is to define a subject and target rectangle.
  11. // The "subject" (on the left) rectangle will be aligned-to
  12. // and scaled-to the "target" (on the right) rectangle based
  13. // on the user's input.
  14. //
  15. // The resulting rectangle (workingSubjectRect in this case),
  16. // displayed in the center, is the result of the operations.
  17. //
  18. // An image is used to further emphasize the effect of various
  19. // aspect-ratio preservation modes.
  20. ofSetFrameRate(30);
  21. ofEnableAlphaBlending();
  22. isScaling = false;
  23. isAligning = true;
  24. isRectScaling = false;
  25. scaleMode = OF_SCALEMODE_FIT;
  26. aspectRatioMode = OF_ASPECT_RATIO_KEEP;
  27. target_hAlign = OF_ALIGN_HORZ_CENTER;
  28. target_vAlign = OF_ALIGN_VERT_CENTER;
  29. subject_hAlign = OF_ALIGN_HORZ_CENTER;
  30. subject_vAlign = OF_ALIGN_VERT_CENTER;
  31. bUseImage = true;
  32. img.load("resolution_test_1080_mini.png");
  33. targetColor = ofColor(255,0,0);
  34. subjectColor = ofColor(255,255,0);
  35. makeNewTarget();
  36. makeNewSubject();
  37. }
  38. //--------------------------------------------------------------
  39. void ofApp::update(){
  40. workingSubjectRect = subjectRect;
  41. // read the results of our keyboard input to determine
  42. // the correct scaling and / or alignment operation.
  43. if(!isRectScaling) {
  44. if(isScaling) {
  45. if(isAligning) {
  46. workingSubjectRect.scaleTo(targetRect,
  47. aspectRatioMode,
  48. target_hAlign,
  49. target_vAlign,
  50. subject_hAlign,
  51. subject_vAlign);
  52. } else {
  53. workingSubjectRect.scaleTo(targetRect,
  54. aspectRatioMode);
  55. }
  56. } else {
  57. if(isAligning) {
  58. workingSubjectRect.alignTo(targetRect,
  59. target_hAlign,
  60. target_vAlign,
  61. subject_hAlign,
  62. subject_vAlign);
  63. } else {
  64. workingSubjectRect.alignTo(targetRect);
  65. }
  66. }
  67. } else {
  68. workingSubjectRect.scaleTo(targetRect,
  69. scaleMode);
  70. }
  71. }
  72. //--------------------------------------------------------------
  73. void ofApp::draw(){
  74. ofBackground(0);
  75. // draw original subject in lower left-hand corner
  76. if(!bUseImage) {
  77. ofFill(); ofSetColor(subjectColor, 80);
  78. ofDrawRectangle(10, ofGetHeight() - subjectRect.height - 10, subjectRect.width, subjectRect.height);
  79. } else {
  80. ofFill(); ofSetColor(255);
  81. img.draw(10, ofGetHeight() - subjectRect.height - 10, subjectRect.width, subjectRect.height);
  82. }
  83. // draw original subject frame in lower left-hand corner
  84. ofNoFill(); ofSetColor(subjectColor, 120);
  85. ofDrawRectangle(10, ofGetHeight() - subjectRect.height - 10, subjectRect.width, subjectRect.height);
  86. // draw original subject label
  87. ofSetColor(255);
  88. ofDrawBitmapStringHighlight("SUBJECT", 16, ofGetHeight() - 20);
  89. // draw original target in lower right-hand corner
  90. ofFill(); ofSetColor(targetColor, 80);
  91. ofDrawRectangle(ofGetWidth() - targetRect.width - 10, ofGetHeight() - targetRect.height - 10, targetRect.width, targetRect.height);
  92. // draw original target frame in lower right-hand corner
  93. ofNoFill(); ofSetColor(targetColor, 120);
  94. ofDrawRectangle(ofGetWidth() - targetRect.width - 10, ofGetHeight() - targetRect.height - 10, targetRect.width, targetRect.height);
  95. ofSetColor(255);
  96. ofDrawBitmapStringHighlight("TARGET", ofGetWidth() - 65, ofGetHeight() - 20);
  97. // draw target rectangle in center
  98. drawAlignRect(targetRect, targetColor, target_hAlign, target_vAlign, false);
  99. // draw aligned / scaled subject with respect to the target
  100. drawAlignRect(workingSubjectRect, subjectColor, subject_hAlign, subject_vAlign, bUseImage);
  101. // make the menu
  102. stringstream ss;
  103. ss << "Keys:" << endl;
  104. ss << "----------------------------------------------------------" << endl;
  105. ss << " New Subject / Target (space) " << endl;
  106. ss << " Use An Image Subject (i) = " << (bUseImage ? "YES" : "NO") << endl;
  107. ss << "----------------------------------------------------------" << endl;
  108. ss << " Enable Custom Align (A) = " << (isAligning && !isRectScaling ? "YES" : "NO") << endl;
  109. ss << " Subject ofAlignHorz (h) = " << (isAligning && !isRectScaling ? getHorzAlignString(subject_hAlign) : "-") << endl;
  110. ss << " Model ofAlignHorz (H) = " << (isAligning && !isRectScaling ? getHorzAlignString(target_hAlign) : "-") << endl;
  111. ss << " Subject ofAlignVert (v) = " << (isAligning && !isRectScaling ? getVertAlignString(subject_vAlign) : "-") << endl;
  112. ss << " Model ofAlignVert (V) = " << (isAligning && !isRectScaling ? getVertAlignString(target_vAlign) : "-") << endl;
  113. ss << "----------------------------------------------------------" << endl;
  114. ss << "Enable Custom Scaling (S) = " << (isScaling && !isRectScaling ? "YES" : "NO") << endl;
  115. ss << " ofAspectRatioMode (a) = " << (isScaling && !isRectScaling ? getAspectRatioModeString(aspectRatioMode) : "-") << endl;
  116. ss << "----------------------------------------------------------" << endl;
  117. ss << " Override Scale/Align (r) = " << (isRectScaling ? "YES" : "NO") << endl;
  118. ss << " ofScaleMode (s) = " << (isRectScaling ? getScaleModeString(scaleMode) : "-") << endl;
  119. // draw the menu
  120. ofSetColor(255);
  121. ofDrawBitmapString(ss.str(), 10, 14);
  122. }
  123. //--------------------------------------------------------------
  124. void ofApp::keyPressed(int key){
  125. // define keyboard interactions
  126. if(key == ' ') {
  127. makeNewTarget();
  128. makeNewSubject();
  129. } else if(key == 'h' && !isRectScaling && isAligning) {
  130. subject_hAlign = getNextHorzAlign(subject_hAlign);
  131. } else if(key == 'H' && !isRectScaling && isAligning) {
  132. target_hAlign = getNextHorzAlign(target_hAlign);
  133. } else if(key == 'v' && !isRectScaling && isAligning) {
  134. subject_vAlign = getNextVertAlign(subject_vAlign);
  135. } else if(key == 'V' && !isRectScaling && isAligning) {
  136. target_vAlign = getNextVertAlign(target_vAlign);
  137. } else if(key == 'a' && !isRectScaling && isScaling) {
  138. aspectRatioMode = getNextAspectRatioMode(aspectRatioMode);
  139. } else if(key == 's' && isRectScaling) {
  140. scaleMode = getNextScaleMode(scaleMode);
  141. } else if(key == 'S') {
  142. if(isRectScaling) {
  143. isScaling = true;
  144. isRectScaling = false;
  145. } else {
  146. isScaling = !isScaling;
  147. }
  148. } else if(key == 'r') {
  149. isRectScaling = !isRectScaling;
  150. if(isRectScaling) {
  151. isScaling = false;
  152. isAligning = false;
  153. }
  154. } else if(key == 'A') {
  155. if(isRectScaling) {
  156. isRectScaling = false;
  157. isAligning = true;
  158. } else {
  159. isAligning = !isAligning;
  160. }
  161. } else if(key == 'i') {
  162. bUseImage = !bUseImage;
  163. if(bUseImage) {
  164. makeNewSubject();
  165. }
  166. }
  167. }
  168. //--------------------------------------------------------------
  169. void ofApp::makeNewSubject() {
  170. // if we are not using an image, make a random subject rectangle
  171. if(!bUseImage) {
  172. subjectRect.setFromCenter(ofGetWidth() / 2.0f,
  173. ofGetHeight() / 2.0f,
  174. ofRandom(30.0f,300.0f),
  175. ofRandom(30.0f,300.0f));
  176. } else {
  177. // if we are using the image, then match the image size
  178. subjectRect.setFromCenter(ofGetWidth() / 2.0f,
  179. ofGetHeight() / 2.0f,
  180. img.getWidth(),
  181. img.getHeight());
  182. }
  183. // copy the subject to the working subject so it can be modified.
  184. workingSubjectRect = subjectRect;
  185. }
  186. //--------------------------------------------------------------
  187. void ofApp::makeNewTarget() {
  188. // create a random target rectangle aligned to the center of the screen
  189. targetRect.setFromCenter(ofGetWidth() / 2.0f,
  190. ofGetHeight() / 2.0f,
  191. ofRandom(30.0f,300.0f),
  192. ofRandom(30.0f,300.0f));
  193. }
  194. //--------------------------------------------------------------
  195. void ofApp::drawAlignRect(const ofRectangle& rect,
  196. const ofColor& color,
  197. ofAlignHorz hAlign,
  198. ofAlignVert vAlign,
  199. bool drawImage) {
  200. // draw the rect -- draw the image if using an image
  201. ofFill();
  202. if(drawImage) {
  203. ofSetColor(255,127);
  204. img.draw(rect);
  205. } else {
  206. ofSetColor(color, 80);
  207. ofDrawRectangle(rect);
  208. }
  209. ofNoFill();
  210. ofSetColor(color, 120);
  211. ofDrawRectangle(rect);
  212. // draw the alignment marks if applicable
  213. if(isAligning && !isRectScaling) {
  214. drawHorzAlignMark(rect, color, hAlign);
  215. drawVertAlignMark(rect, color, vAlign);
  216. }
  217. }
  218. //--------------------------------------------------------------
  219. void ofApp::drawHorzAlignMark(const ofRectangle& rect, const ofColor& color, ofAlignHorz hAlign) {
  220. if(hAlign != OF_ALIGN_HORZ_IGNORE) {
  221. float hAnchor = rect.getHorzAnchor(hAlign);
  222. ofSetColor(color,120);
  223. ofDrawLine(hAnchor, rect.getTop() - 13, hAnchor, rect.getTop() - 3);
  224. ofDrawLine(hAnchor, rect.getBottom() + 13, hAnchor, rect.getBottom() + 3);
  225. }
  226. }
  227. //--------------------------------------------------------------
  228. void ofApp::drawVertAlignMark(const ofRectangle& rect, const ofColor& color, ofAlignVert vAlign) {
  229. if(vAlign != OF_ALIGN_VERT_IGNORE) {
  230. float vAnchor = rect.getVertAnchor(vAlign);
  231. ofSetColor(color,120);
  232. ofDrawLine(rect.getLeft() - 13, vAnchor, rect.getLeft() - 3, vAnchor);
  233. ofDrawLine(rect.getRight() + 13, vAnchor, rect.getRight() + 3, vAnchor);
  234. }
  235. }
  236. //--------------------------------------------------------------
  237. ofScaleMode ofApp::getNextScaleMode(ofScaleMode mode) {
  238. if(mode == OF_SCALEMODE_FIT) {
  239. mode = OF_SCALEMODE_FILL;
  240. } else if(mode == OF_SCALEMODE_FILL) {
  241. mode = OF_SCALEMODE_CENTER;
  242. } else if(mode == OF_SCALEMODE_CENTER) {
  243. mode = OF_SCALEMODE_STRETCH_TO_FILL;
  244. } else if(mode == OF_SCALEMODE_STRETCH_TO_FILL) {
  245. mode = OF_SCALEMODE_FIT;
  246. }
  247. return mode;
  248. }
  249. //--------------------------------------------------------------
  250. ofAspectRatioMode ofApp::getNextAspectRatioMode(ofAspectRatioMode mode) {
  251. if(mode == OF_ASPECT_RATIO_IGNORE) {
  252. mode = OF_ASPECT_RATIO_KEEP;
  253. } else if(mode == OF_ASPECT_RATIO_KEEP) {
  254. mode = OF_ASPECT_RATIO_KEEP_BY_EXPANDING;
  255. } else if(mode == OF_ASPECT_RATIO_KEEP_BY_EXPANDING) {
  256. mode = OF_ASPECT_RATIO_IGNORE;
  257. }
  258. return mode;
  259. }
  260. //--------------------------------------------------------------
  261. ofAlignHorz ofApp::getNextHorzAlign(ofAlignHorz hAlign) {
  262. if(hAlign == OF_ALIGN_HORZ_LEFT) {
  263. hAlign = OF_ALIGN_HORZ_CENTER;
  264. } else if(hAlign == OF_ALIGN_HORZ_CENTER) {
  265. hAlign = OF_ALIGN_HORZ_RIGHT;
  266. } else if(hAlign == OF_ALIGN_HORZ_RIGHT) {
  267. hAlign = OF_ALIGN_HORZ_LEFT;
  268. }
  269. return hAlign;
  270. }
  271. //--------------------------------------------------------------
  272. ofAlignVert ofApp::getNextVertAlign(ofAlignVert vAlign) {
  273. if(vAlign == OF_ALIGN_VERT_TOP) {
  274. vAlign = OF_ALIGN_VERT_CENTER;
  275. } else if(vAlign == OF_ALIGN_VERT_CENTER) {
  276. vAlign = OF_ALIGN_VERT_BOTTOM;
  277. } else if(vAlign == OF_ALIGN_VERT_BOTTOM) {
  278. vAlign = OF_ALIGN_VERT_TOP;
  279. }
  280. return vAlign;
  281. }
  282. //--------------------------------------------------------------
  283. string ofApp::getHorzAlignString(ofAlignHorz hAlign) {
  284. switch (hAlign) {
  285. case OF_ALIGN_HORZ_LEFT:
  286. return "OF_ALIGN_HORZ_LEFT";
  287. case OF_ALIGN_HORZ_CENTER:
  288. return "OF_ALIGN_HORZ_CENTER";
  289. case OF_ALIGN_HORZ_RIGHT:
  290. return "OF_ALIGN_HORZ_RIGHT";
  291. case OF_ALIGN_HORZ_IGNORE:
  292. return "OF_ALIGN_HORZ_IGNORE";
  293. default:
  294. ofLogError() << "Unknown ofAlignHorz: " << hAlign;
  295. return "";
  296. }
  297. }
  298. //--------------------------------------------------------------
  299. string ofApp::getVertAlignString(ofAlignVert vAlign) {
  300. switch (vAlign) {
  301. case OF_ALIGN_VERT_TOP:
  302. return "OF_ALIGN_VERT_TOP";
  303. case OF_ALIGN_VERT_CENTER:
  304. return "OF_ALIGN_VERT_CENTER";
  305. case OF_ALIGN_VERT_BOTTOM:
  306. return "OF_ALIGN_VERT_BOTTOM";
  307. case OF_ALIGN_VERT_IGNORE:
  308. return "OF_ALIGN_VERT_IGNORE";
  309. default:
  310. ofLogError() << "Unknown ofAlignVert: " << vAlign;
  311. return "";
  312. }
  313. }
  314. //--------------------------------------------------------------
  315. string ofApp::getAspectRatioModeString(ofAspectRatioMode mode) {
  316. switch (mode) {
  317. case OF_ASPECT_RATIO_IGNORE:
  318. return "OF_ASPECT_RATIO_IGNORE";
  319. case OF_ASPECT_RATIO_KEEP:
  320. return "OF_ASPECT_RATIO_KEEP";
  321. case OF_ASPECT_RATIO_KEEP_BY_EXPANDING:
  322. return "OF_ASPECT_RATIO_KEEP_BY_EXPANDING";
  323. default:
  324. ofLogError() << "Unknown ofAspectRatioMode: " << mode;
  325. return "";
  326. }
  327. }
  328. //--------------------------------------------------------------
  329. string ofApp::getScaleModeString(ofScaleMode mode) {
  330. switch (mode) {
  331. case OF_SCALEMODE_FIT:
  332. return "OF_SCALEMODE_FIT";
  333. case OF_SCALEMODE_FILL:
  334. return "OF_SCALEMODE_FILL";
  335. case OF_SCALEMODE_CENTER:
  336. return "OF_SCALEMODE_CENTER";
  337. case OF_SCALEMODE_STRETCH_TO_FILL:
  338. return "OF_SCALEMODE_STRETCH_TO_FILL";
  339. default:
  340. ofLogError() << "Unknown ofScaleMode: " << mode;
  341. return "";
  342. }
  343. }