summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/bgfx/slider.cpp
blob: 0cc97275497b7b1fe489745cb5fe954a7b80c5bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
//============================================================
//
//  slider.cpp - BGFX shader parameter slider
//
//============================================================

#include "emu.h"

#include "slider.h"
#include "../frontend/mame/ui/slider.h"

bgfx_slider::bgfx_slider(running_machine &machine, std::string name, float min, float def, float max, float step, slider_type type, screen_type screen, std::string format, std::string description, std::vector<std::string>& strings)
	: m_name(name)
	, m_step(step)
	, m_type(type)
	, m_screen_type(screen)
	, m_format(format)
	, m_description(description)
	, m_machine(machine)
{
	m_min = min;
	m_default = def;
	m_max = max;
	m_value = def;

	for (std::string string : strings)
	{
		m_strings.push_back(string);
	}

	m_slider_state = create_core_slider();
}

bgfx_slider::~bgfx_slider()
{
}

int32_t bgfx_slider::slider_changed(running_machine& /*machine*/, void *arg, int /*id*/, std::string *str, int32_t newval)
{
	if (arg != nullptr)
	{
		return reinterpret_cast<bgfx_slider*>(arg)->update(str, newval);
	}
	return 0;
}

void bgfx_slider::import(float val)
{
	m_value = val;
	slider_changed(m_machine, this, m_slider_state->id, nullptr, int32_t(floor(m_value / m_step + 0.5f)));
}

std::unique_ptr<slider_state> bgfx_slider::create_core_slider()
{
	auto state = make_unique_clear<slider_state>();

	state->minval = int32_t(floor(m_min / m_step + 0.5f));
	state->defval = int32_t(floor(m_default / m_step + 0.5f));
	state->maxval = int32_t(floor(m_max / m_step + 0.5f));
	state->incval = int32_t(floor(m_step / m_step + 0.5f));

	using namespace std::placeholders;
	state->update = std::bind(&bgfx_slider::slider_changed, this, _1, _2, _3, _4, _5);

	state->arg = this;
	state->id = 0;
	state->description = m_description;

	return state;
}

int32_t bgfx_slider::update(std::string *str, int32_t newval)
{
	switch (m_type)
	{
		case SLIDER_INT_ENUM:
		{
			if (newval != SLIDER_NOCHANGE)
			{
				m_value = float(newval);
			}
			if (str != nullptr)
			{
				*str = string_format(m_format, m_strings[as_int()]);
			}
			return as_int();
		}

		case SLIDER_INT:
		{
			if (newval != SLIDER_NOCHANGE)
			{
				m_value = float(newval);
			}
			if (str != nullptr)
			{
				*str = string_format(m_format, as_int());
			}
			return as_int();
		}

		default:
		{
			float *val_ptr = reinterpret_cast<float *>(&m_value);
			if (newval != SLIDER_NOCHANGE)
			{
				*val_ptr = float(newval) * m_step;
			}
			if (str != nullptr)
			{
				*str = string_format(m_format, *val_ptr);
			}
			return int32_t(floor(*val_ptr / m_step + 0.5f));
		}
	}
	return 0;
}

size_t bgfx_slider::get_size_for_type(slider_type type)
{
	switch(type)
	{
		case SLIDER_INT_ENUM:
		case SLIDER_FLOAT:
		case SLIDER_INT:
			return sizeof(float);
		case SLIDER_COLOR:
			return sizeof(float) * 3;
		case SLIDER_VEC2:
			return sizeof(float) * 2;
		default:
			return 0;
	}
}