slop.cpp 3.5KB

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