summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/bgfx/slider.cpp
blob: 4fac9bd48496194ca4da3d231531398a26d16dba (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
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
//============================================================
//
//  slider.cpp - BGFX shader parameter slider
//
//============================================================

#include "emu.h"

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

bgfx_slider::bgfx_slider(running_machine &machine, std::string name, int32_t min, int32_t def, int32_t max, int32_t step, slider_type type, screen_type screen, float scale, 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_scale(scale)
	, m_format(format)
	, m_description(description)
{
	m_min = min;
	m_default = def;
    m_max = max;

    m_value = def;
    if (m_type != slider_type::SLIDER_INT && m_type != slider_type::SLIDER_INT_ENUM)
    {
        float temp = float(def) * scale;
        m_value = *reinterpret_cast<int32_t*>(&temp);
    }

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

    m_slider_state = create_core_slider(machine);
}

bgfx_slider::~bgfx_slider()
{
}

static INT32 update_trampoline(running_machine &machine, void *arg, std::string *str, INT32 newval) {
    if (arg != nullptr) {
        return reinterpret_cast<bgfx_slider*>(arg)->update(str, newval);
    }
    return 0;
}

slider_state* bgfx_slider::create_core_slider(running_machine& machine)
{
    int size = sizeof(slider_state) + m_description.length();
    slider_state *state = reinterpret_cast<slider_state *>(auto_alloc_array_clear(machine, UINT8, size));

    state->minval = m_min;
    state->defval = m_default;
    state->maxval = m_max;
    state->incval = m_step;
    state->update = update_trampoline;
    state->arg = this;
    state->hidden = false;
    state->id = 0; // fixme
    strcpy(state->description, m_description.c_str());
    
    return state;
}

int32_t bgfx_slider::update(std::string *str, int32_t newval)
{
    switch (m_type) {
        case SLIDER_INT_ENUM:
        {
            INT32 *val_ptr = reinterpret_cast<INT32 *>(&m_value);
            if (newval != SLIDER_NOCHANGE) {
                *val_ptr = newval;
            }
            if (str != nullptr) {
                *str = string_format(m_format, m_strings[*val_ptr]);
            }
            return *val_ptr;
        }

        case SLIDER_INT:
        {
            int *val_ptr = reinterpret_cast<int *>(&m_value);
            if (newval != SLIDER_NOCHANGE) {
                *val_ptr = newval;
            }
            if (str != nullptr) {
                *str = string_format(m_format, *val_ptr);
            }
            return *val_ptr;
        }

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