glslop.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "glslop.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. keyboard = new Keyboard( x11 );
  73. // Set up window with GL context
  74. SlopWindow* window = new SlopWindow();
  75. mouse = new Mouse( x11, options->nodecorations, window->window );
  76. if ( options->shader != "textured" ) {
  77. window->framebuffer->setShader( options->shader );
  78. }
  79. // Init our little state machine, memory is a tad of a misnomer
  80. SlopMemory memory( options );
  81. // This is where we'll run through all of our stuffs
  82. while( memory.running ) {
  83. mouse->update();
  84. keyboard->update();
  85. // We move our statemachine forward.
  86. memory.update( 1 );
  87. // Then we draw our junk to a framebuffer.
  88. window->framebuffer->bind();
  89. glClearColor (0.0, 0.0, 0.0, 0.0);
  90. glClear (GL_COLOR_BUFFER_BIT);
  91. memory.draw( window->camera );
  92. window->framebuffer->unbind();
  93. // Then we draw the framebuffer to the screen
  94. window->framebuffer->draw();
  95. window->display();
  96. GLenum err = glGetError();
  97. if ( err != GL_NO_ERROR ) {
  98. throw err;
  99. }
  100. if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
  101. memory.running = false;
  102. if ( cancelled ) {
  103. *cancelled = true;
  104. }
  105. } else {
  106. *cancelled = false;
  107. }
  108. }
  109. // Now we should have a selection! We parse everything we know about it here.
  110. glm::vec4 output = memory.rectangle->getRect();
  111. // Lets now clear both front and back buffers before closing.
  112. // hopefully it'll be completely transparent while closing!
  113. glClearColor (0.0, 0.0, 0.0, 0.0);
  114. glClear (GL_COLOR_BUFFER_BIT);
  115. window->display();
  116. glClear (GL_COLOR_BUFFER_BIT);
  117. window->display();
  118. // Then we clean up.
  119. delete window;
  120. delete mouse;
  121. delete x11;
  122. delete resource;
  123. if ( deleteOptions ) {
  124. delete options;
  125. }
  126. // Finally return the data.
  127. return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
  128. }