123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "slopstates.hpp"
  2. SlopMemory::SlopMemory( SlopOptions* options, Rectangle* rect ) {
  3. running = true;
  4. state = (SlopState*)new SlopStart();
  5. nextState = NULL;
  6. tolerance = options->tolerance;
  7. nodecorations = options->nodecorations;
  8. rectangle = rect;
  9. selectedWindow = x11->root;
  10. state->onEnter( *this );
  11. }
  12. SlopMemory::~SlopMemory() {
  13. delete state;
  14. if ( nextState ) {
  15. delete nextState;
  16. }
  17. delete rectangle;
  18. }
  19. void SlopMemory::update( double dt ) {
  20. state->update( *this, dt );
  21. if ( nextState ) {
  22. state->onExit( *this );
  23. delete state;
  24. state = nextState;
  25. state->onEnter( *this );
  26. nextState = NULL;
  27. }
  28. }
  29. void SlopMemory::setState( SlopState* state ) {
  30. if ( nextState ) {
  31. delete nextState;
  32. }
  33. nextState = state;
  34. }
  35. void SlopMemory::draw( glm::mat4& matrix ) {
  36. state->draw( *this, matrix );
  37. }
  38. SlopState::~SlopState() {
  39. }
  40. void SlopState::onEnter( SlopMemory& memory ) {
  41. }
  42. void SlopState::onExit( SlopMemory& memory ) {
  43. }
  44. void SlopState::update( SlopMemory& memory, double dt ) {
  45. }
  46. void SlopState::draw( SlopMemory& memory, glm::mat4 matrix ) {
  47. }
  48. void SlopStart::onEnter( SlopMemory& memory ) {
  49. setStartPos = false;
  50. }
  51. void SlopStart::update( SlopMemory& memory, double dt ) {
  52. if ( mouse->getButton( 1 ) && !setStartPos ) {
  53. startPos = mouse->getMousePos();
  54. setStartPos = true;
  55. }
  56. if ( setStartPos && glm::distance( startPos, mouse->getMousePos() ) >= memory.tolerance ) {
  57. memory.setState( (SlopState*)new SlopStartDrag( startPos ) );
  58. }
  59. if ( mouse->hoverWindow != None ) {
  60. glm::vec4 rect = getWindowGeometry( mouse->hoverWindow, memory.nodecorations );
  61. memory.rectangle->setPoints( glm::vec2( (float)rect.x, (float)rect.y ), glm::vec2( (float)rect.x+rect.z, (float)rect.y+rect.w ) );
  62. }
  63. if ( setStartPos && !mouse->getButton( 1 ) ) {
  64. memory.selectedWindow = mouse->hoverWindow;
  65. memory.setState( (SlopState*)new SlopEndDrag() );
  66. }
  67. }
  68. void SlopStart::draw( SlopMemory& memory, glm::mat4 matrix ) {
  69. if ( memory.tolerance > 0 ) {
  70. memory.rectangle->draw( matrix );
  71. }
  72. }
  73. SlopStartDrag::SlopStartDrag( glm::vec2 point ) {
  74. startPoint = point;
  75. }
  76. void SlopStartDrag::onEnter( SlopMemory& memory ) {
  77. memory.rectangle->setPoints(startPoint, startPoint);
  78. }
  79. void SlopStartDrag::update( SlopMemory& memory, double dt ) {
  80. char a = startPoint.y > mouse->getMousePos().y;
  81. char b = startPoint.x > mouse->getMousePos().x;
  82. char c = (a << 1) | b;
  83. switch ( c ) {
  84. case 0: mouse->setCursor( XC_lr_angle );
  85. memory.rectangle->setPoints(startPoint+glm::vec2(0,0), mouse->getMousePos()+glm::vec2(1,1));
  86. break;
  87. case 1: mouse->setCursor( XC_ll_angle );
  88. memory.rectangle->setPoints(startPoint+glm::vec2(0,0), mouse->getMousePos()+glm::vec2(1,1));
  89. break;
  90. case 2: mouse->setCursor( XC_ur_angle );
  91. memory.rectangle->setPoints(startPoint+glm::vec2(0,1), mouse->getMousePos()+glm::vec2(1,0));
  92. break;
  93. case 3: mouse->setCursor( XC_ul_angle );
  94. memory.rectangle->setPoints(startPoint+glm::vec2(1,1), mouse->getMousePos()+glm::vec2(0,0));
  95. break;
  96. }
  97. if ( !mouse->getButton( 1 ) ) {
  98. memory.setState( (SlopState*)new SlopEndDrag() );
  99. }
  100. }
  101. void SlopStartDrag::draw( SlopMemory& memory, glm::mat4 matrix ) {
  102. memory.rectangle->draw( matrix );
  103. }
  104. void SlopEndDrag::onEnter( SlopMemory& memory ) {
  105. memory.running = false;
  106. }