slider.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * This file is part of Soft Mood (https://notabug.org/alkeon/soft-mood).
  3. * Copyright (c) 2019 Alejandro "alkeon" Castilla
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "slider.h"
  18. slider::slider(SDL_Renderer * src, int pos_x, int pos_y):
  19. _src(src), _value(0), _pos_x(pos_x), _pos_y(pos_y) {
  20. _button_dst.x = _pos_x - (SLIDER_WIDTH / 2);
  21. _button_dst.y = _pos_y - (SLIDER_HEIGHT / 2);
  22. _button_dst.w = SLIDER_WIDTH;
  23. _button_dst.h = SLIDER_HEIGHT;
  24. _button_bounds_dst.w = _button_dst.w;
  25. _button_bounds_dst.h = (_button_dst.h * 5) + 10;
  26. _button_bounds_dst.x = _button_dst.x;
  27. _button_bounds_dst.y = _button_dst.y - (_button_bounds_dst.h - _button_dst.h);
  28. _fillbar_dst.h = _button_bounds_dst.h - (_button_dst.y - _button_bounds_dst.y);
  29. _fillbar_dst.w = _button_dst.w / 4;
  30. _fillbar_dst.x = _button_dst.x + _button_dst.w / 2 - _fillbar_dst.w / 2;
  31. _fillbar_dst.y = _button_dst.y;
  32. draw();
  33. }
  34. void slider::set_image(const string& file){
  35. _has_image = true;
  36. _image_file = file;
  37. draw_image();
  38. }
  39. void slider::test_mouse_follow(SDL_Point* mouse, SDL_Point* mouse_offset){
  40. if(SDL_PointInRect(mouse, &_button_dst)){
  41. _mouse_follow = true;
  42. mouse_offset->y = mouse->y - _button_dst.y;
  43. }
  44. }
  45. void slider::draw_image(){
  46. if(_has_image){
  47. SDL_Rect s1;
  48. s1.x = _button_bounds_dst.x + 5;
  49. s1.y = _button_bounds_dst.y + _button_bounds_dst.h + 20;
  50. SDL_Surface * temp_surface;
  51. temp_surface = IMG_Load(_image_file.c_str());
  52. if(temp_surface != NULL){
  53. SDL_Texture * fillbar = SDL_CreateTextureFromSurface(_src, temp_surface);
  54. if(fillbar == NULL)
  55. cout << "Fillbar couldn't be created" << endl;
  56. SDL_QueryTexture(fillbar, NULL, NULL, &s1.w, &s1.h);
  57. SDL_RenderCopy(_src, fillbar, NULL, &s1);
  58. SDL_DestroyTexture(fillbar);
  59. }else if(_image_file.size() > 0)
  60. cout << "Icon (" << _image_file << ") couldn't be loaded" << endl;
  61. SDL_FreeSurface(temp_surface);
  62. }
  63. }
  64. void slider::draw(){
  65. SDL_Surface * s;
  66. SDL_Texture * texture;
  67. SDL_Texture * bar;
  68. SDL_Texture * bg;
  69. s = SDL_CreateRGBSurface(0, _pos_y + SLIDER_HEIGHT, _pos_x + SLIDER_WIDTH, 32, 0, 0, 0, 255);
  70. if(s == NULL)
  71. cout << "Surface couldn't be created" << endl;
  72. _button_dst.x = _pos_x - (SLIDER_WIDTH / 2);
  73. SDL_FillRect(s, NULL, SDL_MapRGBA(s->format, 0, 0, 0, 255));
  74. bg = SDL_CreateTextureFromSurface(_src, s);
  75. if(bg == NULL)
  76. cout << "Background couldn't be created" << endl;
  77. int min = _button_bounds_dst.y + _button_dst.h / 2;
  78. int max = (_button_bounds_dst.y + _button_bounds_dst.h) - (_button_dst.h / 2);
  79. int b = max - (_button_dst.y + _button_dst.h / 2);
  80. float step = ((float) (max - min)) / 100;
  81. _value = roundf(b / step);
  82. SDL_FillRect(s,NULL, SDL_MapRGBA(s->format, 100, 100, 100, 255));
  83. texture = SDL_CreateTextureFromSurface(_src, s);
  84. if(texture == NULL)
  85. cout << "Texture couldn't be created" << endl;
  86. SDL_FillRect(s,NULL, SDL_MapRGBA(s->format, 238, 18, 18, 255));
  87. bar = SDL_CreateTextureFromSurface(_src, s);
  88. if(bar == NULL)
  89. cout << "Bar couldn't be created" << endl;
  90. SDL_RenderCopy(_src, bg, NULL, &_button_bounds_dst);
  91. SDL_RenderCopy(_src, bar, NULL, &_fillbar_dst);
  92. SDL_RenderCopy(_src, texture, NULL, &_button_dst);
  93. SDL_FreeSurface(s);
  94. SDL_DestroyTexture(bg);
  95. SDL_DestroyTexture(bar);
  96. SDL_DestroyTexture(texture);
  97. draw_image();
  98. }
  99. void slider::update(int pos_y){
  100. if(_mouse_follow){
  101. _button_dst.y = pos_y;
  102. if (_button_dst.y <= _button_bounds_dst.y)
  103. _button_dst.y = _button_bounds_dst.y;
  104. if (_button_dst.y+_button_dst.h >= _button_bounds_dst.y + _button_bounds_dst.h)
  105. _button_dst.y = (_button_bounds_dst.y + _button_bounds_dst.h) - _button_dst.h;
  106. _fillbar_dst.y = _button_dst.y;
  107. _fillbar_dst.h = _button_bounds_dst.h - (_button_dst.y - _button_bounds_dst.y);
  108. }
  109. }