slop.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "slop.hpp"
  2. Wayland* wayland;
  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. wayland = new Wayland();
  64. wayland->init();
  65. mouse = new Mouse( wayland );
  66. keyboard = new Keyboard( wayland );
  67. // Set up window with GL context
  68. Window* window = new Window( 1, 1 );
  69. window->setTitle("slop");
  70. window->setClass("slop");
  71. window->setFullScreen();
  72. window->setCurrent();
  73. window->framebuffer->setShader( options->shader );
  74. // Init our little state machine, memory is a tad of a misnomer
  75. SlopMemory memory( options );
  76. // This is where we'll run through all of our stuffs
  77. //FIXME: We need to sync up with wayland's draw pings so that we don't overdraw ever...
  78. while( memory.running ) {
  79. // This is specifically for wayland updates.
  80. wl_display_dispatch_pending(wayland->display);
  81. memory.update( 1 );
  82. // Then we draw our junk to a framebuffer.
  83. window->framebuffer->bind();
  84. glClearColor (0.0, 0.0, 0.0, 0.0);
  85. glClear (GL_COLOR_BUFFER_BIT);
  86. memory.draw( window->camera );
  87. window->framebuffer->unbind();
  88. // Then we draw the framebuffer to the screen
  89. window->framebuffer->draw();
  90. window->display();
  91. GLenum err = glGetError();
  92. if ( err != GL_NO_ERROR ) {
  93. throw err;
  94. }
  95. if ( keyboard->anyKeyDown() || mouse->getButton( BTN_RIGHT ) ) {
  96. memory.running = false;
  97. if ( cancelled ) {
  98. *cancelled = true;
  99. }
  100. } else {
  101. *cancelled = false;
  102. }
  103. }
  104. // Now we should have a selection! We parse everything we know about it here.
  105. glm::vec4 output = memory.rectangle->getRect();
  106. // Lets now clear both front and back buffers before closing.
  107. // hopefully it'll be completely transparent while closing!
  108. glClearColor (0.0, 0.0, 0.0, 0.0);
  109. glClear (GL_COLOR_BUFFER_BIT);
  110. window->display();
  111. glClearColor (0.0, 0.0, 0.0, 0.0);
  112. glClear (GL_COLOR_BUFFER_BIT);
  113. window->display();
  114. // Then we clean up.
  115. delete window;
  116. delete mouse;
  117. delete wayland;
  118. delete resource;
  119. if ( deleteOptions ) {
  120. delete options;
  121. }
  122. // Finally return the data.
  123. return SlopSelection( output.x, output.y, output.z+1, output.w+1 );
  124. }