slop.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "slop.hpp"
  2. X11* x11;
  3. Mouse* mouse;
  4. Keyboard* keyboard;
  5. Resource* resource;
  6. // Defaults!
  7. SlopOptions::SlopOptions() {
  8. borderSize = 1;
  9. nodecorations = false;
  10. tolerance = 2;
  11. padding = 0;
  12. shader = "textured";
  13. highlight = false;
  14. xdisplay = ":0";
  15. r = 0.5;
  16. g = 0.5;
  17. b = 0.5;
  18. a = 1;
  19. }
  20. SlopSelection::SlopSelection( float x, float y, float w, float h, Window id ) {
  21. this->x = x;
  22. this->y = y;
  23. this->w = w;
  24. this->h = h;
  25. this->id = id;
  26. }
  27. SlopMemory::SlopMemory( SlopOptions* options ) {
  28. running = true;
  29. state = (SlopState*)new SlopStart();
  30. nextState = NULL;
  31. tolerance = options->tolerance;
  32. nodecorations = options->nodecorations;
  33. rectangle = new Rectangle(glm::vec2(0,0), glm::vec2(0,0), options->borderSize, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight);
  34. selectedWindow = x11->root;
  35. state->onEnter( *this );
  36. }
  37. SlopMemory::~SlopMemory() {
  38. delete state;
  39. if ( nextState ) {
  40. delete nextState;
  41. }
  42. delete rectangle;
  43. }
  44. void SlopMemory::update( double dt ) {
  45. state->update( *this, dt );
  46. if ( nextState ) {
  47. state->onExit( *this );
  48. delete state;
  49. state = nextState;
  50. state->onEnter( *this );
  51. nextState = NULL;
  52. }
  53. }
  54. void SlopMemory::setState( SlopState* state ) {
  55. if ( nextState ) {
  56. delete nextState;
  57. }
  58. nextState = state;
  59. }
  60. void SlopMemory::draw( glm::mat4& matrix ) {
  61. state->draw( *this, matrix );
  62. }
  63. SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
  64. bool deleteOptions = false;
  65. if ( !options ) {
  66. deleteOptions = true;
  67. options = new SlopOptions();
  68. }
  69. resource = new Resource();
  70. // Set up x11 temporarily
  71. x11 = new X11(options->xdisplay);
  72. mouse = new Mouse( x11, options->nodecorations );
  73. keyboard = new Keyboard( x11 );
  74. // Set up window with GL context
  75. SlopWindow* window = new SlopWindow();
  76. window->framebuffer->setShader( options->shader );
  77. // Init our little state machine, memory is a tad of a misnomer
  78. SlopMemory memory( options );
  79. // This is where we'll run through all of our stuffs
  80. while( memory.running ) {
  81. mouse->update();
  82. keyboard->update();
  83. // We move our statemachine forward.
  84. memory.update( 1 );
  85. // Then we draw our junk to a framebuffer.
  86. window->framebuffer->bind();
  87. glClearColor (0.0, 0.0, 0.0, 0.0);
  88. glClear (GL_COLOR_BUFFER_BIT);
  89. memory.draw( window->camera );
  90. window->framebuffer->unbind();
  91. // Then we draw the framebuffer to the screen
  92. window->framebuffer->draw();
  93. window->display();
  94. GLenum err = glGetError();
  95. if ( err != GL_NO_ERROR ) {
  96. throw err;
  97. }
  98. if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
  99. memory.running = false;
  100. if ( cancelled ) {
  101. *cancelled = true;
  102. }
  103. } else {
  104. *cancelled = false;
  105. }
  106. }
  107. // Now we should have a selection! We parse everything we know about it here.
  108. glm::vec4 output = memory.rectangle->getRect();
  109. // Lets now clear both front and back buffers before closing.
  110. // hopefully it'll be completely transparent while closing!
  111. glClearColor (0.0, 0.0, 0.0, 0.0);
  112. glClear (GL_COLOR_BUFFER_BIT);
  113. window->display();
  114. glClear (GL_COLOR_BUFFER_BIT);
  115. window->display();
  116. // Then we clean up.
  117. delete window;
  118. delete mouse;
  119. delete x11;
  120. delete resource;
  121. if ( deleteOptions ) {
  122. delete options;
  123. }
  124. // Finally return the data.
  125. return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
  126. }