summaryrefslogtreecommitdiff
path: root/indra/llui/llluafloater.cpp
blob: 36dd0d58bd702bddc9cc49439aa983e66579d602 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/** 
 * @file llluafloater.cpp
 *
 * $LicenseInfo:firstyear=2024&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2024, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 *
 */

#include "llluafloater.h"

#include <filesystem>
#include "llevents.h"

#include "llcheckboxctrl.h"
#include "llcombobox.h"
#include "llscrolllistctrl.h"

const std::string LISTENER_NAME("LLLuaFloater");

std::map<std::string, std::string> EVENT_LIST = 
{
    {"COMMIT_EVENT", "commit"},
    {"DOUBLE_CLICK_EVENT", "double_click"},
    {"MOUSE_ENTER_EVENT", "mouse_enter"},
    {"MOUSE_LEAVE_EVENT", "mouse_leave"},
    {"MOUSE_DOWN_EVENT", "mouse_down"},
    {"MOUSE_UP_EVENT", "mouse_up"},
    {"RIGHT_MOUSE_DOWN_EVENT", "right_mouse_down"},
    {"RIGHT_MOUSE_UP_EVENT", "right_mouse_up"},
    {"POST_BUILD_EVENT", "post_build"},
    {"CLOSE_EVENT", "floater_close"}
};

LLLuaFloater::LLLuaFloater(const LLSD &key) :
    LLFloater(key), 
    mCommandPumpName(key["reply"].asString()),
    mDispatcher("LLLuaFloater", "action")
{
    mListenerPumpName = LLUUID::generateNewID().asString();

    mBoundListener = LLEventPumps::instance().obtain(mListenerPumpName).listen(LISTENER_NAME, [this](const LLSD &event) 
    { 
        if (event.has("action")) 
        {
            mDispatcher.try_call(event);
        }
        else 
        {
            LL_WARNS("LuaFloater") << "Unknown message: " << event << LL_ENDL;
        }
        return false;
    });

    LLSD requiredParams = LLSD().with("ctrl_name", LLSD()).with("value", LLSD());
    mDispatcher.add("set_enabled", "", [this](const LLSD &event) 
    { 
        LLUICtrl *ctrl = getChild<LLUICtrl>(event["ctrl_name"].asString());
        if(ctrl) 
        {
            ctrl->setEnabled(event["value"].asBoolean());
        }
    }, requiredParams);
    mDispatcher.add("set_visible", "", [this](const LLSD &event) 
    { 
        LLUICtrl *ctrl = getChild<LLUICtrl>(event["ctrl_name"].asString());
        if(ctrl) 
        {
            ctrl->setVisible(event["value"].asBoolean());
        }
    }, requiredParams);
    mDispatcher.add("set_value", "", [this](const LLSD &event) 
    { 
        LLUICtrl *ctrl = getChild<LLUICtrl>(event["ctrl_name"].asString());
        if(ctrl) 
        {
            ctrl->setValue(event["value"]);
        }
    }, requiredParams);
    mDispatcher.add("add_list_element", "", [this](const LLSD &event) 
    { 
        LLScrollListCtrl *ctrl = getChild<LLScrollListCtrl>(event["ctrl_name"].asString());
        if(ctrl) 
        {
            ctrl->addElement(event["value"]);
        }
    }, requiredParams);

    requiredParams = LLSD().with("value", LLSD());
    mDispatcher.add("set_title", "", [this](const LLSD &event) 
    { 
        setTitle(event["value"].asString());
    }, requiredParams);

    requiredParams = LLSD().with("ctrl_name", LLSD()).with("reqid", LLSD());
    mDispatcher.add("get_value", "", [this](const LLSD &event) 
    { 
        LLUICtrl *ctrl = getChild<LLUICtrl>(event["ctrl_name"].asString());
        if(ctrl) 
        {
            LLSD response;
            response["value"] = ctrl->getValue();
            response["reqid"] = event["reqid"];
            post(response);
        }
    }, requiredParams);
}

LLLuaFloater::~LLLuaFloater()
{
    //post empty LLSD() to indicate done, in case it wasn't handled by the script after CLOSE_EVENT
    post(LLSD());
}

BOOL LLLuaFloater::postBuild() 
{
    child_list_t::const_iterator iter = getChildList()->begin();
    child_list_t::const_iterator end = getChildList()->end();
    for (; iter != end; ++iter)
    {
        LLView *view  = *iter;
        if (!view) continue;

        LLUICtrl *ctrl = dynamic_cast<LLUICtrl*>(view);
        if (ctrl)
        {
            LLSD data;
            data["ctrl_name"] = view->getName();
            data["event"] = EVENT_LIST["COMMIT_EVENT"];

            ctrl->setCommitCallback([this, data](LLUICtrl *ctrl, const LLSD &param)
            {
                LLSD event(data);
                event["value"] = ctrl->getValue();
                post(event);
            });
        }
    }

    //optional field to send additional specified events to the script
    if (mKey.has("extra_events"))
    {
        //the first value is ctrl name, the second contains array of events to send
        const LLSD &events_map = mKey["extra_events"];
        for (LLSD::map_const_iterator it = events_map.beginMap(); it != events_map.endMap(); ++it)
        {
            std::string name = (*it).first;
            LLSD data = (*it).second;
            for (LLSD::array_const_iterator events_it = data.beginArray(); events_it != data.endArray(); ++events_it)
            {
                registerCallback(name, events_it->asString());
            }
        }
    }

    //send pump name to the script after the floater is built
    post(LLSD().with("command_name", mListenerPumpName).with("event", EVENT_LIST["POST_BUILD_EVENT"]));

    return true;
}

void LLLuaFloater::onClose(bool app_quitting)
{
    post(LLSD().with("event", EVENT_LIST["CLOSE_EVENT"]));
}

void LLLuaFloater::registerCallback(const std::string &ctrl_name, const std::string &event)
{
    LLUICtrl *ctrl = getChild<LLUICtrl>(ctrl_name);
    if (!ctrl) return;

    LLSD data;
    data["ctrl_name"] = ctrl_name;
    data["event"] = event;

    auto mouse_event_cb = [this, data](LLUICtrl *ctrl, const LLSD &param) { post(data); };

    auto mouse_event_coords_cb = [this, data](LLUICtrl *ctrl, S32 x, S32 y, MASK mask) 
    { 
        LLSD event(data);
        post(event.with("x", x).with("y", y)); 
    };

    if (event == EVENT_LIST["MOUSE_ENTER_EVENT"])
    {
        ctrl->setMouseEnterCallback(mouse_event_cb);
    }
    else if (event == EVENT_LIST["MOUSE_LEAVE_EVENT"])
    {
        ctrl->setMouseLeaveCallback(mouse_event_cb);
    }
    else if (event == EVENT_LIST["MOUSE_DOWN_EVENT"])
    {
        ctrl->setMouseDownCallback(mouse_event_coords_cb);
    }
    else if (event == EVENT_LIST["MOUSE_UP_EVENT"])
    {
        ctrl->setMouseUpCallback(mouse_event_coords_cb);
    }
    else if (event == EVENT_LIST["RIGHT_MOUSE_DOWN_EVENT"])
    {
        ctrl->setRightMouseDownCallback(mouse_event_coords_cb);
    }
    else if (event == EVENT_LIST["RIGHT_MOUSE_UP_EVENT"])
    {
        ctrl->setRightMouseUpCallback(mouse_event_coords_cb);
    }
    else if (event == EVENT_LIST["DOUBLE_CLICK_EVENT"])
    {
        LLScrollListCtrl *list = dynamic_cast<LLScrollListCtrl *>(ctrl);
        if (list)
        {
            list->setDoubleClickCallback(
                [this, data, list]()
                {
                    LLSD event(data);
                    post(event.with("value", list->getCurrentID()));
                });
        }
        else 
        {
            ctrl->setDoubleClickCallback(mouse_event_coords_cb);
        }
    }
    else 
    {
        LL_WARNS("LuaFloater") << "Can't register callback for unknown event: " << event << " , control: " << ctrl_name << LL_ENDL;
    }
}

void LLLuaFloater::post(const LLSD &data)
{
    //send event data to the script
    LLEventPumps::instance().obtain(mCommandPumpName).post(data);
}

void LLLuaFloater::showLuaFloater(const LLSD &data)
{
    std::filesystem::path fs_path(data["xml_path"].asString());

    LLLuaFloater *floater = new LLLuaFloater(data);
    floater->buildFromFile(fs_path.lexically_normal().string());
    floater->openFloater(floater->getKey());
}

LLSD LLLuaFloater::getEventsData()
{
    LLSD event_data;
    for (auto &it : EVENT_LIST)
    {
        event_data[it.first] = it.second;
    }
    return event_data;
}