スライダー
https://scrapbox.io/files/64dcd4e62e92e8001b5cc090.png
code:スライダー.cpp
# include <Siv3D.hpp>
class Slider {
public:
double value = 1.0;
RectF rect;
bool activeFlg = false;
//double 初期値(0~1),RectF 配置する四角形
Slider(double value, const RectF& rect) :value{ value }, rect{ rect } {}
//返り値はスライダーの値
double update() {
if (rect.stretched(15).mouseOver())Cursor::RequestStyle(CursorStyle::Hand);
if (rect.stretched(15).leftClicked())activeFlg = true;
if (activeFlg) {
Cursor::RequestStyle(CursorStyle::Hand);
value = Clamp(Cursor::PosF().x - rect.x, 0.0, rect.w) / rect.w;
if (MouseL.up())activeFlg = false;
}
return value;
}
void draw(const ColorF& color = { 0.3, 0.5, 1.0 })const {
rect.draw(Palette::Gray);
RectF{ rect.pos,value * rect.w,rect.h }.draw(color);
RoundRect(Arg::center(value * rect.w + rect.x, rect.center().y), 20, 50, 10).draw();
}
//0~1を返す
double getValue() {
return value;
}
//操作中ならtrue
bool isActive() {
return activeFlg;
}
};
void Main()
{
Slider slider{ 0.5,RectF{Arg::center = Scene::Center(),400,10} };
while (System::Update())
{
slider.update();
slider.draw();
}
}