123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * This file is part of Soft Mood (https://notabug.org/alkeon/soft-mood).
- * Copyright (c) 2019 Alejandro "alkeon" Castilla
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, version 3.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "slider.h"
- slider::slider(SDL_Renderer * src, int pos_x, int pos_y):
- _src(src), _value(0), _pos_x(pos_x), _pos_y(pos_y) {
-
- _button_dst.x = _pos_x - (SLIDER_WIDTH / 2);
- _button_dst.y = _pos_y - (SLIDER_HEIGHT / 2);
- _button_dst.w = SLIDER_WIDTH;
- _button_dst.h = SLIDER_HEIGHT;
- _button_bounds_dst.w = _button_dst.w;
- _button_bounds_dst.h = (_button_dst.h * 5) + 10;
- _button_bounds_dst.x = _button_dst.x;
- _button_bounds_dst.y = _button_dst.y - (_button_bounds_dst.h - _button_dst.h);
- _fillbar_dst.h = _button_bounds_dst.h - (_button_dst.y - _button_bounds_dst.y);
- _fillbar_dst.w = _button_dst.w / 4;
- _fillbar_dst.x = _button_dst.x + _button_dst.w / 2 - _fillbar_dst.w / 2;
- _fillbar_dst.y = _button_dst.y;
- draw();
- }
- void slider::set_image(const string& file){
- _has_image = true;
- _image_file = file;
- draw_image();
- }
- void slider::test_mouse_follow(SDL_Point* mouse, SDL_Point* mouse_offset){
- if(SDL_PointInRect(mouse, &_button_dst)){
- _mouse_follow = true;
- mouse_offset->y = mouse->y - _button_dst.y;
- }
- }
- void slider::draw_image(){
- if(_has_image){
- SDL_Rect s1;
- s1.x = _button_bounds_dst.x + 5;
- s1.y = _button_bounds_dst.y + _button_bounds_dst.h + 20;
-
- SDL_Surface * temp_surface;
- temp_surface = IMG_Load(_image_file.c_str());
- if(temp_surface != NULL){
-
- SDL_Texture * fillbar = SDL_CreateTextureFromSurface(_src, temp_surface);
- if(fillbar == NULL)
- cout << "Fillbar couldn't be created" << endl;
- SDL_QueryTexture(fillbar, NULL, NULL, &s1.w, &s1.h);
- SDL_RenderCopy(_src, fillbar, NULL, &s1);
- SDL_DestroyTexture(fillbar);
- }else if(_image_file.size() > 0)
- cout << "Icon (" << _image_file << ") couldn't be loaded" << endl;
- SDL_FreeSurface(temp_surface);
- }
- }
- void slider::draw(){
- SDL_Surface * s;
- SDL_Texture * texture;
- SDL_Texture * bar;
- SDL_Texture * bg;
- s = SDL_CreateRGBSurface(0, _pos_y + SLIDER_HEIGHT, _pos_x + SLIDER_WIDTH, 32, 0, 0, 0, 255);
- if(s == NULL)
- cout << "Surface couldn't be created" << endl;
- _button_dst.x = _pos_x - (SLIDER_WIDTH / 2);
- SDL_FillRect(s, NULL, SDL_MapRGBA(s->format, 0, 0, 0, 255));
- bg = SDL_CreateTextureFromSurface(_src, s);
- if(bg == NULL)
- cout << "Background couldn't be created" << endl;
- int min = _button_bounds_dst.y + _button_dst.h / 2;
- int max = (_button_bounds_dst.y + _button_bounds_dst.h) - (_button_dst.h / 2);
- int b = max - (_button_dst.y + _button_dst.h / 2);
- float step = ((float) (max - min)) / 100;
- _value = roundf(b / step);
- SDL_FillRect(s,NULL, SDL_MapRGBA(s->format, 100, 100, 100, 255));
- texture = SDL_CreateTextureFromSurface(_src, s);
- if(texture == NULL)
- cout << "Texture couldn't be created" << endl;
- SDL_FillRect(s,NULL, SDL_MapRGBA(s->format, 238, 18, 18, 255));
- bar = SDL_CreateTextureFromSurface(_src, s);
- if(bar == NULL)
- cout << "Bar couldn't be created" << endl;
- SDL_RenderCopy(_src, bg, NULL, &_button_bounds_dst);
- SDL_RenderCopy(_src, bar, NULL, &_fillbar_dst);
- SDL_RenderCopy(_src, texture, NULL, &_button_dst);
- SDL_FreeSurface(s);
- SDL_DestroyTexture(bg);
- SDL_DestroyTexture(bar);
- SDL_DestroyTexture(texture);
- draw_image();
- }
- void slider::update(int pos_y){
- if(_mouse_follow){
- _button_dst.y = pos_y;
-
- if (_button_dst.y <= _button_bounds_dst.y)
- _button_dst.y = _button_bounds_dst.y;
- if (_button_dst.y+_button_dst.h >= _button_bounds_dst.y + _button_bounds_dst.h)
- _button_dst.y = (_button_bounds_dst.y + _button_bounds_dst.h) - _button_dst.h;
-
- _fillbar_dst.y = _button_dst.y;
- _fillbar_dst.h = _button_bounds_dst.h - (_button_dst.y - _button_bounds_dst.y);
-
- }
- }
|