slopstates.cpp 4.5KB

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